builtin-diff.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/session.h"
  12. #include "util/sort.h"
  13. #include "util/symbol.h"
  14. #include "util/util.h"
  15. #include <stdlib.h>
  16. static char const *input_old = "perf.data.old",
  17. *input_new = "perf.data";
  18. static char diff__default_sort_order[] = "dso,symbol";
  19. static int force;
  20. static bool show_displacement;
  21. static int perf_session__add_hist_entry(struct perf_session *self,
  22. struct addr_location *al, u64 count)
  23. {
  24. bool hit;
  25. struct hist_entry *he = __perf_session__add_hist_entry(self, al, NULL,
  26. count, &hit);
  27. if (he == NULL)
  28. return -ENOMEM;
  29. if (hit)
  30. he->count += count;
  31. return 0;
  32. }
  33. static int diff__process_sample_event(event_t *event, struct perf_session *session)
  34. {
  35. struct addr_location al;
  36. struct sample_data data = { .period = 1, };
  37. dump_printf("(IP, %d): %d: %p\n", event->header.misc,
  38. event->ip.pid, (void *)(long)event->ip.ip);
  39. if (event__preprocess_sample(event, session, &al, NULL) < 0) {
  40. pr_warning("problem processing %d event, skipping it.\n",
  41. event->header.type);
  42. return -1;
  43. }
  44. if (al.filtered)
  45. return 0;
  46. event__parse_sample(event, session->sample_type, &data);
  47. if (al.sym && perf_session__add_hist_entry(session, &al, data.period)) {
  48. pr_warning("problem incrementing symbol count, skipping event\n");
  49. return -1;
  50. }
  51. session->events_stats.total += data.period;
  52. return 0;
  53. }
  54. static struct perf_event_ops event_ops = {
  55. .process_sample_event = diff__process_sample_event,
  56. .process_mmap_event = event__process_mmap,
  57. .process_comm_event = event__process_comm,
  58. .process_exit_event = event__process_task,
  59. .process_fork_event = event__process_task,
  60. .process_lost_event = event__process_lost,
  61. };
  62. static void perf_session__insert_hist_entry_by_name(struct rb_root *root,
  63. struct hist_entry *he)
  64. {
  65. struct rb_node **p = &root->rb_node;
  66. struct rb_node *parent = NULL;
  67. struct hist_entry *iter;
  68. while (*p != NULL) {
  69. int cmp;
  70. parent = *p;
  71. iter = rb_entry(parent, struct hist_entry, rb_node);
  72. cmp = strcmp(he->map->dso->name, iter->map->dso->name);
  73. if (cmp > 0)
  74. p = &(*p)->rb_left;
  75. else if (cmp < 0)
  76. p = &(*p)->rb_right;
  77. else {
  78. cmp = strcmp(he->sym->name, iter->sym->name);
  79. if (cmp > 0)
  80. p = &(*p)->rb_left;
  81. else
  82. p = &(*p)->rb_right;
  83. }
  84. }
  85. rb_link_node(&he->rb_node, parent, p);
  86. rb_insert_color(&he->rb_node, root);
  87. }
  88. static void perf_session__resort_by_name(struct perf_session *self)
  89. {
  90. unsigned long position = 1;
  91. struct rb_root tmp = RB_ROOT;
  92. struct rb_node *next = rb_first(&self->hists);
  93. while (next != NULL) {
  94. struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
  95. next = rb_next(&n->rb_node);
  96. rb_erase(&n->rb_node, &self->hists);
  97. n->position = position++;
  98. perf_session__insert_hist_entry_by_name(&tmp, n);
  99. }
  100. self->hists = tmp;
  101. }
  102. static struct hist_entry *
  103. perf_session__find_hist_entry_by_name(struct perf_session *self,
  104. struct hist_entry *he)
  105. {
  106. struct rb_node *n = self->hists.rb_node;
  107. while (n) {
  108. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
  109. int cmp = strcmp(he->map->dso->name, iter->map->dso->name);
  110. if (cmp > 0)
  111. n = n->rb_left;
  112. else if (cmp < 0)
  113. n = n->rb_right;
  114. else {
  115. cmp = strcmp(he->sym->name, iter->sym->name);
  116. if (cmp > 0)
  117. n = n->rb_left;
  118. else if (cmp < 0)
  119. n = n->rb_right;
  120. else
  121. return iter;
  122. }
  123. }
  124. return NULL;
  125. }
  126. static void perf_session__match_hists(struct perf_session *old_session,
  127. struct perf_session *new_session)
  128. {
  129. struct rb_node *nd;
  130. perf_session__resort_by_name(old_session);
  131. for (nd = rb_first(&new_session->hists); nd; nd = rb_next(nd)) {
  132. struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
  133. pos->pair = perf_session__find_hist_entry_by_name(old_session, pos);
  134. }
  135. }
  136. static int __cmd_diff(void)
  137. {
  138. int ret, i;
  139. struct perf_session *session[2];
  140. session[0] = perf_session__new(input_old, O_RDONLY, force);
  141. session[1] = perf_session__new(input_new, O_RDONLY, force);
  142. if (session[0] == NULL || session[1] == NULL)
  143. return -ENOMEM;
  144. for (i = 0; i < 2; ++i) {
  145. ret = perf_session__process_events(session[i], &event_ops);
  146. if (ret)
  147. goto out_delete;
  148. perf_session__output_resort(session[i], session[i]->events_stats.total);
  149. }
  150. perf_session__match_hists(session[0], session[1]);
  151. perf_session__fprintf_hists(session[1], session[0],
  152. show_displacement, stdout);
  153. out_delete:
  154. for (i = 0; i < 2; ++i)
  155. perf_session__delete(session[i]);
  156. return ret;
  157. }
  158. static const char * const diff_usage[] = {
  159. "perf diff [<options>] [old_file] [new_file]",
  160. NULL,
  161. };
  162. static const struct option options[] = {
  163. OPT_BOOLEAN('v', "verbose", &verbose,
  164. "be more verbose (show symbol address, etc)"),
  165. OPT_BOOLEAN('m', "displacement", &show_displacement,
  166. "Show position displacement relative to baseline"),
  167. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  168. "dump raw trace in ASCII"),
  169. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  170. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  171. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  172. OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
  173. "Don't shorten the pathnames taking into account the cwd"),
  174. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  175. "only consider symbols in these dsos"),
  176. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  177. "only consider symbols in these comms"),
  178. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  179. "only consider these symbols"),
  180. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  181. "sort by key(s): pid, comm, dso, symbol, parent"),
  182. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  183. "separator for columns, no spaces will be added between "
  184. "columns '.' is reserved."),
  185. OPT_END()
  186. };
  187. int cmd_diff(int argc, const char **argv, const char *prefix __used)
  188. {
  189. sort_order = diff__default_sort_order;
  190. argc = parse_options(argc, argv, options, diff_usage, 0);
  191. if (argc) {
  192. if (argc > 2)
  193. usage_with_options(diff_usage, options);
  194. if (argc == 2) {
  195. input_old = argv[0];
  196. input_new = argv[1];
  197. } else
  198. input_new = argv[0];
  199. }
  200. symbol_conf.exclude_other = false;
  201. if (symbol__init() < 0)
  202. return -1;
  203. setup_sorting(diff_usage, options);
  204. setup_pager();
  205. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
  206. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
  207. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
  208. return __cmd_diff();
  209. }