builtin-c2c.c 80 KB

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