sort.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. #include "sort.h"
  2. #include "hist.h"
  3. #include "comm.h"
  4. #include "symbol.h"
  5. #include "evsel.h"
  6. regex_t parent_regex;
  7. const char default_parent_pattern[] = "^sys_|^do_page_fault";
  8. const char *parent_pattern = default_parent_pattern;
  9. const char default_sort_order[] = "comm,dso,symbol";
  10. const char default_branch_sort_order[] = "comm,dso_from,symbol_from,dso_to,symbol_to";
  11. const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
  12. const char default_top_sort_order[] = "dso,symbol";
  13. const char default_diff_sort_order[] = "dso,symbol";
  14. const char *sort_order;
  15. const char *field_order;
  16. regex_t ignore_callees_regex;
  17. int have_ignore_callees = 0;
  18. int sort__need_collapse = 0;
  19. int sort__has_parent = 0;
  20. int sort__has_sym = 0;
  21. int sort__has_dso = 0;
  22. enum sort_mode sort__mode = SORT_MODE__NORMAL;
  23. enum sort_type sort__first_dimension;
  24. LIST_HEAD(hist_entry__sort_list);
  25. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  26. {
  27. int n;
  28. va_list ap;
  29. va_start(ap, fmt);
  30. n = vsnprintf(bf, size, fmt, ap);
  31. if (symbol_conf.field_sep && n > 0) {
  32. char *sep = bf;
  33. while (1) {
  34. sep = strchr(sep, *symbol_conf.field_sep);
  35. if (sep == NULL)
  36. break;
  37. *sep = '.';
  38. }
  39. }
  40. va_end(ap);
  41. if (n >= (int)size)
  42. return size - 1;
  43. return n;
  44. }
  45. static int64_t cmp_null(const void *l, const void *r)
  46. {
  47. if (!l && !r)
  48. return 0;
  49. else if (!l)
  50. return -1;
  51. else
  52. return 1;
  53. }
  54. /* --sort pid */
  55. static int64_t
  56. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  57. {
  58. return right->thread->tid - left->thread->tid;
  59. }
  60. static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
  61. size_t size, unsigned int width)
  62. {
  63. const char *comm = thread__comm_str(he->thread);
  64. return repsep_snprintf(bf, size, "%*s:%5d", width - 6,
  65. comm ?: "", he->thread->tid);
  66. }
  67. struct sort_entry sort_thread = {
  68. .se_header = "Command: Pid",
  69. .se_cmp = sort__thread_cmp,
  70. .se_snprintf = hist_entry__thread_snprintf,
  71. .se_width_idx = HISTC_THREAD,
  72. };
  73. /* --sort comm */
  74. static int64_t
  75. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  76. {
  77. /* Compare the addr that should be unique among comm */
  78. return comm__str(right->comm) - comm__str(left->comm);
  79. }
  80. static int64_t
  81. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  82. {
  83. /* Compare the addr that should be unique among comm */
  84. return comm__str(right->comm) - comm__str(left->comm);
  85. }
  86. static int64_t
  87. sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
  88. {
  89. return strcmp(comm__str(right->comm), comm__str(left->comm));
  90. }
  91. static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
  92. size_t size, unsigned int width)
  93. {
  94. return repsep_snprintf(bf, size, "%*s", width, comm__str(he->comm));
  95. }
  96. struct sort_entry sort_comm = {
  97. .se_header = "Command",
  98. .se_cmp = sort__comm_cmp,
  99. .se_collapse = sort__comm_collapse,
  100. .se_sort = sort__comm_sort,
  101. .se_snprintf = hist_entry__comm_snprintf,
  102. .se_width_idx = HISTC_COMM,
  103. };
  104. /* --sort dso */
  105. static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
  106. {
  107. struct dso *dso_l = map_l ? map_l->dso : NULL;
  108. struct dso *dso_r = map_r ? map_r->dso : NULL;
  109. const char *dso_name_l, *dso_name_r;
  110. if (!dso_l || !dso_r)
  111. return cmp_null(dso_r, dso_l);
  112. if (verbose) {
  113. dso_name_l = dso_l->long_name;
  114. dso_name_r = dso_r->long_name;
  115. } else {
  116. dso_name_l = dso_l->short_name;
  117. dso_name_r = dso_r->short_name;
  118. }
  119. return strcmp(dso_name_l, dso_name_r);
  120. }
  121. static int64_t
  122. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  123. {
  124. return _sort__dso_cmp(right->ms.map, left->ms.map);
  125. }
  126. static int _hist_entry__dso_snprintf(struct map *map, char *bf,
  127. size_t size, unsigned int width)
  128. {
  129. if (map && map->dso) {
  130. const char *dso_name = !verbose ? map->dso->short_name :
  131. map->dso->long_name;
  132. return repsep_snprintf(bf, size, "%-*s", width, dso_name);
  133. }
  134. return repsep_snprintf(bf, size, "%-*s", width, "[unknown]");
  135. }
  136. static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
  137. size_t size, unsigned int width)
  138. {
  139. return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
  140. }
  141. struct sort_entry sort_dso = {
  142. .se_header = "Shared Object",
  143. .se_cmp = sort__dso_cmp,
  144. .se_snprintf = hist_entry__dso_snprintf,
  145. .se_width_idx = HISTC_DSO,
  146. };
  147. /* --sort symbol */
  148. static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
  149. {
  150. return (int64_t)(right_ip - left_ip);
  151. }
  152. static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
  153. {
  154. u64 ip_l, ip_r;
  155. if (!sym_l || !sym_r)
  156. return cmp_null(sym_l, sym_r);
  157. if (sym_l == sym_r)
  158. return 0;
  159. ip_l = sym_l->start;
  160. ip_r = sym_r->start;
  161. return (int64_t)(ip_r - ip_l);
  162. }
  163. static int64_t
  164. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  165. {
  166. int64_t ret;
  167. if (!left->ms.sym && !right->ms.sym)
  168. return _sort__addr_cmp(left->ip, right->ip);
  169. /*
  170. * comparing symbol address alone is not enough since it's a
  171. * relative address within a dso.
  172. */
  173. if (!sort__has_dso) {
  174. ret = sort__dso_cmp(left, right);
  175. if (ret != 0)
  176. return ret;
  177. }
  178. return _sort__sym_cmp(left->ms.sym, right->ms.sym);
  179. }
  180. static int64_t
  181. sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
  182. {
  183. if (!left->ms.sym || !right->ms.sym)
  184. return cmp_null(left->ms.sym, right->ms.sym);
  185. return strcmp(right->ms.sym->name, left->ms.sym->name);
  186. }
  187. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  188. u64 ip, char level, char *bf, size_t size,
  189. unsigned int width)
  190. {
  191. size_t ret = 0;
  192. if (verbose) {
  193. char o = map ? dso__symtab_origin(map->dso) : '!';
  194. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  195. BITS_PER_LONG / 4 + 2, ip, o);
  196. }
  197. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  198. if (sym && map) {
  199. if (map->type == MAP__VARIABLE) {
  200. ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
  201. ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
  202. ip - map->unmap_ip(map, sym->start));
  203. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  204. width - ret, "");
  205. } else {
  206. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  207. width - ret,
  208. sym->name);
  209. }
  210. } else {
  211. size_t len = BITS_PER_LONG / 4;
  212. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  213. len, ip);
  214. ret += repsep_snprintf(bf + ret, size - ret, "%-*s",
  215. width - ret, "");
  216. }
  217. return ret;
  218. }
  219. static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
  220. size_t size, unsigned int width)
  221. {
  222. return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
  223. he->level, bf, size, width);
  224. }
  225. struct sort_entry sort_sym = {
  226. .se_header = "Symbol",
  227. .se_cmp = sort__sym_cmp,
  228. .se_sort = sort__sym_sort,
  229. .se_snprintf = hist_entry__sym_snprintf,
  230. .se_width_idx = HISTC_SYMBOL,
  231. };
  232. /* --sort srcline */
  233. static int64_t
  234. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  235. {
  236. if (!left->srcline) {
  237. if (!left->ms.map)
  238. left->srcline = SRCLINE_UNKNOWN;
  239. else {
  240. struct map *map = left->ms.map;
  241. left->srcline = get_srcline(map->dso,
  242. map__rip_2objdump(map, left->ip));
  243. }
  244. }
  245. if (!right->srcline) {
  246. if (!right->ms.map)
  247. right->srcline = SRCLINE_UNKNOWN;
  248. else {
  249. struct map *map = right->ms.map;
  250. right->srcline = get_srcline(map->dso,
  251. map__rip_2objdump(map, right->ip));
  252. }
  253. }
  254. return strcmp(right->srcline, left->srcline);
  255. }
  256. static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
  257. size_t size,
  258. unsigned int width __maybe_unused)
  259. {
  260. return repsep_snprintf(bf, size, "%s", he->srcline);
  261. }
  262. struct sort_entry sort_srcline = {
  263. .se_header = "Source:Line",
  264. .se_cmp = sort__srcline_cmp,
  265. .se_snprintf = hist_entry__srcline_snprintf,
  266. .se_width_idx = HISTC_SRCLINE,
  267. };
  268. /* --sort parent */
  269. static int64_t
  270. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  271. {
  272. struct symbol *sym_l = left->parent;
  273. struct symbol *sym_r = right->parent;
  274. if (!sym_l || !sym_r)
  275. return cmp_null(sym_l, sym_r);
  276. return strcmp(sym_r->name, sym_l->name);
  277. }
  278. static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
  279. size_t size, unsigned int width)
  280. {
  281. return repsep_snprintf(bf, size, "%-*s", width,
  282. he->parent ? he->parent->name : "[other]");
  283. }
  284. struct sort_entry sort_parent = {
  285. .se_header = "Parent symbol",
  286. .se_cmp = sort__parent_cmp,
  287. .se_snprintf = hist_entry__parent_snprintf,
  288. .se_width_idx = HISTC_PARENT,
  289. };
  290. /* --sort cpu */
  291. static int64_t
  292. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  293. {
  294. return right->cpu - left->cpu;
  295. }
  296. static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
  297. size_t size, unsigned int width)
  298. {
  299. return repsep_snprintf(bf, size, "%*d", width, he->cpu);
  300. }
  301. struct sort_entry sort_cpu = {
  302. .se_header = "CPU",
  303. .se_cmp = sort__cpu_cmp,
  304. .se_snprintf = hist_entry__cpu_snprintf,
  305. .se_width_idx = HISTC_CPU,
  306. };
  307. /* sort keys for branch stacks */
  308. static int64_t
  309. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  310. {
  311. return _sort__dso_cmp(left->branch_info->from.map,
  312. right->branch_info->from.map);
  313. }
  314. static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
  315. size_t size, unsigned int width)
  316. {
  317. return _hist_entry__dso_snprintf(he->branch_info->from.map,
  318. bf, size, width);
  319. }
  320. static int64_t
  321. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  322. {
  323. return _sort__dso_cmp(left->branch_info->to.map,
  324. right->branch_info->to.map);
  325. }
  326. static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
  327. size_t size, unsigned int width)
  328. {
  329. return _hist_entry__dso_snprintf(he->branch_info->to.map,
  330. bf, size, width);
  331. }
  332. static int64_t
  333. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  334. {
  335. struct addr_map_symbol *from_l = &left->branch_info->from;
  336. struct addr_map_symbol *from_r = &right->branch_info->from;
  337. if (!from_l->sym && !from_r->sym)
  338. return _sort__addr_cmp(from_l->addr, from_r->addr);
  339. return _sort__sym_cmp(from_l->sym, from_r->sym);
  340. }
  341. static int64_t
  342. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  343. {
  344. struct addr_map_symbol *to_l = &left->branch_info->to;
  345. struct addr_map_symbol *to_r = &right->branch_info->to;
  346. if (!to_l->sym && !to_r->sym)
  347. return _sort__addr_cmp(to_l->addr, to_r->addr);
  348. return _sort__sym_cmp(to_l->sym, to_r->sym);
  349. }
  350. static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
  351. size_t size, unsigned int width)
  352. {
  353. struct addr_map_symbol *from = &he->branch_info->from;
  354. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  355. he->level, bf, size, width);
  356. }
  357. static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
  358. size_t size, unsigned int width)
  359. {
  360. struct addr_map_symbol *to = &he->branch_info->to;
  361. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  362. he->level, bf, size, width);
  363. }
  364. struct sort_entry sort_dso_from = {
  365. .se_header = "Source Shared Object",
  366. .se_cmp = sort__dso_from_cmp,
  367. .se_snprintf = hist_entry__dso_from_snprintf,
  368. .se_width_idx = HISTC_DSO_FROM,
  369. };
  370. struct sort_entry sort_dso_to = {
  371. .se_header = "Target Shared Object",
  372. .se_cmp = sort__dso_to_cmp,
  373. .se_snprintf = hist_entry__dso_to_snprintf,
  374. .se_width_idx = HISTC_DSO_TO,
  375. };
  376. struct sort_entry sort_sym_from = {
  377. .se_header = "Source Symbol",
  378. .se_cmp = sort__sym_from_cmp,
  379. .se_snprintf = hist_entry__sym_from_snprintf,
  380. .se_width_idx = HISTC_SYMBOL_FROM,
  381. };
  382. struct sort_entry sort_sym_to = {
  383. .se_header = "Target Symbol",
  384. .se_cmp = sort__sym_to_cmp,
  385. .se_snprintf = hist_entry__sym_to_snprintf,
  386. .se_width_idx = HISTC_SYMBOL_TO,
  387. };
  388. static int64_t
  389. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  390. {
  391. const unsigned char mp = left->branch_info->flags.mispred !=
  392. right->branch_info->flags.mispred;
  393. const unsigned char p = left->branch_info->flags.predicted !=
  394. right->branch_info->flags.predicted;
  395. return mp || p;
  396. }
  397. static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
  398. size_t size, unsigned int width){
  399. static const char *out = "N/A";
  400. if (he->branch_info->flags.predicted)
  401. out = "N";
  402. else if (he->branch_info->flags.mispred)
  403. out = "Y";
  404. return repsep_snprintf(bf, size, "%-*s", width, out);
  405. }
  406. /* --sort daddr_sym */
  407. static int64_t
  408. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  409. {
  410. uint64_t l = 0, r = 0;
  411. if (left->mem_info)
  412. l = left->mem_info->daddr.addr;
  413. if (right->mem_info)
  414. r = right->mem_info->daddr.addr;
  415. return (int64_t)(r - l);
  416. }
  417. static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
  418. size_t size, unsigned int width)
  419. {
  420. uint64_t addr = 0;
  421. struct map *map = NULL;
  422. struct symbol *sym = NULL;
  423. if (he->mem_info) {
  424. addr = he->mem_info->daddr.addr;
  425. map = he->mem_info->daddr.map;
  426. sym = he->mem_info->daddr.sym;
  427. }
  428. return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
  429. width);
  430. }
  431. static int64_t
  432. sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  433. {
  434. struct map *map_l = NULL;
  435. struct map *map_r = NULL;
  436. if (left->mem_info)
  437. map_l = left->mem_info->daddr.map;
  438. if (right->mem_info)
  439. map_r = right->mem_info->daddr.map;
  440. return _sort__dso_cmp(map_l, map_r);
  441. }
  442. static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
  443. size_t size, unsigned int width)
  444. {
  445. struct map *map = NULL;
  446. if (he->mem_info)
  447. map = he->mem_info->daddr.map;
  448. return _hist_entry__dso_snprintf(map, bf, size, width);
  449. }
  450. static int64_t
  451. sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
  452. {
  453. union perf_mem_data_src data_src_l;
  454. union perf_mem_data_src data_src_r;
  455. if (left->mem_info)
  456. data_src_l = left->mem_info->data_src;
  457. else
  458. data_src_l.mem_lock = PERF_MEM_LOCK_NA;
  459. if (right->mem_info)
  460. data_src_r = right->mem_info->data_src;
  461. else
  462. data_src_r.mem_lock = PERF_MEM_LOCK_NA;
  463. return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
  464. }
  465. static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
  466. size_t size, unsigned int width)
  467. {
  468. const char *out;
  469. u64 mask = PERF_MEM_LOCK_NA;
  470. if (he->mem_info)
  471. mask = he->mem_info->data_src.mem_lock;
  472. if (mask & PERF_MEM_LOCK_NA)
  473. out = "N/A";
  474. else if (mask & PERF_MEM_LOCK_LOCKED)
  475. out = "Yes";
  476. else
  477. out = "No";
  478. return repsep_snprintf(bf, size, "%-*s", width, out);
  479. }
  480. static int64_t
  481. sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
  482. {
  483. union perf_mem_data_src data_src_l;
  484. union perf_mem_data_src data_src_r;
  485. if (left->mem_info)
  486. data_src_l = left->mem_info->data_src;
  487. else
  488. data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
  489. if (right->mem_info)
  490. data_src_r = right->mem_info->data_src;
  491. else
  492. data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
  493. return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
  494. }
  495. static const char * const tlb_access[] = {
  496. "N/A",
  497. "HIT",
  498. "MISS",
  499. "L1",
  500. "L2",
  501. "Walker",
  502. "Fault",
  503. };
  504. #define NUM_TLB_ACCESS (sizeof(tlb_access)/sizeof(const char *))
  505. static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
  506. size_t size, unsigned int width)
  507. {
  508. char out[64];
  509. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  510. size_t l = 0, i;
  511. u64 m = PERF_MEM_TLB_NA;
  512. u64 hit, miss;
  513. out[0] = '\0';
  514. if (he->mem_info)
  515. m = he->mem_info->data_src.mem_dtlb;
  516. hit = m & PERF_MEM_TLB_HIT;
  517. miss = m & PERF_MEM_TLB_MISS;
  518. /* already taken care of */
  519. m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
  520. for (i = 0; m && i < NUM_TLB_ACCESS; i++, m >>= 1) {
  521. if (!(m & 0x1))
  522. continue;
  523. if (l) {
  524. strcat(out, " or ");
  525. l += 4;
  526. }
  527. strncat(out, tlb_access[i], sz - l);
  528. l += strlen(tlb_access[i]);
  529. }
  530. if (*out == '\0')
  531. strcpy(out, "N/A");
  532. if (hit)
  533. strncat(out, " hit", sz - l);
  534. if (miss)
  535. strncat(out, " miss", sz - l);
  536. return repsep_snprintf(bf, size, "%-*s", width, out);
  537. }
  538. static int64_t
  539. sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
  540. {
  541. union perf_mem_data_src data_src_l;
  542. union perf_mem_data_src data_src_r;
  543. if (left->mem_info)
  544. data_src_l = left->mem_info->data_src;
  545. else
  546. data_src_l.mem_lvl = PERF_MEM_LVL_NA;
  547. if (right->mem_info)
  548. data_src_r = right->mem_info->data_src;
  549. else
  550. data_src_r.mem_lvl = PERF_MEM_LVL_NA;
  551. return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
  552. }
  553. static const char * const mem_lvl[] = {
  554. "N/A",
  555. "HIT",
  556. "MISS",
  557. "L1",
  558. "LFB",
  559. "L2",
  560. "L3",
  561. "Local RAM",
  562. "Remote RAM (1 hop)",
  563. "Remote RAM (2 hops)",
  564. "Remote Cache (1 hop)",
  565. "Remote Cache (2 hops)",
  566. "I/O",
  567. "Uncached",
  568. };
  569. #define NUM_MEM_LVL (sizeof(mem_lvl)/sizeof(const char *))
  570. static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
  571. size_t size, unsigned int width)
  572. {
  573. char out[64];
  574. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  575. size_t i, l = 0;
  576. u64 m = PERF_MEM_LVL_NA;
  577. u64 hit, miss;
  578. if (he->mem_info)
  579. m = he->mem_info->data_src.mem_lvl;
  580. out[0] = '\0';
  581. hit = m & PERF_MEM_LVL_HIT;
  582. miss = m & PERF_MEM_LVL_MISS;
  583. /* already taken care of */
  584. m &= ~(PERF_MEM_LVL_HIT|PERF_MEM_LVL_MISS);
  585. for (i = 0; m && i < NUM_MEM_LVL; i++, m >>= 1) {
  586. if (!(m & 0x1))
  587. continue;
  588. if (l) {
  589. strcat(out, " or ");
  590. l += 4;
  591. }
  592. strncat(out, mem_lvl[i], sz - l);
  593. l += strlen(mem_lvl[i]);
  594. }
  595. if (*out == '\0')
  596. strcpy(out, "N/A");
  597. if (hit)
  598. strncat(out, " hit", sz - l);
  599. if (miss)
  600. strncat(out, " miss", sz - l);
  601. return repsep_snprintf(bf, size, "%-*s", width, out);
  602. }
  603. static int64_t
  604. sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
  605. {
  606. union perf_mem_data_src data_src_l;
  607. union perf_mem_data_src data_src_r;
  608. if (left->mem_info)
  609. data_src_l = left->mem_info->data_src;
  610. else
  611. data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
  612. if (right->mem_info)
  613. data_src_r = right->mem_info->data_src;
  614. else
  615. data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
  616. return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
  617. }
  618. static const char * const snoop_access[] = {
  619. "N/A",
  620. "None",
  621. "Miss",
  622. "Hit",
  623. "HitM",
  624. };
  625. #define NUM_SNOOP_ACCESS (sizeof(snoop_access)/sizeof(const char *))
  626. static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
  627. size_t size, unsigned int width)
  628. {
  629. char out[64];
  630. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  631. size_t i, l = 0;
  632. u64 m = PERF_MEM_SNOOP_NA;
  633. out[0] = '\0';
  634. if (he->mem_info)
  635. m = he->mem_info->data_src.mem_snoop;
  636. for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
  637. if (!(m & 0x1))
  638. continue;
  639. if (l) {
  640. strcat(out, " or ");
  641. l += 4;
  642. }
  643. strncat(out, snoop_access[i], sz - l);
  644. l += strlen(snoop_access[i]);
  645. }
  646. if (*out == '\0')
  647. strcpy(out, "N/A");
  648. return repsep_snprintf(bf, size, "%-*s", width, out);
  649. }
  650. struct sort_entry sort_mispredict = {
  651. .se_header = "Branch Mispredicted",
  652. .se_cmp = sort__mispredict_cmp,
  653. .se_snprintf = hist_entry__mispredict_snprintf,
  654. .se_width_idx = HISTC_MISPREDICT,
  655. };
  656. static u64 he_weight(struct hist_entry *he)
  657. {
  658. return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
  659. }
  660. static int64_t
  661. sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  662. {
  663. return he_weight(left) - he_weight(right);
  664. }
  665. static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
  666. size_t size, unsigned int width)
  667. {
  668. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
  669. }
  670. struct sort_entry sort_local_weight = {
  671. .se_header = "Local Weight",
  672. .se_cmp = sort__local_weight_cmp,
  673. .se_snprintf = hist_entry__local_weight_snprintf,
  674. .se_width_idx = HISTC_LOCAL_WEIGHT,
  675. };
  676. static int64_t
  677. sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  678. {
  679. return left->stat.weight - right->stat.weight;
  680. }
  681. static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
  682. size_t size, unsigned int width)
  683. {
  684. return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
  685. }
  686. struct sort_entry sort_global_weight = {
  687. .se_header = "Weight",
  688. .se_cmp = sort__global_weight_cmp,
  689. .se_snprintf = hist_entry__global_weight_snprintf,
  690. .se_width_idx = HISTC_GLOBAL_WEIGHT,
  691. };
  692. struct sort_entry sort_mem_daddr_sym = {
  693. .se_header = "Data Symbol",
  694. .se_cmp = sort__daddr_cmp,
  695. .se_snprintf = hist_entry__daddr_snprintf,
  696. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  697. };
  698. struct sort_entry sort_mem_daddr_dso = {
  699. .se_header = "Data Object",
  700. .se_cmp = sort__dso_daddr_cmp,
  701. .se_snprintf = hist_entry__dso_daddr_snprintf,
  702. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  703. };
  704. struct sort_entry sort_mem_locked = {
  705. .se_header = "Locked",
  706. .se_cmp = sort__locked_cmp,
  707. .se_snprintf = hist_entry__locked_snprintf,
  708. .se_width_idx = HISTC_MEM_LOCKED,
  709. };
  710. struct sort_entry sort_mem_tlb = {
  711. .se_header = "TLB access",
  712. .se_cmp = sort__tlb_cmp,
  713. .se_snprintf = hist_entry__tlb_snprintf,
  714. .se_width_idx = HISTC_MEM_TLB,
  715. };
  716. struct sort_entry sort_mem_lvl = {
  717. .se_header = "Memory access",
  718. .se_cmp = sort__lvl_cmp,
  719. .se_snprintf = hist_entry__lvl_snprintf,
  720. .se_width_idx = HISTC_MEM_LVL,
  721. };
  722. struct sort_entry sort_mem_snoop = {
  723. .se_header = "Snoop",
  724. .se_cmp = sort__snoop_cmp,
  725. .se_snprintf = hist_entry__snoop_snprintf,
  726. .se_width_idx = HISTC_MEM_SNOOP,
  727. };
  728. static int64_t
  729. sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
  730. {
  731. return left->branch_info->flags.abort !=
  732. right->branch_info->flags.abort;
  733. }
  734. static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
  735. size_t size, unsigned int width)
  736. {
  737. static const char *out = ".";
  738. if (he->branch_info->flags.abort)
  739. out = "A";
  740. return repsep_snprintf(bf, size, "%-*s", width, out);
  741. }
  742. struct sort_entry sort_abort = {
  743. .se_header = "Transaction abort",
  744. .se_cmp = sort__abort_cmp,
  745. .se_snprintf = hist_entry__abort_snprintf,
  746. .se_width_idx = HISTC_ABORT,
  747. };
  748. static int64_t
  749. sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
  750. {
  751. return left->branch_info->flags.in_tx !=
  752. right->branch_info->flags.in_tx;
  753. }
  754. static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
  755. size_t size, unsigned int width)
  756. {
  757. static const char *out = ".";
  758. if (he->branch_info->flags.in_tx)
  759. out = "T";
  760. return repsep_snprintf(bf, size, "%-*s", width, out);
  761. }
  762. struct sort_entry sort_in_tx = {
  763. .se_header = "Branch in transaction",
  764. .se_cmp = sort__in_tx_cmp,
  765. .se_snprintf = hist_entry__in_tx_snprintf,
  766. .se_width_idx = HISTC_IN_TX,
  767. };
  768. static int64_t
  769. sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
  770. {
  771. return left->transaction - right->transaction;
  772. }
  773. static inline char *add_str(char *p, const char *str)
  774. {
  775. strcpy(p, str);
  776. return p + strlen(str);
  777. }
  778. static struct txbit {
  779. unsigned flag;
  780. const char *name;
  781. int skip_for_len;
  782. } txbits[] = {
  783. { PERF_TXN_ELISION, "EL ", 0 },
  784. { PERF_TXN_TRANSACTION, "TX ", 1 },
  785. { PERF_TXN_SYNC, "SYNC ", 1 },
  786. { PERF_TXN_ASYNC, "ASYNC ", 0 },
  787. { PERF_TXN_RETRY, "RETRY ", 0 },
  788. { PERF_TXN_CONFLICT, "CON ", 0 },
  789. { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
  790. { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
  791. { 0, NULL, 0 }
  792. };
  793. int hist_entry__transaction_len(void)
  794. {
  795. int i;
  796. int len = 0;
  797. for (i = 0; txbits[i].name; i++) {
  798. if (!txbits[i].skip_for_len)
  799. len += strlen(txbits[i].name);
  800. }
  801. len += 4; /* :XX<space> */
  802. return len;
  803. }
  804. static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
  805. size_t size, unsigned int width)
  806. {
  807. u64 t = he->transaction;
  808. char buf[128];
  809. char *p = buf;
  810. int i;
  811. buf[0] = 0;
  812. for (i = 0; txbits[i].name; i++)
  813. if (txbits[i].flag & t)
  814. p = add_str(p, txbits[i].name);
  815. if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
  816. p = add_str(p, "NEITHER ");
  817. if (t & PERF_TXN_ABORT_MASK) {
  818. sprintf(p, ":%" PRIx64,
  819. (t & PERF_TXN_ABORT_MASK) >>
  820. PERF_TXN_ABORT_SHIFT);
  821. p += strlen(p);
  822. }
  823. return repsep_snprintf(bf, size, "%-*s", width, buf);
  824. }
  825. struct sort_entry sort_transaction = {
  826. .se_header = "Transaction ",
  827. .se_cmp = sort__transaction_cmp,
  828. .se_snprintf = hist_entry__transaction_snprintf,
  829. .se_width_idx = HISTC_TRANSACTION,
  830. };
  831. struct sort_dimension {
  832. const char *name;
  833. struct sort_entry *entry;
  834. int taken;
  835. };
  836. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  837. static struct sort_dimension common_sort_dimensions[] = {
  838. DIM(SORT_PID, "pid", sort_thread),
  839. DIM(SORT_COMM, "comm", sort_comm),
  840. DIM(SORT_DSO, "dso", sort_dso),
  841. DIM(SORT_SYM, "symbol", sort_sym),
  842. DIM(SORT_PARENT, "parent", sort_parent),
  843. DIM(SORT_CPU, "cpu", sort_cpu),
  844. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  845. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  846. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  847. DIM(SORT_TRANSACTION, "transaction", sort_transaction),
  848. };
  849. #undef DIM
  850. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  851. static struct sort_dimension bstack_sort_dimensions[] = {
  852. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  853. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  854. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  855. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  856. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  857. DIM(SORT_IN_TX, "in_tx", sort_in_tx),
  858. DIM(SORT_ABORT, "abort", sort_abort),
  859. };
  860. #undef DIM
  861. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  862. static struct sort_dimension memory_sort_dimensions[] = {
  863. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  864. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  865. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  866. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  867. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  868. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  869. };
  870. #undef DIM
  871. struct hpp_dimension {
  872. const char *name;
  873. struct perf_hpp_fmt *fmt;
  874. int taken;
  875. };
  876. #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
  877. static struct hpp_dimension hpp_sort_dimensions[] = {
  878. DIM(PERF_HPP__OVERHEAD, "overhead"),
  879. DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
  880. DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
  881. DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
  882. DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
  883. DIM(PERF_HPP__SAMPLES, "sample"),
  884. DIM(PERF_HPP__PERIOD, "period"),
  885. };
  886. #undef DIM
  887. struct hpp_sort_entry {
  888. struct perf_hpp_fmt hpp;
  889. struct sort_entry *se;
  890. };
  891. bool perf_hpp__same_sort_entry(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  892. {
  893. struct hpp_sort_entry *hse_a;
  894. struct hpp_sort_entry *hse_b;
  895. if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
  896. return false;
  897. hse_a = container_of(a, struct hpp_sort_entry, hpp);
  898. hse_b = container_of(b, struct hpp_sort_entry, hpp);
  899. return hse_a->se == hse_b->se;
  900. }
  901. void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
  902. {
  903. struct hpp_sort_entry *hse;
  904. if (!perf_hpp__is_sort_entry(fmt))
  905. return;
  906. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  907. hists__new_col_len(hists, hse->se->se_width_idx,
  908. strlen(hse->se->se_header));
  909. }
  910. static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  911. struct perf_evsel *evsel)
  912. {
  913. struct hpp_sort_entry *hse;
  914. size_t len;
  915. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  916. len = hists__col_len(&evsel->hists, hse->se->se_width_idx);
  917. return scnprintf(hpp->buf, hpp->size, "%*s", len, hse->se->se_header);
  918. }
  919. static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
  920. struct perf_hpp *hpp __maybe_unused,
  921. struct perf_evsel *evsel)
  922. {
  923. struct hpp_sort_entry *hse;
  924. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  925. return hists__col_len(&evsel->hists, hse->se->se_width_idx);
  926. }
  927. static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  928. struct hist_entry *he)
  929. {
  930. struct hpp_sort_entry *hse;
  931. size_t len;
  932. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  933. len = hists__col_len(he->hists, hse->se->se_width_idx);
  934. return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
  935. }
  936. static struct hpp_sort_entry *
  937. __sort_dimension__alloc_hpp(struct sort_dimension *sd)
  938. {
  939. struct hpp_sort_entry *hse;
  940. hse = malloc(sizeof(*hse));
  941. if (hse == NULL) {
  942. pr_err("Memory allocation failed\n");
  943. return NULL;
  944. }
  945. hse->se = sd->entry;
  946. hse->hpp.header = __sort__hpp_header;
  947. hse->hpp.width = __sort__hpp_width;
  948. hse->hpp.entry = __sort__hpp_entry;
  949. hse->hpp.color = NULL;
  950. hse->hpp.cmp = sd->entry->se_cmp;
  951. hse->hpp.collapse = sd->entry->se_collapse ? : sd->entry->se_cmp;
  952. hse->hpp.sort = sd->entry->se_sort ? : hse->hpp.collapse;
  953. INIT_LIST_HEAD(&hse->hpp.list);
  954. INIT_LIST_HEAD(&hse->hpp.sort_list);
  955. return hse;
  956. }
  957. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
  958. {
  959. return format->header == __sort__hpp_header;
  960. }
  961. static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd)
  962. {
  963. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
  964. if (hse == NULL)
  965. return -1;
  966. perf_hpp__register_sort_field(&hse->hpp);
  967. return 0;
  968. }
  969. static int __sort_dimension__add_hpp_output(struct sort_dimension *sd)
  970. {
  971. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
  972. if (hse == NULL)
  973. return -1;
  974. perf_hpp__column_register(&hse->hpp);
  975. return 0;
  976. }
  977. static int __sort_dimension__add(struct sort_dimension *sd, enum sort_type idx)
  978. {
  979. if (sd->taken)
  980. return 0;
  981. if (__sort_dimension__add_hpp_sort(sd) < 0)
  982. return -1;
  983. if (sd->entry->se_collapse)
  984. sort__need_collapse = 1;
  985. if (list_empty(&hist_entry__sort_list))
  986. sort__first_dimension = idx;
  987. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  988. sd->taken = 1;
  989. return 0;
  990. }
  991. static int __hpp_dimension__add(struct hpp_dimension *hd)
  992. {
  993. if (!hd->taken) {
  994. hd->taken = 1;
  995. perf_hpp__register_sort_field(hd->fmt);
  996. }
  997. return 0;
  998. }
  999. static int __sort_dimension__add_output(struct sort_dimension *sd)
  1000. {
  1001. if (sd->taken)
  1002. return 0;
  1003. if (__sort_dimension__add_hpp_output(sd) < 0)
  1004. return -1;
  1005. sd->taken = 1;
  1006. return 0;
  1007. }
  1008. static int __hpp_dimension__add_output(struct hpp_dimension *hd)
  1009. {
  1010. if (!hd->taken) {
  1011. hd->taken = 1;
  1012. perf_hpp__column_register(hd->fmt);
  1013. }
  1014. return 0;
  1015. }
  1016. int sort_dimension__add(const char *tok)
  1017. {
  1018. unsigned int i;
  1019. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  1020. struct sort_dimension *sd = &common_sort_dimensions[i];
  1021. if (strncasecmp(tok, sd->name, strlen(tok)))
  1022. continue;
  1023. if (sd->entry == &sort_parent) {
  1024. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  1025. if (ret) {
  1026. char err[BUFSIZ];
  1027. regerror(ret, &parent_regex, err, sizeof(err));
  1028. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  1029. return -EINVAL;
  1030. }
  1031. sort__has_parent = 1;
  1032. } else if (sd->entry == &sort_sym) {
  1033. sort__has_sym = 1;
  1034. } else if (sd->entry == &sort_dso) {
  1035. sort__has_dso = 1;
  1036. }
  1037. return __sort_dimension__add(sd, i);
  1038. }
  1039. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  1040. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  1041. if (strncasecmp(tok, hd->name, strlen(tok)))
  1042. continue;
  1043. return __hpp_dimension__add(hd);
  1044. }
  1045. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  1046. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  1047. if (strncasecmp(tok, sd->name, strlen(tok)))
  1048. continue;
  1049. if (sort__mode != SORT_MODE__BRANCH)
  1050. return -EINVAL;
  1051. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  1052. sort__has_sym = 1;
  1053. __sort_dimension__add(sd, i + __SORT_BRANCH_STACK);
  1054. return 0;
  1055. }
  1056. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  1057. struct sort_dimension *sd = &memory_sort_dimensions[i];
  1058. if (strncasecmp(tok, sd->name, strlen(tok)))
  1059. continue;
  1060. if (sort__mode != SORT_MODE__MEMORY)
  1061. return -EINVAL;
  1062. if (sd->entry == &sort_mem_daddr_sym)
  1063. sort__has_sym = 1;
  1064. __sort_dimension__add(sd, i + __SORT_MEMORY_MODE);
  1065. return 0;
  1066. }
  1067. return -ESRCH;
  1068. }
  1069. static const char *get_default_sort_order(void)
  1070. {
  1071. const char *default_sort_orders[] = {
  1072. default_sort_order,
  1073. default_branch_sort_order,
  1074. default_mem_sort_order,
  1075. default_top_sort_order,
  1076. default_diff_sort_order,
  1077. };
  1078. BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
  1079. return default_sort_orders[sort__mode];
  1080. }
  1081. static int __setup_sorting(void)
  1082. {
  1083. char *tmp, *tok, *str;
  1084. const char *sort_keys = sort_order;
  1085. int ret = 0;
  1086. if (sort_keys == NULL) {
  1087. if (field_order) {
  1088. /*
  1089. * If user specified field order but no sort order,
  1090. * we'll honor it and not add default sort orders.
  1091. */
  1092. return 0;
  1093. }
  1094. sort_keys = get_default_sort_order();
  1095. }
  1096. str = strdup(sort_keys);
  1097. if (str == NULL) {
  1098. error("Not enough memory to setup sort keys");
  1099. return -ENOMEM;
  1100. }
  1101. for (tok = strtok_r(str, ", ", &tmp);
  1102. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1103. ret = sort_dimension__add(tok);
  1104. if (ret == -EINVAL) {
  1105. error("Invalid --sort key: `%s'", tok);
  1106. break;
  1107. } else if (ret == -ESRCH) {
  1108. error("Unknown --sort key: `%s'", tok);
  1109. break;
  1110. }
  1111. }
  1112. free(str);
  1113. return ret;
  1114. }
  1115. bool perf_hpp__should_skip(struct perf_hpp_fmt *format)
  1116. {
  1117. if (perf_hpp__is_sort_entry(format)) {
  1118. struct hpp_sort_entry *hse;
  1119. hse = container_of(format, struct hpp_sort_entry, hpp);
  1120. return hse->se->elide;
  1121. }
  1122. return false;
  1123. }
  1124. static void sort_entry__setup_elide(struct sort_entry *se,
  1125. struct strlist *list,
  1126. const char *list_name, FILE *fp)
  1127. {
  1128. if (list && strlist__nr_entries(list) == 1) {
  1129. if (fp != NULL)
  1130. fprintf(fp, "# %s: %s\n", list_name,
  1131. strlist__entry(list, 0)->s);
  1132. se->elide = true;
  1133. }
  1134. }
  1135. void sort__setup_elide(FILE *output)
  1136. {
  1137. struct sort_entry *se;
  1138. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  1139. "dso", output);
  1140. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list,
  1141. "comm", output);
  1142. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list,
  1143. "symbol", output);
  1144. if (sort__mode == SORT_MODE__BRANCH) {
  1145. sort_entry__setup_elide(&sort_dso_from,
  1146. symbol_conf.dso_from_list,
  1147. "dso_from", output);
  1148. sort_entry__setup_elide(&sort_dso_to,
  1149. symbol_conf.dso_to_list,
  1150. "dso_to", output);
  1151. sort_entry__setup_elide(&sort_sym_from,
  1152. symbol_conf.sym_from_list,
  1153. "sym_from", output);
  1154. sort_entry__setup_elide(&sort_sym_to,
  1155. symbol_conf.sym_to_list,
  1156. "sym_to", output);
  1157. } else if (sort__mode == SORT_MODE__MEMORY) {
  1158. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  1159. "symbol_daddr", output);
  1160. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  1161. "dso_daddr", output);
  1162. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  1163. "mem", output);
  1164. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  1165. "local_weight", output);
  1166. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  1167. "tlb", output);
  1168. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list,
  1169. "snoop", output);
  1170. }
  1171. /*
  1172. * It makes no sense to elide all of sort entries.
  1173. * Just revert them to show up again.
  1174. */
  1175. list_for_each_entry(se, &hist_entry__sort_list, list) {
  1176. if (!se->elide)
  1177. return;
  1178. }
  1179. list_for_each_entry(se, &hist_entry__sort_list, list)
  1180. se->elide = false;
  1181. }
  1182. static int output_field_add(char *tok)
  1183. {
  1184. unsigned int i;
  1185. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  1186. struct sort_dimension *sd = &common_sort_dimensions[i];
  1187. if (strncasecmp(tok, sd->name, strlen(tok)))
  1188. continue;
  1189. return __sort_dimension__add_output(sd);
  1190. }
  1191. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  1192. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  1193. if (strncasecmp(tok, hd->name, strlen(tok)))
  1194. continue;
  1195. return __hpp_dimension__add_output(hd);
  1196. }
  1197. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  1198. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  1199. if (strncasecmp(tok, sd->name, strlen(tok)))
  1200. continue;
  1201. return __sort_dimension__add_output(sd);
  1202. }
  1203. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  1204. struct sort_dimension *sd = &memory_sort_dimensions[i];
  1205. if (strncasecmp(tok, sd->name, strlen(tok)))
  1206. continue;
  1207. return __sort_dimension__add_output(sd);
  1208. }
  1209. return -ESRCH;
  1210. }
  1211. static void reset_dimensions(void)
  1212. {
  1213. unsigned int i;
  1214. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
  1215. common_sort_dimensions[i].taken = 0;
  1216. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
  1217. hpp_sort_dimensions[i].taken = 0;
  1218. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
  1219. bstack_sort_dimensions[i].taken = 0;
  1220. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
  1221. memory_sort_dimensions[i].taken = 0;
  1222. }
  1223. static int __setup_output_field(void)
  1224. {
  1225. char *tmp, *tok, *str;
  1226. int ret = 0;
  1227. if (field_order == NULL)
  1228. return 0;
  1229. reset_dimensions();
  1230. str = strdup(field_order);
  1231. if (str == NULL) {
  1232. error("Not enough memory to setup output fields");
  1233. return -ENOMEM;
  1234. }
  1235. for (tok = strtok_r(str, ", ", &tmp);
  1236. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1237. ret = output_field_add(tok);
  1238. if (ret == -EINVAL) {
  1239. error("Invalid --fields key: `%s'", tok);
  1240. break;
  1241. } else if (ret == -ESRCH) {
  1242. error("Unknown --fields key: `%s'", tok);
  1243. break;
  1244. }
  1245. }
  1246. free(str);
  1247. return ret;
  1248. }
  1249. int setup_sorting(void)
  1250. {
  1251. int err;
  1252. err = __setup_sorting();
  1253. if (err < 0)
  1254. return err;
  1255. if (parent_pattern != default_parent_pattern) {
  1256. err = sort_dimension__add("parent");
  1257. if (err < 0)
  1258. return err;
  1259. }
  1260. reset_dimensions();
  1261. /*
  1262. * perf diff doesn't use default hpp output fields.
  1263. */
  1264. if (sort__mode != SORT_MODE__DIFF)
  1265. perf_hpp__init();
  1266. err = __setup_output_field();
  1267. if (err < 0)
  1268. return err;
  1269. /* copy sort keys to output fields */
  1270. perf_hpp__setup_output_field();
  1271. /* and then copy output fields to sort keys */
  1272. perf_hpp__append_sort_keys();
  1273. return 0;
  1274. }