|
9 | 9 |
|
10 | 10 | import java.io.IOException;
|
11 | 11 | import java.time.LocalDateTime;
|
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.ThreadLocalRandom; |
12 | 15 | import java.util.concurrent.TimeUnit;
|
13 | 16 |
|
| 17 | +import static java.lang.String.format; |
14 | 18 | 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.*; |
17 | 20 |
|
18 | 21 | public class MultiProcessAffinityTest {
|
19 | 22 |
|
@@ -67,6 +70,48 @@ public void shouldNotAcquireLockOnCoresLockedByOtherProcesses() throws IOExcepti
|
67 | 70 | }
|
68 | 71 | }
|
69 | 72 |
|
| 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 | + */ |
70 | 115 | static class AffinityLockerProcess {
|
71 | 116 |
|
72 | 117 | private static final Logger LOGGER = LoggerFactory.getLogger(AffinityLockerProcess.class);
|
|
0 commit comments