Skip to content

Commit 2d611dc

Browse files
committed
perf(sched): shorten the turns of a thread that spins while the threads it waits on run short turns (#100)
1 parent d85b528 commit 2d611dc

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎src/jvm.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ const WARM_MIN_CLASSES = 200;
136136

137137
const WOKEN_RUN = 16;
138138

139-
// instructions a thread runs per turn
139+
// instructions a thread runs per turn, at most and at least
140140
const SLICE = 20000;
141+
const MIN_SLICE = 200;
142+
// a turn this long (ms) is a spin or a long computation
143+
const LONG_TURN = 0.05;
141144

142145
export class JVM {
143146
readonly opts: JvmOptions;
@@ -164,6 +167,7 @@ export class JVM {
164167
// threads another thread woke in its turn: they run before the rotation, so a hand-off does not wait behind spinners
165168
private woken: JThread[] = [];
166169
private wokenStreak = 0;
170+
private shortTurns = 0;
167171
private queued = new Set<JThread>();
168172
private scheduled = false;
169173
private hashSeed = 0x2545f491;
@@ -1109,12 +1113,32 @@ export class JVM {
11091113
this.queued.delete(t);
11101114
if (t.state !== RUNNABLE) continue;
11111115
sched.thread = t;
1112-
spent += SLICE - runThread(t, SLICE);
1116+
const q = t.quantum || SLICE;
1117+
// only a thread that stayed runnable is timed, so hand-offs read no clock
1118+
const t0 = t.timed ? performance.now() : 0;
1119+
spent += q - runThread(t, q);
11131120
if (t.unmounting !== null) unmountBlocked(t);
1114-
if (t.state === RUNNABLE && !this.queued.has(t)) {
1115-
this.queued.add(t);
1116-
this.runQueue.push(t);
1121+
if (t.state !== RUNNABLE) {
1122+
this.shortTurns++;
1123+
t.quantum = SLICE;
1124+
t.timed = false;
1125+
} else {
1126+
// a thread that runs long turns while others run short ones gets shorter turns
1127+
const now = performance.now();
1128+
const dt = now - t0;
1129+
const others = this.shortTurns !== t.shortSeen && (this.runQueue.length > 0 || this.woken.length > 0);
1130+
const long = t.timed && dt > LONG_TURN;
1131+
if (t.timed && q === SLICE && !long) this.shortTurns++;
1132+
t.quantum = !others ? SLICE : long ? Math.max(MIN_SLICE, q >> 1) : q;
1133+
t.timed = true;
1134+
if (!this.queued.has(t)) {
1135+
this.queued.add(t);
1136+
this.runQueue.push(t);
1137+
}
1138+
if (now > end) break;
1139+
spent = 0;
11171140
}
1141+
t.shortSeen = this.shortTurns;
11181142
if (sched.pause) break;
11191143
if (spent >= SLICE) {
11201144
if (performance.now() > end) break;

‎src/thread.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ export class JThread {
172172
// what is left of the run loop's budget while compiled code runs: its
173173
// loops and the calls it interprets in place spend it
174174
slice = 0;
175+
// instructions of its next turn, and the VM's count of short turns when it last ran
176+
quantum = 0;
177+
shortSeen = 0;
178+
// its last turn ended with it still runnable
179+
timed = false;
175180
// wakes the thread early from sleep, park or wait, for interrupt/unpark
176181
wake: (() => void) | null = null;
177182
// set when the bottom frame finishes

‎test/java/SpinningNotifier.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.concurrent.Executors;
2+
import java.util.concurrent.atomic.AtomicInteger;
3+
4+
// The main thread spins on notify() while virtual threads wait on the same
5+
// locks; the spinning must not hold up the threads it wakes.
6+
public class SpinningNotifier {
7+
public static void main(String[] args) {
8+
int threads = 3000;
9+
Object[] locks = {new Object(), new Object(), new Object(), new Object()};
10+
var finished = new AtomicInteger();
11+
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
12+
for (int i = 0; i < threads; i++) {
13+
Object lock = locks[i % locks.length];
14+
executor.submit(() -> {
15+
synchronized (lock) {
16+
lock.wait(Long.MAX_VALUE);
17+
}
18+
finished.incrementAndGet();
19+
return null;
20+
});
21+
synchronized (lock) {
22+
lock.notify();
23+
}
24+
}
25+
while (finished.get() < threads) {
26+
for (Object lock : locks) {
27+
synchronized (lock) {
28+
lock.notify();
29+
}
30+
}
31+
}
32+
}
33+
System.out.println("finished " + finished.get());
34+
}
35+
}

0 commit comments

Comments
 (0)