Skip to content

Commit 6156511

Browse files
committed
Add process id and thread id
1 parent f076bcd commit 6156511

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ public interface IAffinity {
3737
* @return the current cpu id, or -1 if unknown.
3838
*/
3939
int getCpu();
40+
41+
/**
42+
* @return the process id of the current process.
43+
*/
44+
int getProcessId();
45+
46+
/**
47+
* @return the thread id of the current thread or -1 is not available.
48+
*/
49+
int getThreadId();
4050
}

affinity/src/main/java/net/openhft/affinity/impl/NullAffinity.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import net.openhft.affinity.IAffinity;
2121

22+
import java.lang.management.ManagementFactory;
2223
import java.util.logging.Level;
2324
import java.util.logging.Logger;
2425

@@ -27,7 +28,6 @@
2728
*/
2829
public enum NullAffinity implements IAffinity {
2930
INSTANCE;
30-
3131
private static final Logger LOGGER = Logger.getLogger(NullAffinity.class.getName());
3232

3333
@Override
@@ -46,4 +46,16 @@ public int getCpu() {
4646
return -1;
4747
}
4848

49+
@Override
50+
public int getProcessId() {
51+
final String name = ManagementFactory.getRuntimeMXBean().getName();
52+
return Integer.parseInt(name.split("@")[0]);
53+
}
54+
55+
@Override
56+
public int getThreadId() {
57+
throw new UnsupportedOperationException();
58+
}
59+
60+
4961
}

affinity/src/main/java/net/openhft/affinity/impl/PosixJNAAffinity.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public int getCpu() {
8282
}
8383
}
8484

85+
@Override
86+
public int getProcessId() {
87+
return CLibrary.INSTANCE.getpid();
88+
}
89+
90+
@Override
91+
public int getThreadId() {
92+
return -1;
93+
}
94+
8595

8696
/**
8797
* @author BegemoT
@@ -99,6 +109,8 @@ int sched_getaffinity(final int pid,
99109
final PointerType cpuset) throws LastErrorException;
100110

101111
int sched_getcpu() throws LastErrorException;
112+
113+
int getpid() throws LastErrorException;
102114
}
103115

104116
static {

affinity/src/main/java/net/openhft/affinity/impl/WindowsJNAAffinity.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.sun.jna.Library;
2121
import com.sun.jna.Native;
2222
import com.sun.jna.PointerType;
23+
import com.sun.jna.platform.win32.Kernel32;
2324
import com.sun.jna.platform.win32.WinDef;
2425
import com.sun.jna.ptr.LongByReference;
2526
import net.openhft.affinity.IAffinity;
@@ -90,6 +91,16 @@ public int getCpu() {
9091
return -1;
9192
}
9293

94+
@Override
95+
public int getProcessId() {
96+
return Kernel32.INSTANCE.GetCurrentProcessId();
97+
}
98+
99+
@Override
100+
public int getThreadId() {
101+
return Kernel32.INSTANCE.GetCurrentThreadId();
102+
}
103+
93104

94105
/**
95106
* @author BegemoT

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626

27+
import static net.openhft.affinity.AffinityLock.acquireLock;
28+
import static net.openhft.affinity.AffinityLock.dumpLocks;
2729
import static org.junit.Assert.assertEquals;
2830
import static org.junit.Assert.assertNotSame;
2931

@@ -177,4 +179,32 @@ public void testGettid() {
177179
System.out.println("cpu= " + AffinitySupport.getCpu());
178180
}
179181

182+
@Test
183+
public void testAffinity() throws InterruptedException {
184+
System.out.println("Started");
185+
displayStatus();
186+
final AffinityLock al = acquireLock();
187+
System.out.println("Main locked");
188+
displayStatus();
189+
Thread t = new Thread(new Runnable() {
190+
@Override
191+
public void run() {
192+
AffinityLock al2 = al.acquireLock(AffinityStrategies.ANY);
193+
System.out.println("Thread-0 locked");
194+
displayStatus();
195+
al2.release();
196+
}
197+
});
198+
t.start();
199+
t.join();
200+
System.out.println("Thread-0 unlocked");
201+
displayStatus();
202+
al.release();
203+
System.out.println("All unlocked");
204+
displayStatus();
205+
}
206+
207+
private void displayStatus() {
208+
System.out.println(Thread.currentThread() + " on " + AffinitySupport.getCpu() + "\n" + dumpLocks());
209+
}
180210
}

0 commit comments

Comments
 (0)