hotspot/test/serviceability/logging/TestLogRotation.java
author ysuenaga
Wed, 04 May 2016 21:53:01 -0400
changeset 38195 d317aafcc87c
parent 37477 0ca0b7388bb6
child 40631 ed82623d7831
permissions -rw-r--r--
8153073: UL: Set filesize option with k/m/g Reviewed-by: mlarsson, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     1
/*
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     4
 *
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     7
 * published by the Free Software Foundation.
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     8
 *
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    13
 * accompanied this code).
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    14
 *
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    18
 *
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    21
 * questions.
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    22
 */
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    23
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    24
/*
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    25
 * @test TestLogRotation.java
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    26
 * @summary test flags for log rotation
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    27
 * @library /testlibrary
36851
03e2f4d0a421 8153737: Unsupported Module
chegar
parents: 35061
diff changeset
    28
 * @modules java.base/jdk.internal.misc
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    29
 *          java.management
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    30
 * @run main/othervm/timeout=600 TestLogRotation
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    31
 *
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    32
 */
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    33
import jdk.test.lib.*;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    34
import java.io.File;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    35
import java.io.FilenameFilter;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    36
import java.util.ArrayList;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    37
import java.util.Arrays;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    38
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    39
class GCLoggingGenerator {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    40
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    42
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    43
        long sizeOfLog = Long.parseLong(args[0]);
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    44
        long lines = sizeOfLog / 80;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    45
        // full.GC generates ad least 1-line which is not shorter then 80 chars
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    46
        // for some GC 2 shorter lines are generated
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    47
        for (long i = 0; i < lines; i++) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    48
            System.gc();
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    49
        }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    50
    }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    51
}
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    52
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    53
public class TestLogRotation {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    54
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    55
    static final File currentDirectory = new File(".");
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    56
    static final String logFileName = "test.log";
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    57
    static final int logFileSizeK = 16;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    58
    static FilenameFilter logFilter = new FilenameFilter() {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    59
        @Override
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    60
        public boolean accept(File dir, String name) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    61
            return name.startsWith(logFileName);
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    62
        }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    63
    };
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    64
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    65
    public static void cleanLogs() {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    66
        for (File log : currentDirectory.listFiles(logFilter)) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    67
            if (!log.delete()) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    68
                throw new Error("Unable to delete " + log.getAbsolutePath());
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    69
            }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    70
        }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    71
    }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    72
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    73
    public static void runTest(int numberOfFiles) throws Exception {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    74
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    75
        ArrayList<String> args = new ArrayList();
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    76
        String[] logOpts = new String[]{
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    77
            "-cp", System.getProperty("java.class.path"),
38195
d317aafcc87c 8153073: UL: Set filesize option with k/m/g
ysuenaga
parents: 37477
diff changeset
    78
            "-Xlog:gc=debug:" + logFileName + "::filesize=" + logFileSizeK + "k,filecount=" + numberOfFiles,
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    79
            "-XX:-DisableExplicitGC", // to ensure that System.gc() works
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    80
            "-Xmx128M"};
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    81
        // System.getProperty("test.java.opts") is '' if no options is set
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    82
        // need to skip such empty
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    83
        String[] externalVMopts = System.getProperty("test.java.opts").length() == 0
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    84
                ? new String[0]
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    85
                : System.getProperty("test.java.opts").split(" ");
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    86
        args.addAll(Arrays.asList(externalVMopts));
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    87
        args.addAll(Arrays.asList(logOpts));
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    88
        args.add(GCLoggingGenerator.class.getName());
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    89
        args.add(String.valueOf(numberOfFiles * logFileSizeK * 1024));
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    90
        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args.toArray(new String[0]));
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    91
        pb.redirectErrorStream(true);
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    92
        pb.redirectOutput(new File(GCLoggingGenerator.class.getName() + ".log"));
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    93
        Process process = pb.start();
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    94
        int result = process.waitFor();
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    95
        if (result != 0) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    96
            throw new Error("Unexpected exit code = " + result);
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    97
        }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    98
        File[] logs = currentDirectory.listFiles(logFilter);
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
    99
        int smallFilesNumber = 0;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   100
        for (File log : logs) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   101
            if (log.length() < logFileSizeK * 1024) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   102
                smallFilesNumber++;
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   103
            }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   104
        }
37465
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 35061
diff changeset
   105
        // Expect one more log file since the number-of-files doesn't include the active log file
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 35061
diff changeset
   106
        int expectedNumberOfFiles = numberOfFiles + 1;
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 35061
diff changeset
   107
        if (logs.length != expectedNumberOfFiles) {
1d5551f466ee 8146879: Add option for handling existing log files in UL
mlarsson
parents: 35061
diff changeset
   108
            throw new Error("There are " + logs.length + " logs instead of the expected " + expectedNumberOfFiles);
35061
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   109
        }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   110
        if (smallFilesNumber > 1) {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   111
            throw new Error("There should maximum one log with size < " + logFileSizeK + "K");
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   112
        }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   113
    }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   114
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   115
    public static void main(String[] args) throws Exception {
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   116
        cleanLogs();
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   117
        runTest(1);
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   118
        cleanLogs();
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   119
        runTest(3);
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   120
        cleanLogs();
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   121
    }
be6025ebffea 8145092: Use Unified Logging for the GC logging
brutisso
parents:
diff changeset
   122
}