8054890: Serviceability: New diagnostic commands 'VM.set_flag' and 'JVMTI.data_dump'
authorjbachorik
Mon, 23 Mar 2015 11:46:15 +0100
changeset 30121 cc43664a0ad7
parent 30120 af79e35d33e1
child 30122 7fbc1bc3a020
child 30123 7a8b6bd85e24
child 30126 89b8253271bd
8054890: Serviceability: New diagnostic commands 'VM.set_flag' and 'JVMTI.data_dump' Reviewed-by: sla, fparain, egahlin, ykantser
hotspot/src/share/vm/services/diagnosticCommand.cpp
hotspot/src/share/vm/services/diagnosticCommand.hpp
hotspot/test/serviceability/dcmd/jvmti/DataDumpDcmdTest.java
hotspot/test/serviceability/dcmd/vm/SetVMFlagTest.java
--- a/hotspot/src/share/vm/services/diagnosticCommand.cpp	Sun Mar 29 09:20:27 2015 -0400
+++ b/hotspot/src/share/vm/services/diagnosticCommand.cpp	Mon Mar 23 11:46:15 2015 +0100
@@ -32,6 +32,7 @@
 #include "services/diagnosticArgument.hpp"
 #include "services/diagnosticCommand.hpp"
 #include "services/diagnosticFramework.hpp"
+#include "services/writeableFlags.hpp"
 #include "services/heapDumper.hpp"
 #include "services/management.hpp"
 #include "utilities/macros.hpp"
@@ -50,6 +51,7 @@
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CommandLineDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintSystemPropertiesDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PrintVMFlagsDCmd>(full_export, true, false));
+  DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SetVMFlagDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMDynamicLibrariesDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<VMUptimeDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SystemGCDCmd>(full_export, true, false));
@@ -62,6 +64,9 @@
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<SymboltableDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<StringtableDCmd>(full_export, true, false));
 #endif // INCLUDE_SERVICES
+#if INCLUDE_JVMTI
+  DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JVMTIDataDumpDCmd>(full_export, true, false));
+#endif // INCLUDE_JVMTI
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ThreadDumpDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<RotateGCLogDCmd>(full_export, true, false));
   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<ClassLoaderStatsDCmd>(full_export, true, false));
@@ -197,6 +202,46 @@
     }
 }
 
+SetVMFlagDCmd::SetVMFlagDCmd(outputStream* output, bool heap) :
+                                   DCmdWithParser(output, heap),
+  _flag("flag name", "The name of the flag we want to set",
+        "STRING", true),
+  _value("string value", "The value we want to set", "STRING", false) {
+  _dcmdparser.add_dcmd_argument(&_flag);
+  _dcmdparser.add_dcmd_argument(&_value);
+}
+
+void SetVMFlagDCmd::execute(DCmdSource source, TRAPS) {
+  const char* val = NULL;
+  if (_value.value() != NULL) {
+    val = _value.value();
+  }
+
+  FormatBuffer<80> err_msg("%s", "");
+  int ret = WriteableFlags::set_flag(_flag.value(), val, Flag::MANAGEMENT, err_msg);
+
+  if (ret != WriteableFlags::SUCCESS) {
+    output()->print_cr("%s", err_msg.buffer());
+  }
+}
+
+int SetVMFlagDCmd::num_arguments() {
+  ResourceMark rm;
+  SetVMFlagDCmd* dcmd = new SetVMFlagDCmd(NULL, false);
+  if (dcmd != NULL) {
+    DCmdMark mark(dcmd);
+    return dcmd->_dcmdparser.num_arguments();
+  } else {
+    return 0;
+  }
+}
+
+void JVMTIDataDumpDCmd::execute(DCmdSource source, TRAPS) {
+  if (JvmtiExport::should_post_data_dump()) {
+    JvmtiExport::post_data_dump();
+  }
+}
+
 void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) {
   // load sun.misc.VMSupport
   Symbol* klass = vmSymbols::sun_misc_VMSupport();
--- a/hotspot/src/share/vm/services/diagnosticCommand.hpp	Sun Mar 29 09:20:27 2015 -0400
+++ b/hotspot/src/share/vm/services/diagnosticCommand.hpp	Mon Mar 23 11:46:15 2015 +0100
@@ -131,6 +131,48 @@
   virtual void execute(DCmdSource source, TRAPS);
 };
 
