sort.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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(void *l, 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 *self, char *bf,
  53. size_t size, unsigned int width)
  54. {
  55. return repsep_snprintf(bf, size, "%*s:%5d", width - 6,
  56. self->thread->comm ?: "", self->thread->tid);
  57. }
  58. struct sort_entry sort_thread = {
  59. .se_header = "Command: Pid",
  60. .se_cmp = sort__thread_cmp,
  61. .se_snprintf = hist_entry__thread_snprintf,
  62. .se_width_idx = HISTC_THREAD,
  63. };
  64. /* --sort comm */
  65. static int64_t
  66. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  67. {
  68. return right->thread->tid - left->thread->tid;
  69. }
  70. static int64_t
  71. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  72. {
  73. char *comm_l = left->thread->comm;
  74. char *comm_r = right->thread->comm;
  75. if (!comm_l || !comm_r)
  76. return cmp_null(comm_l, comm_r);
  77. return strcmp(comm_l, comm_r);
  78. }
  79. static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
  80. size_t size, unsigned int width)
  81. {
  82. return repsep_snprintf(bf, size, "%*s", width, self->thread->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 *self, char *bf,
  124. size_t size, unsigned int width)
  125. {
  126. return _hist_entry__dso_snprintf(self->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. if (!left->ms.sym && !right->ms.sym)
  150. return right->level - left->level;
  151. return _sort__sym_cmp(left->ms.sym, right->ms.sym);
  152. }
  153. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  154. u64 ip, char level, char *bf, size_t size,
  155. unsigned int width)
  156. {
  157. size_t ret = 0;
  158. if (verbose) {
  159. char o = map ? dso__symtab_origin(map->dso) : '!';
  160. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  161. BITS_PER_LONG / 4 + 2, ip, o);
  162. }
  163. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  164. if (sym && map) {
  165. if (map->type == MAP__VARIABLE) {
  166. ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
  167. ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
  168. ip - map->unmap_ip(map, sym->start));
  169. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  170. width - ret, "");
  171. } else {
  172. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  173. width - ret,
  174. sym->name);
  175. }
  176. } else {
  177. size_t len = BITS_PER_LONG / 4;
  178. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  179. len, ip);
  180. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  181. width - ret, "");
  182. }
  183. return ret;
  184. }
  185. static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
  186. size_t size, unsigned int width)
  187. {
  188. return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
  189. self->level, bf, size, width);
  190. }
  191. struct sort_entry sort_sym = {
  192. .se_header = "Symbol",
  193. .se_cmp = sort__sym_cmp,
  194. .se_snprintf = hist_entry__sym_snprintf,
  195. .se_width_idx = HISTC_SYMBOL,
  196. };
  197. /* --sort srcline */
  198. static int64_t
  199. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  200. {
  201. return (int64_t)(right->ip - left->ip);
  202. }
  203. static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
  204. size_t size,
  205. unsigned int width __maybe_unused)
  206. {
  207. FILE *fp = NULL;
  208. char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
  209. size_t line_len;
  210. if (path != NULL)
  211. goto out_path;
  212. if (!self->ms.map)
  213. goto out_ip;
  214. if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
  215. goto out_ip;
  216. snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
  217. self->ms.map->dso->long_name, self->ip);
  218. fp = popen(cmd, "r");
  219. if (!fp)
  220. goto out_ip;
  221. if (getline(&path, &line_len, fp) < 0 || !line_len)
  222. goto out_ip;
  223. self->srcline = strdup(path);
  224. if (self->srcline == NULL)
  225. goto out_ip;
  226. nl = strchr(self->srcline, '\n');
  227. if (nl != NULL)
  228. *nl = '\0';
  229. path = self->srcline;
  230. out_path:
  231. if (fp)
  232. pclose(fp);
  233. return repsep_snprintf(bf, size, "%s", path);
  234. out_ip:
  235. if (fp)
  236. pclose(fp);
  237. return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
  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 *self, char *bf,
  256. size_t size, unsigned int width)
  257. {
  258. return repsep_snprintf(bf, size, "%-*s", width,
  259. self->parent ? self->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 *self, char *bf,
  274. size_t size, unsigned int width)
  275. {
  276. return repsep_snprintf(bf, size, "%*d", width, self->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 *self, char *bf,
  292. size_t size, unsigned int width)
  293. {
  294. return _hist_entry__dso_snprintf(self->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 *self, char *bf,
  304. size_t size, unsigned int width)
  305. {
  306. return _hist_entry__dso_snprintf(self->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 right->level - left->level;
  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 right->level - left->level;
  325. return _sort__sym_cmp(to_l->sym, to_r->sym);
  326. }
  327. static int hist_entry__sym_from_snprintf(struct hist_entry *self, char *bf,
  328. size_t size, unsigned int width)
  329. {
  330. struct addr_map_symbol *from = &self->branch_info->from;
  331. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  332. self->level, bf, size, width);
  333. }
  334. static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
  335. size_t size, unsigned int width)
  336. {
  337. struct addr_map_symbol *to = &self->branch_info->to;
  338. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  339. self->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 *self, char *bf,
  375. size_t size, unsigned int width){
  376. static const char *out = "N/A";
  377. if (self->branch_info->flags.predicted)
  378. out = "N";
  379. else if (self->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 *self, 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 (self->mem_info) {
  401. addr = self->mem_info->daddr.addr;
  402. map = self->mem_info->daddr.map;
  403. sym = self->mem_info->daddr.sym;
  404. }
  405. return _hist_entry__sym_snprintf(map, sym, addr, self->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 *self, char *bf,
  420. size_t size, unsigned int width)
  421. {
  422. struct map *map = NULL;
  423. if (self->mem_info)
  424. map = self->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 *self, char *bf,
  443. size_t size, unsigned int width)
  444. {
  445. const char *out;
  446. u64 mask = PERF_MEM_LOCK_NA;
  447. if (self->mem_info)
  448. mask = self->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 *self, 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 (self->mem_info)
  492. m = self->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 *self, 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 (self->mem_info)
  556. m = self->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 *self, 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 (self->mem_info)
  612. m = self->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 *self, char *bf,
  643. size_t size, unsigned int width)
  644. {
  645. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(self));
  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 *self, char *bf,
  659. size_t size, unsigned int width)
  660. {
  661. return repsep_snprintf(bf, size, "%-*llu", width, self->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. struct sort_dimension {
  706. const char *name;
  707. struct sort_entry *entry;
  708. int taken;
  709. };
  710. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  711. static struct sort_dimension common_sort_dimensions[] = {
  712. DIM(SORT_PID, "pid", sort_thread),
  713. DIM(SORT_COMM, "comm", sort_comm),
  714. DIM(SORT_DSO, "dso", sort_dso),
  715. DIM(SORT_SYM, "symbol", sort_sym),
  716. DIM(SORT_PARENT, "parent", sort_parent),
  717. DIM(SORT_CPU, "cpu", sort_cpu),
  718. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  719. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  720. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  721. };
  722. #undef DIM
  723. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  724. static struct sort_dimension bstack_sort_dimensions[] = {
  725. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  726. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  727. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  728. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  729. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  730. };
  731. #undef DIM
  732. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  733. static struct sort_dimension memory_sort_dimensions[] = {
  734. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  735. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  736. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  737. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  738. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  739. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  740. };
  741. #undef DIM
  742. static void __sort_dimension__add(struct sort_dimension *sd, enum sort_type idx)
  743. {
  744. if (sd->taken)
  745. return;
  746. if (sd->entry->se_collapse)
  747. sort__need_collapse = 1;
  748. if (list_empty(&hist_entry__sort_list))
  749. sort__first_dimension = idx;
  750. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  751. sd->taken = 1;
  752. }
  753. int sort_dimension__add(const char *tok)
  754. {
  755. unsigned int i;
  756. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  757. struct sort_dimension *sd = &common_sort_dimensions[i];
  758. if (strncasecmp(tok, sd->name, strlen(tok)))
  759. continue;
  760. if (sd->entry == &sort_parent) {
  761. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  762. if (ret) {
  763. char err[BUFSIZ];
  764. regerror(ret, &parent_regex, err, sizeof(err));
  765. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  766. return -EINVAL;
  767. }
  768. sort__has_parent = 1;
  769. } else if (sd->entry == &sort_sym) {
  770. sort__has_sym = 1;
  771. }
  772. __sort_dimension__add(sd, i);
  773. return 0;
  774. }
  775. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  776. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  777. if (strncasecmp(tok, sd->name, strlen(tok)))
  778. continue;
  779. if (sort__mode != SORT_MODE__BRANCH)
  780. return -EINVAL;
  781. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  782. sort__has_sym = 1;
  783. __sort_dimension__add(sd, i + __SORT_BRANCH_STACK);
  784. return 0;
  785. }
  786. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  787. struct sort_dimension *sd = &memory_sort_dimensions[i];
  788. if (strncasecmp(tok, sd->name, strlen(tok)))
  789. continue;
  790. if (sort__mode != SORT_MODE__MEMORY)
  791. return -EINVAL;
  792. if (sd->entry == &sort_mem_daddr_sym)
  793. sort__has_sym = 1;
  794. __sort_dimension__add(sd, i + __SORT_MEMORY_MODE);
  795. return 0;
  796. }
  797. return -ESRCH;
  798. }
  799. int setup_sorting(void)
  800. {
  801. char *tmp, *tok, *str = strdup(sort_order);
  802. int ret = 0;
  803. if (str == NULL) {
  804. error("Not enough memory to setup sort keys");
  805. return -ENOMEM;
  806. }
  807. for (tok = strtok_r(str, ", ", &tmp);
  808. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  809. ret = sort_dimension__add(tok);
  810. if (ret == -EINVAL) {
  811. error("Invalid --sort key: `%s'", tok);
  812. break;
  813. } else if (ret == -ESRCH) {
  814. error("Unknown --sort key: `%s'", tok);
  815. break;
  816. }
  817. }
  818. free(str);
  819. return ret;
  820. }
  821. static void sort_entry__setup_elide(struct sort_entry *self,
  822. struct strlist *list,
  823. const char *list_name, FILE *fp)
  824. {
  825. if (list && strlist__nr_entries(list) == 1) {
  826. if (fp != NULL)
  827. fprintf(fp, "# %s: %s\n", list_name,
  828. strlist__entry(list, 0)->s);
  829. self->elide = true;
  830. }
  831. }
  832. void sort__setup_elide(FILE *output)
  833. {
  834. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  835. "dso", output);
  836. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list,
  837. "comm", output);
  838. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list,
  839. "symbol", output);
  840. if (sort__mode == SORT_MODE__BRANCH) {
  841. sort_entry__setup_elide(&sort_dso_from,
  842. symbol_conf.dso_from_list,
  843. "dso_from", output);
  844. sort_entry__setup_elide(&sort_dso_to,
  845. symbol_conf.dso_to_list,
  846. "dso_to", output);
  847. sort_entry__setup_elide(&sort_sym_from,
  848. symbol_conf.sym_from_list,
  849. "sym_from", output);
  850. sort_entry__setup_elide(&sort_sym_to,
  851. symbol_conf.sym_to_list,
  852. "sym_to", output);
  853. } else if (sort__mode == SORT_MODE__MEMORY) {
  854. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  855. "symbol_daddr", output);
  856. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  857. "dso_daddr", output);
  858. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  859. "mem", output);
  860. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  861. "local_weight", output);
  862. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  863. "tlb", output);
  864. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  865. "snoop", output);
  866. }
  867. }