changeset 55878:bd613b97c7c8

8220657: JFR.dump does not work when filename is set Reviewed-by: ysuenaga
author egahlin
date Thu, 13 Jun 2019 12:27:29 +0200
parents f48d3bec75ba
children 865775b86780 b2df72a5445f
files src/jdk.jfr/share/classes/jdk/jfr/internal/PlatformRecording.java src/jdk.jfr/share/classes/jdk/jfr/internal/dcmd/DCmdDump.java test/jdk/jdk/jfr/jcmd/TestJcmdDumpWithFileName.java
diffstat 3 files changed, 144 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/PlatformRecording.java	Thu Jun 13 11:54:55 2019 +0200
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/PlatformRecording.java	Thu Jun 13 12:27:29 2019 +0200
@@ -318,7 +318,6 @@
         PlatformRecording clone = recorder.newTemporaryRecording();
         clone.setShouldWriteActiveRecordingEvent(false);
         clone.setName(getName());
-        clone.setDestination(this.destination);
         clone.setToDisk(true);
         // We purposely don't clone settings here, since
         // a union a == a
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/dcmd/DCmdDump.java	Thu Jun 13 11:54:55 2019 +0200
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/dcmd/DCmdDump.java	Thu Jun 13 12:27:29 2019 +0200
@@ -26,6 +26,8 @@
 
 import java.io.IOException;
 import java.nio.file.InvalidPathException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.time.Duration;
 import java.time.Instant;
 import java.time.LocalDate;
@@ -126,26 +128,37 @@
             recording = findRecording(name);
         }
         PlatformRecorder recorder = PrivateAccess.getInstance().getPlatformRecorder();
-        synchronized (recorder) {
-            dump(recorder, recording, name, filename, maxSize, pathToGcRoots, beginTime, endTime);
+
+        try {
+            synchronized (recorder) {
+                dump(recorder, recording, name, filename, maxSize, pathToGcRoots, beginTime, endTime);
+            }
+        } catch (IOException | InvalidPathException e) {
+            throw new DCmdException("Dump failed. Could not copy recording data. %s", e.getMessage());
         }
         return getResult();
     }
 
-    public void dump(PlatformRecorder recorder, Recording recording, String name, String filename, Long maxSize, Boolean pathToGcRoots, Instant beginTime, Instant endTime) throws DCmdException {
+    public void dump(PlatformRecorder recorder, Recording recording, String name, String filename, Long maxSize, Boolean pathToGcRoots, Instant beginTime, Instant endTime) throws DCmdException, IOException {
         try (PlatformRecording r = newSnapShot(recorder, recording, pathToGcRoots)) {
             r.filter(beginTime, endTime, maxSize);
             if (r.getChunks().isEmpty()) {
                 throw new DCmdException("Dump failed. No data found in the specified interval.");
             }
-            SafePath dumpFile = resolvePath(recording, filename);
-
-            // Needed for JVM
-            Utils.touch(dumpFile.toPath());
-            r.dumpStopped(new WriteableUserPath(dumpFile.toPath()));
-            reportOperationComplete("Dumped", name, dumpFile);
-        } catch (IOException | InvalidPathException e) {
-            throw new DCmdException("Dump failed. Could not copy recording data. %s", e.getMessage());
+            // If a filename exist, use it
+            // if a filename doesn't exist, use destination set earlier
+            // if destination doesn't exist, generate a filename
+            WriteableUserPath wup = null;
+            if (recording != null) {
+                PlatformRecording pRecording = PrivateAccess.getInstance().getPlatformRecording(recording);
+                wup = pRecording.getDestination();
+            }
+            if (filename != null || (filename == null && wup == null) ) {
+                SafePath safe = resolvePath(recording, filename);
+                wup = new WriteableUserPath(safe.toPath());
+            }
+            r.dumpStopped(wup);
+            reportOperationComplete("Dumped", name, new SafePath(wup.getRealPathText()));
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/jdk/jfr/jcmd/TestJcmdDumpWithFileName.java	Thu Jun 13 12:27:29 2019 +0200
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.jfr.jcmd;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.stream.Stream;
+
+import jdk.jfr.Recording;
+import jdk.test.lib.Asserts;
+
+/**
+ * @test
+ * @bug 8220657
+ * @key jfr
+ * @requires vm.hasJFR
+ * @library /test/lib /test/jdk
+ * @run main/othervm jdk.jfr.jcmd.TestJcmdDumpWithFileName
+ */
+public class TestJcmdDumpWithFileName {
+
+    public static void main(String[] args) throws Exception {
+        testDumpAll();
+        testDumpNamed();
+        testDumpNamedWithFilename();
+    }
+
+    private static void testDumpAll() throws Exception {
+        Path p = Path.of("testDumpAll.jfr").toAbsolutePath();
+        try (Recording r = new Recording()) {
+            r.setName("testDumpAll");
+            r.setDestination(p);
+            r.start();
+
+            JcmdHelper.jcmd("JFR.dump");
+
+            Asserts.assertFalse(namedFile(p), "Unexpected file: " + p.toString());
+            Asserts.assertTrue(generatedFile(), "Expected generated file");
+        }
+        cleanup();
+    }
+
+    private static void testDumpNamed() throws Exception {
+        Path p = Path.of("testDumpNamed.jfr").toAbsolutePath();
+        try (Recording r = new Recording()) {
+            r.setName("testDumpNamed");
+            r.setDestination(p);
+            r.start();
+
+            JcmdHelper.jcmd("JFR.dump", "name=testDumpNamed");
+
+            Asserts.assertTrue(namedFile(p), "Expected file: " + p.toString());
+            Asserts.assertFalse(generatedFile(), "Unexpected generated file");
+        }
+        cleanup();
+    }
+
+    private static void testDumpNamedWithFilename() throws Exception {
+        Path p = Path.of("testDumpNamedWithFilename.jfr").toAbsolutePath();
+        Path override = Path.of("override.jfr").toAbsolutePath();
+        try (Recording r = new Recording()) {
+            r.setName("testDumpNamedWithFilename");
+            r.setDestination(p);
+            r.start();
+
+            JcmdHelper.jcmd("JFR.dump", "name=testDumpNamedWithFilename", "filename=" + override.toString());
+
+            Asserts.assertFalse(namedFile(p), "Unexpected file: " + p.toString());
+            Asserts.assertTrue(namedFile(override), "Expected file: " + override.toString());
+            Asserts.assertFalse(generatedFile(), "Unexpected generated file");
+        }
+        cleanup();
+    }
+
+    private static boolean namedFile(Path dumpFile) throws IOException {
+        return Files.exists(dumpFile) && (Files.size(dumpFile) > 0);
+    }
+
+    private static boolean generatedFile() throws IOException {
+        long pid = ProcessHandle.current().pid();
+        Stream<Path> stream = Files.find(Path.of("."), 1, (p, a) -> p.toString()
+                                                                     .matches("^.*hotspot-pid-" + pid + "-[0-9_]+\\.jfr$") && (a.size() > 0L));
+        try (stream) {
+            return stream.findAny()
+                         .isPresent();
+        }
+    }
+
+    private static void cleanup() throws IOException {
+        Stream<Path> stream = Files.find(Path.of("."), 1, (p, a) -> p.toString().endsWith(".jfr"));
+        try (stream) {
+            stream.forEach(p -> p.toFile().delete());
+        }
+    }
+
+}