-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_use.lua
141 lines (109 loc) · 2.54 KB
/
cpu_use.lua
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
-- functions related to lookups of cpu usage and system load
-- we can get cpu count from /proc/stat
--[[
function LookupCpus()
local S, str
local cpu_count=0
S=stream.STREAM("/proc/cpuinfo", "r")
if S ~= nil
then
str=S:readln()
while str ~= nil
do
if string.sub(str, 1, 9)=="processor" then cpu_count=cpu_count+1 end
str=S:readln()
end
S:close()
end
display_values["cpu_count"]=cpu_count
end
]]--
function ReadCpuUsageLine(toks)
local total=0
local count=0
local item, val
item=toks:next()
while item ~= nil
do
val=tonumber(item)
if val ~= nil
then
-- add up user/system/kernel etc, INCLUDING IDLE, to give 'total'
total=total + val
-- 3rd item along is 'idle'
if count==3 then idle=val end
count=count+1
end
item=toks:next()
end
return total-idle, total
end
function CpuUsage()
local key, str
local S, toks, item
local used, total
local cpu_count=0
S=stream.STREAM("/proc/stat", "r")
if S ~= nil
then
str=S:readln()
while str ~= nil
do
toks=strutil.TOKENIZER(str, " ")
key=toks:next()
if key=="cpu"
then
key=toks:next()
used,total=ReadCpuUsageLine(toks)
elseif string.match(key, "^cpu[0-9]") ~= nil
then
cpu_count=cpu_count+1
end
str=S:readln()
end
S:close()
display_values["cpu_count"]=cpu_count
if display_values["cpu_last_used"] ~= nil
then
val=(used - tonumber(display_values["cpu_last_used"])) / (total - display_values["cpu_last_total"])
AddDisplayValue("load", val * cpu_count, "%3.1f", nil)
AddDisplayValue("load_percent", val * 100.0, "% 3.1f", usage_color_map)
else
display_values["load"]="---"
display_values["load_percent"]="---"
end
display_values["cpu_last_used"]=used
display_values["cpu_last_total"]=total
else
print("FAIL TO OPEN /proc/stat")
end
end
function CpuFreq()
local Glob, cpuid, path, str
local avg=0
local cpu_count=0
Glob=filesys.GLOB("/sys/devices/system/cpu/cpu[0-9]*")
path=Glob:next();
while path ~= nil
do
cpuid=filesys.basename(path)
str=SysFSReadFile(path.."/cpufreq/scaling_cur_freq")
display_values["cpu_freq:" .. cpuid]=strutil.toMetric(tonumber(str))
avg=avg + tonumber(str)
cpu_count=cpu_count+1
path=Glob:next();
end
if cpu_count > 0
then
display_values["cpu_freq:avg"]=strutil.toMetric(avg / cpu_count)
end
end
function LookupLoad()
local toks, str, val
str=SysFSReadFile("/proc/loadavg")
toks=strutil.TOKENIZER(str, "\\S")
str=toks:next()
display_values["load1min"]=toks:next()
display_values["load5min"]=toks:next()
display_values["load15min"]=toks:next()
end