Skip to contentSkip to conversation

perf stat: Fix JSON output formatting in iostat_prefix()

PATCH RESEND Superseded Suchit Karunakaran 2 participants 4 messages Jun 5, 2025 – Jun 6, 2025 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 perf stat: Fix JSON output formatting in iostat_prefix()

The iostat_prefix() function previously included a TODO noting that its output format was incorrect in JSON mode. This patch corrects that by conditionally formatting the prefix string based on the output mode specified in perf_stat_config.

---
 tools/perf/arch/x86/util/iostat.c | 35 ++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 12 deletions(-)
tools/perf/arch/x86/util/iostat.c +23 -12
index 7442a2cd87ed..1d9c20dab5c7 100644
@@ -403,18 +403,29 @@ void iostat_prefix(struct evlist *evlist,
 	struct iio_root_port *rp = evlist->selected->priv;
 
 	if (rp) {
-		/*
-		 * TODO: This is the incorrect format in JSON mode.
-		 *       See prepare_timestamp()
-		 */
-		if (ts)
-			sprintf(prefix, "%6lu.%09lu%s%04x:%02x%s",
-				ts->tv_sec, ts->tv_nsec,
-				config->csv_sep, rp->domain, rp->bus,
-				config->csv_sep);
-		else
-			sprintf(prefix, "%04x:%02x%s", rp->domain, rp->bus,
-				config->csv_sep);
+		if (ts) {
+			if (config->json_output)
+				sprintf(prefix,
+					"\"interval\" : %lu.%09lu, \"device\" : \"%04x:%02x\"",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					rp->domain, rp->bus);
+			else if (config->csv_output)
+				sprintf(prefix, "%lu.%09lu%s%04x:%02x%s",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					config->csv_sep,
+					rp->domain, rp->bus, config->csv_sep);
+			else
+				sprintf(prefix, "%6lu.%09lu %04x:%02x%s",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					rp->domain, rp->bus, config->csv_sep);
+		} else {
+			if (config->json_output)
+				sprintf(prefix, "\"device\" : \"%04x:%02x\"",
+					rp->domain, rp->bus);
+			else
+				sprintf(prefix, "%04x:%02x%s", rp->domain,
+					rp->bus, config->csv_sep);
+		}
 	}
 }
 
Signature · 1 line
2.49.0
maintainer
#1 lore raw

Hello,

Quoted 4 lines · Suchit Karunakaran

The iostat_prefix() function previously included a TODO noting that its output format was incorrect in JSON mode. This patch corrects that by conditionally formatting the prefix string based on the output mode specified in perf_stat_config.

I've tested this. It doesn't work well.

Before:

  # ./perf iostat -j -I 1000 true
  #          time    port              0.000517525 0000:00 "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:80 
       0.000517525 0000:17 , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:85 
       0.000517525 0000:3a , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:ae 
       0.000517525 0000:5d , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:d7 

After:

  # ./perf iostat -j -I 1000 true
  #          time    port         "interval" : 0.000463559, "device" : "0000:00""Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:80"
  "interval" : 0.000463559, "device" : "0000:17", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:85"
  "interval" : 0.000463559, "device" : "0000:3a", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:ae"
  "interval" : 0.000463559, "device" : "0000:5d", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:d7"

Thanks,
Namhyung

Quoted 55 lines
---
 tools/perf/arch/x86/util/iostat.c | 35 ++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 12 deletions(-)
tools/perf/arch/x86/util/iostat.c +23 -12
index 7442a2cd87ed..1d9c20dab5c7 100644
@@ -403,18 +403,29 @@ void iostat_prefix(struct evlist *evlist,
 	struct iio_root_port *rp = evlist->selected->priv;
 
 	if (rp) {
-		/*
-		 * TODO: This is the incorrect format in JSON mode.
-		 *       See prepare_timestamp()
-		 */
-		if (ts)
-			sprintf(prefix, "%6lu.%09lu%s%04x:%02x%s",
-				ts->tv_sec, ts->tv_nsec,
-				config->csv_sep, rp->domain, rp->bus,
-				config->csv_sep);
-		else
-			sprintf(prefix, "%04x:%02x%s", rp->domain, rp->bus,
-				config->csv_sep);
+		if (ts) {
+			if (config->json_output)
+				sprintf(prefix,
+					"\"interval\" : %lu.%09lu, \"device\" : \"%04x:%02x\"",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					rp->domain, rp->bus);
+			else if (config->csv_output)
+				sprintf(prefix, "%lu.%09lu%s%04x:%02x%s",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					config->csv_sep,
+					rp->domain, rp->bus, config->csv_sep);
+			else
+				sprintf(prefix, "%6lu.%09lu %04x:%02x%s",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					rp->domain, rp->bus, config->csv_sep);
+		} else {
+			if (config->json_output)
+				sprintf(prefix, "\"device\" : \"%04x:%02x\"",
+					rp->domain, rp->bus);
+			else
+				sprintf(prefix, "%04x:%02x%s", rp->domain,
+					rp->bus, config->csv_sep);
+		}
 	}
 }
 
Signature · 1 line
2.49.0
author
#2 lore raw
Quoted 89 lines · Namhyung Kim

