1 /* 2 * Copyright (c) 2012, 2013, 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 * @build com.oracle.java.testlibrary.* 30 * @run main/othervm/timeout=600 Test6941923 31 * 32 */ 33 import com.oracle.java.testlibrary.*; 34 import java.io.File; 35 import java.io.FilenameFilter; 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 39 class GCLoggingGenerator { 40 41 public static void main(String[] args) throws Exception { 42 43 long sizeOfLog = Long.parseLong(args[0]); 44 long lines = sizeOfLog / 80; 45 // full.GC generates ad least 1-line which is not shorter then 80 chars 46 // for some GC 2 shorter lines are generated 47 for (long i = 0; i < lines; i++) { 48 System.gc(); 49 } 50 } 51 } 52 53 public class Test6941923 { 54 55 static final File currentDirectory = new File("."); 56 static final String logFileName = "test.log"; 57 static final int logFileSizeK = 16; 58 static FilenameFilter logFilter = new FilenameFilter() { 59 @Override 60 public boolean accept(File dir, String name) { 61 return name.startsWith(logFileName); 62 } 63 }; 64 65 public static void cleanLogs() { 66 for (File log : currentDirectory.listFiles(logFilter)) { 67 if (!log.delete()) { 68 throw new Error("Unable to delete " + log.getAbsolutePath()); 69 } 70 } 71 } 72 73 public static void runTest(int numberOfFiles) throws Exception { 74 75 ArrayList<String> args = new ArrayList(); 76 String[] logOpts = new String[]{ 77 "-cp", System.getProperty("java.class.path"), 78 "-Xloggc:" + logFileName, 79 "-XX:-DisableExplicitGC", // to sure that System.gc() works 80 "-XX:+PrintGC", "-XX:+PrintGCDetails", "-XX:+UseGCLogFileRotation", 81 "-XX:NumberOfGCLogFiles=" + numberOfFiles, 82 "-XX:GCLogFileSize=" + logFileSizeK + "K", "-Xmx128M"}; 83 // System.getProperty("test.java.opts") is '' if no options is set 84 // need to skip such empty 85 String[] externalVMopts = System.getProperty("test.java.opts").length() == 0 86 ? new String[0] 87 : System.getProperty("test.java.opts").split(" "); 88 args.addAll(Arrays.asList(externalVMopts)); 89 args.addAll(Arrays.asList(logOpts)); 90 args.add(GCLoggingGenerator.class.getName()); 91 args.add(String.valueOf(numberOfFiles * logFileSizeK * 1024)); 92 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args.toArray(new String[0])); 93 pb.redirectErrorStream(true); 94 pb.redirectOutput(new File(GCLoggingGenerator.class.getName() + ".log")); 95 Process process = pb.start(); 96 int result = process.waitFor(); 97 if (result != 0) { 98 throw new Error("Unexpected exit code = " + result); 99 } 100 File[] logs = currentDirectory.listFiles(logFilter); 101 int smallFilesNumber = 0; 102 for (File log : logs) { 103 if (log.length() < logFileSizeK * 1024) { 104 smallFilesNumber++; 105 } 106 } 107 if (logs.length != numberOfFiles) { 108 throw new Error("There are only " + logs.length + " logs instead " + numberOfFiles); 109 } 110 if (smallFilesNumber > 1) { 111 throw new Error("There should maximum one log with size < " + logFileSizeK + "K"); 112 } 113 } 114 115 public static void main(String[] args) throws Exception { 116 cleanLogs(); 117 runTest(1); 118 cleanLogs(); 119 runTest(3); 120 cleanLogs(); 121 } 122 }