builtin-diff.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 int force;
  19. static bool show_percent;
  20. static int perf_session__add_hist_entry(struct perf_session *self,
  21. struct addr_location *al, u64 count)
  22. {
  23. bool hit;
  24. struct hist_entry *he = __perf_session__add_hist_entry(self, al, NULL,
  25. count, &hit);
  26. if (he == NULL)
  27. return -ENOMEM;
  28. if (hit)
  29. he->count += count;
  30. return 0;
  31. }
  32. static int diff__process_sample_event(event_t *event, struct perf_session *session)
  33. {
  34. struct addr_location al;
  35. struct sample_data data = { .period = 1, };
  36. dump_printf("(IP, %d): %d: %p\n", event->header.misc,
  37. event->ip.pid, (void *)(long)event->ip.ip);
  38. if (event__preprocess_sample(event, session, &al, NULL) < 0) {
  39. pr_warning("problem processing %d event, skipping it.\n",
  40. event->header.type);
  41. return -1;
  42. }
  43. if (al.filtered)
  44. return 0;
  45. event__parse_sample(event, session->sample_type, &data);
  46. if (al.sym && perf_session__add_hist_entry(session, &al, data.period)) {
  47. pr_warning("problem incrementing symbol count, skipping event\n");
  48. return -1;
  49. }
  50. session->events_stats.total += data.period;
  51. return 0;
  52. }
  53. static struct perf_event_ops event_ops = {
  54. .process_sample_event = diff__process_sample_event,
  55. .process_mmap_event = event__process_mmap,
  56. .process_comm_event = event__process_comm,
  57. .process_exit_event = event__process_task,
  58. .process_fork_event = event__process_task,
  59. .process_lost_event = event__process_lost,
  60. };
  61. static void perf_session__insert_hist_entry_by_name(struct rb_root *root,
  62. struct hist_entry *he)
  63. {
  64. struct rb_node **p = &root->rb_node;
  65. struct rb_node *parent = NULL;
  66. struct hist_entry *iter;
  67. while (*p != NULL) {
  68. int cmp;
  69. parent = *p;
  70. iter = rb_entry(parent, struct hist_entry, rb_node);
  71. cmp = strcmp(he->map->dso->name, iter->map->dso->name);
  72. if (cmp > 0)
  73. p = &(*p)->rb_left;
  74. else if (cmp < 0)
  75. p = &(*p)->rb_right;
  76. else {
  77. cmp = strcmp(he->sym->name, iter->sym->name);
  78. if (cmp > 0)
  79. p = &(*p)->rb_left;
  80. else
  81. p = &(*p)->rb_right;
  82. }
  83. }
  84. rb_link_node(&he->rb_node, parent, p);
  85. rb_insert_color(&he->rb_node, root);
  86. }
  87. static void perf_session__resort_by_name(struct perf_session *self)
  88. {
  89. unsigned long position = 1;
  90. struct rb_root tmp = RB_ROOT;
  91. struct rb_node *next = rb_first(&self->hists);
  92. while (next != NULL) {
  93. struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
  94. next = rb_next(&n->rb_node);
  95. rb_erase(&n->rb_node, &self->hists);
  96. n->position = position++;
  97. perf_session__insert_hist_entry_by_name(&tmp, n);
  98. }
  99. self->hists = tmp;
  100. }
  101. static struct hist_entry *
  102. perf_session__find_hist_entry_by_name(struct perf_session *self,
  103. struct hist_entry *he)
  104. {
  105. struct rb_node *n = self->hists.rb_node;
  106. while (n) {
  107. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
  108. int cmp = strcmp(he->map->dso->name, iter->map->dso->name);
  109. if (cmp > 0)
  110. n = n->rb_left;
  111. else if (cmp < 0)
  112. n = n->rb_right;
  113. else {
  114. cmp = strcmp(he->sym->name, iter->sym->name);
  115. if (cmp > 0)
  116. n = n->rb_left;
  117. else if (cmp < 0)
  118. n = n->rb_right;
  119. else
  120. return iter;
  121. }
  122. }
  123. return NULL;
  124. }
  125. static void perf_session__match_hists(struct perf_session *old_session,
  126. struct perf_session *new_session)
  127. {
  128. struct rb_node *nd;
  129. perf_session__resort_by_name(old_session);
  130. for (nd = rb_first(&new_session->hists); nd; nd = rb_next(nd)) {
  131. struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
  132. pos->pair = perf_session__find_hist_entry_by_name(old_session, pos);
  133. }
  134. }
  135. static size_t hist_entry__fprintf_matched(struct hist_entry *self,
  136. unsigned long pos,
  137. struct perf_session *session,
  138. struct perf_session *pair_session,
  139. FILE *fp)
  140. {
  141. u64 old_count = 0;
  142. char displacement[16];
  143. size_t printed;
  144. if (self->pair != NULL) {
  145. long pdiff = (long)self->pair->position - (long)pos;
  146. old_count = self->pair->count;
  147. if (pdiff == 0)
  148. goto blank;
  149. snprintf(displacement, sizeof(displacement), "%+4ld", pdiff);
  150. } else {
  151. blank: memset(displacement, ' ', sizeof(displacement));
  152. }
  153. printed = fprintf(fp, "%4lu %5.5s ", pos, displacement);
  154. if (show_percent) {
  155. double old_percent = 0, new_percent = 0, diff;
  156. if (pair_session->events_stats.total > 0)
  157. old_percent = (old_count * 100) / pair_session->events_stats.total;
  158. if (session->events_stats.total > 0)
  159. new_percent = (self->count * 100) / session->events_stats.total;
  160. diff = old_percent - new_percent;
  161. if (verbose)
  162. printed += fprintf(fp, " %3.2f%% %3.2f%%", old_percent, new_percent);
  163. if ((u64)diff != 0)
  164. printed += fprintf(fp, " %+4.2F%%", diff);
  165. else
  166. printed += fprintf(fp, " ");
  167. } else {
  168. if (verbose)
  169. printed += fprintf(fp, " %9Lu %9Lu", old_count, self->count);
  170. printed += fprintf(fp, " %+9Ld", (s64)self->count - (s64)old_count);
  171. }
  172. return printed + fprintf(fp, " %25.25s %s\n",
  173. self->map->dso->name, self->sym->name);
  174. }
  175. static size_t perf_session__fprintf_matched_hists(struct perf_session *self,
  176. struct perf_session *pair,
  177. FILE *fp)
  178. {
  179. struct rb_node *nd;
  180. size_t printed = 0;
  181. unsigned long pos = 1;
  182. for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
  183. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  184. printed += hist_entry__fprintf_matched(he, pos++, self, pair, fp);
  185. }
  186. return printed;
  187. }
  188. static int __cmd_diff(void)
  189. {
  190. int ret, i;
  191. struct perf_session *session[2];
  192. session[0] = perf_session__new(input_old, O_RDONLY, force);
  193. session[1] = perf_session__new(input_new, O_RDONLY, force);
  194. if (session[0] == NULL || session[1] == NULL)
  195. return -ENOMEM;
  196. for (i = 0; i < 2; ++i) {
  197. ret = perf_session__process_events(session[i], &event_ops);
  198. if (ret)
  199. goto out_delete;
  200. perf_session__output_resort(session[i], session[i]->events_stats.total);
  201. }
  202. perf_session__match_hists(session[0], session[1]);
  203. perf_session__fprintf_matched_hists(session[1], session[0], stdout);
  204. out_delete:
  205. for (i = 0; i < 2; ++i)
  206. perf_session__delete(session[i]);
  207. return ret;
  208. }
  209. static const char *const diff_usage[] = {
  210. "perf diff [<options>] [old_file] [new_file]",
  211. };
  212. static const struct option options[] = {
  213. OPT_BOOLEAN('v', "verbose", &verbose,
  214. "be more verbose (show symbol address, etc)"),
  215. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  216. "dump raw trace in ASCII"),
  217. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  218. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  219. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  220. OPT_BOOLEAN('p', "percentages", &show_percent,
  221. "Don't shorten the pathnames taking into account the cwd"),
  222. OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
  223. "Don't shorten the pathnames taking into account the cwd"),
  224. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  225. "only consider symbols in these dsos"),
  226. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  227. "only consider symbols in these comms"),
  228. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  229. "only consider these symbols"),
  230. OPT_END()
  231. };
  232. int cmd_diff(int argc, const char **argv, const char *prefix __used)
  233. {
  234. argc = parse_options(argc, argv, options, diff_usage, 0);
  235. if (argc) {
  236. if (argc > 2)
  237. usage_with_options(diff_usage, options);
  238. if (argc == 2) {
  239. input_old = argv[0];
  240. input_new = argv[1];
  241. } else
  242. input_new = argv[0];
  243. }
  244. if (symbol__init() < 0)
  245. return -1;
  246. setup_sorting(diff_usage, options);
  247. setup_pager();
  248. return __cmd_diff();
  249. }