Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 2 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <string.h> |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 5 | #include "builtin.h" |
| 6 | #include "perf.h" |
| 7 | #include "debug.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 8 | #include <subcmd/parse-options.h> |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame] | 9 | #include "data-convert.h" |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 10 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 11 | typedef int (*data_cmd_fn_t)(int argc, const char **argv); |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 12 | |
| 13 | struct data_cmd { |
| 14 | const char *name; |
| 15 | const char *summary; |
| 16 | data_cmd_fn_t fn; |
| 17 | }; |
| 18 | |
| 19 | static struct data_cmd data_cmds[]; |
| 20 | |
| 21 | #define for_each_cmd(cmd) \ |
| 22 | for (cmd = data_cmds; cmd && cmd->name; cmd++) |
| 23 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 24 | static const char * const data_subcommands[] = { "convert", NULL }; |
| 25 | |
| 26 | static const char *data_usage[] = { |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 27 | "perf data convert [<options>]", |
| 28 | NULL |
| 29 | }; |
| 30 | |
Joshua Martinez | 760f5e7 | 2021-08-24 13:58:29 -0700 | [diff] [blame] | 31 | const char *to_json; |
| 32 | const char *to_ctf; |
| 33 | struct perf_data_convert_opts opts = { |
| 34 | .force = false, |
| 35 | .all = false, |
| 36 | }; |
| 37 | |
| 38 | const struct option data_options[] = { |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 39 | OPT_INCR('v', "verbose", &verbose, "be more verbose"), |
| 40 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
Nicholas Fraser | d0713d4 | 2021-04-26 10:47:16 -0400 | [diff] [blame] | 41 | OPT_STRING(0, "to-json", &to_json, NULL, "Convert to JSON format"), |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 42 | #ifdef HAVE_LIBBABELTRACE_SUPPORT |
| 43 | OPT_STRING(0, "to-ctf", &to_ctf, NULL, "Convert to CTF format"), |
Jiri Olsa | 88371c5 | 2020-08-05 11:34:42 +0200 | [diff] [blame] | 44 | OPT_BOOLEAN(0, "tod", &opts.tod, "Convert time to wall clock time"), |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 45 | #endif |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame] | 46 | OPT_BOOLEAN('f', "force", &opts.force, "don't complain, do it"), |
Wang Nan | 9e1a7ea | 2016-06-24 11:22:11 +0000 | [diff] [blame] | 47 | OPT_BOOLEAN(0, "all", &opts.all, "Convert all events"), |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 48 | OPT_END() |
| 49 | }; |
| 50 | |
Joshua Martinez | 760f5e7 | 2021-08-24 13:58:29 -0700 | [diff] [blame] | 51 | static int cmd_data_convert(int argc, const char **argv) |
| 52 | { |
| 53 | |
| 54 | argc = parse_options(argc, argv, data_options, |
| 55 | data_usage, 0); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 56 | if (argc) { |
Joshua Martinez | 760f5e7 | 2021-08-24 13:58:29 -0700 | [diff] [blame] | 57 | usage_with_options(data_usage, data_options); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 58 | return -1; |
| 59 | } |
| 60 | |
Nicholas Fraser | d0713d4 | 2021-04-26 10:47:16 -0400 | [diff] [blame] | 61 | if (to_json && to_ctf) { |
| 62 | pr_err("You cannot specify both --to-ctf and --to-json.\n"); |
| 63 | return -1; |
| 64 | } |
| 65 | if (!to_json && !to_ctf) { |
| 66 | pr_err("You must specify one of --to-ctf or --to-json.\n"); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | if (to_json) |
| 71 | return bt_convert__perf2json(input_name, to_json, &opts); |
| 72 | |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 73 | if (to_ctf) { |
| 74 | #ifdef HAVE_LIBBABELTRACE_SUPPORT |
Wang Nan | 3275f68 | 2016-06-24 11:22:07 +0000 | [diff] [blame] | 75 | return bt_convert__perf2ctf(input_name, to_ctf, &opts); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 76 | #else |
Nicholas Fraser | d0713d4 | 2021-04-26 10:47:16 -0400 | [diff] [blame] | 77 | pr_err("The libbabeltrace support is not compiled in. perf should be " |
| 78 | "compiled with environment variables LIBBABELTRACE=1 and " |
| 79 | "LIBBABELTRACE_DIR=/path/to/libbabeltrace/\n"); |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 80 | return -1; |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 87 | static struct data_cmd data_cmds[] = { |
Jiri Olsa | edbe981 | 2015-02-20 23:17:00 +0100 | [diff] [blame] | 88 | { "convert", "converts data file between formats", cmd_data_convert }, |
Yunlong Song | 1f924c2 | 2015-02-27 19:53:46 +0800 | [diff] [blame] | 89 | { .name = NULL, }, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 92 | int cmd_data(int argc, const char **argv) |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 93 | { |
| 94 | struct data_cmd *cmd; |
| 95 | const char *cmdstr; |
| 96 | |
Yunlong Song | 01b7160 | 2015-03-18 21:35:52 +0800 | [diff] [blame] | 97 | argc = parse_options_subcommand(argc, argv, data_options, data_subcommands, data_usage, |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 98 | PARSE_OPT_STOP_AT_NON_OPTION); |
Joshua Martinez | 760f5e7 | 2021-08-24 13:58:29 -0700 | [diff] [blame] | 99 | |
| 100 | if (!argc) { |
| 101 | usage_with_options(data_usage, data_options); |
| 102 | return -1; |
| 103 | } |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 104 | |
| 105 | cmdstr = argv[0]; |
| 106 | |
| 107 | for_each_cmd(cmd) { |
| 108 | if (strcmp(cmd->name, cmdstr)) |
| 109 | continue; |
| 110 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 111 | return cmd->fn(argc, argv); |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | pr_err("Unknown command: %s\n", cmdstr); |
Joshua Martinez | 760f5e7 | 2021-08-24 13:58:29 -0700 | [diff] [blame] | 115 | usage_with_options(data_usage, data_options); |
Jiri Olsa | 2245bf1 | 2015-02-20 23:16:59 +0100 | [diff] [blame] | 116 | return -1; |
| 117 | } |