builtin-c2c.c 66 KB

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