builtin-c2c.c 66 KB

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