This project has retired. For details please refer to its
Attic page.
BlockCounters xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.giraph.counters.CustomCounter;
27 import org.apache.giraph.counters.CustomCounters;
28 import org.apache.hadoop.mapreduce.Mapper;
29
30
31 public class BlockCounters {
32 public static final String GROUP = "Blocks Framework";
33
34 private BlockCounters() { }
35
36
37
38
39
40
41
42 public static void setStageCounters(
43 String prefix, Object stage, StatusReporter reporter) {
44 if (stage != null && reporter != null) {
45 Class<?> clazz = stage.getClass();
46
47 while (clazz != null) {
48 Field[] fields = clazz.getDeclaredFields();
49
50 Field.setAccessible(fields, true);
51 for (Field field : fields) {
52 try {
53 long value = field.getLong(stage);
54 String counterName = prefix + field.getName();
55 CustomCounters.addCustomCounter(GROUP, counterName,
56 CustomCounter.Aggregation.SUM);
57 reporter.getCounter(
58 GROUP, prefix + field.getName()).setValue(value);
59
60
61 } catch (IllegalArgumentException | IllegalAccessException e) {
62 }
63
64 }
65 clazz = clazz.getSuperclass();
66 }
67 }
68 }
69
70 public static void setMasterTimeCounter(
71 PairedPieceAndStage<?> masterPiece, long superstep,
72 long millis, StatusReporter reporter,
73 TimeStatsPerEvent timeStats) {
74 String name = masterPiece.getPiece().toString();
75 String groupName = GROUP + " Master Timers";
76 String counterName = String.format(
77 "In %6.1f %s (s)", superstep - 0.5, name);
78 CustomCounters.addCustomCounter(groupName, counterName,
79 CustomCounter.Aggregation.SUM);
80 reporter.getCounter(groupName, counterName).setValue(millis / 1000);
81 timeStats.inc(name, millis);
82 }
83
84 public static void setWorkerTimeCounter(
85 BlockWorkerPieces<?> workerPieces, long superstep,
86 long millis, StatusReporter reporter,
87 TimeStatsPerEvent timeStats) {
88 String name = workerPieces.toStringShort();
89 String groupName = GROUP + " Worker Timers";
90 String counterName = String.format("In %6d %s (s)", superstep, name);
91 CustomCounters.addCustomCounter(groupName, counterName,
92 CustomCounter.Aggregation.SUM);
93 reporter.getCounter(groupName, counterName).setValue(millis / 1000);
94 timeStats.inc(name, millis);
95 }
96
97 public static Counter getCounter(
98 Mapper.Context context, String group, String name) {
99 final org.apache.hadoop.mapreduce.Counter counter =
100 context.getCounter(group, name);
101 return new Counter() {
102 @Override
103 public void increment(long incr) {
104 counter.increment(incr);
105 }
106
107 @Override
108 public void setValue(long value) {
109 counter.setValue(value);
110 }
111 };
112 }
113
114 public static Counter getNoOpCounter() {
115 return new Counter() {
116 @Override
117 public void setValue(long value) { }
118
119 @Override
120 public void increment(long incr) { }
121 };
122 }
123 }