autocore: optimize the performance of obtaining CPU usage

Preformance Test (on NanoPi R2s, repeat 1000 times):

old command

cpu_usage=$(expr 100 - $(cat /tmp/top_tmp | grep 'CPU:' | awk -F '%' '{print$4}' | awk -F ' ' '{print$2}'))

```
real    0m 14.25s
user    0m 7.96s
sys     0m 20.33s
```

new command

cpu_usage=$(cat /tmp/top_tmp | awk '/^CPU/ { printf("%d%%", 100 - $8) }')

```
real    0m 6.91s
user    0m 4.29s
sys     0m 6.06s
```

Signed-off-by: Chuck <fanck0605@qq.com>
[adjusted for our own tree]
Signed-off-by: CN_SZTL <cnsztl@project-openwrt.eu.org>
This commit is contained in:
Chuck 2021-02-18 20:08:26 +08:00 committed by CN_SZTL
parent 1e53f0d4f3
commit 3d194b80c0
No known key found for this signature in database
GPG Key ID: 6850B6345C862176
2 changed files with 2 additions and 2 deletions

View File

@ -49,7 +49,7 @@
local user_info = luci.sys.exec("cat /proc/net/arp | grep 'br-lan' | grep '0x2' | wc -l")
local cpu_usage = (luci.sys.exec("expr 100 - $(top -n 1 | grep 'CPU:' | awk -F '%' '{print$4}' | awk -F ' ' '{print$2}')") or "6") .. "%"
local cpu_usage = luci.sys.exec("top -n1 | awk '/^CPU/ {printf(\"%d%%\", 100 - $8)}'") or "6%"
local cpu_info = luci.sys.exec("/sbin/cpuinfo") or "ARM Processor"
local rv = {

View File

@ -52,7 +52,7 @@
local user_info = luci.sys.exec("cat /proc/net/arp | grep '0x2' | wc -l") or 0
local cpu_usage = (luci.sys.exec("expr 100 - $(top -n 1 | grep 'CPU:' | awk -F '%' '{print$4}' | awk -F ' ' '{print$2}')") or "6") .. "%"
local cpu_usage = luci.sys.exec("top -n1 | awk '/^CPU/ {printf(\"%d%%\", 100 - $8)}'") or "6%"
local rv = {
cpuusage = cpu_usage,