builtin-c2c.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This is rewrite of original c2c tool introduced in here:
  4. * http://lwn.net/Articles/588866/
  5. *
  6. * The original tool was changed to fit in current perf state.
  7. *
  8. * Original authors:
  9. * Don Zickus <dzickus@redhat.com>
  10. * Dick Fowles <fowles@inreach.com>
  11. * Joe Mario <jmario@redhat.com>
  12. */
  13. #include <errno.h>
  14. #include <inttypes.h>
  15. #include <linux/compiler.h>
  16. #include <linux/err.h>
  17. #include <linux/kernel.h>
  18. #include <linux/stringify.h>
  19. #include <linux/zalloc.h>
  20. #include <asm/bug.h>
  21. #include <sys/param.h>
  22. #include "debug.h"
  23. #include "builtin.h"
  24. #include <perf/cpumap.h>
  25. #include <subcmd/pager.h>
  26. #include <subcmd/parse-options.h>
  27. #include "map_symbol.h"
  28. #include "mem-events.h"
  29. #include "session.h"
  30. #include "hist.h"
  31. #include "sort.h"
  32. #include "tool.h"
  33. #include "cacheline.h"
  34. #include "data.h"
  35. #include "event.h"
  36. #include "evlist.h"
  37. #include "evsel.h"
  38. #include "ui/browsers/hists.h"
  39. #include "thread.h"
  40. #include "mem2node.h"
  41. #include "symbol.h"
  42. #include "ui/ui.h"
  43. #include "ui/progress.h"
  44. #include "../perf.h"
  45. struct c2c_hists {
  46. struct hists hists;
  47. struct perf_hpp_list list;
  48. struct c2c_stats stats;
  49. };
  50. struct compute_stats {
  51. struct stats lcl_hitm;
  52. struct stats rmt_hitm;
  53. struct stats load;
  54. };
  55. struct c2c_hist_entry {
  56. struct c2c_hists *hists;
  57. struct c2c_stats stats;
  58. unsigned long *cpuset;
  59. unsigned long *nodeset;
  60. struct c2c_stats *node_stats;
  61. unsigned int cacheline_idx;
  62. struct compute_stats cstats;
  63. unsigned long paddr;
  64. unsigned long paddr_cnt;
  65. bool paddr_zero;
  66. char *nodestr;
  67. /*
  68. * must be at the end,
  69. * because of its callchain dynamic entry
  70. */
  71. struct hist_entry he;
  72. };
  73. static char const *coalesce_default = "iaddr";
  74. struct perf_c2c {
  75. struct perf_tool tool;
  76. struct c2c_hists hists;
  77. struct mem2node mem2node;
  78. unsigned long **nodes;
  79. int nodes_cnt;
  80. int cpus_cnt;
  81. int *cpu2node;
  82. int node_info;
  83. bool show_src;
  84. bool show_all;
  85. bool use_stdio;
  86. bool stats_only;
  87. bool symbol_full;
  88. bool stitch_lbr;
  89. /* HITM shared clines stats */
  90. struct c2c_stats hitm_stats;
  91. int shared_clines;
  92. int display;
  93. const char *coalesce;
  94. char *cl_sort;
  95. char *cl_resort;
  96. char *cl_output;
  97. };
  98. enum {
  99. DISPLAY_LCL,
  100. DISPLAY_RMT,
  101. DISPLAY_TOT,
  102. DISPLAY_MAX,
  103. };
  104. static const char *display_str[DISPLAY_MAX] = {
  105. [DISPLAY_LCL] = "Local",
  106. [DISPLAY_RMT] = "Remote",
  107. [DISPLAY_TOT] = "Total",
  108. };
  109. static const struct option c2c_options[] = {
  110. OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"),
  111. OPT_END()
  112. };
  113. static struct perf_c2c c2c;
  114. static void *c2c_he_zalloc(size_t size)
  115. {
  116. struct c2c_hist_entry *c2c_he;
  117. c2c_he = zalloc(size + sizeof(*c2c_he));
  118. if (!c2c_he)
  119. return NULL;
  120. c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt);
  121. if (!c2c_he->cpuset)
  122. return NULL;
  123. c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt);
  124. if (!c2c_he->nodeset)
  125. return NULL;
  126. c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
  127. if (!c2c_he->node_stats)
  128. return NULL;
  129. init_stats(&c2c_he->cstats.lcl_hitm);
  130. init_stats(&c2c_he->cstats.rmt_hitm);
  131. init_stats(&c2c_he->cstats.load);
  132. return &c2c_he->he;
  133. }
  134. static void c2c_he_free(void *he)
  135. {
  136. struct c2c_hist_entry *c2c_he;
  137. c2c_he = container_of(he, struct c2c_hist_entry, he);
  138. if (c2c_he->hists) {
  139. hists__delete_entries(&c2c_he->hists->hists);
  140. free(c2c_he->hists);
  141. }
  142. free(c2c_he->cpuset);
  143. free(c2c_he->nodeset);
  144. free(c2c_he->nodestr);
  145. free(c2c_he->node_stats);
  146. free(c2c_he);
  147. }
  148. static struct hist_entry_ops c2c_entry_ops = {
  149. .new = c2c_he_zalloc,
  150. .free = c2c_he_free,
  151. };
  152. static int c2c_hists__init(struct c2c_hists *hists,
  153. const char *sort,
  154. int nr_header_lines);
  155. static struct c2c_hists*
  156. he__get_c2c_hists(struct hist_entry *he,
  157. const char *sort,
  158. int nr_header_lines)
  159. {
  160. struct c2c_hist_entry *c2c_he;
  161. struct c2c_hists *hists;
  162. int ret;
  163. c2c_he = container_of(he, struct c2c_hist_entry, he);
  164. if (c2c_he->hists)
  165. return c2c_he->hists;
  166. hists = c2c_he->hists = zalloc(sizeof(*hists));
  167. if (!hists)
  168. return NULL;
  169. ret = c2c_hists__init(hists, sort, nr_header_lines);
  170. if (ret) {
  171. free(hists);
  172. return NULL;
  173. }
  174. return hists;
  175. }
  176. static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he,
  177. struct perf_sample *sample)
  178. {
  179. if (WARN_ONCE(sample->cpu == (unsigned int) -1,
  180. "WARNING: no sample cpu value"))
  181. return;
  182. set_bit(sample->cpu, c2c_he->cpuset);
  183. }
  184. static void c2c_he__set_node(struct c2c_hist_entry *c2c_he,
  185. struct perf_sample *sample)
  186. {
  187. int node;
  188. if (!sample->phys_addr) {
  189. c2c_he->paddr_zero = true;
  190. return;
  191. }
  192. node = mem2node__node(&c2c.mem2node, sample->phys_addr);
  193. if (WARN_ONCE(node < 0, "WARNING: failed to find node\n"))
  194. return;
  195. set_bit(node, c2c_he->nodeset);
  196. if (c2c_he->paddr != sample->phys_addr) {
  197. c2c_he->paddr_cnt++;
  198. c2c_he->paddr = sample->phys_addr;
  199. }
  200. }
  201. static void compute_stats(struct c2c_hist_entry *c2c_he,
  202. struct c2c_stats *stats,
  203. u64 weight)
  204. {
  205. struct compute_stats *cstats = &c2c_he->cstats;
  206. if (stats->rmt_hitm)
  207. update_stats(&cstats->rmt_hitm, weight);
  208. else if (stats->lcl_hitm)
  209. update_stats(&cstats->lcl_hitm, weight);
  210. else if (stats->load)
  211. update_stats(&cstats->load, weight);
  212. }
  213. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  214. union perf_event *event,
  215. struct perf_sample *sample,
  216. struct evsel *evsel,
  217. struct machine *machine)
  218. {
  219. struct c2c_hists *c2c_hists = &c2c.hists;
  220. struct c2c_hist_entry *c2c_he;
  221. struct c2c_stats stats = { .nr_entries = 0, };
  222. struct hist_entry *he;
  223. struct addr_location al;
  224. struct mem_info *mi, *mi_dup;
  225. int ret;
  226. if (machine__resolve(machine, &al, sample) < 0) {
  227. pr_debug("problem processing %d event, skipping it.\n",
  228. event->header.type);
  229. return -1;
  230. }
  231. if (c2c.stitch_lbr)
  232. al.thread->lbr_stitch_enable = true;
  233. ret = sample__resolve_callchain(sample, &callchain_cursor, NULL,
  234. evsel, &al, sysctl_perf_event_max_stack);
  235. if (ret)
  236. goto out;
  237. mi = sample__resolve_mem(sample, &al);
  238. if (mi == NULL)
  239. return -ENOMEM;
  240. /*
  241. * The mi object is released in hists__add_entry_ops,
  242. * if it gets sorted out into existing data, so we need
  243. * to take the copy now.
  244. */
  245. mi_dup = mem_info__get(mi);
  246. c2c_decode_stats(&stats, mi);
  247. he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
  248. &al, NULL, NULL, mi,
  249. sample, true);
  250. if (he == NULL)
  251. goto free_mi;
  252. c2c_he = container_of(he, struct c2c_hist_entry, he);
  253. c2c_add_stats(&c2c_he->stats, &stats);
  254. c2c_add_stats(&c2c_hists->stats, &stats);
  255. c2c_he__set_cpu(c2c_he, sample);
  256. c2c_he__set_node(c2c_he, sample);
  257. hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
  258. ret = hist_entry__append_callchain(he, sample);
  259. if (!ret) {
  260. /*
  261. * There's already been warning about missing
  262. * sample's cpu value. Let's account all to
  263. * node 0 in this case, without any further
  264. * warning.
  265. *
  266. * Doing node stats only for single callchain data.
  267. */
  268. int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu;
  269. int node = c2c.cpu2node[cpu];
  270. mi = mi_dup;
  271. c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2);
  272. if (!c2c_hists)
  273. goto free_mi;
  274. he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops,
  275. &al, NULL, NULL, mi,
  276. sample, true);
  277. if (he == NULL)
  278. goto free_mi;
  279. c2c_he = container_of(he, struct c2c_hist_entry, he);
  280. c2c_add_stats(&c2c_he->stats, &stats);
  281. c2c_add_stats(&c2c_hists->stats, &stats);
  282. c2c_add_stats(&c2c_he->node_stats[node], &stats);
  283. compute_stats(c2c_he, &stats, sample->weight);
  284. c2c_he__set_cpu(c2c_he, sample);
  285. c2c_he__set_node(c2c_he, sample);
  286. hists__inc_nr_samples(&c2c_hists->hists, he->filtered);
  287. ret = hist_entry__append_callchain(he, sample);
  288. }
  289. out:
  290. addr_location__put(&al);
  291. return ret;
  292. free_mi:
  293. mem_info__put(mi_dup);
  294. mem_info__put(mi);
  295. ret = -ENOMEM;
  296. goto out;
  297. }
  298. static struct perf_c2c c2c = {
  299. .tool = {
  300. .sample = process_sample_event,
  301. .mmap = perf_event__process_mmap,
  302. .mmap2 = perf_event__process_mmap2,
  303. .comm = perf_event__process_comm,
  304. .exit = perf_event__process_exit,
  305. .fork = perf_event__process_fork,
  306. .lost = perf_event__process_lost,
  307. .ordered_events = true,
  308. .ordering_requires_timestamps = true,
  309. },
  310. };
  311. static const char * const c2c_usage[] = {
  312. "perf c2c {record|report}",
  313. NULL
  314. };
  315. static const char * const __usage_report[] = {
  316. "perf c2c report",
  317. NULL
  318. };
  319. static const char * const *report_c2c_usage = __usage_report;
  320. #define C2C_HEADER_MAX 2
  321. struct c2c_header {
  322. struct {
  323. const char *text;
  324. int span;
  325. } line[C2C_HEADER_MAX];
  326. };
  327. struct c2c_dimension {
  328. struct c2c_header header;
  329. const char *name;
  330. int width;
  331. struct sort_entry *se;
  332. int64_t (*cmp)(struct perf_hpp_fmt *fmt,
  333. struct hist_entry *, struct hist_entry *);
  334. int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  335. struct hist_entry *he);
  336. int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  337. struct hist_entry *he);
  338. };
  339. struct c2c_fmt {
  340. struct perf_hpp_fmt fmt;
  341. struct c2c_dimension *dim;
  342. };
  343. #define SYMBOL_WIDTH 30
  344. static struct c2c_dimension dim_symbol;
  345. static struct c2c_dimension dim_srcline;
  346. static int symbol_width(struct hists *hists, struct sort_entry *se)
  347. {
  348. int width = hists__col_len(hists, se->se_width_idx);
  349. if (!c2c.symbol_full)
  350. width = MIN(width, SYMBOL_WIDTH);
  351. return width;
  352. }
  353. static int c2c_width(struct perf_hpp_fmt *fmt,
  354. struct perf_hpp *hpp __maybe_unused,
  355. struct hists *hists)
  356. {
  357. struct c2c_fmt *c2c_fmt;
  358. struct c2c_dimension *dim;
  359. c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
  360. dim = c2c_fmt->dim;
  361. if (dim == &dim_symbol || dim == &dim_srcline)
  362. return symbol_width(hists, dim->se);
  363. return dim->se ? hists__col_len(hists, dim->se->se_width_idx) :
  364. c2c_fmt->dim->width;
  365. }
  366. static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  367. struct hists *hists, int line, int *span)
  368. {
  369. struct perf_hpp_list *hpp_list = hists->hpp_list;
  370. struct c2c_fmt *c2c_fmt;
  371. struct c2c_dimension *dim;
  372. const char *text = NULL;
  373. int width = c2c_width(fmt, hpp, hists);
  374. c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
  375. dim = c2c_fmt->dim;
  376. if (dim->se) {
  377. text = dim->header.line[line].text;
  378. /* Use the last line from sort_entry if not defined. */
  379. if (!text && (line == hpp_list->nr_header_lines - 1))
  380. text = dim->se->se_header;
  381. } else {
  382. text = dim->header.line[line].text;
  383. if (*span) {
  384. (*span)--;
  385. return 0;
  386. } else {
  387. *span = dim->header.line[line].span;
  388. }
  389. }
  390. if (text == NULL)
  391. text = "";
  392. return scnprintf(hpp->buf, hpp->size, "%*s", width, text);
  393. }
  394. #define HEX_STR(__s, __v) \
  395. ({ \
  396. scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \
  397. __s; \
  398. })
  399. static int64_t
  400. dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  401. struct hist_entry *left, struct hist_entry *right)
  402. {
  403. return sort__dcacheline_cmp(left, right);
  404. }
  405. static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  406. struct hist_entry *he)
  407. {
  408. uint64_t addr = 0;
  409. int width = c2c_width(fmt, hpp, he->hists);
  410. char buf[20];
  411. if (he->mem_info)
  412. addr = cl_address(he->mem_info->daddr.addr);
  413. return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
  414. }
  415. static int
  416. dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  417. struct hist_entry *he)
  418. {
  419. struct c2c_hist_entry *c2c_he;
  420. int width = c2c_width(fmt, hpp, he->hists);
  421. c2c_he = container_of(he, struct c2c_hist_entry, he);
  422. if (WARN_ON_ONCE(!c2c_he->nodestr))
  423. return 0;
  424. return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr);
  425. }
  426. static int
  427. dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  428. struct hist_entry *he)
  429. {
  430. struct c2c_hist_entry *c2c_he;
  431. int width = c2c_width(fmt, hpp, he->hists);
  432. c2c_he = container_of(he, struct c2c_hist_entry, he);
  433. return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt);
  434. }
  435. static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  436. struct hist_entry *he)
  437. {
  438. uint64_t addr = 0;
  439. int width = c2c_width(fmt, hpp, he->hists);
  440. char buf[20];
  441. if (he->mem_info)
  442. addr = cl_offset(he->mem_info->daddr.al_addr);
  443. return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
  444. }
  445. static int64_t
  446. offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  447. struct hist_entry *left, struct hist_entry *right)
  448. {
  449. uint64_t l = 0, r = 0;
  450. if (left->mem_info)
  451. l = cl_offset(left->mem_info->daddr.addr);
  452. if (right->mem_info)
  453. r = cl_offset(right->mem_info->daddr.addr);
  454. return (int64_t)(r - l);
  455. }
  456. static int
  457. iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  458. struct hist_entry *he)
  459. {
  460. uint64_t addr = 0;
  461. int width = c2c_width(fmt, hpp, he->hists);
  462. char buf[20];
  463. if (he->mem_info)
  464. addr = he->mem_info->iaddr.addr;
  465. return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr));
  466. }
  467. static int64_t
  468. iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  469. struct hist_entry *left, struct hist_entry *right)
  470. {
  471. return sort__iaddr_cmp(left, right);
  472. }
  473. static int
  474. tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  475. struct hist_entry *he)
  476. {
  477. struct c2c_hist_entry *c2c_he;
  478. int width = c2c_width(fmt, hpp, he->hists);
  479. unsigned int tot_hitm;
  480. c2c_he = container_of(he, struct c2c_hist_entry, he);
  481. tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm;
  482. return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm);
  483. }
  484. static int64_t
  485. tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  486. struct hist_entry *left, struct hist_entry *right)
  487. {
  488. struct c2c_hist_entry *c2c_left;
  489. struct c2c_hist_entry *c2c_right;
  490. uint64_t tot_hitm_left;
  491. uint64_t tot_hitm_right;
  492. c2c_left = container_of(left, struct c2c_hist_entry, he);
  493. c2c_right = container_of(right, struct c2c_hist_entry, he);
  494. tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm;
  495. tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm;
  496. return tot_hitm_left - tot_hitm_right;
  497. }
  498. #define STAT_FN_ENTRY(__f) \
  499. static int \
  500. __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \
  501. struct hist_entry *he) \
  502. { \
  503. struct c2c_hist_entry *c2c_he; \
  504. int width = c2c_width(fmt, hpp, he->hists); \
  505. \
  506. c2c_he = container_of(he, struct c2c_hist_entry, he); \
  507. return scnprintf(hpp->buf, hpp->size, "%*u", width, \
  508. c2c_he->stats.__f); \
  509. }
  510. #define STAT_FN_CMP(__f) \
  511. static int64_t \
  512. __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \
  513. struct hist_entry *left, struct hist_entry *right) \
  514. { \
  515. struct c2c_hist_entry *c2c_left, *c2c_right; \
  516. \
  517. c2c_left = container_of(left, struct c2c_hist_entry, he); \
  518. c2c_right = container_of(right, struct c2c_hist_entry, he); \
  519. return (uint64_t) c2c_left->stats.__f - \
  520. (uint64_t) c2c_right->stats.__f; \
  521. }
  522. #define STAT_FN(__f) \
  523. STAT_FN_ENTRY(__f) \
  524. STAT_FN_CMP(__f)
  525. STAT_FN(rmt_hitm)
  526. STAT_FN(lcl_hitm)
  527. STAT_FN(store)
  528. STAT_FN(st_l1hit)
  529. STAT_FN(st_l1miss)
  530. STAT_FN(ld_fbhit)
  531. STAT_FN(ld_l1hit)
  532. STAT_FN(ld_l2hit)
  533. STAT_FN(ld_llchit)
  534. STAT_FN(rmt_hit)
  535. static uint64_t total_records(struct c2c_stats *stats)
  536. {
  537. uint64_t lclmiss, ldcnt, total;
  538. lclmiss = stats->lcl_dram +
  539. stats->rmt_dram +
  540. stats->rmt_hitm +
  541. stats->rmt_hit;
  542. ldcnt = lclmiss +
  543. stats->ld_fbhit +
  544. stats->ld_l1hit +
  545. stats->ld_l2hit +
  546. stats->ld_llchit +
  547. stats->lcl_hitm;
  548. total = ldcnt +
  549. stats->st_l1hit +
  550. stats->st_l1miss;
  551. return total;
  552. }
  553. static int
  554. tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  555. struct hist_entry *he)
  556. {
  557. struct c2c_hist_entry *c2c_he;
  558. int width = c2c_width(fmt, hpp, he->hists);
  559. uint64_t tot_recs;
  560. c2c_he = container_of(he, struct c2c_hist_entry, he);
  561. tot_recs = total_records(&c2c_he->stats);
  562. return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
  563. }
  564. static int64_t
  565. tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  566. struct hist_entry *left, struct hist_entry *right)
  567. {
  568. struct c2c_hist_entry *c2c_left;
  569. struct c2c_hist_entry *c2c_right;
  570. uint64_t tot_recs_left;
  571. uint64_t tot_recs_right;
  572. c2c_left = container_of(left, struct c2c_hist_entry, he);
  573. c2c_right = container_of(right, struct c2c_hist_entry, he);
  574. tot_recs_left = total_records(&c2c_left->stats);
  575. tot_recs_right = total_records(&c2c_right->stats);
  576. return tot_recs_left - tot_recs_right;
  577. }
  578. static uint64_t total_loads(struct c2c_stats *stats)
  579. {
  580. uint64_t lclmiss, ldcnt;
  581. lclmiss = stats->lcl_dram +
  582. stats->rmt_dram +
  583. stats->rmt_hitm +
  584. stats->rmt_hit;
  585. ldcnt = lclmiss +
  586. stats->ld_fbhit +
  587. stats->ld_l1hit +
  588. stats->ld_l2hit +
  589. stats->ld_llchit +
  590. stats->lcl_hitm;
  591. return ldcnt;
  592. }
  593. static int
  594. tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  595. struct hist_entry *he)
  596. {
  597. struct c2c_hist_entry *c2c_he;
  598. int width = c2c_width(fmt, hpp, he->hists);
  599. uint64_t tot_recs;
  600. c2c_he = container_of(he, struct c2c_hist_entry, he);
  601. tot_recs = total_loads(&c2c_he->stats);
  602. return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs);
  603. }
  604. static int64_t
  605. tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  606. struct hist_entry *left, struct hist_entry *right)
  607. {
  608. struct c2c_hist_entry *c2c_left;
  609. struct c2c_hist_entry *c2c_right;
  610. uint64_t tot_recs_left;
  611. uint64_t tot_recs_right;
  612. c2c_left = container_of(left, struct c2c_hist_entry, he);
  613. c2c_right = container_of(right, struct c2c_hist_entry, he);
  614. tot_recs_left = total_loads(&c2c_left->stats);
  615. tot_recs_right = total_loads(&c2c_right->stats);
  616. return tot_recs_left - tot_recs_right;
  617. }
  618. typedef double (get_percent_cb)(struct c2c_hist_entry *);
  619. static int
  620. percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  621. struct hist_entry *he, get_percent_cb get_percent)
  622. {
  623. struct c2c_hist_entry *c2c_he;
  624. int width = c2c_width(fmt, hpp, he->hists);
  625. double per;
  626. c2c_he = container_of(he, struct c2c_hist_entry, he);
  627. per = get_percent(c2c_he);
  628. #ifdef HAVE_SLANG_SUPPORT
  629. if (use_browser)
  630. return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per);
  631. #endif
  632. return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per);
  633. }
  634. static double percent_hitm(struct c2c_hist_entry *c2c_he)
  635. {
  636. struct c2c_hists *hists;
  637. struct c2c_stats *stats;
  638. struct c2c_stats *total;
  639. int tot = 0, st = 0;
  640. double p;
  641. hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
  642. stats = &c2c_he->stats;
  643. total = &hists->stats;
  644. switch (c2c.display) {
  645. case DISPLAY_RMT:
  646. st = stats->rmt_hitm;
  647. tot = total->rmt_hitm;
  648. break;
  649. case DISPLAY_LCL:
  650. st = stats->lcl_hitm;
  651. tot = total->lcl_hitm;
  652. break;
  653. case DISPLAY_TOT:
  654. st = stats->tot_hitm;
  655. tot = total->tot_hitm;
  656. default:
  657. break;
  658. }
  659. p = tot ? (double) st / tot : 0;
  660. return 100 * p;
  661. }
  662. #define PERC_STR(__s, __v) \
  663. ({ \
  664. scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
  665. __s; \
  666. })
  667. static int
  668. percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  669. struct hist_entry *he)
  670. {
  671. struct c2c_hist_entry *c2c_he;
  672. int width = c2c_width(fmt, hpp, he->hists);
  673. char buf[10];
  674. double per;
  675. c2c_he = container_of(he, struct c2c_hist_entry, he);
  676. per = percent_hitm(c2c_he);
  677. return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
  678. }
  679. static int
  680. percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  681. struct hist_entry *he)
  682. {
  683. return percent_color(fmt, hpp, he, percent_hitm);
  684. }
  685. static int64_t
  686. percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  687. struct hist_entry *left, struct hist_entry *right)
  688. {
  689. struct c2c_hist_entry *c2c_left;
  690. struct c2c_hist_entry *c2c_right;
  691. double per_left;
  692. double per_right;
  693. c2c_left = container_of(left, struct c2c_hist_entry, he);
  694. c2c_right = container_of(right, struct c2c_hist_entry, he);
  695. per_left = percent_hitm(c2c_left);
  696. per_right = percent_hitm(c2c_right);
  697. return per_left - per_right;
  698. }
  699. static struct c2c_stats *he_stats(struct hist_entry *he)
  700. {
  701. struct c2c_hist_entry *c2c_he;
  702. c2c_he = container_of(he, struct c2c_hist_entry, he);
  703. return &c2c_he->stats;
  704. }
  705. static struct c2c_stats *total_stats(struct hist_entry *he)
  706. {
  707. struct c2c_hists *hists;
  708. hists = container_of(he->hists, struct c2c_hists, hists);
  709. return &hists->stats;
  710. }
  711. static double percent(int st, int tot)
  712. {
  713. return tot ? 100. * (double) st / (double) tot : 0;
  714. }
  715. #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f)
  716. #define PERCENT_FN(__f) \
  717. static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \
  718. { \
  719. struct c2c_hists *hists; \
  720. \
  721. hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \
  722. return percent(c2c_he->stats.__f, hists->stats.__f); \
  723. }
  724. PERCENT_FN(rmt_hitm)
  725. PERCENT_FN(lcl_hitm)
  726. PERCENT_FN(st_l1hit)
  727. PERCENT_FN(st_l1miss)
  728. static int
  729. percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  730. struct hist_entry *he)
  731. {
  732. int width = c2c_width(fmt, hpp, he->hists);
  733. double per = PERCENT(he, rmt_hitm);
  734. char buf[10];
  735. return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
  736. }
  737. static int
  738. percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  739. struct hist_entry *he)
  740. {
  741. return percent_color(fmt, hpp, he, percent_rmt_hitm);
  742. }
  743. static int64_t
  744. percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  745. struct hist_entry *left, struct hist_entry *right)
  746. {
  747. double per_left;
  748. double per_right;
  749. per_left = PERCENT(left, lcl_hitm);
  750. per_right = PERCENT(right, lcl_hitm);
  751. return per_left - per_right;
  752. }
  753. static int
  754. percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  755. struct hist_entry *he)
  756. {
  757. int width = c2c_width(fmt, hpp, he->hists);
  758. double per = PERCENT(he, lcl_hitm);
  759. char buf[10];
  760. return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
  761. }
  762. static int
  763. percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  764. struct hist_entry *he)
  765. {
  766. return percent_color(fmt, hpp, he, percent_lcl_hitm);
  767. }
  768. static int64_t
  769. percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  770. struct hist_entry *left, struct hist_entry *right)
  771. {
  772. double per_left;
  773. double per_right;
  774. per_left = PERCENT(left, lcl_hitm);
  775. per_right = PERCENT(right, lcl_hitm);
  776. return per_left - per_right;
  777. }
  778. static int
  779. percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  780. struct hist_entry *he)
  781. {
  782. int width = c2c_width(fmt, hpp, he->hists);
  783. double per = PERCENT(he, st_l1hit);
  784. char buf[10];
  785. return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
  786. }
  787. static int
  788. percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  789. struct hist_entry *he)
  790. {
  791. return percent_color(fmt, hpp, he, percent_st_l1hit);
  792. }
  793. static int64_t
  794. percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  795. struct hist_entry *left, struct hist_entry *right)
  796. {
  797. double per_left;
  798. double per_right;
  799. per_left = PERCENT(left, st_l1hit);
  800. per_right = PERCENT(right, st_l1hit);
  801. return per_left - per_right;
  802. }
  803. static int
  804. percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  805. struct hist_entry *he)
  806. {
  807. int width = c2c_width(fmt, hpp, he->hists);
  808. double per = PERCENT(he, st_l1miss);
  809. char buf[10];
  810. return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
  811. }
  812. static int
  813. percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  814. struct hist_entry *he)
  815. {
  816. return percent_color(fmt, hpp, he, percent_st_l1miss);
  817. }
  818. static int64_t
  819. percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  820. struct hist_entry *left, struct hist_entry *right)
  821. {
  822. double per_left;
  823. double per_right;
  824. per_left = PERCENT(left, st_l1miss);
  825. per_right = PERCENT(right, st_l1miss);
  826. return per_left - per_right;
  827. }
  828. STAT_FN(lcl_dram)
  829. STAT_FN(rmt_dram)
  830. static int
  831. pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  832. struct hist_entry *he)
  833. {
  834. int width = c2c_width(fmt, hpp, he->hists);
  835. return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_);
  836. }
  837. static int64_t
  838. pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  839. struct hist_entry *left, struct hist_entry *right)
  840. {
  841. return left->thread->pid_ - right->thread->pid_;
  842. }
  843. static int64_t
  844. empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  845. struct hist_entry *left __maybe_unused,
  846. struct hist_entry *right __maybe_unused)
  847. {
  848. return 0;
  849. }
  850. static int
  851. node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp,
  852. struct hist_entry *he)
  853. {
  854. struct c2c_hist_entry *c2c_he;
  855. bool first = true;
  856. int node;
  857. int ret = 0;
  858. c2c_he = container_of(he, struct c2c_hist_entry, he);
  859. for (node = 0; node < c2c.nodes_cnt; node++) {
  860. DECLARE_BITMAP(set, c2c.cpus_cnt);
  861. bitmap_zero(set, c2c.cpus_cnt);
  862. bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt);
  863. if (!bitmap_weight(set, c2c.cpus_cnt)) {
  864. if (c2c.node_info == 1) {
  865. ret = scnprintf(hpp->buf, hpp->size, "%21s", " ");
  866. advance_hpp(hpp, ret);
  867. }
  868. continue;
  869. }
  870. if (!first) {
  871. ret = scnprintf(hpp->buf, hpp->size, " ");
  872. advance_hpp(hpp, ret);
  873. }
  874. switch (c2c.node_info) {
  875. case 0:
  876. ret = scnprintf(hpp->buf, hpp->size, "%2d", node);
  877. advance_hpp(hpp, ret);
  878. break;
  879. case 1:
  880. {
  881. int num = bitmap_weight(set, c2c.cpus_cnt);
  882. struct c2c_stats *stats = &c2c_he->node_stats[node];
  883. ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num);
  884. advance_hpp(hpp, ret);
  885. #define DISPLAY_HITM(__h) \
  886. if (c2c_he->stats.__h> 0) { \
  887. ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \
  888. percent(stats->__h, c2c_he->stats.__h));\
  889. } else { \
  890. ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \
  891. }
  892. switch (c2c.display) {
  893. case DISPLAY_RMT:
  894. DISPLAY_HITM(rmt_hitm);
  895. break;
  896. case DISPLAY_LCL:
  897. DISPLAY_HITM(lcl_hitm);
  898. break;
  899. case DISPLAY_TOT:
  900. DISPLAY_HITM(tot_hitm);
  901. default:
  902. break;
  903. }
  904. #undef DISPLAY_HITM
  905. advance_hpp(hpp, ret);
  906. if (c2c_he->stats.store > 0) {
  907. ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}",
  908. percent(stats->store, c2c_he->stats.store));
  909. } else {
  910. ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a");
  911. }
  912. advance_hpp(hpp, ret);
  913. break;
  914. }
  915. case 2:
  916. ret = scnprintf(hpp->buf, hpp->size, "%2d{", node);
  917. advance_hpp(hpp, ret);
  918. ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size);
  919. advance_hpp(hpp, ret);
  920. ret = scnprintf(hpp->buf, hpp->size, "}");
  921. advance_hpp(hpp, ret);
  922. break;
  923. default:
  924. break;
  925. }
  926. first = false;
  927. }
  928. return 0;
  929. }
  930. static int
  931. mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  932. struct hist_entry *he, double mean)
  933. {
  934. int width = c2c_width(fmt, hpp, he->hists);
  935. char buf[10];
  936. scnprintf(buf, 10, "%6.0f", mean);
  937. return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
  938. }
  939. #define MEAN_ENTRY(__func, __val) \
  940. static int \
  941. __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \
  942. { \
  943. struct c2c_hist_entry *c2c_he; \
  944. c2c_he = container_of(he, struct c2c_hist_entry, he); \
  945. return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \
  946. }
  947. MEAN_ENTRY(mean_rmt_entry, rmt_hitm);
  948. MEAN_ENTRY(mean_lcl_entry, lcl_hitm);
  949. MEAN_ENTRY(mean_load_entry, load);
  950. static int
  951. cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  952. struct hist_entry *he)
  953. {
  954. struct c2c_hist_entry *c2c_he;
  955. int width = c2c_width(fmt, hpp, he->hists);
  956. char buf[10];
  957. c2c_he = container_of(he, struct c2c_hist_entry, he);
  958. scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt));
  959. return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
  960. }
  961. static int
  962. cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  963. struct hist_entry *he)
  964. {
  965. struct c2c_hist_entry *c2c_he;
  966. int width = c2c_width(fmt, hpp, he->hists);
  967. char buf[10];
  968. c2c_he = container_of(he, struct c2c_hist_entry, he);
  969. scnprintf(buf, 10, "%u", c2c_he->cacheline_idx);
  970. return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
  971. }
  972. static int
  973. cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  974. struct hist_entry *he)
  975. {
  976. int width = c2c_width(fmt, hpp, he->hists);
  977. return scnprintf(hpp->buf, hpp->size, "%*s", width, "");
  978. }
  979. #define HEADER_LOW(__h) \
  980. { \
  981. .line[1] = { \
  982. .text = __h, \
  983. }, \
  984. }
  985. #define HEADER_BOTH(__h0, __h1) \
  986. { \
  987. .line[0] = { \
  988. .text = __h0, \
  989. }, \
  990. .line[1] = { \
  991. .text = __h1, \
  992. }, \
  993. }
  994. #define HEADER_SPAN(__h0, __h1, __s) \
  995. { \
  996. .line[0] = { \
  997. .text = __h0, \
  998. .span = __s, \
  999. }, \
  1000. .line[1] = { \
  1001. .text = __h1, \
  1002. }, \
  1003. }
  1004. #define HEADER_SPAN_LOW(__h) \
  1005. { \
  1006. .line[1] = { \
  1007. .text = __h, \
  1008. }, \
  1009. }
  1010. static struct c2c_dimension dim_dcacheline = {
  1011. .header = HEADER_SPAN("--- Cacheline ----", "Address", 2),
  1012. .name = "dcacheline",
  1013. .cmp = dcacheline_cmp,
  1014. .entry = dcacheline_entry,
  1015. .width = 18,
  1016. };
  1017. static struct c2c_dimension dim_dcacheline_node = {
  1018. .header = HEADER_LOW("Node"),
  1019. .name = "dcacheline_node",
  1020. .cmp = empty_cmp,
  1021. .entry = dcacheline_node_entry,
  1022. .width = 4,
  1023. };
  1024. static struct c2c_dimension dim_dcacheline_count = {
  1025. .header = HEADER_LOW("PA cnt"),
  1026. .name = "dcacheline_count",
  1027. .cmp = empty_cmp,
  1028. .entry = dcacheline_node_count,
  1029. .width = 6,
  1030. };
  1031. static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2);
  1032. static struct c2c_dimension dim_offset = {
  1033. .header = HEADER_SPAN("--- Data address -", "Offset", 2),
  1034. .name = "offset",
  1035. .cmp = offset_cmp,
  1036. .entry = offset_entry,
  1037. .width = 18,
  1038. };
  1039. static struct c2c_dimension dim_offset_node = {
  1040. .header = HEADER_LOW("Node"),
  1041. .name = "offset_node",
  1042. .cmp = empty_cmp,
  1043. .entry = dcacheline_node_entry,
  1044. .width = 4,
  1045. };
  1046. static struct c2c_dimension dim_iaddr = {
  1047. .header = HEADER_LOW("Code address"),
  1048. .name = "iaddr",
  1049. .cmp = iaddr_cmp,
  1050. .entry = iaddr_entry,
  1051. .width = 18,
  1052. };
  1053. static struct c2c_dimension dim_tot_hitm = {
  1054. .header = HEADER_SPAN("------- Load Hitm -------", "Total", 2),
  1055. .name = "tot_hitm",
  1056. .cmp = tot_hitm_cmp,
  1057. .entry = tot_hitm_entry,
  1058. .width = 7,
  1059. };
  1060. static struct c2c_dimension dim_lcl_hitm = {
  1061. .header = HEADER_SPAN_LOW("LclHitm"),
  1062. .name = "lcl_hitm",
  1063. .cmp = lcl_hitm_cmp,
  1064. .entry = lcl_hitm_entry,
  1065. .width = 7,
  1066. };
  1067. static struct c2c_dimension dim_rmt_hitm = {
  1068. .header = HEADER_SPAN_LOW("RmtHitm"),
  1069. .name = "rmt_hitm",
  1070. .cmp = rmt_hitm_cmp,
  1071. .entry = rmt_hitm_entry,
  1072. .width = 7,
  1073. };
  1074. static struct c2c_dimension dim_cl_rmt_hitm = {
  1075. .header = HEADER_SPAN("----- HITM -----", "Rmt", 1),
  1076. .name = "cl_rmt_hitm",
  1077. .cmp = rmt_hitm_cmp,
  1078. .entry = rmt_hitm_entry,
  1079. .width = 7,
  1080. };
  1081. static struct c2c_dimension dim_cl_lcl_hitm = {
  1082. .header = HEADER_SPAN_LOW("Lcl"),
  1083. .name = "cl_lcl_hitm",
  1084. .cmp = lcl_hitm_cmp,
  1085. .entry = lcl_hitm_entry,
  1086. .width = 7,
  1087. };
  1088. static struct c2c_dimension dim_tot_stores = {
  1089. .header = HEADER_BOTH("Total", "Stores"),
  1090. .name = "tot_stores",
  1091. .cmp = store_cmp,
  1092. .entry = store_entry,
  1093. .width = 7,
  1094. };
  1095. static struct c2c_dimension dim_stores_l1hit = {
  1096. .header = HEADER_SPAN("---- Stores ----", "L1Hit", 1),
  1097. .name = "stores_l1hit",
  1098. .cmp = st_l1hit_cmp,
  1099. .entry = st_l1hit_entry,
  1100. .width = 7,
  1101. };
  1102. static struct c2c_dimension dim_stores_l1miss = {
  1103. .header = HEADER_SPAN_LOW("L1Miss"),
  1104. .name = "stores_l1miss",
  1105. .cmp = st_l1miss_cmp,
  1106. .entry = st_l1miss_entry,
  1107. .width = 7,
  1108. };
  1109. static struct c2c_dimension dim_cl_stores_l1hit = {
  1110. .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
  1111. .name = "cl_stores_l1hit",
  1112. .cmp = st_l1hit_cmp,
  1113. .entry = st_l1hit_entry,
  1114. .width = 7,
  1115. };
  1116. static struct c2c_dimension dim_cl_stores_l1miss = {
  1117. .header = HEADER_SPAN_LOW("L1 Miss"),
  1118. .name = "cl_stores_l1miss",
  1119. .cmp = st_l1miss_cmp,
  1120. .entry = st_l1miss_entry,
  1121. .width = 7,
  1122. };
  1123. static struct c2c_dimension dim_ld_fbhit = {
  1124. .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2),
  1125. .name = "ld_fbhit",
  1126. .cmp = ld_fbhit_cmp,
  1127. .entry = ld_fbhit_entry,
  1128. .width = 7,
  1129. };
  1130. static struct c2c_dimension dim_ld_l1hit = {
  1131. .header = HEADER_SPAN_LOW("L1"),
  1132. .name = "ld_l1hit",
  1133. .cmp = ld_l1hit_cmp,
  1134. .entry = ld_l1hit_entry,
  1135. .width = 7,
  1136. };
  1137. static struct c2c_dimension dim_ld_l2hit = {
  1138. .header = HEADER_SPAN_LOW("L2"),
  1139. .name = "ld_l2hit",
  1140. .cmp = ld_l2hit_cmp,
  1141. .entry = ld_l2hit_entry,
  1142. .width = 7,
  1143. };
  1144. static struct c2c_dimension dim_ld_llchit = {
  1145. .header = HEADER_SPAN("- LLC Load Hit --", "LclHit", 1),
  1146. .name = "ld_lclhit",
  1147. .cmp = ld_llchit_cmp,
  1148. .entry = ld_llchit_entry,
  1149. .width = 8,
  1150. };
  1151. static struct c2c_dimension dim_ld_rmthit = {
  1152. .header = HEADER_SPAN("- RMT Load Hit --", "RmtHit", 1),
  1153. .name = "ld_rmthit",
  1154. .cmp = rmt_hit_cmp,
  1155. .entry = rmt_hit_entry,
  1156. .width = 8,
  1157. };
  1158. static struct c2c_dimension dim_tot_recs = {
  1159. .header = HEADER_BOTH("Total", "records"),
  1160. .name = "tot_recs",
  1161. .cmp = tot_recs_cmp,
  1162. .entry = tot_recs_entry,
  1163. .width = 7,
  1164. };
  1165. static struct c2c_dimension dim_tot_loads = {
  1166. .header = HEADER_BOTH("Total", "Loads"),
  1167. .name = "tot_loads",
  1168. .cmp = tot_loads_cmp,
  1169. .entry = tot_loads_entry,
  1170. .width = 7,
  1171. };
  1172. static struct c2c_header percent_hitm_header[] = {
  1173. [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"),
  1174. [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"),
  1175. [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"),
  1176. };
  1177. static struct c2c_dimension dim_percent_hitm = {
  1178. .name = "percent_hitm",
  1179. .cmp = percent_hitm_cmp,
  1180. .entry = percent_hitm_entry,
  1181. .color = percent_hitm_color,
  1182. .width = 7,
  1183. };
  1184. static struct c2c_dimension dim_percent_rmt_hitm = {
  1185. .header = HEADER_SPAN("----- HITM -----", "RmtHitm", 1),
  1186. .name = "percent_rmt_hitm",
  1187. .cmp = percent_rmt_hitm_cmp,
  1188. .entry = percent_rmt_hitm_entry,
  1189. .color = percent_rmt_hitm_color,
  1190. .width = 7,
  1191. };
  1192. static struct c2c_dimension dim_percent_lcl_hitm = {
  1193. .header = HEADER_SPAN_LOW("LclHitm"),
  1194. .name = "percent_lcl_hitm",
  1195. .cmp = percent_lcl_hitm_cmp,
  1196. .entry = percent_lcl_hitm_entry,
  1197. .color = percent_lcl_hitm_color,
  1198. .width = 7,
  1199. };
  1200. static struct c2c_dimension dim_percent_stores_l1hit = {
  1201. .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1),
  1202. .name = "percent_stores_l1hit",
  1203. .cmp = percent_stores_l1hit_cmp,
  1204. .entry = percent_stores_l1hit_entry,
  1205. .color = percent_stores_l1hit_color,
  1206. .width = 7,
  1207. };
  1208. static struct c2c_dimension dim_percent_stores_l1miss = {
  1209. .header = HEADER_SPAN_LOW("L1 Miss"),
  1210. .name = "percent_stores_l1miss",
  1211. .cmp = percent_stores_l1miss_cmp,
  1212. .entry = percent_stores_l1miss_entry,
  1213. .color = percent_stores_l1miss_color,
  1214. .width = 7,
  1215. };
  1216. static struct c2c_dimension dim_dram_lcl = {
  1217. .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1),
  1218. .name = "dram_lcl",
  1219. .cmp = lcl_dram_cmp,
  1220. .entry = lcl_dram_entry,
  1221. .width = 8,
  1222. };
  1223. static struct c2c_dimension dim_dram_rmt = {
  1224. .header = HEADER_SPAN_LOW("Rmt"),
  1225. .name = "dram_rmt",
  1226. .cmp = rmt_dram_cmp,
  1227. .entry = rmt_dram_entry,
  1228. .width = 8,
  1229. };
  1230. static struct c2c_dimension dim_pid = {
  1231. .header = HEADER_LOW("Pid"),
  1232. .name = "pid",
  1233. .cmp = pid_cmp,
  1234. .entry = pid_entry,
  1235. .width = 7,
  1236. };
  1237. static struct c2c_dimension dim_tid = {
  1238. .header = HEADER_LOW("Tid"),
  1239. .name = "tid",
  1240. .se = &sort_thread,
  1241. };
  1242. static struct c2c_dimension dim_symbol = {
  1243. .name = "symbol",
  1244. .se = &sort_sym,
  1245. };
  1246. static struct c2c_dimension dim_dso = {
  1247. .header = HEADER_BOTH("Shared", "Object"),
  1248. .name = "dso",
  1249. .se = &sort_dso,
  1250. };
  1251. static struct c2c_header header_node[3] = {
  1252. HEADER_LOW("Node"),
  1253. HEADER_LOW("Node{cpus %hitms %stores}"),
  1254. HEADER_LOW("Node{cpu list}"),
  1255. };
  1256. static struct c2c_dimension dim_node = {
  1257. .name = "node",
  1258. .cmp = empty_cmp,
  1259. .entry = node_entry,
  1260. .width = 4,
  1261. };
  1262. static struct c2c_dimension dim_mean_rmt = {
  1263. .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2),
  1264. .name = "mean_rmt",
  1265. .cmp = empty_cmp,
  1266. .entry = mean_rmt_entry,
  1267. .width = 8,
  1268. };
  1269. static struct c2c_dimension dim_mean_lcl = {
  1270. .header = HEADER_SPAN_LOW("lcl hitm"),
  1271. .name = "mean_lcl",
  1272. .cmp = empty_cmp,
  1273. .entry = mean_lcl_entry,
  1274. .width = 8,
  1275. };
  1276. static struct c2c_dimension dim_mean_load = {
  1277. .header = HEADER_SPAN_LOW("load"),
  1278. .name = "mean_load",
  1279. .cmp = empty_cmp,
  1280. .entry = mean_load_entry,
  1281. .width = 8,
  1282. };
  1283. static struct c2c_dimension dim_cpucnt = {
  1284. .header = HEADER_BOTH("cpu", "cnt"),
  1285. .name = "cpucnt",
  1286. .cmp = empty_cmp,
  1287. .entry = cpucnt_entry,
  1288. .width = 8,
  1289. };
  1290. static struct c2c_dimension dim_srcline = {
  1291. .name = "cl_srcline",
  1292. .se = &sort_srcline,
  1293. };
  1294. static struct c2c_dimension dim_dcacheline_idx = {
  1295. .header = HEADER_LOW("Index"),
  1296. .name = "cl_idx",
  1297. .cmp = empty_cmp,
  1298. .entry = cl_idx_entry,
  1299. .width = 5,
  1300. };
  1301. static struct c2c_dimension dim_dcacheline_num = {
  1302. .header = HEADER_LOW("Num"),
  1303. .name = "cl_num",
  1304. .cmp = empty_cmp,
  1305. .entry = cl_idx_entry,
  1306. .width = 5,
  1307. };
  1308. static struct c2c_dimension dim_dcacheline_num_empty = {
  1309. .header = HEADER_LOW("Num"),
  1310. .name = "cl_num_empty",
  1311. .cmp = empty_cmp,
  1312. .entry = cl_idx_empty_entry,
  1313. .width = 5,
  1314. };
  1315. static struct c2c_dimension *dimensions[] = {
  1316. &dim_dcacheline,
  1317. &dim_dcacheline_node,
  1318. &dim_dcacheline_count,
  1319. &dim_offset,
  1320. &dim_offset_node,
  1321. &dim_iaddr,
  1322. &dim_tot_hitm,
  1323. &dim_lcl_hitm,
  1324. &dim_rmt_hitm,
  1325. &dim_cl_lcl_hitm,
  1326. &dim_cl_rmt_hitm,
  1327. &dim_tot_stores,
  1328. &dim_stores_l1hit,
  1329. &dim_stores_l1miss,
  1330. &dim_cl_stores_l1hit,
  1331. &dim_cl_stores_l1miss,
  1332. &dim_ld_fbhit,
  1333. &dim_ld_l1hit,
  1334. &dim_ld_l2hit,
  1335. &dim_ld_llchit,
  1336. &dim_ld_rmthit,
  1337. &dim_tot_recs,
  1338. &dim_tot_loads,
  1339. &dim_percent_hitm,
  1340. &dim_percent_rmt_hitm,
  1341. &dim_percent_lcl_hitm,
  1342. &dim_percent_stores_l1hit,
  1343. &dim_percent_stores_l1miss,
  1344. &dim_dram_lcl,
  1345. &dim_dram_rmt,
  1346. &dim_pid,
  1347. &dim_tid,
  1348. &dim_symbol,
  1349. &dim_dso,
  1350. &dim_node,
  1351. &dim_mean_rmt,
  1352. &dim_mean_lcl,
  1353. &dim_mean_load,
  1354. &dim_cpucnt,
  1355. &dim_srcline,
  1356. &dim_dcacheline_idx,
  1357. &dim_dcacheline_num,
  1358. &dim_dcacheline_num_empty,
  1359. NULL,
  1360. };
  1361. static void fmt_free(struct perf_hpp_fmt *fmt)
  1362. {
  1363. struct c2c_fmt *c2c_fmt;
  1364. c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
  1365. free(c2c_fmt);
  1366. }
  1367. static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  1368. {
  1369. struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt);
  1370. struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt);
  1371. return c2c_a->dim == c2c_b->dim;
  1372. }
  1373. static struct c2c_dimension *get_dimension(const char *name)
  1374. {
  1375. unsigned int i;
  1376. for (i = 0; dimensions[i]; i++) {
  1377. struct c2c_dimension *dim = dimensions[i];
  1378. if (!strcmp(dim->name, name))
  1379. return dim;
  1380. }
  1381. return NULL;
  1382. }
  1383. static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1384. struct hist_entry *he)
  1385. {
  1386. struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
  1387. struct c2c_dimension *dim = c2c_fmt->dim;
  1388. size_t len = fmt->user_len;
  1389. if (!len) {
  1390. len = hists__col_len(he->hists, dim->se->se_width_idx);
  1391. if (dim == &dim_symbol || dim == &dim_srcline)
  1392. len = symbol_width(he->hists, dim->se);
  1393. }
  1394. return dim->se->se_snprintf(he, hpp->buf, hpp->size, len);
  1395. }
  1396. static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt,
  1397. struct hist_entry *a, struct hist_entry *b)
  1398. {
  1399. struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
  1400. struct c2c_dimension *dim = c2c_fmt->dim;
  1401. return dim->se->se_cmp(a, b);
  1402. }
  1403. static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt,
  1404. struct hist_entry *a, struct hist_entry *b)
  1405. {
  1406. struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt);
  1407. struct c2c_dimension *dim = c2c_fmt->dim;
  1408. int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *);
  1409. collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp;
  1410. return collapse_fn(a, b);
  1411. }
  1412. static struct c2c_fmt *get_format(const char *name)
  1413. {
  1414. struct c2c_dimension *dim = get_dimension(name);
  1415. struct c2c_fmt *c2c_fmt;
  1416. struct perf_hpp_fmt *fmt;
  1417. if (!dim)
  1418. return NULL;
  1419. c2c_fmt = zalloc(sizeof(*c2c_fmt));
  1420. if (!c2c_fmt)
  1421. return NULL;
  1422. c2c_fmt->dim = dim;
  1423. fmt = &c2c_fmt->fmt;
  1424. INIT_LIST_HEAD(&fmt->list);
  1425. INIT_LIST_HEAD(&fmt->sort_list);
  1426. fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp;
  1427. fmt->sort = dim->se ? c2c_se_cmp : dim->cmp;
  1428. fmt->color = dim->se ? NULL : dim->color;
  1429. fmt->entry = dim->se ? c2c_se_entry : dim->entry;
  1430. fmt->header = c2c_header;
  1431. fmt->width = c2c_width;
  1432. fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp;
  1433. fmt->equal = fmt_equal;
  1434. fmt->free = fmt_free;
  1435. return c2c_fmt;
  1436. }
  1437. static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name)
  1438. {
  1439. struct c2c_fmt *c2c_fmt = get_format(name);
  1440. if (!c2c_fmt) {
  1441. reset_dimensions();
  1442. return output_field_add(hpp_list, name);
  1443. }
  1444. perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt);
  1445. return 0;
  1446. }
  1447. static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name)
  1448. {
  1449. struct c2c_fmt *c2c_fmt = get_format(name);
  1450. struct c2c_dimension *dim;
  1451. if (!c2c_fmt) {
  1452. reset_dimensions();
  1453. return sort_dimension__add(hpp_list, name, NULL, 0);
  1454. }
  1455. dim = c2c_fmt->dim;
  1456. if (dim == &dim_dso)
  1457. hpp_list->dso = 1;
  1458. perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt);
  1459. return 0;
  1460. }
  1461. #define PARSE_LIST(_list, _fn) \
  1462. do { \
  1463. char *tmp, *tok; \
  1464. ret = 0; \
  1465. \
  1466. if (!_list) \
  1467. break; \
  1468. \
  1469. for (tok = strtok_r((char *)_list, ", ", &tmp); \
  1470. tok; tok = strtok_r(NULL, ", ", &tmp)) { \
  1471. ret = _fn(hpp_list, tok); \
  1472. if (ret == -EINVAL) { \
  1473. pr_err("Invalid --fields key: `%s'", tok); \
  1474. break; \
  1475. } else if (ret == -ESRCH) { \
  1476. pr_err("Unknown --fields key: `%s'", tok); \
  1477. break; \
  1478. } \
  1479. } \
  1480. } while (0)
  1481. static int hpp_list__parse(struct perf_hpp_list *hpp_list,
  1482. const char *output_,
  1483. const char *sort_)
  1484. {
  1485. char *output = output_ ? strdup(output_) : NULL;
  1486. char *sort = sort_ ? strdup(sort_) : NULL;
  1487. int ret;
  1488. PARSE_LIST(output, c2c_hists__init_output);
  1489. PARSE_LIST(sort, c2c_hists__init_sort);
  1490. /* copy sort keys to output fields */
  1491. perf_hpp__setup_output_field(hpp_list);
  1492. /*
  1493. * We dont need other sorting keys other than those
  1494. * we already specified. It also really slows down
  1495. * the processing a lot with big number of output
  1496. * fields, so switching this off for c2c.
  1497. */
  1498. #if 0
  1499. /* and then copy output fields to sort keys */
  1500. perf_hpp__append_sort_keys(&hists->list);
  1501. #endif
  1502. free(output);
  1503. free(sort);
  1504. return ret;
  1505. }
  1506. static int c2c_hists__init(struct c2c_hists *hists,
  1507. const char *sort,
  1508. int nr_header_lines)
  1509. {
  1510. __hists__init(&hists->hists, &hists->list);
  1511. /*
  1512. * Initialize only with sort fields, we need to resort
  1513. * later anyway, and that's where we add output fields
  1514. * as well.
  1515. */
  1516. perf_hpp_list__init(&hists->list);
  1517. /* Overload number of header lines.*/
  1518. hists->list.nr_header_lines = nr_header_lines;
  1519. return hpp_list__parse(&hists->list, NULL, sort);
  1520. }
  1521. static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
  1522. const char *output,
  1523. const char *sort)
  1524. {
  1525. perf_hpp__reset_output_field(&c2c_hists->list);
  1526. return hpp_list__parse(&c2c_hists->list, output, sort);
  1527. }
  1528. #define DISPLAY_LINE_LIMIT 0.001
  1529. static bool he__display(struct hist_entry *he, struct c2c_stats *stats)
  1530. {
  1531. struct c2c_hist_entry *c2c_he;
  1532. double ld_dist;
  1533. if (c2c.show_all)
  1534. return true;
  1535. c2c_he = container_of(he, struct c2c_hist_entry, he);
  1536. #define FILTER_HITM(__h) \
  1537. if (stats->__h) { \
  1538. ld_dist = ((double)c2c_he->stats.__h / stats->__h); \
  1539. if (ld_dist < DISPLAY_LINE_LIMIT) \
  1540. he->filtered = HIST_FILTER__C2C; \
  1541. } else { \
  1542. he->filtered = HIST_FILTER__C2C; \
  1543. }
  1544. switch (c2c.display) {
  1545. case DISPLAY_LCL:
  1546. FILTER_HITM(lcl_hitm);
  1547. break;
  1548. case DISPLAY_RMT:
  1549. FILTER_HITM(rmt_hitm);
  1550. break;
  1551. case DISPLAY_TOT:
  1552. FILTER_HITM(tot_hitm);
  1553. default:
  1554. break;
  1555. }
  1556. #undef FILTER_HITM
  1557. return he->filtered == 0;
  1558. }
  1559. static inline int valid_hitm_or_store(struct hist_entry *he)
  1560. {
  1561. struct c2c_hist_entry *c2c_he;
  1562. bool has_hitm;
  1563. c2c_he = container_of(he, struct c2c_hist_entry, he);
  1564. has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm :
  1565. c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm :
  1566. c2c_he->stats.rmt_hitm;
  1567. return has_hitm || c2c_he->stats.store;
  1568. }
  1569. static void set_node_width(struct c2c_hist_entry *c2c_he, int len)
  1570. {
  1571. struct c2c_dimension *dim;
  1572. dim = &c2c.hists == c2c_he->hists ?
  1573. &dim_dcacheline_node : &dim_offset_node;
  1574. if (len > dim->width)
  1575. dim->width = len;
  1576. }
  1577. static int set_nodestr(struct c2c_hist_entry *c2c_he)
  1578. {
  1579. char buf[30];
  1580. int len;
  1581. if (c2c_he->nodestr)
  1582. return 0;
  1583. if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) {
  1584. len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt,
  1585. buf, sizeof(buf));
  1586. } else {
  1587. len = scnprintf(buf, sizeof(buf), "N/A");
  1588. }
  1589. set_node_width(c2c_he, len);
  1590. c2c_he->nodestr = strdup(buf);
  1591. return c2c_he->nodestr ? 0 : -ENOMEM;
  1592. }
  1593. static void calc_width(struct c2c_hist_entry *c2c_he)
  1594. {
  1595. struct c2c_hists *c2c_hists;
  1596. c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists);
  1597. hists__calc_col_len(&c2c_hists->hists, &c2c_he->he);
  1598. set_nodestr(c2c_he);
  1599. }
  1600. static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)
  1601. {
  1602. struct c2c_hist_entry *c2c_he;
  1603. c2c_he = container_of(he, struct c2c_hist_entry, he);
  1604. if (c2c.show_src && !he->srcline)
  1605. he->srcline = hist_entry__srcline(he);
  1606. calc_width(c2c_he);
  1607. if (!valid_hitm_or_store(he))
  1608. he->filtered = HIST_FILTER__C2C;
  1609. return 0;
  1610. }
  1611. static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused)
  1612. {
  1613. struct c2c_hist_entry *c2c_he;
  1614. struct c2c_hists *c2c_hists;
  1615. bool display = he__display(he, &c2c.hitm_stats);
  1616. c2c_he = container_of(he, struct c2c_hist_entry, he);
  1617. c2c_hists = c2c_he->hists;
  1618. if (display && c2c_hists) {
  1619. static unsigned int idx;
  1620. c2c_he->cacheline_idx = idx++;
  1621. calc_width(c2c_he);
  1622. c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort);
  1623. hists__collapse_resort(&c2c_hists->hists, NULL);
  1624. hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb);
  1625. }
  1626. return 0;
  1627. }
  1628. static void setup_nodes_header(void)
  1629. {
  1630. dim_node.header = header_node[c2c.node_info];
  1631. }
  1632. static int setup_nodes(struct perf_session *session)
  1633. {
  1634. struct numa_node *n;
  1635. unsigned long **nodes;
  1636. int node, cpu;
  1637. int *cpu2node;
  1638. if (c2c.node_info > 2)
  1639. c2c.node_info = 2;
  1640. c2c.nodes_cnt = session->header.env.nr_numa_nodes;
  1641. c2c.cpus_cnt = session->header.env.nr_cpus_avail;
  1642. n = session->header.env.numa_nodes;
  1643. if (!n)
  1644. return -EINVAL;
  1645. nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt);
  1646. if (!nodes)
  1647. return -ENOMEM;
  1648. c2c.nodes = nodes;
  1649. cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt);
  1650. if (!cpu2node)
  1651. return -ENOMEM;
  1652. for (cpu = 0; cpu < c2c.cpus_cnt; cpu++)
  1653. cpu2node[cpu] = -1;
  1654. c2c.cpu2node = cpu2node;
  1655. for (node = 0; node < c2c.nodes_cnt; node++) {
  1656. struct perf_cpu_map *map = n[node].map;
  1657. unsigned long *set;
  1658. set = bitmap_alloc(c2c.cpus_cnt);
  1659. if (!set)
  1660. return -ENOMEM;
  1661. nodes[node] = set;
  1662. /* empty node, skip */
  1663. if (perf_cpu_map__empty(map))
  1664. continue;
  1665. for (cpu = 0; cpu < map->nr; cpu++) {
  1666. set_bit(map->map[cpu], set);
  1667. if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug"))
  1668. return -EINVAL;
  1669. cpu2node[map->map[cpu]] = node;
  1670. }
  1671. }
  1672. setup_nodes_header();
  1673. return 0;
  1674. }
  1675. #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm)
  1676. static int resort_hitm_cb(struct hist_entry *he, void *arg __maybe_unused)
  1677. {
  1678. struct c2c_hist_entry *c2c_he;
  1679. c2c_he = container_of(he, struct c2c_hist_entry, he);
  1680. if (HAS_HITMS(c2c_he)) {
  1681. c2c.shared_clines++;
  1682. c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats);
  1683. }
  1684. return 0;
  1685. }
  1686. static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb)
  1687. {
  1688. struct rb_node *next = rb_first_cached(&hists->entries);
  1689. int ret = 0;
  1690. while (next) {
  1691. struct hist_entry *he;
  1692. he = rb_entry(next, struct hist_entry, rb_node);
  1693. ret = cb(he, NULL);
  1694. if (ret)
  1695. break;
  1696. next = rb_next(&he->rb_node);
  1697. }
  1698. return ret;
  1699. }
  1700. static void print_c2c__display_stats(FILE *out)
  1701. {
  1702. int llc_misses;
  1703. struct c2c_stats *stats = &c2c.hists.stats;
  1704. llc_misses = stats->lcl_dram +
  1705. stats->rmt_dram +
  1706. stats->rmt_hit +
  1707. stats->rmt_hitm;
  1708. fprintf(out, "=================================================\n");
  1709. fprintf(out, " Trace Event Information \n");
  1710. fprintf(out, "=================================================\n");
  1711. fprintf(out, " Total records : %10d\n", stats->nr_entries);
  1712. fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks);
  1713. fprintf(out, " Load Operations : %10d\n", stats->load);
  1714. fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache);
  1715. fprintf(out, " Loads - IO : %10d\n", stats->ld_io);
  1716. fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss);
  1717. fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs);
  1718. fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit);
  1719. fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit);
  1720. fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit);
  1721. fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm);
  1722. fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm);
  1723. fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm);
  1724. fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit);
  1725. fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram);
  1726. fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram);
  1727. fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl);
  1728. fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared);
  1729. fprintf(out, " Load LLC Misses : %10d\n", llc_misses);
  1730. fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.);
  1731. fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.);
  1732. fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.);
  1733. fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.);
  1734. fprintf(out, " Store Operations : %10d\n", stats->store);
  1735. fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache);
  1736. fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs);
  1737. fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit);
  1738. fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss);
  1739. fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap);
  1740. fprintf(out, " Unable to parse data source : %10d\n", stats->noparse);
  1741. }
  1742. static void print_shared_cacheline_info(FILE *out)
  1743. {
  1744. struct c2c_stats *stats = &c2c.hitm_stats;
  1745. int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm;
  1746. fprintf(out, "=================================================\n");
  1747. fprintf(out, " Global Shared Cache Line Event Information \n");
  1748. fprintf(out, "=================================================\n");
  1749. fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines);
  1750. fprintf(out, " Load HITs on shared lines : %10d\n", stats->load);
  1751. fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit);
  1752. fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit);
  1753. fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit);
  1754. fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm);
  1755. fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks);
  1756. fprintf(out, " Store HITs on shared lines : %10d\n", stats->store);
  1757. fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit);
  1758. fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store);
  1759. }
  1760. static void print_cacheline(struct c2c_hists *c2c_hists,
  1761. struct hist_entry *he_cl,
  1762. struct perf_hpp_list *hpp_list,
  1763. FILE *out)
  1764. {
  1765. char bf[1000];
  1766. struct perf_hpp hpp = {
  1767. .buf = bf,
  1768. .size = 1000,
  1769. };
  1770. static bool once;
  1771. if (!once) {
  1772. hists__fprintf_headers(&c2c_hists->hists, out);
  1773. once = true;
  1774. } else {
  1775. fprintf(out, "\n");
  1776. }
  1777. fprintf(out, " -------------------------------------------------------------\n");
  1778. __hist_entry__snprintf(he_cl, &hpp, hpp_list);
  1779. fprintf(out, "%s\n", bf);
  1780. fprintf(out, " -------------------------------------------------------------\n");
  1781. hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false);
  1782. }
  1783. static void print_pareto(FILE *out)
  1784. {
  1785. struct perf_hpp_list hpp_list;
  1786. struct rb_node *nd;
  1787. int ret;
  1788. perf_hpp_list__init(&hpp_list);
  1789. ret = hpp_list__parse(&hpp_list,
  1790. "cl_num,"
  1791. "cl_rmt_hitm,"
  1792. "cl_lcl_hitm,"
  1793. "cl_stores_l1hit,"
  1794. "cl_stores_l1miss,"
  1795. "dcacheline",
  1796. NULL);
  1797. if (WARN_ONCE(ret, "failed to setup sort entries\n"))
  1798. return;
  1799. nd = rb_first_cached(&c2c.hists.hists.entries);
  1800. for (; nd; nd = rb_next(nd)) {
  1801. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  1802. struct c2c_hist_entry *c2c_he;
  1803. if (he->filtered)
  1804. continue;
  1805. c2c_he = container_of(he, struct c2c_hist_entry, he);
  1806. print_cacheline(c2c_he->hists, he, &hpp_list, out);
  1807. }
  1808. }
  1809. static void print_c2c_info(FILE *out, struct perf_session *session)
  1810. {
  1811. struct evlist *evlist = session->evlist;
  1812. struct evsel *evsel;
  1813. bool first = true;
  1814. fprintf(out, "=================================================\n");
  1815. fprintf(out, " c2c details \n");
  1816. fprintf(out, "=================================================\n");
  1817. evlist__for_each_entry(evlist, evsel) {
  1818. fprintf(out, "%-36s: %s\n", first ? " Events" : "", evsel__name(evsel));
  1819. first = false;
  1820. }
  1821. fprintf(out, " Cachelines sort on : %s HITMs\n",
  1822. display_str[c2c.display]);
  1823. fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort);
  1824. }
  1825. static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session)
  1826. {
  1827. setup_pager();
  1828. print_c2c__display_stats(out);
  1829. fprintf(out, "\n");
  1830. print_shared_cacheline_info(out);
  1831. fprintf(out, "\n");
  1832. print_c2c_info(out, session);
  1833. if (c2c.stats_only)
  1834. return;
  1835. fprintf(out, "\n");
  1836. fprintf(out, "=================================================\n");
  1837. fprintf(out, " Shared Data Cache Line Table \n");
  1838. fprintf(out, "=================================================\n");
  1839. fprintf(out, "#\n");
  1840. hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true);
  1841. fprintf(out, "\n");
  1842. fprintf(out, "=================================================\n");
  1843. fprintf(out, " Shared Cache Line Distribution Pareto \n");
  1844. fprintf(out, "=================================================\n");
  1845. fprintf(out, "#\n");
  1846. print_pareto(out);
  1847. }
  1848. #ifdef HAVE_SLANG_SUPPORT
  1849. static void c2c_browser__update_nr_entries(struct hist_browser *hb)
  1850. {
  1851. u64 nr_entries = 0;
  1852. struct rb_node *nd = rb_first_cached(&hb->hists->entries);
  1853. while (nd) {
  1854. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  1855. if (!he->filtered)
  1856. nr_entries++;
  1857. nd = rb_next(nd);
  1858. }
  1859. hb->nr_non_filtered_entries = nr_entries;
  1860. }
  1861. struct c2c_cacheline_browser {
  1862. struct hist_browser hb;
  1863. struct hist_entry *he;
  1864. };
  1865. static int
  1866. perf_c2c_cacheline_browser__title(struct hist_browser *browser,
  1867. char *bf, size_t size)
  1868. {
  1869. struct c2c_cacheline_browser *cl_browser;
  1870. struct hist_entry *he;
  1871. uint64_t addr = 0;
  1872. cl_browser = container_of(browser, struct c2c_cacheline_browser, hb);
  1873. he = cl_browser->he;
  1874. if (he->mem_info)
  1875. addr = cl_address(he->mem_info->daddr.addr);
  1876. scnprintf(bf, size, "Cacheline 0x%lx", addr);
  1877. return 0;
  1878. }
  1879. static struct c2c_cacheline_browser*
  1880. c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he)
  1881. {
  1882. struct c2c_cacheline_browser *browser;
  1883. browser = zalloc(sizeof(*browser));
  1884. if (browser) {
  1885. hist_browser__init(&browser->hb, hists);
  1886. browser->hb.c2c_filter = true;
  1887. browser->hb.title = perf_c2c_cacheline_browser__title;
  1888. browser->he = he;
  1889. }
  1890. return browser;
  1891. }
  1892. static int perf_c2c__browse_cacheline(struct hist_entry *he)
  1893. {
  1894. struct c2c_hist_entry *c2c_he;
  1895. struct c2c_hists *c2c_hists;
  1896. struct c2c_cacheline_browser *cl_browser;
  1897. struct hist_browser *browser;
  1898. int key = -1;
  1899. static const char help[] =
  1900. " ENTER Toggle callchains (if present) \n"
  1901. " n Toggle Node details info \n"
  1902. " s Toggle full length of symbol and source line columns \n"
  1903. " q Return back to cacheline list \n";
  1904. if (!he)
  1905. return 0;
  1906. /* Display compact version first. */
  1907. c2c.symbol_full = false;
  1908. c2c_he = container_of(he, struct c2c_hist_entry, he);
  1909. c2c_hists = c2c_he->hists;
  1910. cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he);
  1911. if (cl_browser == NULL)
  1912. return -1;
  1913. browser = &cl_browser->hb;
  1914. /* reset abort key so that it can get Ctrl-C as a key */
  1915. SLang_reset_tty();
  1916. SLang_init_tty(0, 0, 0);
  1917. c2c_browser__update_nr_entries(browser);
  1918. while (1) {
  1919. key = hist_browser__run(browser, "? - help", true, 0);
  1920. switch (key) {
  1921. case 's':
  1922. c2c.symbol_full = !c2c.symbol_full;
  1923. break;
  1924. case 'n':
  1925. c2c.node_info = (c2c.node_info + 1) % 3;
  1926. setup_nodes_header();
  1927. break;
  1928. case 'q':
  1929. goto out;
  1930. case '?':
  1931. ui_browser__help_window(&browser->b, help);
  1932. break;
  1933. default:
  1934. break;
  1935. }
  1936. }
  1937. out:
  1938. free(cl_browser);
  1939. return 0;
  1940. }
  1941. static int perf_c2c_browser__title(struct hist_browser *browser,
  1942. char *bf, size_t size)
  1943. {
  1944. scnprintf(bf, size,
  1945. "Shared Data Cache Line Table "
  1946. "(%lu entries, sorted on %s HITMs)",
  1947. browser->nr_non_filtered_entries,
  1948. display_str[c2c.display]);
  1949. return 0;
  1950. }
  1951. static struct hist_browser*
  1952. perf_c2c_browser__new(struct hists *hists)
  1953. {
  1954. struct hist_browser *browser = hist_browser__new(hists);
  1955. if (browser) {
  1956. browser->title = perf_c2c_browser__title;
  1957. browser->c2c_filter = true;
  1958. }
  1959. return browser;
  1960. }
  1961. static int perf_c2c__hists_browse(struct hists *hists)
  1962. {
  1963. struct hist_browser *browser;
  1964. int key = -1;
  1965. static const char help[] =
  1966. " d Display cacheline details \n"
  1967. " ENTER Toggle callchains (if present) \n"
  1968. " q Quit \n";
  1969. browser = perf_c2c_browser__new(hists);
  1970. if (browser == NULL)
  1971. return -1;
  1972. /* reset abort key so that it can get Ctrl-C as a key */
  1973. SLang_reset_tty();
  1974. SLang_init_tty(0, 0, 0);
  1975. c2c_browser__update_nr_entries(browser);
  1976. while (1) {
  1977. key = hist_browser__run(browser, "? - help", true, 0);
  1978. switch (key) {
  1979. case 'q':
  1980. goto out;
  1981. case 'd':
  1982. perf_c2c__browse_cacheline(browser->he_selection);
  1983. break;
  1984. case '?':
  1985. ui_browser__help_window(&browser->b, help);
  1986. break;
  1987. default:
  1988. break;
  1989. }
  1990. }
  1991. out:
  1992. hist_browser__delete(browser);
  1993. return 0;
  1994. }
  1995. static void perf_c2c_display(struct perf_session *session)
  1996. {
  1997. if (use_browser == 0)
  1998. perf_c2c__hists_fprintf(stdout, session);
  1999. else
  2000. perf_c2c__hists_browse(&c2c.hists.hists);
  2001. }
  2002. #else
  2003. static void perf_c2c_display(struct perf_session *session)
  2004. {
  2005. use_browser = 0;
  2006. perf_c2c__hists_fprintf(stdout, session);
  2007. }
  2008. #endif /* HAVE_SLANG_SUPPORT */
  2009. static char *fill_line(const char *orig, int len)
  2010. {
  2011. int i, j, olen = strlen(orig);
  2012. char *buf;
  2013. buf = zalloc(len + 1);
  2014. if (!buf)
  2015. return NULL;
  2016. j = len / 2 - olen / 2;
  2017. for (i = 0; i < j - 1; i++)
  2018. buf[i] = '-';
  2019. buf[i++] = ' ';
  2020. strcpy(buf + i, orig);
  2021. i += olen;
  2022. buf[i++] = ' ';
  2023. for (; i < len; i++)
  2024. buf[i] = '-';
  2025. return buf;
  2026. }
  2027. static int ui_quirks(void)
  2028. {
  2029. const char *nodestr = "Data address";
  2030. char *buf;
  2031. if (!c2c.use_stdio) {
  2032. dim_offset.width = 5;
  2033. dim_offset.header = header_offset_tui;
  2034. nodestr = "CL";
  2035. }
  2036. dim_percent_hitm.header = percent_hitm_header[c2c.display];
  2037. /* Fix the zero line for dcacheline column. */
  2038. buf = fill_line("Cacheline", dim_dcacheline.width +
  2039. dim_dcacheline_node.width +
  2040. dim_dcacheline_count.width + 4);
  2041. if (!buf)
  2042. return -ENOMEM;
  2043. dim_dcacheline.header.line[0].text = buf;
  2044. /* Fix the zero line for offset column. */
  2045. buf = fill_line(nodestr, dim_offset.width +
  2046. dim_offset_node.width +
  2047. dim_dcacheline_count.width + 4);
  2048. if (!buf)
  2049. return -ENOMEM;
  2050. dim_offset.header.line[0].text = buf;
  2051. return 0;
  2052. }
  2053. #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
  2054. const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
  2055. CALLCHAIN_REPORT_HELP
  2056. "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
  2057. static int
  2058. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  2059. {
  2060. struct callchain_param *callchain = opt->value;
  2061. callchain->enabled = !unset;
  2062. /*
  2063. * --no-call-graph
  2064. */
  2065. if (unset) {
  2066. symbol_conf.use_callchain = false;
  2067. callchain->mode = CHAIN_NONE;
  2068. return 0;
  2069. }
  2070. return parse_callchain_report_opt(arg);
  2071. }
  2072. static int setup_callchain(struct evlist *evlist)
  2073. {
  2074. u64 sample_type = evlist__combined_sample_type(evlist);
  2075. enum perf_call_graph_mode mode = CALLCHAIN_NONE;
  2076. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  2077. (sample_type & PERF_SAMPLE_STACK_USER)) {
  2078. mode = CALLCHAIN_DWARF;
  2079. dwarf_callchain_users = true;
  2080. } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  2081. mode = CALLCHAIN_LBR;
  2082. else if (sample_type & PERF_SAMPLE_CALLCHAIN)
  2083. mode = CALLCHAIN_FP;
  2084. if (!callchain_param.enabled &&
  2085. callchain_param.mode != CHAIN_NONE &&
  2086. mode != CALLCHAIN_NONE) {
  2087. symbol_conf.use_callchain = true;
  2088. if (callchain_register_param(&callchain_param) < 0) {
  2089. ui__error("Can't register callchain params.\n");
  2090. return -EINVAL;
  2091. }
  2092. }
  2093. if (c2c.stitch_lbr && (mode != CALLCHAIN_LBR)) {
  2094. ui__warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
  2095. "Please apply --call-graph lbr when recording.\n");
  2096. c2c.stitch_lbr = false;
  2097. }
  2098. callchain_param.record_mode = mode;
  2099. callchain_param.min_percent = 0;
  2100. return 0;
  2101. }
  2102. static int setup_display(const char *str)
  2103. {
  2104. const char *display = str ?: "tot";
  2105. if (!strcmp(display, "tot"))
  2106. c2c.display = DISPLAY_TOT;
  2107. else if (!strcmp(display, "rmt"))
  2108. c2c.display = DISPLAY_RMT;
  2109. else if (!strcmp(display, "lcl"))
  2110. c2c.display = DISPLAY_LCL;
  2111. else {
  2112. pr_err("failed: unknown display type: %s\n", str);
  2113. return -1;
  2114. }
  2115. return 0;
  2116. }
  2117. #define for_each_token(__tok, __buf, __sep, __tmp) \
  2118. for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \
  2119. __tok = strtok_r(NULL, __sep, &__tmp))
  2120. static int build_cl_output(char *cl_sort, bool no_source)
  2121. {
  2122. char *tok, *tmp, *buf = strdup(cl_sort);
  2123. bool add_pid = false;
  2124. bool add_tid = false;
  2125. bool add_iaddr = false;
  2126. bool add_sym = false;
  2127. bool add_dso = false;
  2128. bool add_src = false;
  2129. int ret = 0;
  2130. if (!buf)
  2131. return -ENOMEM;
  2132. for_each_token(tok, buf, ",", tmp) {
  2133. if (!strcmp(tok, "tid")) {
  2134. add_tid = true;
  2135. } else if (!strcmp(tok, "pid")) {
  2136. add_pid = true;
  2137. } else if (!strcmp(tok, "iaddr")) {
  2138. add_iaddr = true;
  2139. add_sym = true;
  2140. add_dso = true;
  2141. add_src = no_source ? false : true;
  2142. } else if (!strcmp(tok, "dso")) {
  2143. add_dso = true;
  2144. } else if (strcmp(tok, "offset")) {
  2145. pr_err("unrecognized sort token: %s\n", tok);
  2146. ret = -EINVAL;
  2147. goto err;
  2148. }
  2149. }
  2150. if (asprintf(&c2c.cl_output,
  2151. "%s%s%s%s%s%s%s%s%s%s",
  2152. c2c.use_stdio ? "cl_num_empty," : "",
  2153. "percent_rmt_hitm,"
  2154. "percent_lcl_hitm,"
  2155. "percent_stores_l1hit,"
  2156. "percent_stores_l1miss,"
  2157. "offset,offset_node,dcacheline_count,",
  2158. add_pid ? "pid," : "",
  2159. add_tid ? "tid," : "",
  2160. add_iaddr ? "iaddr," : "",
  2161. "mean_rmt,"
  2162. "mean_lcl,"
  2163. "mean_load,"
  2164. "tot_recs,"
  2165. "cpucnt,",
  2166. add_sym ? "symbol," : "",
  2167. add_dso ? "dso," : "",
  2168. add_src ? "cl_srcline," : "",
  2169. "node") < 0) {
  2170. ret = -ENOMEM;
  2171. goto err;
  2172. }
  2173. c2c.show_src = add_src;
  2174. err:
  2175. free(buf);
  2176. return ret;
  2177. }
  2178. static int setup_coalesce(const char *coalesce, bool no_source)
  2179. {
  2180. const char *c = coalesce ?: coalesce_default;
  2181. if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0)
  2182. return -ENOMEM;
  2183. if (build_cl_output(c2c.cl_sort, no_source))
  2184. return -1;
  2185. if (asprintf(&c2c.cl_resort, "offset,%s",
  2186. c2c.display == DISPLAY_TOT ?
  2187. "tot_hitm" :
  2188. c2c.display == DISPLAY_RMT ?
  2189. "rmt_hitm,lcl_hitm" :
  2190. "lcl_hitm,rmt_hitm") < 0)
  2191. return -ENOMEM;
  2192. pr_debug("coalesce sort fields: %s\n", c2c.cl_sort);
  2193. pr_debug("coalesce resort fields: %s\n", c2c.cl_resort);
  2194. pr_debug("coalesce output fields: %s\n", c2c.cl_output);
  2195. return 0;
  2196. }
  2197. static int perf_c2c__report(int argc, const char **argv)
  2198. {
  2199. struct perf_session *session;
  2200. struct ui_progress prog;
  2201. struct perf_data data = {
  2202. .mode = PERF_DATA_MODE_READ,
  2203. };
  2204. char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
  2205. const char *display = NULL;
  2206. const char *coalesce = NULL;
  2207. bool no_source = false;
  2208. const struct option options[] = {
  2209. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  2210. "file", "vmlinux pathname"),
  2211. OPT_STRING('i', "input", &input_name, "file",
  2212. "the input file to process"),
  2213. OPT_INCR('N', "node-info", &c2c.node_info,
  2214. "show extra node info in report (repeat for more info)"),
  2215. #ifdef HAVE_SLANG_SUPPORT
  2216. OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"),
  2217. #endif
  2218. OPT_BOOLEAN(0, "stats", &c2c.stats_only,
  2219. "Display only statistic tables (implies --stdio)"),
  2220. OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full,
  2221. "Display full length of symbols"),
  2222. OPT_BOOLEAN(0, "no-source", &no_source,
  2223. "Do not display Source Line column"),
  2224. OPT_BOOLEAN(0, "show-all", &c2c.show_all,
  2225. "Show all captured HITM lines."),
  2226. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  2227. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  2228. callchain_help, &parse_callchain_opt,
  2229. callchain_default_opt),
  2230. OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"),
  2231. OPT_STRING('c', "coalesce", &coalesce, "coalesce fields",
  2232. "coalesce fields: pid,tid,iaddr,dso"),
  2233. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  2234. OPT_BOOLEAN(0, "stitch-lbr", &c2c.stitch_lbr,
  2235. "Enable LBR callgraph stitching approach"),
  2236. OPT_PARENT(c2c_options),
  2237. OPT_END()
  2238. };
  2239. int err = 0;
  2240. argc = parse_options(argc, argv, options, report_c2c_usage,
  2241. PARSE_OPT_STOP_AT_NON_OPTION);
  2242. if (argc)
  2243. usage_with_options(report_c2c_usage, options);
  2244. if (c2c.stats_only)
  2245. c2c.use_stdio = true;
  2246. if (!input_name || !strlen(input_name))
  2247. input_name = "perf.data";
  2248. data.path = input_name;
  2249. data.force = symbol_conf.force;
  2250. err = setup_display(display);
  2251. if (err)
  2252. goto out;
  2253. err = setup_coalesce(coalesce, no_source);
  2254. if (err) {
  2255. pr_debug("Failed to initialize hists\n");
  2256. goto out;
  2257. }
  2258. err = c2c_hists__init(&c2c.hists, "dcacheline", 2);
  2259. if (err) {
  2260. pr_debug("Failed to initialize hists\n");
  2261. goto out;
  2262. }
  2263. session = perf_session__new(&data, 0, &c2c.tool);
  2264. if (IS_ERR(session)) {
  2265. err = PTR_ERR(session);
  2266. pr_debug("Error creating perf session\n");
  2267. goto out;
  2268. }
  2269. err = setup_nodes(session);
  2270. if (err) {
  2271. pr_err("Failed setup nodes\n");
  2272. goto out;
  2273. }
  2274. err = mem2node__init(&c2c.mem2node, &session->header.env);
  2275. if (err)
  2276. goto out_session;
  2277. err = setup_callchain(session->evlist);
  2278. if (err)
  2279. goto out_mem2node;
  2280. if (symbol__init(&session->header.env) < 0)
  2281. goto out_mem2node;
  2282. /* No pipe support at the moment. */
  2283. if (perf_data__is_pipe(session->data)) {
  2284. pr_debug("No pipe support at the moment.\n");
  2285. goto out_mem2node;
  2286. }
  2287. if (c2c.use_stdio)
  2288. use_browser = 0;
  2289. else
  2290. use_browser = 1;
  2291. setup_browser(false);
  2292. err = perf_session__process_events(session);
  2293. if (err) {
  2294. pr_err("failed to process sample\n");
  2295. goto out_mem2node;
  2296. }
  2297. c2c_hists__reinit(&c2c.hists,
  2298. "cl_idx,"
  2299. "dcacheline,"
  2300. "dcacheline_node,"
  2301. "dcacheline_count,"
  2302. "percent_hitm,"
  2303. "tot_hitm,lcl_hitm,rmt_hitm,"
  2304. "tot_recs,"
  2305. "tot_loads,"
  2306. "tot_stores,"
  2307. "stores_l1hit,stores_l1miss,"
  2308. "ld_fbhit,ld_l1hit,ld_l2hit,"
  2309. "ld_lclhit,lcl_hitm,"
  2310. "ld_rmthit,rmt_hitm,"
  2311. "dram_lcl,dram_rmt",
  2312. c2c.display == DISPLAY_TOT ? "tot_hitm" :
  2313. c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm"
  2314. );
  2315. ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting...");
  2316. hists__collapse_resort(&c2c.hists.hists, NULL);
  2317. hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb);
  2318. hists__iterate_cb(&c2c.hists.hists, resort_cl_cb);
  2319. ui_progress__finish();
  2320. if (ui_quirks()) {
  2321. pr_err("failed to setup UI\n");
  2322. goto out_mem2node;
  2323. }
  2324. perf_c2c_display(session);
  2325. out_mem2node:
  2326. mem2node__exit(&c2c.mem2node);
  2327. out_session:
  2328. perf_session__delete(session);
  2329. out:
  2330. return err;
  2331. }
  2332. static int parse_record_events(const struct option *opt,
  2333. const char *str, int unset __maybe_unused)
  2334. {
  2335. bool *event_set = (bool *) opt->value;
  2336. if (!strcmp(str, "list")) {
  2337. perf_mem_events__list();
  2338. exit(0);
  2339. }
  2340. if (perf_mem_events__parse(str))
  2341. exit(-1);
  2342. *event_set = true;
  2343. return 0;
  2344. }
  2345. static const char * const __usage_record[] = {
  2346. "perf c2c record [<options>] [<command>]",
  2347. "perf c2c record [<options>] -- <command> [<options>]",
  2348. NULL
  2349. };
  2350. static const char * const *record_mem_usage = __usage_record;
  2351. static int perf_c2c__record(int argc, const char **argv)
  2352. {
  2353. int rec_argc, i = 0, j;
  2354. const char **rec_argv;
  2355. int ret;
  2356. bool all_user = false, all_kernel = false;
  2357. bool event_set = false;
  2358. struct option options[] = {
  2359. OPT_CALLBACK('e', "event", &event_set, "event",
  2360. "event selector. Use 'perf c2c record -e list' to list available events",
  2361. parse_record_events),
  2362. OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"),
  2363. OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"),
  2364. OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"),
  2365. OPT_PARENT(c2c_options),
  2366. OPT_END()
  2367. };
  2368. if (perf_mem_events__init()) {
  2369. pr_err("failed: memory events not supported\n");
  2370. return -1;
  2371. }
  2372. argc = parse_options(argc, argv, options, record_mem_usage,
  2373. PARSE_OPT_KEEP_UNKNOWN);
  2374. rec_argc = argc + 11; /* max number of arguments */
  2375. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  2376. if (!rec_argv)
  2377. return -1;
  2378. rec_argv[i++] = "record";
  2379. if (!event_set) {
  2380. perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
  2381. perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
  2382. }
  2383. if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
  2384. rec_argv[i++] = "-W";
  2385. rec_argv[i++] = "-d";
  2386. rec_argv[i++] = "--phys-data";
  2387. rec_argv[i++] = "--sample-cpu";
  2388. for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
  2389. if (!perf_mem_events[j].record)
  2390. continue;
  2391. if (!perf_mem_events[j].supported) {
  2392. pr_err("failed: event '%s' not supported\n",
  2393. perf_mem_events[j].name);
  2394. free(rec_argv);
  2395. return -1;
  2396. }
  2397. rec_argv[i++] = "-e";
  2398. rec_argv[i++] = perf_mem_events__name(j);
  2399. }
  2400. if (all_user)
  2401. rec_argv[i++] = "--all-user";
  2402. if (all_kernel)
  2403. rec_argv[i++] = "--all-kernel";
  2404. for (j = 0; j < argc; j++, i++)
  2405. rec_argv[i] = argv[j];
  2406. if (verbose > 0) {
  2407. pr_debug("calling: ");
  2408. j = 0;
  2409. while (rec_argv[j]) {
  2410. pr_debug("%s ", rec_argv[j]);
  2411. j++;
  2412. }
  2413. pr_debug("\n");
  2414. }
  2415. ret = cmd_record(i, rec_argv);
  2416. free(rec_argv);
  2417. return ret;
  2418. }
  2419. int cmd_c2c(int argc, const char **argv)
  2420. {
  2421. argc = parse_options(argc, argv, c2c_options, c2c_usage,
  2422. PARSE_OPT_STOP_AT_NON_OPTION);
  2423. if (!argc)
  2424. usage_with_options(c2c_usage, c2c_options);
  2425. if (!strncmp(argv[0], "rec", 3)) {
  2426. return perf_c2c__record(argc, argv);
  2427. } else if (!strncmp(argv[0], "rep", 3)) {
  2428. return perf_c2c__report(argc, argv);
  2429. } else {
  2430. usage_with_options(c2c_usage, c2c_options);
  2431. }
  2432. return 0;
  2433. }