Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf mem: Rework command option handling

Split the common option and ones for record or report. Otherwise -U in
the record option cannot be used because it clashes with in the common
(or report) option. Also rename report_events() to __cmd_report() to
follow the convention and to be sync with the record part.

Also set the flag PARSE_OPT_STOP_AT_NON_OPTION for the common option so
that it can show the help message in the subcommand like below:

$ perf mem record -h

Usage: perf mem record [<options>] [<command>]
or: perf mem record [<options>] -- <command> [<options>]

-C, --cpu <cpu> list of cpus to profile
-e, --event <event> event selector. use 'perf mem record -e list' to list available events
-f, --force don't complain, do it
-K, --all-kernel collect only kernel level data
-p, --phys-data Record/Report sample physical addresses
-t, --type <type> memory operations(load,store) Default load,store
-U, --all-user collect only user level data
-v, --verbose be more verbose (show counter open errors, etc)
--data-page-size Record/Report sample data address page size
--ldlat <n> mem-loads latency

Cc: Aditya Gupta <adityag@linux.ibm.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20240731235505.710436-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
35b38a71 3da209bb

+45 -34
+45 -34
tools/perf/builtin-mem.c
··· 34 34 bool force; 35 35 bool phys_addr; 36 36 bool data_page_size; 37 + bool all_kernel; 38 + bool all_user; 37 39 int operation; 38 40 const char *cpu_list; 39 41 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); ··· 64 62 return 0; 65 63 } 66 64 67 - static const char * const __usage[] = { 68 - "perf mem record [<options>] [<command>]", 69 - "perf mem record [<options>] -- <command> [<options>]", 70 - NULL 71 - }; 72 - 73 - static const char * const *record_mem_usage = __usage; 74 - 75 - static int __cmd_record(int argc, const char **argv, struct perf_mem *mem) 65 + static int __cmd_record(int argc, const char **argv, struct perf_mem *mem, 66 + const struct option *options) 76 67 { 77 68 int rec_argc, i = 0, j; 78 69 int start, end; 79 70 const char **rec_argv; 80 71 int ret; 81 - bool all_user = false, all_kernel = false; 82 72 struct perf_mem_event *e; 83 73 struct perf_pmu *pmu; 84 - struct option options[] = { 85 - OPT_CALLBACK('e', "event", &mem, "event", 86 - "event selector. use 'perf mem record -e list' to list available events", 87 - parse_record_events), 88 - OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"), 89 - OPT_INCR('v', "verbose", &verbose, 90 - "be more verbose (show counter open errors, etc)"), 91 - OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"), 92 - OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"), 93 - OPT_END() 74 + const char * const record_usage[] = { 75 + "perf mem record [<options>] [<command>]", 76 + "perf mem record [<options>] -- <command> [<options>]", 77 + NULL 94 78 }; 95 79 96 80 pmu = perf_mem_events_find_pmu(); ··· 90 102 return -1; 91 103 } 92 104 93 - argc = parse_options(argc, argv, options, record_mem_usage, 105 + argc = parse_options(argc, argv, options, record_usage, 94 106 PARSE_OPT_KEEP_UNKNOWN); 95 107 96 108 /* Max number of arguments multiplied by number of PMUs that can support them. */ ··· 146 158 goto out; 147 159 end = i; 148 160 149 - if (all_user) 161 + if (mem->all_user) 150 162 rec_argv[i++] = "--all-user"; 151 163 152 - if (all_kernel) 164 + if (mem->all_kernel) 153 165 rec_argv[i++] = "--all-kernel"; 154 166 155 167 if (mem->cpu_list) { ··· 307 319 perf_session__delete(session); 308 320 return ret; 309 321 } 322 + 310 323 static char *get_sort_order(struct perf_mem *mem) 311 324 { 312 325 bool has_extra_options = (mem->phys_addr | mem->data_page_size) ? true : false; ··· 335 346 return strdup(sort); 336 347 } 337 348 338 - static int report_events(int argc, const char **argv, struct perf_mem *mem) 349 + static int __cmd_report(int argc, const char **argv, struct perf_mem *mem, 350 + const struct option *options) 339 351 { 340 352 const char **rep_argv; 341 353 int ret, i = 0, j, rep_argc; 342 354 char *new_sort_order; 355 + const char * const report_usage[] = { 356 + "perf mem report [<options>]", 357 + NULL 358 + }; 359 + 360 + argc = parse_options(argc, argv, options, report_usage, 361 + PARSE_OPT_KEEP_UNKNOWN); 343 362 344 363 if (mem->dump_raw) 345 364 return report_raw_events(mem); ··· 365 368 if (new_sort_order) 366 369 rep_argv[i++] = new_sort_order; 367 370 368 - for (j = 1; j < argc; j++, i++) 371 + for (j = 0; j < argc; j++, i++) 369 372 rep_argv[i] = argv[j]; 370 373 371 374 ret = cmd_report(i, rep_argv); ··· 472 475 OPT_CALLBACK('t', "type", &mem.operation, 473 476 "type", "memory operations(load,store) Default load,store", 474 477 parse_mem_ops), 478 + OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", 479 + "list of cpus to profile"), 480 + OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"), 481 + OPT_INCR('v', "verbose", &verbose, 482 + "be more verbose (show counter open errors, etc)"), 483 + OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"), 484 + OPT_BOOLEAN(0, "data-page-size", &mem.data_page_size, "Record/Report sample data address page size"), 485 + OPT_END() 486 + }; 487 + const struct option record_options[] = { 488 + OPT_CALLBACK('e', "event", &mem, "event", 489 + "event selector. use 'perf mem record -e list' to list available events", 490 + parse_record_events), 491 + OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"), 492 + OPT_BOOLEAN('U', "all-user", &mem.all_user, "collect only user level data"), 493 + OPT_BOOLEAN('K', "all-kernel", &mem.all_kernel, "collect only kernel level data"), 494 + OPT_PARENT(mem_options) 495 + }; 496 + const struct option report_options[] = { 475 497 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw, 476 498 "dump raw samples in ASCII"), 477 499 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved, 478 500 "Only display entries resolved to a symbol"), 479 501 OPT_STRING('i', "input", &input_name, "file", 480 502 "input file name"), 481 - OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", 482 - "list of cpus to profile"), 483 503 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep, 484 504 "separator", 485 505 "separator for columns, no spaces will be added" 486 506 " between columns '.' is reserved."), 487 - OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"), 488 - OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"), 489 - OPT_BOOLEAN(0, "data-page-size", &mem.data_page_size, "Record/Report sample data address page size"), 490 - OPT_END() 507 + OPT_PARENT(mem_options) 491 508 }; 492 509 const char *const mem_subcommands[] = { "record", "report", NULL }; 493 510 const char *mem_usage[] = { ··· 510 499 }; 511 500 512 501 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands, 513 - mem_usage, PARSE_OPT_KEEP_UNKNOWN); 502 + mem_usage, PARSE_OPT_STOP_AT_NON_OPTION); 514 503 515 504 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation)) 516 505 usage_with_options(mem_usage, mem_options); ··· 523 512 } 524 513 525 514 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) 526 - return __cmd_record(argc, argv, &mem); 515 + return __cmd_record(argc, argv, &mem, record_options); 527 516 else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) 528 - return report_events(argc, argv, &mem); 517 + return __cmd_report(argc, argv, &mem, report_options); 529 518 else 530 519 usage_with_options(mem_usage, mem_options); 531 520