sort.c 23 KB

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