stream.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Compare and figure out the top N hottest streams
  4. * Copyright (c) 2020, Intel Corporation.
  5. * Author: Jin Yao
  6. */
  7. #include <inttypes.h>
  8. #include <stdlib.h>
  9. #include <linux/zalloc.h>
  10. #include "debug.h"
  11. #include "hist.h"
  12. #include "sort.h"
  13. #include "stream.h"
  14. #include "evlist.h"
  15. static void evsel_streams__delete(struct evsel_streams *es, int nr_evsel)
  16. {
  17. for (int i = 0; i < nr_evsel; i++)
  18. zfree(&es[i].streams);
  19. free(es);
  20. }
  21. void evlist_streams__delete(struct evlist_streams *els)
  22. {
  23. evsel_streams__delete(els->ev_streams, els->nr_evsel);
  24. free(els);
  25. }
  26. static struct evlist_streams *evlist_streams__new(int nr_evsel,
  27. int nr_streams_max)
  28. {
  29. struct evlist_streams *els;
  30. struct evsel_streams *es;
  31. els = zalloc(sizeof(*els));
  32. if (!els)
  33. return NULL;
  34. es = calloc(nr_evsel, sizeof(struct evsel_streams));
  35. if (!es) {
  36. free(els);
  37. return NULL;
  38. }
  39. for (int i = 0; i < nr_evsel; i++) {
  40. struct evsel_streams *s = &es[i];
  41. s->streams = calloc(nr_streams_max, sizeof(struct stream));
  42. if (!s->streams)
  43. goto err;
  44. s->nr_streams_max = nr_streams_max;
  45. s->evsel_idx = -1;
  46. }
  47. els->ev_streams = es;
  48. els->nr_evsel = nr_evsel;
  49. return els;
  50. err:
  51. evsel_streams__delete(es, nr_evsel);
  52. return NULL;
  53. }
  54. /*
  55. * The cnodes with high hit number are hot callchains.
  56. */
  57. static void evsel_streams__set_hot_cnode(struct evsel_streams *es,
  58. struct callchain_node *cnode)
  59. {
  60. int i, idx = 0;
  61. u64 hit;
  62. if (es->nr_streams < es->nr_streams_max) {
  63. i = es->nr_streams;
  64. es->streams[i].cnode = cnode;
  65. es->nr_streams++;
  66. return;
  67. }
  68. /*
  69. * Considering a few number of hot streams, only use simple
  70. * way to find the cnode with smallest hit number and replace.
  71. */
  72. hit = (es->streams[0].cnode)->hit;
  73. for (i = 1; i < es->nr_streams; i++) {
  74. if ((es->streams[i].cnode)->hit < hit) {
  75. hit = (es->streams[i].cnode)->hit;
  76. idx = i;
  77. }
  78. }
  79. if (cnode->hit > hit)
  80. es->streams[idx].cnode = cnode;
  81. }
  82. static void update_hot_callchain(struct hist_entry *he,
  83. struct evsel_streams *es)
  84. {
  85. struct rb_root *root = &he->sorted_chain;
  86. struct rb_node *rb_node = rb_first(root);
  87. struct callchain_node *cnode;
  88. while (rb_node) {
  89. cnode = rb_entry(rb_node, struct callchain_node, rb_node);
  90. evsel_streams__set_hot_cnode(es, cnode);
  91. rb_node = rb_next(rb_node);
  92. }
  93. }
  94. static void init_hot_callchain(struct hists *hists, struct evsel_streams *es)
  95. {
  96. struct rb_node *next = rb_first_cached(&hists->entries);
  97. while (next) {
  98. struct hist_entry *he;
  99. he = rb_entry(next, struct hist_entry, rb_node);
  100. update_hot_callchain(he, es);
  101. next = rb_next(&he->rb_node);
  102. }
  103. es->streams_hits = callchain_total_hits(hists);
  104. }
  105. static int evlist__init_callchain_streams(struct evlist *evlist,
  106. struct evlist_streams *els)
  107. {
  108. struct evsel_streams *es = els->ev_streams;
  109. struct evsel *pos;
  110. int i = 0;
  111. BUG_ON(els->nr_evsel < evlist->core.nr_entries);
  112. evlist__for_each_entry(evlist, pos) {
  113. struct hists *hists = evsel__hists(pos);
  114. hists__output_resort(hists, NULL);
  115. init_hot_callchain(hists, &es[i]);
  116. es[i].evsel_idx = pos->idx;
  117. i++;
  118. }
  119. return 0;
  120. }
  121. struct evlist_streams *evlist__create_streams(struct evlist *evlist,
  122. int nr_streams_max)
  123. {
  124. int nr_evsel = evlist->core.nr_entries, ret = -1;
  125. struct evlist_streams *els = evlist_streams__new(nr_evsel,
  126. nr_streams_max);
  127. if (!els)
  128. return NULL;
  129. ret = evlist__init_callchain_streams(evlist, els);
  130. if (ret) {
  131. evlist_streams__delete(els);
  132. return NULL;
  133. }
  134. return els;
  135. }
  136. struct evsel_streams *evsel_streams__entry(struct evlist_streams *els,
  137. int evsel_idx)
  138. {
  139. struct evsel_streams *es = els->ev_streams;
  140. for (int i = 0; i < els->nr_evsel; i++) {
  141. if (es[i].evsel_idx == evsel_idx)
  142. return &es[i];
  143. }
  144. return NULL;
  145. }
  146. static struct stream *stream__callchain_match(struct stream *base_stream,
  147. struct evsel_streams *es_pair)
  148. {
  149. for (int i = 0; i < es_pair->nr_streams; i++) {
  150. struct stream *pair_stream = &es_pair->streams[i];
  151. if (callchain_cnode_matched(base_stream->cnode,
  152. pair_stream->cnode)) {
  153. return pair_stream;
  154. }
  155. }
  156. return NULL;
  157. }
  158. static struct stream *stream__match(struct stream *base_stream,
  159. struct evsel_streams *es_pair)
  160. {
  161. return stream__callchain_match(base_stream, es_pair);
  162. }
  163. static void stream__link(struct stream *base_stream, struct stream *pair_stream)
  164. {
  165. base_stream->pair_cnode = pair_stream->cnode;
  166. pair_stream->pair_cnode = base_stream->cnode;
  167. }
  168. void evsel_streams__match(struct evsel_streams *es_base,
  169. struct evsel_streams *es_pair)
  170. {
  171. for (int i = 0; i < es_base->nr_streams; i++) {
  172. struct stream *base_stream = &es_base->streams[i];
  173. struct stream *pair_stream;
  174. pair_stream = stream__match(base_stream, es_pair);
  175. if (pair_stream)
  176. stream__link(base_stream, pair_stream);
  177. }
  178. }
  179. static void print_callchain_pair(struct stream *base_stream, int idx,
  180. struct evsel_streams *es_base,
  181. struct evsel_streams *es_pair)
  182. {
  183. struct callchain_node *base_cnode = base_stream->cnode;
  184. struct callchain_node *pair_cnode = base_stream->pair_cnode;
  185. struct callchain_list *base_chain, *pair_chain;
  186. char buf1[512], buf2[512], cbuf1[256], cbuf2[256];
  187. char *s1, *s2;
  188. double pct;
  189. printf("\nhot chain pair %d:\n", idx);
  190. pct = (double)base_cnode->hit / (double)es_base->streams_hits;
  191. scnprintf(buf1, sizeof(buf1), "cycles: %ld, hits: %.2f%%",
  192. callchain_avg_cycles(base_cnode), pct * 100.0);
  193. pct = (double)pair_cnode->hit / (double)es_pair->streams_hits;
  194. scnprintf(buf2, sizeof(buf2), "cycles: %ld, hits: %.2f%%",
  195. callchain_avg_cycles(pair_cnode), pct * 100.0);
  196. printf("%35s\t%35s\n", buf1, buf2);
  197. printf("%35s\t%35s\n",
  198. "---------------------------",
  199. "--------------------------");
  200. pair_chain = list_first_entry(&pair_cnode->val,
  201. struct callchain_list,
  202. list);
  203. list_for_each_entry(base_chain, &base_cnode->val, list) {
  204. if (&pair_chain->list == &pair_cnode->val)
  205. return;
  206. s1 = callchain_list__sym_name(base_chain, cbuf1, sizeof(cbuf1),
  207. false);
  208. s2 = callchain_list__sym_name(pair_chain, cbuf2, sizeof(cbuf2),
  209. false);
  210. scnprintf(buf1, sizeof(buf1), "%35s\t%35s", s1, s2);
  211. printf("%s\n", buf1);
  212. pair_chain = list_next_entry(pair_chain, list);
  213. }
  214. }
  215. static void print_stream_callchain(struct stream *stream, int idx,
  216. struct evsel_streams *es, bool pair)
  217. {
  218. struct callchain_node *cnode = stream->cnode;
  219. struct callchain_list *chain;
  220. char buf[512], cbuf[256], *s;
  221. double pct;
  222. printf("\nhot chain %d:\n", idx);
  223. pct = (double)cnode->hit / (double)es->streams_hits;
  224. scnprintf(buf, sizeof(buf), "cycles: %ld, hits: %.2f%%",
  225. callchain_avg_cycles(cnode), pct * 100.0);
  226. if (pair) {
  227. printf("%35s\t%35s\n", "", buf);
  228. printf("%35s\t%35s\n",
  229. "", "--------------------------");
  230. } else {
  231. printf("%35s\n", buf);
  232. printf("%35s\n", "--------------------------");
  233. }
  234. list_for_each_entry(chain, &cnode->val, list) {
  235. s = callchain_list__sym_name(chain, cbuf, sizeof(cbuf), false);
  236. if (pair)
  237. scnprintf(buf, sizeof(buf), "%35s\t%35s", "", s);
  238. else
  239. scnprintf(buf, sizeof(buf), "%35s", s);
  240. printf("%s\n", buf);
  241. }
  242. }
  243. static void callchain_streams_report(struct evsel_streams *es_base,
  244. struct evsel_streams *es_pair)
  245. {
  246. struct stream *base_stream;
  247. int i, idx = 0;
  248. printf("[ Matched hot streams ]\n");
  249. for (i = 0; i < es_base->nr_streams; i++) {
  250. base_stream = &es_base->streams[i];
  251. if (base_stream->pair_cnode) {
  252. print_callchain_pair(base_stream, ++idx,
  253. es_base, es_pair);
  254. }
  255. }
  256. idx = 0;
  257. printf("\n[ Hot streams in old perf data only ]\n");
  258. for (i = 0; i < es_base->nr_streams; i++) {
  259. base_stream = &es_base->streams[i];
  260. if (!base_stream->pair_cnode) {
  261. print_stream_callchain(base_stream, ++idx,
  262. es_base, false);
  263. }
  264. }
  265. idx = 0;
  266. printf("\n[ Hot streams in new perf data only ]\n");
  267. for (i = 0; i < es_pair->nr_streams; i++) {
  268. base_stream = &es_pair->streams[i];
  269. if (!base_stream->pair_cnode) {
  270. print_stream_callchain(base_stream, ++idx,
  271. es_pair, true);
  272. }
  273. }
  274. }
  275. void evsel_streams__report(struct evsel_streams *es_base,
  276. struct evsel_streams *es_pair)
  277. {
  278. return callchain_streams_report(es_base, es_pair);
  279. }