< prev index next >

test/hotspot/jtreg/gc/stress/systemgc/TestSystemGC.java

Print this page
rev 47694 : imported patch 8190703-testsystemgc-timeout
rev 47695 : [mq]: 8190703-ashipilev-review2


  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 


 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 }


  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         this.delayMS = delayMS;
  94     }
  95 
  96     @Override
  97     public void run() {
  98         while (!shouldExit()) {
  99             System.gc();
 100             ThreadUtils.sleep(delayMS);
 101         }
 102     }
 103 }
 104 
 105 public class TestSystemGC {
 106     private static long endTime;
 107 
 108     private static final int numGroups = 7;
 109     private static final int numGCsPerGroup = 4;
 110 
 111     private static Map<String, String> longLivedMap = new TreeMap<>();
 112 
 113     private static void populateLongLived() {
 114         for (int i = 0; i < 1000000; i++) {
 115             String key = "all" + " key = " + i;
 116             String value = "the value is " + i;
 117             longLivedMap.put(key, value);
 118         }
 119     }
 120 


 147     private static SystemGCTask createSystemGCTask(int group) {
 148         long delay0 = getDelayMS(group);
 149         long delay1 = getDelayMS(group + 1);
 150         long delay = delay0 + (delay1 - delay0) / 2;
 151         return new SystemGCTask(delay);
 152     }
 153 
 154     private static void startTask(Runnable task) {
 155         if (task != null) {
 156             new Thread(task).start();
 157         }
 158     }
 159 
 160     private static void exitTask(Exitable task) {
 161         if (task != null) {
 162             task.exit();
 163         }
 164     }
 165 
 166     private static void runAllPhases() {
 167         for (int i = 0; i < 4 && System.currentTimeMillis() < endTime; i++) {

 168             SystemGCTask gcTask =
 169                 (i % 2 == 1) ? createSystemGCTask(numGroups / 3) : null;
 170             ShortLivedAllocationTask shortTask =
 171                 (i == 1 || i == 3) ?  new ShortLivedAllocationTask() : null;
 172             LongLivedAllocationTask longTask =
 173                 (i == 2 || i == 3) ? new LongLivedAllocationTask(longLivedMap) : null;
 174 
 175             startTask(gcTask);
 176             startTask(shortTask);
 177             startTask(longTask);
 178 
 179             doSystemGCs();
 180 
 181             exitTask(gcTask);
 182             exitTask(shortTask);
 183             exitTask(longTask);
 184 
 185             ThreadUtils.sleep(1000);

 186         }
 187     }
 188 
 189     public static void main(String[] args) throws Exception {
 190         if (args.length == 0) {
 191             throw new IllegalArgumentException("Must specify timeout in seconds as first argument");
 192         }
 193         int timeout = Integer.parseInt(args[0]) * 1000;
 194         System.out.println("Running with timeout of " + timeout + "ms");
 195         endTime = System.currentTimeMillis() + timeout;

 196         // First allocate the long lived objects and then run all phases.
 197         populateLongLived();
 198         runAllPhases();
 199     }
 200 }
< prev index next >