overall CPU usage and Memory(RAM) usage in percentage in linux/ubuntu
You can use top
and/or vmstat
from the procps package.
Use vmstat -s
to get the amount of RAM on your machine (optional), and
then use the output of top
to calculate the memory usage percentages.
%Cpu(s): 3.8 us, 2.8 sy, 0.4 ni, 92.0 id, 1.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 24679620 total, 1705524 free, 7735748 used, 15238348 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 16161296 avail Mem
You can also do this for relatively short output:
watch '/usr/bin/top -b | head -4 | tail -2'
A shell pipe that calculates the current RAM usage periodically is
watch -n 5 "/usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf(\"used: %s total: %s => RAM Usage: %.1f%%\", \$F[7], \$F[3], 100*\$F[7]/\$F[3]) if /KiB Mem/'"
(CPU + Swap usages were filtered out here.)
This command prints every 5 seconds:
Every 5.0s: /usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf("u... wb3: Wed Nov 21 13:51:49 2018
used: 8349560 total: 24667856 => RAM Usage: 33.8%
Retrieve CPU usage percentage
A simple awk
could help you here(considering that you want to print only the numbers of sy column).
vmstat 1 10 | awk 'FNR>1{print $(NF-3)}'
NOTE: I have used vmstat 1 10
to perform 10 times vmstat
command on server and then I am printing the $(NF-3)
value which is 4th value from last.
How to get total cpu usage in Linux using C++
cat /proc/stat
http://www.linuxhowtos.org/System/procstat.htm
I agree with this answer above. The cpu line in this file gives the total number of "jiffies" your system has spent doing different types of processing.
What you need to do is take 2 readings of this file, seperated by whatever interval of time you require. The numbers are increasing values (subject to integer rollover) so to get the %cpu you need to calculate how many jiffies have elapsed over your interval, versus how many jiffies were spend doing work.
e.g.
Suppose at 14:00:00 you have
cpu 4698 591 262 8953 916 449 531
total_jiffies_1 = (sum of all values) = 16400
work_jiffies_1 = (sum of user,nice,system = the first 3 values) = 5551
and at 14:00:05 you have
cpu 4739 591 289 9961 936 449 541
total_jiffies_2 = 17506
work_jiffies_2 = 5619
So the %cpu usage over this period is:
work_over_period = work_jiffies_2 - work_jiffies_1 = 68
total_over_period = total_jiffies_2 - total_jiffies_1 = 1106
%cpu = work_over_period / total_over_period * 100 = 6.1%
Hope that helps a bit.
Related Topics
How to Tar a Directory Without Retaining the Directory Structure
How to Exclude a Directory When Using 'Find'
How Does "Cat ≪≪ Eof" Work in Bash
How to Join Multiple Lines of File Names into One With Custom Delimiter
What Is Better "Int 0X80" or "Syscall" in 32-Bit Code on Linux
How to Setup Public-Key Authentication
How to Insert a Text At the Beginning of a File
Is Gettimeofday() Guaranteed to Be of Microsecond Resolution
How to Programmatically "Burn In" Ansi Control Codes to a File Using Unix Utils
Change the X-Frame-Options to Allow All Domains
How to Store a Command in a Variable in a Shell Script
How to Cat ≪≪Eof ≫≫ a File Containing Code
How to Automatically Redirect Http to Https on Apache Servers
What's the Difference Between Nohup and Ampersand