1 /*
   2 * Copyright (c) 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 TestNewSizeThreadIncrease
  26  * @key gc
  27  * @bug 8144527
  28  * @summary Tests argument processing for NewSizeThreadIncrease
  29  * @library /testlibrary
  30  * @modules java.base/sun.misc
  31  *          java.management
  32  */
  33 
  34 // * @requires vm.gc=="Serial"
  35 
  36 import jdk.test.lib.*;
  37 
  38 // Range of NewSizeThreadIncrease is 0 ~ max_uintx.
  39 // Total of 5 threads will be created (1 GCTest thread and 4 TestThread).
  40 public class TestNewSizeThreadIncrease {
  41   static final String VALID_VALUE = "2097152"; // 2MB
  42 
  43   // This value will make an overflow of 'thread count * NewSizeThreadIncrease' at DefNewGeneration::compute_new_size().
  44   // = (max_uintx / 5) + 1, = (18446744073709551615 / 5) + 1
  45   static String INVALID_VALUE_1 = "3689348814741910324";
  46 
  47   // This string is contained when compute_new_size() expands or shrinks.
  48   static final String LOG_NEWSIZE_CHANGED = "New generation size ";
  49 
  50   public static void main(String[] args) throws Exception {
  51     if (Platform.is32bit()) {
  52       // (max_uintx / 5) + 1, 4294967295/5 + 1
  53       INVALID_VALUE_1 = "858993460";
  54     }
  55 
  56     // New size will be applied as NewSizeThreadIncrease is small enough to expand.
  57     runNewSizeThreadIncreaseTest(VALID_VALUE, true);
  58 
  59     // New size will be ignored as 'thread count * NewSizeThreadIncrease' overflows.
  60     runNewSizeThreadIncreaseTest(INVALID_VALUE_1, false);
  61   }
  62 
  63   static void runNewSizeThreadIncreaseTest(String expectedValue, boolean isNewsizeChanged) throws Exception {
  64     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseSerialGC",
  65                                                               "-Xms96M",
  66                                                               "-Xmx128M",
  67                                                               "-XX:NewRatio=2",
  68                                                               "-Xlog:gc+heap+ergo=debug",
  69                                                               "-XX:NewSizeThreadIncrease="+expectedValue,
  70                                                               GCTest.class.getName());
  71     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  72 
  73     output.shouldHaveExitValue(0);
  74 
  75     if (isNewsizeChanged) {
  76       output.shouldContain(LOG_NEWSIZE_CHANGED);
  77     } else {
  78       output.shouldNotContain(LOG_NEWSIZE_CHANGED);
  79     }
  80   }
  81 
  82   static class GCTest {
  83 
  84     static final int MAX_THREADS_COUNT = 4;
  85     static TestThread threads[] = new TestThread[MAX_THREADS_COUNT];
  86 
  87     public static void main(String[] args) {
  88 
  89       System.out.println("Creating garbage");
  90 
  91       for (int i=0; i<MAX_THREADS_COUNT; i++) {
  92         threads[i] = new TestThread();
  93         threads[i].start();
  94       }
  95 
  96       System.gc();
  97 
  98       for (int i=0; i<MAX_THREADS_COUNT; i++) {
  99         threads[i].stopRunning();
 100       }
 101 
 102       System.out.println("Done");
 103     }
 104 
 105     private static class TestThread extends Thread {
 106 
 107       volatile boolean isRunning = true;
 108 
 109       public void run() {
 110         while (isRunning == true) {
 111           try {
 112             Thread.sleep(10);
 113           } catch (Throwable t) {
 114           }
 115         }
 116       }
 117 
 118       public void stopRunning() {
 119         isRunning = false;
 120       }
 121     }
 122   }
 123 }