|
1 /* |
|
2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.jfr.internal; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.nio.file.Paths; |
|
30 import java.security.AccessControlContext; |
|
31 import java.security.AccessController; |
|
32 import java.security.PrivilegedActionException; |
|
33 import java.security.PrivilegedExceptionAction; |
|
34 import java.time.LocalDateTime; |
|
35 |
|
36 import jdk.jfr.RecordingState; |
|
37 |
|
38 /** |
|
39 * Class responsible for dumping recordings on exit |
|
40 * |
|
41 */ |
|
42 final class ShutdownHook implements Runnable { |
|
43 private final PlatformRecorder recorder; |
|
44 Object tlabDummyObject; |
|
45 |
|
46 ShutdownHook(PlatformRecorder recorder) { |
|
47 this.recorder = recorder; |
|
48 } |
|
49 |
|
50 @Override |
|
51 public void run() { |
|
52 // this allocation is done in order to fetch a new TLAB before |
|
53 // starting any "real" operations. In low memory situations, |
|
54 // we would like to take an OOM as early as possible. |
|
55 tlabDummyObject = new Object(); |
|
56 |
|
57 for (PlatformRecording recording : recorder.getRecordings()) { |
|
58 if (recording.getDumpOnExit() && recording.getState() == RecordingState.RUNNING) { |
|
59 dump(recording); |
|
60 } |
|
61 } |
|
62 recorder.destroy(); |
|
63 } |
|
64 |
|
65 private void dump(PlatformRecording recording) { |
|
66 try { |
|
67 WriteableUserPath dest = recording.getDestination(); |
|
68 if (dest == null) { |
|
69 dest = makeDumpOnExitPath(recording); |
|
70 } |
|
71 if (dest != null) { |
|
72 recording.stop("Dump on exit", dest); |
|
73 } |
|
74 } catch (Exception e) { |
|
75 Logger.log(LogTag.JFR, LogLevel.DEBUG, () -> "Could not dump recording " + recording.getName() + " on exit."); |
|
76 } |
|
77 } |
|
78 |
|
79 private WriteableUserPath makeDumpOnExitPath(PlatformRecording recording) { |
|
80 try { |
|
81 String pid = JVM.getJVM().getPid(); |
|
82 String dfText = Repository.REPO_DATE_FORMAT.format(LocalDateTime.now()); |
|
83 String name = "hotspot-" + "pid-" + pid + "-id-" + recording.getId() + "-" + dfText + ".jfr"; |
|
84 AccessControlContext acc = recording.getNoDestinationDumpOnExitAccessControlContext(); |
|
85 return AccessController.doPrivileged(new PrivilegedExceptionAction<WriteableUserPath>() { |
|
86 @Override |
|
87 public WriteableUserPath run() throws Exception { |
|
88 return new WriteableUserPath(Paths.get(".", name)); |
|
89 } |
|
90 }, acc); |
|
91 } catch (PrivilegedActionException e) { |
|
92 Throwable t = e.getCause(); |
|
93 if (t instanceof SecurityException) { |
|
94 Logger.log(LogTag.JFR, LogLevel.WARN, "Not allowed to create dump path for recording " + recording.getId() + " on exit. " + e.getMessage()); |
|
95 } |
|
96 if (t instanceof IOException) { |
|
97 Logger.log(LogTag.JFR, LogLevel.WARN, "Could not dump " + recording.getId() + " on exit. " + e.getMessage()); |
|
98 } |
|
99 return null; |
|
100 } |
|
101 } |
|
102 |
|
103 static final class ExceptionHandler implements Thread.UncaughtExceptionHandler { |
|
104 public void uncaughtException(Thread t, Throwable e) { |
|
105 JVM.getJVM().uncaughtException(t, e); |
|
106 } |
|
107 } |
|
108 } |