1 /*
2  * Copyright (c) 2015, 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.jcmd;
27 
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 import jdk.jfr.internal.Options;
33 import jdk.test.lib.Asserts;
34 
35 /**
36  * @test
37  * @summary The test verifies JFR.configure command
38  * @key jfr
39  * @requires vm.hasJFR
40  * @library /test/lib /test/jdk
41  * @modules jdk.jfr/jdk.jfr.internal
42  * @run main/othervm jdk.jfr.jcmd.TestJcmdConfigure
43  */
44 public class TestJcmdConfigure {
45 
46     private static final String DUMPPATH = "dumppath";
47     private static final String STACK_DEPTH = "stackdepth";
48     private static final String GLOBAL_BUFFER_COUNT = "globalbuffercount";
49     private static final String GLOBAL_BUFFER_SIZE = "globalbuffersize";
50     private static final String THREAD_BUFFER_SIZE = "thread_buffer_size";
51     private static final String MAX_CHUNK_SIZE = "maxchunksize";
52     private static final String SAMPLE_THREADS = "samplethreads";
53     private static final String UNSUPPORTED_OPTION = "unsupportedoption";
54 
main(String[] args)55     public static void main(String[] args) throws Exception {
56         //
57         // Simple sanity tests against what is available in Java,
58         // before Flight Recorder is loaded. To do:
59         //
60         // - set values when JFR is running, check for errors.
61         // - validate against output from JFR.configure
62         // - where feasible, check if they are respected
63         //
64 
65         String dumpPath = Files.createTempDirectory("dump-path").toAbsolutePath().toString();
66 
67         test(DUMPPATH, dumpPath);
68         test(STACK_DEPTH, 15);
69         test(GLOBAL_BUFFER_COUNT, 7);
70         test(GLOBAL_BUFFER_SIZE, 6);
71         test(THREAD_BUFFER_SIZE, 5);
72         test(MAX_CHUNK_SIZE, 14 * 1000 * 1000);
73         test(SAMPLE_THREADS, false);
74         test(SAMPLE_THREADS, true);
75         testNegative(UNSUPPORTED_OPTION, 100000);
76         testNegative(MAX_CHUNK_SIZE, -500);
77 
78         if (!testExceptions.isEmpty()) {
79             for (Exception e : testExceptions) {
80                 System.out.println("Error: " + e.getMessage());
81             }
82             throw testExceptions.get(0);
83         }
84     }
85 
86     private static List<Exception> testExceptions = new ArrayList<>();
87 
test(String configName, Object value)88     private static void test(String configName, Object value) {
89         JcmdHelper.jcmd("JFR.configure", configName + "=" + value);
90         Object actualValue = getOption(configName);
91         System.out.format("Test param='%s', expected='%s', actual='%s'%n", configName, value, actualValue);
92         try {
93             // Need convert to string to compare Integer and Long
94             Asserts.assertEquals(value.toString(), actualValue.toString(), "Wrong JFR.configure " + configName);
95         } catch (Exception e) {
96             testExceptions.add(e);
97         }
98     }
99 
testNegative(String configName, Object value)100     private static void testNegative(String configName, Object value) {
101         try {
102             JcmdHelper.jcmd(1, "JFR.configure", configName + "=" + value);
103         } catch(Exception e) {
104             testExceptions.add(e);
105         }
106     }
107 
getOption(String name)108     private static Object getOption(String name) {
109         switch (name) {
110             case DUMPPATH: return Options.getDumpPath().toString();
111             case STACK_DEPTH: return Options.getStackDepth();
112             case GLOBAL_BUFFER_COUNT: return Options.getGlobalBufferCount();
113             case GLOBAL_BUFFER_SIZE: return Options.getGlobalBufferSize();
114             case THREAD_BUFFER_SIZE: return Options.getThreadBufferSize();
115             case MAX_CHUNK_SIZE: return Options.getMaxChunkSize();
116             case SAMPLE_THREADS: return Options.getSampleThreads();
117             default: throw new RuntimeException("Unknown option " + name);
118         }
119     }
120 }
121