Skip to content

Commit e8ff714

Browse files
committed
Reproduction of #81
1 parent 0bdef18 commit e8ff714

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

affinity/src/test/java/net/openhft/affinity/MultiProcessAffinityTest.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
import java.io.IOException;
1111
import java.time.LocalDateTime;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.concurrent.ThreadLocalRandom;
1215
import java.util.concurrent.TimeUnit;
1316

17+
import static java.lang.String.format;
1418
import static net.openhft.affinity.LockCheck.IS_LINUX;
15-
import static org.junit.Assert.assertNotEquals;
16-
import static org.junit.Assert.fail;
19+
import static org.junit.Assert.*;
1720

1821
public class MultiProcessAffinityTest {
1922

@@ -67,6 +70,48 @@ public void shouldNotAcquireLockOnCoresLockedByOtherProcesses() throws IOExcepti
6770
}
6871
}
6972

73+
@Ignore("https://github.com/OpenHFT/Java-Thread-Affinity/issues/81")
74+
@Test
75+
public void shouldAllocateCoresCorrectlyUnderContention() throws IOException, InterruptedException {
76+
final int numberOfLockers = Math.max(8, Runtime.getRuntime().availableProcessors());
77+
List<Process> lockers = new ArrayList<>();
78+
for (int i = 0; i < numberOfLockers; i++) {
79+
lockers.add(ProcessRunner.runClass(RepeatedAffinityLocker.class, "last", "100"));
80+
}
81+
for (int i = 0; i < numberOfLockers; i++) {
82+
if (!lockers.get(i).waitFor(10, TimeUnit.SECONDS)) {
83+
throw new IllegalStateException("Locker process didn't end in time");
84+
}
85+
assertEquals(0, lockers.get(i).exitValue());
86+
}
87+
}
88+
89+
/**
90+
* Repeatedly acquires and releases a lock on the specified core
91+
*/
92+
static class RepeatedAffinityLocker {
93+
94+
private static final long PID = LockCheck.getPID();
95+
96+
public static void main(String[] args) throws IOException, InterruptedException {
97+
String cpuIdToLock = args[0];
98+
int iterations = Integer.parseInt(args[1]);
99+
100+
for (int i = 0; i < iterations; i++) {
101+
try (final AffinityLock affinityLock = AffinityLock.acquireLock(cpuIdToLock)) {
102+
long lockPID = Long.parseLong(FileLockBasedLockChecker.getInstance().getMetaInfo(affinityLock.cpuId()));
103+
if (lockPID != PID) {
104+
throw new IllegalStateException(format("PID in lock file is not mine (lockPID=%d, myPID=%d)", lockPID, PID));
105+
}
106+
Thread.sleep(ThreadLocalRandom.current().nextInt(50));
107+
}
108+
}
109+
}
110+
}
111+
112+
/**
113+
* Acquires a lock on the specified CPU, holds it until interrupted
114+
*/
70115
static class AffinityLockerProcess {
71116

72117
private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLockerProcess.class);

0 commit comments

Comments
 (0)