sort.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. #include "sort.h"
  2. #include "hist.h"
  3. #include "comm.h"
  4. #include "symbol.h"
  5. regex_t parent_regex;
  6. const char default_parent_pattern[] = "^sys_|^do_page_fault";
  7. const char *parent_pattern = default_parent_pattern;
  8. const char default_sort_order[] = "comm,dso,symbol";
  9. const char *sort_order = default_sort_order;
  10. regex_t ignore_callees_regex;
  11. int have_ignore_callees = 0;
  12. int sort__need_collapse = 0;
  13. int sort__has_parent = 0;
  14. int sort__has_sym = 0;
  15. enum sort_mode sort__mode = SORT_MODE__NORMAL;
  16. enum sort_type sort__first_dimension;
  17. LIST_HEAD(hist_entry__sort_list);
  18. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  19. {
  20. int n;
  21. va_list ap;
  22. va_start(ap, fmt);
  23. n = vsnprintf(bf, size, fmt, ap);
  24. if (symbol_conf.field_sep && n > 0) {
  25. char *sep = bf;
  26. while (1) {
  27. sep = strchr(sep, *symbol_conf.field_sep);
  28. if (sep == NULL)
  29. break;
  30. *sep = '.';
  31. }
  32. }
  33. va_end(ap);
  34. if (n >= (int)size)
  35. return size - 1;
  36. return n;
  37. }
  38. static int64_t cmp_null(const void *l, const void *r)
  39. {
  40. if (!l && !r)
  41. return 0;
  42. else if (!l)
  43. return -1;
  44. else
  45. return 1;
  46. }
  47. /* --sort pid */
  48. static int64_t
  49. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  50. {
  51. return right->thread->tid - left->thread->tid;
  52. }
  53. static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
  54. size_t size, unsigned int width)
  55. {
  56. const char *comm = thread__comm_str(he->thread);
  57. return repsep_snprintf(bf, size, "%*s:%5d", width - 6,
  58. comm ?: "", he->thread->tid);
  59. }
  60. struct sort_entry sort_thread = {
  61. .se_header = "Command: Pid",
  62. .se_cmp = sort__thread_cmp,
  63. .se_snprintf = hist_entry__thread_snprintf,
  64. .se_width_idx = HISTC_THREAD,
  65. };
  66. /* --sort comm */
  67. static int64_t
  68. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  69. {
  70. /* Compare the addr that should be unique among comm */
  71. return comm__str(right->comm) - comm__str(left->comm);
  72. }
  73. static int64_t
  74. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  75. {
  76. /* Compare the addr that should be unique among comm */
  77. return comm__str(right->comm) - comm__str(left->comm);
  78. }
  79. static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
  80. size_t size, unsigned int width)
  81. {
  82. return repsep_snprintf(bf, size, "%*s", width, comm__str(he->comm));
  83. }
  84. struct sort_entry sort_comm = {
  85. .se_header = "Command",
  86. .se_cmp = sort__comm_cmp,
  87. .se_collapse = sort__comm_collapse,
  88. .se_snprintf = hist_entry__comm_snprintf,
  89. .se_width_idx = HISTC_COMM,
  90. };
  91. /* --sort dso */
  92. static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
  93. {
  94. struct dso *dso_l = map_l ? map_l->dso : NULL;
  95. struct dso *dso_r = map_r ? map_r->dso : NULL;
  96. const char *dso_name_l, *dso_name_r;
  97. if (!dso_l || !dso_r)
  98. return cmp_null(dso_l, dso_r);
  99. if (verbose) {
  100. dso_name_l = dso_l->long_name;
  101. dso_name_r = dso_r->long_name;
  102. } else {
  103. dso_name_l = dso_l->short_name;
  104. dso_name_r = dso_r->short_name;
  105. }
  106. return strcmp(dso_name_l, dso_name_r);
  107. }
  108. static int64_t
  109. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  110. {
  111. return _sort__dso_cmp(left->ms.map, right->ms.map);
  112. }
  113. static int _hist_entry__dso_snprintf(struct map *map, char *bf,
  114. size_t size, unsigned int width)
  115. {
  116. if (map && map->dso) {
  117. const char *dso_name = !verbose ? map->dso->short_name :
  118. map->dso->long_name;
  119. return repsep_snprintf(bf, size, "%-*s", width, dso_name);
  120. }
  121. return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
  122. }
  123. static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
  124. size_t size, unsigned int width)
  125. {
  126. return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
  127. }
  128. struct sort_entry sort_dso = {
  129. .se_header = "Shared Object",
  130. .se_cmp = sort__dso_cmp,
  131. .se_snprintf = hist_entry__dso_snprintf,
  132. .se_width_idx = HISTC_DSO,
  133. };
  134. /* --sort symbol */
  135. static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
  136. {
  137. u64 ip_l, ip_r;
  138. if (!sym_l || !sym_r)
  139. return cmp_null(sym_l, sym_r);
  140. if (sym_l == sym_r)
  141. return 0;
  142. ip_l = sym_l->start;
  143. ip_r = sym_r->start;
  144. return (int64_t)(ip_r - ip_l);
  145. }
  146. static int64_t
  147. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  148. {
  149. int64_t ret;
  150. if (!left->ms.sym && !right->ms.sym)
  151. return right->level - left->level;
  152. /*
  153. * comparing symbol address alone is not enough since it's a
  154. * relative address within a dso.
  155. */
  156. ret = sort__dso_cmp(left, right);
  157. if (ret != 0)
  158. return ret;
  159. return _sort__sym_cmp(left->ms.sym, right->ms.sym);
  160. }
  161. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  162. u64 ip, char level, char *bf, size_t size,
  163. unsigned int width)
  164. {
  165. size_t ret = 0;
  166. if (verbose) {
  167. char o = map ? dso__symtab_origin(map->dso) : '!';
  168. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  169. BITS_PER_LONG / 4 + 2, ip, o);
  170. }
  171. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  172. if (sym && map) {
  173. if (map->type == MAP__VARIABLE) {
  174. ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
  175. ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
  176. ip - map->unmap_ip(map, sym->start));
  177. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  178. width - ret, "");
  179. } else {
  180. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  181. width - ret,
  182. sym->name);
  183. }
  184. } else {
  185. size_t len = BITS_PER_LONG / 4;
  186. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  187. len, ip);
  188. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  189. width - ret, "");
  190. }
  191. return ret;
  192. }
  193. static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
  194. size_t size, unsigned int width)
  195. {
  196. return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
  197. he->level, bf, size, width);
  198. }
  199. struct sort_entry sort_sym = {
  200. .se_header = "Symbol",
  201. .se_cmp = sort__sym_cmp,
  202. .se_snprintf = hist_entry__sym_snprintf,
  203. .se_width_idx = HISTC_SYMBOL,
  204. };
  205. /* --sort srcline */
  206. static int64_t
  207. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  208. {
  209. if (!left->srcline) {
  210. if (!left->ms.map)
  211. left->srcline = SRCLINE_UNKNOWN;
  212. else {
  213. struct map *map = left->ms.map;
  214. left->srcline = get_srcline(map->dso,
  215. map__rip_2objdump(map, left->ip));
  216. }
  217. }
  218. if (!right->srcline) {
  219. if (!right->ms.map)
  220. right->srcline = SRCLINE_UNKNOWN;
  221. else {
  222. struct map *map = right->ms.map;
  223. right->srcline = get_srcline(map->dso,
  224. map__rip_2objdump(map, right->ip));
  225. }
  226. }
  227. return strcmp(left->srcline, right->srcline);
  228. }
  229. static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
  230. size_t size,
  231. unsigned int width __maybe_unused)
  232. {
  233. return repsep_snprintf(bf, size, "%s", he->srcline);
  234. }
  235. struct sort_entry sort_srcline = {
  236. .se_header = "Source:Line",
  237. .se_cmp = sort__srcline_cmp,
  238. .se_snprintf = hist_entry__srcline_snprintf,
  239. .se_width_idx = HISTC_SRCLINE,
  240. };
  241. /* --sort parent */
  242. static int64_t
  243. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  244. {
  245. struct symbol *sym_l = left->parent;
  246. struct symbol *sym_r = right->parent;
  247. if (!sym_l || !sym_r)
  248. return cmp_null(sym_l, sym_r);
  249. return strcmp(sym_l->name, sym_r->name);
  250. }
  251. static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
  252. size_t size, unsigned int width)
  253. {
  254. return repsep_snprintf(bf, size, "%-*s", width,
  255. he->parent ? he->parent->name : "[other]");
  256. }
  257. struct sort_entry sort_parent = {
  258. .se_header = "Parent symbol",
  259. .se_cmp = sort__parent_cmp,
  260. .se_snprintf = hist_entry__parent_snprintf,
  261. .se_width_idx = HISTC_PARENT,
  262. };
  263. /* --sort cpu */
  264. static int64_t
  265. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  266. {
  267. return right->cpu - left->cpu;
  268. }
  269. static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
  270. size_t size, unsigned int width)
  271. {
  272. return repsep_snprintf(bf, size, "%*d", width, he->cpu);
  273. }
  274. struct sort_entry sort_cpu = {
  275. .se_header = "CPU",
  276. .se_cmp = sort__cpu_cmp,
  277. .se_snprintf = hist_entry__cpu_snprintf,
  278. .se_width_idx = HISTC_CPU,
  279. };
  280. /* sort keys for branch stacks */
  281. static int64_t
  282. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  283. {
  284. return _sort__dso_cmp(left->branch_info->from.map,
  285. right->branch_info->from.map);
  286. }
  287. static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
  288. size_t size, unsigned int width)
  289. {
  290. return _hist_entry__dso_snprintf(he->branch_info->from.map,
  291. bf, size, width);
  292. }
  293. static int64_t
  294. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  295. {
  296. return _sort__dso_cmp(left->branch_info->to.map,
  297. right->branch_info->to.map);
  298. }
  299. static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
  300. size_t size, unsigned int width)
  301. {
  302. return _hist_entry__dso_snprintf(he->branch_info->to.map,
  303. bf, size, width);
  304. }
  305. static int64_t
  306. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  307. {
  308. struct addr_map_symbol *from_l = &left->branch_info->from;
  309. struct addr_map_symbol *from_r = &right->branch_info->from;
  310. if (!from_l->sym && !from_r->sym)
  311. return right->level - left->level;
  312. return _sort__sym_cmp(from_l->sym, from_r->sym);
  313. }
  314. static int64_t
  315. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  316. {
  317. struct addr_map_symbol *to_l = &left->branch_info->to;
  318. struct addr_map_symbol *to_r = &right->branch_info->to;
  319. if (!to_l->sym && !to_r->sym)
  320. return right->level - left->level;
  321. return _sort__sym_cmp(to_l->sym, to_r->sym);
  322. }
  323. static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
  324. size_t size, unsigned int width)
  325. {
  326. struct addr_map_symbol *from = &he->branch_info->from;
  327. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  328. he->level, bf, size, width);
  329. }
  330. static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
  331. size_t size, unsigned int width)
  332. {
  333. struct addr_map_symbol *to = &he->branch_info->to;
  334. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  335. he->level, bf, size, width);
  336. }
  337. struct sort_entry sort_dso_from = {
  338. .se_header = "Source Shared Object",
  339. .se_cmp = sort__dso_from_cmp,
  340. .se_snprintf = hist_entry__dso_from_snprintf,
  341. .se_width_idx = HISTC_DSO_FROM,
  342. };
  343. struct sort_entry sort_dso_to = {
  344. .se_header = "Target Shared Object",
  345. .se_cmp = sort__dso_to_cmp,
  346. .se_snprintf = hist_entry__dso_to_snprintf,
  347. .se_width_idx = HISTC_DSO_TO,
  348. };
  349. struct sort_entry sort_sym_from = {
  350. .se_header = "Source Symbol",
  351. .se_cmp = sort__sym_from_cmp,
  352. .se_snprintf = hist_entry__sym_from_snprintf,
  353. .se_width_idx = HISTC_SYMBOL_FROM,
  354. };
  355. struct sort_entry sort_sym_to = {
  356. .se_header = "Target Symbol",
  357. .se_cmp = sort__sym_to_cmp,
  358. .se_snprintf = hist_entry__sym_to_snprintf,
  359. .se_width_idx = HISTC_SYMBOL_TO,
  360. };
  361. static int64_t
  362. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  363. {
  364. const unsigned char mp = left->branch_info->flags.mispred !=
  365. right->branch_info->flags.mispred;
  366. const unsigned char p = left->branch_info->flags.predicted !=
  367. right->branch_info->flags.predicted;
  368. return mp || p;
  369. }
  370. static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
  371. size_t size, unsigned int width){
  372. static const char *out = "N/A";
  373. if (he->branch_info->flags.predicted)
  374. out = "N";
  375. else if (he->branch_info->flags.mispred)
  376. out = "Y";
  377. return repsep_snprintf(bf, size, "%-*s", width, out);
  378. }
  379. /* --sort daddr_sym */
  380. static int64_t
  381. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  382. {
  383. uint64_t l = 0, r = 0;
  384. if (left->mem_info)
  385. l = left->mem_info->daddr.addr;
  386. if (right->mem_info)
  387. r = right->mem_info->daddr.addr;
  388. return (int64_t)(r - l);
  389. }
  390. static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
  391. size_t size, unsigned int width)
  392. {
  393. uint64_t addr = 0;
  394. struct map *map = NULL;
  395. struct symbol *sym = NULL;
  396. if (he->mem_info) {
  397. addr = he->mem_info->daddr.addr;
  398. map = he->mem_info->daddr.map;
  399. sym = he->mem_info->daddr.sym;
  400. }
  401. return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
  402. width);
  403. }
  404. static int64_t
  405. sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  406. {
  407. struct map *map_l = NULL;
  408. struct map *map_r = NULL;
  409. if (left->mem_info)
  410. map_l = left->mem_info->daddr.map;
  411. if (right->mem_info)
  412. map_r = right->mem_info->daddr.map;
  413. return _sort__dso_cmp(map_l, map_r);
  414. }
  415. static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
  416. size_t size, unsigned int width)
  417. {
  418. struct map *map = NULL;
  419. if (he->mem_info)
  420. map = he->mem_info->daddr.map;
  421. return _hist_entry__dso_snprintf(map, bf, size, width);
  422. }
  423. static int64_t
  424. sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
  425. {
  426. union perf_mem_data_src data_src_l;
  427. union perf_mem_data_src data_src_r;
  428. if (left->mem_info)
  429. data_src_l = left->mem_info->data_src;
  430. else
  431. data_src_l.mem_lock = PERF_MEM_LOCK_NA;
  432. if (right->mem_info)
  433. data_src_r = right->mem_info->data_src;
  434. else
  435. data_src_r.mem_lock = PERF_MEM_LOCK_NA;
  436. return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
  437. }
  438. static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
  439. size_t size, unsigned int width)
  440. {
  441. const char *out;
  442. u64 mask = PERF_MEM_LOCK_NA;
  443. if (he->mem_info)
  444. mask = he->mem_info->data_src.mem_lock;
  445. if (mask & PERF_MEM_LOCK_NA)
  446. out = "N/A";
  447. else if (mask & PERF_MEM_LOCK_LOCKED)
  448. out = "Yes";
  449. else
  450. out = "No";
  451. return repsep_snprintf(bf, size, "%-*s", width, out);
  452. }
  453. static int64_t
  454. sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
  455. {
  456. union perf_mem_data_src data_src_l;
  457. union perf_mem_data_src data_src_r;
  458. if (left->mem_info)
  459. data_src_l = left->mem_info->data_src;
  460. else
  461. data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
  462. if (right->mem_info)
  463. data_src_r = right->mem_info->data_src;
  464. else
  465. data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
  466. return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
  467. }
  468. static const char * const tlb_access[] = {
  469. "N/A",
  470. "HIT",
  471. "MISS",
  472. "L1",
  473. "L2",
  474. "Walker",
  475. "Fault",
  476. };
  477. #define NUM_TLB_ACCESS (sizeof(tlb_access)/sizeof(const char *))
  478. static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
  479. size_t size, unsigned int width)
  480. {
  481. char out[64];
  482. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  483. size_t l = 0, i;
  484. u64 m = PERF_MEM_TLB_NA;
  485. u64 hit, miss;
  486. out[0] = '\0';
  487. if (he->mem_info)
  488. m = he->mem_info->data_src.mem_dtlb;
  489. hit = m & PERF_MEM_TLB_HIT;
  490. miss = m & PERF_MEM_TLB_MISS;
  491. /* already taken care of */
  492. m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
  493. for (i = 0; m && i < NUM_TLB_ACCESS; i++, m >>= 1) {
  494. if (!(m & 0x1))
  495. continue;
  496. if (l) {
  497. strcat(out, " or ");
  498. l += 4;
  499. }
  500. strncat(out, tlb_access[i], sz - l);
  501. l += strlen(tlb_access[i]);
  502. }
  503. if (*out == '\0')
  504. strcpy(out, "N/A");
  505. if (hit)
  506. strncat(out, " hit", sz - l);
  507. if (miss)
  508. strncat(out, " miss", sz - l);
  509. return repsep_snprintf(bf, size, "%-*s", width, out);
  510. }
  511. static int64_t
  512. sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
  513. {
  514. union perf_mem_data_src data_src_l;
  515. union perf_mem_data_src data_src_r;
  516. if (left->mem_info)
  517. data_src_l = left->mem_info->data_src;
  518. else
  519. data_src_l.mem_lvl = PERF_MEM_LVL_NA;
  520. if (right->mem_info)
  521. data_src_r = right->mem_info->data_src;
  522. else
  523. data_src_r.mem_lvl = PERF_MEM_LVL_NA;
  524. return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
  525. }
  526. static const char * const mem_lvl[] = {
  527. "N/A",
  528. "HIT",
  529. "MISS",
  530. "L1",
  531. "LFB",
  532. "L2",
  533. "L3",
  534. "Local RAM",
  535. "Remote RAM (1 hop)",
  536. "Remote RAM (2 hops)",
  537. "Remote Cache (1 hop)",
  538. "Remote Cache (2 hops)",
  539. "I/O",
  540. "Uncached",
  541. };
  542. #define NUM_MEM_LVL (sizeof(mem_lvl)/sizeof(const char *))
  543. static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
  544. size_t size, unsigned int width)
  545. {
  546. char out[64];
  547. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  548. size_t i, l = 0;
  549. u64 m = PERF_MEM_LVL_NA;
  550. u64 hit, miss;
  551. if (he->mem_info)
  552. m = he->mem_info->data_src.mem_lvl;
  553. out[0] = '\0';
  554. hit = m & PERF_MEM_LVL_HIT;
  555. miss = m & PERF_MEM_LVL_MISS;
  556. /* already taken care of */
  557. m &= ~(PERF_MEM_LVL_HIT|PERF_MEM_LVL_MISS);
  558. for (i = 0; m && i < NUM_MEM_LVL; i++, m >>= 1) {
  559. if (!(m & 0x1))
  560. continue;
  561. if (l) {
  562. strcat(out, " or ");
  563. l += 4;
  564. }
  565. strncat(out, mem_lvl[i], sz - l);
  566. l += strlen(mem_lvl[i]);
  567. }
  568. if (*out == '\0')
  569. strcpy(out, "N/A");
  570. if (hit)
  571. strncat(out, " hit", sz - l);
  572. if (miss)
  573. strncat(out, " miss", sz - l);
  574. return repsep_snprintf(bf, size, "%-*s", width, out);
  575. }
  576. static int64_t
  577. sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
  578. {
  579. union perf_mem_data_src data_src_l;
  580. union perf_mem_data_src data_src_r;
  581. if (left->mem_info)
  582. data_src_l = left->mem_info->data_src;
  583. else
  584. data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
  585. if (right->mem_info)
  586. data_src_r = right->mem_info->data_src;
  587. else
  588. data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
  589. return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
  590. }
  591. static const char * const snoop_access[] = {
  592. "N/A",
  593. "None",
  594. "Miss",
  595. "Hit",
  596. "HitM",
  597. };
  598. #define NUM_SNOOP_ACCESS (sizeof(snoop_access)/sizeof(const char *))
  599. static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
  600. size_t size, unsigned int width)
  601. {
  602. char out[64];
  603. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  604. size_t i, l = 0;
  605. u64 m = PERF_MEM_SNOOP_NA;
  606. out[0] = '\0';
  607. if (he->mem_info)
  608. m = he->mem_info->data_src.mem_snoop;
  609. for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
  610. if (!(m & 0x1))
  611. continue;
  612. if (l) {
  613. strcat(out, " or ");
  614. l += 4;
  615. }
  616. strncat(out, snoop_access[i], sz - l);
  617. l += strlen(snoop_access[i]);
  618. }
  619. if (*out == '\0')
  620. strcpy(out, "N/A");
  621. return repsep_snprintf(bf, size, "%-*s", width, out);
  622. }
  623. struct sort_entry sort_mispredict = {
  624. .se_header = "Branch Mispredicted",
  625. .se_cmp = sort__mispredict_cmp,
  626. .se_snprintf = hist_entry__mispredict_snprintf,
  627. .se_width_idx = HISTC_MISPREDICT,
  628. };
  629. static u64 he_weight(struct hist_entry *he)
  630. {
  631. return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
  632. }
  633. static int64_t
  634. sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  635. {
  636. return he_weight(left) - he_weight(right);
  637. }
  638. static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
  639. size_t size, unsigned int width)
  640. {
  641. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
  642. }
  643. struct sort_entry sort_local_weight = {
  644. .se_header = "Local Weight",
  645. .se_cmp = sort__local_weight_cmp,
  646. .se_snprintf = hist_entry__local_weight_snprintf,
  647. .se_width_idx = HISTC_LOCAL_WEIGHT,
  648. };
  649. static int64_t
  650. sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  651. {
  652. return left->stat.weight - right->stat.weight;
  653. }
  654. static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
  655. size_t size, unsigned int width)
  656. {
  657. return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
  658. }
  659. struct sort_entry sort_global_weight = {
  660. .se_header = "Weight",
  661. .se_cmp = sort__global_weight_cmp,
  662. .se_snprintf = hist_entry__global_weight_snprintf,
  663. .se_width_idx = HISTC_GLOBAL_WEIGHT,
  664. };
  665. struct sort_entry sort_mem_daddr_sym = {
  666. .se_header = "Data Symbol",
  667. .se_cmp = sort__daddr_cmp,
  668. .se_snprintf = hist_entry__daddr_snprintf,
  669. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  670. };
  671. struct sort_entry sort_mem_daddr_dso = {
  672. .se_header = "Data Object",
  673. .se_cmp = sort__dso_daddr_cmp,
  674. .se_snprintf = hist_entry__dso_daddr_snprintf,
  675. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  676. };
  677. struct sort_entry sort_mem_locked = {
  678. .se_header = "Locked",
  679. .se_cmp = sort__locked_cmp,
  680. .se_snprintf = hist_entry__locked_snprintf,
  681. .se_width_idx = HISTC_MEM_LOCKED,
  682. };
  683. struct sort_entry sort_mem_tlb = {
  684. .se_header = "TLB access",
  685. .se_cmp = sort__tlb_cmp,
  686. .se_snprintf = hist_entry__tlb_snprintf,
  687. .se_width_idx = HISTC_MEM_TLB,
  688. };
  689. struct sort_entry sort_mem_lvl = {
  690. .se_header = "Memory access",
  691. .se_cmp = sort__lvl_cmp,
  692. .se_snprintf = hist_entry__lvl_snprintf,
  693. .se_width_idx = HISTC_MEM_LVL,
  694. };
  695. struct sort_entry sort_mem_snoop = {
  696. .se_header = "Snoop",
  697. .se_cmp = sort__snoop_cmp,
  698. .se_snprintf = hist_entry__snoop_snprintf,
  699. .se_width_idx = HISTC_MEM_SNOOP,
  700. };
  701. static int64_t
  702. sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
  703. {
  704. return left->branch_info->flags.abort !=
  705. right->branch_info->flags.abort;
  706. }
  707. static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
  708. size_t size, unsigned int width)
  709. {
  710. static const char *out = ".";
  711. if (he->branch_info->flags.abort)
  712. out = "A";
  713. return repsep_snprintf(bf, size, "%-*s", width, out);
  714. }
  715. struct sort_entry sort_abort = {
  716. .se_header = "Transaction abort",
  717. .se_cmp = sort__abort_cmp,
  718. .se_snprintf = hist_entry__abort_snprintf,
  719. .se_width_idx = HISTC_ABORT,
  720. };
  721. static int64_t
  722. sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
  723. {
  724. return left->branch_info->flags.in_tx !=
  725. right->branch_info->flags.in_tx;
  726. }
  727. static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
  728. size_t size, unsigned int width)
  729. {
  730. static const char *out = ".";
  731. if (he->branch_info->flags.in_tx)
  732. out = "T";
  733. return repsep_snprintf(bf, size, "%-*s", width, out);
  734. }
  735. struct sort_entry sort_in_tx = {
  736. .se_header = "Branch in transaction",
  737. .se_cmp = sort__in_tx_cmp,
  738. .se_snprintf = hist_entry__in_tx_snprintf,
  739. .se_width_idx = HISTC_IN_TX,
  740. };
  741. static int64_t
  742. sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
  743. {
  744. return left->transaction - right->transaction;
  745. }
  746. static inline char *add_str(char *p, const char *str)
  747. {
  748. strcpy(p, str);
  749. return p + strlen(str);
  750. }
  751. static struct txbit {
  752. unsigned flag;
  753. const char *name;
  754. int skip_for_len;
  755. } txbits[] = {
  756. { PERF_TXN_ELISION, "EL ", 0 },
  757. { PERF_TXN_TRANSACTION, "TX ", 1 },
  758. { PERF_TXN_SYNC, "SYNC ", 1 },
  759. { PERF_TXN_ASYNC, "ASYNC ", 0 },
  760. { PERF_TXN_RETRY, "RETRY ", 0 },
  761. { PERF_TXN_CONFLICT, "CON ", 0 },
  762. { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
  763. { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
  764. { 0, NULL, 0 }
  765. };
  766. int hist_entry__transaction_len(void)
  767. {
  768. int i;
  769. int len = 0;
  770. for (i = 0; txbits[i].name; i++) {
  771. if (!txbits[i].skip_for_len)
  772. len += strlen(txbits[i].name);
  773. }
  774. len += 4; /* :XX<space> */
  775. return len;
  776. }
  777. static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
  778. size_t size, unsigned int width)
  779. {
  780. u64 t = he->transaction;
  781. char buf[128];
  782. char *p = buf;
  783. int i;
  784. buf[0] = 0;
  785. for (i = 0; txbits[i].name; i++)
  786. if (txbits[i].flag & t)
  787. p = add_str(p, txbits[i].name);
  788. if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
  789. p = add_str(p, "NEITHER ");
  790. if (t & PERF_TXN_ABORT_MASK) {
  791. sprintf(p, ":%" PRIx64,
  792. (t & PERF_TXN_ABORT_MASK) >>
  793. PERF_TXN_ABORT_SHIFT);
  794. p += strlen(p);
  795. }
  796. return repsep_snprintf(bf, size, "%-*s", width, buf);
  797. }
  798. struct sort_entry sort_transaction = {
  799. .se_header = "Transaction ",
  800. .se_cmp = sort__transaction_cmp,
  801. .se_snprintf = hist_entry__transaction_snprintf,
  802. .se_width_idx = HISTC_TRANSACTION,
  803. };
  804. struct sort_dimension {
  805. const char *name;
  806. struct sort_entry *entry;
  807. int taken;
  808. };
  809. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  810. static struct sort_dimension common_sort_dimensions[] = {
  811. DIM(SORT_PID, "pid", sort_thread),
  812. DIM(SORT_COMM, "comm", sort_comm),
  813. DIM(SORT_DSO, "dso", sort_dso),
  814. DIM(SORT_SYM, "symbol", sort_sym),
  815. DIM(SORT_PARENT, "parent", sort_parent),
  816. DIM(SORT_CPU, "cpu", sort_cpu),
  817. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  818. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  819. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  820. DIM(SORT_TRANSACTION, "transaction", sort_transaction),
  821. };
  822. #undef DIM
  823. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  824. static struct sort_dimension bstack_sort_dimensions[] = {
  825. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  826. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  827. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  828. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  829. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  830. DIM(SORT_IN_TX, "in_tx", sort_in_tx),
  831. DIM(SORT_ABORT, "abort", sort_abort),
  832. };
  833. #undef DIM
  834. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  835. static struct sort_dimension memory_sort_dimensions[] = {
  836. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  837. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  838. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  839. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  840. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  841. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  842. };
  843. #undef DIM
  844. static void __sort_dimension__add(struct sort_dimension *sd, enum sort_type idx)
  845. {
  846. if (sd->taken)
  847. return;
  848. if (sd->entry->se_collapse)
  849. sort__need_collapse = 1;
  850. if (list_empty(&hist_entry__sort_list))
  851. sort__first_dimension = idx;
  852. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  853. sd->taken = 1;
  854. }
  855. int sort_dimension__add(const char *tok)
  856. {
  857. unsigned int i;
  858. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  859. struct sort_dimension *sd = &common_sort_dimensions[i];
  860. if (strncasecmp(tok, sd->name, strlen(tok)))
  861. continue;
  862. if (sd->entry == &sort_parent) {
  863. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  864. if (ret) {
  865. char err[BUFSIZ];
  866. regerror(ret, &parent_regex, err, sizeof(err));
  867. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  868. return -EINVAL;
  869. }
  870. sort__has_parent = 1;
  871. } else if (sd->entry == &sort_sym) {
  872. sort__has_sym = 1;
  873. }
  874. __sort_dimension__add(sd, i);
  875. return 0;
  876. }
  877. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  878. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  879. if (strncasecmp(tok, sd->name, strlen(tok)))
  880. continue;
  881. if (sort__mode != SORT_MODE__BRANCH)
  882. return -EINVAL;
  883. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  884. sort__has_sym = 1;
  885. __sort_dimension__add(sd, i + __SORT_BRANCH_STACK);
  886. return 0;
  887. }
  888. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  889. struct sort_dimension *sd = &memory_sort_dimensions[i];
  890. if (strncasecmp(tok, sd->name, strlen(tok)))
  891. continue;
  892. if (sort__mode != SORT_MODE__MEMORY)
  893. return -EINVAL;
  894. if (sd->entry == &sort_mem_daddr_sym)
  895. sort__has_sym = 1;
  896. __sort_dimension__add(sd, i + __SORT_MEMORY_MODE);
  897. return 0;
  898. }
  899. return -ESRCH;
  900. }
  901. int setup_sorting(void)
  902. {
  903. char *tmp, *tok, *str = strdup(sort_order);
  904. int ret = 0;
  905. if (str == NULL) {
  906. error("Not enough memory to setup sort keys");
  907. return -ENOMEM;
  908. }
  909. for (tok = strtok_r(str, ", ", &tmp);
  910. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  911. ret = sort_dimension__add(tok);
  912. if (ret == -EINVAL) {
  913. error("Invalid --sort key: `%s'", tok);
  914. break;
  915. } else if (ret == -ESRCH) {
  916. error("Unknown --sort key: `%s'", tok);
  917. break;
  918. }
  919. }
  920. free(str);
  921. return ret;
  922. }
  923. static void sort_entry__setup_elide(struct sort_entry *se,
  924. struct strlist *list,
  925. const char *list_name, FILE *fp)
  926. {
  927. if (list && strlist__nr_entries(list) == 1) {
  928. if (fp != NULL)
  929. fprintf(fp, "# %s: %s\n", list_name,
  930. strlist__entry(list, 0)->s);
  931. se->elide = true;
  932. }
  933. }
  934. void sort__setup_elide(FILE *output)
  935. {
  936. struct sort_entry *se;
  937. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  938. "dso", output);
  939. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list,
  940. "comm", output);
  941. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list,
  942. "symbol", output);
  943. if (sort__mode == SORT_MODE__BRANCH) {
  944. sort_entry__setup_elide(&sort_dso_from,
  945. symbol_conf.dso_from_list,
  946. "dso_from", output);
  947. sort_entry__setup_elide(&sort_dso_to,
  948. symbol_conf.dso_to_list,
  949. "dso_to", output);
  950. sort_entry__setup_elide(&sort_sym_from,
  951. symbol_conf.sym_from_list,
  952. "sym_from", output);
  953. sort_entry__setup_elide(&sort_sym_to,
  954. symbol_conf.sym_to_list,
  955. "sym_to", output);
  956. } else if (sort__mode == SORT_MODE__MEMORY) {
  957. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  958. "symbol_daddr", output);
  959. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  960. "dso_daddr", output);
  961. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  962. "mem", output);
  963. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  964. "local_weight", output);
  965. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  966. "tlb", output);
  967. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  968. "snoop", output);
  969. }
  970. /*
  971. * It makes no sense to elide all of sort entries.
  972. * Just revert them to show up again.
  973. */
  974. list_for_each_entry(se, &hist_entry__sort_list, list) {
  975. if (!se->elide)
  976. return;
  977. }
  978. list_for_each_entry(se, &hist_entry__sort_list, list)
  979. se->elide = false;
  980. }