All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf: use C LC_NUMERIC locale for json output in perf stat
@ 2024-11-18 18:41 David Alvarez
  2024-11-18 21:40 ` Ian Rogers
  0 siblings, 1 reply; 3+ messages in thread
From: David Alvarez @ 2024-11-18 18:41 UTC (permalink / raw)
  To: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, Liang Kan
  Cc: David Alvarez, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Namhyung Kim, linux-perf-users

When using perf stat, the tool will use the system locale to format
decimal values. This is incorrect for json output, since the user locale
may lead to incorrect json formatting (which requires a dot as decimal
separator). The following example is perf's output under a ca_ES.UTF-8
locale:

{"interval" : 1.005100134, "counter-value" : "0,486755", "unit" : "Joules", "event" : "power/energy-pkg/", "event-runtime" : 100240331, "pcnt-running" : 100,00, "metric-value" : "0,000000", "metric-unit" : "(null)"}

Solve this issue by setting the LC_NUMERIC locale to "C" when enabling
json output (after parsing arguments).

Signed-off-by: David Alvarez <david.alvarez@bsc.es>
---
 tools/perf/builtin-stat.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 689a3d43c258..b9ebb06b254d 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -2840,6 +2840,15 @@ int cmd_stat(int argc, const char **argv)
 	if (evlist__alloc_stats(&stat_config, evsel_list, interval))
 		goto out;
 
+	/*
+	 * For JSON output, we cannot use the user's numeric
+	 * locale since decimal separators must be dots.
+	 * Set the locale to "C" instead now that we've already parsed
+	 * all of the arguments using the user's locale.
+	 */
+	if (stat_config.json_output)
+		setlocale(LC_NUMERIC, "C");
+
 	/*
 	 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
 	 * while avoiding that older tools show confusing messages.
-- 
2.47.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-11-19  0:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-18 18:41 [PATCH] perf: use C LC_NUMERIC locale for json output in perf stat David Alvarez
2024-11-18 21:40 ` Ian Rogers
2024-11-19  0:36   ` Namhyung Kim

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.