Java源码示例:jdk.test.lib.dcmd.CommandExecutor

示例1
public void run(CommandExecutor executor) {
    OutputAnalyzer output = executor.execute("VM.dynlibs");

    String osDependentBaseString = null;
    if (Platform.isAix()) {
        osDependentBaseString = "lib%s.so";
    } else if (Platform.isLinux()) {
        osDependentBaseString = "lib%s.so";
    } else if (Platform.isOSX()) {
        osDependentBaseString = "lib%s.dylib";
    } else if (Platform.isSolaris()) {
        osDependentBaseString = "lib%s.so";
    } else if (Platform.isWindows()) {
        osDependentBaseString = "%s.dll";
    }

    if (osDependentBaseString == null) {
        Assert.fail("Unsupported OS");
    }

    output.shouldContain(String.format(osDependentBaseString, "jvm"));
    output.shouldContain(String.format(osDependentBaseString, "java"));
    output.shouldContain(String.format(osDependentBaseString, "management"));
}
 
示例2
private void setMutableFlagInternal(CommandExecutor executor, String flag,
                                    boolean val, boolean isNumeric) {
    String strFlagVal;
    if (isNumeric) {
        strFlagVal = val ? "1" : "0";
    } else {
        strFlagVal = val ? "true" : "false";
    }

    OutputAnalyzer out = executor.execute("VM.set_flag " + flag + " " + strFlagVal);
    out.stderrShouldBeEmpty();

    out = getAllFlags(executor);

    String newFlagVal = out.firstMatch(MANAGEABLE_PATTERN.replace("(\\S+)", flag), 1);

    assertNotEquals(newFlagVal, val ? "1" : "0");
}
 
示例3
private void setMutableFlag(CommandExecutor executor) {
    OutputAnalyzer out = getAllFlags(executor);
    String flagName = out.firstMatch(MANAGEABLE_PATTERN, 1);
    String flagVal = out.firstMatch(MANAGEABLE_PATTERN, 2);

    System.out.println("### Setting a mutable flag '" + flagName + "'");

    if (flagVal == null) {
        System.err.println(out.getOutput());
        throw new Error("Can not find a boolean manageable flag");
    }

    Boolean blnVal = Boolean.parseBoolean(flagVal);
    setMutableFlagInternal(executor, flagName, !blnVal, true);
    setMutableFlagInternal(executor, flagName, blnVal, false);
}
 
示例4
private void setMutableFlagWithInvalidValue(CommandExecutor executor) {
    OutputAnalyzer out = getAllFlags(executor);
    String flagName = out.firstMatch(MANAGEABLE_PATTERN, 1);
    String flagVal = out.firstMatch(MANAGEABLE_PATTERN, 2);

    System.out.println("### Setting a mutable flag '" + flagName + "' to an invalid value");

    if (flagVal == null) {
        System.err.println(out.getOutput());
        throw new Error("Can not find a boolean manageable flag");
    }

    // a boolean flag accepts only 0/1 as its value
    out = executor.execute("VM.set_flag " + flagName + " unexpected_value");
    out.stderrShouldBeEmpty();
    out.stdoutShouldContain("flag value must be a boolean (1/0 or true/false)");

    out = getAllFlags(executor);

    String newFlagVal = out.firstMatch(MANAGEABLE_PATTERN.replace("(\\S+)", flagName), 1);

    assertEquals(newFlagVal, flagVal);
}
 
示例5
private void setImmutableFlag(CommandExecutor executor) {
    OutputAnalyzer out = getAllFlags(executor);
    String flagName = out.firstMatch(IMMUTABLE_PATTERN, 1);
    String flagVal = out.firstMatch(IMMUTABLE_PATTERN, 2);

    System.out.println("### Setting an immutable flag '" + flagName + "'");

    if (flagVal == null) {
        System.err.println(out.getOutput());
        throw new Error("Can not find an immutable uintx flag");
    }

    Long numVal = Long.parseLong(flagVal);

    out = executor.execute("VM.set_flag " + flagName + " " + (numVal + 1));
    out.stderrShouldBeEmpty();
    out.stdoutShouldContain("only 'writeable' flags can be set");

    out = getAllFlags(executor);

    String newFlagVal = out.firstMatch(IMMUTABLE_PATTERN.replace("(\\S+)", flagName), 1);

    assertEquals(newFlagVal, flagVal);
}
 
示例6
private static void run(CommandExecutor executor) {
    o2 = new MyObject();
    o2 = null;
    System.out.println("running GC.run_finalization");
    System.gc();
    executor.execute("GC.run_finalization");

    System.out.println("Waiting for finalization");

    try {
        finBlockLatch.await();
        if (wasFinalized) {
            System.out.println(PASSED + ": Object was finalized");
        } else {
            fail("Object was not finalized");
        }
    } catch (InterruptedException e) {
        fail("Interrupted while waiting for finalization", e);
    }
}
 
