Getting started
The Affinity Library lets you assign and release CPUs to threads.
Reserving Some CPUs
You need to reserve some CPUs for dedicated use. On Linux, you can do this by editing grub.conf or grub.cfg under /boot/grub/grub.conf. Add this argument:
isolcpus=2,3,6,7
NOTE:
This assumes you have an i7. If you have a different processor(s) you need to look at /proc/cpuinfo to determine which combination are worth reserving. A reboot is required.
I would suggest using all the threads of the same core (if you have hyper threading) and use the same socket. If you have multiple sockets, you could try reserving an entire socket so that that chip is only used for your programs.
Configuring irqbalance
In /etc/sysconfig/irqbalance or /etc/defaults/irqbalance you can set:
IRQBALANCE_BANNED_CPUS=CC
In the case this is not working, turn off irqbalance instead using the following command:
$ sudo chkconfig irqbalance off
Assigning Reserved CPUs on the Command Line.
With or without this you can set a bitmask of reserved CPUs with:
-Daffinity.reserved=cc
This can be used to trial reserving CPUs, or if you have more than one program needing reserved CPUs, this ensures that they don't try to use the same CPU.
NOTE: This strategy won't be as effective as preventing other threads from using those CPUs.
Building the Library
You will firstly need to build the library for your platform on Linux by:
- Checking out the source.
- Making the Makefile executable. On Unix -
chmod +x src/main/c/Makefile. NOTE: You will needgccandmakeinstalled. - Compile it with
mvn compile. - Add the library
libaffinity.soto your LD_LIBRARY_PATH or link it to you work directory. - Build it with
mvn test.
What To Expect
Estimated clock frequency was 3724 MHz
Assigning cpu 7 to Thread[main,5,main]
Assigning cpu 6 to Thread[reader,5,main]
Assigning cpu 3 to Thread[writer,5,main]
Releasing cpu 7 from Thread[main,5,main]
Assigning cpu 7 to Thread[engine,5,main]
The assignment of CPUs is
0: General use CPU
1: General use CPU
2: Reserved for this application
3: Thread[writer,5,main] alive=true
4: General use CPU
5: General use CPU
6: Thread[reader,5,main] alive=true
7: Thread[engine,5,main] alive=true
Releasing cpu 6 from Thread[reader,5,main]
Releasing cpu 3 from Thread[writer,5,main]
Releasing cpu 7 from Thread[engine,5,main]`