builtin-c2c.c 85 KB

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