perf data: Add JSON export
PATCH 1/1 perf data: Add JSON export
From ddcfd620e7cad4100d0076090c4b39dba8aeead3 Mon Sep 17 00:00:00 2001
From: Nicholas Fraser <[email protected]>
Date: Wed, 31 Mar 2021 06:10:00 -0400
Subject: [PATCH] perf data: Add JSON export
This adds a feature to export perf data to JSON. It uses a minimal inline JSON encoding, no external dependencies. Currently it only outputs some headers and sample metadata but it's easily extensible.
Use it like this:
perf data convert --to-json out.json
--- tools/perf/Documentation/perf-data.txt | 5 +- tools/perf/builtin-data.c | 26 ++- tools/perf/util/Build | 1 + tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/data-convert-bt.h | 11 - tools/perf/util/data-convert-json.c | 310 +++++++++++++++++++++++++ tools/perf/util/data-convert.h | 10 + 7 files changed, 344 insertions(+), 21 deletions(-) delete mode 100644 tools/perf/util/data-convert-bt.h create mode 100644 tools/perf/util/data-convert-json.c
tools/perf/Documentation/perf-data.txt +4 -1
@@ -17,7 +17,7 @@ Data file related processing. COMMANDS -------- convert:: - Converts perf data file into another format (only CTF [1] format is support by now). + Converts perf data file into another format. It's possible to set data-convert debug variable to get debug messages from conversion, like: perf --debug data-convert data convert ...
@@ -27,6 +27,9 @@ OPTIONS for 'convert' --to-ctf:: Triggers the CTF conversion, specify the path of CTF data directory. +--to-json:: + Triggers JSON conversion. Specify the JSON filename to output. + --tod:: Convert time to wall clock time.
tools/perf/builtin-data.c +18 -8
@@ -7,7 +7,6 @@#include "debug.h" #include <subcmd/parse-options.h> #include "data-convert.h" -#include "data-convert-bt.h" typedef int (*data_cmd_fn_t )(int argc,const char **argv);
@@ -55,7 +54,8 @@ static const char * const data_convert_usage[] = {static int cmd_data_convert (int argc,const char **argv) { -const char *to_ctf =NULL ; +const char *to_json =NULL ; +const char *to_ctf =NULL ;struct perf_data_convert_opts opts = { .force = false, .all = false,
@@ -63,6 +63,7 @@ static int cmd_data_convert(int argc, const char **argv)const struct option options[] = {OPT_INCR ('v' ,"verbose" , &verbose,"be more verbose" ),OPT_STRING ('i' ,"input" , &input_name,"file" ,"input file name" ), +OPT_STRING (0 ,"to-json" , &to_json,NULL ,"Convert to JSON format" ), #ifdef HAVE_LIBBABELTRACE_SUPPORTOPT_STRING (0 ,"to-ctf" , &to_ctf,NULL ,"Convert to CTF format" ), OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
@@ -72,11 +73,6 @@ static int cmd_data_convert(int argc, const char **argv) OPT_END() }; -#ifndef HAVE_LIBBABELTRACE_SUPPORT -pr_err ("No conversion support compiled in. perf should be compiled with environment variables LIBBABELTRACE=1 and LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n" ); -return -1 ; -#endif - argc =parse_options (argc, argv, options, data_convert_usage,0 );if (argc) {
@@ -84,11 +80,25 @@ static int cmd_data_convert(int argc, const char **argv)return -1 ; } +if (to_json && to_ctf) { +pr_err ("You cannot specify both --to-ctf and --to-json.\n" ); +return -1 ; + } +if (!to_json && !to_ctf) { +pr_err ("You must specify one of --to-ctf or --to-json.\n" ); +return -1 ; + } + +if (to_json) +return bt_convert__perf2json (input_name, to_json, &opts); +if (to_ctf) {#ifdef HAVE_LIBBABELTRACE_SUPPORT return bt_convert__perf2ctf (input_name, to_ctf, &opts);#else -pr_err ("The libbabeltrace support is not compiled in.\n" ); +pr_err ("The libbabeltrace support is not compiled in. perf should be " +"compiled with environment variables LIBBABELTRACE=1 and " +"LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n" );return -1 ;#endif }
tools/perf/util/Build +1 -0
@@ -163,6 +163,7 @@ perf-$(CONFIG_LIBUNWIND_X86) += libunwind/x86_32.o perf-$(CONFIG_LIBUNWIND_AARCH64) += libunwind/arm64.o perf-$(CONFIG_LIBBABELTRACE) += data-convert-bt.o +perf-y += data-convert-json.o perf-y += scripting-engines/
tools/perf/util/data-convert-bt.c +1 -1
@@ -21,7 +21,7 @@#include <babeltrace/ctf/events.h> #include <traceevent/event-parse.h> #include "asm/bug.h" -#include "data-convert-bt.h" +#include "data-convert.h" #include "session.h" #include "debug.h" #include "tool.h"
tools/perf/util/data-convert-bt.h deleted +0 -11
@@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __DATA_CONVERT_BT_H -#define __DATA_CONVERT_BT_H -#include "data-convert.h" -#ifdef HAVE_LIBBABELTRACE_SUPPORT - -int bt_convert__perf2ctf (const char *input_name,const char *to_ctf, -struct perf_data_convert_opts *opts); - -#endif /* HAVE_LIBBABELTRACE_SUPPORT */ -#endif /* __DATA_CONVERT_BT_H */
tools/perf/util/data-convert-json.c new file +310 -0
310 lines not on this page · raw message
tools/perf/util/data-convert.h +10 -0
@@ -2,10 +2,20 @@#ifndef __DATA_CONVERT_H#define __DATA_CONVERT_H +#include <stdbool.h> +struct perf_data_convert_opts {bool force;bool all;bool tod; }; +#ifdef HAVE_LIBBABELTRACE_SUPPORT +int bt_convert__perf2ctf (const char *input_name,const char *to_ctf, +struct perf_data_convert_opts *opts); +#endif /* HAVE_LIBBABELTRACE_SUPPORT */ + +int bt_convert__perf2json (const char *input_name,const char *to_ctf, +struct perf_data_convert_opts *opts); +#endif /* __DATA_CONVERT_H */
Signature · 1 line
2.31.0
Quoted 4 lines · Nicholas Fraser
From
ddcfd620e7cad4100d0076090c4b39dba8aeead3Mon Sep 17 00:00:00 2001
From: Nicholas Fraser <[email protected]>
Date: Wed, 31 Mar 2021 06:10:00 -0400
Subject: [PATCH] perf data: Add JSON export
no need to add headers again in here
Quoted 8 lines
This adds a feature to export perf data to JSON. It uses a minimal inline JSON encoding, no external dependencies. Currently it only outputs some headers and sample metadata but it's easily extensible.
Use it like this:
perf data convert --to-json out.json
please add similar output summary message we have for CTF conversion:
[ perf data convert: Converted 'perf.data' into CTF data 'data' ] [ perf data convert: Converted and wrote 0.000 MB (10 samples) ]
also I will not push hard for test, becase we don't have any for CTF ;-) but if you could think of any, that'd be great
Quoted 16 lines
tools/perf/util/data-convert-json.c new file +16
-0+ +static void output_headers (struct perf_session *session,struct convert_json *c) +{ +struct stat st; +struct perf_header *header = &session->header; +int ret; +int fd =perf_data__fd (session->data); +int i; +bool first; + +fprintf (c->out,"\n\t\t\t\"header-version\": %u" , header->version); + + ret =fstat (fd, &st); +if (ret >=0 ) { +time_t stctime = st.st_mtime; +char buf[256 ];
tools/perf/util/data-convert-json.c line 191
Quoted 10 lines
tools/perf/util/data-convert-json.c new file +10
-0+ +strftime (buf,sizeof (buf),"%FT%TZ" ,gmtime (&stctime)); +fprintf (c->out,",\n\t\t\t\"captured-on\": \"%s\"" , buf); + }else { +pr_debug ("Failed to get mtime of source file, not writing \"captured-on\"" ); + } + +fprintf (c->out,",\n\t\t\t\"data-offset\": %" PRIu64, header->data_offset); +fprintf (c->out,",\n\t\t\t\"data-size\": %" PRIu64, header->data_size); +fprintf (c->out,",\n\t\t\t\"feat-offset\": %" PRIu64, header->feat_offset);
I was wondering how to make this \t mess more readable, how about you define function like output_json:
output_json(FILE, level, field, format, ...);
and use it:
output_json(c->out, 3, "data-offset", "PRIu64", header->data_offset); output_json(c->out, 3, "data-size", "PRIu64", header->data_size); output_json(c->out, 3, "feat-offset", PRIu64, header->feat_offset);
similar way as we do for pr_debug -> eprintf
SNIP
Quoted 10 lines
tools/perf/util/data-convert-json.c new file +10
-0+ + fd =open (output_name,O_CREAT |O_WRONLY | (opts->force ?0 :O_EXCL ),0666 ); +if (fd ==-1 ) { +if (errno ==EEXIST ) +pr_err ("Output file exists. Use --force to overwrite it.\n" ); +else +pr_err ("Error opening output file!\n" ); +return -1 ; + } +
tools/perf/util/data-convert-json.c line 285
Quoted 10 lines
tools/perf/util/data-convert-json.c new file +10
-0+ c.out =fdopen (fd,"w" ); +if (!c.out) { +fprintf (stderr,"Error opening output file!\n" ); +return -1 ; + } + + session =perf_session__new (&data, false, &c.tool); +if (IS_ERR (session)) { +fprintf (stderr,"Error creating perf session!\n" ); +return -1 ;
here we should close c.out and call perf_session__delete, we normaly do goto to the end of the function in this case
Quoted 12 lines
tools/perf/util/data-convert-json.c new file +12
-0+ } + +if (symbol__init (&session->header.env) <0 ) { +fprintf (stderr,"Symbol init error!\n" ); +return -1 ; + } + +// Version number for future-proofing. Most additions should be able to be +// done in a backwards-compatible way so this should only need to be bumped +// if some major breaking change must be made. +fprintf (c.out,"{\n\t\"linux-perf-json-version\": 1," ); +
tools/perf/util/data-convert-json.c line 307
Quoted 10 lines
tools/perf/util/data-convert-json.c new file +10
-0+// Output headers +fprintf (c.out,"\n\t\"headers\": {" ); +output_headers (session, &c); +fprintf (c.out,"\n\t}," ); + +// Output samples +fprintf (c.out,"\n\t\"samples\": [" ); +perf_session__process_events (session); +fprintf (c.out,"\n\t]\n}\n" ); +
you need to close c.out
Quoted 18 lines
tools/perf/util/data-convert-json.c new file +3
-0+perf_session__delete (session); +return 0 ; +}tools/perf/util/data-convert.h +2
-0@@ -2,10 +2,20 @@#ifndef __DATA_CONVERT_H#define __DATA_CONVERT_H +#include <stdbool.h> +struct perf_data_convert_opts {bool force;bool all;bool tod; };
tools/perf/util/data-convert.h line 21
Quoted 10 lines
tools/perf/util/data-convert.h +8
-0+#ifdef HAVE_LIBBABELTRACE_SUPPORT +int bt_convert__perf2ctf (const char *input_name,const char *to_ctf, +struct perf_data_convert_opts *opts); +#endif /* HAVE_LIBBABELTRACE_SUPPORT */ + +int bt_convert__perf2json (const char *input_name,const char *to_ctf, +struct perf_data_convert_opts *opts); +#endif /* __DATA_CONVERT_H */
great, thanks for this
jirka
Hi Jiri,
Thanks again for the review. I've fixed the issues you mentioned. Some notes below:
Quoted 2 lines · Jiri Olsa
I recall you did not add support for walltime clock, don't you need it to sync with other events?
Not necessarily. As long as the perf recording and the GPU trace are made from the same clock, the events should line up; the real time doesn't matter. I've added it anyway though since it's good to have. It now outputs "clockid", "clock-time" and "real-time" in the headers if clock info was included in the recording.
Quoted 2 lines · Jiri Olsa
also I will not push hard for test, becase we don't have any for CTF ;-) but if you could think of any, that'd be great
I'm not sure what kind of tests would be useful. We could include a small perf.data file and its JSON output and verify that they match. This is probably pointless because almost any change to the JSON code would be expected to change the output so we'd just be resetting the test on each change.
Quoted 5 lines
I was wondering how to make this \t mess more readable, how about you define function like
output_json:output_json(FILE, level, field, format, ...);
Quoted 7 lines
and use it:
output_json(c->out, 3, "data-offset", "PRIu64", header->data_offset); output_json(c->out, 3, "data-size", "PRIu64", header->data_size); output_json(c->out, 3, "feat-offset", PRIu64, header->feat_offset);similar way as we do for
pr_debug-> eprintf
I've cleaned up the output with some additional helper functions. This is essentially turning into its own mini JSON encoder which I was hoping to avoid but I suppose it's necessary to make the code maintainable.
I'll be sending a new patch shortly.
Nick
PATCH 1/1 perf data: Add JSON export
In reply to Jiri OlsaThis adds a feature to export perf data to JSON. It uses a minimal inline JSON encoding, no external dependencies. Currently it only outputs some headers and sample metadata but it's easily extensible.
Use it like this:
perf data convert --to-json out.json
--- tools/perf/Documentation/perf-data.txt | 5 +- tools/perf/builtin-data.c | 26 +- tools/perf/util/Build | 1 + tools/perf/util/data-convert-bt.c | 2 +- tools/perf/util/data-convert-bt.h | 11 - tools/perf/util/data-convert-json.c | 384 +++++++++++++++++++++++++ tools/perf/util/data-convert.h | 10 + 7 files changed, 418 insertions(+), 21 deletions(-) delete mode 100644 tools/perf/util/data-convert-bt.h create mode 100644 tools/perf/util/data-convert-json.c
tools/perf/Documentation/perf-data.txt +4 -1
@@ -17,7 +17,7 @@ Data file related processing. COMMANDS -------- convert:: - Converts perf data file into another format (only CTF [1] format is support by now). + Converts perf data file into another format. It's possible to set data-convert debug variable to get debug messages from conversion, like: perf --debug data-convert data convert ...
@@ -27,6 +27,9 @@ OPTIONS for 'convert' --to-ctf:: Triggers the CTF conversion, specify the path of CTF data directory. +--to-json:: + Triggers JSON conversion. Specify the JSON filename to output. + --tod:: Convert time to wall clock time.
tools/perf/builtin-data.c +18 -8
@@ -7,7 +7,6 @@#include "debug.h" #include <subcmd/parse-options.h> #include "data-convert.h" -#include "data-convert-bt.h" typedef int (*data_cmd_fn_t )(int argc,const char **argv);
@@ -55,7 +54,8 @@ static const char * const data_convert_usage[] = {static int cmd_data_convert (int argc,const char **argv) { -const char *to_ctf =NULL ; +const char *to_json =NULL ; +const char *to_ctf =NULL ;struct perf_data_convert_opts opts = { .force = false, .all = false,
@@ -63,6 +63,7 @@ static int cmd_data_convert(int argc, const char **argv)const struct option options[] = {OPT_INCR ('v' ,"verbose" , &verbose,"be more verbose" ),OPT_STRING ('i' ,"input" , &input_name,"file" ,"input file name" ), +OPT_STRING (0 ,"to-json" , &to_json,NULL ,"Convert to JSON format" ), #ifdef HAVE_LIBBABELTRACE_SUPPORTOPT_STRING (0 ,"to-ctf" , &to_ctf,NULL ,"Convert to CTF format" ), OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"),
@@ -72,11 +73,6 @@ static int cmd_data_convert(int argc, const char **argv) OPT_END() }; -#ifndef HAVE_LIBBABELTRACE_SUPPORT -pr_err ("No conversion support compiled in. perf should be compiled with environment variables LIBBABELTRACE=1 and LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n" ); -return -1 ; -#endif - argc =parse_options (argc, argv, options, data_convert_usage,0 );if (argc) {
@@ -84,11 +80,25 @@ static int cmd_data_convert(int argc, const char **argv)return -1 ; } +if (to_json && to_ctf) { +pr_err ("You cannot specify both --to-ctf and --to-json.\n" ); +return -1 ; + } +if (!to_json && !to_ctf) { +pr_err ("You must specify one of --to-ctf or --to-json.\n" ); +return -1 ; + } + +if (to_json) +return bt_convert__perf2json (input_name, to_json, &opts); +if (to_ctf) {#ifdef HAVE_LIBBABELTRACE_SUPPORT return bt_convert__perf2ctf (input_name, to_ctf, &opts);#else -pr_err ("The libbabeltrace support is not compiled in.\n" ); +pr_err ("The libbabeltrace support is not compiled in. perf should be " +"compiled with environment variables LIBBABELTRACE=1 and " +"LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n" );return -1 ;#endif }
tools/perf/util/Build +1 -0
@@ -163,6 +163,7 @@ perf-$(CONFIG_LIBUNWIND_X86) += libunwind/x86_32.o perf-$(CONFIG_LIBUNWIND_AARCH64) += libunwind/arm64.o perf-$(CONFIG_LIBBABELTRACE) += data-convert-bt.o +perf-y += data-convert-json.o perf-y += scripting-engines/
tools/perf/util/data-convert-bt.c +1 -1
@@ -21,7 +21,7 @@#include <babeltrace/ctf/events.h> #include <traceevent/event-parse.h> #include "asm/bug.h" -#include "data-convert-bt.h" +#include "data-convert.h" #include "session.h" #include "debug.h" #include "tool.h"
tools/perf/util/data-convert-bt.h deleted +0 -11
@@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __DATA_CONVERT_BT_H -#define __DATA_CONVERT_BT_H -#include "data-convert.h" -#ifdef HAVE_LIBBABELTRACE_SUPPORT - -int bt_convert__perf2ctf (const char *input_name,const char *to_ctf, -struct perf_data_convert_opts *opts); - -#endif /* HAVE_LIBBABELTRACE_SUPPORT */ -#endif /* __DATA_CONVERT_BT_H */
tools/perf/util/data-convert-json.c new file +384 -0
384 lines not on this page · raw message
tools/perf/util/data-convert.h +10 -0
@@ -2,10 +2,20 @@#ifndef __DATA_CONVERT_H#define __DATA_CONVERT_H +#include <stdbool.h> +struct perf_data_convert_opts {bool force;bool all;bool tod; }; +#ifdef HAVE_LIBBABELTRACE_SUPPORT +int bt_convert__perf2ctf (const char *input_name,const char *to_ctf, +struct perf_data_convert_opts *opts); +#endif /* HAVE_LIBBABELTRACE_SUPPORT */ + +int bt_convert__perf2json (const char *input_name,const char *to_ctf, +struct perf_data_convert_opts *opts); +#endif /* __DATA_CONVERT_H */
Signature · 1 line
2.31.1