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  * 
  40  * @library /lib /
  41  * 
  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 
  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 
  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 
 100     private static void testNegative(String configName, Object value) {
 101         try {
 102             // Syntactically invalid arguments are catched by the JCMD framework where an error code of 1 is returned.
 103             // 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.
 104             JcmdHelper.jcmd(configName.equals(UNSUPPORTED_OPTION) ? 1 : 0, "JFR.configure", configName + "=" + value);
 105         } catch(Exception e) {
 106             testExceptions.add(e);
 107         }
 108     }
 109 
 110     private static Object getOption(String name) {
 111         switch (name) {
 112             case DUMPPATH: return Options.getDumpPath().toString();
 113             case STACK_DEPTH: return Options.getStackDepth();
 114             case GLOBAL_BUFFER_COUNT: return Options.getGlobalBufferCount();
 115             case GLOBAL_BUFFER_SIZE: return Options.getGlobalBufferSize();
 116             case THREAD_BUFFER_SIZE: return Options.getThreadBufferSize();
 117             case MAX_CHUNK_SIZE: return Options.getMaxChunkSize();
 118             case SAMPLE_THREADS: return Options.getSampleThreads();
 119             default: throw new RuntimeException("Unknown option " + name);
 120         }
 121     }
 122 }