1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
package gc.stress.gclocker;
// Stress the GC locker by calling GetPrimitiveArrayCritical while
// concurrently filling up old gen.
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
final class ThreadUtils {
public static void sleep(long durationMS) {
try {
Thread.sleep(durationMS);
} catch (Exception e) {
}
}
}
class Filler {
private static final int SIZE = 250000;
private int[] i1 = new int[SIZE];
private int[] i2 = new int[SIZE];
private short[] s1 = new short[SIZE];
private short[] s2 = new short[SIZE];
private Map<Object, Object> map = new HashMap<>();
public Filler() {
for (int i = 0; i < 10000; i++) {
map.put(new Object(), new Object());
}
}
}
class Exitable {
private volatile boolean shouldExit = false;
protected boolean shouldExit() {
return shouldExit;
}
public void exit() {
shouldExit = true;
}
}
class MemoryWatcher {
private MemoryPoolMXBean bean;
private final int thresholdPromille = 750;
private final int criticalThresholdPromille = 800;
private final int minGCWaitMS = 1000;
private final int minFreeWaitElapsedMS = 30000;
private final int minFreeCriticalWaitMS;
private int lastUsage = 0;
private long lastGCDetected = System.currentTimeMillis();
private long lastFree = System.currentTimeMillis();
public MemoryWatcher(String mxBeanName, int minFreeCriticalWaitMS) {
this.minFreeCriticalWaitMS = minFreeCriticalWaitMS;
List<MemoryPoolMXBean> memoryBeans = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean bean : memoryBeans) {
if (bean.getName().equals(mxBeanName)) {
this.bean = bean;
break;
}
}
}
private int getMemoryUsage() {
if (bean == null) {
Runtime r = Runtime.getRuntime();
float free = (float) r.freeMemory() / r.maxMemory();
return Math.round((1 - free) * 1000);
} else {
MemoryUsage usage = bean.getUsage();
float used = (float) usage.getUsed() / usage.getCommitted();
return Math.round(used * 1000);
}
}
public synchronized boolean shouldFreeUpSpace() {
int usage = getMemoryUsage();
long now = System.currentTimeMillis();
boolean detectedGC = false;
if (usage < lastUsage) {
lastGCDetected = now;
detectedGC = true;
}
lastUsage = usage;
long elapsed = now - lastFree;
long timeSinceLastGC = now - lastGCDetected;
if (usage > criticalThresholdPromille && elapsed > minFreeCriticalWaitMS) {
lastFree = now;
return true;
} else if (usage > thresholdPromille && !detectedGC) {
if (elapsed > minFreeWaitElapsedMS || timeSinceLastGC > minGCWaitMS) {
lastFree = now;
return true;
}
}
return false;
}
}
class MemoryUser extends Exitable implements Runnable {
private final Queue<Filler> cache = new ArrayDeque<Filler>();
private final MemoryWatcher watcher;
private void load() {
if (watcher.shouldFreeUpSpace()) {
int toRemove = cache.size() / 5;
for (int i = 0; i < toRemove; i++) {
cache.remove();
}
}
cache.add(new Filler());
}
public MemoryUser(String mxBeanName, int minFreeCriticalWaitMS) {
watcher = new MemoryWatcher(mxBeanName, minFreeCriticalWaitMS);
}
@Override
public void run() {
for (int i = 0; i < 200; i++) {
load();
}
while (!shouldExit()) {
load();
}
}
}
class GCLockerStresser extends Exitable implements Runnable {
static native void fillWithRandomValues(byte[] array);
@Override
public void run() {
byte[] array = new byte[1024 * 1024];
while (!shouldExit()) {
fillWithRandomValues(array);
}
}
}
public class TestGCLocker {
private static Exitable startGCLockerStresser(String name) {
GCLockerStresser task = new GCLockerStresser();
Thread thread = new Thread(task);
thread.setName(name);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
return task;
}
private static Exitable startMemoryUser(String mxBeanName, int minFreeCriticalWaitMS) {
MemoryUser task = new MemoryUser(mxBeanName, minFreeCriticalWaitMS);
Thread thread = new Thread(task);
thread.setName("Memory User");
thread.start();
return task;
}
public static void main(String[] args) {
System.loadLibrary("TestGCLocker");
long durationMinutes = args.length > 0 ? Long.parseLong(args[0]) : 5;
String mxBeanName = args.length > 1 ? args[1] : null;
int minFreeCriticalWaitMS = args.length > 2 ? Integer.parseInt(args[2]) : 500;
long startMS = System.currentTimeMillis();
Exitable stresser1 = startGCLockerStresser("GCLockerStresser1");
Exitable stresser2 = startGCLockerStresser("GCLockerStresser2");
Exitable memoryUser = startMemoryUser(mxBeanName, minFreeCriticalWaitMS);
long durationMS = durationMinutes * 60 * 1000;
while ((System.currentTimeMillis() - startMS) < durationMS) {
ThreadUtils.sleep(10 * 1010);
}
stresser1.exit();
stresser2.exit();
memoryUser.exit();
}
}
|