sort.c 47 KB

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