先看一段代码
-
local json = require "luci.json"
-
-
function get_sys_info()
-
local function execute_sys_call(cmd)
-
local f = io.popen(cmd)
-
local ret = f:read("*all")
-
f:close()
-
return ret
-
end
-
-
local function sleep(n)
-
local socket = require("socket")
-
socket.select(nil, nil, n)
-
end
-
-- 计算内存占用率
-
local totalram = execute_sys_call("cat /proc/meminfo | grep MemTotal | awk '{print $2}' | tr -d '\n'")
-
local freeram = execute_sys_call("cat /proc/meminfo | grep MemFree | awk '{print $2}' | tr -d '\n'")
-
local mem_total = string.format("%.0f", totalram / 1024)
-
local mem_utilization = string.format("%.2f", 100 * (totalram - freeram ) / totalram )
-
-- 计算cpu占用率和核心数
-
local cpu_info_1 = execute_sys_call("cat /proc/stat | grep 'cpu ' | awk '{print $2\" \"$3\" \"$4\" \"$5\" \"$6\" \"$7\" \"$8}' | tr -d '\n'")
-
local cpu_idle_1 = tonumber(execute_sys_call("echo "..cpu_info_1.." | awk '{print $4}' | tr -d '\n'"))
-
local cpu_total_1 = tonumber(execute_sys_call("echo "..cpu_info_1.." | awk '{print $1+$2+$3+$4+$5+$6+$7}' | tr -d '\n'"))
-
sleep(0.5)
-
local cpu_info_2 = execute_sys_call("cat /proc/stat | grep 'cpu ' | awk '{print $2\" \"$3\" \"$4\" \"$5\" \"$6\" \"$7\" \"$8}' | tr -d '\n'")
-
local cpu_idle_2 = tonumber(execute_sys_call("echo "..cpu_info_2.." | awk '{print $4}' | tr -d '\n'"))
-
local cpu_total_2 = tonumber(execute_sys_call("echo "..cpu_info_2.." | awk '{print $1+$2+$3+$4+$5+$6+$7}' | tr -d '\n'"))
-
-
local cpu_utilization = string.format("%.2f", 100 * (1 - (cpu_idle_2 - cpu_idle_1) / (cpu_total_2 - cpu_total_1)))
-
local cpu_core = execute_sys_call("cat /proc/stat | grep 'cpu[^ ]' | wc -l | tr -d '\n'")
-
-- 获取cpu和内存频率,判断sram类型
-
local cpu_mem_freq = execute_sys_call("nvram get clkfreq | tr -d '\n'")
-
local cpu_freq = execute_sys_call("echo "..cpu_mem_freq.." | cut -d ',' -f 1 | tr -d '\n'")
-
local mem_freq = execute_sys_call("echo "..cpu_mem_freq.." | cut -d ',' -f 2 | tr -d '\n'")
-
local mem_type
-
local mem_freq_num = tonumber(mem_freq)
-
if (mem_freq_num < 450) then
-
mem_type = "DDR"
-
elseif (mem_freq_num > 450) and (mem_freq_num < 900) then
-
mem_type = "DDR2"
-
else
-
mem_type = "DDR3"
-
end
-
-
local sys_info = {}
-
sys_info.mem_info = {
-
mem_total = mem_total, -- MB
-
mem_utilization = mem_utilization,
-
mem_freq = mem_freq, -- MHz
-
mem_type = mem_type
-
}
-
sys_info.cpu_info = {
-
cpu_utilization = cpu_utilization,
-
cpu_core = cpu_core,
-
cpu_freq = cpu_freq -- MHz
-
}
-
-
return sys_info
-
end
-
-
print(json.encode(get_cpu_mem_info()))
这是在openwrt系统上运行的lua代码,关于判断cpu和内存频率使用的是nvram命令获取的。这里主要讲一讲/proc/meminfo 和 /proc/stat文件对cpu占用率和内存占用率的计算。
1. 计算cpu占用率
可以参考则篇文章,讲述了多种情况cpu使用率的计算方法
http://blog.csdn.net/tenfyguo/article/details/7476306
/proc/stat文件内容如下
-
cpu 122418 0 231542 210673 5053 0 1549 0 0 0
-
cpu0 61260 0 104805 114565 3564 0 1441 0 0 0
-
cpu1 61158 0 126737 96108 1489 0 108 0 0 0
-
intr 299394 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 30215 0 0 0 0 157 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 143569 0 0 0 0 0 114998 0 0 0 0 0 0 0 0 0 10417 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-
ctxt 857059
-
btime 1474333415
-
processes 25190
-
procs_running 3
-
procs_blocked 1
-
softirq 1524730 0 571196 9 46107 10109 0 277960 204748 0 414601
关于每个字段的含义这两篇文章有详细的解释
http://www.cnblogs.com/no7dw/archive/2011/07/04/2097300.html
其中cpu0和cpu1表示有两个逻辑处理器,关于计算处理器数量参考
http://blog.csdn.net/sycflash/article/details/6643492
这里说明一下前三行cpu信息,从第二到第八分别是
-
user (432661) 从系统启动开始累计到当前时刻,用户态的CPU时间(单位:jiffies) ,不包含 nice值为负进程。1jiffies=0.01秒
-
nice (13295) 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间(单位:jiffies)
-
system (86656) 从系统启动开始累计到当前时刻,核心时间(单位:jiffies)
-
idle (422145968) 从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间(单位:jiffies)
-
iowait (171474) 从系统启动开始累计到当前时刻,硬盘IO等待时间(单位:jiffies) ,
-
irq (233) 从系统启动开始累计到当前时刻,硬中断时间(单位:jiffies)
-
softirq (5346) 从系统启动开始累计到当前时刻,软中断时间(单位:jiffies)
你会发现这和top命令上的cpu那一栏是一一对应的,即求cpu占用率的公式为
-
(user + nice + system) / (user + nice + system + idle + iowait + irq + softirq)
但这样求得的cpu占用率是从开机后算起的,计算出来相当稳定,反映不出实时cpu占用率,所以我们可以使用两点求斜率的方法获取cpu实时占用率,公式如下
-
[ (user_1 + nice_1 + system_1) - (user_2 + nice_2 + system_2) ] / [ (user_1 + nice_1 + system_1 + idle_1 + iowait_1 + irq_1 + softirq_1) - (user_2 + nice_2 + system_2 + idle_2 + iowait_2 + irq_2 + softirq_2) ]
2. 计算内存占用率
/proc/meminfo文件内容如下
-
MemTotal: 255984 kB
-
MemFree: 144084 kB
-
Buffers: 6204 kB
-
Cached: 43088 kB
-
SwapCached: 0 kB
-
Active: 24384 kB
-
Inactive: 35192 kB
-
Active(anon): 6036 kB
-
Inactive(anon): 5344 kB
-
Active(file): 18348 kB
-
Inactive(file): 29848 kB
-
Unevictable: 0 kB
-
Mlocked: 0 kB
-
SwapTotal: 0 kB
-
SwapFree: 0 kB
-
Dirty: 0 kB
-
Writeback: 0 kB
-
AnonPages: 10352 kB
-
Mapped: 5808 kB
-
Shmem: 1092 kB
-
Slab: 36052 kB
-
SReclaimable: 12860 kB
-
SUnreclaim: 23192 kB
-
KernelStack: 568 kB
-
PageTables: 584 kB
-
NFS_Unstable: 0 kB
-
Bounce: 0 kB
-
WritebackTmp: 0 kB
-
CommitLimit: 127992 kB
-
Committed_AS: 21200 kB
-
VmallocTotal: 516096 kB
-
VmallocUsed: 18164 kB
-
VmallocChunk: 484908 kB
即及内存占用率为
-
100 * (1 - MemFree / MemTotal)
阅读(1939) | 评论(0) | 转发(0) |