sort.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. #include <sys/mman.h>
  2. #include "sort.h"
  3. #include "hist.h"
  4. #include "comm.h"
  5. #include "symbol.h"
  6. #include "evsel.h"
  7. regex_t parent_regex;
  8. const char default_parent_pattern[] = "^sys_|^do_page_fault";
  9. const char *parent_pattern = default_parent_pattern;
  10. const char default_sort_order[] = "comm,dso,symbol";
  11. const char default_branch_sort_order[] = "comm,dso_from,symbol_from,dso_to,symbol_to";
  12. const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
  13. const char default_top_sort_order[] = "dso,symbol";
  14. const char default_diff_sort_order[] = "dso,symbol";
  15. const char *sort_order;
  16. const char *field_order;
  17. regex_t ignore_callees_regex;
  18. int have_ignore_callees = 0;
  19. int sort__need_collapse = 0;
  20. int sort__has_parent = 0;
  21. int sort__has_sym = 0;
  22. int sort__has_dso = 0;
  23. enum sort_mode sort__mode = SORT_MODE__NORMAL;
  24. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  25. {
  26. int n;
  27. va_list ap;
  28. va_start(ap, fmt);
  29. n = vsnprintf(bf, size, fmt, ap);
  30. if (symbol_conf.field_sep && n > 0) {
  31. char *sep = bf;
  32. while (1) {
  33. sep = strchr(sep, *symbol_conf.field_sep);
  34. if (sep == NULL)
  35. break;
  36. *sep = '.';
  37. }
  38. }
  39. va_end(ap);
  40. if (n >= (int)size)
  41. return size - 1;
  42. return n;
  43. }
  44. static int64_t cmp_null(const void *l, const void *r)
  45. {
  46. if (!l && !r)
  47. return 0;
  48. else if (!l)
  49. return -1;
  50. else
  51. return 1;
  52. }
  53. /* --sort pid */
  54. static int64_t
  55. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  56. {
  57. return right->thread->tid - left->thread->tid;
  58. }
  59. static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
  60. size_t size, unsigned int width)
  61. {
  62. const char *comm = thread__comm_str(he->thread);
  63. width = max(7U, width) - 6;
  64. return repsep_snprintf(bf, size, "%5d:%-*.*s", he->thread->tid,
  65. width, width, comm ?: "");
  66. }
  67. struct sort_entry sort_thread = {
  68. .se_header = " Pid:Command",
  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, 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, width, dso_name);
  133. }
  134. return repsep_snprintf(bf, size, "%-*.*s", width, 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. if (ret > width)
  218. bf[width] = '\0';
  219. return width;
  220. }
  221. static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
  222. size_t size, unsigned int width)
  223. {
  224. return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
  225. he->level, bf, size, width);
  226. }
  227. struct sort_entry sort_sym = {
  228. .se_header = "Symbol",
  229. .se_cmp = sort__sym_cmp,
  230. .se_sort = sort__sym_sort,
  231. .se_snprintf = hist_entry__sym_snprintf,
  232. .se_width_idx = HISTC_SYMBOL,
  233. };
  234. /* --sort srcline */
  235. static int64_t
  236. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  237. {
  238. if (!left->srcline) {
  239. if (!left->ms.map)
  240. left->srcline = SRCLINE_UNKNOWN;
  241. else {
  242. struct map *map = left->ms.map;
  243. left->srcline = get_srcline(map->dso,
  244. map__rip_2objdump(map, left->ip));
  245. }
  246. }
  247. if (!right->srcline) {
  248. if (!right->ms.map)
  249. right->srcline = SRCLINE_UNKNOWN;
  250. else {
  251. struct map *map = right->ms.map;
  252. right->srcline = get_srcline(map->dso,
  253. map__rip_2objdump(map, right->ip));
  254. }
  255. }
  256. return strcmp(right->srcline, left->srcline);
  257. }
  258. static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
  259. size_t size, unsigned int width)
  260. {
  261. return repsep_snprintf(bf, size, "%*.*-s", width, width, he->srcline);
  262. }
  263. struct sort_entry sort_srcline = {
  264. .se_header = "Source:Line",
  265. .se_cmp = sort__srcline_cmp,
  266. .se_snprintf = hist_entry__srcline_snprintf,
  267. .se_width_idx = HISTC_SRCLINE,
  268. };
  269. /* --sort parent */
  270. static int64_t
  271. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  272. {
  273. struct symbol *sym_l = left->parent;
  274. struct symbol *sym_r = right->parent;
  275. if (!sym_l || !sym_r)
  276. return cmp_null(sym_l, sym_r);
  277. return strcmp(sym_r->name, sym_l->name);
  278. }
  279. static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
  280. size_t size, unsigned int width)
  281. {
  282. return repsep_snprintf(bf, size, "%-*.*s", width, width,
  283. he->parent ? he->parent->name : "[other]");
  284. }
  285. struct sort_entry sort_parent = {
  286. .se_header = "Parent symbol",
  287. .se_cmp = sort__parent_cmp,
  288. .se_snprintf = hist_entry__parent_snprintf,
  289. .se_width_idx = HISTC_PARENT,
  290. };
  291. /* --sort cpu */
  292. static int64_t
  293. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  294. {
  295. return right->cpu - left->cpu;
  296. }
  297. static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
  298. size_t size, unsigned int width)
  299. {
  300. return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
  301. }
  302. struct sort_entry sort_cpu = {
  303. .se_header = "CPU",
  304. .se_cmp = sort__cpu_cmp,
  305. .se_snprintf = hist_entry__cpu_snprintf,
  306. .se_width_idx = HISTC_CPU,
  307. };
  308. /* sort keys for branch stacks */
  309. static int64_t
  310. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  311. {
  312. return _sort__dso_cmp(left->branch_info->from.map,
  313. right->branch_info->from.map);
  314. }
  315. static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
  316. size_t size, unsigned int width)
  317. {
  318. return _hist_entry__dso_snprintf(he->branch_info->from.map,
  319. bf, size, width);
  320. }
  321. static int64_t
  322. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  323. {
  324. return _sort__dso_cmp(left->branch_info->to.map,
  325. right->branch_info->to.map);
  326. }
  327. static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
  328. size_t size, unsigned int width)
  329. {
  330. return _hist_entry__dso_snprintf(he->branch_info->to.map,
  331. bf, size, width);
  332. }
  333. static int64_t
  334. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  335. {
  336. struct addr_map_symbol *from_l = &left->branch_info->from;
  337. struct addr_map_symbol *from_r = &right->branch_info->from;
  338. if (!from_l->sym && !from_r->sym)
  339. return _sort__addr_cmp(from_l->addr, from_r->addr);
  340. return _sort__sym_cmp(from_l->sym, from_r->sym);
  341. }
  342. static int64_t
  343. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  344. {
  345. struct addr_map_symbol *to_l, *to_r;
  346. if (!left->branch_info || !right->branch_info)
  347. return cmp_null(left->branch_info, right->branch_info);
  348. to_l = &left->branch_info->to;
  349. to_r = &right->branch_info->to;
  350. if (!to_l->sym && !to_r->sym)
  351. return _sort__addr_cmp(to_l->addr, to_r->addr);
  352. return _sort__sym_cmp(to_l->sym, to_r->sym);
  353. }
  354. static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
  355. size_t size, unsigned int width)
  356. {
  357. struct addr_map_symbol *from = &he->branch_info->from;
  358. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  359. he->level, bf, size, width);
  360. }
  361. static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
  362. size_t size, unsigned int width)
  363. {
  364. if (he->branch_info) {
  365. struct addr_map_symbol *to = &he->branch_info->to;
  366. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  367. he->level, bf, size, width);
  368. }
  369. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  370. }
  371. struct sort_entry sort_dso_from = {
  372. .se_header = "Source Shared Object",
  373. .se_cmp = sort__dso_from_cmp,
  374. .se_snprintf = hist_entry__dso_from_snprintf,
  375. .se_width_idx = HISTC_DSO_FROM,
  376. };
  377. struct sort_entry sort_dso_to = {
  378. .se_header = "Target Shared Object",
  379. .se_cmp = sort__dso_to_cmp,
  380. .se_snprintf = hist_entry__dso_to_snprintf,
  381. .se_width_idx = HISTC_DSO_TO,
  382. };
  383. struct sort_entry sort_sym_from = {
  384. .se_header = "Source Symbol",
  385. .se_cmp = sort__sym_from_cmp,
  386. .se_snprintf = hist_entry__sym_from_snprintf,
  387. .se_width_idx = HISTC_SYMBOL_FROM,
  388. };
  389. struct sort_entry sort_sym_to = {
  390. .se_header = "Target Symbol",
  391. .se_cmp = sort__sym_to_cmp,
  392. .se_snprintf = hist_entry__sym_to_snprintf,
  393. .se_width_idx = HISTC_SYMBOL_TO,
  394. };
  395. static int64_t
  396. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  397. {
  398. unsigned char mp, p;
  399. if (!left->branch_info || !right->branch_info)
  400. return cmp_null(left->branch_info, right->branch_info);
  401. mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
  402. p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
  403. return mp || p;
  404. }
  405. static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
  406. size_t size, unsigned int width){
  407. static const char *out = "N/A";
  408. if (he->branch_info) {
  409. if (he->branch_info->flags.predicted)
  410. out = "N";
  411. else if (he->branch_info->flags.mispred)
  412. out = "Y";
  413. }
  414. return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
  415. }
  416. /* --sort daddr_sym */
  417. static int64_t
  418. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  419. {
  420. uint64_t l = 0, r = 0;
  421. if (left->mem_info)
  422. l = left->mem_info->daddr.addr;
  423. if (right->mem_info)
  424. r = right->mem_info->daddr.addr;
  425. return (int64_t)(r - l);
  426. }
  427. static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
  428. size_t size, unsigned int width)
  429. {
  430. uint64_t addr = 0;
  431. struct map *map = NULL;
  432. struct symbol *sym = NULL;
  433. if (he->mem_info) {
  434. addr = he->mem_info->daddr.addr;
  435. map = he->mem_info->daddr.map;
  436. sym = he->mem_info->daddr.sym;
  437. }
  438. return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
  439. width);
  440. }
  441. static int64_t
  442. sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  443. {
  444. struct map *map_l = NULL;
  445. struct map *map_r = NULL;
  446. if (left->mem_info)
  447. map_l = left->mem_info->daddr.map;
  448. if (right->mem_info)
  449. map_r = right->mem_info->daddr.map;
  450. return _sort__dso_cmp(map_l, map_r);
  451. }
  452. static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
  453. size_t size, unsigned int width)
  454. {
  455. struct map *map = NULL;
  456. if (he->mem_info)
  457. map = he->mem_info->daddr.map;
  458. return _hist_entry__dso_snprintf(map, bf, size, width);
  459. }
  460. static int64_t
  461. sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
  462. {
  463. union perf_mem_data_src data_src_l;
  464. union perf_mem_data_src data_src_r;
  465. if (left->mem_info)
  466. data_src_l = left->mem_info->data_src;
  467. else
  468. data_src_l.mem_lock = PERF_MEM_LOCK_NA;
  469. if (right->mem_info)
  470. data_src_r = right->mem_info->data_src;
  471. else
  472. data_src_r.mem_lock = PERF_MEM_LOCK_NA;
  473. return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
  474. }
  475. static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
  476. size_t size, unsigned int width)
  477. {
  478. const char *out;
  479. u64 mask = PERF_MEM_LOCK_NA;
  480. if (he->mem_info)
  481. mask = he->mem_info->data_src.mem_lock;
  482. if (mask & PERF_MEM_LOCK_NA)
  483. out = "N/A";
  484. else if (mask & PERF_MEM_LOCK_LOCKED)
  485. out = "Yes";
  486. else
  487. out = "No";
  488. return repsep_snprintf(bf, size, "%-*s", width, out);
  489. }
  490. static int64_t
  491. sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
  492. {
  493. union perf_mem_data_src data_src_l;
  494. union perf_mem_data_src data_src_r;
  495. if (left->mem_info)
  496. data_src_l = left->mem_info->data_src;
  497. else
  498. data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
  499. if (right->mem_info)
  500. data_src_r = right->mem_info->data_src;
  501. else
  502. data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
  503. return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
  504. }
  505. static const char * const tlb_access[] = {
  506. "N/A",
  507. "HIT",
  508. "MISS",
  509. "L1",
  510. "L2",
  511. "Walker",
  512. "Fault",
  513. };
  514. #define NUM_TLB_ACCESS (sizeof(tlb_access)/sizeof(const char *))
  515. static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
  516. size_t size, unsigned int width)
  517. {
  518. char out[64];
  519. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  520. size_t l = 0, i;
  521. u64 m = PERF_MEM_TLB_NA;
  522. u64 hit, miss;
  523. out[0] = '\0';
  524. if (he->mem_info)
  525. m = he->mem_info->data_src.mem_dtlb;
  526. hit = m & PERF_MEM_TLB_HIT;
  527. miss = m & PERF_MEM_TLB_MISS;
  528. /* already taken care of */
  529. m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
  530. for (i = 0; m && i < NUM_TLB_ACCESS; i++, m >>= 1) {
  531. if (!(m & 0x1))
  532. continue;
  533. if (l) {
  534. strcat(out, " or ");
  535. l += 4;
  536. }
  537. strncat(out, tlb_access[i], sz - l);
  538. l += strlen(tlb_access[i]);
  539. }
  540. if (*out == '\0')
  541. strcpy(out, "N/A");
  542. if (hit)
  543. strncat(out, " hit", sz - l);
  544. if (miss)
  545. strncat(out, " miss", sz - l);
  546. return repsep_snprintf(bf, size, "%-*s", width, out);
  547. }
  548. static int64_t
  549. sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
  550. {
  551. union perf_mem_data_src data_src_l;
  552. union perf_mem_data_src data_src_r;
  553. if (left->mem_info)
  554. data_src_l = left->mem_info->data_src;
  555. else
  556. data_src_l.mem_lvl = PERF_MEM_LVL_NA;
  557. if (right->mem_info)
  558. data_src_r = right->mem_info->data_src;
  559. else
  560. data_src_r.mem_lvl = PERF_MEM_LVL_NA;
  561. return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
  562. }
  563. static const char * const mem_lvl[] = {
  564. "N/A",
  565. "HIT",
  566. "MISS",
  567. "L1",
  568. "LFB",
  569. "L2",
  570. "L3",
  571. "Local RAM",
  572. "Remote RAM (1 hop)",
  573. "Remote RAM (2 hops)",
  574. "Remote Cache (1 hop)",
  575. "Remote Cache (2 hops)",
  576. "I/O",
  577. "Uncached",
  578. };
  579. #define NUM_MEM_LVL (sizeof(mem_lvl)/sizeof(const char *))
  580. static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
  581. size_t size, unsigned int width)
  582. {
  583. char out[64];
  584. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  585. size_t i, l = 0;
  586. u64 m = PERF_MEM_LVL_NA;
  587. u64 hit, miss;
  588. if (he->mem_info)
  589. m = he->mem_info->data_src.mem_lvl;
  590. out[0] = '\0';
  591. hit = m & PERF_MEM_LVL_HIT;
  592. miss = m & PERF_MEM_LVL_MISS;
  593. /* already taken care of */
  594. m &= ~(PERF_MEM_LVL_HIT|PERF_MEM_LVL_MISS);
  595. for (i = 0; m && i < NUM_MEM_LVL; i++, m >>= 1) {
  596. if (!(m & 0x1))
  597. continue;
  598. if (l) {
  599. strcat(out, " or ");
  600. l += 4;
  601. }
  602. strncat(out, mem_lvl[i], sz - l);
  603. l += strlen(mem_lvl[i]);
  604. }
  605. if (*out == '\0')
  606. strcpy(out, "N/A");
  607. if (hit)
  608. strncat(out, " hit", sz - l);
  609. if (miss)
  610. strncat(out, " miss", sz - l);
  611. return repsep_snprintf(bf, size, "%-*s", width, out);
  612. }
  613. static int64_t
  614. sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
  615. {
  616. union perf_mem_data_src data_src_l;
  617. union perf_mem_data_src data_src_r;
  618. if (left->mem_info)
  619. data_src_l = left->mem_info->data_src;
  620. else
  621. data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
  622. if (right->mem_info)
  623. data_src_r = right->mem_info->data_src;
  624. else
  625. data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
  626. return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
  627. }
  628. static const char * const snoop_access[] = {
  629. "N/A",
  630. "None",
  631. "Miss",
  632. "Hit",
  633. "HitM",
  634. };
  635. #define NUM_SNOOP_ACCESS (sizeof(snoop_access)/sizeof(const char *))
  636. static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
  637. size_t size, unsigned int width)
  638. {
  639. char out[64];
  640. size_t sz = sizeof(out) - 1; /* -1 for null termination */
  641. size_t i, l = 0;
  642. u64 m = PERF_MEM_SNOOP_NA;
  643. out[0] = '\0';
  644. if (he->mem_info)
  645. m = he->mem_info->data_src.mem_snoop;
  646. for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
  647. if (!(m & 0x1))
  648. continue;
  649. if (l) {
  650. strcat(out, " or ");
  651. l += 4;
  652. }
  653. strncat(out, snoop_access[i], sz - l);
  654. l += strlen(snoop_access[i]);
  655. }
  656. if (*out == '\0')
  657. strcpy(out, "N/A");
  658. return repsep_snprintf(bf, size, "%-*s", width, out);
  659. }
  660. static inline u64 cl_address(u64 address)
  661. {
  662. /* return the cacheline of the address */
  663. return (address & ~(cacheline_size - 1));
  664. }
  665. static int64_t
  666. sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
  667. {
  668. u64 l, r;
  669. struct map *l_map, *r_map;
  670. if (!left->mem_info) return -1;
  671. if (!right->mem_info) return 1;
  672. /* group event types together */
  673. if (left->cpumode > right->cpumode) return -1;
  674. if (left->cpumode < right->cpumode) return 1;
  675. l_map = left->mem_info->daddr.map;
  676. r_map = right->mem_info->daddr.map;
  677. /* if both are NULL, jump to sort on al_addr instead */
  678. if (!l_map && !r_map)
  679. goto addr;
  680. if (!l_map) return -1;
  681. if (!r_map) return 1;
  682. if (l_map->maj > r_map->maj) return -1;
  683. if (l_map->maj < r_map->maj) return 1;
  684. if (l_map->min > r_map->min) return -1;
  685. if (l_map->min < r_map->min) return 1;
  686. if (l_map->ino > r_map->ino) return -1;
  687. if (l_map->ino < r_map->ino) return 1;
  688. if (l_map->ino_generation > r_map->ino_generation) return -1;
  689. if (l_map->ino_generation < r_map->ino_generation) return 1;
  690. /*
  691. * Addresses with no major/minor numbers are assumed to be
  692. * anonymous in userspace. Sort those on pid then address.
  693. *
  694. * The kernel and non-zero major/minor mapped areas are
  695. * assumed to be unity mapped. Sort those on address.
  696. */
  697. if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
  698. (!(l_map->flags & MAP_SHARED)) &&
  699. !l_map->maj && !l_map->min && !l_map->ino &&
  700. !l_map->ino_generation) {
  701. /* userspace anonymous */
  702. if (left->thread->pid_ > right->thread->pid_) return -1;
  703. if (left->thread->pid_ < right->thread->pid_) return 1;
  704. }
  705. addr:
  706. /* al_addr does all the right addr - start + offset calculations */
  707. l = cl_address(left->mem_info->daddr.al_addr);
  708. r = cl_address(right->mem_info->daddr.al_addr);
  709. if (l > r) return -1;
  710. if (l < r) return 1;
  711. return 0;
  712. }
  713. static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
  714. size_t size, unsigned int width)
  715. {
  716. uint64_t addr = 0;
  717. struct map *map = NULL;
  718. struct symbol *sym = NULL;
  719. char level = he->level;
  720. if (he->mem_info) {
  721. addr = cl_address(he->mem_info->daddr.al_addr);
  722. map = he->mem_info->daddr.map;
  723. sym = he->mem_info->daddr.sym;
  724. /* print [s] for shared data mmaps */
  725. if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
  726. map && (map->type == MAP__VARIABLE) &&
  727. (map->flags & MAP_SHARED) &&
  728. (map->maj || map->min || map->ino ||
  729. map->ino_generation))
  730. level = 's';
  731. else if (!map)
  732. level = 'X';
  733. }
  734. return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
  735. width);
  736. }
  737. struct sort_entry sort_mispredict = {
  738. .se_header = "Branch Mispredicted",
  739. .se_cmp = sort__mispredict_cmp,
  740. .se_snprintf = hist_entry__mispredict_snprintf,
  741. .se_width_idx = HISTC_MISPREDICT,
  742. };
  743. static u64 he_weight(struct hist_entry *he)
  744. {
  745. return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
  746. }
  747. static int64_t
  748. sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  749. {
  750. return he_weight(left) - he_weight(right);
  751. }
  752. static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
  753. size_t size, unsigned int width)
  754. {
  755. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
  756. }
  757. struct sort_entry sort_local_weight = {
  758. .se_header = "Local Weight",
  759. .se_cmp = sort__local_weight_cmp,
  760. .se_snprintf = hist_entry__local_weight_snprintf,
  761. .se_width_idx = HISTC_LOCAL_WEIGHT,
  762. };
  763. static int64_t
  764. sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  765. {
  766. return left->stat.weight - right->stat.weight;
  767. }
  768. static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
  769. size_t size, unsigned int width)
  770. {
  771. return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
  772. }
  773. struct sort_entry sort_global_weight = {
  774. .se_header = "Weight",
  775. .se_cmp = sort__global_weight_cmp,
  776. .se_snprintf = hist_entry__global_weight_snprintf,
  777. .se_width_idx = HISTC_GLOBAL_WEIGHT,
  778. };
  779. struct sort_entry sort_mem_daddr_sym = {
  780. .se_header = "Data Symbol",
  781. .se_cmp = sort__daddr_cmp,
  782. .se_snprintf = hist_entry__daddr_snprintf,
  783. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  784. };
  785. struct sort_entry sort_mem_daddr_dso = {
  786. .se_header = "Data Object",
  787. .se_cmp = sort__dso_daddr_cmp,
  788. .se_snprintf = hist_entry__dso_daddr_snprintf,
  789. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  790. };
  791. struct sort_entry sort_mem_locked = {
  792. .se_header = "Locked",
  793. .se_cmp = sort__locked_cmp,
  794. .se_snprintf = hist_entry__locked_snprintf,
  795. .se_width_idx = HISTC_MEM_LOCKED,
  796. };
  797. struct sort_entry sort_mem_tlb = {
  798. .se_header = "TLB access",
  799. .se_cmp = sort__tlb_cmp,
  800. .se_snprintf = hist_entry__tlb_snprintf,
  801. .se_width_idx = HISTC_MEM_TLB,
  802. };
  803. struct sort_entry sort_mem_lvl = {
  804. .se_header = "Memory access",
  805. .se_cmp = sort__lvl_cmp,
  806. .se_snprintf = hist_entry__lvl_snprintf,
  807. .se_width_idx = HISTC_MEM_LVL,
  808. };
  809. struct sort_entry sort_mem_snoop = {
  810. .se_header = "Snoop",
  811. .se_cmp = sort__snoop_cmp,
  812. .se_snprintf = hist_entry__snoop_snprintf,
  813. .se_width_idx = HISTC_MEM_SNOOP,
  814. };
  815. struct sort_entry sort_mem_dcacheline = {
  816. .se_header = "Data Cacheline",
  817. .se_cmp = sort__dcacheline_cmp,
  818. .se_snprintf = hist_entry__dcacheline_snprintf,
  819. .se_width_idx = HISTC_MEM_DCACHELINE,
  820. };
  821. static int64_t
  822. sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
  823. {
  824. if (!left->branch_info || !right->branch_info)
  825. return cmp_null(left->branch_info, right->branch_info);
  826. return left->branch_info->flags.abort !=
  827. right->branch_info->flags.abort;
  828. }
  829. static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
  830. size_t size, unsigned int width)
  831. {
  832. static const char *out = "N/A";
  833. if (he->branch_info) {
  834. if (he->branch_info->flags.abort)
  835. out = "A";
  836. else
  837. out = ".";
  838. }
  839. return repsep_snprintf(bf, size, "%-*s", width, out);
  840. }
  841. struct sort_entry sort_abort = {
  842. .se_header = "Transaction abort",
  843. .se_cmp = sort__abort_cmp,
  844. .se_snprintf = hist_entry__abort_snprintf,
  845. .se_width_idx = HISTC_ABORT,
  846. };
  847. static int64_t
  848. sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
  849. {
  850. if (!left->branch_info || !right->branch_info)
  851. return cmp_null(left->branch_info, right->branch_info);
  852. return left->branch_info->flags.in_tx !=
  853. right->branch_info->flags.in_tx;
  854. }
  855. static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
  856. size_t size, unsigned int width)
  857. {
  858. static const char *out = "N/A";
  859. if (he->branch_info) {
  860. if (he->branch_info->flags.in_tx)
  861. out = "T";
  862. else
  863. out = ".";
  864. }
  865. return repsep_snprintf(bf, size, "%-*s", width, out);
  866. }
  867. struct sort_entry sort_in_tx = {
  868. .se_header = "Branch in transaction",
  869. .se_cmp = sort__in_tx_cmp,
  870. .se_snprintf = hist_entry__in_tx_snprintf,
  871. .se_width_idx = HISTC_IN_TX,
  872. };
  873. static int64_t
  874. sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
  875. {
  876. return left->transaction - right->transaction;
  877. }
  878. static inline char *add_str(char *p, const char *str)
  879. {
  880. strcpy(p, str);
  881. return p + strlen(str);
  882. }
  883. static struct txbit {
  884. unsigned flag;
  885. const char *name;
  886. int skip_for_len;
  887. } txbits[] = {
  888. { PERF_TXN_ELISION, "EL ", 0 },
  889. { PERF_TXN_TRANSACTION, "TX ", 1 },
  890. { PERF_TXN_SYNC, "SYNC ", 1 },
  891. { PERF_TXN_ASYNC, "ASYNC ", 0 },
  892. { PERF_TXN_RETRY, "RETRY ", 0 },
  893. { PERF_TXN_CONFLICT, "CON ", 0 },
  894. { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
  895. { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
  896. { 0, NULL, 0 }
  897. };
  898. int hist_entry__transaction_len(void)
  899. {
  900. int i;
  901. int len = 0;
  902. for (i = 0; txbits[i].name; i++) {
  903. if (!txbits[i].skip_for_len)
  904. len += strlen(txbits[i].name);
  905. }
  906. len += 4; /* :XX<space> */
  907. return len;
  908. }
  909. static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
  910. size_t size, unsigned int width)
  911. {
  912. u64 t = he->transaction;
  913. char buf[128];
  914. char *p = buf;
  915. int i;
  916. buf[0] = 0;
  917. for (i = 0; txbits[i].name; i++)
  918. if (txbits[i].flag & t)
  919. p = add_str(p, txbits[i].name);
  920. if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
  921. p = add_str(p, "NEITHER ");
  922. if (t & PERF_TXN_ABORT_MASK) {
  923. sprintf(p, ":%" PRIx64,
  924. (t & PERF_TXN_ABORT_MASK) >>
  925. PERF_TXN_ABORT_SHIFT);
  926. p += strlen(p);
  927. }
  928. return repsep_snprintf(bf, size, "%-*s", width, buf);
  929. }
  930. struct sort_entry sort_transaction = {
  931. .se_header = "Transaction ",
  932. .se_cmp = sort__transaction_cmp,
  933. .se_snprintf = hist_entry__transaction_snprintf,
  934. .se_width_idx = HISTC_TRANSACTION,
  935. };
  936. struct sort_dimension {
  937. const char *name;
  938. struct sort_entry *entry;
  939. int taken;
  940. };
  941. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  942. static struct sort_dimension common_sort_dimensions[] = {
  943. DIM(SORT_PID, "pid", sort_thread),
  944. DIM(SORT_COMM, "comm", sort_comm),
  945. DIM(SORT_DSO, "dso", sort_dso),
  946. DIM(SORT_SYM, "symbol", sort_sym),
  947. DIM(SORT_PARENT, "parent", sort_parent),
  948. DIM(SORT_CPU, "cpu", sort_cpu),
  949. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  950. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  951. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  952. DIM(SORT_TRANSACTION, "transaction", sort_transaction),
  953. };
  954. #undef DIM
  955. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  956. static struct sort_dimension bstack_sort_dimensions[] = {
  957. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  958. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  959. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  960. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  961. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  962. DIM(SORT_IN_TX, "in_tx", sort_in_tx),
  963. DIM(SORT_ABORT, "abort", sort_abort),
  964. };
  965. #undef DIM
  966. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  967. static struct sort_dimension memory_sort_dimensions[] = {
  968. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  969. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  970. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  971. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  972. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  973. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  974. DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
  975. };
  976. #undef DIM
  977. struct hpp_dimension {
  978. const char *name;
  979. struct perf_hpp_fmt *fmt;
  980. int taken;
  981. };
  982. #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
  983. static struct hpp_dimension hpp_sort_dimensions[] = {
  984. DIM(PERF_HPP__OVERHEAD, "overhead"),
  985. DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
  986. DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
  987. DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
  988. DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
  989. DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
  990. DIM(PERF_HPP__SAMPLES, "sample"),
  991. DIM(PERF_HPP__PERIOD, "period"),
  992. };
  993. #undef DIM
  994. struct hpp_sort_entry {
  995. struct perf_hpp_fmt hpp;
  996. struct sort_entry *se;
  997. };
  998. bool perf_hpp__same_sort_entry(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  999. {
  1000. struct hpp_sort_entry *hse_a;
  1001. struct hpp_sort_entry *hse_b;
  1002. if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
  1003. return false;
  1004. hse_a = container_of(a, struct hpp_sort_entry, hpp);
  1005. hse_b = container_of(b, struct hpp_sort_entry, hpp);
  1006. return hse_a->se == hse_b->se;
  1007. }
  1008. void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
  1009. {
  1010. struct hpp_sort_entry *hse;
  1011. if (!perf_hpp__is_sort_entry(fmt))
  1012. return;
  1013. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1014. hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
  1015. }
  1016. static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1017. struct perf_evsel *evsel)
  1018. {
  1019. struct hpp_sort_entry *hse;
  1020. size_t len = fmt->user_len;
  1021. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1022. if (!len)
  1023. len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
  1024. return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
  1025. }
  1026. static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
  1027. struct perf_hpp *hpp __maybe_unused,
  1028. struct perf_evsel *evsel)
  1029. {
  1030. struct hpp_sort_entry *hse;
  1031. size_t len = fmt->user_len;
  1032. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1033. if (!len)
  1034. len = hists__col_len(evsel__hists(evsel), hse->se->se_width_idx);
  1035. return len;
  1036. }
  1037. static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1038. struct hist_entry *he)
  1039. {
  1040. struct hpp_sort_entry *hse;
  1041. size_t len = fmt->user_len;
  1042. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1043. if (!len)
  1044. len = hists__col_len(he->hists, hse->se->se_width_idx);
  1045. return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
  1046. }
  1047. static struct hpp_sort_entry *
  1048. __sort_dimension__alloc_hpp(struct sort_dimension *sd)
  1049. {
  1050. struct hpp_sort_entry *hse;
  1051. hse = malloc(sizeof(*hse));
  1052. if (hse == NULL) {
  1053. pr_err("Memory allocation failed\n");
  1054. return NULL;
  1055. }
  1056. hse->se = sd->entry;
  1057. hse->hpp.name = sd->entry->se_header;
  1058. hse->hpp.header = __sort__hpp_header;
  1059. hse->hpp.width = __sort__hpp_width;
  1060. hse->hpp.entry = __sort__hpp_entry;
  1061. hse->hpp.color = NULL;
  1062. hse->hpp.cmp = sd->entry->se_cmp;
  1063. hse->hpp.collapse = sd->entry->se_collapse ? : sd->entry->se_cmp;
  1064. hse->hpp.sort = sd->entry->se_sort ? : hse->hpp.collapse;
  1065. INIT_LIST_HEAD(&hse->hpp.list);
  1066. INIT_LIST_HEAD(&hse->hpp.sort_list);
  1067. hse->hpp.elide = false;
  1068. hse->hpp.len = 0;
  1069. hse->hpp.user_len = 0;
  1070. return hse;
  1071. }
  1072. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
  1073. {
  1074. return format->header == __sort__hpp_header;
  1075. }
  1076. static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd)
  1077. {
  1078. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
  1079. if (hse == NULL)
  1080. return -1;
  1081. perf_hpp__register_sort_field(&hse->hpp);
  1082. return 0;
  1083. }
  1084. static int __sort_dimension__add_hpp_output(struct sort_dimension *sd)
  1085. {
  1086. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd);
  1087. if (hse == NULL)
  1088. return -1;
  1089. perf_hpp__column_register(&hse->hpp);
  1090. return 0;
  1091. }
  1092. static int __sort_dimension__add(struct sort_dimension *sd)
  1093. {
  1094. if (sd->taken)
  1095. return 0;
  1096. if (__sort_dimension__add_hpp_sort(sd) < 0)
  1097. return -1;
  1098. if (sd->entry->se_collapse)
  1099. sort__need_collapse = 1;
  1100. sd->taken = 1;
  1101. return 0;
  1102. }
  1103. static int __hpp_dimension__add(struct hpp_dimension *hd)
  1104. {
  1105. if (!hd->taken) {
  1106. hd->taken = 1;
  1107. perf_hpp__register_sort_field(hd->fmt);
  1108. }
  1109. return 0;
  1110. }
  1111. static int __sort_dimension__add_output(struct sort_dimension *sd)
  1112. {
  1113. if (sd->taken)
  1114. return 0;
  1115. if (__sort_dimension__add_hpp_output(sd) < 0)
  1116. return -1;
  1117. sd->taken = 1;
  1118. return 0;
  1119. }
  1120. static int __hpp_dimension__add_output(struct hpp_dimension *hd)
  1121. {
  1122. if (!hd->taken) {
  1123. hd->taken = 1;
  1124. perf_hpp__column_register(hd->fmt);
  1125. }
  1126. return 0;
  1127. }
  1128. int sort_dimension__add(const char *tok)
  1129. {
  1130. unsigned int i;
  1131. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  1132. struct sort_dimension *sd = &common_sort_dimensions[i];
  1133. if (strncasecmp(tok, sd->name, strlen(tok)))
  1134. continue;
  1135. if (sd->entry == &sort_parent) {
  1136. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  1137. if (ret) {
  1138. char err[BUFSIZ];
  1139. regerror(ret, &parent_regex, err, sizeof(err));
  1140. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  1141. return -EINVAL;
  1142. }
  1143. sort__has_parent = 1;
  1144. } else if (sd->entry == &sort_sym) {
  1145. sort__has_sym = 1;
  1146. } else if (sd->entry == &sort_dso) {
  1147. sort__has_dso = 1;
  1148. }
  1149. return __sort_dimension__add(sd);
  1150. }
  1151. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  1152. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  1153. if (strncasecmp(tok, hd->name, strlen(tok)))
  1154. continue;
  1155. return __hpp_dimension__add(hd);
  1156. }
  1157. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  1158. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  1159. if (strncasecmp(tok, sd->name, strlen(tok)))
  1160. continue;
  1161. if (sort__mode != SORT_MODE__BRANCH)
  1162. return -EINVAL;
  1163. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  1164. sort__has_sym = 1;
  1165. __sort_dimension__add(sd);
  1166. return 0;
  1167. }
  1168. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  1169. struct sort_dimension *sd = &memory_sort_dimensions[i];
  1170. if (strncasecmp(tok, sd->name, strlen(tok)))
  1171. continue;
  1172. if (sort__mode != SORT_MODE__MEMORY)
  1173. return -EINVAL;
  1174. if (sd->entry == &sort_mem_daddr_sym)
  1175. sort__has_sym = 1;
  1176. __sort_dimension__add(sd);
  1177. return 0;
  1178. }
  1179. return -ESRCH;
  1180. }
  1181. static const char *get_default_sort_order(void)
  1182. {
  1183. const char *default_sort_orders[] = {
  1184. default_sort_order,
  1185. default_branch_sort_order,
  1186. default_mem_sort_order,
  1187. default_top_sort_order,
  1188. default_diff_sort_order,
  1189. };
  1190. BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
  1191. return default_sort_orders[sort__mode];
  1192. }
  1193. static int setup_sort_order(void)
  1194. {
  1195. char *new_sort_order;
  1196. /*
  1197. * Append '+'-prefixed sort order to the default sort
  1198. * order string.
  1199. */
  1200. if (!sort_order || is_strict_order(sort_order))
  1201. return 0;
  1202. if (sort_order[1] == '\0') {
  1203. error("Invalid --sort key: `+'");
  1204. return -EINVAL;
  1205. }
  1206. /*
  1207. * We allocate new sort_order string, but we never free it,
  1208. * because it's checked over the rest of the code.
  1209. */
  1210. if (asprintf(&new_sort_order, "%s,%s",
  1211. get_default_sort_order(), sort_order + 1) < 0) {
  1212. error("Not enough memory to set up --sort");
  1213. return -ENOMEM;
  1214. }
  1215. sort_order = new_sort_order;
  1216. return 0;
  1217. }
  1218. static int __setup_sorting(void)
  1219. {
  1220. char *tmp, *tok, *str;
  1221. const char *sort_keys;
  1222. int ret = 0;
  1223. ret = setup_sort_order();
  1224. if (ret)
  1225. return ret;
  1226. sort_keys = sort_order;
  1227. if (sort_keys == NULL) {
  1228. if (is_strict_order(field_order)) {
  1229. /*
  1230. * If user specified field order but no sort order,
  1231. * we'll honor it and not add default sort orders.
  1232. */
  1233. return 0;
  1234. }
  1235. sort_keys = get_default_sort_order();
  1236. }
  1237. str = strdup(sort_keys);
  1238. if (str == NULL) {
  1239. error("Not enough memory to setup sort keys");
  1240. return -ENOMEM;
  1241. }
  1242. for (tok = strtok_r(str, ", ", &tmp);
  1243. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1244. ret = sort_dimension__add(tok);
  1245. if (ret == -EINVAL) {
  1246. error("Invalid --sort key: `%s'", tok);
  1247. break;
  1248. } else if (ret == -ESRCH) {
  1249. error("Unknown --sort key: `%s'", tok);
  1250. break;
  1251. }
  1252. }
  1253. free(str);
  1254. return ret;
  1255. }
  1256. void perf_hpp__set_elide(int idx, bool elide)
  1257. {
  1258. struct perf_hpp_fmt *fmt;
  1259. struct hpp_sort_entry *hse;
  1260. perf_hpp__for_each_format(fmt) {
  1261. if (!perf_hpp__is_sort_entry(fmt))
  1262. continue;
  1263. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1264. if (hse->se->se_width_idx == idx) {
  1265. fmt->elide = elide;
  1266. break;
  1267. }
  1268. }
  1269. }
  1270. static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
  1271. {
  1272. if (list && strlist__nr_entries(list) == 1) {
  1273. if (fp != NULL)
  1274. fprintf(fp, "# %s: %s\n", list_name,
  1275. strlist__entry(list, 0)->s);
  1276. return true;
  1277. }
  1278. return false;
  1279. }
  1280. static bool get_elide(int idx, FILE *output)
  1281. {
  1282. switch (idx) {
  1283. case HISTC_SYMBOL:
  1284. return __get_elide(symbol_conf.sym_list, "symbol", output);
  1285. case HISTC_DSO:
  1286. return __get_elide(symbol_conf.dso_list, "dso", output);
  1287. case HISTC_COMM:
  1288. return __get_elide(symbol_conf.comm_list, "comm", output);
  1289. default:
  1290. break;
  1291. }
  1292. if (sort__mode != SORT_MODE__BRANCH)
  1293. return false;
  1294. switch (idx) {
  1295. case HISTC_SYMBOL_FROM:
  1296. return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
  1297. case HISTC_SYMBOL_TO:
  1298. return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
  1299. case HISTC_DSO_FROM:
  1300. return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
  1301. case HISTC_DSO_TO:
  1302. return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
  1303. default:
  1304. break;
  1305. }
  1306. return false;
  1307. }
  1308. void sort__setup_elide(FILE *output)
  1309. {
  1310. struct perf_hpp_fmt *fmt;
  1311. struct hpp_sort_entry *hse;
  1312. perf_hpp__for_each_format(fmt) {
  1313. if (!perf_hpp__is_sort_entry(fmt))
  1314. continue;
  1315. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1316. fmt->elide = get_elide(hse->se->se_width_idx, output);
  1317. }
  1318. /*
  1319. * It makes no sense to elide all of sort entries.
  1320. * Just revert them to show up again.
  1321. */
  1322. perf_hpp__for_each_format(fmt) {
  1323. if (!perf_hpp__is_sort_entry(fmt))
  1324. continue;
  1325. if (!fmt->elide)
  1326. return;
  1327. }
  1328. perf_hpp__for_each_format(fmt) {
  1329. if (!perf_hpp__is_sort_entry(fmt))
  1330. continue;
  1331. fmt->elide = false;
  1332. }
  1333. }
  1334. static int output_field_add(char *tok)
  1335. {
  1336. unsigned int i;
  1337. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  1338. struct sort_dimension *sd = &common_sort_dimensions[i];
  1339. if (strncasecmp(tok, sd->name, strlen(tok)))
  1340. continue;
  1341. return __sort_dimension__add_output(sd);
  1342. }
  1343. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  1344. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  1345. if (strncasecmp(tok, hd->name, strlen(tok)))
  1346. continue;
  1347. return __hpp_dimension__add_output(hd);
  1348. }
  1349. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  1350. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  1351. if (strncasecmp(tok, sd->name, strlen(tok)))
  1352. continue;
  1353. return __sort_dimension__add_output(sd);
  1354. }
  1355. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  1356. struct sort_dimension *sd = &memory_sort_dimensions[i];
  1357. if (strncasecmp(tok, sd->name, strlen(tok)))
  1358. continue;
  1359. return __sort_dimension__add_output(sd);
  1360. }
  1361. return -ESRCH;
  1362. }
  1363. static void reset_dimensions(void)
  1364. {
  1365. unsigned int i;
  1366. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
  1367. common_sort_dimensions[i].taken = 0;
  1368. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
  1369. hpp_sort_dimensions[i].taken = 0;
  1370. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
  1371. bstack_sort_dimensions[i].taken = 0;
  1372. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
  1373. memory_sort_dimensions[i].taken = 0;
  1374. }
  1375. bool is_strict_order(const char *order)
  1376. {
  1377. return order && (*order != '+');
  1378. }
  1379. static int __setup_output_field(void)
  1380. {
  1381. char *tmp, *tok, *str, *strp;
  1382. int ret = -EINVAL;
  1383. if (field_order == NULL)
  1384. return 0;
  1385. reset_dimensions();
  1386. strp = str = strdup(field_order);
  1387. if (str == NULL) {
  1388. error("Not enough memory to setup output fields");
  1389. return -ENOMEM;
  1390. }
  1391. if (!is_strict_order(field_order))
  1392. strp++;
  1393. if (!strlen(strp)) {
  1394. error("Invalid --fields key: `+'");
  1395. goto out;
  1396. }
  1397. for (tok = strtok_r(strp, ", ", &tmp);
  1398. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1399. ret = output_field_add(tok);
  1400. if (ret == -EINVAL) {
  1401. error("Invalid --fields key: `%s'", tok);
  1402. break;
  1403. } else if (ret == -ESRCH) {
  1404. error("Unknown --fields key: `%s'", tok);
  1405. break;
  1406. }
  1407. }
  1408. out:
  1409. free(str);
  1410. return ret;
  1411. }
  1412. int setup_sorting(void)
  1413. {
  1414. int err;
  1415. err = __setup_sorting();
  1416. if (err < 0)
  1417. return err;
  1418. if (parent_pattern != default_parent_pattern) {
  1419. err = sort_dimension__add("parent");
  1420. if (err < 0)
  1421. return err;
  1422. }
  1423. reset_dimensions();
  1424. /*
  1425. * perf diff doesn't use default hpp output fields.
  1426. */
  1427. if (sort__mode != SORT_MODE__DIFF)
  1428. perf_hpp__init();
  1429. err = __setup_output_field();
  1430. if (err < 0)
  1431. return err;
  1432. /* copy sort keys to output fields */
  1433. perf_hpp__setup_output_field();
  1434. /* and then copy output fields to sort keys */
  1435. perf_hpp__append_sort_keys();
  1436. return 0;
  1437. }
  1438. void reset_output_field(void)
  1439. {
  1440. sort__need_collapse = 0;
  1441. sort__has_parent = 0;
  1442. sort__has_sym = 0;
  1443. sort__has_dso = 0;
  1444. field_order = NULL;
  1445. sort_order = NULL;
  1446. reset_dimensions();
  1447. perf_hpp__reset_output_field();
  1448. }