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