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
|
/*
* Copyright (c) 2017, 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.
*/
/*
* @test id=z
* @key randomness
* @bug 8059022
* @modules java.base/jdk.internal.misc:+open
* @summary Validate barriers after Unsafe getObject, CAS and swap (GetAndSet)
* @requires vm.gc.Z & !vm.graal.enabled
* @library /test/lib
* @run main/othervm -Xmx256m
* -XX:+UnlockExperimentalVMOptions
* -XX:+UseZGC
* -XX:+UnlockDiagnosticVMOptions
* -XX:+ZUnmapBadViews -XX:ZCollectionInterval=1
* -XX:-CreateCoredumpOnCrash
* -XX:CompileCommand=dontinline,*::mergeImpl* compiler.gcbarriers.UnsafeIntrinsicsTest
*/
/*
* @test id=shenandoah
* @key randomness
* @bug 8059022
* @modules java.base/jdk.internal.misc:+open
* @summary Validate barriers after Unsafe getObject, CAS and swap (GetAndSet)
* @requires vm.gc.Shenandoah & !vm.graal.enabled
* @library /test/lib
* @run main/othervm -Xmx256m
* -XX:+UseShenandoahGC
* -XX:+UnlockDiagnosticVMOptions
* -XX:-CreateCoredumpOnCrash
* -XX:+ShenandoahVerify
* -XX:+IgnoreUnrecognizedVMOptions -XX:+ShenandoahVerifyOptoBarriers
* -XX:CompileCommand=dontinline,*::mergeImpl* compiler.gcbarriers.UnsafeIntrinsicsTest
*/
package compiler.gcbarriers;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Random;
import jdk.test.lib.Utils;
import sun.misc.Unsafe;
public class UnsafeIntrinsicsTest {
/*
* This test triggers the loadbarriers by allocating a lot, keeping the objects alive and then
* letting them die in a way that maximizes fragmentation.
*
* All subtests (OperationType's) could run in parallel.
*/
static int node_count = 133700;
static int thread_count = 4;
static int time = Integer.getInteger("time", 4); // seconds per subtest
static Runner r = new Runner(null, 1, 1, Runner.OperationType.CAS);
static Node first_node;
int epoch = 0;
public static void main(String[] args) {
UnsafeIntrinsicsTest t = new UnsafeIntrinsicsTest();
t.testWithLocalData(Runner.OperationType.CAS);
t.testWithLocalData(Runner.OperationType.Weak_CAS);
t.testWithLocalData(Runner.OperationType.CMPX);
t.testWithSharedData(Runner.OperationType.Swap);
t.testWithSharedData(Runner.OperationType.Load);
}
public UnsafeIntrinsicsTest() {
}
public void testWithLocalData(Runner.OperationType optype) {
System.out.println("Testing " + optype.name() + " with " + thread_count +" thread and " + node_count + " nodes");
// start mutator threads
ArrayList<Thread> thread_list = new ArrayList<Thread>();
Random r = Utils.getRandomInstance();
for (int i = 0; i < thread_count; i++) {
setup(); // each thread has its own circle of nodes
Thread t = new Thread(new Runner(first_node, time, r.nextLong(), optype));
t.start();
thread_list.add(t);
}
waitForCompletion(thread_list);
countNodes();
}
public void testWithSharedData(Runner.OperationType optype) {
System.out.println("Testing " + optype.name() + " with " + thread_count +" thread and " + node_count + " nodes");
setup(); // All nodes are shared between threads
ArrayList<Thread> thread_list = new ArrayList<Thread>();
Random r = Utils.getRandomInstance();
for (int i = 0; i < thread_count; i++) {
Thread t = new Thread(new Runner(first_node, time, r.nextLong(), optype));
t.start();
thread_list.add(t);
}
waitForCompletion(thread_list);
countNodes();
}
public void waitForCompletion(ArrayList<Thread> thread_list) {
// do some waiting
try {
Thread.sleep(time*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// wait for all thread to terminate
for (int i = 0; i < thread_count; i++) {
try {
thread_list.get(i).join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void countNodes() {
epoch++;
int count = 0;
Node node = first_node;
while (node.number() < epoch) {
node.setNumber(epoch);
count++;
node = node.next();
}
System.out.println("Program end, found " + count + " nodes");
}
// Create a circular linked list
public void setup() {
first_node = new Node();
Node last_node = first_node;
for (int i = 0; i < node_count; i++) {
last_node = new Node(last_node);
}
first_node.setNext(last_node);
}
}
class Runner implements Runnable {
OperationType type;
Node current;
Random r;
long time;
long seed;
long milage = 0;
long created = 0;
long skipped = 0;
int iterations = 0;
static final jdk.internal.misc.Unsafe UNSAFE;
static final long offset;
public enum OperationType {
Load("Load"),
Swap("Swap"),
CAS("CAS"),
Weak_CAS("Weak-CAS"),
CMPX("CMPX");
private String name;
private OperationType(String name) { this.name = name; }
}
static {
try {
Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
UNSAFE = (jdk.internal.misc.Unsafe) f.get(null);
offset = UNSAFE.objectFieldOffset(Node.class.getDeclaredField("next"));
} catch (Exception e) {
throw new RuntimeException("Unable to get Unsafe instance.", e);
}
}
public Runner(Node start, int testtime, long seed, OperationType type) {
current = start;
time = testtime*1000000000L;
r = new Random(seed);
this.type = type;
}
@Override
public void run() {
long starttime = System.nanoTime();
while((System.nanoTime() - starttime) < time) {
iterations++;
// Run a bit
int run_length = r.nextInt() & 0xfff;
for (int i = 0; i < run_length; i++) {
current = current.next();
milage++;
}
// find a start node
Node startNode = current;
Node expectedNext = startNode.next;
// Run a bit more
int skip_length = (r.nextInt() & 0xff) + 1;
for (int i = 0; i < skip_length; i++) {
current = current.next();
skipped++;
}
// create a branch
int branch_length = (r.nextInt() & 0xff) + 1;
created += branch_length;
Node head = makeBranch(current, branch_length);
// complete circle, but continue to run on old path
boolean test_fail = ((iterations & 0x1) == 0);
Node current = merge(startNode, expectedNext, head, test_fail);
}
System.out.println("Milage: " + milage + " Skipped: " + skipped + " Created: " + created + " iterations: " + iterations);
}
/*
* The reason for the duplicated code that is wrapping the unsafe operations is that we want
* to test the operations individually. They must not interfere with each other - checking a field
* will heal that reference and no operation after can trigger the barrier.
*
* All mergeImpl*-method are prevented from being inlined.
*/
private Node merge(Node startNode, Node expectedNext, Node head, boolean test_fail) {
switch (type) {
case Load:
return mergeImplLoad(startNode, expectedNext, head);
case Swap:
return mergeImplSwap(startNode, expectedNext, head);
case CAS:
if (test_fail) {
return mergeImplCASFail(startNode, expectedNext, head);
} else {
return mergeImplCAS(startNode, expectedNext, head);
}
case Weak_CAS:
if (test_fail) {
return mergeImplWeakCASFail(startNode, expectedNext, head);
} else {
return mergeImplWeakCAS(startNode, expectedNext, head);
}
case CMPX:
if (test_fail) {
return mergeImplCMPXFail(startNode, expectedNext, head);
} else {
return mergeImplCMPX(startNode, expectedNext, head);
}
default:
throw new Error("Unimplemented");
}
}
private Node mergeImplLoad(Node startNode, Node expectedNext, Node head) {
// Atomic load version
Node temp = (Node) UNSAFE.getObject(startNode, offset);
UNSAFE.storeFence(); // Make all new Node fields visible to concurrent readers.
startNode.setNext(head);
return temp;
}
private Node mergeImplSwap(Node startNode, Node expectedNext, Node head) {
// Swap version
return (Node) UNSAFE.getAndSetObject(startNode, offset, head);
}
private Node mergeImplCAS(Node startNode, Node expectedNext, Node head) {
// CAS - should always be true within a single thread - no other thread can have overwritten
if (!UNSAFE.compareAndSetObject(startNode, offset, expectedNext, head)) {
throw new Error("CAS should always succeed on thread local objects, check you barrier implementation");
}
return expectedNext; // continue on old circle
}
private Node mergeImplCASFail(Node startNode, Node expectedNext, Node head) {
// Force a fail
if (UNSAFE.compareAndSetObject(startNode, offset, "fail", head)) {
throw new Error("This CAS should always fail, check you barrier implementation");
}
if (startNode.next() != expectedNext) {
throw new Error("Shouldn't have changed");
}
return current;
}
private Node mergeImplWeakCAS(Node startNode, Node expectedNext, Node head) {
// Weak CAS - should always be true within a single thread - no other thread can have overwritten
if (!UNSAFE.weakCompareAndSetObject(startNode, offset, expectedNext, head)) {
throw new Error("Weak CAS should always succeed on thread local objects, check you barrier implementation");
}
return expectedNext; // continue on old circle
}
private Node mergeImplWeakCASFail(Node startNode, Node expectedNext, Node head) {
// Force a fail
if (UNSAFE.weakCompareAndSetObject(startNode, offset, "fail", head)) {
throw new Error("This weak CAS should always fail, check you barrier implementation");
}
if (startNode.next() != expectedNext) {
throw new Error("Shouldn't have changed");
}
return current;
}
private Node mergeImplCMPX(Node startNode, Node expectedNext, Node head) {
// CmpX - should always be true within a single thread - no other thread can have overwritten
Object res = UNSAFE.compareAndExchangeObject(startNode, offset, expectedNext, head);
if (!res.equals(expectedNext)) {
throw new Error("Fail CmpX should always succeed on thread local objects, check you barrier implementation");
}
return expectedNext; // continue on old circle
}
private Node mergeImplCMPXFail(Node startNode, Node expectedNext, Node head) {
Object res = UNSAFE.compareAndExchangeObject(startNode, offset, head, head);
if (startNode.next() != expectedNext) {
throw new Error("Shouldn't have changed");
}
if (head == expectedNext) {
throw new Error("Test malfunction");
}
if (!res.equals(expectedNext)) {
throw new Error("This CmpX should have returned 'expectedNext' when it failed");
}
if (res.equals(head)) {
throw new Error("This CmpX shouldn't have returned head when it failed. count: "+ iterations);
}
return current;
}
// Create a new branch that will replace a part of the circle
public Node makeBranch(Node end_node, int count) {
Node head = end_node;
for (int i = 0; i < count; i++) {
head = new Node(head);
}
return head;
}
}
class Node {
Node next;
int number = 0;
public int number() {
return number;
}
public void setNumber(int v) {
number = v;
}
public Node() {
}
public Node(Node link) {
next = link;
}
public void setNext(Node next) {
this.next = next;
}
public Node next() {
return next;
}
}
|