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