Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
YourKitContext |
|
| 2.142857142857143;2.143 |
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 | 0 | private static final Logger LOG = Logger.getLogger(YourKitContext.class); |
39 | ||
40 | /** Joiner on path separator */ | |
41 | 0 | 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 | 0 | YourKitContext(Controller yourKitController) { |
51 | 0 | this.yourKitController = yourKitController; |
52 | 0 | } |
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 | 0 | if (yourKitController != null) { |
61 | String path; | |
62 | try { | |
63 | 0 | path = yourKitController.captureSnapshot(flags); |
64 | // CHECKSTYLE: stop IllegalCatch | |
65 | 0 | } catch (Exception e) { |
66 | // CHECKSTYLE: resume IllegalCatch | |
67 | 0 | return; |
68 | 0 | } |
69 | try { | |
70 | 0 | File destFile = new File(destPath); |
71 | 0 | Files.createParentDirs(destFile); |
72 | 0 | Files.move(new File(path), destFile); |
73 | 0 | } catch (IOException e) { |
74 | 0 | LOG.error("Failed to move YourKit snapshot file from " + path + |
75 | " to " + destPath, e); | |
76 | 0 | } |
77 | } | |
78 | 0 | } |
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 | 0 | snapshot(ProfilingModes.SNAPSHOT_WITH_HEAP, destPath); |
89 | 0 | } |
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 | 0 | snapshot(ProfilingModes.SNAPSHOT_WITH_HEAP, |
102 | 0 | SLASH_JOINER.join(System.getProperty("hadoop.log.dir", "/tmp"), |
103 | 0 | "userlogs", context.getTaskAttemptID(), name + ".snapshot")); |
104 | 0 | } |
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 | 0 | snapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP, destPath); |
113 | 0 | } |
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 | 0 | snapshot(ProfilingModes.SNAPSHOT_WITHOUT_HEAP, |
124 | 0 | SLASH_JOINER.join(System.getProperty("hadoop.log.dir", "/tmp"), |
125 | 0 | "userlogs", context.getTaskAttemptID(), name + ".snapshot")); |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * Stop recording | |
130 | */ | |
131 | public void stop() { | |
132 | 0 | if (yourKitController != null) { |
133 | try { | |
134 | 0 | yourKitController.disableStackTelemetry(); |
135 | 0 | LOG.info("Disabled YourKit stack telemetry"); |
136 | // CHECKSTYLE: stop IllegalCatch | |
137 | 0 | } catch (Exception e) { |
138 | // CHECKSTYLE: resume IllegalCatch | |
139 | 0 | LOG.error("Failed to stop stack telemetry", e); |
140 | 0 | } |
141 | ||
142 | try { | |
143 | 0 | yourKitController.stopCPUProfiling(); |
144 | 0 | LOG.info("Stopped Yourkit CPU profiling"); |
145 | // CHECKSTYLE: stop IllegalCatch | |
146 | 0 | } catch (Exception e) { |
147 | // CHECKSTYLE: resume IllegalCatch | |
148 | 0 | LOG.error("Failed to stop YourKit profiling", e); |
149 | 0 | } |
150 | ||
151 | try { | |
152 | 0 | yourKitController.stopAllocationRecording(); |
153 | 0 | LOG.info("Stopped Yourkit allocation recording"); |
154 | // CHECKSTYLE: stop IllegalCatch | |
155 | 0 | } catch (Exception e) { |
156 | // CHECKSTYLE: resume IllegalCatch | |
157 | 0 | LOG.error("Failed to stop YourKit allocation recording", e); |
158 | 0 | } |
159 | } | |
160 | 0 | } |
161 | } |