示例7
public void run(CommandExecutor executor) {
    try {
        lock.lock();
        for(int i = 0; i < objectsCount; ++i) {
            new MyObject();
        }
        System.out.println("Objects initialized: " + objectsCount);
        System.gc();

        while(wasTrapped < 1) {
            // Waiting for gc thread.
        }

        OutputAnalyzer output = executor.execute(cmd);
        output.shouldContain("MyObject");
    } finally {
        lock.unlock();
    }
}
 
示例8
public void testCompatibility(CommandExecutor executor, int comp_level) throws Exception {

        // Call all validation twice to catch error when overwriting a directive
        // Flag is default on
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));

        // load directives that turn it off
        executor.execute("Compiler.directives_add " + control_off);
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));

        // remove and see that it is true again
        executor.execute("Compiler.directives_remove");
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
    }
 
示例9
public void testCompatibility(CommandExecutor executor, int comp_level) throws Exception {

        // Call all validation twice to catch error when overwriting a directive
        // Flag is default off
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));

        // load directives that turn it on
        executor.execute("Compiler.directives_add " + control_on);
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));

        // remove and see that it is false again
        executor.execute("Compiler.directives_remove");
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
    }
 
示例10
public void testCompatibility(CommandExecutor executor, int comp_level) throws Exception {

        // Call all validation twice to catch error when overwriting a directive
        // Flag is default on
        expect(WB.getBooleanVMFlag("PrintAssembly"));
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));

        // load directives that turn it off
        executor.execute("Compiler.directives_add " + control_off);
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));

        // remove and see that it is true again
        executor.execute("Compiler.directives_remove");
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(WB.shouldPrintAssembly(nomatch, comp_level));
    }
 
示例11
public void testCompatibility(CommandExecutor executor, int comp_level) throws Exception {

        // Call all validation twice to catch error when overwriting a directive
        // Flag is default off
        expect(!WB.getBooleanVMFlag("PrintAssembly"));
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));

        // load directives that turn it on
        executor.execute("Compiler.directives_add " + control_on);
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
        expect(WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));

        // remove and see that it is true again
        executor.execute("Compiler.directives_remove");
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
        expect(!WB.shouldPrintAssembly(method, comp_level));
        expect(!WB.shouldPrintAssembly(nomatch, comp_level));
    }
 
示例12
public static OutputAnalyzer jcmd(String... args) {
    String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
    CommandExecutor executor = new PidJcmdExecutor();
    OutputAnalyzer oa = executor.execute(argsString);
    oa.shouldHaveExitValue(0);
    return oa;
}
 
示例13
public static OutputAnalyzer jcmd(int expectedExitValue, String... args) {
    String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
    CommandExecutor executor = new PidJcmdExecutor();
    OutputAnalyzer oa = executor.execute(argsString);
    oa.shouldHaveExitValue(expectedExitValue);
    return oa;
}
 
示例14
public static OutputAnalyzer jcmd(String... args) {
    String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
    CommandExecutor executor = new PidJcmdExecutor();
    OutputAnalyzer oa = executor.execute(argsString);
    oa.shouldHaveExitValue(0);
    return oa;
}
 
示例15
public static OutputAnalyzer jcmd(int expectedExitValue, String... args) {
    String argsString = Arrays.stream(args).collect(Collectors.joining(" "));
    CommandExecutor executor = new PidJcmdExecutor();
    OutputAnalyzer oa = executor.execute(argsString);
    oa.shouldHaveExitValue(expectedExitValue);
    return oa;
}
 
示例16
public void run(CommandExecutor executor) {
    OutputAnalyzer output = executor.execute("VM.flags");

    /* The following are interpreted by the JVM as actual "flags" */
    output.shouldContain("-XX:+UnlockDiagnosticVMOptions");
    output.shouldContain("-XX:+IgnoreUnrecognizedVMOptions");
    output.shouldContain("-XX:-TieredCompilation");

    /* The following are not */
    output.shouldNotContain("-Xmx129m");
    output.shouldNotContain("-XX:+ThereShouldNotBeAnyVMOptionNamedLikeThis_Right");
}
 
示例17
public void run(CommandExecutor executor) {
    executor.execute("GC.run");

    Path gcLogPath = Paths.get("RunGC.gclog").toAbsolutePath();
    String gcLog = null;

    try {
        gcLog = new String(Files.readAllBytes(gcLogPath));
    } catch (IOException e) {
        Assert.fail("Test error: Could not read GC log file: " + gcLogPath, e);
    }

    OutputAnalyzer output = new OutputAnalyzer(gcLog, "");
    output.shouldContain("Pause Full (Diagnostic Command)");
}
 
