Skip to content

Commit 8be8e23

Browse files
author
Rob Austin
committed
refactors around LockCheck
1 parent 11cb30d commit 8be8e23

4 files changed

Lines changed: 44 additions & 72 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
public enum AffinityStrategies implements AffinityStrategy {
2727

28-
2928
/**
3029
* Any free cpu.
3130
*/
@@ -41,9 +40,9 @@ else if (cpuId != -1 && cpuId2 == -1)
4140
return true;
4241
}
4342

44-
private boolean isFreeCpu(int cpuId2) {
43+
private boolean isFreeCpu(int cpuId) {
4544
try {
46-
return lc.isFreeCpu(cpuId2);
45+
return LockCheck.isCpuFree(cpuId);
4746
} catch (IOException e) {
4847
return true;
4948
}
@@ -93,5 +92,4 @@ public boolean matches(int cpuId, int cpuId2) {
9392
}
9493
};
9594

96-
LockCheck lc = new LockCheck();
9795
}

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

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@
1010
/**
1111
* @author Rob Austin.
1212
*/
13-
class LockCheck {
13+
enum LockCheck {
14+
;
1415

1516
static final String TMP = System.getProperty("java.io.tmpdir");
1617
public static final String TARGET = System.getProperty("project.build.directory", findTarget());
1718
private static final String OS = System.getProperty("os.name").toLowerCase();
1819
static final boolean IS_LINUX = OS.startsWith("linux");
19-
private ThreadLocal<SimpleDateFormat> df = new ThreadLocal() {
20-
21-
@Override
22-
protected Object initialValue() {
23-
return new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z");
24-
}
25-
};
20+
private static SimpleDateFormat df = new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z");
2621

2722
private static String findTarget() {
2823
for (File dir = new File(System.getProperty("user.dir")); dir != null; dir = dir.getParentFile()) {
@@ -33,74 +28,44 @@ private static String findTarget() {
3328
return TMP + "/target";
3429
}
3530

36-
public static long getPID() {
31+
static long getPID() {
3732
String processName =
3833
java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
3934
return Long.parseLong(processName.split("@")[0]);
4035
}
4136

42-
boolean isFreeCpu(int cpu) throws IOException {
43-
long processID = getPID();
37+
static boolean isCpuFree(int cpu) throws IOException {
4438

4539
final File file = toFile(cpu);
4640
final boolean exists = file.exists();
4741

4842
if (!exists) {
49-
// create a lock file
50-
storePid(processID, file);
51-
return true;
52-
} else {
53-
int currentProcess = getProcessForCore(file);
54-
if (!isProcessRunning(currentProcess)) {
55-
replacePid(file, processID);
56-
return true;
57-
}
58-
return false;
59-
}
60-
}
61-
62-
/**
63-
* stores the process id for a give pid
64-
*
65-
* @param core
66-
* @param processID
67-
* @return
68-
* @throws IOException
69-
*/
70-
private boolean isFreeCpu(int core, long processID) throws IOException {
71-
72-
final File file = toFile(core);
73-
final boolean exists = file.exists();
74-
75-
if (!exists) {
76-
// create a lock file
77-
storePid(processID, file);
7843
return true;
7944
} else {
80-
int currentProcess = getProcessForCore(file);
45+
int currentProcess = getProcessForCpu(file);
8146
if (!isProcessRunning(currentProcess)) {
82-
replacePid(file, processID);
47+
file.delete();
8348
return true;
8449
}
8550
return false;
8651
}
8752
}
8853

8954
@NotNull
90-
private File toFile(int core) {
55+
private static File toFile(int core) {
9156
return new File(tmpDir(), "cpu-" + core + ".lock");
9257
}
9358

94-
void replacePid(int core, long processID) throws IOException {
59+
static void replacePid(int core, long processID) throws IOException {
9560
replacePid(toFile(core), processID);
9661
}
9762

98-
private void replacePid(File file, long processID) throws IOException {
63+
private static void replacePid(File file, long processID) throws IOException {
9964
file.delete();
10065
storePid(processID, file);
10166
}
10267

103-
boolean isProcessRunning(long pid) {
68+
static boolean isProcessRunning(long pid) {
10469
if (IS_LINUX)
10570
return new File("/proc/" + pid).exists();
10671
else
@@ -115,28 +80,27 @@ boolean isProcessRunning(long pid) {
11580
* @param coreFile
11681
* @throws IOException
11782
*/
118-
private void storePid(long processID, File coreFile) throws IOException {
119-
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
120-
new FileOutputStream(coreFile), "utf-8"))) {
121-
String processIDStr = Long.toString(processID);
122-
writer.write(processIDStr + "\n" + df.get().format(new Date()));
123-
}
83+
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();
12489
}
12590

126-
int getProcessForCore(int core) throws IOException {
127-
return getProcessForCore(toFile(core));
91+
static int getProcessForCpu(int core) throws IOException {
92+
return getProcessForCpu(toFile(core));
12893
}
12994

130-
private int getProcessForCore(@NotNull File coreFile) throws IOException {
131-
95+
private static int getProcessForCpu(@NotNull File coreFile) throws IOException {
13296
try (LineNumberReader reader = new LineNumberReader(
13397
new BufferedReader(new InputStreamReader(new FileInputStream(coreFile), "utf-8")))) {
13498
String s = reader.readLine().trim();
13599
return Integer.parseInt(s);
136100
}
137101
}
138102

139-
private File tmpDir() {
103+
private static File tmpDir() {
140104
final File tempDir = new File(System.getProperty("java.io.tmpdir"));
141105

142106
if (!tempDir.exists())
@@ -145,4 +109,11 @@ private File tmpDir() {
145109
return tempDir;
146110
}
147111

112+
static void updateCpu(int cpu) {
113+
try {
114+
replacePid(toFile(cpu), getPID());
115+
} catch (IOException e) {
116+
e.printStackTrace();
117+
}
118+
}
148119
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public final synchronized AffinityLock acquireLock(boolean bind, int cpuId, Affi
8888
AffinityLock al = logicalCoreLocks[i];
8989
if (al.canReserve() && (cpuId < 0 || strategy.matches(cpuId, al.cpuId()))) {
9090
al.assignCurrentThread(bind, false);
91+
LockCheck.updateCpu(al.cpuId());
9192
return al;
9293
}
9394
}
@@ -108,6 +109,7 @@ public final synchronized AffinityLock acquireCore(boolean bind, int cpuId, Affi
108109

109110
final AffinityLock al = als[0];
110111
al.assignCurrentThread(bind, true);
112+
LockCheck.updateCpu(al.cpuId());
111113
return al;
112114
}
113115
}

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class LockCheckTest {
1515

1616
private static final int PROCESS_ID = 2222;
17-
private static int CORE = 1111;
17+
private static int CPU = 1111;
1818

1919
@Before
2020
public void before() {
@@ -24,29 +24,30 @@ public void before() {
2424
@Test
2525
public void test() throws IOException {
2626
if (IS_LINUX) {
27-
LockCheck lockCheck = new LockCheck();
28-
Assert.assertTrue(lockCheck.isFreeCpu(CORE));
29-
Assert.assertEquals(LockCheck.getPID(), lockCheck.getProcessForCore(CORE));
27+
28+
Assert.assertTrue(LockCheck.isCpuFree(CPU));
29+
LockCheck.updateCpu(CPU);
30+
Assert.assertEquals(LockCheck.getPID(), LockCheck.getProcessForCpu(CPU));
3031
}
3132
}
3233

3334
@Test
3435
public void testPidOnLinux() {
35-
final LockCheck lockCheck = new LockCheck();
36+
3637

3738
if (IS_LINUX)
38-
Assert.assertTrue(lockCheck.isProcessRunning(LockCheck.getPID()));
39+
Assert.assertTrue(LockCheck.isProcessRunning(LockCheck.getPID()));
3940

4041
}
4142

4243
@Test
4344
public void testReplace() throws IOException {
44-
CORE++;
45+
CPU++;
4546
if (IS_LINUX) {
46-
LockCheck lockCheck = new LockCheck();
47-
Assert.assertTrue(lockCheck.isFreeCpu(CORE + 1));
48-
lockCheck.replacePid(CORE, 123L);
49-
Assert.assertEquals(123L, lockCheck.getProcessForCore(CORE));
47+
48+
Assert.assertTrue(LockCheck.isCpuFree(CPU + 1));
49+
LockCheck.replacePid(CPU, 123L);
50+
Assert.assertEquals(123L, LockCheck.getProcessForCpu(CPU));
5051
}
5152
}
5253

0 commit comments

Comments
 (0)