Skip to contentSkip to conversation

perf stat: Fix JSON output formatting in iostat_prefix()

PATCH In review Suchit Karunakaran 2 participants 3 messages Jul 13, 2025 – Jul 22, 2025 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 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. Note, I didn't test it since my system doesn't support it.

---
 tools/perf/arch/x86/util/iostat.c | 40 +++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 12 deletions(-)

Changes since v1:
- Fix issues in formatting

tools/perf/arch/x86/util/iostat.c +28 -12
index 7442a2cd87ed..d7ae19fb83e5 100644
@@ -403,18 +403,34 @@ 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 (config->json_output) {
+			if (ts) {
+				double timestamp = ts->tv_sec + (double)ts->tv_nsec / 1e9;
+				sprintf(prefix, "{\"interval\": %.6f, \"device\": \"%04x:%02x\", ",
+					timestamp, rp->domain, rp->bus);
+			} else {
+				sprintf(prefix, "{\"device\": \"%04x:%02x\", ",
+					rp->domain, rp->bus);
+			}
+		} else if (config->csv_output) {
+			if (ts) {
+				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, "%04x:%02x%s",
+					rp->domain, rp->bus, config->csv_sep);
+			}
+		} else {
+			if (ts) {
+				sprintf(prefix, "%6lu.%09lu %04x:%02x ",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					rp->domain, rp->bus);
+			} else {
+				sprintf(prefix, "%04x:%02x ",
+					rp->domain, rp->bus);
+			}
+		}
 	}
 }
 
Signature · 1 line
2.50.1
maintainer
#1 lore raw

Hello,

Quoted 5 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. Note, I didn't test it since my system doesn't support it.

Can any Intel folks verify this with combination of various options like -x/-j, -I and --metric-only?

Thanks,
Namhyung

Quoted 63 lines
---
 tools/perf/arch/x86/util/iostat.c | 40 +++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 12 deletions(-)

Changes since v1:
- Fix issues in formatting

tools/perf/arch/x86/util/iostat.c +28 -12
index 7442a2cd87ed..d7ae19fb83e5 100644
@@ -403,18 +403,34 @@ 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 (config->json_output) {
+			if (ts) {
+				double timestamp = ts->tv_sec + (double)ts->tv_nsec / 1e9;
+				sprintf(prefix, "{\"interval\": %.6f, \"device\": \"%04x:%02x\", ",
+					timestamp, rp->domain, rp->bus);
+			} else {
+				sprintf(prefix, "{\"device\": \"%04x:%02x\", ",
+					rp->domain, rp->bus);
+			}
+		} else if (config->csv_output) {
+			if (ts) {
+				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, "%04x:%02x%s",
+					rp->domain, rp->bus, config->csv_sep);
+			}
+		} else {
+			if (ts) {
+				sprintf(prefix, "%6lu.%09lu %04x:%02x ",
+					(unsigned long)ts->tv_sec, ts->tv_nsec,
+					rp->domain, rp->bus);
+			} else {
+				sprintf(prefix, "%04x:%02x ",
+					rp->domain, rp->bus);
+			}
+		}
 	}
 }
 
Signature · 1 line
2.50.1
author
#2 lore raw
Quoted 10 lines · Namhyung Kim

Hello,

Quoted 5 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. Note, I didn't test it since my system doesn't support it.

Quoted 6 lines

Can any Intel folks verify this with combination of various options like -x/-j, -I and --metric-only?

Thanks,
Namhyung

Just following up on this patch to see if anyone had a chance to test it. Thanks