builtin-c2c.c 66 KB

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