sort.c 28 KB

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