builtin-c2c.c 70 KB

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