builtin-c2c.c 70 KB

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