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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/*
* Copyright (c) 2010, 2022, 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 vm.gc.concurrent;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import nsk.share.TestFailure;
import nsk.share.gc.GC;
import nsk.share.gc.Memory;
import nsk.share.gc.ThreadedGCTest;
import nsk.share.gc.gp.GarbageProducer;
import nsk.share.gc.gp.GarbageProducer1Aware;
import nsk.share.gc.gp.GarbageProducerAware;
import nsk.share.gc.gp.MemoryStrategy;
import nsk.share.gc.gp.MemoryStrategyAware;
import nsk.share.gc.tree.*;
import nsk.share.log.Log;
import nsk.share.test.ExecutionController;
import nsk.share.test.LocalRandom;
class Forest {
// the actual size of TreeNode in bytes in the memory calculated as occupied memory / count of nodes
static int nodeSize;
static long treeSize;
private static long allNodesCount;
/* log from test */
static Log log;
static int treeHeight;
static long actuallyMut = 0;
private static Forest instance = new Forest();
private Tree[] trees;
private Lock[] locks;
private int nodeGarbageSize;
private GarbageProducer gp;
/*
* Create array of trees occupyng given percent of heap
*/
static Forest createForest(long percent, int heightToSizeRatio, int nodeGarbageSize, GarbageProducer gp, Log _log) {
log = _log;
long size = Runtime.getRuntime().maxMemory() * percent / 100;
treeHeight = Memory.balancedTreeHeightFromMemory(size, (int) new TreeNode(nodeGarbageSize).getTotalSize());
int ntrees = 0;
while (treeHeight * heightToSizeRatio > ntrees) {
ntrees++;
treeHeight = Memory.balancedTreeHeightFromMemory(size / ntrees, (int) new TreeNode(nodeGarbageSize).getTotalSize());
}
log.debug("The expected forest paramteres: tree height = " + treeHeight + " number of trees = " + ntrees
+ " size = " + new TreeNode(nodeGarbageSize).getTotalSize());
Tree[] localTrees = new Tree[ntrees * 4];
Lock[] localLocks = new Lock[ntrees * 4];
for (int i = 0; i < ntrees * 4; i++) {
localTrees[i] = new Tree(Memory.makeBalancedTreeNode(treeHeight, nodeGarbageSize, gp));
localLocks[i] = new ReentrantLock();
int numOfAttempts = 0;
if (Concurrent.getPercentInfoByMBeans() > percent) {
log.debug("Attempt to System.gc() before control check. (" + numOfAttempts++ + ")");
System.gc();
if (Concurrent.getPercentInfoByMBeans() > percent) {
instance.trees = new Tree[i];
instance.locks = new Lock[i];
for (int j = 0; j < i; j++) {
instance.trees[j] = localTrees[j];
instance.locks[j] = localLocks[j];
}
allNodesCount = Memory.balancedTreeNodes(treeHeight) * instance.trees.length;
nodeSize = (int) (ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / allNodesCount);
treeSize = Memory.balancedTreeSize(treeHeight, nodeSize);
instance.where = new AtomicCycleInteger(instance.trees.length);
instance.nodeGarbageSize = nodeGarbageSize;
log.debug("The forest real paramteres: tree height = " + treeHeight + " number of trees = " + instance.trees.length
+ " number of nodes = " + allNodesCount);
log.debug("Approximate node size = " + nodeSize + " calc = " + instance.trees[0].getRoot().getSize());
return instance;
}
}
}
throw new TestFailure("Should not reach here. The correct exit point is inside cycle");
}
int treesCount() {
return trees.length;
}
long nodesCount() {
return allNodesCount;
}
// Confirms that all trees are balanced and have the correct height.
void checkTrees() {
for (int i = 0; i < trees.length; i++) {
locks[i].lock();
checkTree(trees[i]);
locks[i].unlock();
}
}
private static void checkTree(Tree tree) {
TreeNode root = tree.getRoot();
int h1 = root.getHeight();
int h2 = root.getShortestPath();
if ((h1 != treeHeight) || (h2 != treeHeight)) {
throw new TestFailure("The tree is not balanced expected " + treeHeight
+ " value = " + h1 + " shortedtPath = " + h2);
}
}
// Swap subtrees in 2 trees, the path is used
// as sequence of 1-0 to select subtree (left-reight sequence)
static void swapSubtrees(Tree t1, Tree t2, int depth, int path) {
TreeNode tn1 = t1.getRoot();
TreeNode tn2 = t2.getRoot();
for (int i = 0; i < depth; i++) {
if ((path & 1) == 0) {
tn1 = tn1.getLeft();
tn2 = tn2.getLeft();
} else {
tn1 = tn1.getRight();
tn2 = tn2.getRight();
}
path >>= 1;
}
TreeNode tmp;
if ((path & 1) == 0) {
tmp = tn1.getLeft();
tn1.setLeft(tn2.getLeft());
tn2.setLeft(tmp);
} else {
tmp = tn1.getRight();
tn1.setRight(tn2.getRight());
tn2.setLeft(tmp);
}
}
// Interchanges two randomly selected subtrees (of same size and depth) several times
void swapSubtrees(long count) {
for (int i = 0; i < count; i++) {
int index1 = LocalRandom.nextInt(trees.length);
int index2 = LocalRandom.nextInt(trees.length);
int depth = LocalRandom.nextInt(treeHeight);
int path = LocalRandom.nextInt();
locks[index1].lock();
// Skip the round to avoid deadlocks
if (locks[index2].tryLock()) {
swapSubtrees(trees[index1], trees[index2], depth, path);
actuallyMut += 2;
locks[index2].unlock();
}
locks[index1].unlock();
}
}
static class AtomicCycleInteger extends AtomicInteger {
private int max;
public AtomicCycleInteger(int cycleLength) {
super();
this.max = cycleLength - 1;
}
public int cycleIncrementAndGet() {
for (;;) {
int current = get();
int next = (current == max ? 0 : current + 1);
if (compareAndSet(current, next)) {
return next;
}
}
}
}
// the index in tree array which should be chnaged during next regeneration
AtomicCycleInteger where = null;
// generate new full and partial trees in our forest
void regenerateTrees(long nodesCount) {
int full = (int) (nodesCount / Memory.balancedTreeNodes(treeHeight)) ;
int partial = (int) nodesCount % (Memory.balancedTreeNodes(treeHeight));
for (int i = 0; i < full; i++) {
int idx = where.cycleIncrementAndGet();
locks[idx].lock();
trees[idx] = new Tree(Memory.makeBalancedTreeNode(treeHeight, nodeGarbageSize));
locks[idx].unlock();
}
while (partial > 0) {
int h = Memory.balancedTreeHeightFromNodes(partial);
Tree newTree = new Tree(Memory.makeBalancedTreeNode(h, nodeGarbageSize));
int idx = where.cycleIncrementAndGet();
locks[idx].lock();
replaceTree(trees[idx], newTree);
locks[idx].unlock();
partial = partial - Memory.balancedTreeNodes(h);
}
}
// Given a balanced tree full and a smaller balanced tree partial,
// replaces an appropriate subtree of full by partial, taking care
// to preserve the shape of the full tree.
private static void replaceTree(Tree full, Tree partial) {
boolean dir = (partial.getHeight() % 2) == 0;
actuallyMut++;
replaceTreeWork(full.getRoot(), partial.getRoot(), dir);
}
// Called only by replaceTree (below) and by itself.
static void replaceTreeWork(TreeNode full, TreeNode partial,
boolean dir) {
boolean canGoLeft = full.getLeft() != null && full.getLeft().getHeight() > partial.getHeight();
boolean canGoRight = full.getRight() != null && full.getRight().getHeight() > partial.getHeight();
if (canGoLeft && canGoRight) {
if (dir) {
replaceTreeWork(full.getLeft(), partial, !dir);
} else {
replaceTreeWork(full.getRight(), partial, !dir);
}
} else if (!canGoLeft && !canGoRight) {
if (dir) {
full.setLeft(partial);
} else {
full.setRight(partial);
}
} else if (!canGoLeft) {
full.setLeft(partial);
} else {
full.setRight(partial);
}
}
}
public class Concurrent extends ThreadedGCTest implements GarbageProducerAware, GarbageProducer1Aware, MemoryStrategyAware {
// Heap as tree
Forest forest;
// GP for old gargbage production
GarbageProducer gpOld;
// GP for young gargbage production
GarbageProducer gpYoung;
MemoryStrategy ms;
private void printStatistics() {
log.debug("Actual mutations = " + forest.actuallyMut);
}
private class Worker implements Runnable {
private ExecutionController stresser;
@Override
public void run() {
if (stresser == null) {
stresser = getExecutionController();
}
while (stresser.continueExecution()) {
doStep();
}
}
}
@Override
public Runnable createRunnable(int i) {
return new Worker();
}
public static int getPercentInfoByMBeans() {
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
return (int) (100 * mbean.getHeapMemoryUsage().getUsed() / mbean.getHeapMemoryUsage().getMax());
}
private void printMem(long used, long max, String source) {
log.debug("The Memory after allocation (" + source + "): ");
log.debug("Used = " + used + " Max = " + max + " Percent = " + (100 * used / max));
}
// Command-line parameters.
// young garbage in percent and absolute
private static int youngPercent = 0;
long youngGarbageSize;
// mutation rate (parcent and absolute trees)
private static int ptrMutRate = 50;
long mutateTrees;
// percent of heap to occupy by forest (long live garbage)
private static int livePercent = 60;
// the minimum of which should be available for forest
// test fails if it is not possible to use 60% of heap
private static final int MIN_AVAILABLE_MEM = 60;
// percent of forest to reallocate each step
private static int reallocatePercent = 30;
long reallocateSizeInNodes;
// sleep time in ms
private static int sleepTime = 100;
private void init(int longLivePercent) {
int numberOfThreads = runParams.getNumberOfThreads();
forest = Forest.createForest(longLivePercent, numberOfThreads,
(int) Math.sqrt(ms.getSize(Runtime.getRuntime().maxMemory())), gpOld, log);
youngGarbageSize = Runtime.getRuntime().maxMemory() * youngPercent / 100 / numberOfThreads;
reallocateSizeInNodes = forest.nodesCount() * reallocatePercent / 100 / numberOfThreads;
mutateTrees = forest.treesCount() * ptrMutRate / 100 / numberOfThreads / 2;
log.debug("Young Gen = " + youngGarbageSize);
log.debug("Forest contains " + forest.treesCount() + " trees and " + forest.nodesCount() + " nodes.");
log.debug("Count of nodes to reallocate = " + reallocateSizeInNodes);
log.debug("Count of tree pairs to exchange nodes = " + mutateTrees);
log.debug("Sleep time = " + sleepTime);
// print some info
MemoryUsage mbean = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
printMem(mbean.getUsed(), mbean.getMax(), "Beans");
printMem(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory(),
Runtime.getRuntime().maxMemory(), "System");
}
@Override
public void run() {
try {
init(livePercent);
} catch (OutOfMemoryError oome) {
if (livePercent > MIN_AVAILABLE_MEM) {
log.debug("Unable to use " + livePercent + " use only " + MIN_AVAILABLE_MEM);
init(MIN_AVAILABLE_MEM);
}
}
super.run();
printStatistics();
}
private void doStep() {
// allocate some young garbage
if (youngGarbageSize != 0) {
gpYoung.create(youngGarbageSize);
}
// allocate some long-live garbage (attached to our trees)
forest.regenerateTrees(reallocateSizeInNodes);
// mutate pointers
forest.swapSubtrees(mutateTrees);
// sleep to give GC time for some concurrent actions
try {
Thread.sleep(sleepTime);
} catch (InterruptedException ie) {
}
// verify trees, also read all pointers
forest.checkTrees();
}
public static void main(String[] args) {
init(args);
GC.runTest(new Concurrent(), args);
}
public static void init(String[] args) {
for (int i = 0; i < args.length; ++i) {
if (args[i].equals("-lp")) {
// percent of long lived objects
livePercent = Integer.parseInt(args[++i]);
} else if (args[i].equals("-rp")) {
// percent of trees to reallocate
reallocatePercent = Integer.parseInt(args[++i]);
} else if (args[i].equals("-yp")) {
// percent of young objects
youngPercent = Integer.parseInt(args[++i]);
} else if (args[i].equals("-mr")) {
// percent of trees to exchange (mutate)
ptrMutRate = Integer.parseInt(args[++i]);
} else if (args[i].equals("-st")) {
// sleep time in ms
sleepTime = Integer.parseInt(args[++i]);
}
}
}
@Override
public void setGarbageProducer(GarbageProducer gp) {
this.gpOld = gp;
}
@Override
public void setGarbageProducer1(GarbageProducer gpYoung) {
this.gpYoung = gpYoung;
}
@Override
public void setMemoryStrategy(MemoryStrategy ms) {
this.ms = ms;
}
}
|