1 /*
  2  * Copyright (c) 2017, 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.
  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                     "markWord::epoch_mask_in_place"));
 65             expStrMap.put("longConstant markWord::locked_value", List.of(
 66                     "longConstant markWord::locked_value"));
 67             expStrMap.put("longConstant markWord::lock_bits", List.of(
 68                     "longConstant markWord::lock_bits"));
 69             expStrMap.put("longConstant jtreg::test", List.of(
 70                     "longConstant jtreg::test 6"));
 71 
 72             Map<String, List<String>> unExpStrMap = new HashMap<>();
 73             unExpStrMap.put("longConstant jtreg::test", List.of(
 74                     "Error: java.lang.RuntimeException: No long constant named"));
 75 
 76             String longConstantOutput = test.run(theApp.getPid(), cmds, expStrMap, unExpStrMap);
 77 
 78             checkForTruncation(longConstantOutput);
 79         } catch (SkippedException e) {
 80             throw e;
 81         } catch (Exception ex) {
 82             throw new RuntimeException("Test ERROR " + ex, ex);
 83         } finally {
 84             LingeredApp.stopApp(theApp);
 85         }
 86         System.out.println("Test PASSED");
 87     }
 88 
 89     private static void checkForTruncation(String longConstantOutput) throws Exception {
 90 
 91         // Expected values obtained from the hash_mask_in_place definition in markWord.hpp
 92 
 93         // Expected output snippet is of the form (on x64-64):
 94         // ...
 95         // longConstant VM_Version::CPU_SHA 17179869184
 96         // longConstant markWord::biased_lock_bits 1
 97         // longConstant markWord::age_shift 3
 98         // longConstant markWord::hash_mask_in_place 549755813632
 99         // ...
100 
101         checkLongValue("markWord::hash_mask_in_place",
102                        longConstantOutput,
103                        Platform.is64bit() ? 549755813632L: 4294967168L);
104 
105         String arch = System.getProperty("os.arch");
106         if (arch.equals("amd64") || arch.equals("i386") || arch.equals("x86")) {
107             // Expected value obtained from the CPU_SHA definition in vm_version_x86.hpp
108             checkLongValue("VM_Version::CPU_SHA",
109                            longConstantOutput,
110                            17179869184L);
111         }
112     }
113 
114     private static void checkLongValue(String constName, String longConstantOutput,
115                                        long checkValue) throws Exception {
116 
117         String[] snippets = longConstantOutput.split(constName);
118         String[] words = snippets[1].split("\\R");
119         long readValue = Long.parseLong(words[0].trim());
120         if (readValue != checkValue) {
121             throw new Exception ("Reading " + constName + ". Expected " + checkValue +
122                                  ". Obtained " + readValue + " instead.");
123         }
124     }
125 }