Skip to content

Commit 3e7c35d

Browse files
author
Rob Austin
committed
cleanups with peter
1 parent 8be8e23 commit 3e7c35d

4 files changed

Lines changed: 57 additions & 65 deletions

File tree

affinity/src/main/java/net/openhft/affinity/AffinityLock.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import java.util.BitSet;
3030

3131
/**
32-
* This utility class support locking a thread to a single core, or reserving a whole core for a thread.
32+
* This utility class support locking a thread to a single core, or reserving a whole core for a
33+
* thread.
3334
*
3435
* @author peter.lawrey
3536
*/
@@ -67,8 +68,8 @@ public class AffinityLock implements Closeable {
6768
*/
6869
private final boolean reservable;
6970
/**
70-
* An inventory build from the CPU layout which keeps track of the various locks
71-
* belonging to each CPU.
71+
* An inventory build from the CPU layout which keeps track of the various locks belonging to
72+
* each CPU.
7273
*/
7374
private final LockInventory lockInventory;
7475
boolean bound = false;
@@ -84,10 +85,9 @@ public class AffinityLock implements Closeable {
8485
}
8586

8687
/**
87-
* Set the CPU layout for this machine. CPUs which are not mentioned will be ignored.
88-
* <p>
89-
* Changing the layout will have no impact on thread which have already been assigned.
90-
* It only affects subsequent assignments.
88+
* Set the CPU layout for this machine. CPUs which are not mentioned will be ignored. <p>
89+
* Changing the layout will have no impact on thread which have already been assigned. It only
90+
* affects subsequent assignments.
9191
*
9292
* @param cpuLayout for this application to use for this machine.
9393
*/
@@ -135,9 +135,8 @@ public static AffinityLock acquireLock() {
135135
}
136136

137137
/**
138-
* Assign any free core to this thread.
139-
* <p>
140-
* In reality, only one cpu is assigned, the rest of the threads for that core are reservable so they are not used.
138+
* Assign any free core to this thread. <p> In reality, only one cpu is assigned, the rest of
139+
* the threads for that core are reservable so they are not used.
141140
*
142141
* @return A handle for the current AffinityLock.
143142
*/
@@ -146,11 +145,11 @@ public static AffinityLock acquireCore() {
146145
}
147146

148147
/**
149-
* Assign a cpu which can be bound to the current thread or another thread.
150-
* <p>
151-
* This can be used for defining your thread layout centrally and passing the handle via dependency injection.
148+
* Assign a cpu which can be bound to the current thread or another thread. <p> This can be used
149+
* for defining your thread layout centrally and passing the handle via dependency injection.
152150
*
153-
* @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
151+
* @param bind if true, bind the current thread, if false, reserve a cpu which can be bound
152+
* later.
154153
* @return A handle for an affinity lock.
155154
*/
156155
public static AffinityLock acquireLock(boolean bind) {
@@ -159,10 +158,11 @@ public static AffinityLock acquireLock(boolean bind) {
159158

160159
/**
161160
* Assign a core(and all its cpus) which can be bound to the current thread or another thread.
162-
* <p>
163-
* This can be used for defining your thread layout centrally and passing the handle via dependency injection.
161+
* <p> This can be used for defining your thread layout centrally and passing the handle via
162+
* dependency injection.
164163
*
165-
* @param bind if true, bind the current thread, if false, reserve a cpu which can be bound later.
164+
* @param bind if true, bind the current thread, if false, reserve a cpu which can be bound
165+
* later.
166166
* @return A handle for an affinity lock.
167167
*/
168168
public static AffinityLock acquireCore(boolean bind) {
@@ -192,7 +192,8 @@ private static boolean areAssertionsEnabled() {
192192
}
193193

194194
/**
195-
* Assigning the current thread has a side effect of preventing the lock being used again until it is released.
195+
* Assigning the current thread has a side effect of preventing the lock being used again until
196+
* it is released.
196197
*
197198
* @param bind whether to bind the thread as well
198199
* @param wholeCore whether to reserve all the thread in the same core.
@@ -237,6 +238,10 @@ public void bind(boolean wholeCore) {
237238
}
238239

239240
final boolean canReserve() {
241+
242+
if (!LockCheck.isCpuFree(cpuId))
243+
return false;
244+
240245
if (!reservable) return false;
241246
if (assignedThread != null) {
242247
if (assignedThread.isAlive()) {
@@ -249,10 +254,10 @@ final boolean canReserve() {
249254
}
250255

251256
/**
252-
* Give another affinity lock relative to this one based on a list of strategies.
253-
* <p>
254-
* The strategies are evaluated in order to (like a search path) to find the next appropriate thread.
255-
* If ANY is not the last strategy, a warning is logged and no cpu is assigned (leaving the OS to choose)
257+
* Give another affinity lock relative to this one based on a list of strategies. <p> The
258+
* strategies are evaluated in order to (like a search path) to find the next appropriate
259+
* thread. If ANY is not the last strategy, a warning is logged and no cpu is assigned (leaving
260+
* the OS to choose)
256261
*
257262
* @param strategies To determine if you want the same/different core/socket.
258263
* @return A matching AffinityLock.

affinity/src/main/java/net/openhft/affinity/AffinityStrategies.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package net.openhft.affinity;
1818

19-
import java.io.IOException;
20-
2119
/**
2220
* Pre-defined strategies for determining which thread to pick next.
2321
*
@@ -31,22 +29,8 @@ public enum AffinityStrategies implements AffinityStrategy {
3129
ANY {
3230
@Override
3331
public boolean matches(int cpuId, int cpuId2) {
34-
if (LockCheck.IS_LINUX) {
35-
if (cpuId == -1 && cpuId2 != -1)
36-
return isFreeCpu(cpuId2);
37-
else if (cpuId != -1 && cpuId2 == -1)
38-
return isFreeCpu(cpuId);
39-
}
4032
return true;
4133
}
42-
43-
private boolean isFreeCpu(int cpuId) {
44-
try {
45-
return LockCheck.isCpuFree(cpuId);
46-
} catch (IOException e) {
47-
return true;
48-
}
49-
}
5034
},
5135
/**
5236
* Must be a cpu on the same core.

affinity/src/main/java/net/openhft/affinity/LockCheck.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,24 @@ static long getPID() {
3434
return Long.parseLong(processName.split("@")[0]);
3535
}
3636

37-
static boolean isCpuFree(int cpu) throws IOException {
37+
public static boolean isCpuFree(int cpu) {
38+
39+
if (!IS_LINUX)
40+
return true;
3841

3942
final File file = toFile(cpu);
4043
final boolean exists = file.exists();
4144

4245
if (!exists) {
4346
return true;
4447
} else {
45-
int currentProcess = getProcessForCpu(file);
48+
int currentProcess = 0;
49+
try {
50+
currentProcess = getProcessForCpu(file);
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
return true;
54+
}
4655
if (!isProcessRunning(currentProcess)) {
4756
file.delete();
4857
return true;
@@ -75,17 +84,13 @@ static boolean isProcessRunning(long pid) {
7584
/**
7685
* stores the pid in a file, named by the core, the pid is written to the file with the date
7786
* below
78-
*
79-
* @param processID
80-
* @param coreFile
81-
* @throws IOException
8287
*/
8388
private synchronized static void storePid(long processID, File coreFile) throws IOException {
84-
RandomAccessFile f = new RandomAccessFile(coreFile, "rw");
85-
f.seek(0); // to the beginning
86-
String processIDStr = Long.toString(processID);
87-
f.write((processIDStr + "\n" + df.format(new Date())).getBytes());
88-
f.close();
89+
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
90+
new FileOutputStream(coreFile), "utf-8"))) {
91+
String processIDStr = Long.toString(processID);
92+
writer.write(processIDStr + "\n" + df.format(new Date()));
93+
}
8994
}
9095

9196
static int getProcessForCpu(int core) throws IOException {
@@ -110,10 +115,14 @@ private static File tmpDir() {
110115
}
111116

112117
static void updateCpu(int cpu) {
118+
if (!IS_LINUX)
119+
return;
113120
try {
114121
replacePid(toFile(cpu), getPID());
115122
} catch (IOException e) {
116123
e.printStackTrace();
117124
}
118125
}
126+
127+
119128
}

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.openhft.affinity;
22

33
import org.junit.Assert;
4+
import org.junit.Assume;
45
import org.junit.Before;
56
import org.junit.Test;
67

@@ -18,37 +19,30 @@ public class LockCheckTest {
1819

1920
@Before
2021
public void before() {
21-
System.setProperty("java.io.tmpdir", LockCheck.TMP + "/" + System.nanoTime());
22+
Assume.assumeTrue(IS_LINUX);
23+
System.setProperty("java.io.tmpdir", LockCheck.TARGET + "/" + System.nanoTime());
2224
}
2325

2426
@Test
2527
public void test() throws IOException {
26-
if (IS_LINUX) {
27-
28-
Assert.assertTrue(LockCheck.isCpuFree(CPU));
29-
LockCheck.updateCpu(CPU);
30-
Assert.assertEquals(LockCheck.getPID(), LockCheck.getProcessForCpu(CPU));
31-
}
28+
Assert.assertTrue(LockCheck.isCpuFree(CPU));
29+
LockCheck.updateCpu(CPU);
30+
Assert.assertEquals(LockCheck.getPID(), LockCheck.getProcessForCpu(CPU));
3231
}
3332

3433
@Test
3534
public void testPidOnLinux() {
3635

37-
38-
if (IS_LINUX)
39-
Assert.assertTrue(LockCheck.isProcessRunning(LockCheck.getPID()));
40-
36+
Assert.assertTrue(LockCheck.isProcessRunning(LockCheck.getPID()));
4137
}
4238

4339
@Test
4440
public void testReplace() throws IOException {
4541
CPU++;
46-
if (IS_LINUX) {
42+
Assert.assertTrue(LockCheck.isCpuFree(CPU + 1));
43+
LockCheck.replacePid(CPU, 123L);
44+
Assert.assertEquals(123L, LockCheck.getProcessForCpu(CPU));
4745

48-
Assert.assertTrue(LockCheck.isCpuFree(CPU + 1));
49-
LockCheck.replacePid(CPU, 123L);
50-
Assert.assertEquals(123L, LockCheck.getProcessForCpu(CPU));
51-
}
5246
}
5347

5448

0 commit comments

Comments
 (0)