diff options
| author | ruki <[email protected]> | 2022-07-29 00:52:33 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-07-29 00:52:33 +0800 |
| commit | f588982a9b0d5bfedd23531e9e72724a2149a610 (patch) | |
| tree | 9c3acc0bfc1aaa579dbcf03cece01f808e0ef3a3 | |
| parent | 50af6707efc8f4831d790005cdddad4278f3a37d (diff) | |
cache cpuinfo
| -rw-r--r-- | tests/modules/os/cpuinfo.lua | 2 | ||||
| -rw-r--r-- | tests/modules/os/meminfo.lua | 2 | ||||
| -rw-r--r-- | xmake/core/base/cpu.lua | 20 |
3 files changed, 20 insertions, 4 deletions
diff --git a/tests/modules/os/cpuinfo.lua b/tests/modules/os/cpuinfo.lua index 1f0180739..dfb04350c 100644 --- a/tests/modules/os/cpuinfo.lua +++ b/tests/modules/os/cpuinfo.lua @@ -1,7 +1,7 @@ function main() print(os.cpuinfo()) while true do - print(os.time(), os.cpuinfo("usagerate")) + print("total: %d%%", math.floor(os.cpuinfo("usagerate") * 100)) os.sleep(1000) end end diff --git a/tests/modules/os/meminfo.lua b/tests/modules/os/meminfo.lua index 68a245cda..e42d4350f 100644 --- a/tests/modules/os/meminfo.lua +++ b/tests/modules/os/meminfo.lua @@ -1,6 +1,6 @@ function main() while true do - print(os.time(), os.meminfo()) + print("%d%%", math.floor(os.meminfo("usagerate") * 100)) os.sleep(1000) end end diff --git a/xmake/core/base/cpu.lua b/xmake/core/base/cpu.lua index 35bb97332..aff0ee952 100644 --- a/xmake/core/base/cpu.lua +++ b/xmake/core/base/cpu.lua @@ -259,6 +259,22 @@ function cpu._info() return cpuinfo end +-- get cpu stats info +function cpu._statinfo(name) + local stats = cpu._STATS + local stime = cpu._STIME + if stats == nil or stime == nil or os.time() - stime > 1 then -- cache 1s + stats = os._cpuinfo() + cpu._STATS = stats + cpu._STIME = os.time() + end + if name then + return stats[name] + else + return stats + end +end + -- get vendor id function cpu.vendor() return cpu._info().vendor_id @@ -316,12 +332,12 @@ end -- get cpu number function cpu.number() - return os._cpuinfo().ncpu + return cpu._statinfo("ncpu") end -- get cpu usage rate function cpu.usagerate() - return os._cpuinfo().usagerate + return cpu._statinfo("usagerate") end -- get cpu info |
