builtin-diff.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * builtin-diff.c
  3. *
  4. * Builtin diff command: Analyze two perf.data input files, look up and read
  5. * DSOs and symbol information, sort them and produce a diff.
  6. */
  7. #include "builtin.h"
  8. #include "util/debug.h"
  9. #include "util/event.h"
  10. #include "util/hist.h"
  11. #include "util/evsel.h"
  12. #include "util/evlist.h"
  13. #include "util/session.h"
  14. #include "util/tool.h"
  15. #include "util/sort.h"
  16. #include "util/symbol.h"
  17. #include "util/util.h"
  18. #include <stdlib.h>
  19. static char const *input_old = "perf.data.old",
  20. *input_new = "perf.data";
  21. static char diff__default_sort_order[] = "dso,symbol";
  22. static bool force;
  23. static bool show_displacement;
  24. static bool show_baseline_only;
  25. enum {
  26. COMPUTE_DELTA,
  27. COMPUTE_RATIO,
  28. COMPUTE_MAX,
  29. };
  30. const char *compute_names[COMPUTE_MAX] = {
  31. [COMPUTE_DELTA] = "delta",
  32. [COMPUTE_RATIO] = "ratio",
  33. };
  34. static int compute;
  35. static int setup_compute(const struct option *opt, const char *str,
  36. int unset __maybe_unused)
  37. {
  38. int *cp = (int *) opt->value;
  39. unsigned i;
  40. if (!str) {
  41. *cp = COMPUTE_DELTA;
  42. return 0;
  43. }
  44. for (i = 0; i < COMPUTE_MAX; i++)
  45. if (!strcmp(str, compute_names[i])) {
  46. *cp = i;
  47. return 0;
  48. }
  49. pr_err("Failed: '%s' is not computation method "
  50. "(use 'delta' or 'ratio').\n", str);
  51. return -EINVAL;
  52. }
  53. static int hists__add_entry(struct hists *self,
  54. struct addr_location *al, u64 period)
  55. {
  56. if (__hists__add_entry(self, al, NULL, period) != NULL)
  57. return 0;
  58. return -ENOMEM;
  59. }
  60. static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
  61. union perf_event *event,
  62. struct perf_sample *sample,
  63. struct perf_evsel *evsel,
  64. struct machine *machine)
  65. {
  66. struct addr_location al;
  67. if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
  68. pr_warning("problem processing %d event, skipping it.\n",
  69. event->header.type);
  70. return -1;
  71. }
  72. if (al.filtered || al.sym == NULL)
  73. return 0;
  74. if (hists__add_entry(&evsel->hists, &al, sample->period)) {
  75. pr_warning("problem incrementing symbol period, skipping event\n");
  76. return -1;
  77. }
  78. evsel->hists.stats.total_period += sample->period;
  79. return 0;
  80. }
  81. static struct perf_tool tool = {
  82. .sample = diff__process_sample_event,
  83. .mmap = perf_event__process_mmap,
  84. .comm = perf_event__process_comm,
  85. .exit = perf_event__process_task,
  86. .fork = perf_event__process_task,
  87. .lost = perf_event__process_lost,
  88. .ordered_samples = true,
  89. .ordering_requires_timestamps = true,
  90. };
  91. static void insert_hist_entry_by_name(struct rb_root *root,
  92. struct hist_entry *he)
  93. {
  94. struct rb_node **p = &root->rb_node;
  95. struct rb_node *parent = NULL;
  96. struct hist_entry *iter;
  97. while (*p != NULL) {
  98. parent = *p;
  99. iter = rb_entry(parent, struct hist_entry, rb_node);
  100. if (hist_entry__cmp(he, iter) < 0)
  101. p = &(*p)->rb_left;
  102. else
  103. p = &(*p)->rb_right;
  104. }
  105. rb_link_node(&he->rb_node, parent, p);
  106. rb_insert_color(&he->rb_node, root);
  107. }
  108. static void hists__name_resort(struct hists *self, bool sort)
  109. {
  110. unsigned long position = 1;
  111. struct rb_root tmp = RB_ROOT;
  112. struct rb_node *next = rb_first(&self->entries);
  113. while (next != NULL) {
  114. struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
  115. next = rb_next(&n->rb_node);
  116. n->position = position++;
  117. if (sort) {
  118. rb_erase(&n->rb_node, &self->entries);
  119. insert_hist_entry_by_name(&tmp, n);
  120. }
  121. }
  122. if (sort)
  123. self->entries = tmp;
  124. }
  125. static struct hist_entry *hists__find_entry(struct hists *self,
  126. struct hist_entry *he)
  127. {
  128. struct rb_node *n = self->entries.rb_node;
  129. while (n) {
  130. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
  131. int64_t cmp = hist_entry__cmp(he, iter);
  132. if (cmp < 0)
  133. n = n->rb_left;
  134. else if (cmp > 0)
  135. n = n->rb_right;
  136. else
  137. return iter;
  138. }
  139. return NULL;
  140. }
  141. static void hists__match(struct hists *older, struct hists *newer)
  142. {
  143. struct rb_node *nd;
  144. for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
  145. struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
  146. pos->pair = hists__find_entry(older, pos);
  147. }
  148. }
  149. static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
  150. struct perf_evlist *evlist)
  151. {
  152. struct perf_evsel *e;
  153. list_for_each_entry(e, &evlist->entries, node)
  154. if (perf_evsel__match2(evsel, e))
  155. return e;
  156. return NULL;
  157. }
  158. static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
  159. {
  160. struct perf_evsel *evsel;
  161. list_for_each_entry(evsel, &evlist->entries, node) {
  162. struct hists *hists = &evsel->hists;
  163. hists__output_resort(hists);
  164. /*
  165. * The hists__name_resort only sets possition
  166. * if name is false.
  167. */
  168. if (name || ((!name) && show_displacement))
  169. hists__name_resort(hists, name);
  170. }
  171. }
  172. static void hists__baseline_only(struct hists *hists)
  173. {
  174. struct rb_node *next = rb_first(&hists->entries);
  175. while (next != NULL) {
  176. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
  177. next = rb_next(&he->rb_node);
  178. if (!he->pair) {
  179. rb_erase(&he->rb_node, &hists->entries);
  180. hist_entry__free(he);
  181. }
  182. }
  183. }
  184. static void hists__process(struct hists *old, struct hists *new)
  185. {
  186. hists__match(old, new);
  187. if (show_baseline_only)
  188. hists__baseline_only(new);
  189. hists__fprintf(new, true, 0, 0, stdout);
  190. }
  191. static int __cmd_diff(void)
  192. {
  193. int ret, i;
  194. #define older (session[0])
  195. #define newer (session[1])
  196. struct perf_session *session[2];
  197. struct perf_evlist *evlist_new, *evlist_old;
  198. struct perf_evsel *evsel;
  199. bool first = true;
  200. older = perf_session__new(input_old, O_RDONLY, force, false,
  201. &tool);
  202. newer = perf_session__new(input_new, O_RDONLY, force, false,
  203. &tool);
  204. if (session[0] == NULL || session[1] == NULL)
  205. return -ENOMEM;
  206. for (i = 0; i < 2; ++i) {
  207. ret = perf_session__process_events(session[i], &tool);
  208. if (ret)
  209. goto out_delete;
  210. }
  211. evlist_old = older->evlist;
  212. evlist_new = newer->evlist;
  213. perf_evlist__resort_hists(evlist_old, true);
  214. perf_evlist__resort_hists(evlist_new, false);
  215. list_for_each_entry(evsel, &evlist_new->entries, node) {
  216. struct perf_evsel *evsel_old;
  217. evsel_old = evsel_match(evsel, evlist_old);
  218. if (!evsel_old)
  219. continue;
  220. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  221. perf_evsel__name(evsel));
  222. first = false;
  223. hists__process(&evsel_old->hists, &evsel->hists);
  224. }
  225. out_delete:
  226. for (i = 0; i < 2; ++i)
  227. perf_session__delete(session[i]);
  228. return ret;
  229. #undef older
  230. #undef newer
  231. }
  232. static const char * const diff_usage[] = {
  233. "perf diff [<options>] [old_file] [new_file]",
  234. NULL,
  235. };
  236. static const struct option options[] = {
  237. OPT_INCR('v', "verbose", &verbose,
  238. "be more verbose (show symbol address, etc)"),
  239. OPT_BOOLEAN('M', "displacement", &show_displacement,
  240. "Show position displacement relative to baseline"),
  241. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  242. "Show only items with match in baseline"),
  243. OPT_CALLBACK('c', "compute", &compute, "delta,ratio (default delta)",
  244. "Entries differential computation selection",
  245. setup_compute),
  246. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  247. "dump raw trace in ASCII"),
  248. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  249. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  250. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  251. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  252. "only consider symbols in these dsos"),
  253. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  254. "only consider symbols in these comms"),
  255. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  256. "only consider these symbols"),
  257. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  258. "sort by key(s): pid, comm, dso, symbol, parent"),
  259. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  260. "separator for columns, no spaces will be added between "
  261. "columns '.' is reserved."),
  262. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  263. "Look for files with symbols relative to this directory"),
  264. OPT_END()
  265. };
  266. static void ui_init(void)
  267. {
  268. perf_hpp__init();
  269. /* No overhead column. */
  270. perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
  271. /* Display baseline/delta/ratio/displacement columns. */
  272. perf_hpp__column_enable(PERF_HPP__BASELINE, true);
  273. switch (compute) {
  274. case COMPUTE_DELTA:
  275. perf_hpp__column_enable(PERF_HPP__DELTA, true);
  276. break;
  277. case COMPUTE_RATIO:
  278. perf_hpp__column_enable(PERF_HPP__RATIO, true);
  279. break;
  280. default:
  281. BUG_ON(1);
  282. };
  283. if (show_displacement)
  284. perf_hpp__column_enable(PERF_HPP__DISPL, true);
  285. }
  286. int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
  287. {
  288. sort_order = diff__default_sort_order;
  289. argc = parse_options(argc, argv, options, diff_usage, 0);
  290. if (argc) {
  291. if (argc > 2)
  292. usage_with_options(diff_usage, options);
  293. if (argc == 2) {
  294. input_old = argv[0];
  295. input_new = argv[1];
  296. } else
  297. input_new = argv[0];
  298. } else if (symbol_conf.default_guest_vmlinux_name ||
  299. symbol_conf.default_guest_kallsyms) {
  300. input_old = "perf.data.host";
  301. input_new = "perf.data.guest";
  302. }
  303. symbol_conf.exclude_other = false;
  304. if (symbol__init() < 0)
  305. return -1;
  306. ui_init();
  307. setup_sorting(diff_usage, options);
  308. setup_pager();
  309. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
  310. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
  311. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
  312. return __cmd_diff();
  313. }