builtin-c2c.c 66 KB

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