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.StatusReporter;
23  import org.apache.giraph.block_app.framework.internal.BlockMasterLogic.TimeStatsPerEvent;
24  
25  /** Utility class for Blocks Framework related counters */
26  public class BlockCounters {
27    public static final String GROUP = "Blocks Framework";
28  
29    private BlockCounters() { }
30  
31    /**
32     * Takes all fields from stage object, and puts them into counters,
33     * if possible.
34     * Only fields that are convertible to long via widening are set
35     * (i.e. long/int/short/byte)
36     */
37    public static void setStageCounters(
38        String prefix, Object stage, StatusReporter reporter) {
39      if (stage != null && reporter != null) {
40        Class<?> clazz = stage.getClass();
41  
42        while (clazz != null) {
43          Field[] fields = clazz.getDeclaredFields();
44  
45          Field.setAccessible(fields, true);
46          for (Field field : fields) {
47            try {
48              long value = field.getLong(stage);
49              reporter.getCounter(
50                  GROUP, prefix + field.getName()).setValue(value);
51  
52            // CHECKSTYLE: stop EmptyBlock - ignore any exceptions
53            } catch (IllegalArgumentException | IllegalAccessException e) {
54            }
55            // CHECKSTYLE: resume EmptyBlock
56          }
57          clazz = clazz.getSuperclass();
58        }
59      }
60    }
61  
62    public static void setMasterTimeCounter(
63        PairedPieceAndStage<?> masterPiece, long superstep,
64        long millis, StatusReporter reporter,
65        TimeStatsPerEvent timeStats) {
66      String name = masterPiece.getPiece().toString();
67      reporter.getCounter(
68          GROUP + " Master Timers",
69          String.format(
70              "In %6.1f %s (s)", superstep - 0.5, name)
71      ).setValue(millis / 1000);
72      timeStats.inc(name, millis);
73    }
74  
75    public static void setWorkerTimeCounter(
76        BlockWorkerPieces<?> workerPieces, long superstep,
77        long millis, StatusReporter reporter,
78        TimeStatsPerEvent timeStats) {
79      String name = workerPieces.toStringShort();
80      reporter.getCounter(
81          GROUP + " Worker Timers",
82          String.format("In %6d %s (s)", superstep, name)
83      ).setValue(millis / 1000);
84      timeStats.inc(name, millis);
85    }
86  }