1 /*
  2  * Copyright (c) 2017, 2021, 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.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /**
 25  * @test
 26  * @bug 8190198
 27  * @summary Test clhsdb longConstant command
 28  * @requires vm.hasSA
 29  * @library /test/lib
 30  * @run main/othervm ClhsdbLongConstant
 31  */
 32 
 33 import java.util.HashMap;
 34 import java.util.List;
 35 import java.util.Map;
 36 
 37 import jdk.test.lib.apps.LingeredApp;
 38 import jdk.test.lib.Platform;
 39 import jtreg.SkippedException;
 40 
 41 public class ClhsdbLongConstant {
 42 
 43     public static void main(String[] args) throws Exception {
 44         System.out.println("Starting ClhsdbLongConstant test");
 45 
 46         LingeredApp theApp = null;
 47         try {
 48             ClhsdbLauncher test = new ClhsdbLauncher();
 49             theApp = LingeredApp.startApp();
 50             System.out.println("Started LingeredApp with pid " + theApp.getPid());
 51 
 52             List<String> cmds = List.of(
 53                     "longConstant",
 54                     "longConstant markWord::locked_value",
 55                     "longConstant markWord::lock_bits",
 56                     "longConstant jtreg::test 6",
 57                     "longConstant jtreg::test");
 58 
 59             Map<String, List<String>> expStrMap = new HashMap<>();
 60             expStrMap.put("longConstant", List.of(
 61                     "longConstant markWord::locked_value",
 62                     "longConstant markWord::lock_bits",
 63                     "InvocationCounter::count_increment"));
 64             expStrMap.put("longConstant markWord::locked_value", List.of(
 65                     "longConstant markWord::locked_value"));
 66             expStrMap.put("longConstant markWord::lock_bits", List.of(
 67                     "longConstant markWord::lock_bits"));
 68             expStrMap.put("longConstant jtreg::test", List.of(
 69                     "longConstant jtreg::test 6"));
 70 
 71             Map<String, List<String>> unExpStrMap = new HashMap<>();
 72             unExpStrMap.put("longConstant jtreg::test", List.of(
 73                     "Error: java.lang.RuntimeException: No long constant named"));
 74 
 75             String longConstantOutput = test.run(theApp.getPid(), cmds, expStrMap, unExpStrMap);
 76 
 77             checkForTruncation(longConstantOutput);
 78         } catch (SkippedException e) {
 79             throw e;
 80         } catch (Exception ex) {
 81             throw new RuntimeException("Test ERROR " + ex, ex);
 82         } finally {
 83             LingeredApp.stopApp(theApp);
 84         }
 85         System.out.println("Test PASSED");
 86     }
 87 
 88     private static void checkForTruncation(String longConstantOutput) throws Exception {
 89 
 90         // Expected values obtained from the hash_mask_in_place definition in markWord.hpp
 91 
 92         // Expected output snippet is of the form (on x64-64):
 93         // ...
 94         // longConstant VM_Version::CPU_SHA 17179869184
 95         // longConstant markWord::age_shift 3
 96         // longConstant markWord::hash_mask_in_place 549755813632
 97         // ...
 98 
 99         checkLongValue("markWord::hash_mask_in_place",
100                        longConstantOutput,
101                        Platform.is64bit() ? 549755813632L: 4294967168L);
102 
103         String arch = System.getProperty("os.arch");
104         if (arch.equals("amd64") || arch.equals("i386") || arch.equals("x86")) {
105             // Expected value obtained from the CPU_SHA definition in vm_version_x86.hpp
106             checkLongValue("VM_Version::CPU_SHA",
107                            longConstantOutput,
108                            17179869184L);
109         }
110     }
111 
112     private static void checkLongValue(String constName, String longConstantOutput,
113                                        long checkValue) throws Exception {
114 
115         String[] snippets = longConstantOutput.split(constName);
116         String[] words = snippets[1].split("\\R");
117         long readValue = Long.parseLong(words[0].trim());
118         if (readValue != checkValue) {
119             throw new Exception ("Reading " + constName + ". Expected " + checkValue +
120                                  ". Obtained " + readValue + " instead.");
121         }
122     }
123 }