builtin-diff.c 7.4 KB

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