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.metrics;
2021import java.io.PrintStream;
2223import org.apache.giraph.bsp.BspService;
24import org.apache.giraph.conf.GiraphConfiguration;
2526import com.yammer.metrics.core.Histogram;
27import com.yammer.metrics.core.Metric;
28import com.yammer.metrics.core.MetricName;
29import com.yammer.metrics.core.MetricPredicate;
30import com.yammer.metrics.core.MetricsRegistry;
31import com.yammer.metrics.reporting.ConsoleReporter;
32import com.yammer.metrics.reporting.JmxReporter;
3334/**35 * Wrapper around MetricsRegistry for per-superstep metrics.36 */37publicclassSuperstepMetricsRegistryextendsGiraphMetricsRegistry {
38/** Number of superstep to use for group of metrics created */39privatelong superstep = BspService.INPUT_SUPERSTEP;
4041/**42 * Constructor43 * @param registry {@link com.yammer.metrics.core.MetricsRegistry} to use44 * @param reporter {@link com.yammer.metrics.reporting.JmxReporter} to use45 * @param groupName String grouping for metrics46 * @param type String type name for metrics47 */48protectedSuperstepMetricsRegistry(MetricsRegistry registry,
49 JmxReporter reporter, String groupName, String type) {
50super(registry, reporter, groupName, type);
51 }
5253/**54 * Create with Hadoop Configuration and superstep number.55 *56 * @param conf Hadoop Configuration to use.57 * @param superstep number of superstep to use as group for metrics.58 * @return new metrics registry59 */60publicstaticSuperstepMetricsRegistry create(GiraphConfiguration conf,
61long superstep) {
62if (conf.metricsEnabled()) {
63 MetricsRegistry registry = new MetricsRegistry();
64SuperstepMetricsRegistry superstepMetrics = newSuperstepMetricsRegistry(
65 registry, new JmxReporter(registry),
66"giraph.superstep", String.valueOf(superstep));
67 superstepMetrics.superstep = superstep;
68return superstepMetrics;
69 } else {
70return createFake();
71 }
72 }
7374/**75 * Create an empty registry76 * @return fake metrics registry that returns no op metrics77 */78publicstaticSuperstepMetricsRegistry createFake() {
79returnnewSuperstepMetricsRegistry(newNoOpMetricsRegistry(), null,
80"", "");
81 }
8283/**84 * Get superstep stored here85 * @return long superstep86 */87publiclong getSuperstep() {
88return superstep;
89 }
9091/**92 * Set superstep number used. Internally sets the group for metrics created.93 *94 * @param superstep long number of superstep to use.95 */96publicvoid setSuperstep(long superstep) {
97super.setType(String.valueOf(superstep));
98this.superstep = superstep;
99 }
100101/**102 * Print human readable summary of superstep metrics.103 *104 * @param out PrintStream to write to.105 */106publicvoid printSummary(PrintStream out) {
107newWorkerSuperstepMetrics().readFromRegistry().print(superstep, out);
108 out.println("");
109 MetricPredicate superstepFilter = new MetricPredicate() {
110 @Override
111publicboolean matches(MetricName name, Metric metric) {
112return name.getType().equals(getType());
113 }
114 };
115new ConsoleReporter(getInternalRegistry(), out, superstepFilter) {
116 @Override
117publicvoid processHistogram(MetricName name, Histogram histogram,
118 PrintStream stream) {
119 stream.printf(" sum = %,2.2f%n", histogram.sum());
120super.processHistogram(name, histogram, stream);
121 stream.printf(" count = %d%n", histogram.count());
122 }
123 } .run();
124 }
125 }