This project has retired. For details please refer to its Attic page.
GiraphTimers xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.counters;
20  
21  import org.apache.hadoop.mapreduce.Mapper.Context;
22  
23  import com.google.common.collect.Iterators;
24  import com.google.common.collect.Maps;
25  
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * Hadoop Counters in group "Giraph Timers" for timing things.
34   */
35  public class GiraphTimers extends HadoopCountersBase {
36    /** Counter group name for the giraph timers */
37    public static final String GROUP_NAME = "Giraph Timers";
38    /** Counter name for setup msec */
39    public static final String SETUP_MS_NAME = "Setup (ms)";
40    /** Counter name for total msec */
41    public static final String TOTAL_MS_NAME = "Total (ms)";
42    /** Counter name for shutdown msec */
43    public static final String SHUTDOWN_MS_NAME = "Shutdown (ms)";
44    /** Counter name for initialize msec */
45    public static final String INITIALIZE_MS_NAME = "Initialize (ms)";
46  
47    /** Singleton instance for everyone to use */
48    private static GiraphTimers INSTANCE;
49  
50    /** Setup time in msec */
51    private static final int SETUP_MS = 0;
52    /** Total time in msec (doesn't include initialize time) */
53    private static final int TOTAL_MS = 1;
54    /** Shutdown time in msec */
55    private static final int SHUTDOWN_MS = 2;
56    /** Total time it takes to get minimum machines */
57    private static final int INITIALIZE_MS = 3;
58    /** How many whole job counters we have */
59    private static final int NUM_COUNTERS = 4;
60  
61    /** superstep time in msec */
62    private final Map<Long, GiraphHadoopCounter> superstepMsec;
63  
64    /** Whole job counters stored in this class */
65    private final GiraphHadoopCounter[] jobCounters;
66  
67    /**
68     * Internal use only. Create using Hadoop Context
69     *
70     * @param context Hadoop Context to use.
71     */
72    private GiraphTimers(Context context) {
73      super(context, GROUP_NAME);
74      jobCounters = new GiraphHadoopCounter[NUM_COUNTERS];
75      jobCounters[SETUP_MS] = getCounter(SETUP_MS_NAME);
76      jobCounters[TOTAL_MS] = getCounter(TOTAL_MS_NAME);
77      jobCounters[SHUTDOWN_MS] = getCounter(SHUTDOWN_MS_NAME);
78      jobCounters[INITIALIZE_MS] = getCounter(INITIALIZE_MS_NAME);
79      superstepMsec = Maps.newHashMap();
80    }
81  
82    /**
83     * Instantiate with Hadoop Context.
84     *
85     * @param context Hadoop Context to use.
86     */
87    public static void init(Context context) {
88      INSTANCE = new GiraphTimers(context);
89    }
90  
91    /**
92     * Get singleton instance.
93     *
94     * @return singleton GiraphTimers instance.
95     */
96    public static GiraphTimers getInstance() {
97      return INSTANCE;
98    }
99  
100   /**
101    * Get counter for setup time in milliseconds
102    *
103    * @return Counter for setup time in milliseconds
104    */
105   public GiraphHadoopCounter getSetupMs() {
106     return jobCounters[SETUP_MS];
107   }
108 
109   /**
110    * Get counter for superstep time in milliseconds
111    *
112    * @param superstep Integer superstep number.
113    * @param computationName Name of the computation for display (may be null)
114    * @return Counter for setup time in milliseconds
115    */
116   public GiraphHadoopCounter getSuperstepMs(long superstep,
117                                             String computationName) {
118     GiraphHadoopCounter counter = superstepMsec.get(superstep);
119     if (counter == null) {
120       String counterPrefix;
121       if (superstep == -1) {
122         counterPrefix = "Input superstep";
123       } else {
124         counterPrefix = "Superstep " + superstep +
125             (computationName == null ? "" : " " + computationName);
126       }
127       counter = getCounter(counterPrefix + " (ms)");
128       superstepMsec.put(superstep, counter);
129     }
130     return counter;
131   }
132 
133   /**
134    * Get counter for total time in milliseconds (doesn't include initialize
135    * time).
136    *
137    * @return Counter for total time in milliseconds.
138    */
139   public GiraphHadoopCounter getTotalMs() {
140     return jobCounters[TOTAL_MS];
141   }
142 
143   /**
144    * Get counter for shutdown time in milliseconds.
145    *
146    * @return Counter for shutdown time in milliseconds.
147    */
148   public GiraphHadoopCounter getShutdownMs() {
149     return jobCounters[SHUTDOWN_MS];
150   }
151 
152   /**
153    * Get counter for initializing the process,
154    * having to wait for a minimum number of processes to be available
155    * before setup step
156    * @return Counter for initializing in milliseconds
157    */
158   public GiraphHadoopCounter getInitializeMs() {
159     return jobCounters[INITIALIZE_MS];
160   }
161 
162   /**
163    * Get map of superstep to msec counter.
164    *
165    * @return mapping of superstep to msec counter.
166    */
167   public Map<Long, GiraphHadoopCounter> superstepCounters() {
168     return superstepMsec;
169   }
170 
171   /**
172    * Get Iterable through job counters.
173    *
174    * @return Iterable of job counters.
175    */
176   public Iterable<GiraphHadoopCounter> jobCounters() {
177     return Arrays.asList(jobCounters);
178   }
179 
180   @Override
181   public Iterator<GiraphHadoopCounter> iterator() {
182     return Iterators.concat(jobCounters().iterator(),
183         superstepCounters().values().iterator());
184   }
185 
186   /**
187    * Get a map of counter names and values for the given superstep
188    * Counters include Setup, Initialise, Shutdown, Total, and time for
189    * the given superstep
190    * @param superstep superstep for which to fetch the GiraphTimer
191    * @return Map of counter names and values
192    */
193   public List<CustomCounter> getCounterList(long superstep) {
194     List<CustomCounter> countersList = new ArrayList<>();
195     for (GiraphHadoopCounter counter: jobCounters) {
196       CustomCounter customCounter = new CustomCounter(
197               GROUP_NAME, counter.getName(),
198               CustomCounter.Aggregation.SUM, counter.getValue());
199       countersList.add(customCounter);
200     }
201     GiraphHadoopCounter giraphHadoopCounter = superstepMsec.get(superstep);
202     if (giraphHadoopCounter != null) {
203       CustomCounter customCounter = new CustomCounter(
204               GROUP_NAME, giraphHadoopCounter.getName(),
205               CustomCounter.Aggregation.SUM,
206               giraphHadoopCounter.getValue());
207       countersList.add(customCounter);
208     }
209     return countersList;
210   }
211 }