builtin-c2c.c 66 KB

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