test/jdk/jdk/jfr/jcmd/TestJcmdDumpWithFileName.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 55378 bd613b97c7c8
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55378
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     1
/*
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     4
 *
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    10
 *
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    15
 * accompanied this code).
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    16
 *
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    20
 *
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    23
 * questions.
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    24
 */
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    25
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    26
package jdk.jfr.jcmd;
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    27
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    28
import java.io.IOException;
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    29
import java.nio.file.Files;
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    30
import java.nio.file.Path;
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    31
import java.util.stream.Stream;
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    32
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    33
import jdk.jfr.Recording;
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    34
import jdk.test.lib.Asserts;
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    35
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    36
/**
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    37
 * @test
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    38
 * @bug 8220657
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    39
 * @key jfr
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    40
 * @requires vm.hasJFR
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    41
 * @library /test/lib /test/jdk
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    42
 * @run main/othervm jdk.jfr.jcmd.TestJcmdDumpWithFileName
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    43
 */
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    44
public class TestJcmdDumpWithFileName {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    45
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    47
        testDumpAll();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    48
        testDumpNamed();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    49
        testDumpNamedWithFilename();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    50
    }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    51
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    52
    private static void testDumpAll() throws Exception {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    53
        Path p = Path.of("testDumpAll.jfr").toAbsolutePath();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    54
        try (Recording r = new Recording()) {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    55
            r.setName("testDumpAll");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    56
            r.setDestination(p);
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    57
            r.start();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    58
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    59
            JcmdHelper.jcmd("JFR.dump");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    60
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    61
            Asserts.assertFalse(namedFile(p), "Unexpected file: " + p.toString());
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    62
            Asserts.assertTrue(generatedFile(), "Expected generated file");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    63
        }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    64
        cleanup();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    65
    }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    66
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    67
    private static void testDumpNamed() throws Exception {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    68
        Path p = Path.of("testDumpNamed.jfr").toAbsolutePath();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    69
        try (Recording r = new Recording()) {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    70
            r.setName("testDumpNamed");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    71
            r.setDestination(p);
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    72
            r.start();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    73
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    74
            JcmdHelper.jcmd("JFR.dump", "name=testDumpNamed");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    75
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    76
            Asserts.assertTrue(namedFile(p), "Expected file: " + p.toString());
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    77
            Asserts.assertFalse(generatedFile(), "Unexpected generated file");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    78
        }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    79
        cleanup();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    80
    }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    81
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    82
    private static void testDumpNamedWithFilename() throws Exception {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    83
        Path p = Path.of("testDumpNamedWithFilename.jfr").toAbsolutePath();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    84
        Path override = Path.of("override.jfr").toAbsolutePath();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    85
        try (Recording r = new Recording()) {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    86
            r.setName("testDumpNamedWithFilename");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    87
            r.setDestination(p);
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    88
            r.start();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    89
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    90
            JcmdHelper.jcmd("JFR.dump", "name=testDumpNamedWithFilename", "filename=" + override.toString());
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    91
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    92
            Asserts.assertFalse(namedFile(p), "Unexpected file: " + p.toString());
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    93
            Asserts.assertTrue(namedFile(override), "Expected file: " + override.toString());
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    94
            Asserts.assertFalse(generatedFile(), "Unexpected generated file");
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    95
        }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    96
        cleanup();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    97
    }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    98
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
    99
    private static boolean namedFile(Path dumpFile) throws IOException {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   100
        return Files.exists(dumpFile) && (Files.size(dumpFile) > 0);
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   101
    }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   102
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   103
    private static boolean generatedFile() throws IOException {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   104
        long pid = ProcessHandle.current().pid();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   105
        Stream<Path> stream = Files.find(Path.of("."), 1, (p, a) -> p.toString()
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   106
                                                                     .matches("^.*hotspot-pid-" + pid + "-[0-9_]+\\.jfr$") && (a.size() > 0L));
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   107
        try (stream) {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   108
            return stream.findAny()
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   109
                         .isPresent();
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   110
        }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   111
    }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   112
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   113
    private static void cleanup() throws IOException {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   114
        Stream<Path> stream = Files.find(Path.of("."), 1, (p, a) -> p.toString().endsWith(".jfr"));
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   115
        try (stream) {
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   116
            stream.forEach(p -> p.toFile().delete());
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   117
        }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   118
    }
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   119
bd613b97c7c8 8220657: JFR.dump does not work when filename is set
egahlin
parents:
diff changeset
   120
}