sort.c 28 KB

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