How can I monitor the thread count of a process on linux?

I would like to monitor the number of threads used by a specific process on Linux. Is there an easy way to get this information without impacting the performance of the process?


try

ps huH p <PID_OF_U_PROCESS> | wc -l

or htop


To get the number of threads for a given pid:

$ ps -o nlwp <pid>

Where nlwp stands for Number of Light Weight Processes (threads). Thus ps aliases nlwp to thcount, which means that

$ ps -o thcount <pid>

does also work.

If you want to monitor the thread count, simply use watch:

$ watch ps -o thcount <pid>

To get the sum of all threads running in the system:

$ ps -eo nlwp | tail -n +2 | awk '{ num_threads += $1 } END { print num_threads }'

Each thread in a process creates a directory under /proc/<pid>/task. Count the number of directories, and you have the number of threads.


cat /proc/<PROCESS_PID>/status | grep Threads