builtin-c2c.c 81 KB

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