+class SetVMFlagDCmd : public DCmdWithParser {
+protected:
+  DCmdArgument<char*> _flag;
+  DCmdArgument<char*> _value;
+
+public:
+  SetVMFlagDCmd(outputStream* output, bool heap);
+  static const char* name() { return "VM.set_flag"; }
+  static const char* description() {
+    return "Sets VM flag option using the provided value.";
+  }
+  static const char* impact() {
+    return "Low";
+  }
+  static const JavaPermission permission() {
+    JavaPermission p = {"java.lang.management.ManagementPermission",
+                        "control", NULL};
+    return p;
+  }
+  static int num_arguments();
+  virtual void execute(DCmdSource source, TRAPS);
+};
+
+class JVMTIDataDumpDCmd : public DCmd {
+public:
+  JVMTIDataDumpDCmd(outputStream* output, bool heap) : DCmd(output, heap) { }
+  static const char* name() { return "JVMTI.data_dump"; }
+  static const char* description() {
+    return "Signal the JVM to do a data-dump request for JVMTI.";
+  }
+  static const char* impact() {
+    return "High";
+  }
+  static const JavaPermission permission() {
+    JavaPermission p = {"java.lang.management.ManagementPermission",
+                        "monitor", NULL};
+    return p;
+  }
+  static int num_arguments() { return 0; }
+  virtual void execute(DCmdSource source, TRAPS);
+};
+
 class VMDynamicLibrariesDCmd : public DCmd {
 public:
   VMDynamicLibrariesDCmd(outputStream* output, bool heap);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/serviceability/dcmd/jvmti/DataDumpDcmdTest.java	Mon Mar 23 11:46:15 2015 +0100
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.oracle.java.testlibrary.OutputAnalyzer;
+import com.oracle.java.testlibrary.dcmd.CommandExecutor;
+import com.oracle.java.testlibrary.dcmd.JMXExecutor;
+import com.oracle.java.testlibrary.dcmd.PidJcmdExecutor;
+import org.testng.annotations.Test;
+
+/*
+ * @test
+ * @bug 8054890
+ * @summary Test of JVMTI.data_dump diagnostic command
+ * @library /testlibrary
+ * @build com.oracle.java.testlibrary.*
+ * @run testng DataDumpDcmdTest
+ */
+
+/**
+ * This test issues the "JVMTI.data_dump" command which will dump the related JVMTI
+ * data.
+ *
+ */
+public class DataDumpDcmdTest {
+    public void run(CommandExecutor executor) {
+        OutputAnalyzer output = executor.execute("JVMTI.data_dump");
+
+        output.stderrShouldBeEmpty();
+    }
+
+    @Test
+    public void jmx() throws Throwable {
+        run(new JMXExecutor());
+    }
+
+    @Test
+    public void cli() throws Throwable {
+        run(new PidJcmdExecutor());
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/serviceability/dcmd/vm/SetVMFlagTest.java	Mon Mar 23 11:46:15 2015 +0100
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.oracle.java.testlibrary.OutputAnalyzer;
+import com.oracle.java.testlibrary.dcmd.CommandExecutor;
+import com.oracle.java.testlibrary.dcmd.JMXExecutor;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+/*
+ * @test
+ * @bug 8054890
+ * @summary Test of VM.set_flag diagnostic command
+ * @library /testlibrary
+ * @build com.oracle.java.testlibrary.*
+ * @build com.oracle.java.testlibrary.dcmd.*
+ * @run testng SetVMFlagTest
+ */
+
+public class SetVMFlagTest {
+    private static final String PRINTGC_NAME = "PrintGC";
+    private static final String XMX_NAME = "MaxHeapSize";
+
+    private static final String PRINTGC_PATTERN = "\\s*bool " + PRINTGC_NAME +
+                                                  "\\s+[\\:]?=\\s+(.*?)\\s+\\{manageable\\}";
+    private static final String XMX_PATTERN = "\\s*uintx " + XMX_NAME +
+                                                  "\\s+[\\:]?=\\s+(.*?)\\s+\\{product\\}";
+
+    public void run(CommandExecutor executor) {
+        setMutableFlag(executor);
+        setMutableFlagWithInvalidValue(executor);
+        setImmutableFlag(executor);
+        setNonExistingFlag(executor);
+    }
+
+    @Test
+    public void jmx() {
+        run(new JMXExecutor());
+    }
+
+    private void setMutableFlag(CommandExecutor executor) {
+        OutputAnalyzer out = getAllFlags(executor);
+        String flagVal = out.firstMatch(PRINTGC_PATTERN, 1);
+
+        if (flagVal == null) {
+            System.err.println(out.getOutput());
+            throw new Error("'" + PRINTGC_NAME + "' flag is either not available or manageable");
+        }
+
+        Boolean blnVal = Boolean.parseBoolean(flagVal);
+
+        out = executor.execute("VM.set_flag " + PRINTGC_NAME + " " + (blnVal ? 0 : 1));
+        out.stderrShouldBeEmpty();
+
+        out = getAllFlags(executor);
+
+        String newFlagVal = out.firstMatch(PRINTGC_PATTERN, 1);
+
+        assertNotEquals(newFlagVal, flagVal);
+    }
+
+    private void setMutableFlagWithInvalidValue(CommandExecutor executor) {
+        OutputAnalyzer out = getAllFlags(executor);
+        String flagVal = out.firstMatch(PRINTGC_PATTERN, 1);
+
+        if (flagVal == null) {
+            System.err.println(out.getOutput());
+            throw new Error("'" + PRINTGC_NAME + "' flag is either not available or manageable");
+        }
+
+        // PrintGC is a boolean flag and accepts only 0/1 as its value
+        out = executor.execute("VM.set_flag " + PRINTGC_NAME + " unexpected_value");
+        out.stderrShouldBeEmpty();
+        out.stdoutShouldContain("flag value must be a boolean (1 or 0)");
+
+        out = getAllFlags(executor);
+
+        String newFlagVal = out.firstMatch(PRINTGC_PATTERN, 1);
+
+        assertEquals(newFlagVal, flagVal);
+    }
+
+    private void setImmutableFlag(CommandExecutor executor) {
+        OutputAnalyzer out = getAllFlags(executor);
+        String flagVal = out.firstMatch(XMX_PATTERN, 1);
+
+        if (flagVal == null) {
+            System.err.println(out.getOutput());
+            throw new Error("'" + XMX_NAME + "' flag is not available or immutable");
+        }
+
+        Long numVal = Long.parseLong(flagVal);
+
+        out = executor.execute("VM.set_flag " + XMX_NAME + " " + (numVal + 1));
+        out.stderrShouldBeEmpty();
+        out.stdoutShouldContain("only 'writeable' flags can be set");
+
+        out = getAllFlags(executor);
+
+        String newFlagVal = out.firstMatch(XMX_PATTERN, 1);
+
+        assertEquals(newFlagVal, flagVal);
+    }
+
+    private void setNonExistingFlag(CommandExecutor executor) {
+        String unknownFlag = "ThisIsUnknownFlag";
+        OutputAnalyzer out = executor.execute("VM.set_flag " + unknownFlag + " 1");
+        out.stderrShouldBeEmpty();
+        out.stdoutShouldContain("flag " + unknownFlag + " does not exist");
+    }
+
+    private OutputAnalyzer getAllFlags(CommandExecutor executor) {
+        return executor.execute("VM.flags -all");
+    }
+}
\ No newline at end of file