Skip to content

Instantly share code, notes, and snippets.

@dipanjanS
Last active November 13, 2023 20:36
Show Gist options
  • Save dipanjanS/b6a7b63c6c3f111f3d04 to your computer and use it in GitHub Desktop.
Save dipanjanS/b6a7b63c6c3f111f3d04 to your computer and use it in GitHub Desktop.
Utility commands to check and allocate processor cores to different processes

Get the processor core affinity for a process ( cores on which it is allowed to run )

taskset -cp <PID>

Example,

[root@user]# taskset -cp 74515
pid 74515's current affinity list: 0-7

__Set the processor core affinity for a process ( cores on which it is allowed to run )__
taskset -cp <CORE RANGE> <PID>

Example,

[root@user]# taskset -cp 1-7 74117
pid 74117's current affinity list: 0-7
pid 74117's new affinity list: 1-7

__Check the PIDs and core affinities of any process using grep__
ps -F -ef |  grep <PID or PROCESS NAME> | awk '{s = "|"; for (i = 1; i <= NF; i++) if (i==1||i==2||i==7||i==8) {s = s $i "\t|\t"} else if (i>=11) {s = s $i " "}; print s }'

Output format will be as follows

| User | Process ID | Core it is running on | Date\Time | Process name |

Example,

[root@user]# ps -F -ef |  grep mysql | awk '{s = "|"; for (i = 1; i <= NF; i++) if (i==1||i==2||i==7||i==8) {s = s $i "\t|\t"} else if (i>=11) {s = s $i " "}; print s }'

|DEV    |       9174    |       0       |       Sep09   |       mysql -u test -p
|DEV    |       52637   |       0       |       11:24   |       mysql -u test -p
|root   |       79386   |       2       |       17:15   |       grep mysql

[root@user]# ps -F -ef |  grep 9174 | awk '{s = "|"; for (i = 1; i <= NF; i++) if (i==1||i==2||i==7||i==8) {s = s $i "\t|\t"} else if (i>=11) {s = s $i " "}; print s }'
|DEV    |       9174    |       0       |       Sep09   |       mysql -u test -p
|root   |       79419   |       1       |       17:16   |       grep 9174


__Start any program\process and restrict it to specific cores__
 taskset -cp <CORE RANGE> <Executable Command>

Example,

[root@user]# taskset -cp 1-7 python2.7 src/start.py

Then you can verify if it works by checking it using the following commands
[root@user]# ps -ef | grep start
root      74117  73796  7 16:27 pts/666  00:00:01 python2.7 src/start.py
root      74142  58348  0 16:27 pts/667  00:00:00 grep start

[root@user]# taskset -cp 74117
pid 74117's current affinity list: 1-7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment