1 /*
   2  * Copyright (c) 2012, 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 new added flags for gc log rotation
  28  * @library /testlibrary
  29  * @run main/othervm/timeout=600 Test6941923
  30  * 
  31  */
  32 import com.oracle.java.testlibrary.*;
  33 import java.io.File;
  34 import java.io.FilenameFilter;
  35 import java.util.ArrayList;
  36 
  37 class GarbageGenerator {
  38     
  39     static int cap = 20000;
  40     
  41     // don't let C2 make any optimization    
  42     public static ArrayList< byte[]> tmpList = new ArrayList(cap);
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         long limit = Long.parseLong(args[0]) * 60L * 1000L;   // minutes        
  47         int fix_size = 2048;
  48         int loop = 0;
  49         long time1 = System.currentTimeMillis();
  50         long time2 = System.currentTimeMillis();
  51         while (time2 - time1 < limit) {            
  52             byte[] g = new byte[fix_size];
  53             tmpList.add(g);
  54             loop++;
  55             if (loop > cap) {
  56                 tmpList = null;
  57                 cap *= 2;
  58                 if (cap > 80000) {
  59                     cap = 80000;
  60                 }
  61                 tmpList = new ArrayList(cap);
  62             }
  63             time2 = System.currentTimeMillis();
  64         }
  65 
  66         
  67         System.out.print("\r... " + (time2 - time1) / 1000 + " seconds");
  68 
  69     }
  70 }
  71 
  72 public class Test6941923 {
  73 
  74     static final File currentDirectory = new File(System.getProperty("user.dir"));
  75     static final String logFileName = "test.log";
  76     static final int logFileSizeK = 16;
  77     static FilenameFilter logFilter = new FilenameFilter() {
  78         @Override
  79         public boolean accept(File dir, String name) {
  80             return name.startsWith(logFileName);
  81         }
  82     };
  83 
  84     public static void cleanLogs() {
  85         for (File log : currentDirectory.listFiles(logFilter)) {
  86             if (!log.delete()) {
  87                 throw new Error("Unable to delete " + log.getAbsolutePath());
  88             }
  89         }
  90     }
  91 
  92     public static void runTest(int numberOfFiles) throws Exception {
  93 
  94         String minutesToRun = String.valueOf(numberOfFiles);
  95         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-cp", System.getProperty("java.class.path"), "-Xloggc:" + logFileName,
  96                 "-XX:+PrintGC", "-XX:+PrintGCDetails", "-XX:+UseGCLogFileRotation", "-XX:NumberOfGCLogFiles=" + numberOfFiles,
  97                 "-XX:GCLogFileSize=" + logFileSizeK + "K", "-Xmx128M", GarbageGenerator.class.getName(), minutesToRun);
  98         Process process = pb.start();
  99         int result = process.waitFor();
 100         if (result != 0) {
 101             throw new Error("Unexpected exit code = " + result);
 102         }
 103         File[] logs = currentDirectory.listFiles(logFilter);
 104         int smallFilesNumber = 0;
 105         for (File log : logs) {
 106             if (log.length() < logFileSizeK * 1024) {
 107                 smallFilesNumber++;
 108             }
 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 }