hotspot/test/runtime/CommandLine/PrintTouchedMethods.java
author minqi
Wed, 15 Jul 2015 12:24:41 -0700
changeset 31790 4a08476437e8
child 32074 54b9558a71a2
permissions -rw-r--r--
8025692: Log what methods are touched at run-time Summary: Added two diagnostic flags, LogTouchedMethods and PrintTouchedMethodsAtExit to list all methods that have been touched at run time. Added new jcmd, VM.print_touched_methods. Reviewed-by: acorn, iklam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31790
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     1
/*
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     4
 *
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     8
 *
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    13
 * accompanied this code).
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    14
 *
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    18
 *
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    21
 * questions.
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    22
 */
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    23
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    24
/*
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    25
 * @test
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    26
 * @bug 8025692
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    27
 * @modules java.base/sun.misc
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    28
 *          java.management
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    29
 * @library /testlibrary
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    30
 * @compile TestLogTouchedMethods.java PrintTouchedMethods.java
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    31
 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+LogTouchedMethods PrintTouchedMethods
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    32
 */
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    33
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    34
import java.io.File;
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    35
import java.util.List;
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    36
import jdk.test.lib.*;
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    37
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    38
public class PrintTouchedMethods {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    39
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    40
    public static void main(String args[]) throws Exception {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    41
      String[] javaArgs1 = {"-XX:-UnlockDiagnosticVMOptions", "-XX:+LogTouchedMethods", "-XX:+PrintTouchedMethodsAtExit", "TestLogTouchedMethods"};
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    42
      ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(javaArgs1);
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    43
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    44
      // UnlockDiagnostic turned off, should fail
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    45
      OutputAnalyzer output = new OutputAnalyzer(pb.start());
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    46
      output.shouldContain("Error: VM option 'LogTouchedMethods' is diagnostic and must be enabled via -XX:+UnlockDiagnosticVMOptions.");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    47
      output.shouldContain("Error: Could not create the Java Virtual Machine.");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    48
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    49
      String[] javaArgs2 = {"-XX:+UnlockDiagnosticVMOptions", "-XX:+LogTouchedMethods", "-XX:+PrintTouchedMethodsAtExit", "TestLogTouchedMethods"};
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    50
      pb = ProcessTools.createJavaProcessBuilder(javaArgs2);
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    51
      output = new OutputAnalyzer(pb.start());
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    52
      // check order:
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    53
      // 1 "# Method::print_touched_methods version 1" is the first in first line
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    54
      // 2 should contain TestLogMethods.methodA:()V
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    55
      // 3 should not contain TestLogMethods.methodB:()V
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    56
      // Repeat above for another run with -Xint
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    57
      List<String> lines = output.asLines();
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    58
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    59
      if (lines.size() < 1) {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    60
        throw new Exception("Empty output");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    61
      }
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    62
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    63
      String first = lines.get(0);
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    64
      if (!first.equals("# Method::print_touched_methods version 1")) {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    65
        throw new Exception("First line mismatch");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    66
      }
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    67
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    68
      output.shouldContain("TestLogTouchedMethods.methodA:()V");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    69
      output.shouldNotContain("TestLogTouchedMethods.methodB:()V");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    70
      output.shouldHaveExitValue(0);
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    71
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    72
      String[] javaArgs3 = {"-XX:+UnlockDiagnosticVMOptions", "-Xint", "-XX:+LogTouchedMethods", "-XX:+PrintTouchedMethodsAtExit", "TestLogTouchedMethods"};
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    73
      pb = ProcessTools.createJavaProcessBuilder(javaArgs3);
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    74
      output = new OutputAnalyzer(pb.start());
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    75
      lines = output.asLines();
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    76
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    77
      if (lines.size() < 1) {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    78
        throw new Exception("Empty output");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    79
      }
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    80
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    81
      first = lines.get(0);
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    82
      if (!first.equals("# Method::print_touched_methods version 1")) {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    83
        throw new Exception("First line mismatch");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    84
      }
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    85
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    86
      output.shouldContain("TestLogTouchedMethods.methodA:()V");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    87
      output.shouldNotContain("TestLogTouchedMethods.methodB:()V");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    88
      output.shouldHaveExitValue(0);
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    89
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    90
      // Test jcmd PrintTouchedMethods VM.print_touched_methods
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    91
      String pid = Integer.toString(ProcessTools.getProcessId());
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    92
      pb = new ProcessBuilder();
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    93
      pb.command(new String[] {JDKToolFinder.getJDKTool("jcmd"), pid, "VM.print_touched_methods"});
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    94
      output = new OutputAnalyzer(pb.start());
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    95
      try {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    96
        output.shouldContain("PrintTouchedMethods.main:([Ljava/lang/String;)V");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    97
      } catch (RuntimeException e) {
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    98
        output.shouldContain("Unknown diagnostic command");
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
    99
      }
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
   100
  }
4a08476437e8 8025692: Log what methods are touched at run-time
minqi
parents:
diff changeset
   101
}