You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- V2.0.1 - Added getThreadId for the process if of the thread.
22
+
* V3.1.1 - Upgraded JNA dependency to 4.4.0
23
+
* V2.0.1 - Added getThreadId for the process if of the thread.
22
24
23
-
## Dependencies
25
+
=== Dependencies
24
26
25
-
Java-Thread-Affinity will try to use [JNA](https://github.com/java-native-access/jna)
27
+
Java-Thread-Affinity will try to use https://github.com/java-native-access/jna[JNA]
26
28
to provide access to native thread-handling functions. JNA should be installed on
27
29
your system to get the most from this library.
28
30
29
-
### JNA version
31
+
==== JNA version
30
32
31
33
Java-Thread-Affinity currently depends on JNA version 4.4.0, which in turn
32
34
depends on a version of GLIBC >= 2.14. If your operating system is an old one,
@@ -36,15 +38,15 @@ invoke native functions.
36
38
To work around this problem, fork the repository, and override the `<version>` tag
37
39
for the artifacts `jna` and `jna-platform` in the project's `pom` file.
38
40
39
-
### Installing JNA on Ubuntu
41
+
==== Installing JNA on Ubuntu
40
42
41
43
sudo apt-get install libjna-java
42
44
43
-
### Installing JNA on CentOS
45
+
==== Installing JNA on CentOS
44
46
45
47
sudo yum install jna
46
48
47
-
## How does CPU allocation work?
49
+
=== How does CPU allocation work?
48
50
The library will read your `/proc/cpuinfo` if you have one or provide one and it will determine your CPU layout. If you don't have one it will assume every CPU is on one CPU socket.
49
51
50
52
The library looks for isolated CPUs determined by looking at the CPUs you are not running on by default.
@@ -55,15 +57,15 @@ To control which CPUs a process can use, add -Daffinity.reserved={cpu-mask-in-he
55
57
56
58
Note: the CPU 0 is reserved for the Operating System, it has to run somewhere.
Java-Thread-Affinity requires that you first isolate some CPU's.
69
71
@@ -73,36 +75,41 @@ To isolate the 1st and 3rd CPU cores (CPU numbers start from 0) on your system,
73
75
74
76
isolcpus=1,3
75
77
76
-
## Acquiring a CPU lock for a thread
78
+
==== Acquiring a CPU lock for a thread
77
79
You can acquire a lock for a CPU in the following matter
78
80
79
81
In Java 6
80
-
82
+
[source, java]
83
+
----
81
84
AffinityLock al = AffinityLock.acquireLock();
82
85
try {
83
86
// do some work locked to a CPU.
84
87
} finally {
85
88
al.release();
86
89
}
90
+
----
87
91
88
92
In Java 7 or 8
89
-
93
+
[source, java]
94
+
----
90
95
try (AffinityLock al = AffinityLock.acquireLock()) {
91
96
// do some work while locked to a CPU.
92
97
}
93
-
98
+
----
94
99
You have further options such as
95
100
96
-
## Acquiring a CORE lock for a thread
101
+
=== Acquiring a CORE lock for a thread
97
102
You can reserve a whole core. If you have hyper-threading enabled, this will use one CPU and leave it's twin CPU unused.
98
-
103
+
[source, java]
104
+
----
99
105
try (AffinityLock al = AffinityLock.acquireCore()) {
100
106
// do some work while locked to a CPU.
101
107
}
102
-
103
-
## Controlling layout
108
+
----
109
+
=== Controlling layout
104
110
You can chose a layout relative to an existing lock.
105
-
111
+
[source, java]
112
+
----
106
113
try (final AffinityLock al = AffinityLock.acquireLock()) {
107
114
System.out.println("Main locked");
108
115
Thread t = new Thread(new Runnable() {
@@ -116,20 +123,20 @@ You can chose a layout relative to an existing lock.
116
123
});
117
124
t.start();
118
125
}
119
-
126
+
----
120
127
In this example, the library will prefer a free CPU on the same Socket as the first thread, otherwise it will pick any free CPU.
121
128
122
-
## Getting the thread id.
129
+
=== Getting the thread id.
123
130
You can get the current thread id using
124
131
125
132
int threadId = AffinitySupport.getThreadId();
126
133
127
-
## Determining which CPU you are running on.
134
+
=== Determining which CPU you are running on.
128
135
You can get the current CPU being used by
129
136
130
137
int cpuId = AffinitySupport.getCpu();
131
138
132
-
## Controlling the affinity more directly.
139
+
=== Controlling the affinity more directly.
133
140
The affinity of the process on start up is
134
141
135
142
long baseAffinity = AffinityLock.BASE_AFFINITY;
@@ -143,12 +150,13 @@ If you want to get/set the affinity directly you can do
143
150
long currentAffinity = AffinitySupport.getAffinity();
144
151
AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.
145
152
146
-
## Debugging affinity state
153
+
=== Debugging affinity state
147
154
148
155
For a detailed of view of the current affinity state (as seen by the library),
149
156
execute the following script on Linux systems:
150
157
151
-
```
158
+
[source]
159
+
----
152
160
# change to the affinity lock-file directory (defaults to system property java.io.tmpdir)
153
161
$ cd /tmp
154
162
@@ -163,27 +171,28 @@ $ for i in "$(ls cpu-*)";
163
171
pid 14584's current affinity list: 3
164
172
/opt/jdk1.8.0_141/bin/java ...
165
173
166
-
```
174
+
----
167
175
168
-
# Support Material
176
+
== Support Material
169
177
170
-
[Java Thread Affinity support group](https://groups.google.com/forum/?hl=en-GB#!forum/java-thread-affinity)
178
+
https://groups.google.com/forum/?hl=en-GB#!forum/java-thread-affinity[Java Thread Affinity support group]
171
179
172
180
For an article on how much difference affinity can make and how to use it http://vanillajava.blogspot.com/2013/07/micro-jitter-busy-waiting-and-binding.html
173
181
174
-
# Questions and Answers
182
+
== Questions and Answers
175
183
176
-
## Question
184
+
=== Question
177
185
I am currently working on a project related to deadlock detection in multithreaded programs in java. We are trying to run threads on different processors and thus came across your github posts regarding the same. https://github.com/peter-lawrey/Java-Thread-Affinity/wiki/Getting-started
178
186
Being a beginner, I have little knowledge and thus need your assistance. We need to know how to run threads on specified cpu number and then switch threads when one is waiting.
0 commit comments