This project has retired. For details please refer to its Attic page.
YourKitContext 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.hadoop.mapreduce.Mapper;
22  import org.apache.log4j.Logger;
23  
24  import com.google.common.base.Joiner;
25  import com.google.common.io.Files;
26  import com.yourkit.api.Controller;
27  import com.yourkit.api.ProfilingModes;
28  
29  import java.io.File;
30  import java.io.IOException;
31  
32  /**
33   * Convenience context for profiling. Hides away all of the exception handling.
34   * Do not instantiate directly, use only through {@link YourKitProfiler}.
35   */
36  public class YourKitContext {
37    /** Logger */
38    private static final Logger LOG = Logger.getLogger(YourKitContext.class);
39  
40    /** Joiner on path separator */
41    private static Joiner SLASH_JOINER = Joiner.on("/");
42  
43    /** The YourKit profiling object, or null if no profiling going on. */
44    private final Controller yourKitController;
45  
46    /**
47     * Constructor
48     * @param yourKitController profiling object
49     */
50    YourKitContext(Controller yourKitController) {
51      this.yourKitController = yourKitController;
52    }
53  
54    /**
55     * Capture a snapshot
56     * @param flags See {@link com.yourkit.api.ProfilingModes}
57     * @param destPath where to store snapshot
58     */
59    private void snapshot(long flags, String destPath) {
60      if (yourKitController != null) {
61        String path;
62        try {
63          path = yourKitController.captureSnapshot(flags);
64          // CHECKSTYLE: stop IllegalCatch
65        } catch (Exception e) {
66          // CHECKSTYLE: resume IllegalCatch
67          return;
68        }
69        try {
70          File destFile = new File(destPath);
71          Files.createParentDirs(destFile);
72          Files.move(new File(path), destFile);
73        } catch (IOException e) {
74          LOG.error("Failed to move YourKit snapshot file from " + path +
75              " to " + destPath, e);
76        }
77      }
78    }
79  
80    /**
81     * Capture snapshot with all recorded data including heap dump.
82     *
83     * WARNING: This is likely to be VERY slow for large jobs.
84     *
85     * @param destPath path to store snapshot file
86     */
87    public void snapshotWithMemory(String destPath) {
88      snapshot(ProfilingModes.SNAPSHOT_WITH_HEAP, destPath);
89    }
90  
91    /**
92     * Capture snapshot with all recorded data including heap dump.
93     * The snapshot file is saved in log directory, or /tmp as default.
94     *
95     * WARNING: This is likely to be VERY slow for large jobs.
96     *
97     * @param context context
98     * @param name    snapshot file name
99     */
100   public void snapshotWithMemory(Mapper.Context context, String name) {
101     snapshot(ProfilingModes.SNAPSHOT_WITH_HEAP,
102         SLASH_JOINER.join(System.getProperty("hadoop.log.dir", "/tmp"),
103             "userlogs", context.getTaskAttemptID(), name + ".snapshot"));
104   }
105 
106   /**
107    * Capture snapshot with all recorded data except for heap dump.
108    *
109    * @param destPath path to store snapshot file
110    */
111   public void snapshotCPUOnly(String destPath) {
112     snapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP, destPath);
113   }
114 
115   /**
116    * Capture snapshot with all recorded data except for heap dump.
117    * The snapshot file is saved in log directory, or /tmp as default.
118    *
119    * @param context context
120    * @param name    snapshot file name
121    */
122   public void snapshotCPUOnly(Mapper.Context context, String name) {
123     snapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP,
124         SLASH_JOINER.join(System.getProperty("hadoop.log.dir", "/tmp"),
125             "userlogs", context.getTaskAttemptID(), name + ".snapshot"));
126   }
127 
128   /**
129    * Stop recording
130    */
131   public void stop() {
132     if (yourKitController != null) {
133       try {
134         yourKitController.disableStackTelemetry();
135         LOG.info("Disabled YourKit stack telemetry");
136         // CHECKSTYLE: stop IllegalCatch
137       } catch (Exception e) {
138         // CHECKSTYLE: resume IllegalCatch
139         LOG.error("Failed to stop stack telemetry", e);
140       }
141 
142       try {
143         yourKitController.stopCPUProfiling();
144         LOG.info("Stopped Yourkit CPU profiling");
145         // CHECKSTYLE: stop IllegalCatch
146       } catch (Exception e) {
147         // CHECKSTYLE: resume IllegalCatch
148         LOG.error("Failed to stop YourKit profiling", e);
149       }
150 
151       try {
152         yourKitController.stopAllocationRecording();
153         LOG.info("Stopped Yourkit allocation recording");
154         // CHECKSTYLE: stop IllegalCatch
155       } catch (Exception e) {
156         // CHECKSTYLE: resume IllegalCatch
157         LOG.error("Failed to stop YourKit allocation recording", e);
158       }
159     }
160   }
161 }