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