sort.c 47 KB

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