From: Sebastien GODARD Date: Wed, 6 Mar 2019 17:55:52 +0000 (+0100) Subject: Fix #214: mpstat: JSON output should display "all" for global CPU use X-Git-Tag: v12.1.4~55 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f209356d88fdba056861f9be9217185e63c7b72c;p=sysstat Fix #214: mpstat: JSON output should display "all" for global CPU use Displaying "-1" for CPU number in mpstat's JSON output format is not consistent with what mpstat uses for other options. So display "all" instead. Signed-off-by: Sebastien GODARD --- diff --git a/mpstat.c b/mpstat.c index 9e2a393..ae915a6 100644 --- a/mpstat.c +++ b/mpstat.c @@ -652,6 +652,7 @@ void write_json_cpu_stats(int tab, unsigned long long deltot_jiffies, int prev, unsigned char offline_cpu_bitmap[]) { int i, next = FALSE; + char cpu_name[16]; struct stats_cpu *scc, *scp; xprintf(tab++, "\"cpu-load\": ["); @@ -675,7 +676,15 @@ void write_json_cpu_stats(int tab, unsigned long long deltot_jiffies, int prev, } next = TRUE; - if (i != 0) { + if (i == 0) { + /* This is CPU "all" */ + strcpy(cpu_name, "all"); + + } + else { + snprintf(cpu_name, 16, "%d", i - 1); + cpu_name[15] = '\0'; + /* Recalculate itv for current proc */ deltot_jiffies = get_per_cpu_interval(scc, scp); @@ -694,9 +703,9 @@ void write_json_cpu_stats(int tab, unsigned long long deltot_jiffies, int prev, } } - xprintf0(tab, "{\"cpu\": \"%d\", \"usr\": %.2f, \"nice\": %.2f, \"sys\": %.2f, " + xprintf0(tab, "{\"cpu\": \"%s\", \"usr\": %.2f, \"nice\": %.2f, \"sys\": %.2f, " "\"iowait\": %.2f, \"irq\": %.2f, \"soft\": %.2f, \"steal\": %.2f, " - "\"guest\": %.2f, \"gnice\": %.2f, \"idle\": %.2f}", i - 1, + "\"guest\": %.2f, \"gnice\": %.2f, \"idle\": %.2f}", cpu_name, (scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ? 0.0 : ll_sp_value(scp->cpu_user - scp->cpu_guest,