hotspot/test/serviceability/sa/TestInstanceKlassSizeForInterface.java
changeset 43979 4597fa06b227
parent 42646 591ef3658bb0
equal deleted inserted replaced
43978:dcf18262fc95 43979:4597fa06b227
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
       
    24 import java.util.ArrayList;
       
    25 import java.util.List;
       
    26 
    24 import sun.jvm.hotspot.HotSpotAgent;
    27 import sun.jvm.hotspot.HotSpotAgent;
    25 import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
    28 import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
    26 import sun.jvm.hotspot.oops.InstanceKlass;
    29 import sun.jvm.hotspot.oops.InstanceKlass;
    27 import sun.jvm.hotspot.debugger.*;
    30 import sun.jvm.hotspot.debugger.*;
    28 
    31 
       
    32 import jdk.test.lib.apps.LingeredApp;
    29 import jdk.test.lib.JDKToolLauncher;
    33 import jdk.test.lib.JDKToolLauncher;
    30 import jdk.test.lib.JDKToolFinder;
    34 import jdk.test.lib.JDKToolFinder;
    31 import jdk.test.lib.Platform;
    35 import jdk.test.lib.Platform;
    32 import jdk.test.lib.process.ProcessTools;
    36 import jdk.test.lib.process.ProcessTools;
    33 import jdk.test.lib.process.OutputAnalyzer;
    37 import jdk.test.lib.process.OutputAnalyzer;
    43  *          jdk.hotspot.agent/sun.jvm.hotspot.oops
    47  *          jdk.hotspot.agent/sun.jvm.hotspot.oops
    44  *          jdk.hotspot.agent/sun.jvm.hotspot.debugger
    48  *          jdk.hotspot.agent/sun.jvm.hotspot.debugger
    45  * @run main/othervm TestInstanceKlassSizeForInterface
    49  * @run main/othervm TestInstanceKlassSizeForInterface
    46  */
    50  */
    47 
    51 
    48 interface Language {
       
    49     static final long nbrOfWords = 99999;
       
    50     public abstract long getNbrOfWords();
       
    51 }
       
    52 
       
    53 class ParselTongue implements Language {
       
    54     public long getNbrOfWords() {
       
    55       return nbrOfWords * 4;
       
    56     }
       
    57 }
       
    58 
       
    59 public class TestInstanceKlassSizeForInterface {
    52 public class TestInstanceKlassSizeForInterface {
    60 
    53 
    61     private static void SAInstanceKlassSize(int pid,
    54     private static LingeredAppWithInterface theApp = null;
       
    55 
       
    56     private static void SAInstanceKlassSize(int lingeredAppPid,
    62                                             String[] instanceKlassNames) {
    57                                             String[] instanceKlassNames) {
    63 
    58 
    64         HotSpotAgent agent = new HotSpotAgent();
    59         HotSpotAgent agent = new HotSpotAgent();
    65         try {
    60         try {
    66             agent.attach((int)pid);
    61             agent.attach(lingeredAppPid);
    67         }
    62         }
    68         catch (DebuggerException e) {
    63         catch (DebuggerException e) {
    69             System.out.println(e.getMessage());
    64             System.out.println(e.getMessage());
    70             System.err.println("Unable to connect to process ID: " + pid);
    65             System.err.println("Unable to connect to process ID: " + lingeredAppPid);
    71 
    66 
    72             agent.detach();
    67             agent.detach();
    73             e.printStackTrace();
    68             e.printStackTrace();
    74         }
    69         }
    75 
    70 
    96         }
    91         }
    97         return null;
    92         return null;
    98     }
    93     }
    99 
    94 
   100     private static void createAnotherToAttach(
    95     private static void createAnotherToAttach(
   101                             String[] instanceKlassNames) throws Exception {
    96                             String[] instanceKlassNames,
       
    97                             int lingeredAppPid) throws Exception {
   102 
    98 
   103         ProcessBuilder pb = new ProcessBuilder();
       
   104 
       
   105         // Grab the pid from the current java process and pass it
       
   106         String[] toolArgs = {
    99         String[] toolArgs = {
   107             "--add-modules=jdk.hotspot.agent",
   100             "--add-modules=jdk.hotspot.agent",
   108             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
   101             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
   109             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",
   102             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",
   110             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
   103             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
   111             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",
   104             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",
   112             "TestInstanceKlassSizeForInterface",
   105             "TestInstanceKlassSizeForInterface",
   113             Long.toString(ProcessTools.getProcessId())
   106             Integer.toString(lingeredAppPid)
   114         };
   107         };
   115 
   108 
       
   109         // Start a new process to attach to the LingeredApp process
       
   110         ProcessBuilder processBuilder = ProcessTools
       
   111                   .createJavaProcessBuilder(toolArgs);
       
   112         OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
       
   113         SAOutput.shouldHaveExitValue(0);
       
   114         System.out.println(SAOutput.getOutput());
       
   115 
       
   116         // Run jcmd on the LingeredApp process
       
   117         ProcessBuilder pb = new ProcessBuilder();
   116         pb.command(new String[] {
   118         pb.command(new String[] {
   117                           JDKToolFinder.getJDKTool("jcmd"),
   119                           JDKToolFinder.getJDKTool("jcmd"),
   118                           Long.toString(ProcessTools.getProcessId()),
   120                           Long.toString(lingeredAppPid),
   119                           "GC.class_stats",
   121                           "GC.class_stats",
   120                           "VTab,ITab,OopMap,KlassBytes"
   122                           "VTab,ITab,OopMap,KlassBytes"
   121                       }
   123                       }
   122                   );
   124                   );
   123 
       
   124         // Start a new process to attach to the current process
       
   125         ProcessBuilder processBuilder = ProcessTools
       
   126                   .createJavaProcessBuilder(toolArgs);
       
   127         OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
       
   128         System.out.println(SAOutput.getOutput());
       
   129 
   125 
   130         OutputAnalyzer jcmdOutput = new OutputAnalyzer(pb.start());
   126         OutputAnalyzer jcmdOutput = new OutputAnalyzer(pb.start());
   131         System.out.println(jcmdOutput.getOutput());
   127         System.out.println(jcmdOutput.getOutput());
   132 
   128 
   133         // Match the sizes from both the output streams
   129         // Match the sizes from both the output streams
   151 
   147 
   152     public static void main (String... args) throws Exception {
   148     public static void main (String... args) throws Exception {
   153         String[] instanceKlassNames = new String[] {
   149         String[] instanceKlassNames = new String[] {
   154                                           "Language",
   150                                           "Language",
   155                                           "ParselTongue",
   151                                           "ParselTongue",
   156                                           "TestInstanceKlassSizeForInterface$1"
   152                                           "LingeredAppWithInterface$1"
   157                                       };
   153                                       };
   158 
   154 
   159         if (!Platform.shouldSAAttach()) {
   155         if (!Platform.shouldSAAttach()) {
   160             System.out.println(
   156             System.out.println(
   161                "SA attach not expected to work - test skipped.");
   157                "SA attach not expected to work - test skipped.");
   162             return;
   158             return;
   163         }
   159         }
   164 
   160 
   165         if (args == null || args.length == 0) {
   161         if (args == null || args.length == 0) {
   166             ParselTongue lang = new ParselTongue();
   162             try {
       
   163                 List<String> vmArgs = new ArrayList<String>();
       
   164                 vmArgs.addAll(Utils.getVmOptions());
   167 
   165 
   168             Language ventro = new Language() {
   166                 theApp = new LingeredAppWithInterface();
   169                 public long getNbrOfWords() {
   167                 LingeredApp.startApp(vmArgs, theApp);
   170                     return nbrOfWords * 8;
   168                 createAnotherToAttach(instanceKlassNames,
   171                 }
   169                                       (int)theApp.getPid());
   172             };
   170             } finally {
   173 
   171                 LingeredApp.stopApp(theApp);
   174             // Not tested at this point. The test needs to be enhanced
   172             }
   175             // later to test for the sizes of the Lambda MetaFactory
       
   176             // generated anonymous classes too. (After JDK-8160228 gets
       
   177             // fixed.)
       
   178             Runnable r2 = () -> System.out.println("Hello world!");
       
   179             r2.run();
       
   180 
       
   181             createAnotherToAttach(instanceKlassNames);
       
   182         } else {
   173         } else {
   183             SAInstanceKlassSize(Integer.parseInt(args[0]), instanceKlassNames);
   174             SAInstanceKlassSize(Integer.parseInt(args[0]), instanceKlassNames);
   184         }
   175         }
   185     }
   176     }
   186 }
   177 }