Saturday, September 24, 2022

Some useful Linux command line tools for handling processes and threads

Listing down some command line tools I have found useful for monitoring/experimenting for later reference.

CPU and memory utilization

Use top(1) to see the CPU and memory utilization of the threads in a process

top -H -p <pid>


cpusets

Use https://github.com/SUSE/cpuset to easily try out cpusets (refer https://github.com/SUSE/cpuset/blob/master/INSTALL for building and installing from source)

e.g. 

Cores 1,2, and 3 in user group

cset shield -c 1-3

Move the specified process and all its threads to user group

cset shield --shield --pid=<pid> --threads


CPU affinity

taskset(1)

e.g.

Set CPU affinity of all threads of the specified process to cores 0 and 1

taskset -a -pc 0,1 <pid>


Scheduling policies, priorities

See sched(7) for scheduling details.

Linux scheduler makes decisions based on scheduling policy and priority of threads.

Use chrt(1) to manipulate policy/priority of a process.

e.g.

Set all threads of the specified process to have policy SCHED_RR and priority 30

chrt -a -p -r 30 <pid>


e.g.

Show minimum and maximum valid priorities

$ chrt -m SCHED_OTHER min/max priority : 0/0 SCHED_FIFO min/max priority : 1/99 SCHED_RR min/max priority : 1/99 SCHED_BATCH min/max priority : 0/0 SCHED_IDLE min/max priority : 0/0 SCHED_DEADLINE min/max priority : 0/0


Change a thread's policy to fifo and priority to max

$ chrt -p -f 99 4296


Verify it's set properly

$ chrt -p 4296

pid 4296's current scheduling policy: SCHED_FIFO

pid 4296's current scheduling priority: 99


Naming pthreads

Not related to command line, but very useful for debugging/monitoring purposes, for example

top -H -p <pid>

will show us the thread name.

You can use

pthread_setname_np(3) or

prctl(2) with option PR_SET_NAME: this will set the name of the calling thread