builtin-c2c.c 70 KB

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