Finding CPU usage from top command
to append the entire output to a csv file. However, I’d like the Cpu(%us) field alone to be entered in file1. I’ve gotten as far as this:
top -b -d 1|grep Cpu > file1.csv
it doesn’t deem to work as I’m not even able to view my file. My intention is to plot a LiveGraph for all the CPU values (stored in a csv file) dynamically. Thanks!
Update- I’m able to top -b -d 1 > file1.csv (store it in a file) cat file1.csv | grep Cpu | cut -c 35-39 | nl > cpu.csv But the date doesn’t get updated dynamically as I need to stop top command to execute the next grep.
Why isn’t this not working(Piping both commands to make cpu.csv get updated dynamically.I’m unable to even open the cpu.csv file)- top -b -d 1 > file1.csv | grep Cpu | cut -c 35-39 | nl > cpu.csv Can someone pls help me?
4 Answers 4
If you ONLY want the CPU Usage in general you might try this:
top -b -d1 -n1|grep -i «Cpu(s)»|head -c21|cut -d ‘ ‘ -f3|cut -d ‘%’ -f1 > file1.csv
This will give you only the CPU value and update the one in the file. If you want to APPEND the data to the file (since I see you are naming it a csv file) then instead of one > use two, like >> file1.csv .
For each thread and CPU usage:
First you need to eliminate the rest of the columns so it is easier to get the CPU data.
- Run top and press f.
- In this menu you select which columns you want to see and which you do not. For your case leave only the CPU column and name (if you want the name)
- Press ESC to go back to the main Top menu and save with Capital W the change. Now you command is easier to parse.
Now for general CPU usage you normally have other tools like ps , iostat an doing a cat /proc/stat . For each you need a different parse like the one used for top. I only posted here for TOP since you explicitly mention it in the title.
NOTE: Should be noted that top is not the most efficient way to see the CPU usage or to work with when parsing values for it. For some, using f2 in the cut command shows the value, for others the f3
Get Overall CPU Usage on Linux
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
1. Introduction
Insufficient system resources such as storage, memory, and CPU (Central Processing Unit) can greatly affect an application’s performance. Hence, monitoring these components is crucial.
Unlike disk and memory, monitoring the CPU usage on a Linux system isn’t as straightforward. In this article, we’ll look at how to interpret CPU metrics and display them in a human-readable format.
2. CPU Load vs. CPU Usage
Even though CPU load and CPU usage sound similar, they are not interchangeable. CPU load is defined as the number of processes using or waiting to use one core at a single point in time.
Let’s say we have a single-core system, and our CPU load average is always below 0.6. This indicates that every process that needs to use the CPU can use it instantly without waiting. If the CPU load average is above 1, this indicates that there are processes that need to use the CPU but cannot at the moment due to the unavailability of the CPU.
However, the load average above 1 in a multi-processor system won’t be an issue since more cores are readily available.
The uptime command gives us a view of the load average at 1, 5, and 15-minute intervals:
[[email protected] ~]# uptime 12:40:05 up 2:29, 1 user, load average: 0.37, 0.08, 0.03
Interpreting load average can’t be done without knowing the number of cores of a system:
[[email protected] ~]# cat /proc/cpuinfo |grep core core id : 0 cpu cores : 1
On the other hand, CPU usage is the percentage of time a CPU takes to process non-idle tasks. CPU Usage can only be measured over a specified interval of time. We can determine the CPU usage by taking the percentage of time spent idling and subtracting it from 100.
3. Calculating CPU Usage
3.1. Getting CPU Usage Using vmstat
The vmstat command displays CPU activity in near-real time:
[[email protected] ~]# vmstat 3 4 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 4 0 0 1347080 6120 941464 0 0 68 11 72 137 1 2 97 0 0 1 0 0 1347080 6120 941464 0 0 0 0 84 157 1 2 97 0 0 1 0 0 1347080 6120 941464 0 0 0 0 59 107 1 1 98 0 0 1 0 0 1347080 6120 941464 0 0 0 1 59 104 1 1 98 0 0
The columns under CPU provide an overview of where the processor time is spent:
- us – time spent running non-kernel code
- sy – time spent running kernel code
- id – time spent idle
- wa – time spent waiting for I/O
- st – time is stolen from a virtual machine
The id column is what we’re interested in. With the delay of a second, we calculate the CPU usage using vmstat:
[[email protected] ~]# echo "CPU Usage: "$[100-$(vmstat 1 2|tail -1|awk '')]"%" CPU Usage: 2%
The vmstat command without any arguments provided will give CPU times since boot. This will not provide an accurate CPU usage percentage. Hence, the arguments can only be 1 and 2 where we take the metrics calculated after a second:
3.2. Getting CPU Usage Using /proc/stat
CPU activity can also be extracted from the /proc/stat file. The file contains various metrics about the system since boot:
[[email protected] ~]# cat /proc/stat cpu 3020 28 1863 22404 35 432 47 0 0 0 cpu0 3020 28 1863 22404 35 432 47 0 0 0 intr 96468 28 100 0 0 0 0 0 0 1 0 0 0 1263 0 0 0 3696 0 153 928 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 207 0 41 14600 0 0 0 0 0 0 0 0 0 0 0 0 0 0 343 97 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 ctxt 340950 btime 1628404433 processes 3276 procs_running 2 procs_blocked 0 softirq 112867 1 16857 56 269 510 0 261 0 0 94913
The first line, ‘cpu’ is an aggregate of the metrics of all cores of the system. On a system with 4 cores, there would be 4 cpu lines — cpu0, cpu1, cpu2, and cpu3. The columns in the ‘cpu‘ row represent times spent processing different tasks:
- user – time spent in user mode
- nice – time spent processing nice processes in user mode
- system – time spent executing kernel code
- idle – time spent idle
- iowait – time spent waiting for I/O
- irq – time spent servicing interrupts
- softirq – time spent servicing software interrupts
- steal – time stolen from a virtual machine
- guest – time spent running a virtual CPU for a guest operating system
- guest_nice – time spent running a virtual CPU for a “niced” guest operating system
We’re going to use these metrics to calculate the average idle percentage. Subsequently, we’ll use the calculated value to work out the CPU usage. It’s important to note that older distributions of Linux don’t calculate the steal, guest, or guest_nice metrics. If we were using an older system, we’d omit those metrics in our calculation:
Average idle time (%) = (idle * 100) / (user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice)
cat /proc/stat |grep cpu |tail -1|awk ''|awk '' CPU Usage: 2.4219
Since we’re working on a single-core system, the ‘cpu’ line will be the same as ‘cpu1‘. Hence, the use of tail -1 is to retrieve only one of the lines. Yet, we’d use the ‘cpu‘ line on a multiprocessor system since it’s an aggregate of metrics on all cores.
3.3. Getting CPU Usage Using top
Generally, the top command is usually utilized to display active processes on a system and how much resources the processes are consuming. Nevertheless, we can use this command to measure the state of the CPU:
[[email protected] ~]# top top - 07:08:31 up 2:41, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 322 total, 2 running, 320 sleeping, 0 stopped, 0 zombie %Cpu(s): 10.0 us, 15.0 sy, 0.0 ni, 97.8 id, 0.0 wa, 5.0 hi, 0.0 si, 0.0 st MiB Mem : 3709.4 total, 1483.1 free, 1402.0 used, 824.4 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2053.4 avail Mem
Furthermore, it’s important to note that the top command displays the CPU percentage of a single core. In a multiprocessor system, it’s possible to have the CPU percentage exceed 100%. For example, if 4 cores are at 75%, the top command will show CPU as being 300%.
We need to obtain the value of the time spent idling so that we can subtract it from 100 to get the usage:
[[email protected] ~]# top -bn2 | grep '%Cpu' | tail -1 | grep -P '(. |. ) id,'|awk '' CPU Usage: 2.2%
The -n option is the number of iterations the top command should use before ending. We avoid using the first loop because the metrics we retrieve will be values since boot. Hence, we’ve taken the second iteration.
Alternatively, in a multiprocessor system, we’d have to divide the given ‘id’ value by the number of cores and subtract the value from 100. For example, if we were operating on a quad-core system and the ‘id‘ value was 304%, we’d calculate our CPU usage as:
CPU Usage % = 100 – (304/4)
[[email protected] ~]# top -bn2 | grep '%Cpu' | tail -1 | grep -P '(. |. ) id,'|awk ''
4. Conclusion
In this article, we discussed the difference between CPU usage and CPU load. Many use these two concepts interchangeably, which is incorrect. After that, we delved into the various methods used to retrieve CPU utilization metrics.
We’ve also pointed out what to avoid for better accuracy.