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  package org.apache.giraph.block_app.framework.internal;
19  
20  import java.lang.reflect.Field;
21  
22  import org.apache.giraph.block_app.framework.api.Counter;
23  import org.apache.giraph.block_app.framework.api.StatusReporter;
24  import org.apache.giraph.block_app.framework.internal.BlockMasterLogic.TimeStatsPerEvent;
25  
26  import org.apache.hadoop.mapreduce.Mapper;
27  
28  /** Utility class for Blocks Framework related counters */
29  public class BlockCounters {
30    public static final String GROUP = "Blocks Framework";
31  
32    private BlockCounters() { }
33  
34    /**
35     * Takes all fields from stage object, and puts them into counters,
36     * if possible.
37     * Only fields that are convertible to long via widening are set
38     * (i.e. long/int/short/byte)
39     */
40    public static void setStageCounters(
41        String prefix, Object stage, StatusReporter reporter) {
42      if (stage != null && reporter != null) {
43        Class<?> clazz = stage.getClass();
44  
45        while (clazz != null) {
46          Field[] fields = clazz.getDeclaredFields();
47  
48          Field.setAccessible(fields, true);
49          for (Field field : fields) {
50            try {
51              long value = field.getLong(stage);
52              reporter.getCounter(
53                  GROUP, prefix + field.getName()).setValue(value);
54  
55            // CHECKSTYLE: stop EmptyBlock - ignore any exceptions
56            } catch (IllegalArgumentException | IllegalAccessException e) {
57            }
58            // CHECKSTYLE: resume EmptyBlock
59          }
60          clazz = clazz.getSuperclass();
61        }
62      }
63    }
64  
65    public static void setMasterTimeCounter(
66        PairedPieceAndStage<?> masterPiece, long superstep,
67        long millis, StatusReporter reporter,
68        TimeStatsPerEvent timeStats) {
69      String name = masterPiece.getPiece().toString();
70      reporter.getCounter(
71          GROUP + " Master Timers",
72          String.format(
73              "In %6.1f %s (s)", superstep - 0.5, name)
74      ).setValue(millis / 1000);
75      timeStats.inc(name, millis);
76    }
77  
78    public static void setWorkerTimeCounter(
79        BlockWorkerPieces<?> workerPieces, long superstep,
80        long millis, StatusReporter reporter,
81        TimeStatsPerEvent timeStats) {
82      String name = workerPieces.toStringShort();
83      reporter.getCounter(
84          GROUP + " Worker Timers",
85          String.format("In %6d %s (s)", superstep, name)
86      ).setValue(millis / 1000);
87      timeStats.inc(name, millis);
88    }
89  
90    public static Counter getCounter(
91        Mapper.Context context, String group, String name) {
92      final org.apache.hadoop.mapreduce.Counter counter =
93          context.getCounter(group, name);
94      return new Counter() {
95        @Override
96        public void increment(long incr) {
97          counter.increment(incr);
98        }
99  
100       @Override
101       public void setValue(long value) {
102         counter.setValue(value);
103       }
104     };
105   }
106 
107   public static Counter getNoOpCounter() {
108     return new Counter() {
109       @Override
110       public void setValue(long value) { }
111 
112       @Override
113       public void increment(long incr) { }
114     };
115   }
116 }