8293872: Make runtime/Thread/ThreadCountLimit.java more robust

Reviewed-by: dholmes, adinn
This commit is contained in:
Aleksey Shipilev 2022-09-26 12:44:04 +00:00
parent 2be315877b
commit 36b61c5d7e

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,7 +25,7 @@
* @test * @test
* @summary Stress test that reaches the process limit for thread count, or time limit. * @summary Stress test that reaches the process limit for thread count, or time limit.
* @key stress * @key stress
* @run main ThreadCountLimit * @run main/othervm -Xmx1g ThreadCountLimit
*/ */
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
@ -36,36 +36,18 @@ public class ThreadCountLimit {
static final int TIME_LIMIT_MS = 5000; // Create as many threads as possible in 5 sec static final int TIME_LIMIT_MS = 5000; // Create as many threads as possible in 5 sec
static class Worker extends Thread { static class Worker extends Thread {
private final int index;
private final CountDownLatch startSignal; private final CountDownLatch startSignal;
Worker(int index, CountDownLatch startSignal) { Worker(CountDownLatch startSignal) {
this.index = index;
this.startSignal = startSignal; this.startSignal = startSignal;
} }
@Override @Override
public void run() { public void run() {
if ((index % 250) == 0) {
System.out.println("INFO: thread " + index + " waiting to start");
}
try { try {
startSignal.await(); startSignal.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new Error("Unexpected: " + e); throw new Error("Unexpected", e);
}
setName(String.valueOf(index));
Thread.yield();
if (index != Integer.parseInt(getName())) {
throw new Error("setName/getName failed!");
}
if ((index % 250) == 0) {
System.out.println("INFO: thread " + getName() + " working");
} }
} }
} }
@ -74,27 +56,37 @@ public class ThreadCountLimit {
CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch startSignal = new CountDownLatch(1);
ArrayList<Worker> workers = new ArrayList<Worker>(); ArrayList<Worker> workers = new ArrayList<Worker>();
boolean reachedTimeLimit = false;
boolean reachedNativeOOM = false;
int countAtTimeLimit = -1;
int countAtNativeOOM = -1;
// This is dangerous loop: it depletes system resources,
// so doing additional things there that may end up allocating
// Java/native memory risks failing the VM prematurely.
// Avoid doing unnecessary calls, printouts, etc.
int count = 1; int count = 1;
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
try { try {
while (true) { while (true) {
Worker w = new Worker(count, startSignal); Worker w = new Worker(startSignal);
w.start(); w.start();
workers.add(w); workers.add(w);
count++; count++;
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
if ((end - start) > TIME_LIMIT_MS) { if ((end - start) > TIME_LIMIT_MS) {
// Windows path or a system with very large ulimit reachedTimeLimit = true;
System.out.println("INFO: reached the time limit " + TIME_LIMIT_MS + " ms, with " + count + " threads created"); countAtTimeLimit = count;
break; break;
} }
} }
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
if (e.getMessage().contains("unable to create native thread")) { if (e.getMessage().contains("unable to create native thread")) {
// Linux, macOS path // Linux, macOS path
long end = System.currentTimeMillis(); reachedNativeOOM = true;
System.out.println("INFO: reached this process thread count limit at " + count + " [" + (end - start) + " ms]"); countAtNativeOOM = count;
} else { } else {
throw e; throw e;
} }
@ -107,7 +99,18 @@ public class ThreadCountLimit {
w.join(); w.join();
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new Error("Unexpected: " + e); throw new Error("Unexpected", e);
}
// Now that all threads have joined, we are away from dangerous
// VM state and have enough memory to perform any other things.
if (reachedTimeLimit) {
// Windows path or a system with very large ulimit
System.out.println("INFO: reached the time limit " + TIME_LIMIT_MS +
" ms, with " + countAtTimeLimit + " threads created");
} else if (reachedNativeOOM) {
System.out.println("INFO: reached this process thread count limit with " +
countAtNativeOOM + " threads created");
} }
} }
} }