perf tools: get --list-cmds for subcommands
cover perf tools: get --list-cmds for subcommands
Hi Arnaldo et al,
Quoted 15 lines · Arnaldo Carvalho de Melo
Don't do all those things open coded, introduce functions to print, concat, etc.
The best thing tho, since we have all those sub sub commands in things like 'perf kvm', 'perf bench', etc, we could have some
parse_options_subcmd, and make the parse options machinery aware of this, so that it could receive an array of subcmds and when asked for --list-cmds, would print that sublist, etc, i.e. make sub cmds a first class citizen.So I'd suggest that you first introduce functions for doing the concat to pass to the current infrastructure, so that we have what your patch provides, but prettified, then, as follow on patches, you could work on making the options parsing machinery aware of sub cmds.
Quoted 4 lines
Ah, and try not using fixed sized arrays, or at least verify that space is available, i.e. never use strcat, use strncat, better, take a look at
tools/perf/util/strbuf.h, I guess you can use it to build the string for you in a safe way and expanding the buffer as needed, etc.
Okay, so here's a first cut. util.c/ util.h might not have been the best place to put the functions, but I couldn't think of a better place at the moment. The series isn't ideal, but I think it's getting close.
Thoughts?
Ramkumar Ramachandra (5):
perf kvm: introduce --list-cmds for use by scripts perf kmem: introduce --list-cmds
perf lock: introduce --list-cmds
perf mem: introduce --list-cmds
perf sched: introduce --list-cmds
tools/perf/builtin-kmem.c | 15 +++++++++++---- tools/perf/builtin-kvm.c | 16 +++++++++++----- tools/perf/builtin-lock.c | 14 ++++++++++---- tools/perf/builtin-mem.c | 15 ++++++++++----- tools/perf/builtin-sched.c | 15 +++++++++++---- tools/perf/perf-completion.sh | 6 +++--- tools/perf/util/util.c | 24 ++++++++++++++++++++++++ tools/perf/util/util.h | 4 ++++ 8 files changed, 84 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.5.2.227.g53f3478
PATCH 1/5 perf kvm: introduce --list-cmds for use by scripts
Introduce
$ perf kvm --list-cmds
to dump a raw list of commands for use by the completion script. While at it, refactor kvm_usage so that there's only one copy of the command listing.
--- tools/perf/builtin-kvm.c | 39 ++++++++++++++++++++++++++++++++++----- tools/perf/perf-completion.sh | 2 +- 2 files changed, 35 insertions(+), 6 deletions(-)
tools/perf/builtin-kvm.c +34 -5
@@ -1669,9 +1669,33 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)return cmd_buildid_list (i, rec_argv,NULL ); } +static void build_usage_string (const char **usage_str,const char *command, +const char *const *subcommands) +{ +struct strbuf buf =STRBUF_INIT ; + +strbuf_addf (&buf,"perf %s [<options>] {" , command); +for (int i =0 ; subcommands[i]; i++) { +if (i) +strbuf_addstr (&buf,"|" ); +strbuf_addstr (&buf, subcommands[i]); + } +strbuf_addstr (&buf,"}" ); + + usage_str[0 ] =strdup (buf.buf); +strbuf_release (&buf); +} + +static void list_cmds_raw (const char *const *subcommands) +{ +for (int i =0 ; subcommands[i]; i++) +printf ("%s " , subcommands[i]); +} +int cmd_kvm (int argc,const char **argv,const char *prefix__maybe_unused ) {const char *file_name =NULL ; +bool list_cmds = false;const struct option kvm_options[] = {OPT_STRING ('i' ,"input" , &file_name,"file" ,"Input file name" ),
@@ -1692,20 +1716,25 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused) "file", "file saving guest os /proc/modules"),OPT_INCR ('v' ,"verbose" , &verbose,"be more verbose (show counter open errors, etc)" ), +OPT_BOOLEAN (0 ,"list-cmds" , &list_cmds, +"list commands raw for use by scripts" ), OPT_END() }; - -const char *const kvm_usage[] = { -"perf kvm [<options>] {top|record|report|diff|buildid-list|stat}" , -NULL - }; +const char *const subcommands[] = {"top" ,"record" ,"report" ,"diff" , +"buildid-list" ,"stat" ,NULL }; +const char *kvm_usage[] = {NULL ,NULL }; +build_usage_string (kvm_usage,"kvm" , subcommands); perf_host =0 ; perf_guest =1 ; argc =parse_options (argc, argv, kvm_options, kvm_usage,PARSE_OPT_STOP_AT_NON_OPTION ); +if (list_cmds) { +list_cmds_raw (subcommands); +return 0 ; + }if (!argc)usage_with_options (kvm_usage, kvm_options);
tools/perf/perf-completion.sh +1 -1
@@ -123,7 +123,7 @@ __perf_main () __perfcomp_colon "$evts" "$cur" # List subcommands for 'perf kvm' elif [[ $prev == "kvm" ]]; then - subcmds="top record report diff buildid-list stat" + subcmds=$($cmd kvm --list-cmds) __perfcomp_colon "$subcmds" "$cur" # List long option names elif [[ $cur == --* ]]; then
Signature · 1 line
1.8.5.2.227.g53f3478
PATCH 2/5 perf kmem: introduce --list-cmds
In reply to Ramkumar RamachandraExpose build_usage_string() and list_cmds_raw() via util.h, and reuse the functions to implement --list-cmds for 'perf kmem', just like we did for 'perf kvm'. Also use it in perf-completion.sh to aid completions.
--- tools/perf/builtin-kmem.c | 15 +++++++++++---- tools/perf/builtin-kvm.c | 23 ----------------------- tools/perf/perf-completion.sh | 6 +++--- tools/perf/util/util.c | 24 ++++++++++++++++++++++++ tools/perf/util/util.h | 4 ++++ 5 files changed, 42 insertions(+), 30 deletions(-)
tools/perf/builtin-kmem.c +11 -4
@@ -743,6 +743,7 @@ static int __cmd_record(int argc, const char **argv) int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused) { const char * const default_sort_order = "frag,hit,bytes"; + bool list_cmds = false; const struct option kmem_options[] = { OPT_STRING('i', "input", &input_name, "file", "input file name"), OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL,
@@ -754,14 +755,20 @@ int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused) parse_sort_opt),OPT_CALLBACK ('l' ,"line" ,NULL ,"num" ,"show n lines" , parse_line_opt),OPT_BOOLEAN (0 ,"raw-ip" , &raw_ip,"show raw ip instead of symbol" ), +OPT_BOOLEAN (0 ,"list-cmds" , &list_cmds, +"list commands raw for use by scripts" ), OPT_END() }; -const char *const kmem_usage[] = { -"perf kmem [<options>] {record|stat}" , -NULL - }; +const char *const subcommands[] = {"record" ,"stat" ,NULL }; +const char *kmem_usage[] = {NULL ,NULL }; +build_usage_string (kmem_usage,"kmem" , subcommands); + argc =parse_options (argc, argv, kmem_options, kmem_usage,0 ); +if (list_cmds) { +list_cmds_raw (subcommands); +return 0 ; + }if (!argc)usage_with_options (kmem_usage, kmem_options);
tools/perf/builtin-kvm.c +0 -23
@@ -1669,29 +1669,6 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)return cmd_buildid_list (i, rec_argv,NULL ); } -static void build_usage_string (const char **usage_str,const char *command, -const char *const *subcommands) -{ -struct strbuf buf =STRBUF_INIT ; - -strbuf_addf (&buf,"perf %s [<options>] {" , command); -for (int i =0 ; subcommands[i]; i++) { -if (i) -strbuf_addstr (&buf,"|" ); -strbuf_addstr (&buf, subcommands[i]); - } -strbuf_addstr (&buf,"}" ); - - usage_str[0 ] =strdup (buf.buf); -strbuf_release (&buf); -} - -static void list_cmds_raw (const char *const *subcommands) -{ -for (int i =0 ; subcommands[i]; i++) -printf ("%s " , subcommands[i]); -} -int cmd_kvm (int argc,const char **argv,const char *prefix__maybe_unused ) {const char *file_name =NULL ;
tools/perf/perf-completion.sh +3 -3
@@ -121,9 +121,9 @@ __perf_main () elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) __perfcomp_colon "$evts" "$cur" - # List subcommands for 'perf kvm' - elif [[ $prev == "kvm" ]]; then - subcmds=$($cmd kvm --list-cmds) + # List subcommands for kmem, kvm + elif [[ $prev == @(kmem|kvm) ]]; then + subcmds=$($cmd $prev --list-cmds) __perfcomp_colon "$subcmds" "$cur" # List long option names elif [[ $cur == --* ]]; then
tools/perf/util/util.c +24 -0
@@ -11,6 +11,7 @@#include <errno.h> #include <limits.h> #include <byteswap.h> +#include <strbuf.h> #include <linux/kernel.h> /*
@@ -537,3 +538,26 @@ void mem_bswap_64(void *src, int byte_size) ++m; } } + +void build_usage_string (const char **usage_str,const char *command, +const char *const *subcommands) +{ +struct strbuf buf =STRBUF_INIT ; + +strbuf_addf (&buf,"perf %s [<options>] {" , command); +for (int i =0 ; subcommands[i]; i++) { +if (i) +strbuf_addstr (&buf,"|" ); +strbuf_addstr (&buf, subcommands[i]); + } +strbuf_addstr (&buf,"}" ); + + usage_str[0 ] =strdup (buf.buf); +strbuf_release (&buf); +} + +void list_cmds_raw (const char *const *subcommands) +{ +for (int i =0 ; subcommands[i]; i++) +printf ("%s " , subcommands[i]); +}
tools/perf/util/util.h +4 -0
@@ -327,4 +327,8 @@ void mem_bswap_64(void *src, int byte_size);void mem_bswap_32 (void *src,int byte_size);const char *get_filename_for_perf_kvm (void ); + +void build_usage_string (const char **usage_str,const char *command,const char *const *subcommands); +void list_cmds_raw (const char *const *subcommands); +#endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.8.5.2.227.g53f3478
PATCH 3/5 perf lock: introduce --list-cmds
In reply to Ramkumar Ramachandra--- tools/perf/builtin-lock.c | 14 ++++++++++---- tools/perf/perf-completion.sh | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-)
tools/perf/builtin-lock.c +10 -4
@@ -938,6 +938,7 @@ static int __cmd_record(int argc, const char **argv)int cmd_lock (int argc,const char **argv,const char *prefix__maybe_unused ) { +bool list_cmds = false;const struct option info_options[] = {OPT_BOOLEAN ('t' ,"threads" , &info_threads,"dump thread list in perf.data" ),
@@ -949,6 +950,7 @@ int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)OPT_STRING ('i' ,"input" , &input_name,"file" ,"input file name" ),OPT_INCR ('v' ,"verbose" , &verbose,"be more verbose (show symbol address, etc)" ),OPT_BOOLEAN ('D' ,"dump-raw-trace" , &dump_trace,"dump raw trace in ASCII" ), +OPT_BOOLEAN (0 ,"list-cmds" , &list_cmds,"list commands raw for use by scripts" ),OPT_END () }; const struct option report_options[] = {
@@ -961,10 +963,6 @@ int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)"perf lock info [<options>]" ,NULL }; -const char *const lock_usage[] = { -"perf lock [<options>] {record|report|script|info}" , -NULL - };const char *const report_usage[] = {"perf lock report [<options>]" , NULL
@@ -972,12 +970,20 @@ int cmd_lock(int argc, const char **argv, const char *prefix __maybe_unused)unsigned int i;int rc =0 ; +const char *const subcommands[] = {"record" ,"report" ,"script" ,"info" ,NULL }; +const char *lock_usage[] = {NULL ,NULL }; +build_usage_string (lock_usage,"lock" , subcommands); +symbol__init ();for (i =0 ; i <LOCKHASH_SIZE ; i++)INIT_LIST_HEAD (lockhash_table + i); argc =parse_options (argc, argv, lock_options, lock_usage,PARSE_OPT_STOP_AT_NON_OPTION ); +if (list_cmds) { +list_cmds_raw (subcommands); +return 0 ; + }if (!argc)usage_with_options (lock_usage, lock_options);
tools/perf/perf-completion.sh +2 -2
@@ -121,8 +121,8 @@ __perf_main () elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) __perfcomp_colon "$evts" "$cur" - # List subcommands for kmem, kvm - elif [[ $prev == @(kmem|kvm) ]]; then + # List subcommands for kmem, kvm, lock + elif [[ $prev == @(kmem|kvm|lock) ]]; then subcmds=$($cmd $prev --list-cmds) __perfcomp_colon "$subcmds" "$cur" # List long option names
Signature · 1 line
1.8.5.2.227.g53f3478
PATCH 4/5 perf mem: introduce --list-cmds
In reply to Ramkumar Ramachandra--- tools/perf/builtin-mem.c | 15 ++++++++++----- tools/perf/perf-completion.sh | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-)
tools/perf/builtin-mem.c +10 -5
@@ -21,11 +21,6 @@ struct perf_mem {DECLARE_BITMAP (cpu_bitmap,MAX_NR_CPUS ); }; -static const char *const mem_usage[] = { -"perf mem [<options>] {record <command> |report}" , -NULL -}; -static int __cmd_record (int argc,const char **argv) {int rec_argc, i =0 , j;
@@ -191,6 +186,7 @@ static int report_events(int argc, const char **argv, struct perf_mem *mem)int cmd_mem (int argc,const char **argv,const char *prefix__maybe_unused ) {struct stat st; +bool list_cmds = false;struct perf_mem mem = { .tool = { .sample = process_sample_event,
@@ -219,12 +215,21 @@ int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused) "separator", "separator for columns, no spaces will be added" " between columns '.' is reserved."), + OPT_BOOLEAN(0, "list-cmds", &list_cmds, + "list commands raw for use by scripts"), OPT_END() }; +const char *const subcommands[] = {"record" ,"report" ,NULL }; +const char *mem_usage[] = {NULL ,NULL }; +build_usage_string (mem_usage,"mem" , subcommands); argc =parse_options (argc, argv, mem_options, mem_usage,PARSE_OPT_STOP_AT_NON_OPTION ); +if (list_cmds) { +list_cmds_raw (subcommands); +return 0 ; + }if (!argc || !(strncmp (argv[0 ],"rec" ,3 ) || mem_operation))usage_with_options (mem_usage, mem_options);
tools/perf/perf-completion.sh +2 -2
@@ -121,8 +121,8 @@ __perf_main () elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) __perfcomp_colon "$evts" "$cur" - # List subcommands for kmem, kvm, lock - elif [[ $prev == @(kmem|kvm|lock) ]]; then + # List subcommands for kmem, kvm, lock, mem + elif [[ $prev == @(kmem|kvm|lock|mem) ]]; then subcmds=$($cmd $prev --list-cmds) __perfcomp_colon "$subcmds" "$cur" # List long option names
Signature · 1 line
1.8.5.2.227.g53f3478
PATCH 5/5 perf sched: introduce --list-cmds
In reply to Ramkumar Ramachandra--- tools/perf/builtin-sched.c | 15 +++++++++++---- tools/perf/perf-completion.sh | 4 ++-- 2 files changed, 13 insertions(+), 6 deletions(-)
tools/perf/builtin-sched.c +11 -4
@@ -1658,6 +1658,7 @@ static int __cmd_record(int argc, const char **argv)int cmd_sched (int argc,const char **argv,const char *prefix__maybe_unused ) {const char default_sort_order[] ="avg, max, switch, runtime" ; +bool list_cmds = false;struct perf_sched sched = { .tool = { .sample = perf_sched__process_tracepoint_sample,
@@ -1703,6 +1704,8 @@ int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused) "be more verbose (show symbol address, etc)"),OPT_BOOLEAN ('D' ,"dump-raw-trace" , &dump_trace,"dump raw trace in ASCII" ), +OPT_BOOLEAN (0 ,"list-cmds" , &list_cmds, +"list commands raw for use by scripts" ),OPT_END () }; const char * const latency_usage[] = {
@@ -1713,10 +1716,6 @@ int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused)"perf sched replay [<options>]" ,NULL }; -const char *const sched_usage[] = { -"perf sched [<options>] {record|latency|map|replay|script}" , -NULL - };struct trace_sched_handler lat_ops = { .wakeup_event = latency_wakeup_event, .switch_event = latency_switch_event,
@@ -1732,12 +1731,20 @@ int cmd_sched(int argc, const char **argv, const char *prefix __maybe_unused) .fork_event = replay_fork_event, };unsigned int i; +const char *const subcommands [] = {"record" ,"latency" ,"map" , +"replay" ,"script" }; +const char *sched_usage[] = {NULL ,NULL }; +build_usage_string (sched_usage,"sched" , subcommands);for (i =0 ; i <ARRAY_SIZE (sched.curr_pid); i++) sched.curr_pid[i] =-1 ; argc =parse_options (argc, argv, sched_options, sched_usage,PARSE_OPT_STOP_AT_NON_OPTION ); +if (list_cmds) { +list_cmds_raw (subcommands); +return 0 ; + }if (!argc)usage_with_options (sched_usage, sched_options);
tools/perf/perf-completion.sh +2 -2
@@ -121,8 +121,8 @@ __perf_main () elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) __perfcomp_colon "$evts" "$cur" - # List subcommands for kmem, kvm, lock, mem - elif [[ $prev == @(kmem|kvm|lock|mem) ]]; then + # List subcommands for kmem, kvm, lock, mem, sched + elif [[ $prev == @(kmem|kvm|lock|mem|sched) ]]; then subcmds=$($cmd $prev --list-cmds) __perfcomp_colon "$subcmds" "$cur" # List long option names
Signature · 1 line
1.8.5.2.227.g53f3478
Quoted 6 lines · Ramkumar Ramachandra
Ramkumar Ramachandra (5):
perf kvm: introduce --list-cmds for use by scripts perf kmem: introduce --list-cmds
perf lock: introduce --list-cmds
perf mem: introduce --list-cmds
perf sched: introduce --list-cmds
Ping?
Any comment on these, Arnaldo?