builtin-stat.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595
  1. /*
  2. * builtin-stat.c
  3. *
  4. * Builtin stat command: Give a precise performance counters summary
  5. * overview about any workload, CPU or specific PID.
  6. *
  7. * Sample output:
  8. $ perf stat ./hackbench 10
  9. Time: 0.118
  10. Performance counter stats for './hackbench 10':
  11. 1708.761321 task-clock # 11.037 CPUs utilized
  12. 41,190 context-switches # 0.024 M/sec
  13. 6,735 CPU-migrations # 0.004 M/sec
  14. 17,318 page-faults # 0.010 M/sec
  15. 5,205,202,243 cycles # 3.046 GHz
  16. 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
  17. 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
  18. 2,603,501,247 instructions # 0.50 insns per cycle
  19. # 1.48 stalled cycles per insn
  20. 484,357,498 branches # 283.455 M/sec
  21. 6,388,934 branch-misses # 1.32% of all branches
  22. 0.154822978 seconds time elapsed
  23. *
  24. * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  25. *
  26. * Improvements and fixes by:
  27. *
  28. * Arjan van de Ven <arjan@linux.intel.com>
  29. * Yanmin Zhang <yanmin.zhang@intel.com>
  30. * Wu Fengguang <fengguang.wu@intel.com>
  31. * Mike Galbraith <efault@gmx.de>
  32. * Paul Mackerras <paulus@samba.org>
  33. * Jaswinder Singh Rajput <jaswinder@kernel.org>
  34. *
  35. * Released under the GPL v2. (and only v2, not any later version)
  36. */
  37. #include "perf.h"
  38. #include "builtin.h"
  39. #include "util/cgroup.h"
  40. #include "util/util.h"
  41. #include <subcmd/parse-options.h>
  42. #include "util/parse-events.h"
  43. #include "util/pmu.h"
  44. #include "util/event.h"
  45. #include "util/evlist.h"
  46. #include "util/evsel.h"
  47. #include "util/debug.h"
  48. #include "util/drv_configs.h"
  49. #include "util/color.h"
  50. #include "util/stat.h"
  51. #include "util/header.h"
  52. #include "util/cpumap.h"
  53. #include "util/thread.h"
  54. #include "util/thread_map.h"
  55. #include "util/counts.h"
  56. #include "util/group.h"
  57. #include "util/session.h"
  58. #include "util/tool.h"
  59. #include "util/group.h"
  60. #include "asm/bug.h"
  61. #include <linux/time64.h>
  62. #include <api/fs/fs.h>
  63. #include <stdlib.h>
  64. #include <sys/prctl.h>
  65. #include <locale.h>
  66. #include <math.h>
  67. #define DEFAULT_SEPARATOR " "
  68. #define CNTR_NOT_SUPPORTED "<not supported>"
  69. #define CNTR_NOT_COUNTED "<not counted>"
  70. static void print_counters(struct timespec *ts, int argc, const char **argv);
  71. /* Default events used for perf stat -T */
  72. static const char *transaction_attrs = {
  73. "task-clock,"
  74. "{"
  75. "instructions,"
  76. "cycles,"
  77. "cpu/cycles-t/,"
  78. "cpu/tx-start/,"
  79. "cpu/el-start/,"
  80. "cpu/cycles-ct/"
  81. "}"
  82. };
  83. /* More limited version when the CPU does not have all events. */
  84. static const char * transaction_limited_attrs = {
  85. "task-clock,"
  86. "{"
  87. "instructions,"
  88. "cycles,"
  89. "cpu/cycles-t/,"
  90. "cpu/tx-start/"
  91. "}"
  92. };
  93. static const char * topdown_attrs[] = {
  94. "topdown-total-slots",
  95. "topdown-slots-retired",
  96. "topdown-recovery-bubbles",
  97. "topdown-fetch-bubbles",
  98. "topdown-slots-issued",
  99. NULL,
  100. };
  101. static struct perf_evlist *evsel_list;
  102. static struct target target = {
  103. .uid = UINT_MAX,
  104. };
  105. typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
  106. static int run_count = 1;
  107. static bool no_inherit = false;
  108. static volatile pid_t child_pid = -1;
  109. static bool null_run = false;
  110. static int detailed_run = 0;
  111. static bool transaction_run;
  112. static bool topdown_run = false;
  113. static bool big_num = true;
  114. static int big_num_opt = -1;
  115. static const char *csv_sep = NULL;
  116. static bool csv_output = false;
  117. static bool group = false;
  118. static const char *pre_cmd = NULL;
  119. static const char *post_cmd = NULL;
  120. static bool sync_run = false;
  121. static unsigned int initial_delay = 0;
  122. static unsigned int unit_width = 4; /* strlen("unit") */
  123. static bool forever = false;
  124. static bool metric_only = false;
  125. static bool force_metric_only = false;
  126. static struct timespec ref_time;
  127. static struct cpu_map *aggr_map;
  128. static aggr_get_id_t aggr_get_id;
  129. static bool append_file;
  130. static const char *output_name;
  131. static int output_fd;
  132. struct perf_stat {
  133. bool record;
  134. struct perf_data_file file;
  135. struct perf_session *session;
  136. u64 bytes_written;
  137. struct perf_tool tool;
  138. bool maps_allocated;
  139. struct cpu_map *cpus;
  140. struct thread_map *threads;
  141. enum aggr_mode aggr_mode;
  142. };
  143. static struct perf_stat perf_stat;
  144. #define STAT_RECORD perf_stat.record
  145. static volatile int done = 0;
  146. static struct perf_stat_config stat_config = {
  147. .aggr_mode = AGGR_GLOBAL,
  148. .scale = true,
  149. };
  150. static inline void diff_timespec(struct timespec *r, struct timespec *a,
  151. struct timespec *b)
  152. {
  153. r->tv_sec = a->tv_sec - b->tv_sec;
  154. if (a->tv_nsec < b->tv_nsec) {
  155. r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
  156. r->tv_sec--;
  157. } else {
  158. r->tv_nsec = a->tv_nsec - b->tv_nsec ;
  159. }
  160. }
  161. static void perf_stat__reset_stats(void)
  162. {
  163. perf_evlist__reset_stats(evsel_list);
  164. perf_stat__reset_shadow_stats();
  165. }
  166. static int create_perf_stat_counter(struct perf_evsel *evsel)
  167. {
  168. struct perf_event_attr *attr = &evsel->attr;
  169. if (stat_config.scale)
  170. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  171. PERF_FORMAT_TOTAL_TIME_RUNNING;
  172. attr->inherit = !no_inherit;
  173. /*
  174. * Some events get initialized with sample_(period/type) set,
  175. * like tracepoints. Clear it up for counting.
  176. */
  177. attr->sample_period = 0;
  178. /*
  179. * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
  180. * while avoiding that older tools show confusing messages.
  181. *
  182. * However for pipe sessions we need to keep it zero,
  183. * because script's perf_evsel__check_attr is triggered
  184. * by attr->sample_type != 0, and we can't run it on
  185. * stat sessions.
  186. */
  187. if (!(STAT_RECORD && perf_stat.file.is_pipe))
  188. attr->sample_type = PERF_SAMPLE_IDENTIFIER;
  189. /*
  190. * Disabling all counters initially, they will be enabled
  191. * either manually by us or by kernel via enable_on_exec
  192. * set later.
  193. */
  194. if (perf_evsel__is_group_leader(evsel)) {
  195. attr->disabled = 1;
  196. /*
  197. * In case of initial_delay we enable tracee
  198. * events manually.
  199. */
  200. if (target__none(&target) && !initial_delay)
  201. attr->enable_on_exec = 1;
  202. }
  203. if (target__has_cpu(&target))
  204. return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
  205. return perf_evsel__open_per_thread(evsel, evsel_list->threads);
  206. }
  207. /*
  208. * Does the counter have nsecs as a unit?
  209. */
  210. static inline int nsec_counter(struct perf_evsel *evsel)
  211. {
  212. if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  213. perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  214. return 1;
  215. return 0;
  216. }
  217. static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
  218. union perf_event *event,
  219. struct perf_sample *sample __maybe_unused,
  220. struct machine *machine __maybe_unused)
  221. {
  222. if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
  223. pr_err("failed to write perf data, error: %m\n");
  224. return -1;
  225. }
  226. perf_stat.bytes_written += event->header.size;
  227. return 0;
  228. }
  229. static int write_stat_round_event(u64 tm, u64 type)
  230. {
  231. return perf_event__synthesize_stat_round(NULL, tm, type,
  232. process_synthesized_event,
  233. NULL);
  234. }
  235. #define WRITE_STAT_ROUND_EVENT(time, interval) \
  236. write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
  237. #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
  238. static int
  239. perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
  240. struct perf_counts_values *count)
  241. {
  242. struct perf_sample_id *sid = SID(counter, cpu, thread);
  243. return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
  244. process_synthesized_event, NULL);
  245. }
  246. /*
  247. * Read out the results of a single counter:
  248. * do not aggregate counts across CPUs in system-wide mode
  249. */
  250. static int read_counter(struct perf_evsel *counter)
  251. {
  252. int nthreads = thread_map__nr(evsel_list->threads);
  253. int ncpus, cpu, thread;
  254. if (target__has_cpu(&target))
  255. ncpus = perf_evsel__nr_cpus(counter);
  256. else
  257. ncpus = 1;
  258. if (!counter->supported)
  259. return -ENOENT;
  260. if (counter->system_wide)
  261. nthreads = 1;
  262. for (thread = 0; thread < nthreads; thread++) {
  263. for (cpu = 0; cpu < ncpus; cpu++) {
  264. struct perf_counts_values *count;
  265. count = perf_counts(counter->counts, cpu, thread);
  266. if (perf_evsel__read(counter, cpu, thread, count))
  267. return -1;
  268. if (STAT_RECORD) {
  269. if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
  270. pr_err("failed to write stat event\n");
  271. return -1;
  272. }
  273. }
  274. if (verbose > 1) {
  275. fprintf(stat_config.output,
  276. "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
  277. perf_evsel__name(counter),
  278. cpu,
  279. count->val, count->ena, count->run);
  280. }
  281. }
  282. }
  283. return 0;
  284. }
  285. static void read_counters(void)
  286. {
  287. struct perf_evsel *counter;
  288. evlist__for_each_entry(evsel_list, counter) {
  289. if (read_counter(counter))
  290. pr_debug("failed to read counter %s\n", counter->name);
  291. if (perf_stat_process_counter(&stat_config, counter))
  292. pr_warning("failed to process counter %s\n", counter->name);
  293. }
  294. }
  295. static void process_interval(void)
  296. {
  297. struct timespec ts, rs;
  298. read_counters();
  299. clock_gettime(CLOCK_MONOTONIC, &ts);
  300. diff_timespec(&rs, &ts, &ref_time);
  301. if (STAT_RECORD) {
  302. if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
  303. pr_err("failed to write stat round event\n");
  304. }
  305. print_counters(&rs, 0, NULL);
  306. }
  307. static void enable_counters(void)
  308. {
  309. if (initial_delay)
  310. usleep(initial_delay * USEC_PER_MSEC);
  311. /*
  312. * We need to enable counters only if:
  313. * - we don't have tracee (attaching to task or cpu)
  314. * - we have initial delay configured
  315. */
  316. if (!target__none(&target) || initial_delay)
  317. perf_evlist__enable(evsel_list);
  318. }
  319. static void disable_counters(void)
  320. {
  321. /*
  322. * If we don't have tracee (attaching to task or cpu), counters may
  323. * still be running. To get accurate group ratios, we must stop groups
  324. * from counting before reading their constituent counters.
  325. */
  326. if (!target__none(&target))
  327. perf_evlist__disable(evsel_list);
  328. }
  329. static volatile int workload_exec_errno;
  330. /*
  331. * perf_evlist__prepare_workload will send a SIGUSR1
  332. * if the fork fails, since we asked by setting its
  333. * want_signal to true.
  334. */
  335. static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
  336. void *ucontext __maybe_unused)
  337. {
  338. workload_exec_errno = info->si_value.sival_int;
  339. }
  340. static bool has_unit(struct perf_evsel *counter)
  341. {
  342. return counter->unit && *counter->unit;
  343. }
  344. static bool has_scale(struct perf_evsel *counter)
  345. {
  346. return counter->scale != 1;
  347. }
  348. static int perf_stat_synthesize_config(bool is_pipe)
  349. {
  350. struct perf_evsel *counter;
  351. int err;
  352. if (is_pipe) {
  353. err = perf_event__synthesize_attrs(NULL, perf_stat.session,
  354. process_synthesized_event);
  355. if (err < 0) {
  356. pr_err("Couldn't synthesize attrs.\n");
  357. return err;
  358. }
  359. }
  360. /*
  361. * Synthesize other events stuff not carried within
  362. * attr event - unit, scale, name
  363. */
  364. evlist__for_each_entry(evsel_list, counter) {
  365. if (!counter->supported)
  366. continue;
  367. /*
  368. * Synthesize unit and scale only if it's defined.
  369. */
  370. if (has_unit(counter)) {
  371. err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
  372. if (err < 0) {
  373. pr_err("Couldn't synthesize evsel unit.\n");
  374. return err;
  375. }
  376. }
  377. if (has_scale(counter)) {
  378. err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
  379. if (err < 0) {
  380. pr_err("Couldn't synthesize evsel scale.\n");
  381. return err;
  382. }
  383. }
  384. if (counter->own_cpus) {
  385. err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
  386. if (err < 0) {
  387. pr_err("Couldn't synthesize evsel scale.\n");
  388. return err;
  389. }
  390. }
  391. /*
  392. * Name is needed only for pipe output,
  393. * perf.data carries event names.
  394. */
  395. if (is_pipe) {
  396. err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
  397. if (err < 0) {
  398. pr_err("Couldn't synthesize evsel name.\n");
  399. return err;
  400. }
  401. }
  402. }
  403. err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
  404. process_synthesized_event,
  405. NULL);
  406. if (err < 0) {
  407. pr_err("Couldn't synthesize thread map.\n");
  408. return err;
  409. }
  410. err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
  411. process_synthesized_event, NULL);
  412. if (err < 0) {
  413. pr_err("Couldn't synthesize thread map.\n");
  414. return err;
  415. }
  416. err = perf_event__synthesize_stat_config(NULL, &stat_config,
  417. process_synthesized_event, NULL);
  418. if (err < 0) {
  419. pr_err("Couldn't synthesize config.\n");
  420. return err;
  421. }
  422. return 0;
  423. }
  424. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  425. static int __store_counter_ids(struct perf_evsel *counter,
  426. struct cpu_map *cpus,
  427. struct thread_map *threads)
  428. {
  429. int cpu, thread;
  430. for (cpu = 0; cpu < cpus->nr; cpu++) {
  431. for (thread = 0; thread < threads->nr; thread++) {
  432. int fd = FD(counter, cpu, thread);
  433. if (perf_evlist__id_add_fd(evsel_list, counter,
  434. cpu, thread, fd) < 0)
  435. return -1;
  436. }
  437. }
  438. return 0;
  439. }
  440. static int store_counter_ids(struct perf_evsel *counter)
  441. {
  442. struct cpu_map *cpus = counter->cpus;
  443. struct thread_map *threads = counter->threads;
  444. if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
  445. return -ENOMEM;
  446. return __store_counter_ids(counter, cpus, threads);
  447. }
  448. static int __run_perf_stat(int argc, const char **argv)
  449. {
  450. int interval = stat_config.interval;
  451. char msg[512];
  452. unsigned long long t0, t1;
  453. struct perf_evsel *counter;
  454. struct timespec ts;
  455. size_t l;
  456. int status = 0;
  457. const bool forks = (argc > 0);
  458. bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
  459. struct perf_evsel_config_term *err_term;
  460. if (interval) {
  461. ts.tv_sec = interval / USEC_PER_MSEC;
  462. ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
  463. } else {
  464. ts.tv_sec = 1;
  465. ts.tv_nsec = 0;
  466. }
  467. if (forks) {
  468. if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
  469. workload_exec_failed_signal) < 0) {
  470. perror("failed to prepare workload");
  471. return -1;
  472. }
  473. child_pid = evsel_list->workload.pid;
  474. }
  475. if (group)
  476. perf_evlist__set_leader(evsel_list);
  477. evlist__for_each_entry(evsel_list, counter) {
  478. try_again:
  479. if (create_perf_stat_counter(counter) < 0) {
  480. /*
  481. * PPC returns ENXIO for HW counters until 2.6.37
  482. * (behavior changed with commit b0a873e).
  483. */
  484. if (errno == EINVAL || errno == ENOSYS ||
  485. errno == ENOENT || errno == EOPNOTSUPP ||
  486. errno == ENXIO) {
  487. if (verbose)
  488. ui__warning("%s event is not supported by the kernel.\n",
  489. perf_evsel__name(counter));
  490. counter->supported = false;
  491. if ((counter->leader != counter) ||
  492. !(counter->leader->nr_members > 1))
  493. continue;
  494. } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
  495. if (verbose)
  496. ui__warning("%s\n", msg);
  497. goto try_again;
  498. }
  499. perf_evsel__open_strerror(counter, &target,
  500. errno, msg, sizeof(msg));
  501. ui__error("%s\n", msg);
  502. if (child_pid != -1)
  503. kill(child_pid, SIGTERM);
  504. return -1;
  505. }
  506. counter->supported = true;
  507. l = strlen(counter->unit);
  508. if (l > unit_width)
  509. unit_width = l;
  510. if (STAT_RECORD && store_counter_ids(counter))
  511. return -1;
  512. }
  513. if (perf_evlist__apply_filters(evsel_list, &counter)) {
  514. error("failed to set filter \"%s\" on event %s with %d (%s)\n",
  515. counter->filter, perf_evsel__name(counter), errno,
  516. str_error_r(errno, msg, sizeof(msg)));
  517. return -1;
  518. }
  519. if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
  520. error("failed to set config \"%s\" on event %s with %d (%s)\n",
  521. err_term->val.drv_cfg, perf_evsel__name(counter), errno,
  522. str_error_r(errno, msg, sizeof(msg)));
  523. return -1;
  524. }
  525. if (STAT_RECORD) {
  526. int err, fd = perf_data_file__fd(&perf_stat.file);
  527. if (is_pipe) {
  528. err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
  529. } else {
  530. err = perf_session__write_header(perf_stat.session, evsel_list,
  531. fd, false);
  532. }
  533. if (err < 0)
  534. return err;
  535. err = perf_stat_synthesize_config(is_pipe);
  536. if (err < 0)
  537. return err;
  538. }
  539. /*
  540. * Enable counters and exec the command:
  541. */
  542. t0 = rdclock();
  543. clock_gettime(CLOCK_MONOTONIC, &ref_time);
  544. if (forks) {
  545. perf_evlist__start_workload(evsel_list);
  546. enable_counters();
  547. if (interval) {
  548. while (!waitpid(child_pid, &status, WNOHANG)) {
  549. nanosleep(&ts, NULL);
  550. process_interval();
  551. }
  552. }
  553. wait(&status);
  554. if (workload_exec_errno) {
  555. const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
  556. pr_err("Workload failed: %s\n", emsg);
  557. return -1;
  558. }
  559. if (WIFSIGNALED(status))
  560. psignal(WTERMSIG(status), argv[0]);
  561. } else {
  562. enable_counters();
  563. while (!done) {
  564. nanosleep(&ts, NULL);
  565. if (interval)
  566. process_interval();
  567. }
  568. }
  569. disable_counters();
  570. t1 = rdclock();
  571. update_stats(&walltime_nsecs_stats, t1 - t0);
  572. /*
  573. * Closing a group leader splits the group, and as we only disable
  574. * group leaders, results in remaining events becoming enabled. To
  575. * avoid arbitrary skew, we must read all counters before closing any
  576. * group leaders.
  577. */
  578. read_counters();
  579. perf_evlist__close(evsel_list);
  580. return WEXITSTATUS(status);
  581. }
  582. static int run_perf_stat(int argc, const char **argv)
  583. {
  584. int ret;
  585. if (pre_cmd) {
  586. ret = system(pre_cmd);
  587. if (ret)
  588. return ret;
  589. }
  590. if (sync_run)
  591. sync();
  592. ret = __run_perf_stat(argc, argv);
  593. if (ret)
  594. return ret;
  595. if (post_cmd) {
  596. ret = system(post_cmd);
  597. if (ret)
  598. return ret;
  599. }
  600. return ret;
  601. }
  602. static void print_running(u64 run, u64 ena)
  603. {
  604. if (csv_output) {
  605. fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
  606. csv_sep,
  607. run,
  608. csv_sep,
  609. ena ? 100.0 * run / ena : 100.0);
  610. } else if (run != ena) {
  611. fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
  612. }
  613. }
  614. static void print_noise_pct(double total, double avg)
  615. {
  616. double pct = rel_stddev_stats(total, avg);
  617. if (csv_output)
  618. fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
  619. else if (pct)
  620. fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
  621. }
  622. static void print_noise(struct perf_evsel *evsel, double avg)
  623. {
  624. struct perf_stat_evsel *ps;
  625. if (run_count == 1)
  626. return;
  627. ps = evsel->priv;
  628. print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
  629. }
  630. static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
  631. {
  632. switch (stat_config.aggr_mode) {
  633. case AGGR_CORE:
  634. fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
  635. cpu_map__id_to_socket(id),
  636. csv_output ? 0 : -8,
  637. cpu_map__id_to_cpu(id),
  638. csv_sep,
  639. csv_output ? 0 : 4,
  640. nr,
  641. csv_sep);
  642. break;
  643. case AGGR_SOCKET:
  644. fprintf(stat_config.output, "S%*d%s%*d%s",
  645. csv_output ? 0 : -5,
  646. id,
  647. csv_sep,
  648. csv_output ? 0 : 4,
  649. nr,
  650. csv_sep);
  651. break;
  652. case AGGR_NONE:
  653. fprintf(stat_config.output, "CPU%*d%s",
  654. csv_output ? 0 : -4,
  655. perf_evsel__cpus(evsel)->map[id], csv_sep);
  656. break;
  657. case AGGR_THREAD:
  658. fprintf(stat_config.output, "%*s-%*d%s",
  659. csv_output ? 0 : 16,
  660. thread_map__comm(evsel->threads, id),
  661. csv_output ? 0 : -8,
  662. thread_map__pid(evsel->threads, id),
  663. csv_sep);
  664. break;
  665. case AGGR_GLOBAL:
  666. case AGGR_UNSET:
  667. default:
  668. break;
  669. }
  670. }
  671. struct outstate {
  672. FILE *fh;
  673. bool newline;
  674. const char *prefix;
  675. int nfields;
  676. int id, nr;
  677. struct perf_evsel *evsel;
  678. };
  679. #define METRIC_LEN 35
  680. static void new_line_std(void *ctx)
  681. {
  682. struct outstate *os = ctx;
  683. os->newline = true;
  684. }
  685. static void do_new_line_std(struct outstate *os)
  686. {
  687. fputc('\n', os->fh);
  688. fputs(os->prefix, os->fh);
  689. aggr_printout(os->evsel, os->id, os->nr);
  690. if (stat_config.aggr_mode == AGGR_NONE)
  691. fprintf(os->fh, " ");
  692. fprintf(os->fh, " ");
  693. }
  694. static void print_metric_std(void *ctx, const char *color, const char *fmt,
  695. const char *unit, double val)
  696. {
  697. struct outstate *os = ctx;
  698. FILE *out = os->fh;
  699. int n;
  700. bool newline = os->newline;
  701. os->newline = false;
  702. if (unit == NULL || fmt == NULL) {
  703. fprintf(out, "%-*s", METRIC_LEN, "");
  704. return;
  705. }
  706. if (newline)
  707. do_new_line_std(os);
  708. n = fprintf(out, " # ");
  709. if (color)
  710. n += color_fprintf(out, color, fmt, val);
  711. else
  712. n += fprintf(out, fmt, val);
  713. fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
  714. }
  715. static void new_line_csv(void *ctx)
  716. {
  717. struct outstate *os = ctx;
  718. int i;
  719. fputc('\n', os->fh);
  720. if (os->prefix)
  721. fprintf(os->fh, "%s%s", os->prefix, csv_sep);
  722. aggr_printout(os->evsel, os->id, os->nr);
  723. for (i = 0; i < os->nfields; i++)
  724. fputs(csv_sep, os->fh);
  725. }
  726. static void print_metric_csv(void *ctx,
  727. const char *color __maybe_unused,
  728. const char *fmt, const char *unit, double val)
  729. {
  730. struct outstate *os = ctx;
  731. FILE *out = os->fh;
  732. char buf[64], *vals, *ends;
  733. if (unit == NULL || fmt == NULL) {
  734. fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
  735. return;
  736. }
  737. snprintf(buf, sizeof(buf), fmt, val);
  738. vals = buf;
  739. while (isspace(*vals))
  740. vals++;
  741. ends = vals;
  742. while (isdigit(*ends) || *ends == '.')
  743. ends++;
  744. *ends = 0;
  745. while (isspace(*unit))
  746. unit++;
  747. fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
  748. }
  749. #define METRIC_ONLY_LEN 20
  750. /* Filter out some columns that don't work well in metrics only mode */
  751. static bool valid_only_metric(const char *unit)
  752. {
  753. if (!unit)
  754. return false;
  755. if (strstr(unit, "/sec") ||
  756. strstr(unit, "hz") ||
  757. strstr(unit, "Hz") ||
  758. strstr(unit, "CPUs utilized"))
  759. return false;
  760. return true;
  761. }
  762. static const char *fixunit(char *buf, struct perf_evsel *evsel,
  763. const char *unit)
  764. {
  765. if (!strncmp(unit, "of all", 6)) {
  766. snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
  767. unit);
  768. return buf;
  769. }
  770. return unit;
  771. }
  772. static void print_metric_only(void *ctx, const char *color, const char *fmt,
  773. const char *unit, double val)
  774. {
  775. struct outstate *os = ctx;
  776. FILE *out = os->fh;
  777. int n;
  778. char buf[1024];
  779. unsigned mlen = METRIC_ONLY_LEN;
  780. if (!valid_only_metric(unit))
  781. return;
  782. unit = fixunit(buf, os->evsel, unit);
  783. if (color)
  784. n = color_fprintf(out, color, fmt, val);
  785. else
  786. n = fprintf(out, fmt, val);
  787. if (n > METRIC_ONLY_LEN)
  788. n = METRIC_ONLY_LEN;
  789. if (mlen < strlen(unit))
  790. mlen = strlen(unit) + 1;
  791. fprintf(out, "%*s", mlen - n, "");
  792. }
  793. static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
  794. const char *fmt,
  795. const char *unit, double val)
  796. {
  797. struct outstate *os = ctx;
  798. FILE *out = os->fh;
  799. char buf[64], *vals, *ends;
  800. char tbuf[1024];
  801. if (!valid_only_metric(unit))
  802. return;
  803. unit = fixunit(tbuf, os->evsel, unit);
  804. snprintf(buf, sizeof buf, fmt, val);
  805. vals = buf;
  806. while (isspace(*vals))
  807. vals++;
  808. ends = vals;
  809. while (isdigit(*ends) || *ends == '.')
  810. ends++;
  811. *ends = 0;
  812. fprintf(out, "%s%s", vals, csv_sep);
  813. }
  814. static void new_line_metric(void *ctx __maybe_unused)
  815. {
  816. }
  817. static void print_metric_header(void *ctx, const char *color __maybe_unused,
  818. const char *fmt __maybe_unused,
  819. const char *unit, double val __maybe_unused)
  820. {
  821. struct outstate *os = ctx;
  822. char tbuf[1024];
  823. if (!valid_only_metric(unit))
  824. return;
  825. unit = fixunit(tbuf, os->evsel, unit);
  826. if (csv_output)
  827. fprintf(os->fh, "%s%s", unit, csv_sep);
  828. else
  829. fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
  830. }
  831. static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
  832. {
  833. FILE *output = stat_config.output;
  834. double msecs = avg / NSEC_PER_MSEC;
  835. const char *fmt_v, *fmt_n;
  836. char name[25];
  837. fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
  838. fmt_n = csv_output ? "%s" : "%-25s";
  839. aggr_printout(evsel, id, nr);
  840. scnprintf(name, sizeof(name), "%s%s",
  841. perf_evsel__name(evsel), csv_output ? "" : " (msec)");
  842. fprintf(output, fmt_v, msecs, csv_sep);
  843. if (csv_output)
  844. fprintf(output, "%s%s", evsel->unit, csv_sep);
  845. else
  846. fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
  847. fprintf(output, fmt_n, name);
  848. if (evsel->cgrp)
  849. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  850. }
  851. static int first_shadow_cpu(struct perf_evsel *evsel, int id)
  852. {
  853. int i;
  854. if (!aggr_get_id)
  855. return 0;
  856. if (stat_config.aggr_mode == AGGR_NONE)
  857. return id;
  858. if (stat_config.aggr_mode == AGGR_GLOBAL)
  859. return 0;
  860. for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
  861. int cpu2 = perf_evsel__cpus(evsel)->map[i];
  862. if (aggr_get_id(evsel_list->cpus, cpu2) == id)
  863. return cpu2;
  864. }
  865. return 0;
  866. }
  867. static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
  868. {
  869. FILE *output = stat_config.output;
  870. double sc = evsel->scale;
  871. const char *fmt;
  872. if (csv_output) {
  873. fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
  874. } else {
  875. if (big_num)
  876. fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
  877. else
  878. fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
  879. }
  880. aggr_printout(evsel, id, nr);
  881. fprintf(output, fmt, avg, csv_sep);
  882. if (evsel->unit)
  883. fprintf(output, "%-*s%s",
  884. csv_output ? 0 : unit_width,
  885. evsel->unit, csv_sep);
  886. fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
  887. if (evsel->cgrp)
  888. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  889. }
  890. static void printout(int id, int nr, struct perf_evsel *counter, double uval,
  891. char *prefix, u64 run, u64 ena, double noise)
  892. {
  893. struct perf_stat_output_ctx out;
  894. struct outstate os = {
  895. .fh = stat_config.output,
  896. .prefix = prefix ? prefix : "",
  897. .id = id,
  898. .nr = nr,
  899. .evsel = counter,
  900. };
  901. print_metric_t pm = print_metric_std;
  902. void (*nl)(void *);
  903. if (metric_only) {
  904. nl = new_line_metric;
  905. if (csv_output)
  906. pm = print_metric_only_csv;
  907. else
  908. pm = print_metric_only;
  909. } else
  910. nl = new_line_std;
  911. if (csv_output && !metric_only) {
  912. static int aggr_fields[] = {
  913. [AGGR_GLOBAL] = 0,
  914. [AGGR_THREAD] = 1,
  915. [AGGR_NONE] = 1,
  916. [AGGR_SOCKET] = 2,
  917. [AGGR_CORE] = 2,
  918. };
  919. pm = print_metric_csv;
  920. nl = new_line_csv;
  921. os.nfields = 3;
  922. os.nfields += aggr_fields[stat_config.aggr_mode];
  923. if (counter->cgrp)
  924. os.nfields++;
  925. }
  926. if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
  927. if (metric_only) {
  928. pm(&os, NULL, "", "", 0);
  929. return;
  930. }
  931. aggr_printout(counter, id, nr);
  932. fprintf(stat_config.output, "%*s%s",
  933. csv_output ? 0 : 18,
  934. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  935. csv_sep);
  936. fprintf(stat_config.output, "%-*s%s",
  937. csv_output ? 0 : unit_width,
  938. counter->unit, csv_sep);
  939. fprintf(stat_config.output, "%*s",
  940. csv_output ? 0 : -25,
  941. perf_evsel__name(counter));
  942. if (counter->cgrp)
  943. fprintf(stat_config.output, "%s%s",
  944. csv_sep, counter->cgrp->name);
  945. if (!csv_output)
  946. pm(&os, NULL, NULL, "", 0);
  947. print_noise(counter, noise);
  948. print_running(run, ena);
  949. if (csv_output)
  950. pm(&os, NULL, NULL, "", 0);
  951. return;
  952. }
  953. if (metric_only)
  954. /* nothing */;
  955. else if (nsec_counter(counter))
  956. nsec_printout(id, nr, counter, uval);
  957. else
  958. abs_printout(id, nr, counter, uval);
  959. out.print_metric = pm;
  960. out.new_line = nl;
  961. out.ctx = &os;
  962. if (csv_output && !metric_only) {
  963. print_noise(counter, noise);
  964. print_running(run, ena);
  965. }
  966. perf_stat__print_shadow_stats(counter, uval,
  967. first_shadow_cpu(counter, id),
  968. &out);
  969. if (!csv_output && !metric_only) {
  970. print_noise(counter, noise);
  971. print_running(run, ena);
  972. }
  973. }
  974. static void aggr_update_shadow(void)
  975. {
  976. int cpu, s2, id, s;
  977. u64 val;
  978. struct perf_evsel *counter;
  979. for (s = 0; s < aggr_map->nr; s++) {
  980. id = aggr_map->map[s];
  981. evlist__for_each_entry(evsel_list, counter) {
  982. val = 0;
  983. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  984. s2 = aggr_get_id(evsel_list->cpus, cpu);
  985. if (s2 != id)
  986. continue;
  987. val += perf_counts(counter->counts, cpu, 0)->val;
  988. }
  989. val = val * counter->scale;
  990. perf_stat__update_shadow_stats(counter, &val,
  991. first_shadow_cpu(counter, id));
  992. }
  993. }
  994. }
  995. static void print_aggr(char *prefix)
  996. {
  997. FILE *output = stat_config.output;
  998. struct perf_evsel *counter;
  999. int cpu, s, s2, id, nr;
  1000. double uval;
  1001. u64 ena, run, val;
  1002. bool first;
  1003. if (!(aggr_map || aggr_get_id))
  1004. return;
  1005. aggr_update_shadow();
  1006. /*
  1007. * With metric_only everything is on a single line.
  1008. * Without each counter has its own line.
  1009. */
  1010. for (s = 0; s < aggr_map->nr; s++) {
  1011. if (prefix && metric_only)
  1012. fprintf(output, "%s", prefix);
  1013. id = aggr_map->map[s];
  1014. first = true;
  1015. evlist__for_each_entry(evsel_list, counter) {
  1016. val = ena = run = 0;
  1017. nr = 0;
  1018. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  1019. s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
  1020. if (s2 != id)
  1021. continue;
  1022. val += perf_counts(counter->counts, cpu, 0)->val;
  1023. ena += perf_counts(counter->counts, cpu, 0)->ena;
  1024. run += perf_counts(counter->counts, cpu, 0)->run;
  1025. nr++;
  1026. }
  1027. if (first && metric_only) {
  1028. first = false;
  1029. aggr_printout(counter, id, nr);
  1030. }
  1031. if (prefix && !metric_only)
  1032. fprintf(output, "%s", prefix);
  1033. uval = val * counter->scale;
  1034. printout(id, nr, counter, uval, prefix, run, ena, 1.0);
  1035. if (!metric_only)
  1036. fputc('\n', output);
  1037. }
  1038. if (metric_only)
  1039. fputc('\n', output);
  1040. }
  1041. }
  1042. static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
  1043. {
  1044. FILE *output = stat_config.output;
  1045. int nthreads = thread_map__nr(counter->threads);
  1046. int ncpus = cpu_map__nr(counter->cpus);
  1047. int cpu, thread;
  1048. double uval;
  1049. for (thread = 0; thread < nthreads; thread++) {
  1050. u64 ena = 0, run = 0, val = 0;
  1051. for (cpu = 0; cpu < ncpus; cpu++) {
  1052. val += perf_counts(counter->counts, cpu, thread)->val;
  1053. ena += perf_counts(counter->counts, cpu, thread)->ena;
  1054. run += perf_counts(counter->counts, cpu, thread)->run;
  1055. }
  1056. if (prefix)
  1057. fprintf(output, "%s", prefix);
  1058. uval = val * counter->scale;
  1059. printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
  1060. fputc('\n', output);
  1061. }
  1062. }
  1063. /*
  1064. * Print out the results of a single counter:
  1065. * aggregated counts in system-wide mode
  1066. */
  1067. static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
  1068. {
  1069. FILE *output = stat_config.output;
  1070. struct perf_stat_evsel *ps = counter->priv;
  1071. double avg = avg_stats(&ps->res_stats[0]);
  1072. double uval;
  1073. double avg_enabled, avg_running;
  1074. avg_enabled = avg_stats(&ps->res_stats[1]);
  1075. avg_running = avg_stats(&ps->res_stats[2]);
  1076. if (prefix && !metric_only)
  1077. fprintf(output, "%s", prefix);
  1078. uval = avg * counter->scale;
  1079. printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
  1080. if (!metric_only)
  1081. fprintf(output, "\n");
  1082. }
  1083. /*
  1084. * Print out the results of a single counter:
  1085. * does not use aggregated count in system-wide
  1086. */
  1087. static void print_counter(struct perf_evsel *counter, char *prefix)
  1088. {
  1089. FILE *output = stat_config.output;
  1090. u64 ena, run, val;
  1091. double uval;
  1092. int cpu;
  1093. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  1094. val = perf_counts(counter->counts, cpu, 0)->val;
  1095. ena = perf_counts(counter->counts, cpu, 0)->ena;
  1096. run = perf_counts(counter->counts, cpu, 0)->run;
  1097. if (prefix)
  1098. fprintf(output, "%s", prefix);
  1099. uval = val * counter->scale;
  1100. printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
  1101. fputc('\n', output);
  1102. }
  1103. }
  1104. static void print_no_aggr_metric(char *prefix)
  1105. {
  1106. int cpu;
  1107. int nrcpus = 0;
  1108. struct perf_evsel *counter;
  1109. u64 ena, run, val;
  1110. double uval;
  1111. nrcpus = evsel_list->cpus->nr;
  1112. for (cpu = 0; cpu < nrcpus; cpu++) {
  1113. bool first = true;
  1114. if (prefix)
  1115. fputs(prefix, stat_config.output);
  1116. evlist__for_each_entry(evsel_list, counter) {
  1117. if (first) {
  1118. aggr_printout(counter, cpu, 0);
  1119. first = false;
  1120. }
  1121. val = perf_counts(counter->counts, cpu, 0)->val;
  1122. ena = perf_counts(counter->counts, cpu, 0)->ena;
  1123. run = perf_counts(counter->counts, cpu, 0)->run;
  1124. uval = val * counter->scale;
  1125. printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
  1126. }
  1127. fputc('\n', stat_config.output);
  1128. }
  1129. }
  1130. static int aggr_header_lens[] = {
  1131. [AGGR_CORE] = 18,
  1132. [AGGR_SOCKET] = 12,
  1133. [AGGR_NONE] = 6,
  1134. [AGGR_THREAD] = 24,
  1135. [AGGR_GLOBAL] = 0,
  1136. };
  1137. static const char *aggr_header_csv[] = {
  1138. [AGGR_CORE] = "core,cpus,",
  1139. [AGGR_SOCKET] = "socket,cpus",
  1140. [AGGR_NONE] = "cpu,",
  1141. [AGGR_THREAD] = "comm-pid,",
  1142. [AGGR_GLOBAL] = ""
  1143. };
  1144. static void print_metric_headers(const char *prefix, bool no_indent)
  1145. {
  1146. struct perf_stat_output_ctx out;
  1147. struct perf_evsel *counter;
  1148. struct outstate os = {
  1149. .fh = stat_config.output
  1150. };
  1151. if (prefix)
  1152. fprintf(stat_config.output, "%s", prefix);
  1153. if (!csv_output && !no_indent)
  1154. fprintf(stat_config.output, "%*s",
  1155. aggr_header_lens[stat_config.aggr_mode], "");
  1156. if (csv_output) {
  1157. if (stat_config.interval)
  1158. fputs("time,", stat_config.output);
  1159. fputs(aggr_header_csv[stat_config.aggr_mode],
  1160. stat_config.output);
  1161. }
  1162. /* Print metrics headers only */
  1163. evlist__for_each_entry(evsel_list, counter) {
  1164. os.evsel = counter;
  1165. out.ctx = &os;
  1166. out.print_metric = print_metric_header;
  1167. out.new_line = new_line_metric;
  1168. os.evsel = counter;
  1169. perf_stat__print_shadow_stats(counter, 0,
  1170. 0,
  1171. &out);
  1172. }
  1173. fputc('\n', stat_config.output);
  1174. }
  1175. static void print_interval(char *prefix, struct timespec *ts)
  1176. {
  1177. FILE *output = stat_config.output;
  1178. static int num_print_interval;
  1179. sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
  1180. if (num_print_interval == 0 && !csv_output) {
  1181. switch (stat_config.aggr_mode) {
  1182. case AGGR_SOCKET:
  1183. fprintf(output, "# time socket cpus");
  1184. if (!metric_only)
  1185. fprintf(output, " counts %*s events\n", unit_width, "unit");
  1186. break;
  1187. case AGGR_CORE:
  1188. fprintf(output, "# time core cpus");
  1189. if (!metric_only)
  1190. fprintf(output, " counts %*s events\n", unit_width, "unit");
  1191. break;
  1192. case AGGR_NONE:
  1193. fprintf(output, "# time CPU");
  1194. if (!metric_only)
  1195. fprintf(output, " counts %*s events\n", unit_width, "unit");
  1196. break;
  1197. case AGGR_THREAD:
  1198. fprintf(output, "# time comm-pid");
  1199. if (!metric_only)
  1200. fprintf(output, " counts %*s events\n", unit_width, "unit");
  1201. break;
  1202. case AGGR_GLOBAL:
  1203. default:
  1204. fprintf(output, "# time");
  1205. if (!metric_only)
  1206. fprintf(output, " counts %*s events\n", unit_width, "unit");
  1207. case AGGR_UNSET:
  1208. break;
  1209. }
  1210. }
  1211. if (num_print_interval == 0 && metric_only)
  1212. print_metric_headers(" ", true);
  1213. if (++num_print_interval == 25)
  1214. num_print_interval = 0;
  1215. }
  1216. static void print_header(int argc, const char **argv)
  1217. {
  1218. FILE *output = stat_config.output;
  1219. int i;
  1220. fflush(stdout);
  1221. if (!csv_output) {
  1222. fprintf(output, "\n");
  1223. fprintf(output, " Performance counter stats for ");
  1224. if (target.system_wide)
  1225. fprintf(output, "\'system wide");
  1226. else if (target.cpu_list)
  1227. fprintf(output, "\'CPU(s) %s", target.cpu_list);
  1228. else if (!target__has_task(&target)) {
  1229. fprintf(output, "\'%s", argv ? argv[0] : "pipe");
  1230. for (i = 1; argv && (i < argc); i++)
  1231. fprintf(output, " %s", argv[i]);
  1232. } else if (target.pid)
  1233. fprintf(output, "process id \'%s", target.pid);
  1234. else
  1235. fprintf(output, "thread id \'%s", target.tid);
  1236. fprintf(output, "\'");
  1237. if (run_count > 1)
  1238. fprintf(output, " (%d runs)", run_count);
  1239. fprintf(output, ":\n\n");
  1240. }
  1241. }
  1242. static void print_footer(void)
  1243. {
  1244. FILE *output = stat_config.output;
  1245. if (!null_run)
  1246. fprintf(output, "\n");
  1247. fprintf(output, " %17.9f seconds time elapsed",
  1248. avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
  1249. if (run_count > 1) {
  1250. fprintf(output, " ");
  1251. print_noise_pct(stddev_stats(&walltime_nsecs_stats),
  1252. avg_stats(&walltime_nsecs_stats));
  1253. }
  1254. fprintf(output, "\n\n");
  1255. }
  1256. static void print_counters(struct timespec *ts, int argc, const char **argv)
  1257. {
  1258. int interval = stat_config.interval;
  1259. struct perf_evsel *counter;
  1260. char buf[64], *prefix = NULL;
  1261. /* Do not print anything if we record to the pipe. */
  1262. if (STAT_RECORD && perf_stat.file.is_pipe)
  1263. return;
  1264. if (interval)
  1265. print_interval(prefix = buf, ts);
  1266. else
  1267. print_header(argc, argv);
  1268. if (metric_only) {
  1269. static int num_print_iv;
  1270. if (num_print_iv == 0 && !interval)
  1271. print_metric_headers(prefix, false);
  1272. if (num_print_iv++ == 25)
  1273. num_print_iv = 0;
  1274. if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
  1275. fprintf(stat_config.output, "%s", prefix);
  1276. }
  1277. switch (stat_config.aggr_mode) {
  1278. case AGGR_CORE:
  1279. case AGGR_SOCKET:
  1280. print_aggr(prefix);
  1281. break;
  1282. case AGGR_THREAD:
  1283. evlist__for_each_entry(evsel_list, counter)
  1284. print_aggr_thread(counter, prefix);
  1285. break;
  1286. case AGGR_GLOBAL:
  1287. evlist__for_each_entry(evsel_list, counter)
  1288. print_counter_aggr(counter, prefix);
  1289. if (metric_only)
  1290. fputc('\n', stat_config.output);
  1291. break;
  1292. case AGGR_NONE:
  1293. if (metric_only)
  1294. print_no_aggr_metric(prefix);
  1295. else {
  1296. evlist__for_each_entry(evsel_list, counter)
  1297. print_counter(counter, prefix);
  1298. }
  1299. break;
  1300. case AGGR_UNSET:
  1301. default:
  1302. break;
  1303. }
  1304. if (!interval && !csv_output)
  1305. print_footer();
  1306. fflush(stat_config.output);
  1307. }
  1308. static volatile int signr = -1;
  1309. static void skip_signal(int signo)
  1310. {
  1311. if ((child_pid == -1) || stat_config.interval)
  1312. done = 1;
  1313. signr = signo;
  1314. /*
  1315. * render child_pid harmless
  1316. * won't send SIGTERM to a random
  1317. * process in case of race condition
  1318. * and fast PID recycling
  1319. */
  1320. child_pid = -1;
  1321. }
  1322. static void sig_atexit(void)
  1323. {
  1324. sigset_t set, oset;
  1325. /*
  1326. * avoid race condition with SIGCHLD handler
  1327. * in skip_signal() which is modifying child_pid
  1328. * goal is to avoid send SIGTERM to a random
  1329. * process
  1330. */
  1331. sigemptyset(&set);
  1332. sigaddset(&set, SIGCHLD);
  1333. sigprocmask(SIG_BLOCK, &set, &oset);
  1334. if (child_pid != -1)
  1335. kill(child_pid, SIGTERM);
  1336. sigprocmask(SIG_SETMASK, &oset, NULL);
  1337. if (signr == -1)
  1338. return;
  1339. signal(signr, SIG_DFL);
  1340. kill(getpid(), signr);
  1341. }
  1342. static int stat__set_big_num(const struct option *opt __maybe_unused,
  1343. const char *s __maybe_unused, int unset)
  1344. {
  1345. big_num_opt = unset ? 0 : 1;
  1346. return 0;
  1347. }
  1348. static int enable_metric_only(const struct option *opt __maybe_unused,
  1349. const char *s __maybe_unused, int unset)
  1350. {
  1351. force_metric_only = true;
  1352. metric_only = !unset;
  1353. return 0;
  1354. }
  1355. static const struct option stat_options[] = {
  1356. OPT_BOOLEAN('T', "transaction", &transaction_run,
  1357. "hardware transaction statistics"),
  1358. OPT_CALLBACK('e', "event", &evsel_list, "event",
  1359. "event selector. use 'perf list' to list available events",
  1360. parse_events_option),
  1361. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  1362. "event filter", parse_filter),
  1363. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  1364. "child tasks do not inherit counters"),
  1365. OPT_STRING('p', "pid", &target.pid, "pid",
  1366. "stat events on existing process id"),
  1367. OPT_STRING('t', "tid", &target.tid, "tid",
  1368. "stat events on existing thread id"),
  1369. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  1370. "system-wide collection from all CPUs"),
  1371. OPT_BOOLEAN('g', "group", &group,
  1372. "put the counters into a counter group"),
  1373. OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
  1374. OPT_INCR('v', "verbose", &verbose,
  1375. "be more verbose (show counter open errors, etc)"),
  1376. OPT_INTEGER('r', "repeat", &run_count,
  1377. "repeat command and print average + stddev (max: 100, forever: 0)"),
  1378. OPT_BOOLEAN('n', "null", &null_run,
  1379. "null run - dont start any counters"),
  1380. OPT_INCR('d', "detailed", &detailed_run,
  1381. "detailed run - start a lot of events"),
  1382. OPT_BOOLEAN('S', "sync", &sync_run,
  1383. "call sync() before starting a run"),
  1384. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  1385. "print large numbers with thousands\' separators",
  1386. stat__set_big_num),
  1387. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  1388. "list of cpus to monitor in system-wide"),
  1389. OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
  1390. "disable CPU count aggregation", AGGR_NONE),
  1391. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  1392. "print counts with custom separator"),
  1393. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  1394. "monitor event in cgroup name only", parse_cgroups),
  1395. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1396. OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
  1397. OPT_INTEGER(0, "log-fd", &output_fd,
  1398. "log output to fd, instead of stderr"),
  1399. OPT_STRING(0, "pre", &pre_cmd, "command",
  1400. "command to run prior to the measured command"),
  1401. OPT_STRING(0, "post", &post_cmd, "command",
  1402. "command to run after to the measured command"),
  1403. OPT_UINTEGER('I', "interval-print", &stat_config.interval,
  1404. "print counts at regular interval in ms (>= 10)"),
  1405. OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
  1406. "aggregate counts per processor socket", AGGR_SOCKET),
  1407. OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
  1408. "aggregate counts per physical processor core", AGGR_CORE),
  1409. OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
  1410. "aggregate counts per thread", AGGR_THREAD),
  1411. OPT_UINTEGER('D', "delay", &initial_delay,
  1412. "ms to wait before starting measurement after program start"),
  1413. OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
  1414. "Only print computed metrics. No raw values", enable_metric_only),
  1415. OPT_BOOLEAN(0, "topdown", &topdown_run,
  1416. "measure topdown level 1 statistics"),
  1417. OPT_END()
  1418. };
  1419. static int perf_stat__get_socket(struct cpu_map *map, int cpu)
  1420. {
  1421. return cpu_map__get_socket(map, cpu, NULL);
  1422. }
  1423. static int perf_stat__get_core(struct cpu_map *map, int cpu)
  1424. {
  1425. return cpu_map__get_core(map, cpu, NULL);
  1426. }
  1427. static int cpu_map__get_max(struct cpu_map *map)
  1428. {
  1429. int i, max = -1;
  1430. for (i = 0; i < map->nr; i++) {
  1431. if (map->map[i] > max)
  1432. max = map->map[i];
  1433. }
  1434. return max;
  1435. }
  1436. static struct cpu_map *cpus_aggr_map;
  1437. static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
  1438. {
  1439. int cpu;
  1440. if (idx >= map->nr)
  1441. return -1;
  1442. cpu = map->map[idx];
  1443. if (cpus_aggr_map->map[cpu] == -1)
  1444. cpus_aggr_map->map[cpu] = get_id(map, idx);
  1445. return cpus_aggr_map->map[cpu];
  1446. }
  1447. static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
  1448. {
  1449. return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
  1450. }
  1451. static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
  1452. {
  1453. return perf_stat__get_aggr(perf_stat__get_core, map, idx);
  1454. }
  1455. static int perf_stat_init_aggr_mode(void)
  1456. {
  1457. int nr;
  1458. switch (stat_config.aggr_mode) {
  1459. case AGGR_SOCKET:
  1460. if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
  1461. perror("cannot build socket map");
  1462. return -1;
  1463. }
  1464. aggr_get_id = perf_stat__get_socket_cached;
  1465. break;
  1466. case AGGR_CORE:
  1467. if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
  1468. perror("cannot build core map");
  1469. return -1;
  1470. }
  1471. aggr_get_id = perf_stat__get_core_cached;
  1472. break;
  1473. case AGGR_NONE:
  1474. case AGGR_GLOBAL:
  1475. case AGGR_THREAD:
  1476. case AGGR_UNSET:
  1477. default:
  1478. break;
  1479. }
  1480. /*
  1481. * The evsel_list->cpus is the base we operate on,
  1482. * taking the highest cpu number to be the size of
  1483. * the aggregation translate cpumap.
  1484. */
  1485. nr = cpu_map__get_max(evsel_list->cpus);
  1486. cpus_aggr_map = cpu_map__empty_new(nr + 1);
  1487. return cpus_aggr_map ? 0 : -ENOMEM;
  1488. }
  1489. static void perf_stat__exit_aggr_mode(void)
  1490. {
  1491. cpu_map__put(aggr_map);
  1492. cpu_map__put(cpus_aggr_map);
  1493. aggr_map = NULL;
  1494. cpus_aggr_map = NULL;
  1495. }
  1496. static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
  1497. {
  1498. int cpu;
  1499. if (idx > map->nr)
  1500. return -1;
  1501. cpu = map->map[idx];
  1502. if (cpu >= env->nr_cpus_online)
  1503. return -1;
  1504. return cpu;
  1505. }
  1506. static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
  1507. {
  1508. struct perf_env *env = data;
  1509. int cpu = perf_env__get_cpu(env, map, idx);
  1510. return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
  1511. }
  1512. static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
  1513. {
  1514. struct perf_env *env = data;
  1515. int core = -1, cpu = perf_env__get_cpu(env, map, idx);
  1516. if (cpu != -1) {
  1517. int socket_id = env->cpu[cpu].socket_id;
  1518. /*
  1519. * Encode socket in upper 16 bits
  1520. * core_id is relative to socket, and
  1521. * we need a global id. So we combine
  1522. * socket + core id.
  1523. */
  1524. core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
  1525. }
  1526. return core;
  1527. }
  1528. static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
  1529. struct cpu_map **sockp)
  1530. {
  1531. return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
  1532. }
  1533. static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
  1534. struct cpu_map **corep)
  1535. {
  1536. return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
  1537. }
  1538. static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
  1539. {
  1540. return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
  1541. }
  1542. static int perf_stat__get_core_file(struct cpu_map *map, int idx)
  1543. {
  1544. return perf_env__get_core(map, idx, &perf_stat.session->header.env);
  1545. }
  1546. static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
  1547. {
  1548. struct perf_env *env = &st->session->header.env;
  1549. switch (stat_config.aggr_mode) {
  1550. case AGGR_SOCKET:
  1551. if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
  1552. perror("cannot build socket map");
  1553. return -1;
  1554. }
  1555. aggr_get_id = perf_stat__get_socket_file;
  1556. break;
  1557. case AGGR_CORE:
  1558. if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
  1559. perror("cannot build core map");
  1560. return -1;
  1561. }
  1562. aggr_get_id = perf_stat__get_core_file;
  1563. break;
  1564. case AGGR_NONE:
  1565. case AGGR_GLOBAL:
  1566. case AGGR_THREAD:
  1567. case AGGR_UNSET:
  1568. default:
  1569. break;
  1570. }
  1571. return 0;
  1572. }
  1573. static int topdown_filter_events(const char **attr, char **str, bool use_group)
  1574. {
  1575. int off = 0;
  1576. int i;
  1577. int len = 0;
  1578. char *s;
  1579. for (i = 0; attr[i]; i++) {
  1580. if (pmu_have_event("cpu", attr[i])) {
  1581. len += strlen(attr[i]) + 1;
  1582. attr[i - off] = attr[i];
  1583. } else
  1584. off++;
  1585. }
  1586. attr[i - off] = NULL;
  1587. *str = malloc(len + 1 + 2);
  1588. if (!*str)
  1589. return -1;
  1590. s = *str;
  1591. if (i - off == 0) {
  1592. *s = 0;
  1593. return 0;
  1594. }
  1595. if (use_group)
  1596. *s++ = '{';
  1597. for (i = 0; attr[i]; i++) {
  1598. strcpy(s, attr[i]);
  1599. s += strlen(s);
  1600. *s++ = ',';
  1601. }
  1602. if (use_group) {
  1603. s[-1] = '}';
  1604. *s = 0;
  1605. } else
  1606. s[-1] = 0;
  1607. return 0;
  1608. }
  1609. __weak bool arch_topdown_check_group(bool *warn)
  1610. {
  1611. *warn = false;
  1612. return false;
  1613. }
  1614. __weak void arch_topdown_group_warn(void)
  1615. {
  1616. }
  1617. /*
  1618. * Add default attributes, if there were no attributes specified or
  1619. * if -d/--detailed, -d -d or -d -d -d is used:
  1620. */
  1621. static int add_default_attributes(void)
  1622. {
  1623. int err;
  1624. struct perf_event_attr default_attrs0[] = {
  1625. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  1626. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  1627. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  1628. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  1629. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  1630. };
  1631. struct perf_event_attr frontend_attrs[] = {
  1632. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  1633. };
  1634. struct perf_event_attr backend_attrs[] = {
  1635. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  1636. };
  1637. struct perf_event_attr default_attrs1[] = {
  1638. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  1639. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  1640. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  1641. };
  1642. /*
  1643. * Detailed stats (-d), covering the L1 and last level data caches:
  1644. */
  1645. struct perf_event_attr detailed_attrs[] = {
  1646. { .type = PERF_TYPE_HW_CACHE,
  1647. .config =
  1648. PERF_COUNT_HW_CACHE_L1D << 0 |
  1649. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1650. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1651. { .type = PERF_TYPE_HW_CACHE,
  1652. .config =
  1653. PERF_COUNT_HW_CACHE_L1D << 0 |
  1654. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1655. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1656. { .type = PERF_TYPE_HW_CACHE,
  1657. .config =
  1658. PERF_COUNT_HW_CACHE_LL << 0 |
  1659. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1660. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1661. { .type = PERF_TYPE_HW_CACHE,
  1662. .config =
  1663. PERF_COUNT_HW_CACHE_LL << 0 |
  1664. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1665. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1666. };
  1667. /*
  1668. * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
  1669. */
  1670. struct perf_event_attr very_detailed_attrs[] = {
  1671. { .type = PERF_TYPE_HW_CACHE,
  1672. .config =
  1673. PERF_COUNT_HW_CACHE_L1I << 0 |
  1674. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1675. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1676. { .type = PERF_TYPE_HW_CACHE,
  1677. .config =
  1678. PERF_COUNT_HW_CACHE_L1I << 0 |
  1679. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1680. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1681. { .type = PERF_TYPE_HW_CACHE,
  1682. .config =
  1683. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1684. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1685. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1686. { .type = PERF_TYPE_HW_CACHE,
  1687. .config =
  1688. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1689. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1690. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1691. { .type = PERF_TYPE_HW_CACHE,
  1692. .config =
  1693. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1694. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1695. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1696. { .type = PERF_TYPE_HW_CACHE,
  1697. .config =
  1698. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1699. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1700. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1701. };
  1702. /*
  1703. * Very, very detailed stats (-d -d -d), adding prefetch events:
  1704. */
  1705. struct perf_event_attr very_very_detailed_attrs[] = {
  1706. { .type = PERF_TYPE_HW_CACHE,
  1707. .config =
  1708. PERF_COUNT_HW_CACHE_L1D << 0 |
  1709. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1710. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1711. { .type = PERF_TYPE_HW_CACHE,
  1712. .config =
  1713. PERF_COUNT_HW_CACHE_L1D << 0 |
  1714. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1715. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1716. };
  1717. /* Set attrs if no event is selected and !null_run: */
  1718. if (null_run)
  1719. return 0;
  1720. if (transaction_run) {
  1721. if (pmu_have_event("cpu", "cycles-ct") &&
  1722. pmu_have_event("cpu", "el-start"))
  1723. err = parse_events(evsel_list, transaction_attrs, NULL);
  1724. else
  1725. err = parse_events(evsel_list, transaction_limited_attrs, NULL);
  1726. if (err) {
  1727. fprintf(stderr, "Cannot set up transaction events\n");
  1728. return -1;
  1729. }
  1730. return 0;
  1731. }
  1732. if (topdown_run) {
  1733. char *str = NULL;
  1734. bool warn = false;
  1735. if (stat_config.aggr_mode != AGGR_GLOBAL &&
  1736. stat_config.aggr_mode != AGGR_CORE) {
  1737. pr_err("top down event configuration requires --per-core mode\n");
  1738. return -1;
  1739. }
  1740. stat_config.aggr_mode = AGGR_CORE;
  1741. if (nr_cgroups || !target__has_cpu(&target)) {
  1742. pr_err("top down event configuration requires system-wide mode (-a)\n");
  1743. return -1;
  1744. }
  1745. if (!force_metric_only)
  1746. metric_only = true;
  1747. if (topdown_filter_events(topdown_attrs, &str,
  1748. arch_topdown_check_group(&warn)) < 0) {
  1749. pr_err("Out of memory\n");
  1750. return -1;
  1751. }
  1752. if (topdown_attrs[0] && str) {
  1753. if (warn)
  1754. arch_topdown_group_warn();
  1755. err = parse_events(evsel_list, str, NULL);
  1756. if (err) {
  1757. fprintf(stderr,
  1758. "Cannot set up top down events %s: %d\n",
  1759. str, err);
  1760. free(str);
  1761. return -1;
  1762. }
  1763. } else {
  1764. fprintf(stderr, "System does not support topdown\n");
  1765. return -1;
  1766. }
  1767. free(str);
  1768. }
  1769. if (!evsel_list->nr_entries) {
  1770. if (target__has_cpu(&target))
  1771. default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
  1772. if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
  1773. return -1;
  1774. if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
  1775. if (perf_evlist__add_default_attrs(evsel_list,
  1776. frontend_attrs) < 0)
  1777. return -1;
  1778. }
  1779. if (pmu_have_event("cpu", "stalled-cycles-backend")) {
  1780. if (perf_evlist__add_default_attrs(evsel_list,
  1781. backend_attrs) < 0)
  1782. return -1;
  1783. }
  1784. if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
  1785. return -1;
  1786. }
  1787. /* Detailed events get appended to the event list: */
  1788. if (detailed_run < 1)
  1789. return 0;
  1790. /* Append detailed run extra attributes: */
  1791. if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
  1792. return -1;
  1793. if (detailed_run < 2)
  1794. return 0;
  1795. /* Append very detailed run extra attributes: */
  1796. if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
  1797. return -1;
  1798. if (detailed_run < 3)
  1799. return 0;
  1800. /* Append very, very detailed run extra attributes: */
  1801. return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
  1802. }
  1803. static const char * const stat_record_usage[] = {
  1804. "perf stat record [<options>]",
  1805. NULL,
  1806. };
  1807. static void init_features(struct perf_session *session)
  1808. {
  1809. int feat;
  1810. for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
  1811. perf_header__set_feat(&session->header, feat);
  1812. perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
  1813. perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
  1814. perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
  1815. perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
  1816. }
  1817. static int __cmd_record(int argc, const char **argv)
  1818. {
  1819. struct perf_session *session;
  1820. struct perf_data_file *file = &perf_stat.file;
  1821. argc = parse_options(argc, argv, stat_options, stat_record_usage,
  1822. PARSE_OPT_STOP_AT_NON_OPTION);
  1823. if (output_name)
  1824. file->path = output_name;
  1825. if (run_count != 1 || forever) {
  1826. pr_err("Cannot use -r option with perf stat record.\n");
  1827. return -1;
  1828. }
  1829. session = perf_session__new(file, false, NULL);
  1830. if (session == NULL) {
  1831. pr_err("Perf session creation failed.\n");
  1832. return -1;
  1833. }
  1834. init_features(session);
  1835. session->evlist = evsel_list;
  1836. perf_stat.session = session;
  1837. perf_stat.record = true;
  1838. return argc;
  1839. }
  1840. static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
  1841. union perf_event *event,
  1842. struct perf_session *session)
  1843. {
  1844. struct stat_round_event *stat_round = &event->stat_round;
  1845. struct perf_evsel *counter;
  1846. struct timespec tsh, *ts = NULL;
  1847. const char **argv = session->header.env.cmdline_argv;
  1848. int argc = session->header.env.nr_cmdline;
  1849. evlist__for_each_entry(evsel_list, counter)
  1850. perf_stat_process_counter(&stat_config, counter);
  1851. if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
  1852. update_stats(&walltime_nsecs_stats, stat_round->time);
  1853. if (stat_config.interval && stat_round->time) {
  1854. tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
  1855. tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
  1856. ts = &tsh;
  1857. }
  1858. print_counters(ts, argc, argv);
  1859. return 0;
  1860. }
  1861. static
  1862. int process_stat_config_event(struct perf_tool *tool __maybe_unused,
  1863. union perf_event *event,
  1864. struct perf_session *session __maybe_unused)
  1865. {
  1866. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1867. perf_event__read_stat_config(&stat_config, &event->stat_config);
  1868. if (cpu_map__empty(st->cpus)) {
  1869. if (st->aggr_mode != AGGR_UNSET)
  1870. pr_warning("warning: processing task data, aggregation mode not set\n");
  1871. return 0;
  1872. }
  1873. if (st->aggr_mode != AGGR_UNSET)
  1874. stat_config.aggr_mode = st->aggr_mode;
  1875. if (perf_stat.file.is_pipe)
  1876. perf_stat_init_aggr_mode();
  1877. else
  1878. perf_stat_init_aggr_mode_file(st);
  1879. return 0;
  1880. }
  1881. static int set_maps(struct perf_stat *st)
  1882. {
  1883. if (!st->cpus || !st->threads)
  1884. return 0;
  1885. if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
  1886. return -EINVAL;
  1887. perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
  1888. if (perf_evlist__alloc_stats(evsel_list, true))
  1889. return -ENOMEM;
  1890. st->maps_allocated = true;
  1891. return 0;
  1892. }
  1893. static
  1894. int process_thread_map_event(struct perf_tool *tool __maybe_unused,
  1895. union perf_event *event,
  1896. struct perf_session *session __maybe_unused)
  1897. {
  1898. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1899. if (st->threads) {
  1900. pr_warning("Extra thread map event, ignoring.\n");
  1901. return 0;
  1902. }
  1903. st->threads = thread_map__new_event(&event->thread_map);
  1904. if (!st->threads)
  1905. return -ENOMEM;
  1906. return set_maps(st);
  1907. }
  1908. static
  1909. int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
  1910. union perf_event *event,
  1911. struct perf_session *session __maybe_unused)
  1912. {
  1913. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1914. struct cpu_map *cpus;
  1915. if (st->cpus) {
  1916. pr_warning("Extra cpu map event, ignoring.\n");
  1917. return 0;
  1918. }
  1919. cpus = cpu_map__new_data(&event->cpu_map.data);
  1920. if (!cpus)
  1921. return -ENOMEM;
  1922. st->cpus = cpus;
  1923. return set_maps(st);
  1924. }
  1925. static const char * const stat_report_usage[] = {
  1926. "perf stat report [<options>]",
  1927. NULL,
  1928. };
  1929. static struct perf_stat perf_stat = {
  1930. .tool = {
  1931. .attr = perf_event__process_attr,
  1932. .event_update = perf_event__process_event_update,
  1933. .thread_map = process_thread_map_event,
  1934. .cpu_map = process_cpu_map_event,
  1935. .stat_config = process_stat_config_event,
  1936. .stat = perf_event__process_stat_event,
  1937. .stat_round = process_stat_round_event,
  1938. },
  1939. .aggr_mode = AGGR_UNSET,
  1940. };
  1941. static int __cmd_report(int argc, const char **argv)
  1942. {
  1943. struct perf_session *session;
  1944. const struct option options[] = {
  1945. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1946. OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
  1947. "aggregate counts per processor socket", AGGR_SOCKET),
  1948. OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
  1949. "aggregate counts per physical processor core", AGGR_CORE),
  1950. OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
  1951. "disable CPU count aggregation", AGGR_NONE),
  1952. OPT_END()
  1953. };
  1954. struct stat st;
  1955. int ret;
  1956. argc = parse_options(argc, argv, options, stat_report_usage, 0);
  1957. if (!input_name || !strlen(input_name)) {
  1958. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  1959. input_name = "-";
  1960. else
  1961. input_name = "perf.data";
  1962. }
  1963. perf_stat.file.path = input_name;
  1964. perf_stat.file.mode = PERF_DATA_MODE_READ;
  1965. session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
  1966. if (session == NULL)
  1967. return -1;
  1968. perf_stat.session = session;
  1969. stat_config.output = stderr;
  1970. evsel_list = session->evlist;
  1971. ret = perf_session__process_events(session);
  1972. if (ret)
  1973. return ret;
  1974. perf_session__delete(session);
  1975. return 0;
  1976. }
  1977. int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
  1978. {
  1979. const char * const stat_usage[] = {
  1980. "perf stat [<options>] [<command>]",
  1981. NULL
  1982. };
  1983. int status = -EINVAL, run_idx;
  1984. const char *mode;
  1985. FILE *output = stderr;
  1986. unsigned int interval;
  1987. const char * const stat_subcommands[] = { "record", "report" };
  1988. setlocale(LC_ALL, "");
  1989. evsel_list = perf_evlist__new();
  1990. if (evsel_list == NULL)
  1991. return -ENOMEM;
  1992. parse_events__shrink_config_terms();
  1993. argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
  1994. (const char **) stat_usage,
  1995. PARSE_OPT_STOP_AT_NON_OPTION);
  1996. perf_stat__init_shadow_stats();
  1997. if (csv_sep) {
  1998. csv_output = true;
  1999. if (!strcmp(csv_sep, "\\t"))
  2000. csv_sep = "\t";
  2001. } else
  2002. csv_sep = DEFAULT_SEPARATOR;
  2003. if (argc && !strncmp(argv[0], "rec", 3)) {
  2004. argc = __cmd_record(argc, argv);
  2005. if (argc < 0)
  2006. return -1;
  2007. } else if (argc && !strncmp(argv[0], "rep", 3))
  2008. return __cmd_report(argc, argv);
  2009. interval = stat_config.interval;
  2010. /*
  2011. * For record command the -o is already taken care of.
  2012. */
  2013. if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
  2014. output = NULL;
  2015. if (output_name && output_fd) {
  2016. fprintf(stderr, "cannot use both --output and --log-fd\n");
  2017. parse_options_usage(stat_usage, stat_options, "o", 1);
  2018. parse_options_usage(NULL, stat_options, "log-fd", 0);
  2019. goto out;
  2020. }
  2021. if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
  2022. fprintf(stderr, "--metric-only is not supported with --per-thread\n");
  2023. goto out;
  2024. }
  2025. if (metric_only && run_count > 1) {
  2026. fprintf(stderr, "--metric-only is not supported with -r\n");
  2027. goto out;
  2028. }
  2029. if (output_fd < 0) {
  2030. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  2031. parse_options_usage(stat_usage, stat_options, "log-fd", 0);
  2032. goto out;
  2033. }
  2034. if (!output) {
  2035. struct timespec tm;
  2036. mode = append_file ? "a" : "w";
  2037. output = fopen(output_name, mode);
  2038. if (!output) {
  2039. perror("failed to create output file");
  2040. return -1;
  2041. }
  2042. clock_gettime(CLOCK_REALTIME, &tm);
  2043. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  2044. } else if (output_fd > 0) {
  2045. mode = append_file ? "a" : "w";
  2046. output = fdopen(output_fd, mode);
  2047. if (!output) {
  2048. perror("Failed opening logfd");
  2049. return -errno;
  2050. }
  2051. }
  2052. stat_config.output = output;
  2053. /*
  2054. * let the spreadsheet do the pretty-printing
  2055. */
  2056. if (csv_output) {
  2057. /* User explicitly passed -B? */
  2058. if (big_num_opt == 1) {
  2059. fprintf(stderr, "-B option not supported with -x\n");
  2060. parse_options_usage(stat_usage, stat_options, "B", 1);
  2061. parse_options_usage(NULL, stat_options, "x", 1);
  2062. goto out;
  2063. } else /* Nope, so disable big number formatting */
  2064. big_num = false;
  2065. } else if (big_num_opt == 0) /* User passed --no-big-num */
  2066. big_num = false;
  2067. if (!argc && target__none(&target))
  2068. usage_with_options(stat_usage, stat_options);
  2069. if (run_count < 0) {
  2070. pr_err("Run count must be a positive number\n");
  2071. parse_options_usage(stat_usage, stat_options, "r", 1);
  2072. goto out;
  2073. } else if (run_count == 0) {
  2074. forever = true;
  2075. run_count = 1;
  2076. }
  2077. if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
  2078. fprintf(stderr, "The --per-thread option is only available "
  2079. "when monitoring via -p -t options.\n");
  2080. parse_options_usage(NULL, stat_options, "p", 1);
  2081. parse_options_usage(NULL, stat_options, "t", 1);
  2082. goto out;
  2083. }
  2084. /*
  2085. * no_aggr, cgroup are for system-wide only
  2086. * --per-thread is aggregated per thread, we dont mix it with cpu mode
  2087. */
  2088. if (((stat_config.aggr_mode != AGGR_GLOBAL &&
  2089. stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
  2090. !target__has_cpu(&target)) {
  2091. fprintf(stderr, "both cgroup and no-aggregation "
  2092. "modes only available in system-wide mode\n");
  2093. parse_options_usage(stat_usage, stat_options, "G", 1);
  2094. parse_options_usage(NULL, stat_options, "A", 1);
  2095. parse_options_usage(NULL, stat_options, "a", 1);
  2096. goto out;
  2097. }
  2098. if (add_default_attributes())
  2099. goto out;
  2100. target__validate(&target);
  2101. if (perf_evlist__create_maps(evsel_list, &target) < 0) {
  2102. if (target__has_task(&target)) {
  2103. pr_err("Problems finding threads of monitor\n");
  2104. parse_options_usage(stat_usage, stat_options, "p", 1);
  2105. parse_options_usage(NULL, stat_options, "t", 1);
  2106. } else if (target__has_cpu(&target)) {
  2107. perror("failed to parse CPUs map");
  2108. parse_options_usage(stat_usage, stat_options, "C", 1);
  2109. parse_options_usage(NULL, stat_options, "a", 1);
  2110. }
  2111. goto out;
  2112. }
  2113. /*
  2114. * Initialize thread_map with comm names,
  2115. * so we could print it out on output.
  2116. */
  2117. if (stat_config.aggr_mode == AGGR_THREAD)
  2118. thread_map__read_comms(evsel_list->threads);
  2119. if (interval && interval < 100) {
  2120. if (interval < 10) {
  2121. pr_err("print interval must be >= 10ms\n");
  2122. parse_options_usage(stat_usage, stat_options, "I", 1);
  2123. goto out;
  2124. } else
  2125. pr_warning("print interval < 100ms. "
  2126. "The overhead percentage could be high in some cases. "
  2127. "Please proceed with caution.\n");
  2128. }
  2129. if (perf_evlist__alloc_stats(evsel_list, interval))
  2130. goto out;
  2131. if (perf_stat_init_aggr_mode())
  2132. goto out;
  2133. /*
  2134. * We dont want to block the signals - that would cause
  2135. * child tasks to inherit that and Ctrl-C would not work.
  2136. * What we want is for Ctrl-C to work in the exec()-ed
  2137. * task, but being ignored by perf stat itself:
  2138. */
  2139. atexit(sig_atexit);
  2140. if (!forever)
  2141. signal(SIGINT, skip_signal);
  2142. signal(SIGCHLD, skip_signal);
  2143. signal(SIGALRM, skip_signal);
  2144. signal(SIGABRT, skip_signal);
  2145. status = 0;
  2146. for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
  2147. if (run_count != 1 && verbose)
  2148. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  2149. run_idx + 1);
  2150. status = run_perf_stat(argc, argv);
  2151. if (forever && status != -1) {
  2152. print_counters(NULL, argc, argv);
  2153. perf_stat__reset_stats();
  2154. }
  2155. }
  2156. if (!forever && status != -1 && !interval)
  2157. print_counters(NULL, argc, argv);
  2158. if (STAT_RECORD) {
  2159. /*
  2160. * We synthesize the kernel mmap record just so that older tools
  2161. * don't emit warnings about not being able to resolve symbols
  2162. * due to /proc/sys/kernel/kptr_restrict settings and instear provide
  2163. * a saner message about no samples being in the perf.data file.
  2164. *
  2165. * This also serves to suppress a warning about f_header.data.size == 0
  2166. * in header.c at the moment 'perf stat record' gets introduced, which
  2167. * is not really needed once we start adding the stat specific PERF_RECORD_
  2168. * records, but the need to suppress the kptr_restrict messages in older
  2169. * tools remain -acme
  2170. */
  2171. int fd = perf_data_file__fd(&perf_stat.file);
  2172. int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
  2173. process_synthesized_event,
  2174. &perf_stat.session->machines.host);
  2175. if (err) {
  2176. pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
  2177. "older tools may produce warnings about this file\n.");
  2178. }
  2179. if (!interval) {
  2180. if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
  2181. pr_err("failed to write stat round event\n");
  2182. }
  2183. if (!perf_stat.file.is_pipe) {
  2184. perf_stat.session->header.data_size += perf_stat.bytes_written;
  2185. perf_session__write_header(perf_stat.session, evsel_list, fd, true);
  2186. }
  2187. perf_session__delete(perf_stat.session);
  2188. }
  2189. perf_stat__exit_aggr_mode();
  2190. perf_evlist__free_stats(evsel_list);
  2191. out:
  2192. perf_evlist__delete(evsel_list);
  2193. return status;
  2194. }