sort.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. int sort__need_collapse = 0;
  10. int sort__has_parent = 0;
  11. int sort__has_sym = 0;
  12. enum sort_mode sort__mode = SORT_MODE__NORMAL;
  13. enum sort_type sort__first_dimension;
  14. LIST_HEAD(hist_entry__sort_list);
  15. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  16. {
  17. int n;
  18. va_list ap;
  19. va_start(ap, fmt);
  20. n = vsnprintf(bf, size, fmt, ap);
  21. if (symbol_conf.field_sep && n > 0) {
  22. char *sep = bf;
  23. while (1) {
  24. sep = strchr(sep, *symbol_conf.field_sep);
  25. if (sep == NULL)
  26. break;
  27. *sep = '.';
  28. }
  29. }
  30. va_end(ap);
  31. if (n >= (int)size)
  32. return size - 1;
  33. return n;
  34. }
  35. static int64_t cmp_null(void *l, void *r)
  36. {
  37. if (!l && !r)
  38. return 0;
  39. else if (!l)
  40. return -1;
  41. else
  42. return 1;
  43. }
  44. /* --sort pid */
  45. static int64_t
  46. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  47. {
  48. return right->thread->pid - left->thread->pid;
  49. }
  50. static int hist_entry__thread_snprintf(struct hist_entry *self, char *bf,
  51. size_t size, unsigned int width)
  52. {
  53. return repsep_snprintf(bf, size, "%*s:%5d", width - 6,
  54. self->thread->comm ?: "", self->thread->pid);
  55. }
  56. struct sort_entry sort_thread = {
  57. .se_header = "Command: Pid",
  58. .se_cmp = sort__thread_cmp,
  59. .se_snprintf = hist_entry__thread_snprintf,
  60. .se_width_idx = HISTC_THREAD,
  61. };
  62. /* --sort comm */
  63. static int64_t
  64. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  65. {
  66. return right->thread->pid - left->thread->pid;
  67. }
  68. static int64_t
  69. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  70. {
  71. char *comm_l = left->thread->comm;
  72. char *comm_r = right->thread->comm;
  73. if (!comm_l || !comm_r)
  74. return cmp_null(comm_l, comm_r);
  75. return strcmp(comm_l, comm_r);
  76. }
  77. static int hist_entry__comm_snprintf(struct hist_entry *self, char *bf,
  78. size_t size, unsigned int width)
  79. {
  80. return repsep_snprintf(bf, size, "%*s", width, self->thread->comm);
  81. }
  82. struct sort_entry sort_comm = {
  83. .se_header = "Command",
  84. .se_cmp = sort__comm_cmp,
  85. .se_collapse = sort__comm_collapse,
  86. .se_snprintf = hist_entry__comm_snprintf,
  87. .se_width_idx = HISTC_COMM,
  88. };
  89. /* --sort dso */
  90. static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
  91. {
  92. struct dso *dso_l = map_l ? map_l->dso : NULL;
  93. struct dso *dso_r = map_r ? map_r->dso : NULL;
  94. const char *dso_name_l, *dso_name_r;
  95. if (!dso_l || !dso_r)
  96. return cmp_null(dso_l, dso_r);
  97. if (verbose) {
  98. dso_name_l = dso_l->long_name;
  99. dso_name_r = dso_r->long_name;
  100. } else {
  101. dso_name_l = dso_l->short_name;
  102. dso_name_r = dso_r->short_name;
  103. }
  104. return strcmp(dso_name_l, dso_name_r);
  105. }
  106. static int64_t
  107. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  108. {
  109. return _sort__dso_cmp(left->ms.map, right->ms.map);
  110. }
  111. static int _hist_entry__dso_snprintf(struct map *map, char *bf,
  112. size_t size, unsigned int width)
  113. {
  114. if (map && map->dso) {
  115. const char *dso_name = !verbose ? map->dso->short_name :
  116. map->dso->long_name;
  117. return repsep_snprintf(bf, size, "%-*s", width, dso_name);
  118. }
  119. return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
  120. }
  121. static int hist_entry__dso_snprintf(struct hist_entry *self, char *bf,
  122. size_t size, unsigned int width)
  123. {
  124. return _hist_entry__dso_snprintf(self->ms.map, bf, size, width);
  125. }
  126. struct sort_entry sort_dso = {
  127. .se_header = "Shared Object",
  128. .se_cmp = sort__dso_cmp,
  129. .se_snprintf = hist_entry__dso_snprintf,
  130. .se_width_idx = HISTC_DSO,
  131. };
  132. /* --sort symbol */
  133. static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
  134. {
  135. u64 ip_l, ip_r;
  136. if (!sym_l || !sym_r)
  137. return cmp_null(sym_l, sym_r);
  138. if (sym_l == sym_r)
  139. return 0;
  140. ip_l = sym_l->start;
  141. ip_r = sym_r->start;
  142. return (int64_t)(ip_r - ip_l);
  143. }
  144. static int64_t
  145. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  146. {
  147. if (!left->ms.sym && !right->ms.sym)
  148. return right->level - left->level;
  149. return _sort__sym_cmp(left->ms.sym, right->ms.sym);
  150. }
  151. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  152. u64 ip, char level, char *bf, size_t size,
  153. unsigned int width)
  154. {
  155. size_t ret = 0;
  156. if (verbose) {
  157. char o = map ? dso__symtab_origin(map->dso) : '!';
  158. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  159. BITS_PER_LONG / 4 + 2, ip, o);
  160. }
  161. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  162. if (sym && map) {
  163. if (map->type == MAP__VARIABLE) {
  164. ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
  165. ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
  166. ip - map->unmap_ip(map, sym->start));
  167. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  168. width - ret, "");
  169. } else {
  170. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  171. width - ret,
  172. sym->name);
  173. }
  174. } else {
  175. size_t len = BITS_PER_LONG / 4;
  176. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  177. len, ip);
  178. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  179. width - ret, "");
  180. }
  181. return ret;
  182. }
  183. static int hist_entry__sym_snprintf(struct hist_entry *self, char *bf,
  184. size_t size, unsigned int width)
  185. {
  186. return _hist_entry__sym_snprintf(self->ms.map, self->ms.sym, self->ip,
  187. self->level, bf, size, width);
  188. }
  189. struct sort_entry sort_sym = {
  190. .se_header = "Symbol",
  191. .se_cmp = sort__sym_cmp,
  192. .se_snprintf = hist_entry__sym_snprintf,
  193. .se_width_idx = HISTC_SYMBOL,
  194. };
  195. /* --sort srcline */
  196. static int64_t
  197. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  198. {
  199. return (int64_t)(right->ip - left->ip);
  200. }
  201. static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
  202. size_t size,
  203. unsigned int width __maybe_unused)
  204. {
  205. FILE *fp = NULL;
  206. char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
  207. size_t line_len;
  208. if (path != NULL)
  209. goto out_path;
  210. if (!self->ms.map)
  211. goto out_ip;
  212. if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
  213. goto out_ip;
  214. snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
  215. self->ms.map->dso->long_name, self->ip);
  216. fp = popen(cmd, "r");
  217. if (!fp)
  218. goto out_ip;
  219. if (getline(&path, &line_len, fp) < 0 || !line_len)
  220. goto out_ip;
  221. self->srcline = strdup(path);
  222. if (self->srcline == NULL)
  223. goto out_ip;
  224. nl = strchr(self->srcline, '\n');
  225. if (nl != NULL)
  226. *nl = '\0';
  227. path = self->srcline;
  228. out_path:
  229. if (fp)
  230. pclose(fp);
  231. return repsep_snprintf(bf, size, "%s", path);
  232. out_ip:
  233. if (fp)
  234. pclose(fp);
  235. return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
  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 *self, char *bf,
  254. size_t size, unsigned int width)
  255. {
  256. return repsep_snprintf(bf, size, "%-*s", width,
  257. self->parent ? self->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 *self, char *bf,
  272. size_t size, unsigned int width)
  273. {
  274. return repsep_snprintf(bf, size, "%*d", width, self->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 *self, char *bf,
  290. size_t size, unsigned int width)
  291. {
  292. return _hist_entry__dso_snprintf(self->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 *self, char *bf,
  302. size_t size, unsigned int width)
  303. {
  304. return _hist_entry__dso_snprintf(self->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 *self, char *bf,
  326. size_t size, unsigned int width)
  327. {
  328. struct addr_map_symbol *from = &self->branch_info->from;
  329. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  330. self->level, bf, size, width);
  331. }
  332. static int hist_entry__sym_to_snprintf(struct hist_entry *self, char *bf,
  333. size_t size, unsigned int width)
  334. {
  335. struct addr_map_symbol *to = &self->branch_info->to;
  336. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  337. self->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 *self, char *bf,
  373. size_t size, unsigned int width){
  374. static const char *out = "N/A";
  375. if (self->branch_info->flags.predicted)
  376. out = "N";
  377. else if (self->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 *self, 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 (self->mem_info) {
  399. addr = self->mem_info->daddr.addr;
  400. map = self->mem_info->daddr.map;
  401. sym = self->mem_info->daddr.sym;
  402. }
  403. return _hist_entry__sym_snprintf(map, sym, addr, self->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 *self, char *bf,
  418. size_t size, unsigned int width)
  419. {
  420. struct map *map = NULL;
  421. if (self->mem_info)
  422. map = self->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 *self, char *bf,
  441. size_t size, unsigned int width)
  442. {
  443. const char *out;
  444. u64 mask = PERF_MEM_LOCK_NA;
  445. if (self->mem_info)
  446. mask = self->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 *self, 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 (self->mem_info)
  490. m = self->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 *self, 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 (self->mem_info)
  554. m = self->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 *self, 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 (self->mem_info)
  610. m = self->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 *self, char *bf,
  641. size_t size, unsigned int width)
  642. {
  643. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(self));
  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 *self, char *bf,
  657. size_t size, unsigned int width)
  658. {
  659. return repsep_snprintf(bf, size, "%-*llu", width, self->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. struct sort_dimension {
  704. const char *name;
  705. struct sort_entry *entry;
  706. int taken;
  707. };
  708. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  709. static struct sort_dimension common_sort_dimensions[] = {
  710. DIM(SORT_PID, "pid", sort_thread),
  711. DIM(SORT_COMM, "comm", sort_comm),
  712. DIM(SORT_DSO, "dso", sort_dso),
  713. DIM(SORT_SYM, "symbol", sort_sym),
  714. DIM(SORT_PARENT, "parent", sort_parent),
  715. DIM(SORT_CPU, "cpu", sort_cpu),
  716. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  717. };
  718. #undef DIM
  719. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  720. static struct sort_dimension bstack_sort_dimensions[] = {
  721. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  722. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  723. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  724. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  725. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  726. };
  727. #undef DIM
  728. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  729. static struct sort_dimension memory_sort_dimensions[] = {
  730. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  731. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  732. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  733. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  734. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  735. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  736. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  737. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  738. };
  739. #undef DIM
  740. static void __sort_dimension__add(struct sort_dimension *sd, enum sort_type idx)
  741. {
  742. if (sd->taken)
  743. return;
  744. if (sd->entry->se_collapse)
  745. sort__need_collapse = 1;
  746. if (list_empty(&hist_entry__sort_list))
  747. sort__first_dimension = idx;
  748. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  749. sd->taken = 1;
  750. }
  751. int sort_dimension__add(const char *tok)
  752. {
  753. unsigned int i;
  754. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  755. struct sort_dimension *sd = &common_sort_dimensions[i];
  756. if (strncasecmp(tok, sd->name, strlen(tok)))
  757. continue;
  758. if (sd->entry == &sort_parent) {
  759. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  760. if (ret) {
  761. char err[BUFSIZ];
  762. regerror(ret, &parent_regex, err, sizeof(err));
  763. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  764. return -EINVAL;
  765. }
  766. sort__has_parent = 1;
  767. } else if (sd->entry == &sort_sym) {
  768. sort__has_sym = 1;
  769. }
  770. __sort_dimension__add(sd, i);
  771. return 0;
  772. }
  773. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  774. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  775. if (strncasecmp(tok, sd->name, strlen(tok)))
  776. continue;
  777. if (sort__mode != SORT_MODE__BRANCH)
  778. return -EINVAL;
  779. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  780. sort__has_sym = 1;
  781. __sort_dimension__add(sd, i + __SORT_BRANCH_STACK);
  782. return 0;
  783. }
  784. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  785. struct sort_dimension *sd = &memory_sort_dimensions[i];
  786. if (strncasecmp(tok, sd->name, strlen(tok)))
  787. continue;
  788. if (sort__mode != SORT_MODE__MEMORY)
  789. return -EINVAL;
  790. if (sd->entry == &sort_mem_daddr_sym)
  791. sort__has_sym = 1;
  792. __sort_dimension__add(sd, i + __SORT_MEMORY_MODE);
  793. return 0;
  794. }
  795. return -ESRCH;
  796. }
  797. int setup_sorting(void)
  798. {
  799. char *tmp, *tok, *str = strdup(sort_order);
  800. int ret = 0;
  801. if (str == NULL) {
  802. error("Not enough memory to setup sort keys");
  803. return -ENOMEM;
  804. }
  805. for (tok = strtok_r(str, ", ", &tmp);
  806. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  807. ret = sort_dimension__add(tok);
  808. if (ret == -EINVAL) {
  809. error("Invalid --sort key: `%s'", tok);
  810. break;
  811. } else if (ret == -ESRCH) {
  812. error("Unknown --sort key: `%s'", tok);
  813. break;
  814. }
  815. }
  816. free(str);
  817. return ret;
  818. }
  819. static void sort_entry__setup_elide(struct sort_entry *self,
  820. struct strlist *list,
  821. const char *list_name, FILE *fp)
  822. {
  823. if (list && strlist__nr_entries(list) == 1) {
  824. if (fp != NULL)
  825. fprintf(fp, "# %s: %s\n", list_name,
  826. strlist__entry(list, 0)->s);
  827. self->elide = true;
  828. }
  829. }
  830. void sort__setup_elide(FILE *output)
  831. {
  832. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  833. "dso", output);
  834. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list,
  835. "comm", output);
  836. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list,
  837. "symbol", output);
  838. if (sort__mode == SORT_MODE__BRANCH) {
  839. sort_entry__setup_elide(&sort_dso_from,
  840. symbol_conf.dso_from_list,
  841. "dso_from", output);
  842. sort_entry__setup_elide(&sort_dso_to,
  843. symbol_conf.dso_to_list,
  844. "dso_to", output);
  845. sort_entry__setup_elide(&sort_sym_from,
  846. symbol_conf.sym_from_list,
  847. "sym_from", output);
  848. sort_entry__setup_elide(&sort_sym_to,
  849. symbol_conf.sym_to_list,
  850. "sym_to", output);
  851. } else if (sort__mode == SORT_MODE__MEMORY) {
  852. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  853. "symbol_daddr", output);
  854. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  855. "dso_daddr", output);
  856. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  857. "mem", output);
  858. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  859. "local_weight", output);
  860. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  861. "tlb", output);
  862. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  863. "snoop", output);
  864. }
  865. }