Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit afd9574

Browse files
Documentation polish (LICENSE/README/requirements) + test import cleanup (#189)
* Refactor requirements document for clarity and formatting improvements * Remove unused imports in PosixJNAAffinityTest * Format LICENSE.adoc for improved readability * Fix formatting and punctuation in README.adoc for improved readability
1 parent e2175d6 commit afd9574

File tree

4 files changed

+111
-96
lines changed

4 files changed

+111
-96
lines changed

‎LICENSE.adoc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
== Copyright 2016-2025 chronicle.software
22

3-
Licensed under the *Apache License, Version 2.0* (the "License"); you may not use this file except in compliance with the License.
3+
Licensed under the *Apache License, Version 2.0* (the "License");
4+
you may not use this file except in compliance with the License.
45
You may obtain a copy of the License at
56

67
http://www.apache.org/licenses/LICENSE-2.0
78

8-
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9-
See the License for the specific language governing permissions and limitations under the License.
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

‎README.adoc‎

Lines changed: 73 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ image::docs/images/Thread-Affinity_line.png[width=20%]
55
== Version
66

77
[#image-maven]
8-
[caption="",link=https://maven-badges.herokuapp.com/maven-central/net.openhft/affinity]
8+
[caption="",link=https://maven-badges.herokuapp.com/maven-central/net.openhft/affinity]
99
image::https://maven-badges.herokuapp.com/maven-central/net.openhft/affinity/badge.svg[]
1010
image:https://javadoc.io/badge2/net.openhft/affinity/javadoc.svg[link="https://www.javadoc.io/doc/net.openhft/affinity/latest/index.html"]
1111

@@ -14,22 +14,20 @@ Lets you bind a thread to a given core, this can improve performance (this libra
1414

1515
OpenHFT Java Thread Affinity library
1616

17-
See https://github.com/OpenHFT/Java-Thread-Affinity/tree/master/affinity/src/test/java[affinity/src/test/java]
17+
See https://github.com/OpenHFT/Java-Thread-Affinity/tree/master/affinity/src/test/java[affinity/src/test/java]
1818
for working examples of how to use this library.
1919

2020
=== Supported operating systems
2121

22-
The library detects the running platform in `Affinity.java` and selects an
23-
implementation for that OS. Features differ between systems:
22+
The library detects the running platform in `Affinity.java` and selects an implementation for that OS.
23+
Features differ between systems:
2424

25-
* *Linux* - full affinity control via JNA. The implementation can get and set
26-
thread affinity, query the current CPU, and obtain process and thread IDs.
27-
* *Windows* - thread affinity is managed through the kernel API. Process and
28-
thread IDs are available, while `getCpu()` returns `-1`.
29-
* *macOS* - provides process and thread IDs but does not modify affinity and
30-
reports the CPU id as `-1`.
31-
* *Solaris* - mirrors the macOS implementation: only process and thread IDs are
32-
returned with no affinity or CPU querying support.
25+
* *Linux* - full affinity control via JNA.
26+
The implementation can get and set thread affinity, query the current CPU, and obtain process and thread IDs.
27+
* *Windows* - thread affinity is managed through the kernel API.
28+
Process and thread IDs are available, while `getCpu()` returns `-1`.
29+
* *macOS* - provides process and thread IDs but does not modify affinity and reports the CPU id as `-1`.
30+
* *Solaris* - mirrors the macOS implementation: only process and thread IDs are returned with no affinity or CPU querying support.
3331

3432
=== Changes
3533

@@ -39,19 +37,15 @@ implementation for that OS. Features differ between systems:
3937

4038
=== Dependencies
4139

42-
Java-Thread-Affinity will try to use https://github.com/java-native-access/jna[JNA]
43-
to provide access to native thread-handling functions. JNA should be installed on
44-
your system to get the most from this library.
40+
Java-Thread-Affinity will try to use link:https://github.com/java-native-access/jna[JNA]
41+
to provide access to native thread-handling functions.
42+
JNA should be installed on your system to get the most from this library.
4543

4644
=== JNA version
4745

48-
Java-Thread-Affinity currently depends on JNA version 4.4.0, which in turn
49-
depends on a version of GLIBC >= 2.14. If your operating system is an old one,
50-
with a version of GLIBC released before 2011, this library will not be able to
51-
invoke native functions.
46+
Java-Thread-Affinity currently depends on JNA version 4.4.0, which in turn depends on a version of GLIBC >= 2.14. If your operating system is an old one, with a version of GLIBC released before 2011, this library will not be able to invoke native functions.
5247

53-
To work around this problem, fork the repository, and override the `<version>` tag
54-
for the artifacts `jna` and `jna-platform` in the project's `pom` file.
48+
To work around this problem, fork the repository, and override the `<version>` tag for the artifacts `jna` and `jna-platform` in the project's `pom` file.
5549

5650
=== Installing JNA on Ubuntu
5751

@@ -68,26 +62,25 @@ for the artifacts `jna` and `jna-platform` in the project's `pom` file.
6862
Or download jna.jar and jna-platform.jar from the JNA project and add them to your classpath.
6963

7064
=== How does CPU allocation work?
71-
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.
65+
The library will read your `/proc/cpuinfo` if you have one or provide one and it will determine your CPU layout.
66+
If you don't have one it will assume every CPU is on one CPU socket.
7267

73-
The library looks for isolated CPUs determined by looking at the CPUs you are not running on by default.
68+
The library looks for isolated CPUs determined by looking at the CPUs you are not running on by default.
7469
i.e. if you have 16 CPUs but 8 of them are not available for general use (as determined by the affinity of the process on startup) it will start assigning to those CPUs.
7570

7671
Note: if you have more than one process using this library you need to specify which CPUs the process can use otherwise it will assign the same CPUs to both processes.
7772

78-
To control which CPUs a process can use, add `-Daffinity.reserved={cpu-mask-in-hex}`
79-
to the command line of the process. The mask is a hexadecimal bit mask without
80-
the `0x` prefix where bit `0` represents CPU `0`, bit `1` represents CPU `1` and
81-
so on. Multiple CPUs can be specified by setting more than one bit.
73+
To control which CPUs a process can use, add `-Daffinity.reserved={cpu-mask-in-hex}` to the command line of the process.
74+
The mask is a hexadecimal bit mask without the `0x` prefix where bit `0` represents CPU `0`, bit `1` represents CPU `1` and so on.
75+
Multiple CPUs can be specified by setting more than one bit.
8276

8377
For example:
8478

8579
* `-Daffinity.reserved=2` reserves only CPU `1`.
8680
* `-Daffinity.reserved=6` reserves CPUs `1` and `2`.
8781
* `-Daffinity.reserved=10` reserves CPUs `1` and `3` (hexadecimal `a`).
8882

89-
Use an appropriate mask when starting each process to avoid reserving the same
90-
cores for multiple JVMs.
83+
Use an appropriate mask when starting each process to avoid reserving the same cores for multiple JVMs.
9184

9285
Note: the CPU 0 is reserved for the Operating System, it has to run somewhere.
9386

@@ -103,21 +96,22 @@ http://vanillajava.blogspot.co.uk/2013/07/micro-jitter-busy-waiting-and-binding.
10396

10497
Java-Thread-Affinity requires that you first isolate some CPU's.
10598

106-
Once a CPU core is isolated, the Linux scheduler will not use the CPU core to run any user-space processes. The isolated CPUs will not participate in load balancing, and will not have tasks running on them unless explicitly assigned.
99+
Once a CPU core is isolated, the Linux scheduler will not use the CPU core to run any user-space processes.
100+
The isolated CPUs will not participate in load balancing, and will not have tasks running on them unless explicitly assigned.
107101

108102
To isolate the 1st and 3rd CPU cores (CPU numbers start from 0) on your system, add the following to the kernel command line during boot:
109103

110104
isolcpus=1,3
111105

112-
Using GRUB
106+
.Using GRUB
113107
[source]
114108
----
115109
sudo sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT="/GRUB_CMDLINE_LINUX_DEFAULT="isolcpus=1,3 /' /etc/default/grub
116110
sudo update-grub
117111
sudo reboot
118112
----
119113

120-
Using systemd-boot
114+
.Using systemd-boot
121115
[source]
122116
----
123117
sudo sed -i 's/^options \(.*\)/options 1円 isolcpus=1,3/' /boot/loader/entries/*.conf
@@ -129,8 +123,8 @@ sudo reboot
129123
=== Acquiring a CPU lock for a thread
130124
You can acquire a lock for a CPU in the following way:
131125

132-
In Java 6
133-
[source,java]
126+
.In Java 6
127+
[source,java]
134128
----
135129
AffinityLock al = AffinityLock.acquireLock();
136130
try {
@@ -140,26 +134,32 @@ try {
140134
}
141135
----
142136

143-
In Java 7 or 8
144-
[source,java]
137+
.In Java 7 or 8
138+
[source,java]
145139
----
146140
try (AffinityLock al = AffinityLock.acquireLock()) {
147141
// do some work while locked to a CPU.
148142
}
149143
----
144+
150145
You have further options such as
151146

152147
=== Acquiring a CORE lock for a thread
153-
You can reserve a whole core. If you have hyper-threading enabled, this will use one CPU and leave it's twin CPU unused.
154-
[source, java]
148+
You can reserve a whole core.
149+
If you have hyper-threading enabled, this will use one CPU and leave it's twin CPU unused.
150+
151+
[source,java]
155152
----
156153
try (AffinityLock al = AffinityLock.acquireCore()) {
157154
// do some work while locked to a CPU.
158155
}
159156
----
157+
160158
=== Controlling layout
159+
161160
You can chose a layout relative to an existing lock.
162-
[source, java]
161+
162+
[source,java]
163163
----
164164
try (final AffinityLock al = AffinityLock.acquireLock()) {
165165
System.out.println("Main locked");
@@ -175,6 +175,7 @@ try (final AffinityLock al = AffinityLock.acquireLock()) {
175175
t.start();
176176
}
177177
----
178+
178179
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.
179180

180181
=== Affinity strategies
@@ -193,32 +194,39 @@ The `AffinityStrategies` enum defines hints for selecting a CPU relative to an e
193194

194195
=== Getting the thread id
195196
You can get the current thread id using
196-
[source, java]
197-
----
198197

198+
[source,java]
199+
----
199200
int threadId = AffinitySupport.getThreadId();
200201
----
202+
201203
=== Determining which CPU you are running on
202204
You can get the current CPU being used by
203-
[source, java]
204-
----
205205

206+
[source,java]
207+
----
206208
int cpuId = AffinitySupport.getCpu();
207209
----
210+
208211
=== Controlling the affinity more directly
212+
209213
The affinity of the process on start up is
210-
[source, java]
211-
----
212214

215+
[source,java]
216+
----
213217
long baseAffinity = AffinityLock.BASE_AFFINITY;
214218
----
219+
215220
The available CPU for reservation is
216-
[source, java]
221+
222+
[source,java]
217223
----
218224
long reservedAffinity = AffinityLock.RESERVED_AFFINITY;
219225
----
226+
220227
If you want to get/set the affinity directly you can do
221-
[source, java]
228+
229+
[source,java]
222230
----
223231
long currentAffinity = AffinitySupport.getAffinity();
224232
AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.
@@ -227,8 +235,8 @@ AffinitySupport.setAffinity(1L << 5); // lock to CPU 5.
227235
=== Understanding dumpLocks() output
228236

229237
Several examples print the current CPU assignments using `AffinityLock.dumpLocks()`.
230-
Each line of the output begins with the zero based CPU id followed by the status
231-
of that CPU. Example output might look like:
238+
Each line of the output begins with the zero based CPU id followed by the status of that CPU.
239+
Example output might look like:
232240

233241
[source]
234242
----
@@ -239,35 +247,32 @@ of that CPU. Example output might look like:
239247
----
240248

241249
The number on each line is the logical CPU index as recognised by the library.
242-
The text after the colon describes whether that CPU is free, reserved or already
243-
bound to a thread. Use these indices when calling `AffinityLock.acquireLock(n)`
250+
The text after the colon describes whether that CPU is free, reserved or already bound to a thread.
251+
Use these indices when calling `AffinityLock.acquireLock(n)`
244252
or when constructing explicit affinity masks.
245253

246254
=== Lock file directory
247255

248-
AffinityLock stores a small lock file for each CPU. These files are placed in
249-
the directory specified by the `java.io.tmpdir` system property, which by
250-
default points to your system's temporary directory (usually `/tmp` on Linux).
256+
AffinityLock stores a small lock file for each CPU.
257+
These files are placed in the directory specified by the `java.io.tmpdir` system property, which by default points to your system's temporary directory (usually `/tmp` on Linux).
251258

252-
If you want to keep the lock files elsewhere, set this property before using any
253-
affinity APIs:
259+
If you want to keep the lock files elsewhere, set this property before using any affinity APIs:
254260

255-
[source,bash]
261+
[source,bash]
256262
----
257263
java -Djava.io.tmpdir=/path/to/dir ...
258264
----
259265

260266
or in code
261267

262-
[source,java]
268+
[source,java]
263269
----
264270
System.setProperty("java.io.tmpdir", "/path/to/dir");
265271
----
266272

267273
=== Debugging affinity state
268274

269-
For a detailed of view of the current affinity state (as seen by the library),
270-
execute the following script on Linux systems:
275+
For a detailed of view of the current affinity state (as seen by the library), execute the following script on Linux systems:
271276

272277
[source]
273278
----
@@ -289,11 +294,10 @@ $ for i in "$(ls cpu-*)";
289294

290295
== Using AffinityThreadFactory
291296

292-
`AffinityThreadFactory` binds each thread it creates according to a set of
293-
`AffinityStrategy` rules. This allows executors to automatically run tasks on
294-
cores selected by the library.
297+
`AffinityThreadFactory` binds each thread it creates according to a set of `AffinityStrategy` rules.
298+
This allows executors to automatically run tasks on cores selected by the library.
295299

296-
[source,java]
300+
[source,java]
297301
----
298302
ExecutorService es = Executors.newFixedThreadPool(4,
299303
new AffinityThreadFactory("worker",
@@ -315,12 +319,14 @@ For an article on how much difference affinity can make and how to use it http:/
315319
== Questions and Answers
316320

317321
=== Question: How to lock a specific cpuId
318-
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
319-
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.
322+
I am currently working on a project related to deadlock detection in multithreaded programs in java.
323+
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
324+
Being a beginner, I have little knowledge and thus need your assistance.
325+
We need to know how to run threads on specified cpu number and then switch threads when one is waiting.
320326

321327
=== Answer
322328

323-
[source,java]
329+
[source,java]
324330
----
325331
// lock a cpuId
326332
try (AffinityLock lock = AffinityLock.acquireLock(n)) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /