hotspot/test/stress/gc/TestMultiThreadStressRSet.java
changeset 37713 a3c34538726f
parent 37712 a34269e72fe1
parent 37646 84aba7335005
child 37714 7a0b1c7e7054
equal deleted inserted replaced
37712:a34269e72fe1 37713:a3c34538726f
     1 /*
       
     2  * Copyright (c) 2016, 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 import java.io.PrintStream;
       
    25 import java.util.ArrayList;
       
    26 import java.util.List;
       
    27 import java.util.Map;
       
    28 import java.util.Random;
       
    29 import sun.hotspot.WhiteBox;
       
    30 
       
    31 /*
       
    32  * @test TestMultiThreadStressRSet.java
       
    33  * @key stress
       
    34  * @requires vm.gc=="G1" | vm.gc=="null"
       
    35  * @requires os.maxMemory > 2G
       
    36  *
       
    37  * @summary Stress G1 Remembered Set using multiple threads
       
    38  * @library /test/lib /testlibrary
       
    39  * @build sun.hotspot.WhiteBox
       
    40  * @run main ClassFileInstaller sun.hotspot.WhiteBox
       
    41  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
       
    42  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
       
    43  *   -XX:+UseG1GC -XX:G1SummarizeRSetStatsPeriod=1 -Xlog:gc
       
    44  *   -Xmx500m -XX:G1HeapRegionSize=1m -XX:MaxGCPauseMillis=1000 TestMultiThreadStressRSet 10 4
       
    45  *
       
    46  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
       
    47  *   -XX:+UseG1GC -XX:G1SummarizeRSetStatsPeriod=100 -Xlog:gc
       
    48  *   -Xmx1G -XX:G1HeapRegionSize=8m -XX:MaxGCPauseMillis=1000 TestMultiThreadStressRSet 60 16
       
    49  *
       
    50  * @run main/othervm/timeout=700 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
       
    51  *   -XX:+UseG1GC -XX:G1SummarizeRSetStatsPeriod=100 -Xlog:gc
       
    52  *   -Xmx500m -XX:G1HeapRegionSize=1m -XX:MaxGCPauseMillis=1000 TestMultiThreadStressRSet 600 32
       
    53  */
       
    54 public class TestMultiThreadStressRSet {
       
    55 
       
    56     private static final Random RND = new Random(2015 * 2016);
       
    57     private static final WhiteBox WB = WhiteBox.getWhiteBox();
       
    58     private static final int REF_SIZE = WB.getHeapOopSize();
       
    59     private static final int REGION_SIZE = WB.g1RegionSize();
       
    60 
       
    61     // How many regions to use for the storage
       
    62     private static final int STORAGE_REGIONS = 20;
       
    63 
       
    64     // Size a single obj in the storage
       
    65     private static final int OBJ_SIZE = 1024;
       
    66 
       
    67     // How many regions of young/old gen to use in the BUFFER
       
    68     private static final int BUFFER_YOUNG_REGIONS = 60;
       
    69     private static final int BUFFER_OLD_REGIONS = 40;
       
    70 
       
    71     // Total number of objects in the storage.
       
    72     private final int N;
       
    73 
       
    74     // The storage of byte[]
       
    75     private final List<Object> STORAGE;
       
    76 
       
    77     // Where references to the Storage will be stored
       
    78     private final List<Object[]> BUFFER;
       
    79 
       
    80     // The length of a buffer element.
       
    81     // RSet deals with "cards" (areas of 512 bytes), not with single refs
       
    82     // So, to affect the RSet the BUFFER refs should be allocated in different
       
    83     // memory cards.
       
    84     private final int BUF_ARR_LEN = 100 * (512 / REF_SIZE);
       
    85 
       
    86     // Total number of objects in the young/old buffers
       
    87     private final int YOUNG;
       
    88     private final int OLD;
       
    89 
       
    90     // To cause Remembered Sets change their coarse level the test uses a window
       
    91     // within STORAGE. All the BUFFER elements refer to only STORAGE objects
       
    92     // from the current window. The window is defined by a range.
       
    93     // The first element has got the index: 'windowStart',
       
    94     // the last one: 'windowStart + windowSize - 1'
       
    95     // The window is shifting periodically.
       
    96     private int windowStart;
       
    97     private final int windowSize;
       
    98 
       
    99     // Counter of created worker threads
       
   100     private int counter = 0;
       
   101 
       
   102     private volatile String errorMessage = null;
       
   103     private volatile boolean isEnough = false;
       
   104 
       
   105     public static void main(String args[]) {
       
   106         if (args.length != 2) {
       
   107             throw new IllegalArgumentException("TEST BUG: wrong arg count " + args.length);
       
   108         }
       
   109         long time = Long.parseLong(args[0]);
       
   110         int threads = Integer.parseInt(args[1]);
       
   111         new TestMultiThreadStressRSet().test(time * 1000, threads);
       
   112     }
       
   113 
       
   114     /**
       
   115      * Initiates test parameters, fills out the STORAGE and BUFFER.
       
   116      */
       
   117     public TestMultiThreadStressRSet() {
       
   118 
       
   119         N = (REGION_SIZE - 1) * STORAGE_REGIONS / OBJ_SIZE + 1;
       
   120         STORAGE = new ArrayList<>(N);
       
   121         int bytes = OBJ_SIZE - 20;
       
   122         for (int i = 0; i < N - 1; i++) {
       
   123             STORAGE.add(new byte[bytes]);
       
   124         }
       
   125         STORAGE.add(new byte[REGION_SIZE / 2 + 100]); // humongous
       
   126         windowStart = 0;
       
   127         windowSize = REGION_SIZE / OBJ_SIZE;
       
   128 
       
   129         BUFFER = new ArrayList<>();
       
   130         int sizeOfBufferObject = 20 + REF_SIZE * BUF_ARR_LEN;
       
   131         OLD = REGION_SIZE * BUFFER_OLD_REGIONS / sizeOfBufferObject;
       
   132         YOUNG = REGION_SIZE * BUFFER_YOUNG_REGIONS / sizeOfBufferObject;
       
   133         for (int i = 0; i < OLD + YOUNG; i++) {
       
   134             BUFFER.add(new Object[BUF_ARR_LEN]);
       
   135         }
       
   136     }
       
   137 
       
   138     /**
       
   139      * Does the testing. Steps:
       
   140      * <ul>
       
   141      * <li> starts the Shifter thread
       
   142      * <li> during the given time starts new Worker threads, keeping the number
       
   143      * of live thread under limit.
       
   144      * <li> stops the Shifter thread
       
   145      * </ul>
       
   146      *
       
   147      * @param timeInMillis how long to stress
       
   148      * @param maxThreads the maximum number of Worker thread working together.
       
   149      */
       
   150     public void test(long timeInMillis, int maxThreads) {
       
   151         if (timeInMillis <= 0 || maxThreads <= 0) {
       
   152             throw new IllegalArgumentException("TEST BUG: be positive!");
       
   153         }
       
   154         System.out.println("%% Time to work: " + timeInMillis / 1000 + "s");
       
   155         System.out.println("%% Number of threads: " + maxThreads);
       
   156         long finish = System.currentTimeMillis() + timeInMillis;
       
   157         Shifter shift = new Shifter(this, 1000, (int) (windowSize * 0.9));
       
   158         shift.start();
       
   159         for (int i = 0; i < maxThreads; i++) {
       
   160             new Worker(this, 100).start();
       
   161         }
       
   162         try {
       
   163             while (System.currentTimeMillis() < finish && errorMessage == null) {
       
   164                 Thread.sleep(100);
       
   165             }
       
   166         } catch (Throwable t) {
       
   167             printAllStackTraces(System.err);
       
   168             t.printStackTrace(System.err);
       
   169             this.errorMessage = t.getMessage();
       
   170         } finally {
       
   171             isEnough = true;
       
   172         }
       
   173         System.out.println("%% Total work cycles: " + counter);
       
   174         if (errorMessage != null) {
       
   175             throw new RuntimeException(errorMessage);
       
   176         }
       
   177     }
       
   178 
       
   179     /**
       
   180      * Returns an element from from the BUFFER (an object array) to keep
       
   181      * references to the storage.
       
   182      *
       
   183      * @return an Object[] from buffer.
       
   184      */
       
   185     private Object[] getFromBuffer() {
       
   186         int index = counter % (OLD + YOUNG);
       
   187         synchronized (BUFFER) {
       
   188             if (index < OLD) {
       
   189                 if (counter % 100 == (counter / 100) % 100) {
       
   190                     // need to generate garbage in the old gen to provoke mixed GC
       
   191                     return replaceInBuffer(index);
       
   192                 } else {
       
   193                     return BUFFER.get(index);
       
   194                 }
       
   195             } else {
       
   196                 return replaceInBuffer(index);
       
   197             }
       
   198         }
       
   199     }
       
   200 
       
   201     private Object[] replaceInBuffer(int index) {
       
   202         Object[] objs = new Object[BUF_ARR_LEN];
       
   203         BUFFER.set(index, objs);
       
   204         return objs;
       
   205     }
       
   206 
       
   207     /**
       
   208      * Returns a random object from the current window within the storage.
       
   209      * A storage element with index from windowStart to windowStart+windowSize.
       
   210      *
       
   211      * @return a random element from the current window within the storage.
       
   212      */
       
   213     private Object getRandomObject() {
       
   214         int index = (windowStart + RND.nextInt(windowSize)) % N;
       
   215         return STORAGE.get(index);
       
   216     }
       
   217 
       
   218     private static void printAllStackTraces(PrintStream ps) {
       
   219         Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
       
   220         for (Thread t : traces.keySet()) {
       
   221             ps.println(t.toString() + " " + t.getState());
       
   222             for (StackTraceElement traceElement : traces.get(t)) {
       
   223                 ps.println("\tat " + traceElement);
       
   224             }
       
   225         }
       
   226     }
       
   227 
       
   228     /**
       
   229      * Thread to create a number of references from BUFFER to STORAGE.
       
   230      */
       
   231     private static class Worker extends Thread {
       
   232 
       
   233         final TestMultiThreadStressRSet boss;
       
   234         final int refs; // number of refs to OldGen
       
   235 
       
   236         /**
       
   237          * @param boss the tests
       
   238          * @param refsToOldGen how many references to the OldGen to create
       
   239          */
       
   240         Worker(TestMultiThreadStressRSet boss, int refsToOldGen) {
       
   241             this.boss = boss;
       
   242             this.refs = refsToOldGen;
       
   243         }
       
   244 
       
   245         @Override
       
   246         public void run() {
       
   247             try {
       
   248                 while (!boss.isEnough) {
       
   249                     Object[] objs = boss.getFromBuffer();
       
   250                     int step = objs.length / refs;
       
   251                     for (int i = 0; i < refs; i += step) {
       
   252                         objs[i] = boss.getRandomObject();
       
   253                     }
       
   254                     boss.counter++;
       
   255                 }
       
   256             } catch (Throwable t) {
       
   257                 t.printStackTrace(System.out);
       
   258                 boss.errorMessage = t.getMessage();
       
   259             }
       
   260         }
       
   261     }
       
   262 
       
   263     /**
       
   264      * Periodically shifts the current STORAGE window, removing references
       
   265      * in BUFFER that refer to objects outside the window.
       
   266      */
       
   267     private static class Shifter extends Thread {
       
   268 
       
   269         final TestMultiThreadStressRSet boss;
       
   270         final int sleepTime;
       
   271         final int shift;
       
   272 
       
   273         Shifter(TestMultiThreadStressRSet boss, int sleepTime, int shift) {
       
   274             this.boss = boss;
       
   275             this.sleepTime = sleepTime;
       
   276             this.shift = shift;
       
   277         }
       
   278 
       
   279         @Override
       
   280         public void run() {
       
   281             try {
       
   282                 while (!boss.isEnough) {
       
   283                     Thread.sleep(sleepTime);
       
   284                     boss.windowStart += shift;
       
   285                     for (int i = 0; i < boss.OLD; i++) {
       
   286                         Object[] objs = boss.BUFFER.get(i);
       
   287                         for (int j = 0; j < objs.length; j++) {
       
   288                             objs[j] = null;
       
   289                         }
       
   290                     }
       
   291                     if (!WB.g1InConcurrentMark()) {
       
   292                         System.out.println("%% start CMC");
       
   293                         WB.g1StartConcMarkCycle();
       
   294                     } else {
       
   295                         System.out.println("%% CMC is already in progress");
       
   296                     }
       
   297                 }
       
   298             } catch (Throwable t) {
       
   299                 t.printStackTrace(System.out);
       
   300                 boss.errorMessage = t.getMessage();
       
   301             }
       
   302         }
       
   303     }
       
   304 }
       
   305