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.hadoop.mapreduce.Mapper;
22import org.apache.log4j.Logger;
2324import com.google.common.base.Joiner;
25import com.google.common.io.Files;
26import com.yourkit.api.Controller;
27import com.yourkit.api.ProfilingModes;
2829import java.io.File;
30import java.io.IOException;
3132/**33 * Convenience context for profiling. Hides away all of the exception handling.34 * Do not instantiate directly, use only through {@link YourKitProfiler}.35 */36publicclassYourKitContext {
37/** Logger */38privatestaticfinal Logger LOG = Logger.getLogger(YourKitContext.class);
3940/** Joiner on path separator */41privatestatic Joiner SLASH_JOINER = Joiner.on("/");
4243/** The YourKit profiling object, or null if no profiling going on. */44privatefinal Controller yourKitController;
4546/**47 * Constructor48 * @param yourKitController profiling object49 */50YourKitContext(Controller yourKitController) {
51this.yourKitController = yourKitController;
52 }
5354/**55 * Capture a snapshot56 * @param flags See {@link com.yourkit.api.ProfilingModes}57 * @param destPath where to store snapshot58 */59privatevoid snapshot(long flags, String destPath) {
60if (yourKitController != null) {
61 String path;
62try {
63 path = yourKitController.captureSnapshot(flags);
64// CHECKSTYLE: stop IllegalCatch65 } catch (Exception e) {
66// CHECKSTYLE: resume IllegalCatch67return;
68 }
69try {
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 }
7980/**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 file86 */87publicvoid snapshotWithMemory(String destPath) {
88 snapshot(ProfilingModes.SNAPSHOT_WITH_HEAP, destPath);
89 }
9091/**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 context98 * @param name snapshot file name99 */100publicvoid 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 }
105106/**107 * Capture snapshot with all recorded data except for heap dump.108 *109 * @param destPath path to store snapshot file110 */111publicvoid snapshotCPUOnly(String destPath) {
112 snapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP, destPath);
113 }
114115/**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 context120 * @param name snapshot file name121 */122publicvoid 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 }
127128/**129 * Stop recording130 */131publicvoid stop() {
132if (yourKitController != null) {
133try {
134 yourKitController.disableStackTelemetry();
135 LOG.info("Disabled YourKit stack telemetry");
136// CHECKSTYLE: stop IllegalCatch137 } catch (Exception e) {
138// CHECKSTYLE: resume IllegalCatch139 LOG.error("Failed to stop stack telemetry", e);
140 }
141142try {
143 yourKitController.stopCPUProfiling();
144 LOG.info("Stopped Yourkit CPU profiling");
145// CHECKSTYLE: stop IllegalCatch146 } catch (Exception e) {
147// CHECKSTYLE: resume IllegalCatch148 LOG.error("Failed to stop YourKit profiling", e);
149 }
150151try {
152 yourKitController.stopAllocationRecording();
153 LOG.info("Stopped Yourkit allocation recording");
154// CHECKSTYLE: stop IllegalCatch155 } catch (Exception e) {
156// CHECKSTYLE: resume IllegalCatch157 LOG.error("Failed to stop YourKit allocation recording", e);
158 }
159 }
160 }
161 }