Skip to content

Commit e24fb3a

Browse files
authoredJun 1, 2021
CODETOOLS-7902947: jcstress: Skip affinity initialization when non-local affinity mode is requested
1 parent 345f0ae commit e24fb3a

File tree

5 files changed

+65
-30
lines changed

5 files changed

+65
-30
lines changed
 

‎jcstress-core/src/main/java/org/openjdk/jcstress/ForkedMain.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ public static void main(String[] args) throws Exception {
4747
throw new IllegalStateException("Expected three arguments");
4848
}
4949

50-
// Pre-initialize the affinity support and threads, so that workers
51-
// do not have to do this on critical paths during the execution.
52-
// This also runs when the rest of the infrastructure starts up.
53-
new WarmupAffinityTask().start();
54-
55-
String host = args[0];
56-
int port = Integer.parseInt(args[1]);
57-
int token = Integer.parseInt(args[2]);
50+
boolean initLocalAffinity = Boolean.parseBoolean(args[0]);
51+
52+
if (initLocalAffinity) {
53+
// Pre-initialize the affinity support and threads, so that workers
54+
// do not have to do this on critical paths during the execution.
55+
// This also runs when the rest of the infrastructure starts up.
56+
new WarmupAffinityTask().start();
57+
}
58+
59+
String host = args[1];
60+
int port = Integer.parseInt(args[2]);
61+
int token = Integer.parseInt(args[3]);
5862

5963
BinaryLinkClient link = new BinaryLinkClient(host, port);
6064

‎jcstress-core/src/main/java/org/openjdk/jcstress/TestExecutor.java

