Skip to content

Commit

Permalink
7903492: JMH: Infrastructure code should yield occasionally for virtu…
Browse files Browse the repository at this point in the history
…al executor to make progress
  • Loading branch information
shipilev authored Jun 9, 2023
1 parent 9a97557 commit 482561a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public class ControlStopTest {
@Group("pingpong")
public void ping(Control cnt) {
while (!cnt.stopMeasurement) {
// this body is intentionally left blank
Thread.yield();
}
}

@Benchmark
@Group("pingpong")
public void pong(Control cnt) {
while (!cnt.stopMeasurement) {
// this body is intentionally left blank
Thread.yield();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ private void generateThroughput(PrintWriter writer, Mode benchmarkKind, MethodGr
writer.println(ident(4) + emitCall(method, states) + ';');
invocationEpilog(writer, 4, method, states, false);

writer.println(ident(4) + "if (control.shouldYield) Thread.yield();");
writer.println(ident(4) + "res.allOps++;");
writer.println(ident(3) + "}");
writer.println();
Expand All @@ -593,6 +594,7 @@ private void generateThroughput(PrintWriter writer, Mode benchmarkKind, MethodGr
writer.println(ident(5) + emitCall(method, states) + ';');
invocationEpilog(writer, 5, method, states, false);

writer.println(ident(5) + "if (control.shouldYield) Thread.yield();");
writer.println(ident(5) + "res.allOps++;");
writer.println(ident(4) + "}");
writer.println(ident(3) + "} catch (Throwable e) {");
Expand Down Expand Up @@ -702,6 +704,7 @@ private void generateAverageTime(PrintWriter writer, Mode benchmarkKind, MethodG
writer.println(ident(4) + emitCall(method, states) + ';');
invocationEpilog(writer, 4, method, states, false);

writer.println(ident(4) + "if (control.shouldYield) Thread.yield();");
writer.println(ident(4) + "res.allOps++;");
writer.println(ident(3) + "}");
writer.println();
Expand All @@ -726,6 +729,7 @@ private void generateAverageTime(PrintWriter writer, Mode benchmarkKind, MethodG
writer.println(ident(5) + emitCall(method, states) + ';');
invocationEpilog(writer, 5, method, states, false);

writer.println(ident(5) + "if (control.shouldYield) Thread.yield();");
writer.println(ident(5) + "res.allOps++;");
writer.println(ident(4) + "}");
writer.println(ident(3) + "} catch (Throwable e) {");
Expand Down Expand Up @@ -860,6 +864,7 @@ private void generateSampleTime(PrintWriter writer, Mode benchmarkKind, MethodGr
writer.println(ident(4) + emitCall(method, states) + ';');
invocationEpilog(writer, 4, method, states, false);

writer.println(ident(4) + "if (control.shouldYield) Thread.yield();");
writer.println(ident(4) + "res.allOps++;");
writer.println(ident(3) + "}");
writer.println();
Expand Down Expand Up @@ -889,6 +894,7 @@ private void generateSampleTime(PrintWriter writer, Mode benchmarkKind, MethodGr
writer.println(ident(5) + emitCall(method, states) + ';');
invocationEpilog(writer, 5, method, states, false);

writer.println(ident(5) + "if (control.shouldYield) Thread.yield();");
writer.println(ident(5) + "res.allOps++;");
writer.println(ident(4) + "}");
writer.println(ident(3) + "} catch (Throwable e) {");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ boolean stableThreads() {
ExecutorService createExecutor(int maxThreads, String prefix) {
return Executors.newFixedThreadPool(maxThreads, WorkerThreadFactories.virtualWorkerFactory(prefix));
}

@Override
boolean shouldYield() {
return true;
}
},

/**
Expand Down Expand Up @@ -236,6 +241,11 @@ ExecutorService createExecutor(int maxThreads, String prefix) throws Exception {
* @return Executor always reuses the same threads?
*/
boolean stableThreads() { return false; }

/**
* @return Executing threads should yield occasionally to guarantee progress?
*/
boolean shouldYield() { return false; }
}

protected void startProfilers(BenchmarkParams benchmarkParams, IterationParams iterationParams) {
Expand Down Expand Up @@ -309,6 +319,7 @@ public IterationResult runIteration(BenchmarkParams benchmarkParams, IterationPa
InfraControl control = new InfraControl(benchmarkParams, params,
preSetupBarrier, preTearDownBarrier,
isFirstIteration, isLastIteration,
EXECUTOR_TYPE.shouldYield(),
new Control());

// preparing the worker runnables
Expand Down
14 changes: 11 additions & 3 deletions jmh-core/src/main/java/org/openjdk/jmh/runner/InfraControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class InfraControl extends InfraControlL4 {
Utils.check(InfraControl.class, "preSetup", "preTearDown");
Utils.check(InfraControl.class, "firstIteration");
Utils.check(InfraControl.class, "lastIteration");
Utils.check(InfraControl.class, "shouldYield");
Utils.check(InfraControl.class, "warmupVisited", "warmdownVisited");
Utils.check(InfraControl.class, "warmupShouldWait", "warmdownShouldWait");
Utils.check(InfraControl.class, "warmupDone", "warmdownDone");
Expand All @@ -59,8 +60,9 @@ public class InfraControl extends InfraControlL4 {
public InfraControl(BenchmarkParams benchmarkParams, IterationParams iterationParams,
CountDownLatch preSetup, CountDownLatch preTearDown,
boolean firstIteration, boolean lastIteration,
boolean shouldYield,
Control notifyControl) {
super(benchmarkParams, iterationParams, preSetup, preTearDown, firstIteration, lastIteration, notifyControl);
super(benchmarkParams, iterationParams, preSetup, preTearDown, firstIteration, lastIteration, shouldYield, notifyControl);
}

/**
Expand Down Expand Up @@ -165,6 +167,7 @@ abstract class InfraControlL2 extends InfraControlL1 {
public final CountDownLatch preTearDown;
public final boolean firstIteration;
public final boolean lastIteration;
public final boolean shouldYield;

public final AtomicInteger warmupVisited, warmdownVisited;
public volatile boolean warmupShouldWait, warmdownShouldWait;
Expand All @@ -180,6 +183,7 @@ abstract class InfraControlL2 extends InfraControlL1 {
public InfraControlL2(BenchmarkParams benchmarkParams, IterationParams iterationParams,
CountDownLatch preSetup, CountDownLatch preTearDown,
boolean firstIteration, boolean lastIteration,
boolean shouldYield,
Control notifyControl) {
warmupVisited = new AtomicInteger();
warmdownVisited = new AtomicInteger();
Expand All @@ -199,6 +203,8 @@ public InfraControlL2(BenchmarkParams benchmarkParams, IterationParams iteration
this.preTearDown = preTearDown;
this.firstIteration = firstIteration;
this.lastIteration = lastIteration;
this.shouldYield = shouldYield;

this.benchmarkParams = benchmarkParams;
this.iterationParams = iterationParams;
}
Expand Down Expand Up @@ -281,8 +287,9 @@ abstract class InfraControlL3 extends InfraControlL2 {
public InfraControlL3(BenchmarkParams benchmarkParams, IterationParams iterationParams,
CountDownLatch preSetup, CountDownLatch preTearDown,
boolean firstIteration, boolean lastIteration,
boolean shouldYield,
Control notifyControl) {
super(benchmarkParams, iterationParams, preSetup, preTearDown, firstIteration, lastIteration, notifyControl);
super(benchmarkParams, iterationParams, preSetup, preTearDown, firstIteration, lastIteration, shouldYield, notifyControl);
}
}

Expand All @@ -292,8 +299,9 @@ abstract class InfraControlL4 extends InfraControlL3 {
public InfraControlL4(BenchmarkParams benchmarkParams, IterationParams iterationParams,
CountDownLatch preSetup, CountDownLatch preTearDown,
boolean firstIteration, boolean lastIteration,
boolean shouldYield,
Control notifyControl) {
super(benchmarkParams, iterationParams, preSetup, preTearDown, firstIteration, lastIteration, notifyControl);
super(benchmarkParams, iterationParams, preSetup, preTearDown, firstIteration, lastIteration, shouldYield, notifyControl);
}
}

0 comments on commit 482561a

Please sign in to comment.