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.utils;
2021import org.apache.giraph.conf.DefaultImmutableClassesGiraphConfigurable;
22import org.apache.giraph.conf.GiraphConstants;
23import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
24import org.apache.giraph.master.MasterObserver;
25import org.apache.giraph.metrics.AggregatedMetrics;
26import org.apache.giraph.partition.PartitionStats;
27import org.apache.giraph.worker.WorkerObserver;
28import org.apache.log4j.Logger;
2930import java.util.List;
3132/**33 * An observer for both worker and master that periodically checks if available34 * memory on heap is below certain threshold, and if found to be the case35 * dumps jmap -histo for the process36 */37publicclassReactiveJMapHistoDumperextends38DefaultImmutableClassesGiraphConfigurableimplements39 MasterObserver, WorkerObserver {
40/** Logger */41privatestaticfinal Logger LOG = Logger.getLogger(
42 ReactiveJMapHistoDumper.class);
43/** Size of mb */44privatestaticfinalint MB = 1024 * 1024;
4546/** How many msec to sleep between calls */47privateint sleepMillis;
48/** How many lines of output to print */49privateint linesToPrint;
50/** How much free memory is expected */51privateint minFreeMemory;
5253/** The jmap printing thread */54private Thread thread;
55/** Halt jmap thread */56privatevolatileboolean stop = false;
57/** Path to jmap*/58private String jmapPath;
5960 @Override
61publicvoid preLoad() {
62// This is called by both WorkerObserver and MasterObserver63 startSupervisorThread();
64 }
6566 @Override
67publicvoid postSave() {
68// This is called by both WorkerObserver and MasterObserver69 joinSupervisorThread();
70 }
7172 @Override
73publicvoid preApplication() {
74 }
7576 @Override
77publicvoid postApplication() {
78 }
7980/**81 * Join the supervisor thread82 */83privatevoid joinSupervisorThread() {
84 stop = true;
85try {
86 thread.join(sleepMillis + 5000);
87 } catch (InterruptedException e) {
88 LOG.error("Failed to join jmap thread");
89 }
90 }
9192/**93 * Start the supervisor thread94 */95publicvoid startSupervisorThread() {
96 stop = false;
97final Runtime runtime = Runtime.getRuntime();
98 thread = ThreadUtils.startThread(new Runnable() {
99 @Override
100publicvoid run() {
101while (!stop) {
102long potentialMemory = (runtime.maxMemory() -
103 runtime.totalMemory()) + runtime.freeMemory();
104if (potentialMemory / MB < minFreeMemory) {
105 JMap.heapHistogramDump(linesToPrint, jmapPath);
106 }
107 ThreadUtils.trySleep(sleepMillis);
108 }
109 }
110 }, "ReactiveJMapHistoDumperSupervisorThread");
111 }
112113 @Override
114publicvoid preSuperstep(long superstep) { }
115116 @Override
117publicvoid postSuperstep(long superstep) { }
118119 @Override
120publicvoid superstepMetricsUpdate(long superstep,
121AggregatedMetrics aggregatedMetrics,
122 List<PartitionStats> partitionStatsList) { }
123124 @Override
125publicvoid applicationFailed(Exception e) { }
126127 @Override
128publicvoid setConf(ImmutableClassesGiraphConfiguration configuration) {
129 sleepMillis = GiraphConstants.JMAP_SLEEP_MILLIS.get(configuration);
130 linesToPrint = GiraphConstants.JMAP_PRINT_LINES.get(configuration);
131 minFreeMemory = GiraphConstants.MIN_FREE_MBS_ON_HEAP.get(configuration);
132 jmapPath = GiraphConstants.JMAP_PATH.get(configuration);
133 }
134 }