builtin-stat.c 64 KB

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