]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
os: Fix CPU time calculation
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 11:43:03 +0000 (11:43 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Oct 2023 11:43:03 +0000 (11:43 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/os.c

index c1b8807bc172e5f91d580d11cada87fb22009b77..f3142aabfaf9c06d88d8260c7dd5aecf737dbe17 100644 (file)
@@ -179,19 +179,20 @@ static int pakfire_parse_cpustat(char* line, size_t length, void* data) {
                        return -EINVAL;
 
                // Fetch how many ticks a second
-               long int ticks = sysconf(_SC_CLK_TCK);
+               unsigned long int ticks = stat.user + stat.nice + stat.system + stat.idle
+                       + stat.iowait + stat.irq + stat.softirq + stat.steal + stat.guest + stat.guest_nice;
 
                // Convert to relative terms
-               cpustat->user       = stat.user       / ticks;
-               cpustat->nice       = stat.nice       / ticks;
-               cpustat->system     = stat.system     / ticks;
-               cpustat->idle       = stat.idle       / ticks;
-               cpustat->iowait     = stat.iowait     / ticks;
-               cpustat->irq        = stat.irq        / ticks;
-               cpustat->softirq    = stat.softirq    / ticks;
-               cpustat->steal      = stat.steal      / ticks;
-               cpustat->guest      = stat.guest      / ticks;
-               cpustat->guest_nice = stat.guest_nice / ticks;
+               cpustat->user       = (double)stat.user       / ticks;
+               cpustat->nice       = (double)stat.nice       / ticks;
+               cpustat->system     = (double)stat.system     / ticks;
+               cpustat->idle       = (double)stat.idle       / ticks;
+               cpustat->iowait     = (double)stat.iowait     / ticks;
+               cpustat->irq        = (double)stat.irq        / ticks;
+               cpustat->softirq    = (double)stat.softirq    / ticks;
+               cpustat->steal      = (double)stat.steal      / ticks;
+               cpustat->guest      = (double)stat.guest      / ticks;
+               cpustat->guest_nice = (double)stat.guest_nice / ticks;
        }
 
        return 0;