Skip to content
Advertisement

Is it possible to get current CPU utilisation from a specific core via /sys in Linux?

I would like to write a shellscript that reads the current CPU utilisation on a per-core basis. Is it possible to read this from the /sys directory in Linux (CentOS 8)? I have found /sys/bus/cpu/drivers/processor/cpu0 which does give me a fair bit of information (like current frequency), but I’ve yet to figure out how to read CPU utilisation.

In other words: Is there a file that gives me current utilisation of a specific CPU core in Linux, specifically CentOS 8?

Advertisement

Answer

I believe that you should be able to extract information from /proc/stat – the lines that start with cpu$N, where $N is 0, 1, 2, …… For example:

Strongly suggesting reading articles referenced on other answer.

JavaScript

Repeated call will show larger values:

JavaScript

Notice CPU0 5th column (idle count) moving from 80508446 to 80523961

Format of each line in

cpuN user-time nice-time system-time idle-time io-wait ireq softirq steal guest guest_nice

So a basic solution:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement