1 /*
2  * Copyright (c) 2015, 2019, 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.io.File;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 import jdk.jfr.internal.Repository;
35 import jdk.jfr.internal.SecuritySupport.SafePath;
36 import jdk.jfr.internal.Options;
37 import jdk.test.lib.Asserts;
38 import jdk.test.lib.Utils;
39 
40 /**
41  * @test
42  * @summary The test verifies JFR.configure command
43  * @key jfr
44  * @requires vm.hasJFR
45  * @library /test/lib /test/jdk
46  * @modules jdk.jfr/jdk.jfr.internal
47  * @run main/othervm -Xlog:jfr=info:file=jfr_info.txt jdk.jfr.jcmd.TestJcmdConfigure
48  */
49 public class TestJcmdConfigure {
50 
51     private static final String DUMPPATH = "dumppath";
52     private static final String STACK_DEPTH = "stackdepth";
53     private static final String GLOBAL_BUFFER_COUNT = "globalbuffercount";
54     private static final String GLOBAL_BUFFER_SIZE = "globalbuffersize";
55     private static final String THREAD_BUFFER_SIZE = "thread_buffer_size";
56     private static final String MAX_CHUNK_SIZE = "maxchunksize";
57     private static final String SAMPLE_THREADS = "samplethreads";
58     private static final String UNSUPPORTED_OPTION = "unsupportedoption";
59 
60     private static final String REPOSITORYPATH_1 = "." + File.pathSeparator + "repo1";
61     private static final String REPOSITORYPATH_2 = "." + File.pathSeparator + "repo2";
62 
63     private static final String REPOSITORYPATH_SETTING_1 = "repositorypath="+REPOSITORYPATH_1;
64     private static final String REPOSITORYPATH_SETTING_2 = "repositorypath="+REPOSITORYPATH_2;
65     private static final String JFR_UNIFIED_LOG_FILE = "jfr_info.txt";
66 
main(String[] args)67     public static void main(String[] args) throws Exception {
68         //
69         // Simple sanity tests against what is available in Java,
70         // before Flight Recorder is loaded. To do:
71         //
72         // - set values when JFR is running, check for errors.
73         // - validate against output from JFR.configure
74         // - where feasible, check if they are respected
75         //
76 
77         String dumpPath = Utils.createTempDirectory("dump-path-").toAbsolutePath().toString();
78 
79         test(DUMPPATH, dumpPath);
80         test(STACK_DEPTH, 15);
81         test(GLOBAL_BUFFER_COUNT, 7);
82         test(GLOBAL_BUFFER_SIZE, 6);
83         test(THREAD_BUFFER_SIZE, 5);
84         test(MAX_CHUNK_SIZE, 14 * 1000 * 1000);
85         test(SAMPLE_THREADS, false);
86         test(SAMPLE_THREADS, true);
87         testNegative(UNSUPPORTED_OPTION, 100000);
88         testNegative(MAX_CHUNK_SIZE, -500);
89 
90         testRepository();
91 
92         if (!testExceptions.isEmpty()) {
93             for (Exception e : testExceptions) {
94                 System.out.println("Error: " + e.getMessage());
95             }
96             throw testExceptions.get(0);
97         }
98     }
99 
100     private static List<Exception> testExceptions = new ArrayList<>();
101 
test(String configName, Object value)102     private static void test(String configName, Object value) {
103         JcmdHelper.jcmd("JFR.configure", configName + "=" + value);
104         Object actualValue = getOption(configName);
105         System.out.format("Test param='%s', expected='%s', actual='%s'%n", configName, value, actualValue);
106         try {
107             // Need convert to string to compare Integer and Long
108             Asserts.assertEquals(value.toString(), actualValue.toString(), "Wrong JFR.configure " + configName);
109         } catch (Exception e) {
110             testExceptions.add(e);
111         }
112     }
113 
testNegative(String configName, Object value)114     private static void testNegative(String configName, Object value) {
115         try {
116             JcmdHelper.jcmd(1, "JFR.configure", configName + "=" + value);
117         } catch(Exception e) {
118             testExceptions.add(e);
119         }
120     }
121 
getOption(String name)122     private static Object getOption(String name) {
123         switch (name) {
124             case DUMPPATH: return Options.getDumpPath().toString();
125             case STACK_DEPTH: return Options.getStackDepth();
126             case GLOBAL_BUFFER_COUNT: return Options.getGlobalBufferCount();
127             case GLOBAL_BUFFER_SIZE: return Options.getGlobalBufferSize();
128             case THREAD_BUFFER_SIZE: return Options.getThreadBufferSize();
129             case MAX_CHUNK_SIZE: return Options.getMaxChunkSize();
130             case SAMPLE_THREADS: return Options.getSampleThreads();
131             default: throw new RuntimeException("Unknown option " + name);
132         }
133     }
134 
testRepository()135     private static void testRepository(){
136         final String findWhat = "[info][jfr] Same base repository path " + REPOSITORYPATH_1 + " is set";
137 
138         try {
139             JcmdHelper.jcmd("JFR.configure", REPOSITORYPATH_SETTING_1);
140             SafePath initialPath = Repository.getRepository().getRepositoryPath();
141 
142             JcmdHelper.jcmd("JFR.configure", REPOSITORYPATH_SETTING_1);
143             SafePath samePath = Repository.getRepository().getRepositoryPath();
144             Asserts.assertTrue(samePath.equals(initialPath));
145 
146             List<String> lines = Files.readAllLines(Paths.get(JFR_UNIFIED_LOG_FILE));
147             Asserts.assertTrue(lines.stream().anyMatch(l->l.contains(findWhat)));
148 
149             JcmdHelper.jcmd("JFR.configure", REPOSITORYPATH_SETTING_2);
150             SafePath changedPath = Repository.getRepository().getRepositoryPath();
151 
152             Asserts.assertFalse(changedPath.equals(initialPath));
153 
154         } catch(Exception e) {
155             testExceptions.add(e);
156         }
157     }
158 }
159