GIRAPH-1036: Allow mappers to fail early on exceptions
[giraph.git] / giraph-core / src / main / java / org / apache / giraph / utils / JMapHistoDumper.java
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
19 package org.apache.giraph.utils;
20
21 import org.apache.giraph.conf.GiraphConstants;
22 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
23 import org.apache.giraph.master.MasterObserver;
24 import org.apache.giraph.worker.WorkerObserver;
25 import org.apache.log4j.Logger;
26
27 /**
28 * An observer for both worker and master that periodically dumps the memory
29 * usage using jmap tool.
30 */
31 public class JMapHistoDumper implements MasterObserver, WorkerObserver {
32 /** Logger */
33 private static final Logger LOG = Logger.getLogger(JMapHistoDumper.class);
34
35 /** How many msec to sleep between calls */
36 private int sleepMillis;
37 /** How many lines of output to print */
38 private int linesToPrint;
39 /** Should only print live objects */
40 private boolean liveObjectsOnly;
41
42 /** The jmap printing thread */
43 private Thread thread;
44 /** Halt jmap thread */
45 private boolean stop = false;
46
47 @Override
48 public void preLoad() {
49 // This is called by both WorkerObserver and MasterObserver
50 startJMapThread();
51 }
52
53 @Override
54 public void postSave() {
55 // This is called by both WorkerObserver and MasterObserver
56 joinJMapThread();
57 }
58
59 @Override
60 public void preApplication() {
61 }
62
63 @Override
64 public void postApplication() {
65 }
66
67 /**
68 * Join the jmap thread
69 */
70 private void joinJMapThread() {
71 stop = true;
72 try {
73 thread.join(sleepMillis + 5000);
74 } catch (InterruptedException e) {
75 LOG.error("Failed to join jmap thread");
76 }
77 }
78
79 /**
80 * Start the jmap thread
81 */
82 public void startJMapThread() {
83 stop = false;
84 thread = new Thread(new Runnable() {
85 @Override
86 public void run() {
87 while (!stop) {
88 JMap.heapHistogramDump(linesToPrint, liveObjectsOnly);
89 try {
90 Thread.sleep(sleepMillis);
91 } catch (InterruptedException e) {
92 LOG.warn("JMap histogram sleep interrupted", e);
93 }
94 }
95 }
96 });
97 thread.setDaemon(true);
98 thread.start();
99 }
100
101 @Override
102 public void preSuperstep(long superstep) { }
103
104 @Override
105 public void postSuperstep(long superstep) { }
106
107 @Override
108 public void applicationFailed(Exception e) { }
109
110 @Override
111 public void setConf(ImmutableClassesGiraphConfiguration configuration) {
112 sleepMillis = GiraphConstants.JMAP_SLEEP_MILLIS.get(configuration);
113 linesToPrint = GiraphConstants.JMAP_PRINT_LINES.get(configuration);
114 liveObjectsOnly = GiraphConstants.JMAP_LIVE_ONLY.get(configuration);
115 }
116
117 @Override
118 public ImmutableClassesGiraphConfiguration getConf() {
119 return null;
120 }
121 }