Junk

Junk Commit Details

Date:2017-01-14 10:25:07 (2 years 6 months ago)
Author:Nicola Fontana
Branch:master
Commit:f78a274564f6935424cf03f233498cf1a84f714a
Parents: 26c907d62af563aacf9084dcc4edc54c5a24aa31
Message:cpupercentage: first implementation

A script that gets the CPU usage since the last call to itself. Useful
for providing CPU percentage for panel applets.
Changes:
Amisc/cpupercentage (full)

File differences

misc/cpupercentage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#! /usr/bin/awk -f
#
# A solution to the problem raised here:
# http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux
#
# It evaluates the cpu usage percentage since the last call. This means
# you should use it in a loop for e.g. sampling the cpu usage for a dock
# panel applet.
#
# The only drawback I'm aware of is it needs to create a temporary in a
# specific location to store the old samplings.
BEGIN {
# Read current stats
getline < "/proc/stat"
if ($1 != "cpu") {
print "Invalid /proc/stat" > "/dev/stderr"
exit 1
}
# Read old stats before updating them
r=(getline old_line < "/tmp/cpupercentage-snapshot")
print $0 > "/tmp/cpupercentage-snapshot"
if (r <= 0) {
# First call: by convention just return 0
printf "0"
exit 0
}
# Get the percentage
split(old_line, old)
usage = $2-old[2] + $3-old[3] + $4-old[4]
total = usage + $5-old[5]
if (total == 0) {
print "Sampling too quick" > "/dev/stderr"
exit 1
}
printf "%.0f", usage/total*100
}

Archive Download the corresponding diff file

Branches