How to bind a process to a CPU | | ________________________________________________________________________
Applicable Environment ________________________________________________________________________ - Topic: Processor affinity
- SDP: 8.0.0, 7.1.0, 7.0.0
- Target: All supported targets
________________________________________________________________________
Question ________________________________________________________________________
How to bind a process to a specific CPU core?
________________________________________________________________________
Solution________________________________________________________________________
Setting the CPU affinity of a process at launchtime
There are many ways to set the CPU affinity of a process to a particular CPU when spawning.
Method 1: Editing the buildfile with the [cpu=] attribute
You can edit a buildfile to set the cpu affinity of a launching process during startup. With this option, there are three different ways to bind a process (or a set of processes) to a single CPU core.
1. Bind the named program to the specified core.
In the buildfile, in front of the process to be launched, add the [cpu=zero-based core number] attribute, followed by the program name.
This will cause debv-umass to run only on core 0.
2. Bind all programs launched after this point to the specified core.
In the buildfile, above the set of processes to be launched, add the [cpu=zero-based core number] attribute, followed by the programs.
[cpu=0] devb-umass devb-nvme ...
This will cause devb-umass, devb-nvme, and any process launched after it to run only on core 0.
3. Bind the named program to run on any CPU core.
In the buildfile, in front of the process to be launched, add the [cpu=*] attribute, followed by the program name.
This will cause devb-umass to run on any core, regardless of the cpu affinity set above it (this is the default).
Method 2: Using the on utility
The second method of setting CPU affinity is by launching an application with the on utility, which allows binding an application to multiple cores instead of one. There are two options that allow this: the -C cpunum and -R runmask options.
Example (-C):
This will allow devb-umass to run on only CPU cores 0 and 1. Alternatively, you can use the -R option to set the runmask.
Example (-R):
This will allow devb-umass to run on only CPU core 0. It is equivalent to on -C0 devb-umass. You can specify more than one -R option to specify a runmask that's more than 32 bits long. The first -R option specifies bits 0 through 31, the second specifies bits 32 through 63, etc.
Finally, you can use the -C and -R options together. The resultant mask is the bitwise ORing of all -CR options.
Example (-C and -R):
This will allow devb-umass to run only on CPU cores 0 and 3.
Method 3: Using the posix_spawn() API
A third method to specify the runmask of a new thread or process is by setting the spawn attributes using the functions posix_spawnattr_setrunmask(), followed by posix_spawnattr_setxflags(), and finally posix_spawn().
Example:
posix_spawnattr_t *attrp; pid_t spawned_pid; uint32_t runmask = 0x00000001; char path[] = "/path/to/executable"; char envp[] = { NULL };
posix_spawnattr_setrunmask(&attrp, runmask); posix_spawnattr_setxflags(&attrp, POSIX_SPAWN_EXPLICIT_CPU);
posix_spawn(&spawned_pid, path, NULL, &attrp, NULL, envp);
This will cause executable to be bound to core 0.
Method 4: Using the spawn() API
Finally, you can set the runmask member of the inheritance structure and specifying SPAWN_EXPLICIT_CPU when calling spawn().
Example:
#include <spawn.h>
char path[] = "/path/to/executable"; struct inheritance inherit; char *argv[];
argv[0] = "executable"; argv[1] = NULL;
inherit.flags = SPAWN_EXPLICIT_CPU; interit.runmask = 0x00000005; // 0x5 = 0b00000101
spawn(path, 0, 0, &inherit, argv, NULL);
This will cause executable to be bound to CPU cores 0 and 2.
Changing the CPU affinity of a running process
You can also change the CPU affinity of a running process. There are two ways to accomplish this: using the ThreadCtl() API or by using the slay utility.
When calling ThreadCtl() you can pass one of three commands: - _NTO_TCTL_RUNMASK
- _NTO_TCTL_RUNMASK_GET_AND_SET
- _NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT
as well as the runmask. See your SDP's documentation page on ThreadCtl() for more information.
When using the slay utility, you can use the -C or -R option to change a process's runmask, much like the on utility.
Example:
This will cause devb-umass to change its affinity to CPU cores 0 and 1.
________________________________________________________________________ NOTE: This entry has been validated against the SDP version listed above. Use caution when considering this advice for any other SDP version. For supported releases, please reach out to QNX Technical Support f you have any questions/concerns. ________________________________________________________________________ |
|
|