1 /*
   2  * Copyright (c) 2017, 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 // A test that stresses a full GC by allocating objects of different lifetimes
  25 // and concurrently calling System.gc().
  26 
  27 import java.util.ArrayList;
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 import java.util.TreeMap;
  31 
  32 final class ThreadUtils {
  33     public static void sleep(long durationMS) {
  34         try {
  35             Thread.sleep(durationMS);
  36         } catch (Exception e) {
  37         }
  38     }
  39 }
  40 
  41 class Exitable {
  42     private volatile boolean shouldExit = false;
  43 
  44     protected boolean shouldExit() {
  45         return shouldExit;
  46     }
  47 
  48     public void exit() {
  49         shouldExit = true;
  50     }
  51 }
  52 
  53 class ShortLivedAllocationTask extends Exitable implements Runnable {
  54     private Map<String, String> map = new HashMap<>();
  55 
  56     @Override
  57     public void run() {
  58         map = new HashMap<>();
  59         while (!shouldExit()) {
  60             for (int i = 0; i < 200; i++) {
  61                 String key = "short" + " key = " + i;
  62                 String value = "the value is " + i;
  63                 map.put(key, value);
  64             }
  65         }
  66     }
  67 }
  68 
  69 class LongLivedAllocationTask extends Exitable implements Runnable {
  70     private Map<String, String> map;
  71 
  72     LongLivedAllocationTask(Map<String, String> map) {
  73         this.map = map;
  74     }
  75 
  76     @Override
  77     public void run() {
  78         while (!shouldExit()) {
  79             String prefix = "long" + System.currentTimeMillis();
  80             for (int i = 0; i < 10; i++) {
  81                 String key = prefix + " key = " + i;
  82                 String value = "the value is " + i;
  83                 map.put(key, value);
  84             }
  85         }
  86     }
  87 }
  88 
  89 class SystemGCTask extends Exitable implements Runnable {
  90     private long delayMS;
  91 
  92     SystemGCTask(long delayMS) {
  93 System.err.println("SystemGCTask with delay " + delayMS);
  94         this.delayMS = delayMS;
  95     }
  96 
  97     @Override
  98     public void run() {
  99         while (!shouldExit()) {
 100             System.gc();
 101             ThreadUtils.sleep(delayMS * 10);
 102         }
 103     }
 104 }
 105 
 106 public class TestSystemGC {
 107     private static long endTime;
 108 
 109     private static final int numGroups = 7;
 110     private static final int numGCsPerGroup = 4;
 111 
 112     private static Map<String, String> longLivedMap = new TreeMap<>();
 113 
 114     private static void populateLongLived() {
 115         for (int i = 0; i < 1000000; i++) {
 116             String key = "all" + " key = " + i;
 117             String value = "the value is " + i;
 118             longLivedMap.put(key, value);
 119         }
 120     }
 121 
 122     private static long getDelayMS(int group) {
 123         if (group == 0) {
 124             return 0;
 125         }
 126 
 127         int res = 16;
 128         for (int i = 0; i < group; i++) {
 129             res *= 2;
 130         }
 131         return res;
 132     }
 133 
 134     private static void doSystemGCs() {
 135         ThreadUtils.sleep(1000);
 136 
 137         for (int i = 0; i < numGroups; i++) {
 138             for (int j = 0; j < numGCsPerGroup; j++) {
 139                System.gc();
 140                if (System.currentTimeMillis() >= endTime) {
 141                    return;
 142                }
 143                ThreadUtils.sleep(getDelayMS(i));
 144             }
 145         }
 146     }
 147 
 148     private static SystemGCTask createSystemGCTask(int group) {
 149         long delay0 = getDelayMS(group);
 150         long delay1 = getDelayMS(group + 1);
 151         long delay = delay0 + (delay1 - delay0) / 2;
 152         return new SystemGCTask(delay);
 153     }
 154 
 155     private static void startTask(Runnable task) {
 156         if (task != null) {
 157             new Thread(task).start();
 158         }
 159     }
 160 
 161     private static void exitTask(Exitable task) {
 162         if (task != null) {
 163             task.exit();
 164         }
 165     }
 166 
 167     private static void runAllPhases() {
 168         int i = 0;
 169         while (i < 4 && System.currentTimeMillis() < endTime) {
 170             SystemGCTask gcTask =
 171                 (i % 2 == 1) ? createSystemGCTask(numGroups / 3) : null;
 172             ShortLivedAllocationTask shortTask =
 173                 (i == 1 || i == 3) ?  new ShortLivedAllocationTask() : null;
 174             LongLivedAllocationTask longTask =
 175                 (i == 2 || i == 3) ? new LongLivedAllocationTask(longLivedMap) : null;
 176 
 177             startTask(gcTask);
 178             startTask(shortTask);
 179             startTask(longTask);
 180 
 181             doSystemGCs();
 182 
 183             exitTask(gcTask);
 184             exitTask(shortTask);
 185             exitTask(longTask);
 186 
 187             ThreadUtils.sleep(1000);
 188             i++;
 189         }
 190     }
 191 
 192     public static void main(String[] args) {
 193         if (args.length > 0) {
 194             int timeout = Integer.parseInt(args[0]) * 1000;
 195             System.out.println("Running with timeout of " + timeout + "ms");
 196             endTime = System.currentTimeMillis() + timeout;
 197         }
 198         // First allocate the long lived objects and then run all phases.
 199         populateLongLived();
 200         runAllPhases();
 201     }
 202 }