sort.c 37 KB

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