1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.giraph.counters;
2021import org.apache.hadoop.mapreduce.Mapper.Context;
2223import com.google.common.collect.Iterators;
24import com.google.common.collect.Maps;
2526import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.Iterator;
29import java.util.List;
30import java.util.Map;
3132/**33 * Hadoop Counters in group "Giraph Timers" for timing things.34 */35publicclassGiraphTimersextendsHadoopCountersBase {
36/** Counter group name for the giraph timers */37publicstaticfinal String GROUP_NAME = "Giraph Timers";
38/** Counter name for setup msec */39publicstaticfinal String SETUP_MS_NAME = "Setup (ms)";
40/** Counter name for total msec */41publicstaticfinal String TOTAL_MS_NAME = "Total (ms)";
42/** Counter name for shutdown msec */43publicstaticfinal String SHUTDOWN_MS_NAME = "Shutdown (ms)";
44/** Counter name for initialize msec */45publicstaticfinal String INITIALIZE_MS_NAME = "Initialize (ms)";
4647/** Singleton instance for everyone to use */48privatestaticGiraphTimers INSTANCE;
4950/** Setup time in msec */51privatestaticfinalint SETUP_MS = 0;
52/** Total time in msec (doesn't include initialize time) */53privatestaticfinalint TOTAL_MS = 1;
54/** Shutdown time in msec */55privatestaticfinalint SHUTDOWN_MS = 2;
56/** Total time it takes to get minimum machines */57privatestaticfinalint INITIALIZE_MS = 3;
58/** How many whole job counters we have */59privatestaticfinalint NUM_COUNTERS = 4;
6061/** superstep time in msec */62privatefinal Map<Long, GiraphHadoopCounter> superstepMsec;
6364/** Whole job counters stored in this class */65privatefinalGiraphHadoopCounter[] jobCounters;
6667/**68 * Internal use only. Create using Hadoop Context69 *70 * @param context Hadoop Context to use.71 */72privateGiraphTimers(Context context) {
73super(context, GROUP_NAME);
74 jobCounters = newGiraphHadoopCounter[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 }
8182/**83 * Instantiate with Hadoop Context.84 *85 * @param context Hadoop Context to use.86 */87publicstaticvoid init(Context context) {
88 INSTANCE = newGiraphTimers(context);
89 }
9091/**92 * Get singleton instance.93 *94 * @return singleton GiraphTimers instance.95 */96publicstaticGiraphTimers getInstance() {
97return INSTANCE;
98 }
99100/**101 * Get counter for setup time in milliseconds102 *103 * @return Counter for setup time in milliseconds104 */105publicGiraphHadoopCounter getSetupMs() {
106return jobCounters[SETUP_MS];
107 }
108109/**110 * Get counter for superstep time in milliseconds111 *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 milliseconds115 */116publicGiraphHadoopCounter getSuperstepMs(long superstep,
117 String computationName) {
118GiraphHadoopCounter counter = superstepMsec.get(superstep);
119if (counter == null) {
120 String counterPrefix;
121if (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 }
130return counter;
131 }
132133/**134 * Get counter for total time in milliseconds (doesn't include initialize135 * time).136 *137 * @return Counter for total time in milliseconds.138 */139publicGiraphHadoopCounter getTotalMs() {
140return jobCounters[TOTAL_MS];
141 }
142143/**144 * Get counter for shutdown time in milliseconds.145 *146 * @return Counter for shutdown time in milliseconds.147 */148publicGiraphHadoopCounter getShutdownMs() {
149return jobCounters[SHUTDOWN_MS];
150 }
151152/**153 * Get counter for initializing the process,154 * having to wait for a minimum number of processes to be available155 * before setup step156 * @return Counter for initializing in milliseconds157 */158publicGiraphHadoopCounter getInitializeMs() {
159return jobCounters[INITIALIZE_MS];
160 }
161162/**163 * Get map of superstep to msec counter.164 *165 * @return mapping of superstep to msec counter.166 */167public Map<Long, GiraphHadoopCounter> superstepCounters() {
168return superstepMsec;
169 }
170171/**172 * Get Iterable through job counters.173 *174 * @return Iterable of job counters.175 */176public Iterable<GiraphHadoopCounter> jobCounters() {
177return Arrays.asList(jobCounters);
178 }
179180 @Override
181public Iterator<GiraphHadoopCounter> iterator() {
182return Iterators.concat(jobCounters().iterator(),
183 superstepCounters().values().iterator());
184 }
185186/**187 * Get a map of counter names and values for the given superstep188 * Counters include Setup, Initialise, Shutdown, Total, and time for189 * the given superstep190 * @param superstep superstep for which to fetch the GiraphTimer191 * @return Map of counter names and values192 */193public List<CustomCounter> getCounterList(long superstep) {
194 List<CustomCounter> countersList = new ArrayList<>();
195for (GiraphHadoopCounter counter: jobCounters) {
196CustomCounter customCounter = newCustomCounter(
197 GROUP_NAME, counter.getName(),
198 CustomCounter.Aggregation.SUM, counter.getValue());
199 countersList.add(customCounter);
200 }
201GiraphHadoopCounter giraphHadoopCounter = superstepMsec.get(superstep);
202if (giraphHadoopCounter != null) {
203CustomCounter customCounter = newCustomCounter(
204 GROUP_NAME, giraphHadoopCounter.getName(),
205 CustomCounter.Aggregation.SUM,
206 giraphHadoopCounter.getValue());
207 countersList.add(customCounter);
208 }
209return countersList;
210 }
211 }