[PATCH][RFC] tools/power turbostat: Fix ACPI CState format issue

From: Chen Yu
Date: Mon Oct 12 2020 - 06:00:37 EST


Currently if the system boots with BIOS _CST Cstate information
enabled, the turbostat output would have unaligned problems:

C1_ACPI C2_ACPI C3_ACPI POLL% C1_ACPI% C2_ACPI% C3_ACPI% CPU%c1
5 37 138 0.00 0.02 1.30 98.51 0.38 0.00 0.00 99.43

The C1_ACPI% is of 8 bytes, so extend the format accordingly if the field name
equals to/longer than 8 bytes.

After the patch applied:

C1_ACPI C2_ACPI C3_ACPI POLL% C1_ACPI% C2_ACPI% C3_ACPI% CPU%c1
2 42 96 0.00 0.12 2.60 97.09 0.60

Reported-by: Zhang Rui <rui.zhang@xxxxxxxxx>
Signed-off-by: Chen Yu <yu.c.chen@xxxxxxxxx>
---
tools/power/x86/turbostat/turbostat.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 33b370865d16..73aa8738ae36 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -664,14 +664,16 @@ void print_header(char *delim)
outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));

for (mp = sys.tp; mp; mp = mp->next) {
-
+ int name_len = strlen(mp->name);
if (mp->format == FORMAT_RAW) {
if (mp->width == 64)
outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
else
outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
} else {
- if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
+ if (((mp->type == COUNTER_ITEMS) && sums_need_wide_columns) ||
+ /* Deal with corner case: Cx_ACPI% is of 8 bytes. */
+ ((mp->type == COUNTER_USEC) && name_len >= 8))
outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
else
outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
@@ -1005,6 +1007,7 @@ int format_counters(struct thread_data *t, struct core_data *c,

/* Added counters */
for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
+ int name_len = strlen(mp->name);
if (mp->format == FORMAT_RAW) {
if (mp->width == 32)
outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
@@ -1016,9 +1019,13 @@ int format_counters(struct thread_data *t, struct core_data *c,
else
outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
} else if (mp->format == FORMAT_PERCENT) {
- if (mp->type == COUNTER_USEC)
- outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
- else
+ if (mp->type == COUNTER_USEC) {
+ /* Deal with corner case: Cx_ACPI% is of 8 bytes. */
+ if (name_len >= 8)
+ outp += sprintf(outp, "%s%-8.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
+ else
+ outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
+ } else
outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
}
}
--
2.25.1