hotspot/test/gc/TestGCLogRotationViaJcmd.java
author sjiang
Fri, 17 Apr 2015 10:53:31 +0200
changeset 30247 9925b07bba5b
parent 29678 dd2f3932c21e
child 30604 b8d532cb6420
permissions -rw-r--r--
8042901: Allow com.sun.management to be in a different module to java.lang.management Reviewed-by: mchung, dfuchs, erikj, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23517
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     1
/*
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 23517
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
23517
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     4
 *
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     7
 * published by the Free Software Foundation.
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     8
 *
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    13
 * accompanied this code).
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    14
 *
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    18
 *
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    21
 * questions.
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    22
 */
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    23
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    24
/*
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    25
 * @test TestGCLogRotationViaJcmd.java
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    26
 * @bug 7090324
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    27
 * @summary test for gc log rotation via jcmd
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    28
 * @library /testlibrary
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 23517
diff changeset
    29
 * @modules java.base/sun.misc
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 23517
diff changeset
    30
 *          java.management
23517
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    31
 * @run main/othervm -Xloggc:test.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=3 TestGCLogRotationViaJcmd
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    32
 *
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    33
 */
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    34
import com.oracle.java.testlibrary.*;
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    35
import java.io.File;
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    36
import java.io.FilenameFilter;
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    37
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    38
public class TestGCLogRotationViaJcmd {
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    39
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    40
    static final File currentDirectory = new File(".");
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    41
    static final String LOG_FILE_NAME = "test.log";
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    42
    static final int NUM_LOGS = 3;
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    43
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    44
    static FilenameFilter logFilter = new FilenameFilter() {
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    45
        @Override
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    46
        public boolean accept(File dir, String name) {
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    47
            return name.startsWith(LOG_FILE_NAME);
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    48
        }
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    49
    };
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    50
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    51
    public static void main(String[] args) throws Exception {
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    52
        // Grab the pid from the current java process
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    53
        String pid = Integer.toString(ProcessTools.getProcessId());
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    54
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    55
        // Create a JDKToolLauncher
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    56
        JDKToolLauncher jcmd = JDKToolLauncher.create("jcmd")
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    57
                                              .addToolArg(pid)
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    58
                                              .addToolArg("GC.rotate_log");
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    59
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    60
        for (int times = 1; times < NUM_LOGS; times++) {
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    61
            // Run jcmd <pid> GC.rotate_log
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    62
            ProcessBuilder pb = new ProcessBuilder(jcmd.getCommand());
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    63
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    64
            // Make sure we didn't crash
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    65
            OutputAnalyzer output = new OutputAnalyzer(pb.start());
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    66
            output.shouldHaveExitValue(0);
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    67
        }
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    68
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    69
        // GC log check
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    70
        File[] logs = currentDirectory.listFiles(logFilter);
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    71
        if (logs.length != NUM_LOGS) {
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    72
            throw new Error("There are only " + logs.length
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    73
                                              + " logs instead " + NUM_LOGS);
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    74
        }
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    75
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    76
    }
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    77
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    78
}
27c4307d6cda 7090324: gclog rotation via external tool
minqi
parents:
diff changeset
    79