Hello,

Quoted 4 lines · Suchit Karunakaran

The iostat_prefix() function previously included a TODO noting that its output format was incorrect in JSON mode. This patch corrects that by conditionally formatting the prefix string based on the output mode specified in perf_stat_config.

I've tested this. It doesn't work well.

Before:

  # ./perf iostat -j -I 1000 true
  #          time    port              0.000517525 0000:00 "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:80
       0.000517525 0000:17 , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:85
       0.000517525 0000:3a , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:ae
       0.000517525 0000:5d , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:d7

After:

  # ./perf iostat -j -I 1000 true
  #          time    port         "interval" : 0.000463559, "device" : "0000:00""Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:80"
  "interval" : 0.000463559, "device" : "0000:17", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:85"
  "interval" : 0.000463559, "device" : "0000:3a", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:ae"
  "interval" : 0.000463559, "device" : "0000:5d", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:d7"

Thanks,
Namhyung

Quoted 52 lines
---
 tools/perf/arch/x86/util/iostat.c | 35 ++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 12 deletions(-)
tools/perf/arch/x86/util/iostat.c +23 -12
index 7442a2cd87ed..1d9c20dab5c7 100644
@@ -403,18 +403,29 @@ void iostat_prefix(struct evlist *evlist,
      struct iio_root_port *rp = evlist->selected->priv;
 
      if (rp) {
-             /*
-              * TODO: This is the incorrect format in JSON mode.
-              *       See prepare_timestamp()
-              */
-             if (ts)
-                     sprintf(prefix, "%6lu.%09lu%s%04x:%02x%s",
-                             ts->tv_sec, ts->tv_nsec,
-                             config->csv_sep, rp->domain, rp->bus,
-                             config->csv_sep);
-             else
-                     sprintf(prefix, "%04x:%02x%s", rp->domain, rp->bus,
-                             config->csv_sep);
+             if (ts) {
+                     if (config->json_output)
+                             sprintf(prefix,
+                                     "\"interval\" : %lu.%09lu, \"device\" : \"%04x:%02x\"",
+                                     (unsigned long)ts->tv_sec, ts->tv_nsec,
+                                     rp->domain, rp->bus);
+                     else if (config->csv_output)
+                             sprintf(prefix, "%lu.%09lu%s%04x:%02x%s",
+                                     (unsigned long)ts->tv_sec, ts->tv_nsec,
+                                     config->csv_sep,
+                                     rp->domain, rp->bus, config->csv_sep);
+                     else
+                             sprintf(prefix, "%6lu.%09lu %04x:%02x%s",
+                                     (unsigned long)ts->tv_sec, ts->tv_nsec,
+                                     rp->domain, rp->bus, config->csv_sep);
+             } else {
+                     if (config->json_output)
+                             sprintf(prefix, "\"device\" : \"%04x:%02x\"",
+                                     rp->domain, rp->bus);
+                     else
+                             sprintf(prefix, "%04x:%02x%s", rp->domain,
+                                     rp->bus, config->csv_sep);
+             }
      }
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.49.0

Hi Namhyung,
Thanks for testing it. Could you please tell what could be improved?

maintainer
#3 lore raw
Quoted 35 lines · Suchit K
Quoted 24 lines · Namhyung Kim

Hello,

Quoted 4 lines · Suchit Karunakaran

The iostat_prefix() function previously included a TODO noting that its output format was incorrect in JSON mode. This patch corrects that by conditionally formatting the prefix string based on the output mode specified in perf_stat_config.

I've tested this. It doesn't work well.

Before:

  # ./perf iostat -j -I 1000 true
  #          time    port              0.000517525 0000:00 "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:80
       0.000517525 0000:17 , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:85
       0.000517525 0000:3a , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:ae
       0.000517525 0000:5d , "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
       0.000517525 0000:d7

After:

Quoted 9 lines
  # ./perf iostat -j -I 1000 true
  #          time    port         "interval" : 0.000463559, "device" : "0000:00""Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:80"
  "interval" : 0.000463559, "device" : "0000:17", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:85"
  "interval" : 0.000463559, "device" : "0000:3a", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:ae"
  "interval" : 0.000463559, "device" : "0000:5d", "Inbound Read(MB)" : "0", "Inbound Write(MB)" : "0", "Outbound Read(MB)" : "0", "Outbound Write(MB)" : "0"
  "interval" : 0.000463559, "device" : "0000:d7"
Quoted 2 lines

Hi Namhyung,
Thanks for testing it. Could you please tell what could be improved?

I think it should show a valid JSON. Something like this?

  # ./perf iostat -j -I 1000 true
  {"interval" : 0.000463559, "device" : "0000:00", "Inbound Read(MB)" : "0", ...}
  {"interval" : 0.000463559, "device" : "0000:80", "Inbound Read(MB)" : "0", ...}
  {"interval" : 0.000463559, "device" : "0000:17", "Inbound Read(MB)" : "0", ...}
  {"interval" : 0.000463559, "device" : "0000:85", "Inbound Read(MB)" : "0", ...}
  ...

Thanks,
Namhyung