示例18
public void run(CommandExecutor executor) {
    OutputAnalyzer output = executor.execute("GC.class_histogram " + classHistogramArgs);

    /*
     * example output:
     *  num     #instances         #bytes  class name (module)
     * -------------------------------------------------------
     *    1:          7991         757792  [B ([email protected])
     *    2:          1811         217872  java.lang.Class ([email protected])
     *    3:          6724         215168  java.util.HashMap$Node ([email protected])
     *    4:          7852         188448  java.lang.String ([email protected])
     *    5:          1378         105040  [Ljava.util.HashMap$Node; ([email protected])
     *    6:          1863          95096  [Ljava.lang.Object; ([email protected])

     * ...
     */

    /* Require at least one java.lang.Class */
    output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.Class \\([email protected]\\S*\\)\\s*$");

    /* Require at least one java.lang.String */
    output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.String \\([email protected]\\S*\\)\\s*$");

    /* Require at least one java.lang.Object */
    output.shouldMatch("^\\s+\\d+:\\s+\\d+\\s+\\d+\\s+java.lang.Object \\([email protected]\\S*\\)\\s*$");

    /* Require at exactly one TestClass[] */
    output.shouldMatch("^\\s+\\d+:\\s+1\\s+\\d+\\s+" +
        Pattern.quote(TestClass[].class.getName()) + "\\s*$");

    /* Require at exactly 1024 TestClass */
    output.shouldMatch("^\\s+\\d+:\\s+1024\\s+\\d+\\s+" +
        Pattern.quote(TestClass.class.getName()) + "\\s*$");
}
 
示例19
public void run(CommandExecutor executor) throws IOException {
    File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof");
    if (dump.exists()) {
        dump.delete();
    }

    String cmd = "GC.heap_dump " + heapDumpArgs + " " + dump.getAbsolutePath();
    executor.execute(cmd);

    verifyHeapDump(dump);
    dump.delete();
}
 
示例20
public void run(CommandExecutor executor) {

        if (Platform.isServer() && !Platform.isEmulatedClient()) {
            filename = System.getProperty("test.src", ".") + File.separator + "control2.txt";
        } else {
            filename = System.getProperty("test.src", ".") + File.separator + "control1.txt";
        }
        testPrintCommand(executor);
        testAddAndRemoveCommand(executor);
    }
 
示例21
public static void testPrintCommand(CommandExecutor executor) {

        // Get output from dcmd (diagnostic command)
        OutputAnalyzer output = executor.execute("Compiler.directives_print");
        int count = find(output, "Directive:");
        if (count < 1) {
            Assert.fail("Expected at least one directive - found " + count);
        }
    }
 
示例22
public void run(CommandExecutor executor) {
    OutputAnalyzer output = executor.execute("help");

    output.shouldContain("The following commands are available");
    output.shouldContain("help");
    output.shouldContain("VM.version");
}
 
示例23
protected OutputAnalyzer[] executeJCMD(int pid) {
    int size = jcmdCommands.size();
    OutputAnalyzer[] outputArray = new OutputAnalyzer[size];
    CommandExecutor jcmdExecutor = new PidJcmdExecutor(String.valueOf(pid));
    for (int i = 0; i < size; i++) {
        outputArray[i] = jcmdExecutor.execute(jcmdCommands.get(i));
    }
    return outputArray;
}
 
示例24
public void run(CommandExecutor executor) throws Exception {

        control_on = System.getProperty("test.src", ".") + File.separator + "control_on.txt";
        control_off = System.getProperty("test.src", ".") + File.separator + "control_off.txt";
        method  = getMethod(TestCompilerDirectivesCompatibilityBase.class, "helper");
        nomatch = getMethod(TestCompilerDirectivesCompatibilityBase.class, "another");

        int[] levels = CompilerUtils.getAvailableCompilationLevels();
        for (int complevel : levels) {
            // Only test the major compilers, ignore profiling levels
            if (complevel == CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE || complevel == CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION){
                testCompatibility(executor, complevel);
            }
        }
    }
 
示例25
public void run(CommandExecutor executor) {
    OutputAnalyzer output = executor.execute("VM.command_line");
    output.shouldContain("-XX:+IgnoreUnrecognizedVMOptions");
    output.shouldContain("-XX:+ThereShouldNotBeAnyVMOptionNamedLikeThis");
}
 
示例26
public void run(CommandExecutor executor) {
    setMutableFlag(executor);
    setMutableFlagWithInvalidValue(executor);
    setImmutableFlag(executor);
    setNonExistingFlag(executor);
}
 
示例27
private OutputAnalyzer getAllFlags(CommandExecutor executor) {
    return executor.execute("VM.flags -all", true);
}
 
示例28
public void run(CommandExecutor executor) {
    System.setProperty(PROPERTY_NAME, PROPERTY_VALUE);

    OutputAnalyzer output = executor.execute("VM.system_properties");
    output.shouldContain(PROPERTY_NAME + "=" + PROPERTY_VALUE);
}
 
示例29
public void run(CommandExecutor executor) {
    String cmd = "GC.heap_info";
    OutputAnalyzer output = executor.execute(cmd);
    output.shouldContain("Metaspace");
}
 
示例30
public void run(CommandExecutor executor) {
    OutputAnalyzer output = executor.execute("JVMTI.data_dump");

    output.stderrShouldBeEmpty();
}