From 766d03f6f3eeaedc9bcd5c9553d17baebeb6d253 Mon Sep 17 00:00:00 2001 From: Chuck Date: Thu, 18 Feb 2021 20:08:26 +0800 Subject: [PATCH] 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 [adjusted for our own tree] Signed-off-by: CN_SZTL --- package/lean/autocore/files/arm/rpcd_luci | 2 +- package/lean/autocore/files/x86/rpcd_luci | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package/lean/autocore/files/arm/rpcd_luci b/package/lean/autocore/files/arm/rpcd_luci index 1bbcfa8302..5ac633730d 100755 --- a/package/lean/autocore/files/arm/rpcd_luci +++ b/package/lean/autocore/files/arm/rpcd_luci @@ -644,7 +644,7 @@ local methods = { local sys = require "luci.sys" local cpuusage = {} - cpuusage.cpuusage = (sys.exec("expr 100 - $(top -n 1 | grep 'CPU:' | awk -F '%' '{print$4}' | awk -F ' ' '{print$2}')") or "2.33") .. "%" + cpuusage.cpuusage = sys.exec("top -n1 | awk '/^CPU/ {printf(\"%d%%\", 100 - $8)}'") or "6%" return cpuusage end }, diff --git a/package/lean/autocore/files/x86/rpcd_luci b/package/lean/autocore/files/x86/rpcd_luci index a906a62379..6a344ae69b 100755 --- a/package/lean/autocore/files/x86/rpcd_luci +++ b/package/lean/autocore/files/x86/rpcd_luci @@ -644,7 +644,7 @@ local methods = { local sys = require "luci.sys" local cpuusage = {} - cpuusage.cpuusage = (sys.exec("expr 100 - $(top -n 1 | grep 'CPU:' | awk -F '%' '{print$4}' | awk -F ' ' '{print$2}')") or "2.33") .. "%" + cpuusage.cpuusage = sys.exec("top -n1 | awk '/^CPU/ {printf(\"%d%%\", 100 - $8)}'") or "6%" return cpuusage end },