builtin-stat.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * builtin-stat.c
  4. *
  5. * Builtin stat command: Give a precise performance counters summary
  6. * overview about any workload, CPU or specific PID.
  7. *
  8. * Sample output:
  9. $ perf stat ./hackbench 10
  10. Time: 0.118
  11. Performance counter stats for './hackbench 10':
  12. 1708.761321 task-clock # 11.037 CPUs utilized
  13. 41,190 context-switches # 0.024 M/sec
  14. 6,735 CPU-migrations # 0.004 M/sec
  15. 17,318 page-faults # 0.010 M/sec
  16. 5,205,202,243 cycles # 3.046 GHz
  17. 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
  18. 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
  19. 2,603,501,247 instructions # 0.50 insns per cycle
  20. # 1.48 stalled cycles per insn
  21. 484,357,498 branches # 283.455 M/sec
  22. 6,388,934 branch-misses # 1.32% of all branches
  23. 0.154822978 seconds time elapsed
  24. *
  25. * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  26. *
  27. * Improvements and fixes by:
  28. *
  29. * Arjan van de Ven <arjan@linux.intel.com>
  30. * Yanmin Zhang <yanmin.zhang@intel.com>
  31. * Wu Fengguang <fengguang.wu@intel.com>
  32. * Mike Galbraith <efault@gmx.de>
  33. * Paul Mackerras <paulus@samba.org>
  34. * Jaswinder Singh Rajput <jaswinder@kernel.org>
  35. */
  36. #include "builtin.h"
  37. #include "util/cgroup.h"
  38. #include <subcmd/parse-options.h>
  39. #include "util/parse-events.h"
  40. #include "util/pmus.h"
  41. #include "util/pmu.h"
  42. #include "util/tool_pmu.h"
  43. #include "util/event.h"
  44. #include "util/evlist.h"
  45. #include "util/evsel.h"
  46. #include "util/debug.h"
  47. #include "util/color.h"
  48. #include "util/stat.h"
  49. #include "util/header.h"
  50. #include "util/cpumap.h"
  51. #include "util/thread_map.h"
  52. #include "util/counts.h"
  53. #include "util/topdown.h"
  54. #include "util/session.h"
  55. #include "util/tool.h"
  56. #include "util/string2.h"
  57. #include "util/metricgroup.h"
  58. #include "util/synthetic-events.h"
  59. #include "util/target.h"
  60. #include "util/time-utils.h"
  61. #include "util/top.h"
  62. #include "util/affinity.h"
  63. #include "util/pfm.h"
  64. #include "util/bpf_counter.h"
  65. #include "util/iostat.h"
  66. #include "util/util.h"
  67. #include "util/intel-tpebs.h"
  68. #include "asm/bug.h"
  69. #include <linux/list_sort.h>
  70. #include <linux/time64.h>
  71. #include <linux/zalloc.h>
  72. #include <api/fs/fs.h>
  73. #include <errno.h>
  74. #include <signal.h>
  75. #include <stdlib.h>
  76. #include <sys/prctl.h>
  77. #include <inttypes.h>
  78. #include <locale.h>
  79. #include <math.h>
  80. #include <sys/types.h>
  81. #include <sys/stat.h>
  82. #include <sys/wait.h>
  83. #include <unistd.h>
  84. #include <sys/time.h>
  85. #include <sys/resource.h>
  86. #include <linux/err.h>
  87. #include <linux/ctype.h>
  88. #include <perf/evlist.h>
  89. #include <internal/threadmap.h>
  90. #ifdef HAVE_BPF_SKEL
  91. #include "util/bpf_skel/bperf_cgroup.h"
  92. #endif
  93. #define DEFAULT_SEPARATOR " "
  94. #define FREEZE_ON_SMI_PATH "bus/event_source/devices/cpu/freeze_on_smi"
  95. struct rusage_stats {
  96. struct stats ru_utime_usec_stat;
  97. struct stats ru_stime_usec_stat;
  98. };
  99. static void print_counters(struct timespec *ts, int argc, const char **argv);
  100. static struct evlist *evsel_list;
  101. static struct parse_events_option_args parse_events_option_args = {
  102. .evlistp = &evsel_list,
  103. };
  104. static bool all_counters_use_bpf = true;
  105. static struct target target;
  106. static volatile sig_atomic_t child_pid = -1;
  107. static int detailed_run = 0;
  108. static bool transaction_run;
  109. static bool topdown_run = false;
  110. static bool smi_cost = false;
  111. static bool smi_reset = false;
  112. static int big_num_opt = -1;
  113. static const char *pre_cmd = NULL;
  114. static const char *post_cmd = NULL;
  115. static bool sync_run = false;
  116. static bool forever = false;
  117. static bool force_metric_only = false;
  118. static struct timespec ref_time;
  119. static bool append_file;
  120. static bool interval_count;
  121. static const char *output_name;
  122. static int output_fd;
  123. static char *metrics;
  124. static struct rusage_stats ru_stats;
  125. struct perf_stat {
  126. bool record;
  127. struct perf_data data;
  128. struct perf_session *session;
  129. u64 bytes_written;
  130. struct perf_tool tool;
  131. bool maps_allocated;
  132. struct perf_cpu_map *cpus;
  133. struct perf_thread_map *threads;
  134. enum aggr_mode aggr_mode;
  135. u32 aggr_level;
  136. };
  137. static struct perf_stat perf_stat;
  138. #define STAT_RECORD perf_stat.record
  139. static volatile sig_atomic_t done = 0;
  140. /* Options set from the command line. */
  141. struct opt_aggr_mode {
  142. bool node, socket, die, cluster, cache, core, thread, no_aggr;
  143. };
  144. /* Turn command line option into most generic aggregation mode setting. */
  145. static enum aggr_mode opt_aggr_mode_to_aggr_mode(struct opt_aggr_mode *opt_mode)
  146. {
  147. enum aggr_mode mode = AGGR_GLOBAL;
  148. if (opt_mode->node)
  149. mode = AGGR_NODE;
  150. if (opt_mode->socket)
  151. mode = AGGR_SOCKET;
  152. if (opt_mode->die)
  153. mode = AGGR_DIE;
  154. if (opt_mode->cluster)
  155. mode = AGGR_CLUSTER;
  156. if (opt_mode->cache)
  157. mode = AGGR_CACHE;
  158. if (opt_mode->core)
  159. mode = AGGR_CORE;
  160. if (opt_mode->thread)
  161. mode = AGGR_THREAD;
  162. if (opt_mode->no_aggr)
  163. mode = AGGR_NONE;
  164. return mode;
  165. }
  166. static void evlist__check_cpu_maps(struct evlist *evlist)
  167. {
  168. struct evsel *evsel, *warned_leader = NULL;
  169. evlist__for_each_entry(evlist, evsel) {
  170. struct evsel *leader = evsel__leader(evsel);
  171. /* Check that leader matches cpus with each member. */
  172. if (leader == evsel)
  173. continue;
  174. if (perf_cpu_map__equal(leader->core.cpus, evsel->core.cpus))
  175. continue;
  176. /* If there's mismatch disable the group and warn user. */
  177. if (warned_leader != leader) {
  178. char buf[200];
  179. pr_warning("WARNING: grouped events cpus do not match.\n"
  180. "Events with CPUs not matching the leader will "
  181. "be removed from the group.\n");
  182. evsel__group_desc(leader, buf, sizeof(buf));
  183. pr_warning(" %s\n", buf);
  184. warned_leader = leader;
  185. }
  186. if (verbose > 0) {
  187. char buf[200];
  188. cpu_map__snprint(leader->core.cpus, buf, sizeof(buf));
  189. pr_warning(" %s: %s\n", leader->name, buf);
  190. cpu_map__snprint(evsel->core.cpus, buf, sizeof(buf));
  191. pr_warning(" %s: %s\n", evsel->name, buf);
  192. }
  193. evsel__remove_from_group(evsel, leader);
  194. }
  195. }
  196. static inline void diff_timespec(struct timespec *r, struct timespec *a,
  197. struct timespec *b)
  198. {
  199. r->tv_sec = a->tv_sec - b->tv_sec;
  200. if (a->tv_nsec < b->tv_nsec) {
  201. r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
  202. r->tv_sec--;
  203. } else {
  204. r->tv_nsec = a->tv_nsec - b->tv_nsec ;
  205. }
  206. }
  207. static void perf_stat__reset_stats(void)
  208. {
  209. evlist__reset_stats(evsel_list);
  210. memset(stat_config.walltime_nsecs_stats, 0, sizeof(*stat_config.walltime_nsecs_stats));
  211. }
  212. static int process_synthesized_event(const struct perf_tool *tool __maybe_unused,
  213. union perf_event *event,
  214. struct perf_sample *sample __maybe_unused,
  215. struct machine *machine __maybe_unused)
  216. {
  217. if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
  218. pr_err("failed to write perf data, error: %m\n");
  219. return -1;
  220. }
  221. perf_stat.bytes_written += event->header.size;
  222. return 0;
  223. }
  224. static int write_stat_round_event(u64 tm, u64 type)
  225. {
  226. return perf_event__synthesize_stat_round(NULL, tm, type,
  227. process_synthesized_event,
  228. NULL);
  229. }
  230. #define WRITE_STAT_ROUND_EVENT(time, interval) \
  231. write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
  232. #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
  233. static int evsel__write_stat_event(struct evsel *counter, int cpu_map_idx, u32 thread,
  234. struct perf_counts_values *count)
  235. {
  236. struct perf_sample_id *sid = SID(counter, cpu_map_idx, thread);
  237. struct perf_cpu cpu = perf_cpu_map__cpu(evsel__cpus(counter), cpu_map_idx);
  238. return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
  239. process_synthesized_event, NULL);
  240. }
  241. static int read_single_counter(struct evsel *counter, int cpu_map_idx, int thread)
  242. {
  243. int err = evsel__read_counter(counter, cpu_map_idx, thread);
  244. /*
  245. * Reading user and system time will fail when the process
  246. * terminates. Use the wait4 values in that case.
  247. */
  248. if (err && cpu_map_idx == 0 &&
  249. (evsel__tool_event(counter) == TOOL_PMU__EVENT_USER_TIME ||
  250. evsel__tool_event(counter) == TOOL_PMU__EVENT_SYSTEM_TIME)) {
  251. struct perf_counts_values *count =
  252. perf_counts(counter->counts, cpu_map_idx, thread);
  253. struct perf_counts_values *old_count = NULL;
  254. u64 val;
  255. if (counter->prev_raw_counts)
  256. old_count = perf_counts(counter->prev_raw_counts, cpu_map_idx, thread);
  257. if (evsel__tool_event(counter) == TOOL_PMU__EVENT_USER_TIME)
  258. val = ru_stats.ru_utime_usec_stat.mean;
  259. else
  260. val = ru_stats.ru_stime_usec_stat.mean;
  261. count->val = val;
  262. if (old_count) {
  263. count->run = old_count->run + 1;
  264. count->ena = old_count->ena + 1;
  265. } else {
  266. count->run++;
  267. count->ena++;
  268. }
  269. return 0;
  270. }
  271. return err;
  272. }
  273. /*
  274. * Read out the results of a single counter:
  275. * do not aggregate counts across CPUs in system-wide mode
  276. */
  277. static int read_counter_cpu(struct evsel *counter, int cpu_map_idx)
  278. {
  279. int nthreads = perf_thread_map__nr(evsel_list->core.threads);
  280. int thread;
  281. if (!counter->supported)
  282. return -ENOENT;
  283. for (thread = 0; thread < nthreads; thread++) {
  284. struct perf_counts_values *count;
  285. count = perf_counts(counter->counts, cpu_map_idx, thread);
  286. /*
  287. * The leader's group read loads data into its group members
  288. * (via evsel__read_counter()) and sets their count->loaded.
  289. */
  290. if (!perf_counts__is_loaded(counter->counts, cpu_map_idx, thread) &&
  291. read_single_counter(counter, cpu_map_idx, thread)) {
  292. counter->counts->scaled = -1;
  293. perf_counts(counter->counts, cpu_map_idx, thread)->ena = 0;
  294. perf_counts(counter->counts, cpu_map_idx, thread)->run = 0;
  295. return -1;
  296. }
  297. perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, false);
  298. if (STAT_RECORD) {
  299. if (evsel__write_stat_event(counter, cpu_map_idx, thread, count)) {
  300. pr_err("failed to write stat event\n");
  301. return -1;
  302. }
  303. }
  304. if (verbose > 1) {
  305. fprintf(stat_config.output,
  306. "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  307. evsel__name(counter),
  308. perf_cpu_map__cpu(evsel__cpus(counter),
  309. cpu_map_idx).cpu,
  310. count->val, count->ena, count->run);
  311. }
  312. }
  313. return 0;
  314. }
  315. static int read_counters_with_affinity(void)
  316. {
  317. struct evlist_cpu_iterator evlist_cpu_itr;
  318. if (all_counters_use_bpf)
  319. return 0;
  320. evlist__for_each_cpu(evlist_cpu_itr, evsel_list) {
  321. struct evsel *counter = evlist_cpu_itr.evsel;
  322. if (evsel__is_bpf(counter))
  323. continue;
  324. if (evsel__is_tool(counter))
  325. continue;
  326. if (!counter->err)
  327. counter->err = read_counter_cpu(counter, evlist_cpu_itr.cpu_map_idx);
  328. }
  329. return 0;
  330. }
  331. static int read_bpf_map_counters(void)
  332. {
  333. struct evsel *counter;
  334. int err;
  335. evlist__for_each_entry(evsel_list, counter) {
  336. if (!evsel__is_bpf(counter))
  337. continue;
  338. err = bpf_counter__read(counter);
  339. if (err)
  340. return err;
  341. }
  342. return 0;
  343. }
  344. static int read_tool_counters(void)
  345. {
  346. struct evsel *counter;
  347. evlist__for_each_entry(evsel_list, counter) {
  348. int idx;
  349. if (!evsel__is_tool(counter))
  350. continue;
  351. perf_cpu_map__for_each_idx(idx, counter->core.cpus) {
  352. if (!counter->err)
  353. counter->err = read_counter_cpu(counter, idx);
  354. }
  355. }
  356. return 0;
  357. }
  358. static int read_counters(void)
  359. {
  360. int ret;
  361. if (stat_config.stop_read_counter)
  362. return 0;
  363. // Read all BPF counters first.
  364. ret = read_bpf_map_counters();
  365. if (ret)
  366. return ret;
  367. // Read non-BPF and non-tool counters next.
  368. ret = read_counters_with_affinity();
  369. if (ret)
  370. return ret;
  371. // Read the tool counters last. This way the duration_time counter
  372. // should always be greater than any other counter's enabled time.
  373. return read_tool_counters();
  374. }
  375. static void process_counters(void)
  376. {
  377. struct evsel *counter;
  378. evlist__for_each_entry(evsel_list, counter) {
  379. if (counter->err)
  380. pr_debug("failed to read counter %s\n", counter->name);
  381. if (counter->err == 0 && perf_stat_process_counter(&stat_config, counter))
  382. pr_warning("failed to process counter %s\n", counter->name);
  383. counter->err = 0;
  384. }
  385. perf_stat_merge_counters(&stat_config, evsel_list);
  386. perf_stat_process_percore(&stat_config, evsel_list);
  387. }
  388. static void process_interval(void)
  389. {
  390. struct timespec ts, rs;
  391. clock_gettime(CLOCK_MONOTONIC, &ts);
  392. diff_timespec(&rs, &ts, &ref_time);
  393. evlist__reset_aggr_stats(evsel_list);
  394. if (read_counters() == 0)
  395. process_counters();
  396. if (STAT_RECORD) {
  397. if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
  398. pr_err("failed to write stat round event\n");
  399. }
  400. init_stats(stat_config.walltime_nsecs_stats);
  401. update_stats(stat_config.walltime_nsecs_stats, stat_config.interval * 1000000ULL);
  402. print_counters(&rs, 0, NULL);
  403. }
  404. static bool handle_interval(unsigned int interval, int *times)
  405. {
  406. if (interval) {
  407. process_interval();
  408. if (interval_count && !(--(*times)))
  409. return true;
  410. }
  411. return false;
  412. }
  413. static int enable_counters(void)
  414. {
  415. struct evsel *evsel;
  416. int err;
  417. evlist__for_each_entry(evsel_list, evsel) {
  418. if (!evsel__is_bpf(evsel))
  419. continue;
  420. err = bpf_counter__enable(evsel);
  421. if (err)
  422. return err;
  423. }
  424. if (!target__enable_on_exec(&target)) {
  425. if (!all_counters_use_bpf)
  426. evlist__enable(evsel_list);
  427. }
  428. return 0;
  429. }
  430. static void disable_counters(void)
  431. {
  432. struct evsel *counter;
  433. /*
  434. * If we don't have tracee (attaching to task or cpu), counters may
  435. * still be running. To get accurate group ratios, we must stop groups
  436. * from counting before reading their constituent counters.
  437. */
  438. if (!target__none(&target)) {
  439. evlist__for_each_entry(evsel_list, counter)
  440. bpf_counter__disable(counter);
  441. if (!all_counters_use_bpf)
  442. evlist__disable(evsel_list);
  443. }
  444. }
  445. static volatile sig_atomic_t workload_exec_errno;
  446. /*
  447. * evlist__prepare_workload will send a SIGUSR1
  448. * if the fork fails, since we asked by setting its
  449. * want_signal to true.
  450. */
  451. static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
  452. void *ucontext __maybe_unused)
  453. {
  454. workload_exec_errno = info->si_value.sival_int;
  455. }
  456. static bool evsel__should_store_id(struct evsel *counter)
  457. {
  458. return STAT_RECORD || counter->core.attr.read_format & PERF_FORMAT_ID;
  459. }
  460. static bool is_target_alive(struct target *_target,
  461. struct perf_thread_map *threads)
  462. {
  463. struct stat st;
  464. int i;
  465. if (!target__has_task(_target))
  466. return true;
  467. for (i = 0; i < threads->nr; i++) {
  468. char path[PATH_MAX];
  469. scnprintf(path, PATH_MAX, "%s/%d", procfs__mountpoint(),
  470. threads->map[i].pid);
  471. if (!stat(path, &st))
  472. return true;
  473. }
  474. return false;
  475. }
  476. static void process_evlist(struct evlist *evlist, unsigned int interval)
  477. {
  478. enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
  479. if (evlist__ctlfd_process(evlist, &cmd) > 0) {
  480. switch (cmd) {
  481. case EVLIST_CTL_CMD_ENABLE:
  482. fallthrough;
  483. case EVLIST_CTL_CMD_DISABLE:
  484. if (interval)
  485. process_interval();
  486. break;
  487. case EVLIST_CTL_CMD_SNAPSHOT:
  488. case EVLIST_CTL_CMD_ACK:
  489. case EVLIST_CTL_CMD_UNSUPPORTED:
  490. case EVLIST_CTL_CMD_EVLIST:
  491. case EVLIST_CTL_CMD_STOP:
  492. case EVLIST_CTL_CMD_PING:
  493. default:
  494. break;
  495. }
  496. }
  497. }
  498. static void compute_tts(struct timespec *time_start, struct timespec *time_stop,
  499. int *time_to_sleep)
  500. {
  501. int tts = *time_to_sleep;
  502. struct timespec time_diff;
  503. diff_timespec(&time_diff, time_stop, time_start);
  504. tts -= time_diff.tv_sec * MSEC_PER_SEC +
  505. time_diff.tv_nsec / NSEC_PER_MSEC;
  506. if (tts < 0)
  507. tts = 0;
  508. *time_to_sleep = tts;
  509. }
  510. static int dispatch_events(bool forks, int timeout, int interval, int *times)
  511. {
  512. int child_exited = 0, status = 0;
  513. int time_to_sleep, sleep_time;
  514. struct timespec time_start, time_stop;
  515. if (interval)
  516. sleep_time = interval;
  517. else if (timeout)
  518. sleep_time = timeout;
  519. else
  520. sleep_time = 1000;
  521. time_to_sleep = sleep_time;
  522. while (!done) {
  523. if (forks)
  524. child_exited = waitpid(child_pid, &status, WNOHANG);
  525. else
  526. child_exited = !is_target_alive(&target, evsel_list->core.threads) ? 1 : 0;
  527. if (child_exited)
  528. break;
  529. clock_gettime(CLOCK_MONOTONIC, &time_start);
  530. if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */
  531. if (timeout || handle_interval(interval, times))
  532. break;
  533. time_to_sleep = sleep_time;
  534. } else { /* fd revent */
  535. process_evlist(evsel_list, interval);
  536. clock_gettime(CLOCK_MONOTONIC, &time_stop);
  537. compute_tts(&time_start, &time_stop, &time_to_sleep);
  538. }
  539. }
  540. return status;
  541. }
  542. enum counter_recovery {
  543. COUNTER_SKIP,
  544. COUNTER_RETRY,
  545. };
  546. static enum counter_recovery stat_handle_error(struct evsel *counter, int err)
  547. {
  548. char msg[BUFSIZ];
  549. assert(!counter->supported);
  550. /*
  551. * PPC returns ENXIO for HW counters until 2.6.37
  552. * (behavior changed with commit b0a873e).
  553. */
  554. if (err == EINVAL || err == ENOSYS || err == ENOENT || err == ENXIO) {
  555. if (verbose > 0) {
  556. evsel__open_strerror(counter, &target, err, msg, sizeof(msg));
  557. ui__warning("%s event is not supported by the kernel.\n%s\n",
  558. evsel__name(counter), msg);
  559. }
  560. return COUNTER_SKIP;
  561. }
  562. if (evsel__fallback(counter, &target, err, msg, sizeof(msg))) {
  563. if (verbose > 0)
  564. ui__warning("%s\n", msg);
  565. counter->supported = true;
  566. return COUNTER_RETRY;
  567. }
  568. if (target__has_per_thread(&target) && err != EOPNOTSUPP &&
  569. evsel_list->core.threads && evsel_list->core.threads->err_thread != -1) {
  570. /*
  571. * For global --per-thread case, skip current
  572. * error thread.
  573. */
  574. if (!thread_map__remove(evsel_list->core.threads,
  575. evsel_list->core.threads->err_thread)) {
  576. evsel_list->core.threads->err_thread = -1;
  577. counter->supported = true;
  578. return COUNTER_RETRY;
  579. }
  580. }
  581. if (verbose > 0) {
  582. evsel__open_strerror(counter, &target, err, msg, sizeof(msg));
  583. ui__warning(err == EOPNOTSUPP
  584. ? "%s event is not supported by the kernel.\n%s\n"
  585. : "skipping event %s that kernel failed to open.\n%s\n",
  586. evsel__name(counter), msg);
  587. }
  588. return COUNTER_SKIP;
  589. }
  590. static int create_perf_stat_counter(struct evsel *evsel,
  591. struct perf_stat_config *config,
  592. int cpu_map_idx)
  593. {
  594. struct perf_event_attr *attr = &evsel->core.attr;
  595. struct evsel *leader = evsel__leader(evsel);
  596. /* Reset supported flag as creating a stat counter is retried. */
  597. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  598. PERF_FORMAT_TOTAL_TIME_RUNNING;
  599. /*
  600. * The event is part of non trivial group, let's enable
  601. * the group read (for leader) and ID retrieval for all
  602. * members.
  603. */
  604. if (leader->core.nr_members > 1)
  605. attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
  606. attr->inherit = !config->no_inherit && list_empty(&evsel->bpf_counter_list);
  607. /*
  608. * Some events get initialized with sample_(period/type) set,
  609. * like tracepoints. Clear it up for counting.
  610. */
  611. attr->sample_period = 0;
  612. if (config->identifier)
  613. attr->sample_type = PERF_SAMPLE_IDENTIFIER;
  614. if (config->all_user) {
  615. attr->exclude_kernel = 1;
  616. attr->exclude_user = 0;
  617. }
  618. if (config->all_kernel) {
  619. attr->exclude_kernel = 0;
  620. attr->exclude_user = 1;
  621. }
  622. /*
  623. * Disabling all counters initially, they will be enabled
  624. * either manually by us or by kernel via enable_on_exec
  625. * set later.
  626. */
  627. if (evsel__is_group_leader(evsel)) {
  628. attr->disabled = 1;
  629. if (target__enable_on_exec(&target))
  630. attr->enable_on_exec = 1;
  631. }
  632. return evsel__open_per_cpu_and_thread(evsel, evsel__cpus(evsel), cpu_map_idx,
  633. evsel->core.threads);
  634. }
  635. static void update_rusage_stats(const struct rusage *rusage)
  636. {
  637. const u64 us_to_ns = 1000;
  638. const u64 s_to_ns = 1000000000;
  639. update_stats(&ru_stats.ru_utime_usec_stat,
  640. (rusage->ru_utime.tv_usec * us_to_ns + rusage->ru_utime.tv_sec * s_to_ns));
  641. update_stats(&ru_stats.ru_stime_usec_stat,
  642. (rusage->ru_stime.tv_usec * us_to_ns + rusage->ru_stime.tv_sec * s_to_ns));
  643. }
  644. static int __run_perf_stat(int argc, const char **argv, int run_idx)
  645. {
  646. int interval = stat_config.interval;
  647. int times = stat_config.times;
  648. int timeout = stat_config.timeout;
  649. char msg[BUFSIZ];
  650. unsigned long long t0, t1;
  651. struct evsel *counter;
  652. size_t l;
  653. int status = 0;
  654. const bool forks = (argc > 0);
  655. bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
  656. struct evlist_cpu_iterator evlist_cpu_itr;
  657. int err, open_err = 0;
  658. bool second_pass = false, has_supported_counters;
  659. if (forks) {
  660. if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe, workload_exec_failed_signal) < 0) {
  661. perror("failed to prepare workload");
  662. return -1;
  663. }
  664. child_pid = evsel_list->workload.pid;
  665. }
  666. evlist__for_each_entry(evsel_list, counter) {
  667. counter->reset_group = false;
  668. if (bpf_counter__load(counter, &target)) {
  669. err = -1;
  670. goto err_out;
  671. }
  672. if (!(evsel__is_bperf(counter)))
  673. all_counters_use_bpf = false;
  674. }
  675. evlist__reset_aggr_stats(evsel_list);
  676. /*
  677. * bperf calls evsel__open_per_cpu() in bperf__load(), so
  678. * no need to call it again here.
  679. */
  680. if (!target.use_bpf) {
  681. evlist__for_each_cpu(evlist_cpu_itr, evsel_list) {
  682. counter = evlist_cpu_itr.evsel;
  683. if (counter->reset_group || !counter->supported)
  684. continue;
  685. if (evsel__is_bperf(counter))
  686. continue;
  687. while (true) {
  688. if (create_perf_stat_counter(counter, &stat_config,
  689. evlist_cpu_itr.cpu_map_idx) == 0)
  690. break;
  691. open_err = errno;
  692. /*
  693. * Weak group failed. We cannot just undo this
  694. * here because earlier CPUs might be in group
  695. * mode, and the kernel doesn't support mixing
  696. * group and non group reads. Defer it to later.
  697. * Don't close here because we're in the wrong
  698. * affinity.
  699. */
  700. if ((open_err == EINVAL || open_err == EBADF) &&
  701. evsel__leader(counter) != counter &&
  702. counter->weak_group) {
  703. evlist__reset_weak_group(evsel_list, counter, false);
  704. assert(counter->reset_group);
  705. counter->supported = true;
  706. second_pass = true;
  707. break;
  708. }
  709. if (stat_handle_error(counter, open_err) != COUNTER_RETRY)
  710. break;
  711. }
  712. }
  713. }
  714. if (second_pass) {
  715. /*
  716. * Now redo all the weak group after closing them,
  717. * and also close errored counters.
  718. */
  719. /* First close errored or weak retry */
  720. evlist__for_each_cpu(evlist_cpu_itr, evsel_list) {
  721. counter = evlist_cpu_itr.evsel;
  722. if (!counter->reset_group && counter->supported)
  723. continue;
  724. perf_evsel__close_cpu(&counter->core, evlist_cpu_itr.cpu_map_idx);
  725. }
  726. /* Now reopen weak */
  727. evlist__for_each_cpu(evlist_cpu_itr, evsel_list) {
  728. counter = evlist_cpu_itr.evsel;
  729. if (!counter->reset_group)
  730. continue;
  731. while (true) {
  732. pr_debug2("reopening weak %s\n", evsel__name(counter));
  733. if (create_perf_stat_counter(counter, &stat_config,
  734. evlist_cpu_itr.cpu_map_idx) == 0) {
  735. evlist_cpu_iterator__exit(&evlist_cpu_itr);
  736. break;
  737. }
  738. open_err = errno;
  739. if (stat_handle_error(counter, open_err) != COUNTER_RETRY) {
  740. evlist_cpu_iterator__exit(&evlist_cpu_itr);
  741. break;
  742. }
  743. }
  744. }
  745. }
  746. has_supported_counters = false;
  747. evlist__for_each_entry(evsel_list, counter) {
  748. if (!counter->supported) {
  749. perf_evsel__free_fd(&counter->core);
  750. continue;
  751. }
  752. has_supported_counters = true;
  753. l = strlen(counter->unit);
  754. if (l > stat_config.unit_width)
  755. stat_config.unit_width = l;
  756. if (evsel__should_store_id(counter) &&
  757. evsel__store_ids(counter, evsel_list)) {
  758. err = -1;
  759. goto err_out;
  760. }
  761. }
  762. if (!has_supported_counters && !stat_config.null_run) {
  763. if (open_err) {
  764. evsel__open_strerror(evlist__first(evsel_list), &target, open_err,
  765. msg, sizeof(msg));
  766. }
  767. ui__error("No supported events found.\n%s\n", msg);
  768. if (child_pid != -1)
  769. kill(child_pid, SIGTERM);
  770. err = -1;
  771. goto err_out;
  772. }
  773. if (evlist__apply_filters(evsel_list, &counter, &target)) {
  774. pr_err("failed to set filter \"%s\" on event %s: %m\n",
  775. counter->filter, evsel__name(counter));
  776. return -1;
  777. }
  778. if (STAT_RECORD) {
  779. int fd = perf_data__fd(&perf_stat.data);
  780. if (is_pipe) {
  781. err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
  782. } else {
  783. err = perf_session__write_header(perf_stat.session, evsel_list,
  784. fd, false);
  785. }
  786. if (err < 0)
  787. goto err_out;
  788. err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list,
  789. process_synthesized_event, is_pipe);
  790. if (err < 0)
  791. goto err_out;
  792. }
  793. if (target.initial_delay) {
  794. pr_info(EVLIST_DISABLED_MSG);
  795. } else {
  796. err = enable_counters();
  797. if (err) {
  798. err = -1;
  799. goto err_out;
  800. }
  801. }
  802. /* Exec the command, if any */
  803. if (forks)
  804. evlist__start_workload(evsel_list);
  805. if (target.initial_delay > 0) {
  806. usleep(target.initial_delay * USEC_PER_MSEC);
  807. err = enable_counters();
  808. if (err) {
  809. err = -1;
  810. goto err_out;
  811. }
  812. pr_info(EVLIST_ENABLED_MSG);
  813. }
  814. t0 = rdclock();
  815. clock_gettime(CLOCK_MONOTONIC, &ref_time);
  816. if (forks) {
  817. if (interval || timeout || evlist__ctlfd_initialized(evsel_list))
  818. status = dispatch_events(forks, timeout, interval, &times);
  819. if (child_pid != -1) {
  820. if (timeout)
  821. kill(child_pid, SIGTERM);
  822. wait4(child_pid, &status, 0, &stat_config.ru_data);
  823. }
  824. if (workload_exec_errno) {
  825. errno = workload_exec_errno;
  826. pr_err("Workload failed: %m\n");
  827. err = -1;
  828. goto err_out;
  829. }
  830. if (WIFSIGNALED(status)) {
  831. /*
  832. * We want to indicate failure to stop a repeat run,
  833. * hence negative. We want the value to be the exit code
  834. * of perf, which for termination by a signal is 128
  835. * plus the signal number.
  836. */
  837. err = 0 - (128 + WTERMSIG(status));
  838. psignal(WTERMSIG(status), argv[0]);
  839. } else {
  840. err = WEXITSTATUS(status);
  841. }
  842. } else {
  843. err = dispatch_events(forks, timeout, interval, &times);
  844. }
  845. disable_counters();
  846. t1 = rdclock();
  847. if (stat_config.walltime_run_table)
  848. stat_config.walltime_run[run_idx] = t1 - t0;
  849. if (interval && stat_config.summary) {
  850. stat_config.interval = 0;
  851. stat_config.stop_read_counter = true;
  852. init_stats(stat_config.walltime_nsecs_stats);
  853. update_stats(stat_config.walltime_nsecs_stats, t1 - t0);
  854. evlist__copy_prev_raw_counts(evsel_list);
  855. evlist__reset_prev_raw_counts(evsel_list);
  856. evlist__reset_aggr_stats(evsel_list);
  857. } else {
  858. update_stats(stat_config.walltime_nsecs_stats, t1 - t0);
  859. update_rusage_stats(&stat_config.ru_data);
  860. }
  861. /*
  862. * Closing a group leader splits the group, and as we only disable
  863. * group leaders, results in remaining events becoming enabled. To
  864. * avoid arbitrary skew, we must read all counters before closing any
  865. * group leaders.
  866. */
  867. if (read_counters() == 0)
  868. process_counters();
  869. /*
  870. * We need to keep evsel_list alive, because it's processed
  871. * later the evsel_list will be closed after.
  872. */
  873. if (!STAT_RECORD)
  874. evlist__close(evsel_list);
  875. return err;
  876. err_out:
  877. if (forks)
  878. evlist__cancel_workload(evsel_list);
  879. return err;
  880. }
  881. /*
  882. * Returns -1 for fatal errors which signifies to not continue
  883. * when in repeat mode.
  884. *
  885. * Returns < -1 error codes when stat record is used. These
  886. * result in the stat information being displayed, but writing
  887. * to the file fails and is non fatal.
  888. */
  889. static int run_perf_stat(int argc, const char **argv, int run_idx)
  890. {
  891. int ret;
  892. if (pre_cmd) {
  893. ret = system(pre_cmd);
  894. if (ret)
  895. return ret;
  896. }
  897. if (sync_run)
  898. sync();
  899. ret = __run_perf_stat(argc, argv, run_idx);
  900. if (ret)
  901. return ret;
  902. if (post_cmd) {
  903. ret = system(post_cmd);
  904. if (ret)
  905. return ret;
  906. }
  907. return ret;
  908. }
  909. static void print_counters(struct timespec *ts, int argc, const char **argv)
  910. {
  911. /* Do not print anything if we record to the pipe. */
  912. if (STAT_RECORD && perf_stat.data.is_pipe)
  913. return;
  914. if (quiet)
  915. return;
  916. evlist__print_counters(evsel_list, &stat_config, &target, ts, argc, argv);
  917. }
  918. static volatile sig_atomic_t signr = -1;
  919. static void skip_signal(int signo)
  920. {
  921. if ((child_pid == -1) || stat_config.interval)
  922. done = 1;
  923. signr = signo;
  924. /*
  925. * render child_pid harmless
  926. * won't send SIGTERM to a random
  927. * process in case of race condition
  928. * and fast PID recycling
  929. */
  930. child_pid = -1;
  931. }
  932. static void sig_atexit(void)
  933. {
  934. sigset_t set, oset;
  935. /*
  936. * avoid race condition with SIGCHLD handler
  937. * in skip_signal() which is modifying child_pid
  938. * goal is to avoid send SIGTERM to a random
  939. * process
  940. */
  941. sigemptyset(&set);
  942. sigaddset(&set, SIGCHLD);
  943. sigprocmask(SIG_BLOCK, &set, &oset);
  944. if (child_pid != -1)
  945. kill(child_pid, SIGTERM);
  946. sigprocmask(SIG_SETMASK, &oset, NULL);
  947. if (signr == -1)
  948. return;
  949. signal(signr, SIG_DFL);
  950. kill(getpid(), signr);
  951. }
  952. static int stat__set_big_num(const struct option *opt __maybe_unused,
  953. const char *s __maybe_unused, int unset)
  954. {
  955. big_num_opt = unset ? 0 : 1;
  956. perf_stat__set_big_num(!unset);
  957. return 0;
  958. }
  959. static int enable_metric_only(const struct option *opt __maybe_unused,
  960. const char *s __maybe_unused, int unset)
  961. {
  962. force_metric_only = true;
  963. stat_config.metric_only = !unset;
  964. return 0;
  965. }
  966. static int append_metric_groups(const struct option *opt __maybe_unused,
  967. const char *str,
  968. int unset __maybe_unused)
  969. {
  970. if (metrics) {
  971. char *tmp;
  972. if (asprintf(&tmp, "%s,%s", metrics, str) < 0)
  973. return -ENOMEM;
  974. free(metrics);
  975. metrics = tmp;
  976. } else {
  977. metrics = strdup(str);
  978. if (!metrics)
  979. return -ENOMEM;
  980. }
  981. return 0;
  982. }
  983. static int parse_control_option(const struct option *opt,
  984. const char *str,
  985. int unset __maybe_unused)
  986. {
  987. struct perf_stat_config *config = opt->value;
  988. return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close);
  989. }
  990. static int parse_stat_cgroups(const struct option *opt,
  991. const char *str, int unset)
  992. {
  993. if (stat_config.cgroup_list) {
  994. pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
  995. return -1;
  996. }
  997. return parse_cgroups(opt, str, unset);
  998. }
  999. static int parse_cputype(const struct option *opt,
  1000. const char *str,
  1001. int unset __maybe_unused)
  1002. {
  1003. const struct perf_pmu *pmu;
  1004. struct evlist *evlist = *(struct evlist **)opt->value;
  1005. if (!list_empty(&evlist->core.entries)) {
  1006. fprintf(stderr, "Must define cputype before events/metrics\n");
  1007. return -1;
  1008. }
  1009. pmu = perf_pmus__pmu_for_pmu_filter(str);
  1010. if (!pmu) {
  1011. fprintf(stderr, "--cputype %s is not supported!\n", str);
  1012. return -1;
  1013. }
  1014. parse_events_option_args.pmu_filter = pmu->name;
  1015. return 0;
  1016. }
  1017. static int parse_cache_level(const struct option *opt,
  1018. const char *str,
  1019. int unset __maybe_unused)
  1020. {
  1021. int level;
  1022. struct opt_aggr_mode *opt_aggr_mode = (struct opt_aggr_mode *)opt->value;
  1023. u32 *aggr_level = (u32 *)opt->data;
  1024. /*
  1025. * If no string is specified, aggregate based on the topology of
  1026. * Last Level Cache (LLC). Since the LLC level can change from
  1027. * architecture to architecture, set level greater than
  1028. * MAX_CACHE_LVL which will be interpreted as LLC.
  1029. */
  1030. if (str == NULL) {
  1031. level = MAX_CACHE_LVL + 1;
  1032. goto out;
  1033. }
  1034. /*
  1035. * The format to specify cache level is LX or lX where X is the
  1036. * cache level.
  1037. */
  1038. if (strlen(str) != 2 || (str[0] != 'l' && str[0] != 'L')) {
  1039. pr_err("Cache level must be of form L[1-%d], or l[1-%d]\n",
  1040. MAX_CACHE_LVL,
  1041. MAX_CACHE_LVL);
  1042. return -EINVAL;
  1043. }
  1044. level = atoi(&str[1]);
  1045. if (level < 1) {
  1046. pr_err("Cache level must be of form L[1-%d], or l[1-%d]\n",
  1047. MAX_CACHE_LVL,
  1048. MAX_CACHE_LVL);
  1049. return -EINVAL;
  1050. }
  1051. if (level > MAX_CACHE_LVL) {
  1052. pr_err("perf only supports max cache level of %d.\n"
  1053. "Consider increasing MAX_CACHE_LVL\n", MAX_CACHE_LVL);
  1054. return -EINVAL;
  1055. }
  1056. out:
  1057. opt_aggr_mode->cache = true;
  1058. *aggr_level = level;
  1059. return 0;
  1060. }
  1061. /**
  1062. * Calculate the cache instance ID from the map in
  1063. * /sys/devices/system/cpu/cpuX/cache/indexY/shared_cpu_list
  1064. * Cache instance ID is the first CPU reported in the shared_cpu_list file.
  1065. */
  1066. static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
  1067. {
  1068. int id;
  1069. struct perf_cpu_map *cpu_map = perf_cpu_map__new(map);
  1070. /*
  1071. * If the map contains no CPU, consider the current CPU to
  1072. * be the first online CPU in the cache domain else use the
  1073. * first online CPU of the cache domain as the ID.
  1074. */
  1075. id = perf_cpu_map__min(cpu_map).cpu;
  1076. if (id == -1)
  1077. id = cpu.cpu;
  1078. /* Free the perf_cpu_map used to find the cache ID */
  1079. perf_cpu_map__put(cpu_map);
  1080. return id;
  1081. }
  1082. /**
  1083. * cpu__get_cache_id - Returns 0 if successful in populating the
  1084. * cache level and cache id. Cache level is read from
  1085. * /sys/devices/system/cpu/cpuX/cache/indexY/level where as cache instance ID
  1086. * is the first CPU reported by
  1087. * /sys/devices/system/cpu/cpuX/cache/indexY/shared_cpu_list
  1088. */
  1089. static int cpu__get_cache_details(struct perf_cpu cpu, struct perf_cache *cache)
  1090. {
  1091. int ret = 0;
  1092. u32 cache_level = stat_config.aggr_level;
  1093. struct cpu_cache_level caches[MAX_CACHE_LVL];
  1094. u32 i = 0, caches_cnt = 0;
  1095. cache->cache_lvl = (cache_level > MAX_CACHE_LVL) ? 0 : cache_level;
  1096. cache->cache = -1;
  1097. ret = build_caches_for_cpu(cpu.cpu, caches, &caches_cnt);
  1098. if (ret) {
  1099. /*
  1100. * If caches_cnt is not 0, cpu_cache_level data
  1101. * was allocated when building the topology.
  1102. * Free the allocated data before returning.
  1103. */
  1104. if (caches_cnt)
  1105. goto free_caches;
  1106. return ret;
  1107. }
  1108. if (!caches_cnt)
  1109. return -1;
  1110. /*
  1111. * Save the data for the highest level if no
  1112. * level was specified by the user.
  1113. */
  1114. if (cache_level > MAX_CACHE_LVL) {
  1115. int max_level_index = 0;
  1116. for (i = 1; i < caches_cnt; ++i) {
  1117. if (caches[i].level > caches[max_level_index].level)
  1118. max_level_index = i;
  1119. }
  1120. cache->cache_lvl = caches[max_level_index].level;
  1121. cache->cache = cpu__get_cache_id_from_map(cpu, caches[max_level_index].map);
  1122. /* Reset i to 0 to free entire caches[] */
  1123. i = 0;
  1124. goto free_caches;
  1125. }
  1126. for (i = 0; i < caches_cnt; ++i) {
  1127. if (caches[i].level == cache_level) {
  1128. cache->cache_lvl = cache_level;
  1129. cache->cache = cpu__get_cache_id_from_map(cpu, caches[i].map);
  1130. }
  1131. cpu_cache_level__free(&caches[i]);
  1132. }
  1133. free_caches:
  1134. /*
  1135. * Free all the allocated cpu_cache_level data.
  1136. */
  1137. while (i < caches_cnt)
  1138. cpu_cache_level__free(&caches[i++]);
  1139. return ret;
  1140. }
  1141. /**
  1142. * aggr_cpu_id__cache - Create an aggr_cpu_id with cache instache ID, cache
  1143. * level, die and socket populated with the cache instache ID, cache level,
  1144. * die and socket for cpu. The function signature is compatible with
  1145. * aggr_cpu_id_get_t.
  1146. */
  1147. static struct aggr_cpu_id aggr_cpu_id__cache(struct perf_cpu cpu, void *data)
  1148. {
  1149. int ret;
  1150. struct aggr_cpu_id id;
  1151. struct perf_cache cache;
  1152. id = aggr_cpu_id__die(cpu, data);
  1153. if (aggr_cpu_id__is_empty(&id))
  1154. return id;
  1155. ret = cpu__get_cache_details(cpu, &cache);
  1156. if (ret)
  1157. return id;
  1158. id.cache_lvl = cache.cache_lvl;
  1159. id.cache = cache.cache;
  1160. return id;
  1161. }
  1162. static const char *const aggr_mode__string[] = {
  1163. [AGGR_CORE] = "core",
  1164. [AGGR_CACHE] = "cache",
  1165. [AGGR_CLUSTER] = "cluster",
  1166. [AGGR_DIE] = "die",
  1167. [AGGR_GLOBAL] = "global",
  1168. [AGGR_NODE] = "node",
  1169. [AGGR_NONE] = "none",
  1170. [AGGR_SOCKET] = "socket",
  1171. [AGGR_THREAD] = "thread",
  1172. [AGGR_UNSET] = "unset",
  1173. };
  1174. static struct aggr_cpu_id perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
  1175. struct perf_cpu cpu)
  1176. {
  1177. return aggr_cpu_id__socket(cpu, /*data=*/NULL);
  1178. }
  1179. static struct aggr_cpu_id perf_stat__get_die(struct perf_stat_config *config __maybe_unused,
  1180. struct perf_cpu cpu)
  1181. {
  1182. return aggr_cpu_id__die(cpu, /*data=*/NULL);
  1183. }
  1184. static struct aggr_cpu_id perf_stat__get_cache_id(struct perf_stat_config *config __maybe_unused,
  1185. struct perf_cpu cpu)
  1186. {
  1187. return aggr_cpu_id__cache(cpu, /*data=*/NULL);
  1188. }
  1189. static struct aggr_cpu_id perf_stat__get_cluster(struct perf_stat_config *config __maybe_unused,
  1190. struct perf_cpu cpu)
  1191. {
  1192. return aggr_cpu_id__cluster(cpu, /*data=*/NULL);
  1193. }
  1194. static struct aggr_cpu_id perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
  1195. struct perf_cpu cpu)
  1196. {
  1197. return aggr_cpu_id__core(cpu, /*data=*/NULL);
  1198. }
  1199. static struct aggr_cpu_id perf_stat__get_node(struct perf_stat_config *config __maybe_unused,
  1200. struct perf_cpu cpu)
  1201. {
  1202. return aggr_cpu_id__node(cpu, /*data=*/NULL);
  1203. }
  1204. static struct aggr_cpu_id perf_stat__get_global(struct perf_stat_config *config __maybe_unused,
  1205. struct perf_cpu cpu)
  1206. {
  1207. return aggr_cpu_id__global(cpu, /*data=*/NULL);
  1208. }
  1209. static struct aggr_cpu_id perf_stat__get_cpu(struct perf_stat_config *config __maybe_unused,
  1210. struct perf_cpu cpu)
  1211. {
  1212. return aggr_cpu_id__cpu(cpu, /*data=*/NULL);
  1213. }
  1214. static struct aggr_cpu_id perf_stat__get_aggr(struct perf_stat_config *config,
  1215. aggr_get_id_t get_id, struct perf_cpu cpu)
  1216. {
  1217. struct aggr_cpu_id id;
  1218. /* per-process mode - should use global aggr mode */
  1219. if (cpu.cpu == -1 || cpu.cpu >= config->cpus_aggr_map->nr)
  1220. return get_id(config, cpu);
  1221. if (aggr_cpu_id__is_empty(&config->cpus_aggr_map->map[cpu.cpu]))
  1222. config->cpus_aggr_map->map[cpu.cpu] = get_id(config, cpu);
  1223. id = config->cpus_aggr_map->map[cpu.cpu];
  1224. return id;
  1225. }
  1226. static struct aggr_cpu_id perf_stat__get_socket_cached(struct perf_stat_config *config,
  1227. struct perf_cpu cpu)
  1228. {
  1229. return perf_stat__get_aggr(config, perf_stat__get_socket, cpu);
  1230. }
  1231. static struct aggr_cpu_id perf_stat__get_die_cached(struct perf_stat_config *config,
  1232. struct perf_cpu cpu)
  1233. {
  1234. return perf_stat__get_aggr(config, perf_stat__get_die, cpu);
  1235. }
  1236. static struct aggr_cpu_id perf_stat__get_cluster_cached(struct perf_stat_config *config,
  1237. struct perf_cpu cpu)
  1238. {
  1239. return perf_stat__get_aggr(config, perf_stat__get_cluster, cpu);
  1240. }
  1241. static struct aggr_cpu_id perf_stat__get_cache_id_cached(struct perf_stat_config *config,
  1242. struct perf_cpu cpu)
  1243. {
  1244. return perf_stat__get_aggr(config, perf_stat__get_cache_id, cpu);
  1245. }
  1246. static struct aggr_cpu_id perf_stat__get_core_cached(struct perf_stat_config *config,
  1247. struct perf_cpu cpu)
  1248. {
  1249. return perf_stat__get_aggr(config, perf_stat__get_core, cpu);
  1250. }
  1251. static struct aggr_cpu_id perf_stat__get_node_cached(struct perf_stat_config *config,
  1252. struct perf_cpu cpu)
  1253. {
  1254. return perf_stat__get_aggr(config, perf_stat__get_node, cpu);
  1255. }
  1256. static struct aggr_cpu_id perf_stat__get_global_cached(struct perf_stat_config *config,
  1257. struct perf_cpu cpu)
  1258. {
  1259. return perf_stat__get_aggr(config, perf_stat__get_global, cpu);
  1260. }
  1261. static struct aggr_cpu_id perf_stat__get_cpu_cached(struct perf_stat_config *config,
  1262. struct perf_cpu cpu)
  1263. {
  1264. return perf_stat__get_aggr(config, perf_stat__get_cpu, cpu);
  1265. }
  1266. static aggr_cpu_id_get_t aggr_mode__get_aggr(enum aggr_mode aggr_mode)
  1267. {
  1268. switch (aggr_mode) {
  1269. case AGGR_SOCKET:
  1270. return aggr_cpu_id__socket;
  1271. case AGGR_DIE:
  1272. return aggr_cpu_id__die;
  1273. case AGGR_CLUSTER:
  1274. return aggr_cpu_id__cluster;
  1275. case AGGR_CACHE:
  1276. return aggr_cpu_id__cache;
  1277. case AGGR_CORE:
  1278. return aggr_cpu_id__core;
  1279. case AGGR_NODE:
  1280. return aggr_cpu_id__node;
  1281. case AGGR_NONE:
  1282. return aggr_cpu_id__cpu;
  1283. case AGGR_GLOBAL:
  1284. return aggr_cpu_id__global;
  1285. case AGGR_THREAD:
  1286. case AGGR_UNSET:
  1287. case AGGR_MAX:
  1288. default:
  1289. return NULL;
  1290. }
  1291. }
  1292. static aggr_get_id_t aggr_mode__get_id(enum aggr_mode aggr_mode)
  1293. {
  1294. switch (aggr_mode) {
  1295. case AGGR_SOCKET:
  1296. return perf_stat__get_socket_cached;
  1297. case AGGR_DIE:
  1298. return perf_stat__get_die_cached;
  1299. case AGGR_CLUSTER:
  1300. return perf_stat__get_cluster_cached;
  1301. case AGGR_CACHE:
  1302. return perf_stat__get_cache_id_cached;
  1303. case AGGR_CORE:
  1304. return perf_stat__get_core_cached;
  1305. case AGGR_NODE:
  1306. return perf_stat__get_node_cached;
  1307. case AGGR_NONE:
  1308. return perf_stat__get_cpu_cached;
  1309. case AGGR_GLOBAL:
  1310. return perf_stat__get_global_cached;
  1311. case AGGR_THREAD:
  1312. case AGGR_UNSET:
  1313. case AGGR_MAX:
  1314. default:
  1315. return NULL;
  1316. }
  1317. }
  1318. static int perf_stat_init_aggr_mode(void)
  1319. {
  1320. int nr;
  1321. aggr_cpu_id_get_t get_id = aggr_mode__get_aggr(stat_config.aggr_mode);
  1322. if (get_id) {
  1323. bool needs_sort = stat_config.aggr_mode != AGGR_NONE;
  1324. stat_config.aggr_map = cpu_aggr_map__new(evsel_list->core.user_requested_cpus,
  1325. get_id, /*data=*/NULL, needs_sort);
  1326. if (!stat_config.aggr_map) {
  1327. pr_err("cannot build %s map\n", aggr_mode__string[stat_config.aggr_mode]);
  1328. return -1;
  1329. }
  1330. stat_config.aggr_get_id = aggr_mode__get_id(stat_config.aggr_mode);
  1331. }
  1332. if (stat_config.aggr_mode == AGGR_THREAD) {
  1333. nr = perf_thread_map__nr(evsel_list->core.threads);
  1334. stat_config.aggr_map = cpu_aggr_map__empty_new(nr);
  1335. if (stat_config.aggr_map == NULL)
  1336. return -ENOMEM;
  1337. for (int s = 0; s < nr; s++) {
  1338. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1339. id.thread_idx = s;
  1340. stat_config.aggr_map->map[s] = id;
  1341. }
  1342. return 0;
  1343. }
  1344. /*
  1345. * The evsel_list->cpus is the base we operate on,
  1346. * taking the highest cpu number to be the size of
  1347. * the aggregation translate cpumap.
  1348. */
  1349. nr = perf_cpu_map__max(evsel_list->core.all_cpus).cpu + 1;
  1350. stat_config.cpus_aggr_map = cpu_aggr_map__empty_new(nr);
  1351. return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
  1352. }
  1353. static void cpu_aggr_map__delete(struct cpu_aggr_map *map)
  1354. {
  1355. free(map);
  1356. }
  1357. static void perf_stat__exit_aggr_mode(void)
  1358. {
  1359. cpu_aggr_map__delete(stat_config.aggr_map);
  1360. cpu_aggr_map__delete(stat_config.cpus_aggr_map);
  1361. stat_config.aggr_map = NULL;
  1362. stat_config.cpus_aggr_map = NULL;
  1363. }
  1364. static struct aggr_cpu_id perf_env__get_socket_aggr_by_cpu(struct perf_cpu cpu, void *data)
  1365. {
  1366. struct perf_env *env = data;
  1367. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1368. if (cpu.cpu != -1)
  1369. id.socket = env->cpu[cpu.cpu].socket_id;
  1370. return id;
  1371. }
  1372. static struct aggr_cpu_id perf_env__get_die_aggr_by_cpu(struct perf_cpu cpu, void *data)
  1373. {
  1374. struct perf_env *env = data;
  1375. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1376. if (cpu.cpu != -1) {
  1377. /*
  1378. * die_id is relative to socket, so start
  1379. * with the socket ID and then add die to
  1380. * make a unique ID.
  1381. */
  1382. id.socket = env->cpu[cpu.cpu].socket_id;
  1383. id.die = env->cpu[cpu.cpu].die_id;
  1384. }
  1385. return id;
  1386. }
  1387. static void perf_env__get_cache_id_for_cpu(struct perf_cpu cpu, struct perf_env *env,
  1388. u32 cache_level, struct aggr_cpu_id *id)
  1389. {
  1390. int i;
  1391. int caches_cnt = env->caches_cnt;
  1392. struct cpu_cache_level *caches = env->caches;
  1393. id->cache_lvl = (cache_level > MAX_CACHE_LVL) ? 0 : cache_level;
  1394. id->cache = -1;
  1395. if (!caches_cnt)
  1396. return;
  1397. for (i = caches_cnt - 1; i > -1; --i) {
  1398. struct perf_cpu_map *cpu_map;
  1399. int map_contains_cpu;
  1400. /*
  1401. * If user has not specified a level, find the fist level with
  1402. * the cpu in the map. Since building the map is expensive, do
  1403. * this only if levels match.
  1404. */
  1405. if (cache_level <= MAX_CACHE_LVL && caches[i].level != cache_level)
  1406. continue;
  1407. cpu_map = perf_cpu_map__new(caches[i].map);
  1408. map_contains_cpu = perf_cpu_map__idx(cpu_map, cpu);
  1409. perf_cpu_map__put(cpu_map);
  1410. if (map_contains_cpu != -1) {
  1411. id->cache_lvl = caches[i].level;
  1412. id->cache = cpu__get_cache_id_from_map(cpu, caches[i].map);
  1413. return;
  1414. }
  1415. }
  1416. }
  1417. static struct aggr_cpu_id perf_env__get_cache_aggr_by_cpu(struct perf_cpu cpu,
  1418. void *data)
  1419. {
  1420. struct perf_env *env = data;
  1421. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1422. if (cpu.cpu != -1) {
  1423. u32 cache_level = (perf_stat.aggr_level) ?: stat_config.aggr_level;
  1424. id.socket = env->cpu[cpu.cpu].socket_id;
  1425. id.die = env->cpu[cpu.cpu].die_id;
  1426. perf_env__get_cache_id_for_cpu(cpu, env, cache_level, &id);
  1427. }
  1428. return id;
  1429. }
  1430. static struct aggr_cpu_id perf_env__get_cluster_aggr_by_cpu(struct perf_cpu cpu,
  1431. void *data)
  1432. {
  1433. struct perf_env *env = data;
  1434. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1435. if (cpu.cpu != -1) {
  1436. id.socket = env->cpu[cpu.cpu].socket_id;
  1437. id.die = env->cpu[cpu.cpu].die_id;
  1438. id.cluster = env->cpu[cpu.cpu].cluster_id;
  1439. }
  1440. return id;
  1441. }
  1442. static struct aggr_cpu_id perf_env__get_core_aggr_by_cpu(struct perf_cpu cpu, void *data)
  1443. {
  1444. struct perf_env *env = data;
  1445. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1446. if (cpu.cpu != -1) {
  1447. /*
  1448. * core_id is relative to socket, die and cluster, we need a
  1449. * global id. So we set socket, die id, cluster id and core id.
  1450. */
  1451. id.socket = env->cpu[cpu.cpu].socket_id;
  1452. id.die = env->cpu[cpu.cpu].die_id;
  1453. id.cluster = env->cpu[cpu.cpu].cluster_id;
  1454. id.core = env->cpu[cpu.cpu].core_id;
  1455. }
  1456. return id;
  1457. }
  1458. static struct aggr_cpu_id perf_env__get_cpu_aggr_by_cpu(struct perf_cpu cpu, void *data)
  1459. {
  1460. struct perf_env *env = data;
  1461. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1462. if (cpu.cpu != -1) {
  1463. /*
  1464. * core_id is relative to socket and die,
  1465. * we need a global id. So we set
  1466. * socket, die id and core id
  1467. */
  1468. id.socket = env->cpu[cpu.cpu].socket_id;
  1469. id.die = env->cpu[cpu.cpu].die_id;
  1470. id.core = env->cpu[cpu.cpu].core_id;
  1471. id.cpu = cpu;
  1472. }
  1473. return id;
  1474. }
  1475. static struct aggr_cpu_id perf_env__get_node_aggr_by_cpu(struct perf_cpu cpu, void *data)
  1476. {
  1477. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1478. id.node = perf_env__numa_node(data, cpu);
  1479. return id;
  1480. }
  1481. static struct aggr_cpu_id perf_env__get_global_aggr_by_cpu(struct perf_cpu cpu __maybe_unused,
  1482. void *data __maybe_unused)
  1483. {
  1484. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1485. /* it always aggregates to the cpu 0 */
  1486. id.cpu = (struct perf_cpu){ .cpu = 0 };
  1487. return id;
  1488. }
  1489. static struct aggr_cpu_id perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
  1490. struct perf_cpu cpu)
  1491. {
  1492. return perf_env__get_socket_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1493. }
  1494. static struct aggr_cpu_id perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused,
  1495. struct perf_cpu cpu)
  1496. {
  1497. return perf_env__get_die_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1498. }
  1499. static struct aggr_cpu_id perf_stat__get_cluster_file(struct perf_stat_config *config __maybe_unused,
  1500. struct perf_cpu cpu)
  1501. {
  1502. return perf_env__get_cluster_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1503. }
  1504. static struct aggr_cpu_id perf_stat__get_cache_file(struct perf_stat_config *config __maybe_unused,
  1505. struct perf_cpu cpu)
  1506. {
  1507. return perf_env__get_cache_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1508. }
  1509. static struct aggr_cpu_id perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
  1510. struct perf_cpu cpu)
  1511. {
  1512. return perf_env__get_core_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1513. }
  1514. static struct aggr_cpu_id perf_stat__get_cpu_file(struct perf_stat_config *config __maybe_unused,
  1515. struct perf_cpu cpu)
  1516. {
  1517. return perf_env__get_cpu_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1518. }
  1519. static struct aggr_cpu_id perf_stat__get_node_file(struct perf_stat_config *config __maybe_unused,
  1520. struct perf_cpu cpu)
  1521. {
  1522. return perf_env__get_node_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1523. }
  1524. static struct aggr_cpu_id perf_stat__get_global_file(struct perf_stat_config *config __maybe_unused,
  1525. struct perf_cpu cpu)
  1526. {
  1527. return perf_env__get_global_aggr_by_cpu(cpu, perf_session__env(perf_stat.session));
  1528. }
  1529. static aggr_cpu_id_get_t aggr_mode__get_aggr_file(enum aggr_mode aggr_mode)
  1530. {
  1531. switch (aggr_mode) {
  1532. case AGGR_SOCKET:
  1533. return perf_env__get_socket_aggr_by_cpu;
  1534. case AGGR_DIE:
  1535. return perf_env__get_die_aggr_by_cpu;
  1536. case AGGR_CLUSTER:
  1537. return perf_env__get_cluster_aggr_by_cpu;
  1538. case AGGR_CACHE:
  1539. return perf_env__get_cache_aggr_by_cpu;
  1540. case AGGR_CORE:
  1541. return perf_env__get_core_aggr_by_cpu;
  1542. case AGGR_NODE:
  1543. return perf_env__get_node_aggr_by_cpu;
  1544. case AGGR_GLOBAL:
  1545. return perf_env__get_global_aggr_by_cpu;
  1546. case AGGR_NONE:
  1547. return perf_env__get_cpu_aggr_by_cpu;
  1548. case AGGR_THREAD:
  1549. case AGGR_UNSET:
  1550. case AGGR_MAX:
  1551. default:
  1552. return NULL;
  1553. }
  1554. }
  1555. static aggr_get_id_t aggr_mode__get_id_file(enum aggr_mode aggr_mode)
  1556. {
  1557. switch (aggr_mode) {
  1558. case AGGR_SOCKET:
  1559. return perf_stat__get_socket_file;
  1560. case AGGR_DIE:
  1561. return perf_stat__get_die_file;
  1562. case AGGR_CLUSTER:
  1563. return perf_stat__get_cluster_file;
  1564. case AGGR_CACHE:
  1565. return perf_stat__get_cache_file;
  1566. case AGGR_CORE:
  1567. return perf_stat__get_core_file;
  1568. case AGGR_NODE:
  1569. return perf_stat__get_node_file;
  1570. case AGGR_GLOBAL:
  1571. return perf_stat__get_global_file;
  1572. case AGGR_NONE:
  1573. return perf_stat__get_cpu_file;
  1574. case AGGR_THREAD:
  1575. case AGGR_UNSET:
  1576. case AGGR_MAX:
  1577. default:
  1578. return NULL;
  1579. }
  1580. }
  1581. static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
  1582. {
  1583. struct perf_env *env = perf_session__env(st->session);
  1584. aggr_cpu_id_get_t get_id = aggr_mode__get_aggr_file(stat_config.aggr_mode);
  1585. bool needs_sort = stat_config.aggr_mode != AGGR_NONE;
  1586. if (stat_config.aggr_mode == AGGR_THREAD) {
  1587. int nr = perf_thread_map__nr(evsel_list->core.threads);
  1588. stat_config.aggr_map = cpu_aggr_map__empty_new(nr);
  1589. if (stat_config.aggr_map == NULL)
  1590. return -ENOMEM;
  1591. for (int s = 0; s < nr; s++) {
  1592. struct aggr_cpu_id id = aggr_cpu_id__empty();
  1593. id.thread_idx = s;
  1594. stat_config.aggr_map->map[s] = id;
  1595. }
  1596. return 0;
  1597. }
  1598. if (!get_id)
  1599. return 0;
  1600. stat_config.aggr_map = cpu_aggr_map__new(evsel_list->core.user_requested_cpus,
  1601. get_id, env, needs_sort);
  1602. if (!stat_config.aggr_map) {
  1603. pr_err("cannot build %s map\n", aggr_mode__string[stat_config.aggr_mode]);
  1604. return -1;
  1605. }
  1606. stat_config.aggr_get_id = aggr_mode__get_id_file(stat_config.aggr_mode);
  1607. return 0;
  1608. }
  1609. static int default_evlist_evsel_cmp(void *priv __maybe_unused,
  1610. const struct list_head *l,
  1611. const struct list_head *r)
  1612. {
  1613. const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node);
  1614. const struct evsel *lhs = container_of(lhs_core, struct evsel, core);
  1615. const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node);
  1616. const struct evsel *rhs = container_of(rhs_core, struct evsel, core);
  1617. if (evsel__leader(lhs) == evsel__leader(rhs)) {
  1618. /* Within the same group, respect the original order. */
  1619. return lhs_core->idx - rhs_core->idx;
  1620. }
  1621. /* Sort default metrics evsels first, and default show events before those. */
  1622. if (lhs->default_metricgroup != rhs->default_metricgroup)
  1623. return lhs->default_metricgroup ? -1 : 1;
  1624. if (lhs->default_show_events != rhs->default_show_events)
  1625. return lhs->default_show_events ? -1 : 1;
  1626. /* Sort by PMU type (prefers legacy types first). */
  1627. if (lhs->pmu != rhs->pmu)
  1628. return lhs->pmu->type - rhs->pmu->type;
  1629. /* Sort by name. */
  1630. return strcmp(evsel__name((struct evsel *)lhs), evsel__name((struct evsel *)rhs));
  1631. }
  1632. /*
  1633. * Add default events, if there were no attributes specified or
  1634. * if -d/--detailed, -d -d or -d -d -d is used:
  1635. */
  1636. static int add_default_events(void)
  1637. {
  1638. const char *pmu = parse_events_option_args.pmu_filter ?: "all";
  1639. struct parse_events_error err;
  1640. struct evlist *evlist = evlist__new();
  1641. struct evsel *evsel;
  1642. int ret = 0;
  1643. if (!evlist)
  1644. return -ENOMEM;
  1645. parse_events_error__init(&err);
  1646. /* Set attrs if no event is selected and !null_run: */
  1647. if (stat_config.null_run)
  1648. goto out;
  1649. if (transaction_run) {
  1650. /* Handle -T as -M transaction. Once platform specific metrics
  1651. * support has been added to the json files, all architectures
  1652. * will use this approach. To determine transaction support
  1653. * on an architecture test for such a metric name.
  1654. */
  1655. if (!metricgroup__has_metric_or_groups(pmu, "transaction")) {
  1656. pr_err("Missing transaction metrics\n");
  1657. ret = -1;
  1658. goto out;
  1659. }
  1660. ret = metricgroup__parse_groups(evlist, pmu, "transaction",
  1661. stat_config.metric_no_group,
  1662. stat_config.metric_no_merge,
  1663. stat_config.metric_no_threshold,
  1664. stat_config.user_requested_cpu_list,
  1665. stat_config.system_wide,
  1666. stat_config.hardware_aware_grouping);
  1667. goto out;
  1668. }
  1669. if (smi_cost) {
  1670. int smi;
  1671. if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
  1672. pr_err("freeze_on_smi is not supported.\n");
  1673. ret = -1;
  1674. goto out;
  1675. }
  1676. if (!smi) {
  1677. if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
  1678. pr_err("Failed to set freeze_on_smi.\n");
  1679. ret = -1;
  1680. goto out;
  1681. }
  1682. smi_reset = true;
  1683. }
  1684. if (!metricgroup__has_metric_or_groups(pmu, "smi")) {
  1685. pr_err("Missing smi metrics\n");
  1686. ret = -1;
  1687. goto out;
  1688. }
  1689. if (!force_metric_only)
  1690. stat_config.metric_only = true;
  1691. ret = metricgroup__parse_groups(evlist, pmu, "smi",
  1692. stat_config.metric_no_group,
  1693. stat_config.metric_no_merge,
  1694. stat_config.metric_no_threshold,
  1695. stat_config.user_requested_cpu_list,
  1696. stat_config.system_wide,
  1697. stat_config.hardware_aware_grouping);
  1698. goto out;
  1699. }
  1700. if (topdown_run) {
  1701. unsigned int max_level = metricgroups__topdown_max_level();
  1702. char str[] = "TopdownL1";
  1703. if (!force_metric_only)
  1704. stat_config.metric_only = true;
  1705. if (!max_level) {
  1706. pr_err("Topdown requested but the topdown metric groups aren't present.\n"
  1707. "(See perf list the metric groups have names like TopdownL1)\n");
  1708. ret = -1;
  1709. goto out;
  1710. }
  1711. if (stat_config.topdown_level > max_level) {
  1712. pr_err("Invalid top-down metrics level. The max level is %u.\n", max_level);
  1713. ret = -1;
  1714. goto out;
  1715. } else if (!stat_config.topdown_level) {
  1716. stat_config.topdown_level = 1;
  1717. }
  1718. if (!stat_config.interval && !stat_config.metric_only) {
  1719. fprintf(stat_config.output,
  1720. "Topdown accuracy may decrease when measuring long periods.\n"
  1721. "Please print the result regularly, e.g. -I1000\n");
  1722. }
  1723. str[8] = stat_config.topdown_level + '0';
  1724. if (metricgroup__parse_groups(evlist,
  1725. pmu, str,
  1726. /*metric_no_group=*/false,
  1727. /*metric_no_merge=*/false,
  1728. /*metric_no_threshold=*/true,
  1729. stat_config.user_requested_cpu_list,
  1730. stat_config.system_wide,
  1731. stat_config.hardware_aware_grouping) < 0) {
  1732. ret = -1;
  1733. goto out;
  1734. }
  1735. }
  1736. if (!stat_config.topdown_level)
  1737. stat_config.topdown_level = 1;
  1738. if (!evlist->core.nr_entries && !evsel_list->core.nr_entries) {
  1739. /*
  1740. * Add Default metrics. To minimize multiplexing, don't request
  1741. * threshold computation, but it will be computed if the events
  1742. * are present.
  1743. */
  1744. const char *default_metricgroup_names[] = {
  1745. "Default", "Default2", "Default3", "Default4",
  1746. };
  1747. for (size_t i = 0; i < ARRAY_SIZE(default_metricgroup_names); i++) {
  1748. struct evlist *metric_evlist;
  1749. if (!metricgroup__has_metric_or_groups(pmu, default_metricgroup_names[i]))
  1750. continue;
  1751. if ((int)i > detailed_run)
  1752. break;
  1753. metric_evlist = evlist__new();
  1754. if (!metric_evlist) {
  1755. ret = -ENOMEM;
  1756. break;
  1757. }
  1758. if (metricgroup__parse_groups(metric_evlist, pmu, default_metricgroup_names[i],
  1759. /*metric_no_group=*/false,
  1760. /*metric_no_merge=*/false,
  1761. /*metric_no_threshold=*/true,
  1762. stat_config.user_requested_cpu_list,
  1763. stat_config.system_wide,
  1764. stat_config.hardware_aware_grouping) < 0) {
  1765. evlist__delete(metric_evlist);
  1766. ret = -1;
  1767. break;
  1768. }
  1769. evlist__for_each_entry(metric_evlist, evsel)
  1770. evsel->default_metricgroup = true;
  1771. evlist__splice_list_tail(evlist, &metric_evlist->core.entries);
  1772. metricgroup__copy_metric_events(evlist, /*cgrp=*/NULL,
  1773. &evlist->metric_events,
  1774. &metric_evlist->metric_events);
  1775. evlist__delete(metric_evlist);
  1776. }
  1777. list_sort(/*priv=*/NULL, &evlist->core.entries, default_evlist_evsel_cmp);
  1778. }
  1779. out:
  1780. if (!ret) {
  1781. evlist__for_each_entry(evlist, evsel) {
  1782. /*
  1783. * Make at least one event non-skippable so fatal errors are visible.
  1784. * 'cycles' always used to be default and non-skippable, so use that.
  1785. */
  1786. if (!evsel__match(evsel, HARDWARE, HW_CPU_CYCLES))
  1787. evsel->skippable = true;
  1788. }
  1789. }
  1790. parse_events_error__exit(&err);
  1791. evlist__splice_list_tail(evsel_list, &evlist->core.entries);
  1792. metricgroup__copy_metric_events(evsel_list, /*cgrp=*/NULL,
  1793. &evsel_list->metric_events,
  1794. &evlist->metric_events);
  1795. evlist__delete(evlist);
  1796. return ret;
  1797. }
  1798. static const char * const stat_record_usage[] = {
  1799. "perf stat record [<options>]",
  1800. NULL,
  1801. };
  1802. static void init_features(struct perf_session *session)
  1803. {
  1804. int feat;
  1805. for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
  1806. perf_header__set_feat(&session->header, feat);
  1807. perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
  1808. perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
  1809. perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
  1810. perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
  1811. perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
  1812. }
  1813. static int __cmd_record(const struct option stat_options[], struct opt_aggr_mode *opt_mode,
  1814. int argc, const char **argv)
  1815. {
  1816. struct perf_session *session;
  1817. struct perf_data *data = &perf_stat.data;
  1818. argc = parse_options(argc, argv, stat_options, stat_record_usage,
  1819. PARSE_OPT_STOP_AT_NON_OPTION);
  1820. stat_config.aggr_mode = opt_aggr_mode_to_aggr_mode(opt_mode);
  1821. if (output_name)
  1822. data->path = output_name;
  1823. if (stat_config.run_count != 1 || forever) {
  1824. pr_err("Cannot use -r option with perf stat record.\n");
  1825. return -1;
  1826. }
  1827. session = perf_session__new(data, NULL);
  1828. if (IS_ERR(session)) {
  1829. pr_err("Perf session creation failed\n");
  1830. return PTR_ERR(session);
  1831. }
  1832. init_features(session);
  1833. session->evlist = evsel_list;
  1834. perf_stat.session = session;
  1835. perf_stat.record = true;
  1836. return argc;
  1837. }
  1838. static int process_stat_round_event(const struct perf_tool *tool __maybe_unused,
  1839. struct perf_session *session,
  1840. union perf_event *event)
  1841. {
  1842. struct perf_record_stat_round *stat_round = &event->stat_round;
  1843. struct timespec tsh, *ts = NULL;
  1844. struct perf_env *env = perf_session__env(session);
  1845. const char **argv = env->cmdline_argv;
  1846. int argc = env->nr_cmdline;
  1847. process_counters();
  1848. if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
  1849. update_stats(stat_config.walltime_nsecs_stats, stat_round->time);
  1850. if (stat_config.interval && stat_round->time) {
  1851. tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
  1852. tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
  1853. ts = &tsh;
  1854. }
  1855. print_counters(ts, argc, argv);
  1856. return 0;
  1857. }
  1858. static
  1859. int process_stat_config_event(const struct perf_tool *tool,
  1860. struct perf_session *session,
  1861. union perf_event *event)
  1862. {
  1863. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1864. perf_event__read_stat_config(&stat_config, &event->stat_config);
  1865. if (perf_cpu_map__is_empty(st->cpus)) {
  1866. if (st->aggr_mode != AGGR_UNSET)
  1867. pr_warning("warning: processing task data, aggregation mode not set\n");
  1868. } else if (st->aggr_mode != AGGR_UNSET) {
  1869. stat_config.aggr_mode = st->aggr_mode;
  1870. }
  1871. if (perf_stat.data.is_pipe)
  1872. perf_stat_init_aggr_mode();
  1873. else
  1874. perf_stat_init_aggr_mode_file(st);
  1875. if (stat_config.aggr_map) {
  1876. int nr_aggr = stat_config.aggr_map->nr;
  1877. if (evlist__alloc_aggr_stats(session->evlist, nr_aggr) < 0) {
  1878. pr_err("cannot allocate aggr counts\n");
  1879. return -1;
  1880. }
  1881. }
  1882. return 0;
  1883. }
  1884. static int set_maps(struct perf_stat *st)
  1885. {
  1886. if (!st->cpus || !st->threads)
  1887. return 0;
  1888. if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
  1889. return -EINVAL;
  1890. perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads);
  1891. if (evlist__alloc_stats(&stat_config, evsel_list, /*alloc_raw=*/true))
  1892. return -ENOMEM;
  1893. st->maps_allocated = true;
  1894. return 0;
  1895. }
  1896. static
  1897. int process_thread_map_event(const struct perf_tool *tool,
  1898. struct perf_session *session __maybe_unused,
  1899. union perf_event *event)
  1900. {
  1901. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1902. if (st->threads) {
  1903. pr_warning("Extra thread map event, ignoring.\n");
  1904. return 0;
  1905. }
  1906. st->threads = thread_map__new_event(&event->thread_map);
  1907. if (!st->threads)
  1908. return -ENOMEM;
  1909. return set_maps(st);
  1910. }
  1911. static
  1912. int process_cpu_map_event(const struct perf_tool *tool,
  1913. struct perf_session *session __maybe_unused,
  1914. union perf_event *event)
  1915. {
  1916. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1917. struct perf_cpu_map *cpus;
  1918. if (st->cpus) {
  1919. pr_warning("Extra cpu map event, ignoring.\n");
  1920. return 0;
  1921. }
  1922. cpus = cpu_map__new_data(&event->cpu_map.data);
  1923. if (!cpus)
  1924. return -ENOMEM;
  1925. st->cpus = cpus;
  1926. return set_maps(st);
  1927. }
  1928. static const char * const stat_report_usage[] = {
  1929. "perf stat report [<options>]",
  1930. NULL,
  1931. };
  1932. static struct perf_stat perf_stat = {
  1933. .aggr_mode = AGGR_UNSET,
  1934. .aggr_level = 0,
  1935. };
  1936. static int __cmd_report(int argc, const char **argv)
  1937. {
  1938. struct perf_session *session;
  1939. const struct option options[] = {
  1940. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1941. OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
  1942. "aggregate counts per processor socket", AGGR_SOCKET),
  1943. OPT_SET_UINT(0, "per-die", &perf_stat.aggr_mode,
  1944. "aggregate counts per processor die", AGGR_DIE),
  1945. OPT_SET_UINT(0, "per-cluster", &perf_stat.aggr_mode,
  1946. "aggregate counts perf processor cluster", AGGR_CLUSTER),
  1947. OPT_CALLBACK_OPTARG(0, "per-cache", &perf_stat.aggr_mode, &perf_stat.aggr_level,
  1948. "cache level",
  1949. "aggregate count at this cache level (Default: LLC)",
  1950. parse_cache_level),
  1951. OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
  1952. "aggregate counts per physical processor core", AGGR_CORE),
  1953. OPT_SET_UINT(0, "per-node", &perf_stat.aggr_mode,
  1954. "aggregate counts per numa node", AGGR_NODE),
  1955. OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
  1956. "disable CPU count aggregation", AGGR_NONE),
  1957. OPT_END()
  1958. };
  1959. struct stat st;
  1960. int ret;
  1961. argc = parse_options(argc, argv, options, stat_report_usage, 0);
  1962. if (!input_name || !strlen(input_name)) {
  1963. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  1964. input_name = "-";
  1965. else
  1966. input_name = "perf.data";
  1967. }
  1968. perf_stat.data.path = input_name;
  1969. perf_stat.data.mode = PERF_DATA_MODE_READ;
  1970. perf_tool__init(&perf_stat.tool, /*ordered_events=*/false);
  1971. perf_stat.tool.attr = perf_event__process_attr;
  1972. perf_stat.tool.event_update = perf_event__process_event_update;
  1973. perf_stat.tool.thread_map = process_thread_map_event;
  1974. perf_stat.tool.cpu_map = process_cpu_map_event;
  1975. perf_stat.tool.stat_config = process_stat_config_event;
  1976. perf_stat.tool.stat = perf_event__process_stat_event;
  1977. perf_stat.tool.stat_round = process_stat_round_event;
  1978. session = perf_session__new(&perf_stat.data, &perf_stat.tool);
  1979. if (IS_ERR(session))
  1980. return PTR_ERR(session);
  1981. perf_stat.session = session;
  1982. stat_config.output = stderr;
  1983. evlist__delete(evsel_list);
  1984. evsel_list = session->evlist;
  1985. ret = perf_session__process_events(session);
  1986. if (ret)
  1987. return ret;
  1988. perf_session__delete(session);
  1989. return 0;
  1990. }
  1991. static void setup_system_wide(int forks)
  1992. {
  1993. /*
  1994. * Make system wide (-a) the default target if
  1995. * no target was specified and one of following
  1996. * conditions is met:
  1997. *
  1998. * - there's no workload specified
  1999. * - there is workload specified but all requested
  2000. * events are system wide events
  2001. */
  2002. if (!target__none(&target))
  2003. return;
  2004. if (!forks)
  2005. target.system_wide = true;
  2006. else {
  2007. struct evsel *counter;
  2008. evlist__for_each_entry(evsel_list, counter) {
  2009. if (!counter->core.requires_cpu &&
  2010. !evsel__name_is(counter, "duration_time")) {
  2011. return;
  2012. }
  2013. }
  2014. if (evsel_list->core.nr_entries)
  2015. target.system_wide = true;
  2016. }
  2017. }
  2018. #ifdef HAVE_ARCH_X86_64_SUPPORT
  2019. static int parse_tpebs_mode(const struct option *opt, const char *str,
  2020. int unset __maybe_unused)
  2021. {
  2022. enum tpebs_mode *mode = opt->value;
  2023. if (!strcasecmp("mean", str)) {
  2024. *mode = TPEBS_MODE__MEAN;
  2025. return 0;
  2026. }
  2027. if (!strcasecmp("min", str)) {
  2028. *mode = TPEBS_MODE__MIN;
  2029. return 0;
  2030. }
  2031. if (!strcasecmp("max", str)) {
  2032. *mode = TPEBS_MODE__MAX;
  2033. return 0;
  2034. }
  2035. if (!strcasecmp("last", str)) {
  2036. *mode = TPEBS_MODE__LAST;
  2037. return 0;
  2038. }
  2039. return -1;
  2040. }
  2041. #endif // HAVE_ARCH_X86_64_SUPPORT
  2042. int cmd_stat(int argc, const char **argv)
  2043. {
  2044. struct opt_aggr_mode opt_mode = {};
  2045. bool affinity = true, affinity_set = false;
  2046. struct option stat_options[] = {
  2047. OPT_BOOLEAN('T', "transaction", &transaction_run,
  2048. "hardware transaction statistics"),
  2049. OPT_CALLBACK('e', "event", &parse_events_option_args, "event",
  2050. "event selector. use 'perf list' to list available events",
  2051. parse_events_option),
  2052. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  2053. "event filter", parse_filter),
  2054. OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
  2055. "child tasks do not inherit counters"),
  2056. OPT_STRING('p', "pid", &target.pid, "pid",
  2057. "stat events on existing process id"),
  2058. OPT_STRING('t', "tid", &target.tid, "tid",
  2059. "stat events on existing thread id"),
  2060. #ifdef HAVE_BPF_SKEL
  2061. OPT_STRING('b', "bpf-prog", &target.bpf_str, "bpf-prog-id",
  2062. "stat events on existing bpf program id"),
  2063. OPT_BOOLEAN(0, "bpf-counters", &target.use_bpf,
  2064. "use bpf program to count events"),
  2065. OPT_STRING(0, "bpf-attr-map", &target.attr_map, "attr-map-path",
  2066. "path to perf_event_attr map"),
  2067. #endif
  2068. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  2069. "system-wide collection from all CPUs"),
  2070. OPT_BOOLEAN(0, "scale", &stat_config.scale,
  2071. "Use --no-scale to disable counter scaling for multiplexing"),
  2072. OPT_INCR('v', "verbose", &verbose,
  2073. "be more verbose (show counter open errors, etc)"),
  2074. OPT_INTEGER('r', "repeat", &stat_config.run_count,
  2075. "repeat command and print average + stddev (max: 100, forever: 0)"),
  2076. OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table,
  2077. "display details about each run (only with -r option)"),
  2078. OPT_BOOLEAN('n', "null", &stat_config.null_run,
  2079. "null run - dont start any counters"),
  2080. OPT_INCR('d', "detailed", &detailed_run,
  2081. "detailed run - start a lot of events"),
  2082. OPT_BOOLEAN('S', "sync", &sync_run,
  2083. "call sync() before starting a run"),
  2084. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  2085. "print large numbers with thousands\' separators",
  2086. stat__set_big_num),
  2087. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  2088. "list of cpus to monitor in system-wide"),
  2089. OPT_BOOLEAN('A', "no-aggr", &opt_mode.no_aggr,
  2090. "disable aggregation across CPUs or PMUs"),
  2091. OPT_BOOLEAN(0, "no-merge", &opt_mode.no_aggr,
  2092. "disable aggregation the same as -A or -no-aggr"),
  2093. OPT_BOOLEAN(0, "hybrid-merge", &stat_config.hybrid_merge,
  2094. "Merge identical named hybrid events"),
  2095. OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
  2096. "print counts with custom separator"),
  2097. OPT_BOOLEAN('j', "json-output", &stat_config.json_output,
  2098. "print counts in JSON format"),
  2099. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  2100. "monitor event in cgroup name only", parse_stat_cgroups),
  2101. OPT_STRING(0, "for-each-cgroup", &stat_config.cgroup_list, "name",
  2102. "expand events for each cgroup"),
  2103. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  2104. OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
  2105. OPT_INTEGER(0, "log-fd", &output_fd,
  2106. "log output to fd, instead of stderr"),
  2107. OPT_STRING(0, "pre", &pre_cmd, "command",
  2108. "command to run prior to the measured command"),
  2109. OPT_STRING(0, "post", &post_cmd, "command",
  2110. "command to run after to the measured command"),
  2111. OPT_UINTEGER('I', "interval-print", &stat_config.interval,
  2112. "print counts at regular interval in ms "
  2113. "(overhead is possible for values <= 100ms)"),
  2114. OPT_INTEGER(0, "interval-count", &stat_config.times,
  2115. "print counts for fixed number of times"),
  2116. OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
  2117. "clear screen in between new interval"),
  2118. OPT_UINTEGER(0, "timeout", &stat_config.timeout,
  2119. "stop workload and print counts after a timeout period in ms (>= 10ms)"),
  2120. OPT_BOOLEAN(0, "per-socket", &opt_mode.socket,
  2121. "aggregate counts per processor socket"),
  2122. OPT_BOOLEAN(0, "per-die", &opt_mode.die, "aggregate counts per processor die"),
  2123. OPT_BOOLEAN(0, "per-cluster", &opt_mode.cluster,
  2124. "aggregate counts per processor cluster"),
  2125. OPT_CALLBACK_OPTARG(0, "per-cache", &opt_mode, &stat_config.aggr_level,
  2126. "cache level", "aggregate count at this cache level (Default: LLC)",
  2127. parse_cache_level),
  2128. OPT_BOOLEAN(0, "per-core", &opt_mode.core,
  2129. "aggregate counts per physical processor core"),
  2130. OPT_BOOLEAN(0, "per-thread", &opt_mode.thread, "aggregate counts per thread"),
  2131. OPT_BOOLEAN(0, "per-node", &opt_mode.node, "aggregate counts per numa node"),
  2132. OPT_INTEGER('D', "delay", &target.initial_delay,
  2133. "ms to wait before starting measurement after program start (-1: start with events disabled)"),
  2134. OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
  2135. "Only print computed metrics. No raw values", enable_metric_only),
  2136. OPT_BOOLEAN(0, "metric-no-group", &stat_config.metric_no_group,
  2137. "don't group metric events, impacts multiplexing"),
  2138. OPT_BOOLEAN(0, "metric-no-merge", &stat_config.metric_no_merge,
  2139. "don't try to share events between metrics in a group"),
  2140. OPT_BOOLEAN(0, "metric-no-threshold", &stat_config.metric_no_threshold,
  2141. "disable adding events for the metric threshold calculation"),
  2142. OPT_BOOLEAN(0, "topdown", &topdown_run,
  2143. "measure top-down statistics"),
  2144. #ifdef HAVE_ARCH_X86_64_SUPPORT
  2145. OPT_BOOLEAN(0, "record-tpebs", &tpebs_recording,
  2146. "enable recording for tpebs when retire_latency required"),
  2147. OPT_CALLBACK(0, "tpebs-mode", &tpebs_mode, "tpebs-mode",
  2148. "Mode of TPEBS recording: mean, min or max",
  2149. parse_tpebs_mode),
  2150. #endif
  2151. OPT_UINTEGER(0, "td-level", &stat_config.topdown_level,
  2152. "Set the metrics level for the top-down statistics (0: max level)"),
  2153. OPT_BOOLEAN(0, "smi-cost", &smi_cost,
  2154. "measure SMI cost"),
  2155. OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
  2156. "monitor specified metrics or metric groups (separated by ,)",
  2157. append_metric_groups),
  2158. OPT_BOOLEAN_FLAG(0, "all-kernel", &stat_config.all_kernel,
  2159. "Configure all used events to run in kernel space.",
  2160. PARSE_OPT_EXCLUSIVE),
  2161. OPT_BOOLEAN_FLAG(0, "all-user", &stat_config.all_user,
  2162. "Configure all used events to run in user space.",
  2163. PARSE_OPT_EXCLUSIVE),
  2164. OPT_BOOLEAN(0, "percore-show-thread", &stat_config.percore_show_thread,
  2165. "Use with 'percore' event qualifier to show the event "
  2166. "counts of one hardware thread by sum up total hardware "
  2167. "threads of same physical core"),
  2168. OPT_BOOLEAN(0, "summary", &stat_config.summary,
  2169. "print summary for interval mode"),
  2170. OPT_BOOLEAN(0, "no-csv-summary", &stat_config.no_csv_summary,
  2171. "don't print 'summary' for CSV summary output"),
  2172. OPT_BOOLEAN(0, "quiet", &quiet,
  2173. "don't print any output, messages or warnings (useful with record)"),
  2174. OPT_BOOLEAN_SET(0, "affinity", &affinity, &affinity_set,
  2175. "enable (default) or disable affinity optimizations to reduce IPIs"),
  2176. OPT_CALLBACK(0, "cputype", &evsel_list, "hybrid cpu type",
  2177. "Only enable events on applying cpu with this type "
  2178. "for hybrid platform (e.g. core or atom)",
  2179. parse_cputype),
  2180. #ifdef HAVE_LIBPFM
  2181. OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
  2182. "libpfm4 event selector. use 'perf list' to list available events",
  2183. parse_libpfm_events_option),
  2184. #endif
  2185. OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
  2186. "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
  2187. "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
  2188. "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
  2189. parse_control_option),
  2190. OPT_CALLBACK_OPTARG(0, "iostat", &evsel_list, &stat_config, "default",
  2191. "measure I/O performance metrics provided by arch/platform",
  2192. iostat_parse),
  2193. OPT_END()
  2194. };
  2195. const char * const stat_usage[] = {
  2196. "perf stat [<options>] [<command>]",
  2197. NULL
  2198. };
  2199. int status = -EINVAL, run_idx, err;
  2200. const char *mode;
  2201. FILE *output = stderr;
  2202. unsigned int interval, timeout;
  2203. const char * const stat_subcommands[] = { "record", "report" };
  2204. char errbuf[BUFSIZ];
  2205. struct evsel *counter;
  2206. setlocale(LC_ALL, "");
  2207. evsel_list = evlist__new();
  2208. if (evsel_list == NULL)
  2209. return -ENOMEM;
  2210. parse_events__shrink_config_terms();
  2211. /* String-parsing callback-based options would segfault when negated */
  2212. set_option_flag(stat_options, 'e', "event", PARSE_OPT_NONEG);
  2213. set_option_flag(stat_options, 'M', "metrics", PARSE_OPT_NONEG);
  2214. set_option_flag(stat_options, 'G', "cgroup", PARSE_OPT_NONEG);
  2215. argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
  2216. (const char **) stat_usage,
  2217. PARSE_OPT_STOP_AT_NON_OPTION);
  2218. stat_config.aggr_mode = opt_aggr_mode_to_aggr_mode(&opt_mode);
  2219. if (stat_config.csv_sep) {
  2220. stat_config.csv_output = true;
  2221. if (!strcmp(stat_config.csv_sep, "\\t"))
  2222. stat_config.csv_sep = "\t";
  2223. } else
  2224. stat_config.csv_sep = DEFAULT_SEPARATOR;
  2225. if (affinity_set)
  2226. evsel_list->no_affinity = !affinity;
  2227. if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
  2228. argc = __cmd_record(stat_options, &opt_mode, argc, argv);
  2229. if (argc < 0)
  2230. return -1;
  2231. } else if (argc && strlen(argv[0]) > 2 && strstarts("report", argv[0]))
  2232. return __cmd_report(argc, argv);
  2233. interval = stat_config.interval;
  2234. timeout = stat_config.timeout;
  2235. /*
  2236. * For record command the -o is already taken care of.
  2237. */
  2238. if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
  2239. output = NULL;
  2240. if (output_name && output_fd) {
  2241. fprintf(stderr, "cannot use both --output and --log-fd\n");
  2242. parse_options_usage(stat_usage, stat_options, "o", 1);
  2243. parse_options_usage(NULL, stat_options, "log-fd", 0);
  2244. goto out;
  2245. }
  2246. if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
  2247. fprintf(stderr, "--metric-only is not supported with --per-thread\n");
  2248. goto out;
  2249. }
  2250. if (stat_config.metric_only && stat_config.run_count > 1) {
  2251. fprintf(stderr, "--metric-only is not supported with -r\n");
  2252. goto out;
  2253. }
  2254. if (stat_config.csv_output || (stat_config.metric_only && stat_config.json_output)) {
  2255. /*
  2256. * Current CSV and metric-only JSON output doesn't display the
  2257. * metric threshold so don't compute it.
  2258. */
  2259. stat_config.metric_no_threshold = true;
  2260. }
  2261. if (stat_config.walltime_run_table && stat_config.run_count <= 1) {
  2262. fprintf(stderr, "--table is only supported with -r\n");
  2263. parse_options_usage(stat_usage, stat_options, "r", 1);
  2264. parse_options_usage(NULL, stat_options, "table", 0);
  2265. goto out;
  2266. }
  2267. if (output_fd < 0) {
  2268. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  2269. parse_options_usage(stat_usage, stat_options, "log-fd", 0);
  2270. goto out;
  2271. }
  2272. if (!output && !quiet) {
  2273. struct timespec tm;
  2274. mode = append_file ? "a" : "w";
  2275. output = fopen(output_name, mode);
  2276. if (!output) {
  2277. perror("failed to create output file");
  2278. return -1;
  2279. }
  2280. if (!stat_config.json_output) {
  2281. clock_gettime(CLOCK_REALTIME, &tm);
  2282. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  2283. }
  2284. } else if (output_fd > 0) {
  2285. mode = append_file ? "a" : "w";
  2286. output = fdopen(output_fd, mode);
  2287. if (!output) {
  2288. perror("Failed opening logfd");
  2289. return -errno;
  2290. }
  2291. }
  2292. if (stat_config.interval_clear && !isatty(fileno(output))) {
  2293. fprintf(stderr, "--interval-clear does not work with output\n");
  2294. parse_options_usage(stat_usage, stat_options, "o", 1);
  2295. parse_options_usage(NULL, stat_options, "log-fd", 0);
  2296. parse_options_usage(NULL, stat_options, "interval-clear", 0);
  2297. return -1;
  2298. }
  2299. stat_config.output = output;
  2300. /*
  2301. * let the spreadsheet do the pretty-printing
  2302. */
  2303. if (stat_config.csv_output) {
  2304. /* User explicitly passed -B? */
  2305. if (big_num_opt == 1) {
  2306. fprintf(stderr, "-B option not supported with -x\n");
  2307. parse_options_usage(stat_usage, stat_options, "B", 1);
  2308. parse_options_usage(NULL, stat_options, "x", 1);
  2309. goto out;
  2310. } else /* Nope, so disable big number formatting */
  2311. stat_config.big_num = false;
  2312. } else if (big_num_opt == 0) /* User passed --no-big-num */
  2313. stat_config.big_num = false;
  2314. target.inherit = !stat_config.no_inherit;
  2315. err = target__validate(&target);
  2316. if (err) {
  2317. target__strerror(&target, err, errbuf, BUFSIZ);
  2318. pr_warning("%s\n", errbuf);
  2319. }
  2320. setup_system_wide(argc);
  2321. /*
  2322. * Display user/system times only for single
  2323. * run and when there's specified tracee.
  2324. */
  2325. if ((stat_config.run_count == 1) && target__none(&target))
  2326. stat_config.ru_display = true;
  2327. if (stat_config.run_count < 0) {
  2328. pr_err("Run count must be a positive number\n");
  2329. parse_options_usage(stat_usage, stat_options, "r", 1);
  2330. goto out;
  2331. } else if (stat_config.run_count == 0) {
  2332. forever = true;
  2333. stat_config.run_count = 1;
  2334. }
  2335. if (stat_config.walltime_run_table) {
  2336. stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
  2337. if (!stat_config.walltime_run) {
  2338. pr_err("failed to setup -r option");
  2339. goto out;
  2340. }
  2341. }
  2342. if ((stat_config.aggr_mode == AGGR_THREAD) &&
  2343. !target__has_task(&target)) {
  2344. if (!target.system_wide || target.cpu_list) {
  2345. fprintf(stderr, "The --per-thread option is only "
  2346. "available when monitoring via -p -t -a "
  2347. "options or only --per-thread.\n");
  2348. parse_options_usage(NULL, stat_options, "p", 1);
  2349. parse_options_usage(NULL, stat_options, "t", 1);
  2350. goto out;
  2351. }
  2352. }
  2353. /*
  2354. * no_aggr, cgroup are for system-wide only
  2355. * --per-thread is aggregated per thread, we dont mix it with cpu mode
  2356. */
  2357. if (((stat_config.aggr_mode != AGGR_GLOBAL &&
  2358. stat_config.aggr_mode != AGGR_THREAD) ||
  2359. (nr_cgroups || stat_config.cgroup_list)) &&
  2360. !target__has_cpu(&target)) {
  2361. fprintf(stderr, "both cgroup and no-aggregation "
  2362. "modes only available in system-wide mode\n");
  2363. parse_options_usage(stat_usage, stat_options, "G", 1);
  2364. parse_options_usage(NULL, stat_options, "A", 1);
  2365. parse_options_usage(NULL, stat_options, "a", 1);
  2366. parse_options_usage(NULL, stat_options, "for-each-cgroup", 0);
  2367. goto out;
  2368. }
  2369. if (stat_config.iostat_run) {
  2370. status = iostat_prepare(evsel_list, &stat_config);
  2371. if (status)
  2372. goto out;
  2373. if (iostat_mode == IOSTAT_LIST) {
  2374. iostat_list(evsel_list, &stat_config);
  2375. goto out;
  2376. } else if (verbose > 0)
  2377. iostat_list(evsel_list, &stat_config);
  2378. if (iostat_mode == IOSTAT_RUN && !target__has_cpu(&target))
  2379. target.system_wide = true;
  2380. }
  2381. if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
  2382. target.per_thread = true;
  2383. stat_config.system_wide = target.system_wide;
  2384. if (target.cpu_list) {
  2385. stat_config.user_requested_cpu_list = strdup(target.cpu_list);
  2386. if (!stat_config.user_requested_cpu_list) {
  2387. status = -ENOMEM;
  2388. goto out;
  2389. }
  2390. }
  2391. /*
  2392. * Metric parsing needs to be delayed as metrics may optimize events
  2393. * knowing the target is system-wide.
  2394. */
  2395. if (metrics) {
  2396. const char *pmu = parse_events_option_args.pmu_filter ?: "all";
  2397. int ret = metricgroup__parse_groups(evsel_list, pmu, metrics,
  2398. stat_config.metric_no_group,
  2399. stat_config.metric_no_merge,
  2400. stat_config.metric_no_threshold,
  2401. stat_config.user_requested_cpu_list,
  2402. stat_config.system_wide,
  2403. stat_config.hardware_aware_grouping);
  2404. zfree(&metrics);
  2405. if (ret) {
  2406. status = ret;
  2407. goto out;
  2408. }
  2409. }
  2410. if (add_default_events())
  2411. goto out;
  2412. if (stat_config.cgroup_list) {
  2413. if (nr_cgroups > 0) {
  2414. pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
  2415. parse_options_usage(stat_usage, stat_options, "G", 1);
  2416. parse_options_usage(NULL, stat_options, "for-each-cgroup", 0);
  2417. goto out;
  2418. }
  2419. if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list, true) < 0) {
  2420. parse_options_usage(stat_usage, stat_options,
  2421. "for-each-cgroup", 0);
  2422. goto out;
  2423. }
  2424. }
  2425. #ifdef HAVE_BPF_SKEL
  2426. if (target.use_bpf && nr_cgroups &&
  2427. (evsel_list->core.nr_entries / nr_cgroups) > BPERF_CGROUP__MAX_EVENTS) {
  2428. pr_warning("Disabling BPF counters due to more events (%d) than the max (%d)\n",
  2429. evsel_list->core.nr_entries / nr_cgroups, BPERF_CGROUP__MAX_EVENTS);
  2430. target.use_bpf = false;
  2431. }
  2432. #endif // HAVE_BPF_SKEL
  2433. evlist__warn_user_requested_cpus(evsel_list, target.cpu_list);
  2434. evlist__for_each_entry(evsel_list, counter) {
  2435. /*
  2436. * Setup BPF counters to require CPUs as any(-1) isn't
  2437. * supported. evlist__create_maps below will propagate this
  2438. * information to the evsels. Note, evsel__is_bperf isn't yet
  2439. * set up, and this change must happen early, so directly use
  2440. * the bpf_counter variable and target information.
  2441. */
  2442. if ((counter->bpf_counter || target.use_bpf) && !target__has_cpu(&target))
  2443. counter->core.requires_cpu = true;
  2444. }
  2445. if (evlist__create_maps(evsel_list, &target) < 0) {
  2446. if (target__has_task(&target)) {
  2447. pr_err("Problems finding threads of monitor\n");
  2448. parse_options_usage(stat_usage, stat_options, "p", 1);
  2449. parse_options_usage(NULL, stat_options, "t", 1);
  2450. } else if (target__has_cpu(&target)) {
  2451. perror("failed to parse CPUs map");
  2452. parse_options_usage(stat_usage, stat_options, "C", 1);
  2453. parse_options_usage(NULL, stat_options, "a", 1);
  2454. }
  2455. goto out;
  2456. }
  2457. evlist__check_cpu_maps(evsel_list);
  2458. /*
  2459. * Initialize thread_map with comm names,
  2460. * so we could print it out on output.
  2461. */
  2462. if (stat_config.aggr_mode == AGGR_THREAD) {
  2463. thread_map__read_comms(evsel_list->core.threads);
  2464. }
  2465. if (stat_config.aggr_mode == AGGR_NODE)
  2466. cpu__setup_cpunode_map();
  2467. if (stat_config.times && interval)
  2468. interval_count = true;
  2469. else if (stat_config.times && !interval) {
  2470. pr_err("interval-count option should be used together with "
  2471. "interval-print.\n");
  2472. parse_options_usage(stat_usage, stat_options, "interval-count", 0);
  2473. parse_options_usage(stat_usage, stat_options, "I", 1);
  2474. goto out;
  2475. }
  2476. if (timeout && timeout < 100) {
  2477. if (timeout < 10) {
  2478. pr_err("timeout must be >= 10ms.\n");
  2479. parse_options_usage(stat_usage, stat_options, "timeout", 0);
  2480. goto out;
  2481. } else
  2482. pr_warning("timeout < 100ms. "
  2483. "The overhead percentage could be high in some cases. "
  2484. "Please proceed with caution.\n");
  2485. }
  2486. if (timeout && interval) {
  2487. pr_err("timeout option is not supported with interval-print.\n");
  2488. parse_options_usage(stat_usage, stat_options, "timeout", 0);
  2489. parse_options_usage(stat_usage, stat_options, "I", 1);
  2490. goto out;
  2491. }
  2492. if (perf_stat_init_aggr_mode())
  2493. goto out;
  2494. if (evlist__alloc_stats(&stat_config, evsel_list, interval))
  2495. goto out;
  2496. /*
  2497. * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
  2498. * while avoiding that older tools show confusing messages.
  2499. *
  2500. * However for pipe sessions we need to keep it zero,
  2501. * because script's perf_evsel__check_attr is triggered
  2502. * by attr->sample_type != 0, and we can't run it on
  2503. * stat sessions.
  2504. */
  2505. stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
  2506. /*
  2507. * We dont want to block the signals - that would cause
  2508. * child tasks to inherit that and Ctrl-C would not work.
  2509. * What we want is for Ctrl-C to work in the exec()-ed
  2510. * task, but being ignored by perf stat itself:
  2511. */
  2512. atexit(sig_atexit);
  2513. if (!forever)
  2514. signal(SIGINT, skip_signal);
  2515. signal(SIGCHLD, skip_signal);
  2516. signal(SIGALRM, skip_signal);
  2517. signal(SIGABRT, skip_signal);
  2518. if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack))
  2519. goto out;
  2520. /* Enable ignoring missing threads when -p option is defined. */
  2521. evlist__first(evsel_list)->ignore_missing_thread = target.pid;
  2522. status = 0;
  2523. for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
  2524. if (stat_config.run_count != 1 && verbose > 0)
  2525. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  2526. run_idx + 1);
  2527. if (run_idx != 0)
  2528. evlist__reset_prev_raw_counts(evsel_list);
  2529. status = run_perf_stat(argc, argv, run_idx);
  2530. if (status < 0)
  2531. break;
  2532. if (forever && !interval) {
  2533. print_counters(NULL, argc, argv);
  2534. perf_stat__reset_stats();
  2535. }
  2536. }
  2537. if (!forever && status != -1 && (!interval || stat_config.summary)) {
  2538. if (stat_config.run_count > 1)
  2539. evlist__copy_res_stats(&stat_config, evsel_list);
  2540. print_counters(NULL, argc, argv);
  2541. }
  2542. evlist__finalize_ctlfd(evsel_list);
  2543. if (STAT_RECORD) {
  2544. /*
  2545. * We synthesize the kernel mmap record just so that older tools
  2546. * don't emit warnings about not being able to resolve symbols
  2547. * due to /proc/sys/kernel/kptr_restrict settings and instead provide
  2548. * a saner message about no samples being in the perf.data file.
  2549. *
  2550. * This also serves to suppress a warning about f_header.data.size == 0
  2551. * in header.c at the moment 'perf stat record' gets introduced, which
  2552. * is not really needed once we start adding the stat specific PERF_RECORD_
  2553. * records, but the need to suppress the kptr_restrict messages in older
  2554. * tools remain -acme
  2555. */
  2556. int fd = perf_data__fd(&perf_stat.data);
  2557. err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
  2558. process_synthesized_event,
  2559. &perf_stat.session->machines.host);
  2560. if (err) {
  2561. pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
  2562. "older tools may produce warnings about this file\n.");
  2563. }
  2564. if (!interval) {
  2565. if (WRITE_STAT_ROUND_EVENT(stat_config.walltime_nsecs_stats->max, FINAL))
  2566. pr_err("failed to write stat round event\n");
  2567. }
  2568. if (!perf_stat.data.is_pipe) {
  2569. perf_stat.session->header.data_size += perf_stat.bytes_written;
  2570. perf_session__write_header(perf_stat.session, evsel_list, fd, true);
  2571. }
  2572. evlist__close(evsel_list);
  2573. perf_session__delete(perf_stat.session);
  2574. }
  2575. perf_stat__exit_aggr_mode();
  2576. evlist__free_stats(evsel_list);
  2577. out:
  2578. if (stat_config.iostat_run)
  2579. iostat_release(evsel_list);
  2580. zfree(&stat_config.walltime_run);
  2581. zfree(&stat_config.user_requested_cpu_list);
  2582. if (smi_cost && smi_reset)
  2583. sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
  2584. evlist__delete(evsel_list);
  2585. evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
  2586. /* Only the low byte of status becomes the exit code. */
  2587. return abs(status);
  2588. }