builtin-mem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/parse-options.h"
  4. #include "util/trace-event.h"
  5. #include "util/tool.h"
  6. #include "util/session.h"
  7. #include "util/data.h"
  8. #define MEM_OPERATION_LOAD 0x1
  9. #define MEM_OPERATION_STORE 0x2
  10. struct perf_mem {
  11. struct perf_tool tool;
  12. char const *input_name;
  13. bool hide_unresolved;
  14. bool dump_raw;
  15. bool force;
  16. int operation;
  17. const char *cpu_list;
  18. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  19. };
  20. static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
  21. {
  22. int rec_argc, i = 0, j;
  23. const char **rec_argv;
  24. int ret;
  25. rec_argc = argc + 7; /* max number of arguments */
  26. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  27. if (!rec_argv)
  28. return -1;
  29. rec_argv[i++] = "record";
  30. if (mem->operation & MEM_OPERATION_LOAD)
  31. rec_argv[i++] = "-W";
  32. rec_argv[i++] = "-d";
  33. if (mem->operation & MEM_OPERATION_LOAD) {
  34. rec_argv[i++] = "-e";
  35. rec_argv[i++] = "cpu/mem-loads/pp";
  36. }
  37. if (mem->operation & MEM_OPERATION_STORE) {
  38. rec_argv[i++] = "-e";
  39. rec_argv[i++] = "cpu/mem-stores/pp";
  40. }
  41. for (j = 1; j < argc; j++, i++)
  42. rec_argv[i] = argv[j];
  43. ret = cmd_record(i, rec_argv, NULL);
  44. free(rec_argv);
  45. return ret;
  46. }
  47. static int
  48. dump_raw_samples(struct perf_tool *tool,
  49. union perf_event *event,
  50. struct perf_sample *sample,
  51. struct machine *machine)
  52. {
  53. struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
  54. struct addr_location al;
  55. const char *fmt;
  56. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  57. fprintf(stderr, "problem processing %d event, skipping it.\n",
  58. event->header.type);
  59. return -1;
  60. }
  61. if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
  62. goto out_put;
  63. if (al.map != NULL)
  64. al.map->dso->hit = 1;
  65. if (symbol_conf.field_sep) {
  66. fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
  67. "%s0x%"PRIx64"%s%s:%s\n";
  68. } else {
  69. fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
  70. "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
  71. symbol_conf.field_sep = " ";
  72. }
  73. printf(fmt,
  74. sample->pid,
  75. symbol_conf.field_sep,
  76. sample->tid,
  77. symbol_conf.field_sep,
  78. sample->ip,
  79. symbol_conf.field_sep,
  80. sample->addr,
  81. symbol_conf.field_sep,
  82. sample->weight,
  83. symbol_conf.field_sep,
  84. sample->data_src,
  85. symbol_conf.field_sep,
  86. al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
  87. al.sym ? al.sym->name : "???");
  88. out_put:
  89. addr_location__put(&al);
  90. return 0;
  91. }
  92. static int process_sample_event(struct perf_tool *tool,
  93. union perf_event *event,
  94. struct perf_sample *sample,
  95. struct perf_evsel *evsel __maybe_unused,
  96. struct machine *machine)
  97. {
  98. return dump_raw_samples(tool, event, sample, machine);
  99. }
  100. static int report_raw_events(struct perf_mem *mem)
  101. {
  102. struct perf_data_file file = {
  103. .path = input_name,
  104. .mode = PERF_DATA_MODE_READ,
  105. .force = mem->force,
  106. };
  107. int ret;
  108. struct perf_session *session = perf_session__new(&file, false,
  109. &mem->tool);
  110. if (session == NULL)
  111. return -1;
  112. if (mem->cpu_list) {
  113. ret = perf_session__cpu_bitmap(session, mem->cpu_list,
  114. mem->cpu_bitmap);
  115. if (ret < 0)
  116. goto out_delete;
  117. }
  118. ret = symbol__init(&session->header.env);
  119. if (ret < 0)
  120. goto out_delete;
  121. printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
  122. ret = perf_session__process_events(session);
  123. out_delete:
  124. perf_session__delete(session);
  125. return ret;
  126. }
  127. static int report_events(int argc, const char **argv, struct perf_mem *mem)
  128. {
  129. const char **rep_argv;
  130. int ret, i = 0, j, rep_argc;
  131. if (mem->dump_raw)
  132. return report_raw_events(mem);
  133. rep_argc = argc + 3;
  134. rep_argv = calloc(rep_argc + 1, sizeof(char *));
  135. if (!rep_argv)
  136. return -1;
  137. rep_argv[i++] = "report";
  138. rep_argv[i++] = "--mem-mode";
  139. rep_argv[i++] = "-n"; /* display number of samples */
  140. /*
  141. * there is no weight (cost) associated with stores, so don't print
  142. * the column
  143. */
  144. if (!(mem->operation & MEM_OPERATION_LOAD))
  145. rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
  146. "dso_daddr,tlb,locked";
  147. for (j = 1; j < argc; j++, i++)
  148. rep_argv[i] = argv[j];
  149. ret = cmd_report(i, rep_argv, NULL);
  150. free(rep_argv);
  151. return ret;
  152. }
  153. struct mem_mode {
  154. const char *name;
  155. int mode;
  156. };
  157. #define MEM_OPT(n, m) \
  158. { .name = n, .mode = (m) }
  159. #define MEM_END { .name = NULL }
  160. static const struct mem_mode mem_modes[]={
  161. MEM_OPT("load", MEM_OPERATION_LOAD),
  162. MEM_OPT("store", MEM_OPERATION_STORE),
  163. MEM_END
  164. };
  165. static int
  166. parse_mem_ops(const struct option *opt, const char *str, int unset)
  167. {
  168. int *mode = (int *)opt->value;
  169. const struct mem_mode *m;
  170. char *s, *os = NULL, *p;
  171. int ret = -1;
  172. if (unset)
  173. return 0;
  174. /* str may be NULL in case no arg is passed to -t */
  175. if (str) {
  176. /* because str is read-only */
  177. s = os = strdup(str);
  178. if (!s)
  179. return -1;
  180. /* reset mode */
  181. *mode = 0;
  182. for (;;) {
  183. p = strchr(s, ',');
  184. if (p)
  185. *p = '\0';
  186. for (m = mem_modes; m->name; m++) {
  187. if (!strcasecmp(s, m->name))
  188. break;
  189. }
  190. if (!m->name) {
  191. fprintf(stderr, "unknown sampling op %s,"
  192. " check man page\n", s);
  193. goto error;
  194. }
  195. *mode |= m->mode;
  196. if (!p)
  197. break;
  198. s = p + 1;
  199. }
  200. }
  201. ret = 0;
  202. if (*mode == 0)
  203. *mode = MEM_OPERATION_LOAD;
  204. error:
  205. free(os);
  206. return ret;
  207. }
  208. int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
  209. {
  210. struct stat st;
  211. struct perf_mem mem = {
  212. .tool = {
  213. .sample = process_sample_event,
  214. .mmap = perf_event__process_mmap,
  215. .mmap2 = perf_event__process_mmap2,
  216. .comm = perf_event__process_comm,
  217. .lost = perf_event__process_lost,
  218. .fork = perf_event__process_fork,
  219. .build_id = perf_event__process_build_id,
  220. .ordered_events = true,
  221. },
  222. .input_name = "perf.data",
  223. /*
  224. * default to both load an store sampling
  225. */
  226. .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
  227. };
  228. const struct option mem_options[] = {
  229. OPT_CALLBACK('t', "type", &mem.operation,
  230. "type", "memory operations(load,store) Default load,store",
  231. parse_mem_ops),
  232. OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
  233. "dump raw samples in ASCII"),
  234. OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
  235. "Only display entries resolved to a symbol"),
  236. OPT_STRING('i', "input", &input_name, "file",
  237. "input file name"),
  238. OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
  239. "list of cpus to profile"),
  240. OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
  241. "separator",
  242. "separator for columns, no spaces will be added"
  243. " between columns '.' is reserved."),
  244. OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
  245. OPT_END()
  246. };
  247. const char *const mem_subcommands[] = { "record", "report", NULL };
  248. const char *mem_usage[] = {
  249. NULL,
  250. NULL
  251. };
  252. argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
  253. mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  254. if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
  255. usage_with_options(mem_usage, mem_options);
  256. if (!mem.input_name || !strlen(mem.input_name)) {
  257. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  258. mem.input_name = "-";
  259. else
  260. mem.input_name = "perf.data";
  261. }
  262. if (!strncmp(argv[0], "rec", 3))
  263. return __cmd_record(argc, argv, &mem);
  264. else if (!strncmp(argv[0], "rep", 3))
  265. return report_events(argc, argv, &mem);
  266. else
  267. usage_with_options(mem_usage, mem_options);
  268. return 0;
  269. }