This project has retired. For details please refer to its Attic page.
JMapHistoDumper xref
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  
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.metrics.AggregatedMetrics;
25  import org.apache.giraph.partition.PartitionStats;
26  import org.apache.giraph.worker.WorkerObserver;
27  import org.apache.log4j.Logger;
28  
29  import java.util.List;
30  
31  /**
32   * An observer for both worker and master that periodically dumps the memory
33   * usage using jmap tool.
34   */
35  public class JMapHistoDumper implements MasterObserver, WorkerObserver {
36    /** Logger */
37    private static final Logger LOG = Logger.getLogger(JMapHistoDumper.class);
38  
39    /** How many msec to sleep between calls */
40    private int sleepMillis;
41    /** How many lines of output to print */
42    private int linesToPrint;
43    /** Should only print live objects */
44    private boolean liveObjectsOnly;
45  
46    /** The jmap printing thread */
47    private Thread thread;
48    /** Halt jmap thread */
49    private volatile boolean stop = false;
50    /** Path to jmap*/
51    private String jmapPath;
52  
53    @Override
54    public void preLoad() {
55      // This is called by both WorkerObserver and MasterObserver
56      startJMapThread();
57    }
58  
59    @Override
60    public void postSave() {
61      // This is called by both WorkerObserver and MasterObserver
62      joinJMapThread();
63    }
64  
65    @Override
66    public void preApplication() {
67    }
68  
69    @Override
70    public void postApplication() {
71    }
72  
73    /**
74     * Join the jmap thread
75     */
76    private void joinJMapThread() {
77      stop = true;
78      try {
79        thread.interrupt();
80        thread.join(sleepMillis + 5000);
81      } catch (InterruptedException e) {
82        LOG.error("Failed to join jmap thread");
83      }
84    }
85  
86    /**
87     * Start the jmap thread
88     */
89    public void startJMapThread() {
90      stop = false;
91      thread = ThreadUtils.startThread(new Runnable() {
92        @Override
93        public void run() {
94          while (!stop) {
95            JMap.heapHistogramDump(linesToPrint, liveObjectsOnly, jmapPath);
96            ThreadUtils.trySleep(sleepMillis);
97          }
98        }
99      }, "jmap-dumper");
100   }
101 
102   @Override
103   public void preSuperstep(long superstep) { }
104 
105   @Override
106   public void postSuperstep(long superstep) { }
107 
108   @Override
109   public void superstepMetricsUpdate(long superstep,
110       AggregatedMetrics aggregatedMetrics,
111       List<PartitionStats> partitionStatsList) { }
112 
113   @Override
114   public void applicationFailed(Exception e) { }
115 
116   @Override
117   public void setConf(ImmutableClassesGiraphConfiguration configuration) {
118     sleepMillis = GiraphConstants.JMAP_SLEEP_MILLIS.get(configuration);
119     linesToPrint = GiraphConstants.JMAP_PRINT_LINES.get(configuration);
120     liveObjectsOnly = GiraphConstants.JMAP_LIVE_ONLY.get(configuration);
121     jmapPath = GiraphConstants.JMAP_PATH.get(configuration);
122   }
123 
124   @Override
125   public ImmutableClassesGiraphConfiguration getConf() {
126     return null;
127   }
128 }