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.Arrays;
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  /**
31   * Hadoop Counters in group "Giraph Timers" for timing things.
32   */
33  public class GiraphTimers extends HadoopCountersBase {
34    /** Counter group name for the giraph timers */
35    public static final String GROUP_NAME = "Giraph Timers";
36    /** Counter name for setup msec */
37    public static final String SETUP_MS_NAME = "Setup (ms)";
38    /** Counter name for total msec */
39    public static final String TOTAL_MS_NAME = "Total (ms)";
40    /** Counter name for shutdown msec */
41    public static final String SHUTDOWN_MS_NAME = "Shutdown (ms)";
42    /** Counter name for initialize msec */
43    public static final String INITIALIZE_MS_NAME = "Initialize (ms)";
44  
45    /** Singleton instance for everyone to use */
46    private static GiraphTimers INSTANCE;
47  
48    /** Setup time in msec */
49    private static final int SETUP_MS = 0;
50    /** Total time in msec (doesn't include initialize time) */
51    private static final int TOTAL_MS = 1;
52    /** Shutdown time in msec */
53    private static final int SHUTDOWN_MS = 2;
54    /** Total time it takes to get minimum machines */
55    private static final int INITIALIZE_MS = 3;
56    /** How many whole job counters we have */
57    private static final int NUM_COUNTERS = 4;
58  
59    /** superstep time in msec */
60    private final Map<Long, GiraphHadoopCounter> superstepMsec;
61  
62    /** Whole job counters stored in this class */
63    private final GiraphHadoopCounter[] jobCounters;
64  
65    /**
66     * Internal use only. Create using Hadoop Context
67     *
68     * @param context Hadoop Context to use.
69     */
70    private GiraphTimers(Context context) {
71      super(context, GROUP_NAME);
72      jobCounters = new GiraphHadoopCounter[NUM_COUNTERS];
73      jobCounters[SETUP_MS] = getCounter(SETUP_MS_NAME);
74      jobCounters[TOTAL_MS] = getCounter(TOTAL_MS_NAME);
75      jobCounters[SHUTDOWN_MS] = getCounter(SHUTDOWN_MS_NAME);
76      jobCounters[INITIALIZE_MS] = getCounter(INITIALIZE_MS_NAME);
77      superstepMsec = Maps.newHashMap();
78    }
79  
80    /**
81     * Instantiate with Hadoop Context.
82     *
83     * @param context Hadoop Context to use.
84     */
85    public static void init(Context context) {
86      INSTANCE = new GiraphTimers(context);
87    }
88  
89    /**
90     * Get singleton instance.
91     *
92     * @return singleton GiraphTimers instance.
93     */
94    public static GiraphTimers getInstance() {
95      return INSTANCE;
96    }
97  
98    /**
99     * Get counter for setup time in milliseconds
100    *
101    * @return Counter for setup time in milliseconds
102    */
103   public GiraphHadoopCounter getSetupMs() {
104     return jobCounters[SETUP_MS];
105   }
106 
107   /**
108    * Get counter for superstep time in milliseconds
109    *
110    * @param superstep Integer superstep number.
111    * @param computationName Name of the computation for display (may be null)
112    * @return Counter for setup time in milliseconds
113    */
114   public GiraphHadoopCounter getSuperstepMs(long superstep,
115                                             String computationName) {
116     GiraphHadoopCounter counter = superstepMsec.get(superstep);
117     if (counter == null) {
118       String counterPrefix;
119       if (superstep == -1) {
120         counterPrefix = "Input superstep";
121       } else {
122         counterPrefix = "Superstep " + superstep +
123             (computationName == null ? "" : " " + computationName);
124       }
125       counter = getCounter(counterPrefix + " (ms)");
126       superstepMsec.put(superstep, counter);
127     }
128     return counter;
129   }
130 
131   /**
132    * Get counter for total time in milliseconds (doesn't include initialize
133    * time).
134    *
135    * @return Counter for total time in milliseconds.
136    */
137   public GiraphHadoopCounter getTotalMs() {
138     return jobCounters[TOTAL_MS];
139   }
140 
141   /**
142    * Get counter for shutdown time in milliseconds.
143    *
144    * @return Counter for shutdown time in milliseconds.
145    */
146   public GiraphHadoopCounter getShutdownMs() {
147     return jobCounters[SHUTDOWN_MS];
148   }
149 
150   /**
151    * Get counter for initializing the process,
152    * having to wait for a minimum number of processes to be available
153    * before setup step
154    * @return Counter for initializing in milliseconds
155    */
156   public GiraphHadoopCounter getInitializeMs() {
157     return jobCounters[INITIALIZE_MS];
158   }
159 
160   /**
161    * Get map of superstep to msec counter.
162    *
163    * @return mapping of superstep to msec counter.
164    */
165   public Map<Long, GiraphHadoopCounter> superstepCounters() {
166     return superstepMsec;
167   }
168 
169   /**
170    * Get Iterable through job counters.
171    *
172    * @return Iterable of job counters.
173    */
174   public Iterable<GiraphHadoopCounter> jobCounters() {
175     return Arrays.asList(jobCounters);
176   }
177 
178   @Override
179   public Iterator<GiraphHadoopCounter> iterator() {
180     return Iterators.concat(jobCounters().iterator(),
181         superstepCounters().values().iterator());
182   }
183 }