fs/procfs: fix the issue of /proc/cpuload in SMP

Signed-off-by: ligd <liguiding1@xiaomi.com>
This commit is contained in:
fangxiang 2022-06-14 13:49:18 +08:00 committed by GUIDINGLI
parent 7e7d4ab744
commit 2c8c35431d

View File

@ -201,7 +201,8 @@ static ssize_t cpuload_read(FAR struct file *filep, FAR char *buffer,
if (filep->f_pos == 0) if (filep->f_pos == 0)
{ {
struct cpuload_s cpuload; uint32_t total = 0;
uint32_t active = 0;
uint32_t intpart; uint32_t intpart;
uint32_t fracpart; uint32_t fracpart;
@ -209,18 +210,39 @@ static ssize_t cpuload_read(FAR struct file *filep, FAR char *buffer,
* fail if the PID is not valid. This, however, should never happen * fail if the PID is not valid. This, however, should never happen
* for the IDLE thread. * for the IDLE thread.
*/ */
#ifdef CONFIG_SMP
struct cpuload_s cpuloads[CONFIG_SMP_NCPUS];
uint32_t i;
for (i = 0; i < CONFIG_SMP_NCPUS; i++)
{
DEBUGVERIFY(clock_cpuload(i, &cpuloads[i]));
active += cpuloads[i].active;
}
total = cpuloads[0].total;
#else
struct cpuload_s cpuload;
DEBUGVERIFY(clock_cpuload(0, &cpuload)); DEBUGVERIFY(clock_cpuload(0, &cpuload));
active = cpuload.active;
total = cpuload.total;
#endif
if (active > total)
{
active = total;
}
/* On the simulator, you may hit cpuload.total == 0, but probably never /* On the simulator, you may hit cpuload.total == 0, but probably never
* on real hardware. * on real hardware.
*/ */
if (cpuload.total > 0) if (total > 0)
{ {
uint32_t tmp; uint32_t tmp;
tmp = 1000 - (1000 * cpuload.active) / cpuload.total; tmp = 1000 - (1000 * active) / total;
intpart = tmp / 10; intpart = tmp / 10;
fracpart = tmp - 10 * intpart; fracpart = tmp - 10 * intpart;
} }