sort.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <regex.h>
  5. #include <sys/mman.h>
  6. #include "sort.h"
  7. #include "hist.h"
  8. #include "comm.h"
  9. #include "symbol.h"
  10. #include "thread.h"
  11. #include "evsel.h"
  12. #include "evlist.h"
  13. #include "strlist.h"
  14. #include <traceevent/event-parse.h>
  15. #include "mem-events.h"
  16. #include <linux/kernel.h>
  17. regex_t parent_regex;
  18. const char default_parent_pattern[] = "^sys_|^do_page_fault";
  19. const char *parent_pattern = default_parent_pattern;
  20. const char *default_sort_order = "comm,dso,symbol";
  21. const char default_branch_sort_order[] = "comm,dso_from,symbol_from,symbol_to,cycles";
  22. const char default_mem_sort_order[] = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
  23. const char default_top_sort_order[] = "dso,symbol";
  24. const char default_diff_sort_order[] = "dso,symbol";
  25. const char default_tracepoint_sort_order[] = "trace";
  26. const char *sort_order;
  27. const char *field_order;
  28. regex_t ignore_callees_regex;
  29. int have_ignore_callees = 0;
  30. enum sort_mode sort__mode = SORT_MODE__NORMAL;
  31. /*
  32. * Replaces all occurrences of a char used with the:
  33. *
  34. * -t, --field-separator
  35. *
  36. * option, that uses a special separator character and don't pad with spaces,
  37. * replacing all occurances of this separator in symbol names (and other
  38. * output) with a '.' character, that thus it's the only non valid separator.
  39. */
  40. static int repsep_snprintf(char *bf, size_t size, const char *fmt, ...)
  41. {
  42. int n;
  43. va_list ap;
  44. va_start(ap, fmt);
  45. n = vsnprintf(bf, size, fmt, ap);
  46. if (symbol_conf.field_sep && n > 0) {
  47. char *sep = bf;
  48. while (1) {
  49. sep = strchr(sep, *symbol_conf.field_sep);
  50. if (sep == NULL)
  51. break;
  52. *sep = '.';
  53. }
  54. }
  55. va_end(ap);
  56. if (n >= (int)size)
  57. return size - 1;
  58. return n;
  59. }
  60. static int64_t cmp_null(const void *l, const void *r)
  61. {
  62. if (!l && !r)
  63. return 0;
  64. else if (!l)
  65. return -1;
  66. else
  67. return 1;
  68. }
  69. /* --sort pid */
  70. static int64_t
  71. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  72. {
  73. return right->thread->tid - left->thread->tid;
  74. }
  75. static int hist_entry__thread_snprintf(struct hist_entry *he, char *bf,
  76. size_t size, unsigned int width)
  77. {
  78. const char *comm = thread__comm_str(he->thread);
  79. width = max(7U, width) - 8;
  80. return repsep_snprintf(bf, size, "%7d:%-*.*s", he->thread->tid,
  81. width, width, comm ?: "");
  82. }
  83. static int hist_entry__thread_filter(struct hist_entry *he, int type, const void *arg)
  84. {
  85. const struct thread *th = arg;
  86. if (type != HIST_FILTER__THREAD)
  87. return -1;
  88. return th && he->thread != th;
  89. }
  90. struct sort_entry sort_thread = {
  91. .se_header = " Pid:Command",
  92. .se_cmp = sort__thread_cmp,
  93. .se_snprintf = hist_entry__thread_snprintf,
  94. .se_filter = hist_entry__thread_filter,
  95. .se_width_idx = HISTC_THREAD,
  96. };
  97. /* --sort comm */
  98. /*
  99. * We can't use pointer comparison in functions below,
  100. * because it gives different results based on pointer
  101. * values, which could break some sorting assumptions.
  102. */
  103. static int64_t
  104. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  105. {
  106. return strcmp(comm__str(right->comm), comm__str(left->comm));
  107. }
  108. static int64_t
  109. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  110. {
  111. return strcmp(comm__str(right->comm), comm__str(left->comm));
  112. }
  113. static int64_t
  114. sort__comm_sort(struct hist_entry *left, struct hist_entry *right)
  115. {
  116. return strcmp(comm__str(right->comm), comm__str(left->comm));
  117. }
  118. static int hist_entry__comm_snprintf(struct hist_entry *he, char *bf,
  119. size_t size, unsigned int width)
  120. {
  121. return repsep_snprintf(bf, size, "%-*.*s", width, width, comm__str(he->comm));
  122. }
  123. struct sort_entry sort_comm = {
  124. .se_header = "Command",
  125. .se_cmp = sort__comm_cmp,
  126. .se_collapse = sort__comm_collapse,
  127. .se_sort = sort__comm_sort,
  128. .se_snprintf = hist_entry__comm_snprintf,
  129. .se_filter = hist_entry__thread_filter,
  130. .se_width_idx = HISTC_COMM,
  131. };
  132. /* --sort dso */
  133. static int64_t _sort__dso_cmp(struct map *map_l, struct map *map_r)
  134. {
  135. struct dso *dso_l = map_l ? map_l->dso : NULL;
  136. struct dso *dso_r = map_r ? map_r->dso : NULL;
  137. const char *dso_name_l, *dso_name_r;
  138. if (!dso_l || !dso_r)
  139. return cmp_null(dso_r, dso_l);
  140. if (verbose > 0) {
  141. dso_name_l = dso_l->long_name;
  142. dso_name_r = dso_r->long_name;
  143. } else {
  144. dso_name_l = dso_l->short_name;
  145. dso_name_r = dso_r->short_name;
  146. }
  147. return strcmp(dso_name_l, dso_name_r);
  148. }
  149. static int64_t
  150. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  151. {
  152. return _sort__dso_cmp(right->ms.map, left->ms.map);
  153. }
  154. static int _hist_entry__dso_snprintf(struct map *map, char *bf,
  155. size_t size, unsigned int width)
  156. {
  157. if (map && map->dso) {
  158. const char *dso_name = verbose > 0 ? map->dso->long_name :
  159. map->dso->short_name;
  160. return repsep_snprintf(bf, size, "%-*.*s", width, width, dso_name);
  161. }
  162. return repsep_snprintf(bf, size, "%-*.*s", width, width, "[unknown]");
  163. }
  164. static int hist_entry__dso_snprintf(struct hist_entry *he, char *bf,
  165. size_t size, unsigned int width)
  166. {
  167. return _hist_entry__dso_snprintf(he->ms.map, bf, size, width);
  168. }
  169. static int hist_entry__dso_filter(struct hist_entry *he, int type, const void *arg)
  170. {
  171. const struct dso *dso = arg;
  172. if (type != HIST_FILTER__DSO)
  173. return -1;
  174. return dso && (!he->ms.map || he->ms.map->dso != dso);
  175. }
  176. struct sort_entry sort_dso = {
  177. .se_header = "Shared Object",
  178. .se_cmp = sort__dso_cmp,
  179. .se_snprintf = hist_entry__dso_snprintf,
  180. .se_filter = hist_entry__dso_filter,
  181. .se_width_idx = HISTC_DSO,
  182. };
  183. /* --sort symbol */
  184. static int64_t _sort__addr_cmp(u64 left_ip, u64 right_ip)
  185. {
  186. return (int64_t)(right_ip - left_ip);
  187. }
  188. static int64_t _sort__sym_cmp(struct symbol *sym_l, struct symbol *sym_r)
  189. {
  190. if (!sym_l || !sym_r)
  191. return cmp_null(sym_l, sym_r);
  192. if (sym_l == sym_r)
  193. return 0;
  194. if (sym_l->inlined || sym_r->inlined)
  195. return strcmp(sym_l->name, sym_r->name);
  196. if (sym_l->start != sym_r->start)
  197. return (int64_t)(sym_r->start - sym_l->start);
  198. return (int64_t)(sym_r->end - sym_l->end);
  199. }
  200. static int64_t
  201. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  202. {
  203. int64_t ret;
  204. if (!left->ms.sym && !right->ms.sym)
  205. return _sort__addr_cmp(left->ip, right->ip);
  206. /*
  207. * comparing symbol address alone is not enough since it's a
  208. * relative address within a dso.
  209. */
  210. if (!hists__has(left->hists, dso) || hists__has(right->hists, dso)) {
  211. ret = sort__dso_cmp(left, right);
  212. if (ret != 0)
  213. return ret;
  214. }
  215. return _sort__sym_cmp(left->ms.sym, right->ms.sym);
  216. }
  217. static int64_t
  218. sort__sym_sort(struct hist_entry *left, struct hist_entry *right)
  219. {
  220. if (!left->ms.sym || !right->ms.sym)
  221. return cmp_null(left->ms.sym, right->ms.sym);
  222. return strcmp(right->ms.sym->name, left->ms.sym->name);
  223. }
  224. static int _hist_entry__sym_snprintf(struct map *map, struct symbol *sym,
  225. u64 ip, char level, char *bf, size_t size,
  226. unsigned int width)
  227. {
  228. size_t ret = 0;
  229. if (verbose > 0) {
  230. char o = map ? dso__symtab_origin(map->dso) : '!';
  231. ret += repsep_snprintf(bf, size, "%-#*llx %c ",
  232. BITS_PER_LONG / 4 + 2, ip, o);
  233. }
  234. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", level);
  235. if (sym && map) {
  236. if (map->type == MAP__VARIABLE) {
  237. ret += repsep_snprintf(bf + ret, size - ret, "%s", sym->name);
  238. ret += repsep_snprintf(bf + ret, size - ret, "+0x%llx",
  239. ip - map->unmap_ip(map, sym->start));
  240. } else {
  241. ret += repsep_snprintf(bf + ret, size - ret, "%.*s",
  242. width - ret,
  243. sym->name);
  244. if (sym->inlined)
  245. ret += repsep_snprintf(bf + ret, size - ret,
  246. " (inlined)");
  247. }
  248. } else {
  249. size_t len = BITS_PER_LONG / 4;
  250. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx",
  251. len, ip);
  252. }
  253. return ret;
  254. }
  255. static int hist_entry__sym_snprintf(struct hist_entry *he, char *bf,
  256. size_t size, unsigned int width)
  257. {
  258. return _hist_entry__sym_snprintf(he->ms.map, he->ms.sym, he->ip,
  259. he->level, bf, size, width);
  260. }
  261. static int hist_entry__sym_filter(struct hist_entry *he, int type, const void *arg)
  262. {
  263. const char *sym = arg;
  264. if (type != HIST_FILTER__SYMBOL)
  265. return -1;
  266. return sym && (!he->ms.sym || !strstr(he->ms.sym->name, sym));
  267. }
  268. struct sort_entry sort_sym = {
  269. .se_header = "Symbol",
  270. .se_cmp = sort__sym_cmp,
  271. .se_sort = sort__sym_sort,
  272. .se_snprintf = hist_entry__sym_snprintf,
  273. .se_filter = hist_entry__sym_filter,
  274. .se_width_idx = HISTC_SYMBOL,
  275. };
  276. /* --sort srcline */
  277. char *hist_entry__get_srcline(struct hist_entry *he)
  278. {
  279. struct map *map = he->ms.map;
  280. if (!map)
  281. return SRCLINE_UNKNOWN;
  282. return get_srcline(map->dso, map__rip_2objdump(map, he->ip),
  283. he->ms.sym, true, true, he->ip);
  284. }
  285. static int64_t
  286. sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
  287. {
  288. if (!left->srcline)
  289. left->srcline = hist_entry__get_srcline(left);
  290. if (!right->srcline)
  291. right->srcline = hist_entry__get_srcline(right);
  292. return strcmp(right->srcline, left->srcline);
  293. }
  294. static int hist_entry__srcline_snprintf(struct hist_entry *he, char *bf,
  295. size_t size, unsigned int width)
  296. {
  297. if (!he->srcline)
  298. he->srcline = hist_entry__get_srcline(he);
  299. return repsep_snprintf(bf, size, "%-.*s", width, he->srcline);
  300. }
  301. struct sort_entry sort_srcline = {
  302. .se_header = "Source:Line",
  303. .se_cmp = sort__srcline_cmp,
  304. .se_snprintf = hist_entry__srcline_snprintf,
  305. .se_width_idx = HISTC_SRCLINE,
  306. };
  307. /* --sort srcline_from */
  308. static int64_t
  309. sort__srcline_from_cmp(struct hist_entry *left, struct hist_entry *right)
  310. {
  311. if (!left->branch_info->srcline_from) {
  312. struct map *map = left->branch_info->from.map;
  313. if (!map)
  314. left->branch_info->srcline_from = SRCLINE_UNKNOWN;
  315. else
  316. left->branch_info->srcline_from = get_srcline(map->dso,
  317. map__rip_2objdump(map,
  318. left->branch_info->from.al_addr),
  319. left->branch_info->from.sym,
  320. true, true,
  321. left->branch_info->from.al_addr);
  322. }
  323. if (!right->branch_info->srcline_from) {
  324. struct map *map = right->branch_info->from.map;
  325. if (!map)
  326. right->branch_info->srcline_from = SRCLINE_UNKNOWN;
  327. else
  328. right->branch_info->srcline_from = get_srcline(map->dso,
  329. map__rip_2objdump(map,
  330. right->branch_info->from.al_addr),
  331. right->branch_info->from.sym,
  332. true, true,
  333. right->branch_info->from.al_addr);
  334. }
  335. return strcmp(right->branch_info->srcline_from, left->branch_info->srcline_from);
  336. }
  337. static int hist_entry__srcline_from_snprintf(struct hist_entry *he, char *bf,
  338. size_t size, unsigned int width)
  339. {
  340. return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_from);
  341. }
  342. struct sort_entry sort_srcline_from = {
  343. .se_header = "From Source:Line",
  344. .se_cmp = sort__srcline_from_cmp,
  345. .se_snprintf = hist_entry__srcline_from_snprintf,
  346. .se_width_idx = HISTC_SRCLINE_FROM,
  347. };
  348. /* --sort srcline_to */
  349. static int64_t
  350. sort__srcline_to_cmp(struct hist_entry *left, struct hist_entry *right)
  351. {
  352. if (!left->branch_info->srcline_to) {
  353. struct map *map = left->branch_info->to.map;
  354. if (!map)
  355. left->branch_info->srcline_to = SRCLINE_UNKNOWN;
  356. else
  357. left->branch_info->srcline_to = get_srcline(map->dso,
  358. map__rip_2objdump(map,
  359. left->branch_info->to.al_addr),
  360. left->branch_info->from.sym,
  361. true, true,
  362. left->branch_info->to.al_addr);
  363. }
  364. if (!right->branch_info->srcline_to) {
  365. struct map *map = right->branch_info->to.map;
  366. if (!map)
  367. right->branch_info->srcline_to = SRCLINE_UNKNOWN;
  368. else
  369. right->branch_info->srcline_to = get_srcline(map->dso,
  370. map__rip_2objdump(map,
  371. right->branch_info->to.al_addr),
  372. right->branch_info->to.sym,
  373. true, true,
  374. right->branch_info->to.al_addr);
  375. }
  376. return strcmp(right->branch_info->srcline_to, left->branch_info->srcline_to);
  377. }
  378. static int hist_entry__srcline_to_snprintf(struct hist_entry *he, char *bf,
  379. size_t size, unsigned int width)
  380. {
  381. return repsep_snprintf(bf, size, "%-*.*s", width, width, he->branch_info->srcline_to);
  382. }
  383. struct sort_entry sort_srcline_to = {
  384. .se_header = "To Source:Line",
  385. .se_cmp = sort__srcline_to_cmp,
  386. .se_snprintf = hist_entry__srcline_to_snprintf,
  387. .se_width_idx = HISTC_SRCLINE_TO,
  388. };
  389. /* --sort srcfile */
  390. static char no_srcfile[1];
  391. static char *hist_entry__get_srcfile(struct hist_entry *e)
  392. {
  393. char *sf, *p;
  394. struct map *map = e->ms.map;
  395. if (!map)
  396. return no_srcfile;
  397. sf = __get_srcline(map->dso, map__rip_2objdump(map, e->ip),
  398. e->ms.sym, false, true, true, e->ip);
  399. if (!strcmp(sf, SRCLINE_UNKNOWN))
  400. return no_srcfile;
  401. p = strchr(sf, ':');
  402. if (p && *sf) {
  403. *p = 0;
  404. return sf;
  405. }
  406. free(sf);
  407. return no_srcfile;
  408. }
  409. static int64_t
  410. sort__srcfile_cmp(struct hist_entry *left, struct hist_entry *right)
  411. {
  412. if (!left->srcfile)
  413. left->srcfile = hist_entry__get_srcfile(left);
  414. if (!right->srcfile)
  415. right->srcfile = hist_entry__get_srcfile(right);
  416. return strcmp(right->srcfile, left->srcfile);
  417. }
  418. static int hist_entry__srcfile_snprintf(struct hist_entry *he, char *bf,
  419. size_t size, unsigned int width)
  420. {
  421. if (!he->srcfile)
  422. he->srcfile = hist_entry__get_srcfile(he);
  423. return repsep_snprintf(bf, size, "%-.*s", width, he->srcfile);
  424. }
  425. struct sort_entry sort_srcfile = {
  426. .se_header = "Source File",
  427. .se_cmp = sort__srcfile_cmp,
  428. .se_snprintf = hist_entry__srcfile_snprintf,
  429. .se_width_idx = HISTC_SRCFILE,
  430. };
  431. /* --sort parent */
  432. static int64_t
  433. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  434. {
  435. struct symbol *sym_l = left->parent;
  436. struct symbol *sym_r = right->parent;
  437. if (!sym_l || !sym_r)
  438. return cmp_null(sym_l, sym_r);
  439. return strcmp(sym_r->name, sym_l->name);
  440. }
  441. static int hist_entry__parent_snprintf(struct hist_entry *he, char *bf,
  442. size_t size, unsigned int width)
  443. {
  444. return repsep_snprintf(bf, size, "%-*.*s", width, width,
  445. he->parent ? he->parent->name : "[other]");
  446. }
  447. struct sort_entry sort_parent = {
  448. .se_header = "Parent symbol",
  449. .se_cmp = sort__parent_cmp,
  450. .se_snprintf = hist_entry__parent_snprintf,
  451. .se_width_idx = HISTC_PARENT,
  452. };
  453. /* --sort cpu */
  454. static int64_t
  455. sort__cpu_cmp(struct hist_entry *left, struct hist_entry *right)
  456. {
  457. return right->cpu - left->cpu;
  458. }
  459. static int hist_entry__cpu_snprintf(struct hist_entry *he, char *bf,
  460. size_t size, unsigned int width)
  461. {
  462. return repsep_snprintf(bf, size, "%*.*d", width, width, he->cpu);
  463. }
  464. struct sort_entry sort_cpu = {
  465. .se_header = "CPU",
  466. .se_cmp = sort__cpu_cmp,
  467. .se_snprintf = hist_entry__cpu_snprintf,
  468. .se_width_idx = HISTC_CPU,
  469. };
  470. /* --sort cgroup_id */
  471. static int64_t _sort__cgroup_dev_cmp(u64 left_dev, u64 right_dev)
  472. {
  473. return (int64_t)(right_dev - left_dev);
  474. }
  475. static int64_t _sort__cgroup_inode_cmp(u64 left_ino, u64 right_ino)
  476. {
  477. return (int64_t)(right_ino - left_ino);
  478. }
  479. static int64_t
  480. sort__cgroup_id_cmp(struct hist_entry *left, struct hist_entry *right)
  481. {
  482. int64_t ret;
  483. ret = _sort__cgroup_dev_cmp(right->cgroup_id.dev, left->cgroup_id.dev);
  484. if (ret != 0)
  485. return ret;
  486. return _sort__cgroup_inode_cmp(right->cgroup_id.ino,
  487. left->cgroup_id.ino);
  488. }
  489. static int hist_entry__cgroup_id_snprintf(struct hist_entry *he,
  490. char *bf, size_t size,
  491. unsigned int width __maybe_unused)
  492. {
  493. return repsep_snprintf(bf, size, "%lu/0x%lx", he->cgroup_id.dev,
  494. he->cgroup_id.ino);
  495. }
  496. struct sort_entry sort_cgroup_id = {
  497. .se_header = "cgroup id (dev/inode)",
  498. .se_cmp = sort__cgroup_id_cmp,
  499. .se_snprintf = hist_entry__cgroup_id_snprintf,
  500. .se_width_idx = HISTC_CGROUP_ID,
  501. };
  502. /* --sort socket */
  503. static int64_t
  504. sort__socket_cmp(struct hist_entry *left, struct hist_entry *right)
  505. {
  506. return right->socket - left->socket;
  507. }
  508. static int hist_entry__socket_snprintf(struct hist_entry *he, char *bf,
  509. size_t size, unsigned int width)
  510. {
  511. return repsep_snprintf(bf, size, "%*.*d", width, width-3, he->socket);
  512. }
  513. static int hist_entry__socket_filter(struct hist_entry *he, int type, const void *arg)
  514. {
  515. int sk = *(const int *)arg;
  516. if (type != HIST_FILTER__SOCKET)
  517. return -1;
  518. return sk >= 0 && he->socket != sk;
  519. }
  520. struct sort_entry sort_socket = {
  521. .se_header = "Socket",
  522. .se_cmp = sort__socket_cmp,
  523. .se_snprintf = hist_entry__socket_snprintf,
  524. .se_filter = hist_entry__socket_filter,
  525. .se_width_idx = HISTC_SOCKET,
  526. };
  527. /* --sort trace */
  528. static char *get_trace_output(struct hist_entry *he)
  529. {
  530. struct trace_seq seq;
  531. struct perf_evsel *evsel;
  532. struct pevent_record rec = {
  533. .data = he->raw_data,
  534. .size = he->raw_size,
  535. };
  536. evsel = hists_to_evsel(he->hists);
  537. trace_seq_init(&seq);
  538. if (symbol_conf.raw_trace) {
  539. pevent_print_fields(&seq, he->raw_data, he->raw_size,
  540. evsel->tp_format);
  541. } else {
  542. pevent_event_info(&seq, evsel->tp_format, &rec);
  543. }
  544. /*
  545. * Trim the buffer, it starts at 4KB and we're not going to
  546. * add anything more to this buffer.
  547. */
  548. return realloc(seq.buffer, seq.len + 1);
  549. }
  550. static int64_t
  551. sort__trace_cmp(struct hist_entry *left, struct hist_entry *right)
  552. {
  553. struct perf_evsel *evsel;
  554. evsel = hists_to_evsel(left->hists);
  555. if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
  556. return 0;
  557. if (left->trace_output == NULL)
  558. left->trace_output = get_trace_output(left);
  559. if (right->trace_output == NULL)
  560. right->trace_output = get_trace_output(right);
  561. return strcmp(right->trace_output, left->trace_output);
  562. }
  563. static int hist_entry__trace_snprintf(struct hist_entry *he, char *bf,
  564. size_t size, unsigned int width)
  565. {
  566. struct perf_evsel *evsel;
  567. evsel = hists_to_evsel(he->hists);
  568. if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
  569. return scnprintf(bf, size, "%-.*s", width, "N/A");
  570. if (he->trace_output == NULL)
  571. he->trace_output = get_trace_output(he);
  572. return repsep_snprintf(bf, size, "%-.*s", width, he->trace_output);
  573. }
  574. struct sort_entry sort_trace = {
  575. .se_header = "Trace output",
  576. .se_cmp = sort__trace_cmp,
  577. .se_snprintf = hist_entry__trace_snprintf,
  578. .se_width_idx = HISTC_TRACE,
  579. };
  580. /* sort keys for branch stacks */
  581. static int64_t
  582. sort__dso_from_cmp(struct hist_entry *left, struct hist_entry *right)
  583. {
  584. if (!left->branch_info || !right->branch_info)
  585. return cmp_null(left->branch_info, right->branch_info);
  586. return _sort__dso_cmp(left->branch_info->from.map,
  587. right->branch_info->from.map);
  588. }
  589. static int hist_entry__dso_from_snprintf(struct hist_entry *he, char *bf,
  590. size_t size, unsigned int width)
  591. {
  592. if (he->branch_info)
  593. return _hist_entry__dso_snprintf(he->branch_info->from.map,
  594. bf, size, width);
  595. else
  596. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  597. }
  598. static int hist_entry__dso_from_filter(struct hist_entry *he, int type,
  599. const void *arg)
  600. {
  601. const struct dso *dso = arg;
  602. if (type != HIST_FILTER__DSO)
  603. return -1;
  604. return dso && (!he->branch_info || !he->branch_info->from.map ||
  605. he->branch_info->from.map->dso != dso);
  606. }
  607. static int64_t
  608. sort__dso_to_cmp(struct hist_entry *left, struct hist_entry *right)
  609. {
  610. if (!left->branch_info || !right->branch_info)
  611. return cmp_null(left->branch_info, right->branch_info);
  612. return _sort__dso_cmp(left->branch_info->to.map,
  613. right->branch_info->to.map);
  614. }
  615. static int hist_entry__dso_to_snprintf(struct hist_entry *he, char *bf,
  616. size_t size, unsigned int width)
  617. {
  618. if (he->branch_info)
  619. return _hist_entry__dso_snprintf(he->branch_info->to.map,
  620. bf, size, width);
  621. else
  622. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  623. }
  624. static int hist_entry__dso_to_filter(struct hist_entry *he, int type,
  625. const void *arg)
  626. {
  627. const struct dso *dso = arg;
  628. if (type != HIST_FILTER__DSO)
  629. return -1;
  630. return dso && (!he->branch_info || !he->branch_info->to.map ||
  631. he->branch_info->to.map->dso != dso);
  632. }
  633. static int64_t
  634. sort__sym_from_cmp(struct hist_entry *left, struct hist_entry *right)
  635. {
  636. struct addr_map_symbol *from_l = &left->branch_info->from;
  637. struct addr_map_symbol *from_r = &right->branch_info->from;
  638. if (!left->branch_info || !right->branch_info)
  639. return cmp_null(left->branch_info, right->branch_info);
  640. from_l = &left->branch_info->from;
  641. from_r = &right->branch_info->from;
  642. if (!from_l->sym && !from_r->sym)
  643. return _sort__addr_cmp(from_l->addr, from_r->addr);
  644. return _sort__sym_cmp(from_l->sym, from_r->sym);
  645. }
  646. static int64_t
  647. sort__sym_to_cmp(struct hist_entry *left, struct hist_entry *right)
  648. {
  649. struct addr_map_symbol *to_l, *to_r;
  650. if (!left->branch_info || !right->branch_info)
  651. return cmp_null(left->branch_info, right->branch_info);
  652. to_l = &left->branch_info->to;
  653. to_r = &right->branch_info->to;
  654. if (!to_l->sym && !to_r->sym)
  655. return _sort__addr_cmp(to_l->addr, to_r->addr);
  656. return _sort__sym_cmp(to_l->sym, to_r->sym);
  657. }
  658. static int hist_entry__sym_from_snprintf(struct hist_entry *he, char *bf,
  659. size_t size, unsigned int width)
  660. {
  661. if (he->branch_info) {
  662. struct addr_map_symbol *from = &he->branch_info->from;
  663. return _hist_entry__sym_snprintf(from->map, from->sym, from->addr,
  664. he->level, bf, size, width);
  665. }
  666. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  667. }
  668. static int hist_entry__sym_to_snprintf(struct hist_entry *he, char *bf,
  669. size_t size, unsigned int width)
  670. {
  671. if (he->branch_info) {
  672. struct addr_map_symbol *to = &he->branch_info->to;
  673. return _hist_entry__sym_snprintf(to->map, to->sym, to->addr,
  674. he->level, bf, size, width);
  675. }
  676. return repsep_snprintf(bf, size, "%-*.*s", width, width, "N/A");
  677. }
  678. static int hist_entry__sym_from_filter(struct hist_entry *he, int type,
  679. const void *arg)
  680. {
  681. const char *sym = arg;
  682. if (type != HIST_FILTER__SYMBOL)
  683. return -1;
  684. return sym && !(he->branch_info && he->branch_info->from.sym &&
  685. strstr(he->branch_info->from.sym->name, sym));
  686. }
  687. static int hist_entry__sym_to_filter(struct hist_entry *he, int type,
  688. const void *arg)
  689. {
  690. const char *sym = arg;
  691. if (type != HIST_FILTER__SYMBOL)
  692. return -1;
  693. return sym && !(he->branch_info && he->branch_info->to.sym &&
  694. strstr(he->branch_info->to.sym->name, sym));
  695. }
  696. struct sort_entry sort_dso_from = {
  697. .se_header = "Source Shared Object",
  698. .se_cmp = sort__dso_from_cmp,
  699. .se_snprintf = hist_entry__dso_from_snprintf,
  700. .se_filter = hist_entry__dso_from_filter,
  701. .se_width_idx = HISTC_DSO_FROM,
  702. };
  703. struct sort_entry sort_dso_to = {
  704. .se_header = "Target Shared Object",
  705. .se_cmp = sort__dso_to_cmp,
  706. .se_snprintf = hist_entry__dso_to_snprintf,
  707. .se_filter = hist_entry__dso_to_filter,
  708. .se_width_idx = HISTC_DSO_TO,
  709. };
  710. struct sort_entry sort_sym_from = {
  711. .se_header = "Source Symbol",
  712. .se_cmp = sort__sym_from_cmp,
  713. .se_snprintf = hist_entry__sym_from_snprintf,
  714. .se_filter = hist_entry__sym_from_filter,
  715. .se_width_idx = HISTC_SYMBOL_FROM,
  716. };
  717. struct sort_entry sort_sym_to = {
  718. .se_header = "Target Symbol",
  719. .se_cmp = sort__sym_to_cmp,
  720. .se_snprintf = hist_entry__sym_to_snprintf,
  721. .se_filter = hist_entry__sym_to_filter,
  722. .se_width_idx = HISTC_SYMBOL_TO,
  723. };
  724. static int64_t
  725. sort__mispredict_cmp(struct hist_entry *left, struct hist_entry *right)
  726. {
  727. unsigned char mp, p;
  728. if (!left->branch_info || !right->branch_info)
  729. return cmp_null(left->branch_info, right->branch_info);
  730. mp = left->branch_info->flags.mispred != right->branch_info->flags.mispred;
  731. p = left->branch_info->flags.predicted != right->branch_info->flags.predicted;
  732. return mp || p;
  733. }
  734. static int hist_entry__mispredict_snprintf(struct hist_entry *he, char *bf,
  735. size_t size, unsigned int width){
  736. static const char *out = "N/A";
  737. if (he->branch_info) {
  738. if (he->branch_info->flags.predicted)
  739. out = "N";
  740. else if (he->branch_info->flags.mispred)
  741. out = "Y";
  742. }
  743. return repsep_snprintf(bf, size, "%-*.*s", width, width, out);
  744. }
  745. static int64_t
  746. sort__cycles_cmp(struct hist_entry *left, struct hist_entry *right)
  747. {
  748. if (!left->branch_info || !right->branch_info)
  749. return cmp_null(left->branch_info, right->branch_info);
  750. return left->branch_info->flags.cycles -
  751. right->branch_info->flags.cycles;
  752. }
  753. static int hist_entry__cycles_snprintf(struct hist_entry *he, char *bf,
  754. size_t size, unsigned int width)
  755. {
  756. if (!he->branch_info)
  757. return scnprintf(bf, size, "%-.*s", width, "N/A");
  758. if (he->branch_info->flags.cycles == 0)
  759. return repsep_snprintf(bf, size, "%-*s", width, "-");
  760. return repsep_snprintf(bf, size, "%-*hd", width,
  761. he->branch_info->flags.cycles);
  762. }
  763. struct sort_entry sort_cycles = {
  764. .se_header = "Basic Block Cycles",
  765. .se_cmp = sort__cycles_cmp,
  766. .se_snprintf = hist_entry__cycles_snprintf,
  767. .se_width_idx = HISTC_CYCLES,
  768. };
  769. /* --sort daddr_sym */
  770. int64_t
  771. sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  772. {
  773. uint64_t l = 0, r = 0;
  774. if (left->mem_info)
  775. l = left->mem_info->daddr.addr;
  776. if (right->mem_info)
  777. r = right->mem_info->daddr.addr;
  778. return (int64_t)(r - l);
  779. }
  780. static int hist_entry__daddr_snprintf(struct hist_entry *he, char *bf,
  781. size_t size, unsigned int width)
  782. {
  783. uint64_t addr = 0;
  784. struct map *map = NULL;
  785. struct symbol *sym = NULL;
  786. if (he->mem_info) {
  787. addr = he->mem_info->daddr.addr;
  788. map = he->mem_info->daddr.map;
  789. sym = he->mem_info->daddr.sym;
  790. }
  791. return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
  792. width);
  793. }
  794. int64_t
  795. sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right)
  796. {
  797. uint64_t l = 0, r = 0;
  798. if (left->mem_info)
  799. l = left->mem_info->iaddr.addr;
  800. if (right->mem_info)
  801. r = right->mem_info->iaddr.addr;
  802. return (int64_t)(r - l);
  803. }
  804. static int hist_entry__iaddr_snprintf(struct hist_entry *he, char *bf,
  805. size_t size, unsigned int width)
  806. {
  807. uint64_t addr = 0;
  808. struct map *map = NULL;
  809. struct symbol *sym = NULL;
  810. if (he->mem_info) {
  811. addr = he->mem_info->iaddr.addr;
  812. map = he->mem_info->iaddr.map;
  813. sym = he->mem_info->iaddr.sym;
  814. }
  815. return _hist_entry__sym_snprintf(map, sym, addr, he->level, bf, size,
  816. width);
  817. }
  818. static int64_t
  819. sort__dso_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  820. {
  821. struct map *map_l = NULL;
  822. struct map *map_r = NULL;
  823. if (left->mem_info)
  824. map_l = left->mem_info->daddr.map;
  825. if (right->mem_info)
  826. map_r = right->mem_info->daddr.map;
  827. return _sort__dso_cmp(map_l, map_r);
  828. }
  829. static int hist_entry__dso_daddr_snprintf(struct hist_entry *he, char *bf,
  830. size_t size, unsigned int width)
  831. {
  832. struct map *map = NULL;
  833. if (he->mem_info)
  834. map = he->mem_info->daddr.map;
  835. return _hist_entry__dso_snprintf(map, bf, size, width);
  836. }
  837. static int64_t
  838. sort__locked_cmp(struct hist_entry *left, struct hist_entry *right)
  839. {
  840. union perf_mem_data_src data_src_l;
  841. union perf_mem_data_src data_src_r;
  842. if (left->mem_info)
  843. data_src_l = left->mem_info->data_src;
  844. else
  845. data_src_l.mem_lock = PERF_MEM_LOCK_NA;
  846. if (right->mem_info)
  847. data_src_r = right->mem_info->data_src;
  848. else
  849. data_src_r.mem_lock = PERF_MEM_LOCK_NA;
  850. return (int64_t)(data_src_r.mem_lock - data_src_l.mem_lock);
  851. }
  852. static int hist_entry__locked_snprintf(struct hist_entry *he, char *bf,
  853. size_t size, unsigned int width)
  854. {
  855. char out[10];
  856. perf_mem__lck_scnprintf(out, sizeof(out), he->mem_info);
  857. return repsep_snprintf(bf, size, "%.*s", width, out);
  858. }
  859. static int64_t
  860. sort__tlb_cmp(struct hist_entry *left, struct hist_entry *right)
  861. {
  862. union perf_mem_data_src data_src_l;
  863. union perf_mem_data_src data_src_r;
  864. if (left->mem_info)
  865. data_src_l = left->mem_info->data_src;
  866. else
  867. data_src_l.mem_dtlb = PERF_MEM_TLB_NA;
  868. if (right->mem_info)
  869. data_src_r = right->mem_info->data_src;
  870. else
  871. data_src_r.mem_dtlb = PERF_MEM_TLB_NA;
  872. return (int64_t)(data_src_r.mem_dtlb - data_src_l.mem_dtlb);
  873. }
  874. static int hist_entry__tlb_snprintf(struct hist_entry *he, char *bf,
  875. size_t size, unsigned int width)
  876. {
  877. char out[64];
  878. perf_mem__tlb_scnprintf(out, sizeof(out), he->mem_info);
  879. return repsep_snprintf(bf, size, "%-*s", width, out);
  880. }
  881. static int64_t
  882. sort__lvl_cmp(struct hist_entry *left, struct hist_entry *right)
  883. {
  884. union perf_mem_data_src data_src_l;
  885. union perf_mem_data_src data_src_r;
  886. if (left->mem_info)
  887. data_src_l = left->mem_info->data_src;
  888. else
  889. data_src_l.mem_lvl = PERF_MEM_LVL_NA;
  890. if (right->mem_info)
  891. data_src_r = right->mem_info->data_src;
  892. else
  893. data_src_r.mem_lvl = PERF_MEM_LVL_NA;
  894. return (int64_t)(data_src_r.mem_lvl - data_src_l.mem_lvl);
  895. }
  896. static int hist_entry__lvl_snprintf(struct hist_entry *he, char *bf,
  897. size_t size, unsigned int width)
  898. {
  899. char out[64];
  900. perf_mem__lvl_scnprintf(out, sizeof(out), he->mem_info);
  901. return repsep_snprintf(bf, size, "%-*s", width, out);
  902. }
  903. static int64_t
  904. sort__snoop_cmp(struct hist_entry *left, struct hist_entry *right)
  905. {
  906. union perf_mem_data_src data_src_l;
  907. union perf_mem_data_src data_src_r;
  908. if (left->mem_info)
  909. data_src_l = left->mem_info->data_src;
  910. else
  911. data_src_l.mem_snoop = PERF_MEM_SNOOP_NA;
  912. if (right->mem_info)
  913. data_src_r = right->mem_info->data_src;
  914. else
  915. data_src_r.mem_snoop = PERF_MEM_SNOOP_NA;
  916. return (int64_t)(data_src_r.mem_snoop - data_src_l.mem_snoop);
  917. }
  918. static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
  919. size_t size, unsigned int width)
  920. {
  921. char out[64];
  922. perf_mem__snp_scnprintf(out, sizeof(out), he->mem_info);
  923. return repsep_snprintf(bf, size, "%-*s", width, out);
  924. }
  925. int64_t
  926. sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right)
  927. {
  928. u64 l, r;
  929. struct map *l_map, *r_map;
  930. if (!left->mem_info) return -1;
  931. if (!right->mem_info) return 1;
  932. /* group event types together */
  933. if (left->cpumode > right->cpumode) return -1;
  934. if (left->cpumode < right->cpumode) return 1;
  935. l_map = left->mem_info->daddr.map;
  936. r_map = right->mem_info->daddr.map;
  937. /* if both are NULL, jump to sort on al_addr instead */
  938. if (!l_map && !r_map)
  939. goto addr;
  940. if (!l_map) return -1;
  941. if (!r_map) return 1;
  942. if (l_map->maj > r_map->maj) return -1;
  943. if (l_map->maj < r_map->maj) return 1;
  944. if (l_map->min > r_map->min) return -1;
  945. if (l_map->min < r_map->min) return 1;
  946. if (l_map->ino > r_map->ino) return -1;
  947. if (l_map->ino < r_map->ino) return 1;
  948. if (l_map->ino_generation > r_map->ino_generation) return -1;
  949. if (l_map->ino_generation < r_map->ino_generation) return 1;
  950. /*
  951. * Addresses with no major/minor numbers are assumed to be
  952. * anonymous in userspace. Sort those on pid then address.
  953. *
  954. * The kernel and non-zero major/minor mapped areas are
  955. * assumed to be unity mapped. Sort those on address.
  956. */
  957. if ((left->cpumode != PERF_RECORD_MISC_KERNEL) &&
  958. (!(l_map->flags & MAP_SHARED)) &&
  959. !l_map->maj && !l_map->min && !l_map->ino &&
  960. !l_map->ino_generation) {
  961. /* userspace anonymous */
  962. if (left->thread->pid_ > right->thread->pid_) return -1;
  963. if (left->thread->pid_ < right->thread->pid_) return 1;
  964. }
  965. addr:
  966. /* al_addr does all the right addr - start + offset calculations */
  967. l = cl_address(left->mem_info->daddr.al_addr);
  968. r = cl_address(right->mem_info->daddr.al_addr);
  969. if (l > r) return -1;
  970. if (l < r) return 1;
  971. return 0;
  972. }
  973. static int hist_entry__dcacheline_snprintf(struct hist_entry *he, char *bf,
  974. size_t size, unsigned int width)
  975. {
  976. uint64_t addr = 0;
  977. struct map *map = NULL;
  978. struct symbol *sym = NULL;
  979. char level = he->level;
  980. if (he->mem_info) {
  981. addr = cl_address(he->mem_info->daddr.al_addr);
  982. map = he->mem_info->daddr.map;
  983. sym = he->mem_info->daddr.sym;
  984. /* print [s] for shared data mmaps */
  985. if ((he->cpumode != PERF_RECORD_MISC_KERNEL) &&
  986. map && (map->type == MAP__VARIABLE) &&
  987. (map->flags & MAP_SHARED) &&
  988. (map->maj || map->min || map->ino ||
  989. map->ino_generation))
  990. level = 's';
  991. else if (!map)
  992. level = 'X';
  993. }
  994. return _hist_entry__sym_snprintf(map, sym, addr, level, bf, size,
  995. width);
  996. }
  997. struct sort_entry sort_mispredict = {
  998. .se_header = "Branch Mispredicted",
  999. .se_cmp = sort__mispredict_cmp,
  1000. .se_snprintf = hist_entry__mispredict_snprintf,
  1001. .se_width_idx = HISTC_MISPREDICT,
  1002. };
  1003. static u64 he_weight(struct hist_entry *he)
  1004. {
  1005. return he->stat.nr_events ? he->stat.weight / he->stat.nr_events : 0;
  1006. }
  1007. static int64_t
  1008. sort__local_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  1009. {
  1010. return he_weight(left) - he_weight(right);
  1011. }
  1012. static int hist_entry__local_weight_snprintf(struct hist_entry *he, char *bf,
  1013. size_t size, unsigned int width)
  1014. {
  1015. return repsep_snprintf(bf, size, "%-*llu", width, he_weight(he));
  1016. }
  1017. struct sort_entry sort_local_weight = {
  1018. .se_header = "Local Weight",
  1019. .se_cmp = sort__local_weight_cmp,
  1020. .se_snprintf = hist_entry__local_weight_snprintf,
  1021. .se_width_idx = HISTC_LOCAL_WEIGHT,
  1022. };
  1023. static int64_t
  1024. sort__global_weight_cmp(struct hist_entry *left, struct hist_entry *right)
  1025. {
  1026. return left->stat.weight - right->stat.weight;
  1027. }
  1028. static int hist_entry__global_weight_snprintf(struct hist_entry *he, char *bf,
  1029. size_t size, unsigned int width)
  1030. {
  1031. return repsep_snprintf(bf, size, "%-*llu", width, he->stat.weight);
  1032. }
  1033. struct sort_entry sort_global_weight = {
  1034. .se_header = "Weight",
  1035. .se_cmp = sort__global_weight_cmp,
  1036. .se_snprintf = hist_entry__global_weight_snprintf,
  1037. .se_width_idx = HISTC_GLOBAL_WEIGHT,
  1038. };
  1039. struct sort_entry sort_mem_daddr_sym = {
  1040. .se_header = "Data Symbol",
  1041. .se_cmp = sort__daddr_cmp,
  1042. .se_snprintf = hist_entry__daddr_snprintf,
  1043. .se_width_idx = HISTC_MEM_DADDR_SYMBOL,
  1044. };
  1045. struct sort_entry sort_mem_iaddr_sym = {
  1046. .se_header = "Code Symbol",
  1047. .se_cmp = sort__iaddr_cmp,
  1048. .se_snprintf = hist_entry__iaddr_snprintf,
  1049. .se_width_idx = HISTC_MEM_IADDR_SYMBOL,
  1050. };
  1051. struct sort_entry sort_mem_daddr_dso = {
  1052. .se_header = "Data Object",
  1053. .se_cmp = sort__dso_daddr_cmp,
  1054. .se_snprintf = hist_entry__dso_daddr_snprintf,
  1055. .se_width_idx = HISTC_MEM_DADDR_DSO,
  1056. };
  1057. struct sort_entry sort_mem_locked = {
  1058. .se_header = "Locked",
  1059. .se_cmp = sort__locked_cmp,
  1060. .se_snprintf = hist_entry__locked_snprintf,
  1061. .se_width_idx = HISTC_MEM_LOCKED,
  1062. };
  1063. struct sort_entry sort_mem_tlb = {
  1064. .se_header = "TLB access",
  1065. .se_cmp = sort__tlb_cmp,
  1066. .se_snprintf = hist_entry__tlb_snprintf,
  1067. .se_width_idx = HISTC_MEM_TLB,
  1068. };
  1069. struct sort_entry sort_mem_lvl = {
  1070. .se_header = "Memory access",
  1071. .se_cmp = sort__lvl_cmp,
  1072. .se_snprintf = hist_entry__lvl_snprintf,
  1073. .se_width_idx = HISTC_MEM_LVL,
  1074. };
  1075. struct sort_entry sort_mem_snoop = {
  1076. .se_header = "Snoop",
  1077. .se_cmp = sort__snoop_cmp,
  1078. .se_snprintf = hist_entry__snoop_snprintf,
  1079. .se_width_idx = HISTC_MEM_SNOOP,
  1080. };
  1081. struct sort_entry sort_mem_dcacheline = {
  1082. .se_header = "Data Cacheline",
  1083. .se_cmp = sort__dcacheline_cmp,
  1084. .se_snprintf = hist_entry__dcacheline_snprintf,
  1085. .se_width_idx = HISTC_MEM_DCACHELINE,
  1086. };
  1087. static int64_t
  1088. sort__phys_daddr_cmp(struct hist_entry *left, struct hist_entry *right)
  1089. {
  1090. uint64_t l = 0, r = 0;
  1091. if (left->mem_info)
  1092. l = left->mem_info->daddr.phys_addr;
  1093. if (right->mem_info)
  1094. r = right->mem_info->daddr.phys_addr;
  1095. return (int64_t)(r - l);
  1096. }
  1097. static int hist_entry__phys_daddr_snprintf(struct hist_entry *he, char *bf,
  1098. size_t size, unsigned int width)
  1099. {
  1100. uint64_t addr = 0;
  1101. size_t ret = 0;
  1102. size_t len = BITS_PER_LONG / 4;
  1103. addr = he->mem_info->daddr.phys_addr;
  1104. ret += repsep_snprintf(bf + ret, size - ret, "[%c] ", he->level);
  1105. ret += repsep_snprintf(bf + ret, size - ret, "%-#.*llx", len, addr);
  1106. ret += repsep_snprintf(bf + ret, size - ret, "%-*s", width - ret, "");
  1107. if (ret > width)
  1108. bf[width] = '\0';
  1109. return width;
  1110. }
  1111. struct sort_entry sort_mem_phys_daddr = {
  1112. .se_header = "Data Physical Address",
  1113. .se_cmp = sort__phys_daddr_cmp,
  1114. .se_snprintf = hist_entry__phys_daddr_snprintf,
  1115. .se_width_idx = HISTC_MEM_PHYS_DADDR,
  1116. };
  1117. static int64_t
  1118. sort__abort_cmp(struct hist_entry *left, struct hist_entry *right)
  1119. {
  1120. if (!left->branch_info || !right->branch_info)
  1121. return cmp_null(left->branch_info, right->branch_info);
  1122. return left->branch_info->flags.abort !=
  1123. right->branch_info->flags.abort;
  1124. }
  1125. static int hist_entry__abort_snprintf(struct hist_entry *he, char *bf,
  1126. size_t size, unsigned int width)
  1127. {
  1128. static const char *out = "N/A";
  1129. if (he->branch_info) {
  1130. if (he->branch_info->flags.abort)
  1131. out = "A";
  1132. else
  1133. out = ".";
  1134. }
  1135. return repsep_snprintf(bf, size, "%-*s", width, out);
  1136. }
  1137. struct sort_entry sort_abort = {
  1138. .se_header = "Transaction abort",
  1139. .se_cmp = sort__abort_cmp,
  1140. .se_snprintf = hist_entry__abort_snprintf,
  1141. .se_width_idx = HISTC_ABORT,
  1142. };
  1143. static int64_t
  1144. sort__in_tx_cmp(struct hist_entry *left, struct hist_entry *right)
  1145. {
  1146. if (!left->branch_info || !right->branch_info)
  1147. return cmp_null(left->branch_info, right->branch_info);
  1148. return left->branch_info->flags.in_tx !=
  1149. right->branch_info->flags.in_tx;
  1150. }
  1151. static int hist_entry__in_tx_snprintf(struct hist_entry *he, char *bf,
  1152. size_t size, unsigned int width)
  1153. {
  1154. static const char *out = "N/A";
  1155. if (he->branch_info) {
  1156. if (he->branch_info->flags.in_tx)
  1157. out = "T";
  1158. else
  1159. out = ".";
  1160. }
  1161. return repsep_snprintf(bf, size, "%-*s", width, out);
  1162. }
  1163. struct sort_entry sort_in_tx = {
  1164. .se_header = "Branch in transaction",
  1165. .se_cmp = sort__in_tx_cmp,
  1166. .se_snprintf = hist_entry__in_tx_snprintf,
  1167. .se_width_idx = HISTC_IN_TX,
  1168. };
  1169. static int64_t
  1170. sort__transaction_cmp(struct hist_entry *left, struct hist_entry *right)
  1171. {
  1172. return left->transaction - right->transaction;
  1173. }
  1174. static inline char *add_str(char *p, const char *str)
  1175. {
  1176. strcpy(p, str);
  1177. return p + strlen(str);
  1178. }
  1179. static struct txbit {
  1180. unsigned flag;
  1181. const char *name;
  1182. int skip_for_len;
  1183. } txbits[] = {
  1184. { PERF_TXN_ELISION, "EL ", 0 },
  1185. { PERF_TXN_TRANSACTION, "TX ", 1 },
  1186. { PERF_TXN_SYNC, "SYNC ", 1 },
  1187. { PERF_TXN_ASYNC, "ASYNC ", 0 },
  1188. { PERF_TXN_RETRY, "RETRY ", 0 },
  1189. { PERF_TXN_CONFLICT, "CON ", 0 },
  1190. { PERF_TXN_CAPACITY_WRITE, "CAP-WRITE ", 1 },
  1191. { PERF_TXN_CAPACITY_READ, "CAP-READ ", 0 },
  1192. { 0, NULL, 0 }
  1193. };
  1194. int hist_entry__transaction_len(void)
  1195. {
  1196. int i;
  1197. int len = 0;
  1198. for (i = 0; txbits[i].name; i++) {
  1199. if (!txbits[i].skip_for_len)
  1200. len += strlen(txbits[i].name);
  1201. }
  1202. len += 4; /* :XX<space> */
  1203. return len;
  1204. }
  1205. static int hist_entry__transaction_snprintf(struct hist_entry *he, char *bf,
  1206. size_t size, unsigned int width)
  1207. {
  1208. u64 t = he->transaction;
  1209. char buf[128];
  1210. char *p = buf;
  1211. int i;
  1212. buf[0] = 0;
  1213. for (i = 0; txbits[i].name; i++)
  1214. if (txbits[i].flag & t)
  1215. p = add_str(p, txbits[i].name);
  1216. if (t && !(t & (PERF_TXN_SYNC|PERF_TXN_ASYNC)))
  1217. p = add_str(p, "NEITHER ");
  1218. if (t & PERF_TXN_ABORT_MASK) {
  1219. sprintf(p, ":%" PRIx64,
  1220. (t & PERF_TXN_ABORT_MASK) >>
  1221. PERF_TXN_ABORT_SHIFT);
  1222. p += strlen(p);
  1223. }
  1224. return repsep_snprintf(bf, size, "%-*s", width, buf);
  1225. }
  1226. struct sort_entry sort_transaction = {
  1227. .se_header = "Transaction ",
  1228. .se_cmp = sort__transaction_cmp,
  1229. .se_snprintf = hist_entry__transaction_snprintf,
  1230. .se_width_idx = HISTC_TRANSACTION,
  1231. };
  1232. /* --sort symbol_size */
  1233. static int64_t _sort__sym_size_cmp(struct symbol *sym_l, struct symbol *sym_r)
  1234. {
  1235. int64_t size_l = sym_l != NULL ? symbol__size(sym_l) : 0;
  1236. int64_t size_r = sym_r != NULL ? symbol__size(sym_r) : 0;
  1237. return size_l < size_r ? -1 :
  1238. size_l == size_r ? 0 : 1;
  1239. }
  1240. static int64_t
  1241. sort__sym_size_cmp(struct hist_entry *left, struct hist_entry *right)
  1242. {
  1243. return _sort__sym_size_cmp(right->ms.sym, left->ms.sym);
  1244. }
  1245. static int _hist_entry__sym_size_snprintf(struct symbol *sym, char *bf,
  1246. size_t bf_size, unsigned int width)
  1247. {
  1248. if (sym)
  1249. return repsep_snprintf(bf, bf_size, "%*d", width, symbol__size(sym));
  1250. return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
  1251. }
  1252. static int hist_entry__sym_size_snprintf(struct hist_entry *he, char *bf,
  1253. size_t size, unsigned int width)
  1254. {
  1255. return _hist_entry__sym_size_snprintf(he->ms.sym, bf, size, width);
  1256. }
  1257. struct sort_entry sort_sym_size = {
  1258. .se_header = "Symbol size",
  1259. .se_cmp = sort__sym_size_cmp,
  1260. .se_snprintf = hist_entry__sym_size_snprintf,
  1261. .se_width_idx = HISTC_SYM_SIZE,
  1262. };
  1263. /* --sort dso_size */
  1264. static int64_t _sort__dso_size_cmp(struct map *map_l, struct map *map_r)
  1265. {
  1266. int64_t size_l = map_l != NULL ? map__size(map_l) : 0;
  1267. int64_t size_r = map_r != NULL ? map__size(map_r) : 0;
  1268. return size_l < size_r ? -1 :
  1269. size_l == size_r ? 0 : 1;
  1270. }
  1271. static int64_t
  1272. sort__dso_size_cmp(struct hist_entry *left, struct hist_entry *right)
  1273. {
  1274. return _sort__dso_size_cmp(right->ms.map, left->ms.map);
  1275. }
  1276. static int _hist_entry__dso_size_snprintf(struct map *map, char *bf,
  1277. size_t bf_size, unsigned int width)
  1278. {
  1279. if (map && map->dso)
  1280. return repsep_snprintf(bf, bf_size, "%*d", width,
  1281. map__size(map));
  1282. return repsep_snprintf(bf, bf_size, "%*s", width, "unknown");
  1283. }
  1284. static int hist_entry__dso_size_snprintf(struct hist_entry *he, char *bf,
  1285. size_t size, unsigned int width)
  1286. {
  1287. return _hist_entry__dso_size_snprintf(he->ms.map, bf, size, width);
  1288. }
  1289. struct sort_entry sort_dso_size = {
  1290. .se_header = "DSO size",
  1291. .se_cmp = sort__dso_size_cmp,
  1292. .se_snprintf = hist_entry__dso_size_snprintf,
  1293. .se_width_idx = HISTC_DSO_SIZE,
  1294. };
  1295. struct sort_dimension {
  1296. const char *name;
  1297. struct sort_entry *entry;
  1298. int taken;
  1299. };
  1300. #define DIM(d, n, func) [d] = { .name = n, .entry = &(func) }
  1301. static struct sort_dimension common_sort_dimensions[] = {
  1302. DIM(SORT_PID, "pid", sort_thread),
  1303. DIM(SORT_COMM, "comm", sort_comm),
  1304. DIM(SORT_DSO, "dso", sort_dso),
  1305. DIM(SORT_SYM, "symbol", sort_sym),
  1306. DIM(SORT_PARENT, "parent", sort_parent),
  1307. DIM(SORT_CPU, "cpu", sort_cpu),
  1308. DIM(SORT_SOCKET, "socket", sort_socket),
  1309. DIM(SORT_SRCLINE, "srcline", sort_srcline),
  1310. DIM(SORT_SRCFILE, "srcfile", sort_srcfile),
  1311. DIM(SORT_LOCAL_WEIGHT, "local_weight", sort_local_weight),
  1312. DIM(SORT_GLOBAL_WEIGHT, "weight", sort_global_weight),
  1313. DIM(SORT_TRANSACTION, "transaction", sort_transaction),
  1314. DIM(SORT_TRACE, "trace", sort_trace),
  1315. DIM(SORT_SYM_SIZE, "symbol_size", sort_sym_size),
  1316. DIM(SORT_DSO_SIZE, "dso_size", sort_dso_size),
  1317. DIM(SORT_CGROUP_ID, "cgroup_id", sort_cgroup_id),
  1318. };
  1319. #undef DIM
  1320. #define DIM(d, n, func) [d - __SORT_BRANCH_STACK] = { .name = n, .entry = &(func) }
  1321. static struct sort_dimension bstack_sort_dimensions[] = {
  1322. DIM(SORT_DSO_FROM, "dso_from", sort_dso_from),
  1323. DIM(SORT_DSO_TO, "dso_to", sort_dso_to),
  1324. DIM(SORT_SYM_FROM, "symbol_from", sort_sym_from),
  1325. DIM(SORT_SYM_TO, "symbol_to", sort_sym_to),
  1326. DIM(SORT_MISPREDICT, "mispredict", sort_mispredict),
  1327. DIM(SORT_IN_TX, "in_tx", sort_in_tx),
  1328. DIM(SORT_ABORT, "abort", sort_abort),
  1329. DIM(SORT_CYCLES, "cycles", sort_cycles),
  1330. DIM(SORT_SRCLINE_FROM, "srcline_from", sort_srcline_from),
  1331. DIM(SORT_SRCLINE_TO, "srcline_to", sort_srcline_to),
  1332. };
  1333. #undef DIM
  1334. #define DIM(d, n, func) [d - __SORT_MEMORY_MODE] = { .name = n, .entry = &(func) }
  1335. static struct sort_dimension memory_sort_dimensions[] = {
  1336. DIM(SORT_MEM_DADDR_SYMBOL, "symbol_daddr", sort_mem_daddr_sym),
  1337. DIM(SORT_MEM_IADDR_SYMBOL, "symbol_iaddr", sort_mem_iaddr_sym),
  1338. DIM(SORT_MEM_DADDR_DSO, "dso_daddr", sort_mem_daddr_dso),
  1339. DIM(SORT_MEM_LOCKED, "locked", sort_mem_locked),
  1340. DIM(SORT_MEM_TLB, "tlb", sort_mem_tlb),
  1341. DIM(SORT_MEM_LVL, "mem", sort_mem_lvl),
  1342. DIM(SORT_MEM_SNOOP, "snoop", sort_mem_snoop),
  1343. DIM(SORT_MEM_DCACHELINE, "dcacheline", sort_mem_dcacheline),
  1344. DIM(SORT_MEM_PHYS_DADDR, "phys_daddr", sort_mem_phys_daddr),
  1345. };
  1346. #undef DIM
  1347. struct hpp_dimension {
  1348. const char *name;
  1349. struct perf_hpp_fmt *fmt;
  1350. int taken;
  1351. };
  1352. #define DIM(d, n) { .name = n, .fmt = &perf_hpp__format[d], }
  1353. static struct hpp_dimension hpp_sort_dimensions[] = {
  1354. DIM(PERF_HPP__OVERHEAD, "overhead"),
  1355. DIM(PERF_HPP__OVERHEAD_SYS, "overhead_sys"),
  1356. DIM(PERF_HPP__OVERHEAD_US, "overhead_us"),
  1357. DIM(PERF_HPP__OVERHEAD_GUEST_SYS, "overhead_guest_sys"),
  1358. DIM(PERF_HPP__OVERHEAD_GUEST_US, "overhead_guest_us"),
  1359. DIM(PERF_HPP__OVERHEAD_ACC, "overhead_children"),
  1360. DIM(PERF_HPP__SAMPLES, "sample"),
  1361. DIM(PERF_HPP__PERIOD, "period"),
  1362. };
  1363. #undef DIM
  1364. struct hpp_sort_entry {
  1365. struct perf_hpp_fmt hpp;
  1366. struct sort_entry *se;
  1367. };
  1368. void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists)
  1369. {
  1370. struct hpp_sort_entry *hse;
  1371. if (!perf_hpp__is_sort_entry(fmt))
  1372. return;
  1373. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1374. hists__new_col_len(hists, hse->se->se_width_idx, strlen(fmt->name));
  1375. }
  1376. static int __sort__hpp_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1377. struct hists *hists, int line __maybe_unused,
  1378. int *span __maybe_unused)
  1379. {
  1380. struct hpp_sort_entry *hse;
  1381. size_t len = fmt->user_len;
  1382. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1383. if (!len)
  1384. len = hists__col_len(hists, hse->se->se_width_idx);
  1385. return scnprintf(hpp->buf, hpp->size, "%-*.*s", len, len, fmt->name);
  1386. }
  1387. static int __sort__hpp_width(struct perf_hpp_fmt *fmt,
  1388. struct perf_hpp *hpp __maybe_unused,
  1389. struct hists *hists)
  1390. {
  1391. struct hpp_sort_entry *hse;
  1392. size_t len = fmt->user_len;
  1393. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1394. if (!len)
  1395. len = hists__col_len(hists, hse->se->se_width_idx);
  1396. return len;
  1397. }
  1398. static int __sort__hpp_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1399. struct hist_entry *he)
  1400. {
  1401. struct hpp_sort_entry *hse;
  1402. size_t len = fmt->user_len;
  1403. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1404. if (!len)
  1405. len = hists__col_len(he->hists, hse->se->se_width_idx);
  1406. return hse->se->se_snprintf(he, hpp->buf, hpp->size, len);
  1407. }
  1408. static int64_t __sort__hpp_cmp(struct perf_hpp_fmt *fmt,
  1409. struct hist_entry *a, struct hist_entry *b)
  1410. {
  1411. struct hpp_sort_entry *hse;
  1412. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1413. return hse->se->se_cmp(a, b);
  1414. }
  1415. static int64_t __sort__hpp_collapse(struct perf_hpp_fmt *fmt,
  1416. struct hist_entry *a, struct hist_entry *b)
  1417. {
  1418. struct hpp_sort_entry *hse;
  1419. int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
  1420. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1421. collapse_fn = hse->se->se_collapse ?: hse->se->se_cmp;
  1422. return collapse_fn(a, b);
  1423. }
  1424. static int64_t __sort__hpp_sort(struct perf_hpp_fmt *fmt,
  1425. struct hist_entry *a, struct hist_entry *b)
  1426. {
  1427. struct hpp_sort_entry *hse;
  1428. int64_t (*sort_fn)(struct hist_entry *, struct hist_entry *);
  1429. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1430. sort_fn = hse->se->se_sort ?: hse->se->se_cmp;
  1431. return sort_fn(a, b);
  1432. }
  1433. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format)
  1434. {
  1435. return format->header == __sort__hpp_header;
  1436. }
  1437. #define MK_SORT_ENTRY_CHK(key) \
  1438. bool perf_hpp__is_ ## key ## _entry(struct perf_hpp_fmt *fmt) \
  1439. { \
  1440. struct hpp_sort_entry *hse; \
  1441. \
  1442. if (!perf_hpp__is_sort_entry(fmt)) \
  1443. return false; \
  1444. \
  1445. hse = container_of(fmt, struct hpp_sort_entry, hpp); \
  1446. return hse->se == &sort_ ## key ; \
  1447. }
  1448. MK_SORT_ENTRY_CHK(trace)
  1449. MK_SORT_ENTRY_CHK(srcline)
  1450. MK_SORT_ENTRY_CHK(srcfile)
  1451. MK_SORT_ENTRY_CHK(thread)
  1452. MK_SORT_ENTRY_CHK(comm)
  1453. MK_SORT_ENTRY_CHK(dso)
  1454. MK_SORT_ENTRY_CHK(sym)
  1455. static bool __sort__hpp_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  1456. {
  1457. struct hpp_sort_entry *hse_a;
  1458. struct hpp_sort_entry *hse_b;
  1459. if (!perf_hpp__is_sort_entry(a) || !perf_hpp__is_sort_entry(b))
  1460. return false;
  1461. hse_a = container_of(a, struct hpp_sort_entry, hpp);
  1462. hse_b = container_of(b, struct hpp_sort_entry, hpp);
  1463. return hse_a->se == hse_b->se;
  1464. }
  1465. static void hse_free(struct perf_hpp_fmt *fmt)
  1466. {
  1467. struct hpp_sort_entry *hse;
  1468. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1469. free(hse);
  1470. }
  1471. static struct hpp_sort_entry *
  1472. __sort_dimension__alloc_hpp(struct sort_dimension *sd, int level)
  1473. {
  1474. struct hpp_sort_entry *hse;
  1475. hse = malloc(sizeof(*hse));
  1476. if (hse == NULL) {
  1477. pr_err("Memory allocation failed\n");
  1478. return NULL;
  1479. }
  1480. hse->se = sd->entry;
  1481. hse->hpp.name = sd->entry->se_header;
  1482. hse->hpp.header = __sort__hpp_header;
  1483. hse->hpp.width = __sort__hpp_width;
  1484. hse->hpp.entry = __sort__hpp_entry;
  1485. hse->hpp.color = NULL;
  1486. hse->hpp.cmp = __sort__hpp_cmp;
  1487. hse->hpp.collapse = __sort__hpp_collapse;
  1488. hse->hpp.sort = __sort__hpp_sort;
  1489. hse->hpp.equal = __sort__hpp_equal;
  1490. hse->hpp.free = hse_free;
  1491. INIT_LIST_HEAD(&hse->hpp.list);
  1492. INIT_LIST_HEAD(&hse->hpp.sort_list);
  1493. hse->hpp.elide = false;
  1494. hse->hpp.len = 0;
  1495. hse->hpp.user_len = 0;
  1496. hse->hpp.level = level;
  1497. return hse;
  1498. }
  1499. static void hpp_free(struct perf_hpp_fmt *fmt)
  1500. {
  1501. free(fmt);
  1502. }
  1503. static struct perf_hpp_fmt *__hpp_dimension__alloc_hpp(struct hpp_dimension *hd,
  1504. int level)
  1505. {
  1506. struct perf_hpp_fmt *fmt;
  1507. fmt = memdup(hd->fmt, sizeof(*fmt));
  1508. if (fmt) {
  1509. INIT_LIST_HEAD(&fmt->list);
  1510. INIT_LIST_HEAD(&fmt->sort_list);
  1511. fmt->free = hpp_free;
  1512. fmt->level = level;
  1513. }
  1514. return fmt;
  1515. }
  1516. int hist_entry__filter(struct hist_entry *he, int type, const void *arg)
  1517. {
  1518. struct perf_hpp_fmt *fmt;
  1519. struct hpp_sort_entry *hse;
  1520. int ret = -1;
  1521. int r;
  1522. perf_hpp_list__for_each_format(he->hpp_list, fmt) {
  1523. if (!perf_hpp__is_sort_entry(fmt))
  1524. continue;
  1525. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1526. if (hse->se->se_filter == NULL)
  1527. continue;
  1528. /*
  1529. * hist entry is filtered if any of sort key in the hpp list
  1530. * is applied. But it should skip non-matched filter types.
  1531. */
  1532. r = hse->se->se_filter(he, type, arg);
  1533. if (r >= 0) {
  1534. if (ret < 0)
  1535. ret = 0;
  1536. ret |= r;
  1537. }
  1538. }
  1539. return ret;
  1540. }
  1541. static int __sort_dimension__add_hpp_sort(struct sort_dimension *sd,
  1542. struct perf_hpp_list *list,
  1543. int level)
  1544. {
  1545. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, level);
  1546. if (hse == NULL)
  1547. return -1;
  1548. perf_hpp_list__register_sort_field(list, &hse->hpp);
  1549. return 0;
  1550. }
  1551. static int __sort_dimension__add_hpp_output(struct sort_dimension *sd,
  1552. struct perf_hpp_list *list)
  1553. {
  1554. struct hpp_sort_entry *hse = __sort_dimension__alloc_hpp(sd, 0);
  1555. if (hse == NULL)
  1556. return -1;
  1557. perf_hpp_list__column_register(list, &hse->hpp);
  1558. return 0;
  1559. }
  1560. struct hpp_dynamic_entry {
  1561. struct perf_hpp_fmt hpp;
  1562. struct perf_evsel *evsel;
  1563. struct format_field *field;
  1564. unsigned dynamic_len;
  1565. bool raw_trace;
  1566. };
  1567. static int hde_width(struct hpp_dynamic_entry *hde)
  1568. {
  1569. if (!hde->hpp.len) {
  1570. int len = hde->dynamic_len;
  1571. int namelen = strlen(hde->field->name);
  1572. int fieldlen = hde->field->size;
  1573. if (namelen > len)
  1574. len = namelen;
  1575. if (!(hde->field->flags & FIELD_IS_STRING)) {
  1576. /* length for print hex numbers */
  1577. fieldlen = hde->field->size * 2 + 2;
  1578. }
  1579. if (fieldlen > len)
  1580. len = fieldlen;
  1581. hde->hpp.len = len;
  1582. }
  1583. return hde->hpp.len;
  1584. }
  1585. static void update_dynamic_len(struct hpp_dynamic_entry *hde,
  1586. struct hist_entry *he)
  1587. {
  1588. char *str, *pos;
  1589. struct format_field *field = hde->field;
  1590. size_t namelen;
  1591. bool last = false;
  1592. if (hde->raw_trace)
  1593. return;
  1594. /* parse pretty print result and update max length */
  1595. if (!he->trace_output)
  1596. he->trace_output = get_trace_output(he);
  1597. namelen = strlen(field->name);
  1598. str = he->trace_output;
  1599. while (str) {
  1600. pos = strchr(str, ' ');
  1601. if (pos == NULL) {
  1602. last = true;
  1603. pos = str + strlen(str);
  1604. }
  1605. if (!strncmp(str, field->name, namelen)) {
  1606. size_t len;
  1607. str += namelen + 1;
  1608. len = pos - str;
  1609. if (len > hde->dynamic_len)
  1610. hde->dynamic_len = len;
  1611. break;
  1612. }
  1613. if (last)
  1614. str = NULL;
  1615. else
  1616. str = pos + 1;
  1617. }
  1618. }
  1619. static int __sort__hde_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1620. struct hists *hists __maybe_unused,
  1621. int line __maybe_unused,
  1622. int *span __maybe_unused)
  1623. {
  1624. struct hpp_dynamic_entry *hde;
  1625. size_t len = fmt->user_len;
  1626. hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
  1627. if (!len)
  1628. len = hde_width(hde);
  1629. return scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, hde->field->name);
  1630. }
  1631. static int __sort__hde_width(struct perf_hpp_fmt *fmt,
  1632. struct perf_hpp *hpp __maybe_unused,
  1633. struct hists *hists __maybe_unused)
  1634. {
  1635. struct hpp_dynamic_entry *hde;
  1636. size_t len = fmt->user_len;
  1637. hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
  1638. if (!len)
  1639. len = hde_width(hde);
  1640. return len;
  1641. }
  1642. bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists)
  1643. {
  1644. struct hpp_dynamic_entry *hde;
  1645. hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
  1646. return hists_to_evsel(hists) == hde->evsel;
  1647. }
  1648. static int __sort__hde_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1649. struct hist_entry *he)
  1650. {
  1651. struct hpp_dynamic_entry *hde;
  1652. size_t len = fmt->user_len;
  1653. char *str, *pos;
  1654. struct format_field *field;
  1655. size_t namelen;
  1656. bool last = false;
  1657. int ret;
  1658. hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
  1659. if (!len)
  1660. len = hde_width(hde);
  1661. if (hde->raw_trace)
  1662. goto raw_field;
  1663. if (!he->trace_output)
  1664. he->trace_output = get_trace_output(he);
  1665. field = hde->field;
  1666. namelen = strlen(field->name);
  1667. str = he->trace_output;
  1668. while (str) {
  1669. pos = strchr(str, ' ');
  1670. if (pos == NULL) {
  1671. last = true;
  1672. pos = str + strlen(str);
  1673. }
  1674. if (!strncmp(str, field->name, namelen)) {
  1675. str += namelen + 1;
  1676. str = strndup(str, pos - str);
  1677. if (str == NULL)
  1678. return scnprintf(hpp->buf, hpp->size,
  1679. "%*.*s", len, len, "ERROR");
  1680. break;
  1681. }
  1682. if (last)
  1683. str = NULL;
  1684. else
  1685. str = pos + 1;
  1686. }
  1687. if (str == NULL) {
  1688. struct trace_seq seq;
  1689. raw_field:
  1690. trace_seq_init(&seq);
  1691. pevent_print_field(&seq, he->raw_data, hde->field);
  1692. str = seq.buffer;
  1693. }
  1694. ret = scnprintf(hpp->buf, hpp->size, "%*.*s", len, len, str);
  1695. free(str);
  1696. return ret;
  1697. }
  1698. static int64_t __sort__hde_cmp(struct perf_hpp_fmt *fmt,
  1699. struct hist_entry *a, struct hist_entry *b)
  1700. {
  1701. struct hpp_dynamic_entry *hde;
  1702. struct format_field *field;
  1703. unsigned offset, size;
  1704. hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
  1705. if (b == NULL) {
  1706. update_dynamic_len(hde, a);
  1707. return 0;
  1708. }
  1709. field = hde->field;
  1710. if (field->flags & FIELD_IS_DYNAMIC) {
  1711. unsigned long long dyn;
  1712. pevent_read_number_field(field, a->raw_data, &dyn);
  1713. offset = dyn & 0xffff;
  1714. size = (dyn >> 16) & 0xffff;
  1715. /* record max width for output */
  1716. if (size > hde->dynamic_len)
  1717. hde->dynamic_len = size;
  1718. } else {
  1719. offset = field->offset;
  1720. size = field->size;
  1721. }
  1722. return memcmp(a->raw_data + offset, b->raw_data + offset, size);
  1723. }
  1724. bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *fmt)
  1725. {
  1726. return fmt->cmp == __sort__hde_cmp;
  1727. }
  1728. static bool __sort__hde_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  1729. {
  1730. struct hpp_dynamic_entry *hde_a;
  1731. struct hpp_dynamic_entry *hde_b;
  1732. if (!perf_hpp__is_dynamic_entry(a) || !perf_hpp__is_dynamic_entry(b))
  1733. return false;
  1734. hde_a = container_of(a, struct hpp_dynamic_entry, hpp);
  1735. hde_b = container_of(b, struct hpp_dynamic_entry, hpp);
  1736. return hde_a->field == hde_b->field;
  1737. }
  1738. static void hde_free(struct perf_hpp_fmt *fmt)
  1739. {
  1740. struct hpp_dynamic_entry *hde;
  1741. hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
  1742. free(hde);
  1743. }
  1744. static struct hpp_dynamic_entry *
  1745. __alloc_dynamic_entry(struct perf_evsel *evsel, struct format_field *field,
  1746. int level)
  1747. {
  1748. struct hpp_dynamic_entry *hde;
  1749. hde = malloc(sizeof(*hde));
  1750. if (hde == NULL) {
  1751. pr_debug("Memory allocation failed\n");
  1752. return NULL;
  1753. }
  1754. hde->evsel = evsel;
  1755. hde->field = field;
  1756. hde->dynamic_len = 0;
  1757. hde->hpp.name = field->name;
  1758. hde->hpp.header = __sort__hde_header;
  1759. hde->hpp.width = __sort__hde_width;
  1760. hde->hpp.entry = __sort__hde_entry;
  1761. hde->hpp.color = NULL;
  1762. hde->hpp.cmp = __sort__hde_cmp;
  1763. hde->hpp.collapse = __sort__hde_cmp;
  1764. hde->hpp.sort = __sort__hde_cmp;
  1765. hde->hpp.equal = __sort__hde_equal;
  1766. hde->hpp.free = hde_free;
  1767. INIT_LIST_HEAD(&hde->hpp.list);
  1768. INIT_LIST_HEAD(&hde->hpp.sort_list);
  1769. hde->hpp.elide = false;
  1770. hde->hpp.len = 0;
  1771. hde->hpp.user_len = 0;
  1772. hde->hpp.level = level;
  1773. return hde;
  1774. }
  1775. struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt)
  1776. {
  1777. struct perf_hpp_fmt *new_fmt = NULL;
  1778. if (perf_hpp__is_sort_entry(fmt)) {
  1779. struct hpp_sort_entry *hse, *new_hse;
  1780. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  1781. new_hse = memdup(hse, sizeof(*hse));
  1782. if (new_hse)
  1783. new_fmt = &new_hse->hpp;
  1784. } else if (perf_hpp__is_dynamic_entry(fmt)) {
  1785. struct hpp_dynamic_entry *hde, *new_hde;
  1786. hde = container_of(fmt, struct hpp_dynamic_entry, hpp);
  1787. new_hde = memdup(hde, sizeof(*hde));
  1788. if (new_hde)
  1789. new_fmt = &new_hde->hpp;
  1790. } else {
  1791. new_fmt = memdup(fmt, sizeof(*fmt));
  1792. }
  1793. INIT_LIST_HEAD(&new_fmt->list);
  1794. INIT_LIST_HEAD(&new_fmt->sort_list);
  1795. return new_fmt;
  1796. }
  1797. static int parse_field_name(char *str, char **event, char **field, char **opt)
  1798. {
  1799. char *event_name, *field_name, *opt_name;
  1800. event_name = str;
  1801. field_name = strchr(str, '.');
  1802. if (field_name) {
  1803. *field_name++ = '\0';
  1804. } else {
  1805. event_name = NULL;
  1806. field_name = str;
  1807. }
  1808. opt_name = strchr(field_name, '/');
  1809. if (opt_name)
  1810. *opt_name++ = '\0';
  1811. *event = event_name;
  1812. *field = field_name;
  1813. *opt = opt_name;
  1814. return 0;
  1815. }
  1816. /* find match evsel using a given event name. The event name can be:
  1817. * 1. '%' + event index (e.g. '%1' for first event)
  1818. * 2. full event name (e.g. sched:sched_switch)
  1819. * 3. partial event name (should not contain ':')
  1820. */
  1821. static struct perf_evsel *find_evsel(struct perf_evlist *evlist, char *event_name)
  1822. {
  1823. struct perf_evsel *evsel = NULL;
  1824. struct perf_evsel *pos;
  1825. bool full_name;
  1826. /* case 1 */
  1827. if (event_name[0] == '%') {
  1828. int nr = strtol(event_name+1, NULL, 0);
  1829. if (nr > evlist->nr_entries)
  1830. return NULL;
  1831. evsel = perf_evlist__first(evlist);
  1832. while (--nr > 0)
  1833. evsel = perf_evsel__next(evsel);
  1834. return evsel;
  1835. }
  1836. full_name = !!strchr(event_name, ':');
  1837. evlist__for_each_entry(evlist, pos) {
  1838. /* case 2 */
  1839. if (full_name && !strcmp(pos->name, event_name))
  1840. return pos;
  1841. /* case 3 */
  1842. if (!full_name && strstr(pos->name, event_name)) {
  1843. if (evsel) {
  1844. pr_debug("'%s' event is ambiguous: it can be %s or %s\n",
  1845. event_name, evsel->name, pos->name);
  1846. return NULL;
  1847. }
  1848. evsel = pos;
  1849. }
  1850. }
  1851. return evsel;
  1852. }
  1853. static int __dynamic_dimension__add(struct perf_evsel *evsel,
  1854. struct format_field *field,
  1855. bool raw_trace, int level)
  1856. {
  1857. struct hpp_dynamic_entry *hde;
  1858. hde = __alloc_dynamic_entry(evsel, field, level);
  1859. if (hde == NULL)
  1860. return -ENOMEM;
  1861. hde->raw_trace = raw_trace;
  1862. perf_hpp__register_sort_field(&hde->hpp);
  1863. return 0;
  1864. }
  1865. static int add_evsel_fields(struct perf_evsel *evsel, bool raw_trace, int level)
  1866. {
  1867. int ret;
  1868. struct format_field *field;
  1869. field = evsel->tp_format->format.fields;
  1870. while (field) {
  1871. ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
  1872. if (ret < 0)
  1873. return ret;
  1874. field = field->next;
  1875. }
  1876. return 0;
  1877. }
  1878. static int add_all_dynamic_fields(struct perf_evlist *evlist, bool raw_trace,
  1879. int level)
  1880. {
  1881. int ret;
  1882. struct perf_evsel *evsel;
  1883. evlist__for_each_entry(evlist, evsel) {
  1884. if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
  1885. continue;
  1886. ret = add_evsel_fields(evsel, raw_trace, level);
  1887. if (ret < 0)
  1888. return ret;
  1889. }
  1890. return 0;
  1891. }
  1892. static int add_all_matching_fields(struct perf_evlist *evlist,
  1893. char *field_name, bool raw_trace, int level)
  1894. {
  1895. int ret = -ESRCH;
  1896. struct perf_evsel *evsel;
  1897. struct format_field *field;
  1898. evlist__for_each_entry(evlist, evsel) {
  1899. if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
  1900. continue;
  1901. field = pevent_find_any_field(evsel->tp_format, field_name);
  1902. if (field == NULL)
  1903. continue;
  1904. ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
  1905. if (ret < 0)
  1906. break;
  1907. }
  1908. return ret;
  1909. }
  1910. static int add_dynamic_entry(struct perf_evlist *evlist, const char *tok,
  1911. int level)
  1912. {
  1913. char *str, *event_name, *field_name, *opt_name;
  1914. struct perf_evsel *evsel;
  1915. struct format_field *field;
  1916. bool raw_trace = symbol_conf.raw_trace;
  1917. int ret = 0;
  1918. if (evlist == NULL)
  1919. return -ENOENT;
  1920. str = strdup(tok);
  1921. if (str == NULL)
  1922. return -ENOMEM;
  1923. if (parse_field_name(str, &event_name, &field_name, &opt_name) < 0) {
  1924. ret = -EINVAL;
  1925. goto out;
  1926. }
  1927. if (opt_name) {
  1928. if (strcmp(opt_name, "raw")) {
  1929. pr_debug("unsupported field option %s\n", opt_name);
  1930. ret = -EINVAL;
  1931. goto out;
  1932. }
  1933. raw_trace = true;
  1934. }
  1935. if (!strcmp(field_name, "trace_fields")) {
  1936. ret = add_all_dynamic_fields(evlist, raw_trace, level);
  1937. goto out;
  1938. }
  1939. if (event_name == NULL) {
  1940. ret = add_all_matching_fields(evlist, field_name, raw_trace, level);
  1941. goto out;
  1942. }
  1943. evsel = find_evsel(evlist, event_name);
  1944. if (evsel == NULL) {
  1945. pr_debug("Cannot find event: %s\n", event_name);
  1946. ret = -ENOENT;
  1947. goto out;
  1948. }
  1949. if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  1950. pr_debug("%s is not a tracepoint event\n", event_name);
  1951. ret = -EINVAL;
  1952. goto out;
  1953. }
  1954. if (!strcmp(field_name, "*")) {
  1955. ret = add_evsel_fields(evsel, raw_trace, level);
  1956. } else {
  1957. field = pevent_find_any_field(evsel->tp_format, field_name);
  1958. if (field == NULL) {
  1959. pr_debug("Cannot find event field for %s.%s\n",
  1960. event_name, field_name);
  1961. return -ENOENT;
  1962. }
  1963. ret = __dynamic_dimension__add(evsel, field, raw_trace, level);
  1964. }
  1965. out:
  1966. free(str);
  1967. return ret;
  1968. }
  1969. static int __sort_dimension__add(struct sort_dimension *sd,
  1970. struct perf_hpp_list *list,
  1971. int level)
  1972. {
  1973. if (sd->taken)
  1974. return 0;
  1975. if (__sort_dimension__add_hpp_sort(sd, list, level) < 0)
  1976. return -1;
  1977. if (sd->entry->se_collapse)
  1978. list->need_collapse = 1;
  1979. sd->taken = 1;
  1980. return 0;
  1981. }
  1982. static int __hpp_dimension__add(struct hpp_dimension *hd,
  1983. struct perf_hpp_list *list,
  1984. int level)
  1985. {
  1986. struct perf_hpp_fmt *fmt;
  1987. if (hd->taken)
  1988. return 0;
  1989. fmt = __hpp_dimension__alloc_hpp(hd, level);
  1990. if (!fmt)
  1991. return -1;
  1992. hd->taken = 1;
  1993. perf_hpp_list__register_sort_field(list, fmt);
  1994. return 0;
  1995. }
  1996. static int __sort_dimension__add_output(struct perf_hpp_list *list,
  1997. struct sort_dimension *sd)
  1998. {
  1999. if (sd->taken)
  2000. return 0;
  2001. if (__sort_dimension__add_hpp_output(sd, list) < 0)
  2002. return -1;
  2003. sd->taken = 1;
  2004. return 0;
  2005. }
  2006. static int __hpp_dimension__add_output(struct perf_hpp_list *list,
  2007. struct hpp_dimension *hd)
  2008. {
  2009. struct perf_hpp_fmt *fmt;
  2010. if (hd->taken)
  2011. return 0;
  2012. fmt = __hpp_dimension__alloc_hpp(hd, 0);
  2013. if (!fmt)
  2014. return -1;
  2015. hd->taken = 1;
  2016. perf_hpp_list__column_register(list, fmt);
  2017. return 0;
  2018. }
  2019. int hpp_dimension__add_output(unsigned col)
  2020. {
  2021. BUG_ON(col >= PERF_HPP__MAX_INDEX);
  2022. return __hpp_dimension__add_output(&perf_hpp_list, &hpp_sort_dimensions[col]);
  2023. }
  2024. int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
  2025. struct perf_evlist *evlist,
  2026. int level)
  2027. {
  2028. unsigned int i;
  2029. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  2030. struct sort_dimension *sd = &common_sort_dimensions[i];
  2031. if (strncasecmp(tok, sd->name, strlen(tok)))
  2032. continue;
  2033. if (sd->entry == &sort_parent) {
  2034. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  2035. if (ret) {
  2036. char err[BUFSIZ];
  2037. regerror(ret, &parent_regex, err, sizeof(err));
  2038. pr_err("Invalid regex: %s\n%s", parent_pattern, err);
  2039. return -EINVAL;
  2040. }
  2041. list->parent = 1;
  2042. } else if (sd->entry == &sort_sym) {
  2043. list->sym = 1;
  2044. /*
  2045. * perf diff displays the performance difference amongst
  2046. * two or more perf.data files. Those files could come
  2047. * from different binaries. So we should not compare
  2048. * their ips, but the name of symbol.
  2049. */
  2050. if (sort__mode == SORT_MODE__DIFF)
  2051. sd->entry->se_collapse = sort__sym_sort;
  2052. } else if (sd->entry == &sort_dso) {
  2053. list->dso = 1;
  2054. } else if (sd->entry == &sort_socket) {
  2055. list->socket = 1;
  2056. } else if (sd->entry == &sort_thread) {
  2057. list->thread = 1;
  2058. } else if (sd->entry == &sort_comm) {
  2059. list->comm = 1;
  2060. }
  2061. return __sort_dimension__add(sd, list, level);
  2062. }
  2063. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  2064. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  2065. if (strncasecmp(tok, hd->name, strlen(tok)))
  2066. continue;
  2067. return __hpp_dimension__add(hd, list, level);
  2068. }
  2069. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  2070. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  2071. if (strncasecmp(tok, sd->name, strlen(tok)))
  2072. continue;
  2073. if (sort__mode != SORT_MODE__BRANCH)
  2074. return -EINVAL;
  2075. if (sd->entry == &sort_sym_from || sd->entry == &sort_sym_to)
  2076. list->sym = 1;
  2077. __sort_dimension__add(sd, list, level);
  2078. return 0;
  2079. }
  2080. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  2081. struct sort_dimension *sd = &memory_sort_dimensions[i];
  2082. if (strncasecmp(tok, sd->name, strlen(tok)))
  2083. continue;
  2084. if (sort__mode != SORT_MODE__MEMORY)
  2085. return -EINVAL;
  2086. if (sd->entry == &sort_mem_dcacheline && cacheline_size == 0)
  2087. return -EINVAL;
  2088. if (sd->entry == &sort_mem_daddr_sym)
  2089. list->sym = 1;
  2090. __sort_dimension__add(sd, list, level);
  2091. return 0;
  2092. }
  2093. if (!add_dynamic_entry(evlist, tok, level))
  2094. return 0;
  2095. return -ESRCH;
  2096. }
  2097. static int setup_sort_list(struct perf_hpp_list *list, char *str,
  2098. struct perf_evlist *evlist)
  2099. {
  2100. char *tmp, *tok;
  2101. int ret = 0;
  2102. int level = 0;
  2103. int next_level = 1;
  2104. bool in_group = false;
  2105. do {
  2106. tok = str;
  2107. tmp = strpbrk(str, "{}, ");
  2108. if (tmp) {
  2109. if (in_group)
  2110. next_level = level;
  2111. else
  2112. next_level = level + 1;
  2113. if (*tmp == '{')
  2114. in_group = true;
  2115. else if (*tmp == '}')
  2116. in_group = false;
  2117. *tmp = '\0';
  2118. str = tmp + 1;
  2119. }
  2120. if (*tok) {
  2121. ret = sort_dimension__add(list, tok, evlist, level);
  2122. if (ret == -EINVAL) {
  2123. if (!cacheline_size && !strncasecmp(tok, "dcacheline", strlen(tok)))
  2124. pr_err("The \"dcacheline\" --sort key needs to know the cacheline size and it couldn't be determined on this system");
  2125. else
  2126. pr_err("Invalid --sort key: `%s'", tok);
  2127. break;
  2128. } else if (ret == -ESRCH) {
  2129. pr_err("Unknown --sort key: `%s'", tok);
  2130. break;
  2131. }
  2132. }
  2133. level = next_level;
  2134. } while (tmp);
  2135. return ret;
  2136. }
  2137. static const char *get_default_sort_order(struct perf_evlist *evlist)
  2138. {
  2139. const char *default_sort_orders[] = {
  2140. default_sort_order,
  2141. default_branch_sort_order,
  2142. default_mem_sort_order,
  2143. default_top_sort_order,
  2144. default_diff_sort_order,
  2145. default_tracepoint_sort_order,
  2146. };
  2147. bool use_trace = true;
  2148. struct perf_evsel *evsel;
  2149. BUG_ON(sort__mode >= ARRAY_SIZE(default_sort_orders));
  2150. if (evlist == NULL || perf_evlist__empty(evlist))
  2151. goto out_no_evlist;
  2152. evlist__for_each_entry(evlist, evsel) {
  2153. if (evsel->attr.type != PERF_TYPE_TRACEPOINT) {
  2154. use_trace = false;
  2155. break;
  2156. }
  2157. }
  2158. if (use_trace) {
  2159. sort__mode = SORT_MODE__TRACEPOINT;
  2160. if (symbol_conf.raw_trace)
  2161. return "trace_fields";
  2162. }
  2163. out_no_evlist:
  2164. return default_sort_orders[sort__mode];
  2165. }
  2166. static int setup_sort_order(struct perf_evlist *evlist)
  2167. {
  2168. char *new_sort_order;
  2169. /*
  2170. * Append '+'-prefixed sort order to the default sort
  2171. * order string.
  2172. */
  2173. if (!sort_order || is_strict_order(sort_order))
  2174. return 0;
  2175. if (sort_order[1] == '\0') {
  2176. pr_err("Invalid --sort key: `+'");
  2177. return -EINVAL;
  2178. }
  2179. /*
  2180. * We allocate new sort_order string, but we never free it,
  2181. * because it's checked over the rest of the code.
  2182. */
  2183. if (asprintf(&new_sort_order, "%s,%s",
  2184. get_default_sort_order(evlist), sort_order + 1) < 0) {
  2185. pr_err("Not enough memory to set up --sort");
  2186. return -ENOMEM;
  2187. }
  2188. sort_order = new_sort_order;
  2189. return 0;
  2190. }
  2191. /*
  2192. * Adds 'pre,' prefix into 'str' is 'pre' is
  2193. * not already part of 'str'.
  2194. */
  2195. static char *prefix_if_not_in(const char *pre, char *str)
  2196. {
  2197. char *n;
  2198. if (!str || strstr(str, pre))
  2199. return str;
  2200. if (asprintf(&n, "%s,%s", pre, str) < 0)
  2201. return NULL;
  2202. free(str);
  2203. return n;
  2204. }
  2205. static char *setup_overhead(char *keys)
  2206. {
  2207. if (sort__mode == SORT_MODE__DIFF)
  2208. return keys;
  2209. keys = prefix_if_not_in("overhead", keys);
  2210. if (symbol_conf.cumulate_callchain)
  2211. keys = prefix_if_not_in("overhead_children", keys);
  2212. return keys;
  2213. }
  2214. static int __setup_sorting(struct perf_evlist *evlist)
  2215. {
  2216. char *str;
  2217. const char *sort_keys;
  2218. int ret = 0;
  2219. ret = setup_sort_order(evlist);
  2220. if (ret)
  2221. return ret;
  2222. sort_keys = sort_order;
  2223. if (sort_keys == NULL) {
  2224. if (is_strict_order(field_order)) {
  2225. /*
  2226. * If user specified field order but no sort order,
  2227. * we'll honor it and not add default sort orders.
  2228. */
  2229. return 0;
  2230. }
  2231. sort_keys = get_default_sort_order(evlist);
  2232. }
  2233. str = strdup(sort_keys);
  2234. if (str == NULL) {
  2235. pr_err("Not enough memory to setup sort keys");
  2236. return -ENOMEM;
  2237. }
  2238. /*
  2239. * Prepend overhead fields for backward compatibility.
  2240. */
  2241. if (!is_strict_order(field_order)) {
  2242. str = setup_overhead(str);
  2243. if (str == NULL) {
  2244. pr_err("Not enough memory to setup overhead keys");
  2245. return -ENOMEM;
  2246. }
  2247. }
  2248. ret = setup_sort_list(&perf_hpp_list, str, evlist);
  2249. free(str);
  2250. return ret;
  2251. }
  2252. void perf_hpp__set_elide(int idx, bool elide)
  2253. {
  2254. struct perf_hpp_fmt *fmt;
  2255. struct hpp_sort_entry *hse;
  2256. perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
  2257. if (!perf_hpp__is_sort_entry(fmt))
  2258. continue;
  2259. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  2260. if (hse->se->se_width_idx == idx) {
  2261. fmt->elide = elide;
  2262. break;
  2263. }
  2264. }
  2265. }
  2266. static bool __get_elide(struct strlist *list, const char *list_name, FILE *fp)
  2267. {
  2268. if (list && strlist__nr_entries(list) == 1) {
  2269. if (fp != NULL)
  2270. fprintf(fp, "# %s: %s\n", list_name,
  2271. strlist__entry(list, 0)->s);
  2272. return true;
  2273. }
  2274. return false;
  2275. }
  2276. static bool get_elide(int idx, FILE *output)
  2277. {
  2278. switch (idx) {
  2279. case HISTC_SYMBOL:
  2280. return __get_elide(symbol_conf.sym_list, "symbol", output);
  2281. case HISTC_DSO:
  2282. return __get_elide(symbol_conf.dso_list, "dso", output);
  2283. case HISTC_COMM:
  2284. return __get_elide(symbol_conf.comm_list, "comm", output);
  2285. default:
  2286. break;
  2287. }
  2288. if (sort__mode != SORT_MODE__BRANCH)
  2289. return false;
  2290. switch (idx) {
  2291. case HISTC_SYMBOL_FROM:
  2292. return __get_elide(symbol_conf.sym_from_list, "sym_from", output);
  2293. case HISTC_SYMBOL_TO:
  2294. return __get_elide(symbol_conf.sym_to_list, "sym_to", output);
  2295. case HISTC_DSO_FROM:
  2296. return __get_elide(symbol_conf.dso_from_list, "dso_from", output);
  2297. case HISTC_DSO_TO:
  2298. return __get_elide(symbol_conf.dso_to_list, "dso_to", output);
  2299. default:
  2300. break;
  2301. }
  2302. return false;
  2303. }
  2304. void sort__setup_elide(FILE *output)
  2305. {
  2306. struct perf_hpp_fmt *fmt;
  2307. struct hpp_sort_entry *hse;
  2308. perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
  2309. if (!perf_hpp__is_sort_entry(fmt))
  2310. continue;
  2311. hse = container_of(fmt, struct hpp_sort_entry, hpp);
  2312. fmt->elide = get_elide(hse->se->se_width_idx, output);
  2313. }
  2314. /*
  2315. * It makes no sense to elide all of sort entries.
  2316. * Just revert them to show up again.
  2317. */
  2318. perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
  2319. if (!perf_hpp__is_sort_entry(fmt))
  2320. continue;
  2321. if (!fmt->elide)
  2322. return;
  2323. }
  2324. perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
  2325. if (!perf_hpp__is_sort_entry(fmt))
  2326. continue;
  2327. fmt->elide = false;
  2328. }
  2329. }
  2330. int output_field_add(struct perf_hpp_list *list, char *tok)
  2331. {
  2332. unsigned int i;
  2333. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++) {
  2334. struct sort_dimension *sd = &common_sort_dimensions[i];
  2335. if (strncasecmp(tok, sd->name, strlen(tok)))
  2336. continue;
  2337. return __sort_dimension__add_output(list, sd);
  2338. }
  2339. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++) {
  2340. struct hpp_dimension *hd = &hpp_sort_dimensions[i];
  2341. if (strncasecmp(tok, hd->name, strlen(tok)))
  2342. continue;
  2343. return __hpp_dimension__add_output(list, hd);
  2344. }
  2345. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++) {
  2346. struct sort_dimension *sd = &bstack_sort_dimensions[i];
  2347. if (strncasecmp(tok, sd->name, strlen(tok)))
  2348. continue;
  2349. return __sort_dimension__add_output(list, sd);
  2350. }
  2351. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++) {
  2352. struct sort_dimension *sd = &memory_sort_dimensions[i];
  2353. if (strncasecmp(tok, sd->name, strlen(tok)))
  2354. continue;
  2355. return __sort_dimension__add_output(list, sd);
  2356. }
  2357. return -ESRCH;
  2358. }
  2359. static int setup_output_list(struct perf_hpp_list *list, char *str)
  2360. {
  2361. char *tmp, *tok;
  2362. int ret = 0;
  2363. for (tok = strtok_r(str, ", ", &tmp);
  2364. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  2365. ret = output_field_add(list, tok);
  2366. if (ret == -EINVAL) {
  2367. ui__error("Invalid --fields key: `%s'", tok);
  2368. break;
  2369. } else if (ret == -ESRCH) {
  2370. ui__error("Unknown --fields key: `%s'", tok);
  2371. break;
  2372. }
  2373. }
  2374. return ret;
  2375. }
  2376. void reset_dimensions(void)
  2377. {
  2378. unsigned int i;
  2379. for (i = 0; i < ARRAY_SIZE(common_sort_dimensions); i++)
  2380. common_sort_dimensions[i].taken = 0;
  2381. for (i = 0; i < ARRAY_SIZE(hpp_sort_dimensions); i++)
  2382. hpp_sort_dimensions[i].taken = 0;
  2383. for (i = 0; i < ARRAY_SIZE(bstack_sort_dimensions); i++)
  2384. bstack_sort_dimensions[i].taken = 0;
  2385. for (i = 0; i < ARRAY_SIZE(memory_sort_dimensions); i++)
  2386. memory_sort_dimensions[i].taken = 0;
  2387. }
  2388. bool is_strict_order(const char *order)
  2389. {
  2390. return order && (*order != '+');
  2391. }
  2392. static int __setup_output_field(void)
  2393. {
  2394. char *str, *strp;
  2395. int ret = -EINVAL;
  2396. if (field_order == NULL)
  2397. return 0;
  2398. strp = str = strdup(field_order);
  2399. if (str == NULL) {
  2400. pr_err("Not enough memory to setup output fields");
  2401. return -ENOMEM;
  2402. }
  2403. if (!is_strict_order(field_order))
  2404. strp++;
  2405. if (!strlen(strp)) {
  2406. pr_err("Invalid --fields key: `+'");
  2407. goto out;
  2408. }
  2409. ret = setup_output_list(&perf_hpp_list, strp);
  2410. out:
  2411. free(str);
  2412. return ret;
  2413. }
  2414. int setup_sorting(struct perf_evlist *evlist)
  2415. {
  2416. int err;
  2417. err = __setup_sorting(evlist);
  2418. if (err < 0)
  2419. return err;
  2420. if (parent_pattern != default_parent_pattern) {
  2421. err = sort_dimension__add(&perf_hpp_list, "parent", evlist, -1);
  2422. if (err < 0)
  2423. return err;
  2424. }
  2425. reset_dimensions();
  2426. /*
  2427. * perf diff doesn't use default hpp output fields.
  2428. */
  2429. if (sort__mode != SORT_MODE__DIFF)
  2430. perf_hpp__init();
  2431. err = __setup_output_field();
  2432. if (err < 0)
  2433. return err;
  2434. /* copy sort keys to output fields */
  2435. perf_hpp__setup_output_field(&perf_hpp_list);
  2436. /* and then copy output fields to sort keys */
  2437. perf_hpp__append_sort_keys(&perf_hpp_list);
  2438. /* setup hists-specific output fields */
  2439. if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0)
  2440. return -1;
  2441. return 0;
  2442. }
  2443. void reset_output_field(void)
  2444. {
  2445. perf_hpp_list.need_collapse = 0;
  2446. perf_hpp_list.parent = 0;
  2447. perf_hpp_list.sym = 0;
  2448. perf_hpp_list.dso = 0;
  2449. field_order = NULL;
  2450. sort_order = NULL;
  2451. reset_dimensions();
  2452. perf_hpp__reset_output_field(&perf_hpp_list);
  2453. }