Path: blob/master/test/hotspot/jtreg/serviceability/logging/TestLogRotation.java
41152 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestLogRotation.java25* @summary test flags for log rotation26* @library /test/lib27* @modules java.base/jdk.internal.misc28* java.management29* @run driver/timeout=600 TestLogRotation30*31*/32import jdk.test.lib.process.ProcessTools;33import java.io.File;34import java.io.FilenameFilter;35import java.util.ArrayList;36import java.util.Arrays;3738class GCLoggingGenerator {3940public static void main(String[] args) throws Exception {4142long sizeOfLog = Long.parseLong(args[0]);43long lines = sizeOfLog / 70;44// full.GC generates ad least 1-line which is not shorter then 70 chars45// for some GC 2 shorter lines are generated46for (long i = 0; i < lines; i++) {47System.gc();48}49}50}5152public class TestLogRotation {5354static final File currentDirectory = new File(".");55static final String logFileName = "test.log";56static final int logFileSizeK = 16;57static FilenameFilter logFilter = new FilenameFilter() {58@Override59public boolean accept(File dir, String name) {60return name.startsWith(logFileName);61}62};6364public static void cleanLogs() {65for (File log : currentDirectory.listFiles(logFilter)) {66if (!log.delete()) {67throw new Error("Unable to delete " + log.getAbsolutePath());68}69}70}7172public static void runTest(int numberOfFiles) throws Exception {73ProcessBuilder pb = ProcessTools.createTestJvm(74"-cp", System.getProperty("java.class.path"),75"-Xlog:gc=debug:" + logFileName76+ "::filesize=" + logFileSizeK + "k"77+ ",filecount=" + numberOfFiles,78"-XX:-DisableExplicitGC", // to ensure that System.gc() works79"-Xmx128M",80GCLoggingGenerator.class.getName(),81String.valueOf(numberOfFiles * logFileSizeK * 1024));82pb.redirectErrorStream(true);83pb.redirectOutput(new File(GCLoggingGenerator.class.getName() + ".log"));84Process process = pb.start();85int result = process.waitFor();86if (result != 0) {87throw new Error("Unexpected exit code = " + result);88}89File[] logs = currentDirectory.listFiles(logFilter);90int smallFilesNumber = 0;91for (File log : logs) {92if (log.length() < logFileSizeK * 1024) {93smallFilesNumber++;94}95}96// Expect one more log file since the number-of-files doesn't include the active log file97int expectedNumberOfFiles = numberOfFiles + 1;98if (logs.length != expectedNumberOfFiles) {99throw new Error("There are " + logs.length + " logs instead of the expected " + expectedNumberOfFiles);100}101if (smallFilesNumber > 1) {102throw new Error("There should maximum one log with size < " + logFileSizeK + "K");103}104}105106public static void main(String[] args) throws Exception {107cleanLogs();108runTest(1);109cleanLogs();110runTest(3);111cleanLogs();112}113}114115116