sort.c 67 KB

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