+3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ void start() {
342342

343343
command.add(ForkedMain.class.getName());
344344

345+
// notify the forked VM whether we want the local affinity initialized
346+
command.add(Boolean.toString(task.shClass.mode() == AffinityMode.LOCAL));
347+
345348
command.add(host);
346349
command.add(String.valueOf(port));
347350

‎jcstress-core/src/main/java/org/openjdk/jcstress/infra/processors/JCStressTestProcessor.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,18 @@ private void generateContinuous(TestInfo info) {
457457
pw.println();
458458
pw.println(" control.isStopped = false;");
459459
pw.println();
460+
461+
// Initialize affinity before starting the timing measurement, so that init time
462+
// does not eat up into the test run time.
463+
pw.println(" if (config.localAffinity) {");
464+
pw.println(" try {");
465+
pw.println(" AffinitySupport.tryBind();");
466+
pw.println(" } catch (Exception e) {");
467+
pw.println(" // Do not care");
468+
pw.println(" }");
469+
pw.println(" }");
470+
pw.println();
471+
460472
pw.println(" ArrayList<CounterThread<" + r + ">> threads = new ArrayList<>(" + actorsCount + ");");
461473
for (ExecutableElement a : info.getActors()) {
462474
pw.println(" threads.add(new CounterThread<" + r + ">() { public Counter<" + r + "> internalRun() {");
@@ -557,7 +569,7 @@ private void generateContinuous(TestInfo info) {
557569
pw.println();
558570
pw.println(" private Counter<" + r + "> " + TASK_LOOP_PREFIX + a.getSimpleName() + "() {");
559571
pw.println(" Counter<" + r + "> counter = new Counter<>();");
560-
pw.println(" AffinitySupport.bind(config.actorMap[" + n + "]);");
572+
pw.println(" if (config.localAffinity) AffinitySupport.bind(config.localAffinityMap[" + n + "]);");
561573
pw.println(" while (true) {");
562574
pw.println(" WorkerSync sync = workerSync;");
563575
pw.println(" if (sync.stopped) {");

‎jcstress-core/src/main/java/org/openjdk/jcstress/infra/runners/ForkedTestConfig.java

+21-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package org.openjdk.jcstress.infra.runners;
2626

27+
import org.openjdk.jcstress.os.AffinityMode;
28+
2729
import java.io.DataInputStream;
2830
import java.io.DataOutputStream;
2931
import java.io.IOException;
@@ -37,7 +39,8 @@ public class ForkedTestConfig {
3739
public final int maxFootprintMB;
3840
public int minStride;
3941
public int maxStride;
40-
public int[] actorMap;
42+
public boolean localAffinity;
43+
public int[] localAffinityMap;
4144

4245
public ForkedTestConfig(TestConfig cfg) {
4346
spinLoopStyle = cfg.spinLoopStyle;
@@ -47,7 +50,10 @@ public ForkedTestConfig(TestConfig cfg) {
4750
maxFootprintMB = cfg.maxFootprintMB;
4851
minStride = cfg.minStride;
4952
maxStride = cfg.maxStride;
50-
actorMap = cfg.cpuMap.actorMap();
53+
localAffinity = cfg.shClass.mode() == AffinityMode.LOCAL;
54+
if (localAffinity) {
55+
localAffinityMap = cfg.cpuMap.actorMap();
56+
}
5157
}
5258

5359
public ForkedTestConfig(DataInputStream dis) throws IOException {
@@ -58,10 +64,13 @@ public ForkedTestConfig(DataInputStream dis) throws IOException {
5864
maxFootprintMB = dis.readInt();
5965
minStride = dis.readInt();
6066
maxStride = dis.readInt();
61-
int len = dis.readInt();
62-
actorMap = new int[len];
63-
for (int c = 0; c < len; c++) {
64-
actorMap[c] = dis.readInt();
67+
localAffinity = dis.readBoolean();
68+
if (localAffinity) {
69+
int len = dis.readInt();
70+
localAffinityMap = new int[len];
71+
for (int c = 0; c < len; c++) {
72+
localAffinityMap[c] = dis.readInt();
73+
}
6574
}
6675
}
6776

@@ -73,9 +82,12 @@ public void write(DataOutputStream dos) throws IOException {
7382
dos.writeInt(maxFootprintMB);
7483
dos.writeInt(minStride);
7584
dos.writeInt(maxStride);
76-
dos.writeInt(actorMap.length);
77-
for (int am : actorMap) {
78-
dos.writeInt(am);
85+
dos.writeBoolean(localAffinity);
86+
if (localAffinity) {
87+
dos.writeInt(localAffinityMap.length);
88+
for (int am : localAffinityMap) {
89+
dos.writeInt(am);
90+
}
7991
}
8092
}
8193

‎jcstress-core/src/main/java/org/openjdk/jcstress/os/AffinitySupport.java

+16-12
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@
3333
public class AffinitySupport {
3434

3535
public static void bind(int cpu) {
36-
if (cpu == -1) {
37-
// Special case: no need to affine.
38-
return;
39-
}
40-
4136
if (VMSupport.isLinux()) {
4237
Linux.bind(cpu);
4338
} else {
@@ -55,6 +50,7 @@ public static void tryBind() {
5550

5651
static class Linux {
5752
private static volatile CLibrary INSTANCE;
53+
private static boolean BIND_TRIED;
5854

5955
public static void tryInit() {
6056
if (INSTANCE == null) {
@@ -76,15 +72,23 @@ public static void bind(int cpu) {
7672
}
7773

7874
public static void tryBind() {
79-
tryInit();
75+
if (BIND_TRIED) return;
76+
77+
synchronized (Linux.class) {
78+
if (BIND_TRIED) return;
8079

81-
final cpu_set_t new_cpuset = new cpu_set_t();
82-
new_cpuset.set(0);
83-
final cpu_set_t old_cpuset = new cpu_set_t();
80+
tryInit();
8481

85-
get(old_cpuset);
86-
set(new_cpuset);
87-
set(old_cpuset);
82+
final cpu_set_t new_cpuset = new cpu_set_t();
83+
new_cpuset.set(0);
84+
final cpu_set_t old_cpuset = new cpu_set_t();
85+
86+
get(old_cpuset);
87+
set(new_cpuset);
88+
set(old_cpuset);
89+
90+
BIND_TRIED = true;
91+
}
8892
}
8993

9094
private static void get(cpu_set_t cpuset) {

0 commit comments

Comments
 (0)
Please sign in to comment.