8192985: SA: Test cases for the clhsdb 'inspect', 'scanoops' and 'printas' commands
authorjgeorge
Thu, 14 Dec 2017 12:49:47 +0530
changeset 48401 be065f758154
parent 48400 8604408bc26e
child 48402 945332d45710
8192985: SA: Test cases for the clhsdb 'inspect', 'scanoops' and 'printas' commands Summary: Create tests for the clhsdb commands: inspect, scanoops and printas Reviewed-by: sspitsyn, sballal, cjplummer
test/hotspot/jtreg/serviceability/sa/ClhsdbInspect.java
test/hotspot/jtreg/serviceability/sa/ClhsdbPrintAs.java
test/hotspot/jtreg/serviceability/sa/ClhsdbScanOops.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/sa/ClhsdbInspect.java	Thu Dec 14 12:49:47 2017 +0530
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2017, 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import jdk.test.lib.apps.LingeredApp;
+
+/*
+ * @test
+ * @bug 8192985
+ * @summary Test the clhsdb 'inspect' command
+ * @library /test/lib
+ * @run main/othervm ClhsdbInspect
+ */
+
+public class ClhsdbInspect {
+
+    public static void main(String[] args) throws Exception {
+        System.out.println("Starting the ClhsdbInspect test");
+
+        LingeredAppWithLock theApp = null;
+        try {
+            ClhsdbLauncher test = new ClhsdbLauncher();
+
+            theApp = new LingeredAppWithLock();
+            LingeredApp.startApp(null, theApp);
+            System.out.println("Started LingeredApp with pid " + theApp.getPid());
+
+            // Run the 'jstack -v' command to get the address of a Method*
+            // and the oop address of a java.lang.ref.ReferenceQueue$Lock
+            // object
+            List<String> cmds = List.of("jstack -v");
+
+            String jstackOutput = test.run(theApp.getPid(), cmds, null, null);
+
+            if (jstackOutput == null) {
+                // Output could be null due to attach permission issues
+                // and if we are skipping this.
+                LingeredApp.stopApp(theApp);
+                return;
+            }
+
+            String addressString = null;
+            Map<String, String> tokensMap = new HashMap<>();
+            tokensMap.put("waiting to lock",
+                          "instance of Oop for java/lang/Class");
+            tokensMap.put("Method\\*=", "Type is Method");
+            tokensMap.put("waiting to re-lock in wait",
+                          "instance of Oop for java/lang/ref/ReferenceQueue$Lock");
+
+            for (String key: tokensMap.keySet()) {
+                cmds = new ArrayList<String>();
+                Map<String, List<String>> expStrMap = new HashMap<>();
+
+                String[] snippets = jstackOutput.split(key);
+                String[] tokens = snippets[1].split(" ");
+                for (String token: tokens) {
+                    if (token.contains("0x")) {
+                        addressString = token.replace("<", "").replace(">", "");
+                        break;
+                    }
+                }
+
+                String cmd = "inspect " + addressString;
+                cmds.add(cmd);
+                expStrMap.put(cmd, List.of(tokensMap.get(key)));
+                test.run(theApp.getPid(), cmds, expStrMap, null);
+            }
+        } catch (Exception ex) {
+            throw new RuntimeException("Test ERROR " + ex, ex);
+        } finally {
+            LingeredApp.stopApp(theApp);
+        }
+        System.out.println("Test PASSED");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/sa/ClhsdbPrintAs.java	Thu Dec 14 12:49:47 2017 +0530
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2017, 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import jdk.test.lib.apps.LingeredApp;
+
+/*
+ * @test
+ * @bug 8192985
+ * @summary Test the clhsdb 'printas' command
+ * @library /test/lib
+ * @run main/othervm ClhsdbPrintAs
+ */
+
+public class ClhsdbPrintAs {
+
+    public static void main(String[] args) throws Exception {
+        System.out.println("Starting the ClhsdbPrintAs test");
+
+        LingeredApp theApp = null;
+        try {
+            ClhsdbLauncher test = new ClhsdbLauncher();
+            theApp = LingeredApp.startApp();
+            System.out.println("Started LingeredApp with pid " + theApp.getPid());
+
+            // Run the 'jstack -v' command to get the address of a the Method*
+            // representing LingeredApp.main
+            List<String> cmds = List.of("jstack -v");
+            Map<String, List<String>> expStrMap;
+
+            String jstackOutput = test.run(theApp.getPid(), cmds, null, null);
+
+            if (jstackOutput == null) {
+                // Output could be null due to attach permission issues
+                // and if we are skipping this.
+                LingeredApp.stopApp(theApp);
+                return;
+            }
+
+            String[] snippets = jstackOutput.split("LingeredApp.main");
+            String addressString = null;
+
+            String[] tokens = snippets[1].split("Method\\*=");
+            String[] words = tokens[1].split(" ");
+            addressString = words[0];
+
+            cmds = new ArrayList<String>();
+            expStrMap = new HashMap<>();
+
+            String cmd = "printas Method " + addressString;
+            cmds.add(cmd);
+            expStrMap.put(cmd, List.of
+                ("ConstMethod", "MethodCounters", "Method::_access_flags"));
+
+            // Run the printas Method <addr> command to obtain the address
+            // of ConstMethod*
+            String methodDetailsOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
+            snippets = methodDetailsOutput.split("ConstMethod*");
+
+            tokens = snippets[1].split(" ");
+            for (String token : tokens) {
+                if (token.contains("0x")) {
+                    addressString = token.replace("\n", "");
+                    break;
+                }
+            }
+
+            cmds = new ArrayList<String>();
+            expStrMap = new HashMap<>();
+
+            cmd = "printas ConstMethod " + addressString;
+            cmds.add(cmd);
+            expStrMap.put(cmd, List.of
+                ("ConstantPool", "_max_locals", "_flags"));
+
+            // Run the printas constMethod <addr> command to obtain the address
+            // of ConstantPool*
+            String constMethodDetailsOutput = test.run(theApp.getPid(), cmds, expStrMap, null);
+            snippets = constMethodDetailsOutput.split("ConstantPool*");
+
+            tokens = snippets[1].split(" ");
+            for (String token : tokens) {
+                if (token.contains("0x")) {
+                    addressString = token.replace("\n", "");
+                    break;
+                }
+            }
+
+            cmds = new ArrayList<String>();
+            expStrMap = new HashMap<>();
+
+            cmd = "printas ConstantPool " + addressString;
+            cmds.add(cmd);
+            expStrMap.put(cmd, List.of
+                ("ConstantPoolCache", "_pool_holder", "InstanceKlass*"));
+            test.run(theApp.getPid(), cmds, expStrMap, null);
+        } catch (Exception ex) {
+            throw new RuntimeException("Test ERROR " + ex, ex);
+        } finally {
+            LingeredApp.stopApp(theApp);
+        }
+        System.out.println("Test PASSED");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/serviceability/sa/ClhsdbScanOops.java	Thu Dec 14 12:49:47 2017 +0530
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2017, 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 java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.ArrayList;
+import jdk.test.lib.Utils;
+import jdk.test.lib.apps.LingeredApp;
+
+/*
+ * @test
+ * @bug 8192985
+ * @summary Test the clhsdb 'scanoops' command
+ * @library /test/lib
+ * @run main/othervm/timeout=1200 ClhsdbScanOops
+ */
+
+public class ClhsdbScanOops {
+
+    private static void testWithGcType(String gc) throws Exception {
+
+        LingeredApp theApp = null;
+
+        try {
+            ClhsdbLauncher test = new ClhsdbLauncher();
+            List<String> vmArgs = new ArrayList<String>();
+            vmArgs.add(gc);
+            theApp = LingeredApp.startApp(vmArgs);
+
+            System.out.println ("Started LingeredApp with the GC option " + gc +
+                                " and pid " + theApp.getPid());
+
+            // Run the 'universe' command to get the address ranges
+            List<String> cmds = List.of("universe");
+
+            String universeOutput = test.run(theApp.getPid(), cmds, null, null);
+
+            if (universeOutput == null) {
+                // Output could be null due to attach permission issues
+                // and if we are skipping this.
+                LingeredApp.stopApp(theApp);
+                return;
+            }
+
+            cmds = new ArrayList<String>();
+            Map<String, List<String>> expStrMap = new HashMap<>();
+            Map<String, List<String>> unExpStrMap = new HashMap<>();
+
+            String startAddress = null;
+            String endAddress = null;
+            String[] snippets = null;
+
+            if (gc.contains("UseParallelGC")) {
+                snippets = universeOutput.split("eden =  ");
+            } else {
+                snippets = universeOutput.split("eden \\[");
+            }
+            String[] words = snippets[1].split(",");
+            // Get the addresses from Eden
+            startAddress = words[0].replace("[", "");
+            endAddress = words[1];
+            String cmd = "scanoops " + startAddress + " " + endAddress;
+            cmds.add(cmd);
+
+            expStrMap.put(cmd, List.of
+                ("java/lang/Object", "java/lang/Class", "java/lang/Thread",
+                 "java/lang/String", "[B", "[I"));
+
+            // Test the 'type' option also
+            // scanoops <start addr> <end addr> java/lang/String
+            // Ensure that only the java/lang/String oops are printed.
+            cmd = cmd + " java/lang/String";
+            cmds.add(cmd);
+            expStrMap.put(cmd, List.of("java/lang/String"));
+            unExpStrMap.put(cmd, List.of("java/lang/Thread"));
+
+            test.run(theApp.getPid(), cmds, expStrMap, unExpStrMap);
+        } catch (Exception ex) {
+            throw new RuntimeException("Test ERROR " + ex, ex);
+        } finally {
+            LingeredApp.stopApp(theApp);
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        System.out.println("Starting the ClhsdbScanOops test");
+        try {
+            testWithGcType("-XX:+UseParallelGC");
+            testWithGcType("-XX:+UseSerialGC");
+        } catch (Exception e) {
+            throw new Error("Test failed with " + e);
+        }
+        System.out.println("Test PASSED");
+    }
+}