builtin-mem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include "builtin.h"
  7. #include "perf.h"
  8. #include <subcmd/parse-options.h>
  9. #include "util/trace-event.h"
  10. #include "util/tool.h"
  11. #include "util/session.h"
  12. #include "util/data.h"
  13. #include "util/mem-events.h"
  14. #include "util/debug.h"
  15. #include "util/symbol.h"
  16. #define MEM_OPERATION_LOAD 0x1
  17. #define MEM_OPERATION_STORE 0x2
  18. struct perf_mem {
  19. struct perf_tool tool;
  20. char const *input_name;
  21. bool hide_unresolved;
  22. bool dump_raw;
  23. bool force;
  24. bool phys_addr;
  25. int operation;
  26. const char *cpu_list;
  27. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  28. };
  29. static int parse_record_events(const struct option *opt,
  30. const char *str, int unset __maybe_unused)
  31. {
  32. struct perf_mem *mem = *(struct perf_mem **)opt->value;
  33. int j;
  34. if (strcmp(str, "list")) {
  35. if (!perf_mem_events__parse(str)) {
  36. mem->operation = 0;
  37. return 0;
  38. }
  39. exit(-1);
  40. }
  41. for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
  42. struct perf_mem_event *e = &perf_mem_events[j];
  43. fprintf(stderr, "%-13s%-*s%s\n",
  44. e->tag,
  45. verbose > 0 ? 25 : 0,
  46. verbose > 0 ? perf_mem_events__name(j) : "",
  47. e->supported ? ": available" : "");
  48. }
  49. exit(0);
  50. }
  51. static const char * const __usage[] = {
  52. "perf mem record [<options>] [<command>]",
  53. "perf mem record [<options>] -- <command> [<options>]",
  54. NULL
  55. };
  56. static const char * const *record_mem_usage = __usage;
  57. static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
  58. {
  59. int rec_argc, i = 0, j;
  60. const char **rec_argv;
  61. int ret;
  62. bool all_user = false, all_kernel = false;
  63. struct option options[] = {
  64. OPT_CALLBACK('e', "event", &mem, "event",
  65. "event selector. use 'perf mem record -e list' to list available events",
  66. parse_record_events),
  67. OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
  68. OPT_INCR('v', "verbose", &verbose,
  69. "be more verbose (show counter open errors, etc)"),
  70. OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"),
  71. OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"),
  72. OPT_END()
  73. };
  74. argc = parse_options(argc, argv, options, record_mem_usage,
  75. PARSE_OPT_KEEP_UNKNOWN);
  76. rec_argc = argc + 9; /* max number of arguments */
  77. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  78. if (!rec_argv)
  79. return -1;
  80. rec_argv[i++] = "record";
  81. if (mem->operation & MEM_OPERATION_LOAD)
  82. perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
  83. if (mem->operation & MEM_OPERATION_STORE)
  84. perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
  85. if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
  86. rec_argv[i++] = "-W";
  87. rec_argv[i++] = "-d";
  88. if (mem->phys_addr)
  89. rec_argv[i++] = "--phys-data";
  90. for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
  91. if (!perf_mem_events[j].record)
  92. continue;
  93. if (!perf_mem_events[j].supported) {
  94. pr_err("failed: event '%s' not supported\n",
  95. perf_mem_events__name(j));
  96. free(rec_argv);
  97. return -1;
  98. }
  99. rec_argv[i++] = "-e";
  100. rec_argv[i++] = perf_mem_events__name(j);
  101. };
  102. if (all_user)
  103. rec_argv[i++] = "--all-user";
  104. if (all_kernel)
  105. rec_argv[i++] = "--all-kernel";
  106. for (j = 0; j < argc; j++, i++)
  107. rec_argv[i] = argv[j];
  108. if (verbose > 0) {
  109. pr_debug("calling: record ");
  110. while (rec_argv[j]) {
  111. pr_debug("%s ", rec_argv[j]);
  112. j++;
  113. }
  114. pr_debug("\n");
  115. }
  116. ret = cmd_record(i, rec_argv);
  117. free(rec_argv);
  118. return ret;
  119. }
  120. static int
  121. dump_raw_samples(struct perf_tool *tool,
  122. union perf_event *event,
  123. struct perf_sample *sample,
  124. struct machine *machine)
  125. {
  126. struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
  127. struct addr_location al;
  128. const char *fmt;
  129. if (machine__resolve(machine, &al, sample) < 0) {
  130. fprintf(stderr, "problem processing %d event, skipping it.\n",
  131. event->header.type);
  132. return -1;
  133. }
  134. if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
  135. goto out_put;
  136. if (al.map != NULL)
  137. al.map->dso->hit = 1;
  138. if (mem->phys_addr) {
  139. if (symbol_conf.field_sep) {
  140. fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s0x%016"PRIx64
  141. "%s%"PRIu64"%s0x%"PRIx64"%s%s:%s\n";
  142. } else {
  143. fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
  144. "%s0x%016"PRIx64"%s%5"PRIu64"%s0x%06"PRIx64
  145. "%s%s:%s\n";
  146. symbol_conf.field_sep = " ";
  147. }
  148. printf(fmt,
  149. sample->pid,
  150. symbol_conf.field_sep,
  151. sample->tid,
  152. symbol_conf.field_sep,
  153. sample->ip,
  154. symbol_conf.field_sep,
  155. sample->addr,
  156. symbol_conf.field_sep,
  157. sample->phys_addr,
  158. symbol_conf.field_sep,
  159. sample->weight,
  160. symbol_conf.field_sep,
  161. sample->data_src,
  162. symbol_conf.field_sep,
  163. al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
  164. al.sym ? al.sym->name : "???");
  165. } else {
  166. if (symbol_conf.field_sep) {
  167. fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
  168. "%s0x%"PRIx64"%s%s:%s\n";
  169. } else {
  170. fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
  171. "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
  172. symbol_conf.field_sep = " ";
  173. }
  174. printf(fmt,
  175. sample->pid,
  176. symbol_conf.field_sep,
  177. sample->tid,
  178. symbol_conf.field_sep,
  179. sample->ip,
  180. symbol_conf.field_sep,
  181. sample->addr,
  182. symbol_conf.field_sep,
  183. sample->weight,
  184. symbol_conf.field_sep,
  185. sample->data_src,
  186. symbol_conf.field_sep,
  187. al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
  188. al.sym ? al.sym->name : "???");
  189. }
  190. out_put:
  191. addr_location__put(&al);
  192. return 0;
  193. }
  194. static int process_sample_event(struct perf_tool *tool,
  195. union perf_event *event,
  196. struct perf_sample *sample,
  197. struct perf_evsel *evsel __maybe_unused,
  198. struct machine *machine)
  199. {
  200. return dump_raw_samples(tool, event, sample, machine);
  201. }
  202. static int report_raw_events(struct perf_mem *mem)
  203. {
  204. struct perf_data data = {
  205. .file = {
  206. .path = input_name,
  207. },
  208. .mode = PERF_DATA_MODE_READ,
  209. .force = mem->force,
  210. };
  211. int ret;
  212. struct perf_session *session = perf_session__new(&data, false,
  213. &mem->tool);
  214. if (session == NULL)
  215. return -1;
  216. if (mem->cpu_list) {
  217. ret = perf_session__cpu_bitmap(session, mem->cpu_list,
  218. mem->cpu_bitmap);
  219. if (ret < 0)
  220. goto out_delete;
  221. }
  222. ret = symbol__init(&session->header.env);
  223. if (ret < 0)
  224. goto out_delete;
  225. if (mem->phys_addr)
  226. printf("# PID, TID, IP, ADDR, PHYS ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
  227. else
  228. printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
  229. ret = perf_session__process_events(session);
  230. out_delete:
  231. perf_session__delete(session);
  232. return ret;
  233. }
  234. static int report_events(int argc, const char **argv, struct perf_mem *mem)
  235. {
  236. const char **rep_argv;
  237. int ret, i = 0, j, rep_argc;
  238. if (mem->dump_raw)
  239. return report_raw_events(mem);
  240. rep_argc = argc + 3;
  241. rep_argv = calloc(rep_argc + 1, sizeof(char *));
  242. if (!rep_argv)
  243. return -1;
  244. rep_argv[i++] = "report";
  245. rep_argv[i++] = "--mem-mode";
  246. rep_argv[i++] = "-n"; /* display number of samples */
  247. /*
  248. * there is no weight (cost) associated with stores, so don't print
  249. * the column
  250. */
  251. if (!(mem->operation & MEM_OPERATION_LOAD)) {
  252. if (mem->phys_addr)
  253. rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
  254. "dso_daddr,tlb,locked,phys_daddr";
  255. else
  256. rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
  257. "dso_daddr,tlb,locked";
  258. } else if (mem->phys_addr)
  259. rep_argv[i++] = "--sort=local_weight,mem,sym,dso,symbol_daddr,"
  260. "dso_daddr,snoop,tlb,locked,phys_daddr";
  261. for (j = 1; j < argc; j++, i++)
  262. rep_argv[i] = argv[j];
  263. ret = cmd_report(i, rep_argv);
  264. free(rep_argv);
  265. return ret;
  266. }
  267. struct mem_mode {
  268. const char *name;
  269. int mode;
  270. };
  271. #define MEM_OPT(n, m) \
  272. { .name = n, .mode = (m) }
  273. #define MEM_END { .name = NULL }
  274. static const struct mem_mode mem_modes[]={
  275. MEM_OPT("load", MEM_OPERATION_LOAD),
  276. MEM_OPT("store", MEM_OPERATION_STORE),
  277. MEM_END
  278. };
  279. static int
  280. parse_mem_ops(const struct option *opt, const char *str, int unset)
  281. {
  282. int *mode = (int *)opt->value;
  283. const struct mem_mode *m;
  284. char *s, *os = NULL, *p;
  285. int ret = -1;
  286. if (unset)
  287. return 0;
  288. /* str may be NULL in case no arg is passed to -t */
  289. if (str) {
  290. /* because str is read-only */
  291. s = os = strdup(str);
  292. if (!s)
  293. return -1;
  294. /* reset mode */
  295. *mode = 0;
  296. for (;;) {
  297. p = strchr(s, ',');
  298. if (p)
  299. *p = '\0';
  300. for (m = mem_modes; m->name; m++) {
  301. if (!strcasecmp(s, m->name))
  302. break;
  303. }
  304. if (!m->name) {
  305. fprintf(stderr, "unknown sampling op %s,"
  306. " check man page\n", s);
  307. goto error;
  308. }
  309. *mode |= m->mode;
  310. if (!p)
  311. break;
  312. s = p + 1;
  313. }
  314. }
  315. ret = 0;
  316. if (*mode == 0)
  317. *mode = MEM_OPERATION_LOAD;
  318. error:
  319. free(os);
  320. return ret;
  321. }
  322. int cmd_mem(int argc, const char **argv)
  323. {
  324. struct stat st;
  325. struct perf_mem mem = {
  326. .tool = {
  327. .sample = process_sample_event,
  328. .mmap = perf_event__process_mmap,
  329. .mmap2 = perf_event__process_mmap2,
  330. .comm = perf_event__process_comm,
  331. .lost = perf_event__process_lost,
  332. .fork = perf_event__process_fork,
  333. .build_id = perf_event__process_build_id,
  334. .namespaces = perf_event__process_namespaces,
  335. .ordered_events = true,
  336. },
  337. .input_name = "perf.data",
  338. /*
  339. * default to both load an store sampling
  340. */
  341. .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
  342. };
  343. const struct option mem_options[] = {
  344. OPT_CALLBACK('t', "type", &mem.operation,
  345. "type", "memory operations(load,store) Default load,store",
  346. parse_mem_ops),
  347. OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
  348. "dump raw samples in ASCII"),
  349. OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
  350. "Only display entries resolved to a symbol"),
  351. OPT_STRING('i', "input", &input_name, "file",
  352. "input file name"),
  353. OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
  354. "list of cpus to profile"),
  355. OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
  356. "separator",
  357. "separator for columns, no spaces will be added"
  358. " between columns '.' is reserved."),
  359. OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
  360. OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
  361. OPT_END()
  362. };
  363. const char *const mem_subcommands[] = { "record", "report", NULL };
  364. const char *mem_usage[] = {
  365. NULL,
  366. NULL
  367. };
  368. if (perf_mem_events__init()) {
  369. pr_err("failed: memory events not supported\n");
  370. return -1;
  371. }
  372. argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
  373. mem_usage, PARSE_OPT_KEEP_UNKNOWN);
  374. if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
  375. usage_with_options(mem_usage, mem_options);
  376. if (!mem.input_name || !strlen(mem.input_name)) {
  377. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  378. mem.input_name = "-";
  379. else
  380. mem.input_name = "perf.data";
  381. }
  382. if (!strncmp(argv[0], "rec", 3))
  383. return __cmd_record(argc, argv, &mem);
  384. else if (!strncmp(argv[0], "rep", 3))
  385. return report_events(argc, argv, &mem);
  386. else
  387. usage_with_options(mem_usage, mem_options);
  388. return 0;
  389. }