

Let’s write a Bash script called total_cpu_usage.sh to perform the calculation: #!/bin/bash This way, we can easily calculate the total CPU usage of a process. Finally, we calculate the elapsed time since the process started and divide the sum of utime and stime by the elapsed time. Then, we convert the process’s values from clock ticks to seconds. First, we get the process’s utime, stime, and starttime, and the system’s uptime. We can split the CPU usage calculation into three steps. As the system’s uptime is in seconds, we have to convert the clock ticks to seconds to calculate the process’s CPU usage. Instead, they are in units called clock ticks. However, the process’s utime, stime, and starttime are not in seconds.
#Utime and stime stat how to#
Now, we know how to calculate the total CPU usage. Let’s see the content of /proc/uptime: $ cat /proc/uptime The first value is the system’s uptime in seconds. We can get the system’s uptime by reading the /proc/uptime file.

So, we can calculate the elapsed time by subtracting the process’s starttime from the system’s uptime. The process’s starttime is measured since the system booted. This value is the 22nd column from the /proc//stat file and is called starttime. To get the elapsed time since the process started, we can use the process’s start time. So, the total CPU usage of an application is the sum of utime and stime, divided by the elapsed time. Usually, we represent the CPU usage as a percentage of the elapsed time since the process started.
