From: Namhyung Kim <namhyung@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: David Alvarez <david.alvarez@bsc.es>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
Liang Kan <kan.liang@linux.intel.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
linux-perf-users@vger.kernel.org
Subject: Re: [PATCH] perf: use C LC_NUMERIC locale for json output in perf stat
Date: Mon, 18 Nov 2024 16:36:42 -0800 [thread overview]
Message-ID: <Zzvdmne-nOz9LAS6@google.com> (raw)
In-Reply-To: <CAP-5=fWf86BFpjvkUZ328o267afdsoXzZ3VgKwCF8gJUWntEKg@mail.gmail.com>
On Mon, Nov 18, 2024 at 01:40:34PM -0800, Ian Rogers wrote:
> On Mon, Nov 18, 2024 at 10:45 AM David Alvarez <david.alvarez@bsc.es> wrote:
> >
> > 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>
>
> Thanks for the report but ugh, this is broken everywhere. For example,
> for CSV output the printf format is created here:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/stat-shadow.c#n199
> Before being used:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/stat-display.c#n417
> On BSD there is a locale specific printf with printf_l, but it looks
> to be missing with glibc and no doubt missing on all the obscure libcs
> the maintainers test with. Given this I think this is the right fix
> but wonder, should we just set the locale globally and not for json
> output?
+1.
This might break something but it looks like a proper fix.
Thanks,
Namhyung
>
> We could also change tests like:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/tests/shell/stat+json_output.sh
> to test both with the default and locales like ca_ES.UTF-8 (export
> LC_NUMERIC=...). We could test other forms of output like CSV
> similarly:
> https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/tests/shell/stat+csv_output.sh
>
> Thanks,
> Ian
>
> > ---
> > 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
> >
prev parent reply other threads:[~2024-11-19 0:36 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Zzvdmne-nOz9LAS6@google.com \
--to=namhyung@kernel.org \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=david.alvarez@bsc.es \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.