builtin-stat.c 81 KB

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