builtin-stat.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  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/color.h"
  49. #include "util/stat.h"
  50. #include "util/header.h"
  51. #include "util/cpumap.h"
  52. #include "util/thread.h"
  53. #include "util/thread_map.h"
  54. #include "util/counts.h"
  55. #include "util/session.h"
  56. #include "util/tool.h"
  57. #include "asm/bug.h"
  58. #include <stdlib.h>
  59. #include <sys/prctl.h>
  60. #include <locale.h>
  61. #define DEFAULT_SEPARATOR " "
  62. #define CNTR_NOT_SUPPORTED "<not supported>"
  63. #define CNTR_NOT_COUNTED "<not counted>"
  64. static void print_counters(struct timespec *ts, int argc, const char **argv);
  65. /* Default events used for perf stat -T */
  66. static const char *transaction_attrs = {
  67. "task-clock,"
  68. "{"
  69. "instructions,"
  70. "cycles,"
  71. "cpu/cycles-t/,"
  72. "cpu/tx-start/,"
  73. "cpu/el-start/,"
  74. "cpu/cycles-ct/"
  75. "}"
  76. };
  77. /* More limited version when the CPU does not have all events. */
  78. static const char * transaction_limited_attrs = {
  79. "task-clock,"
  80. "{"
  81. "instructions,"
  82. "cycles,"
  83. "cpu/cycles-t/,"
  84. "cpu/tx-start/"
  85. "}"
  86. };
  87. static struct perf_evlist *evsel_list;
  88. static struct target target = {
  89. .uid = UINT_MAX,
  90. };
  91. typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
  92. static int run_count = 1;
  93. static bool no_inherit = false;
  94. static volatile pid_t child_pid = -1;
  95. static bool null_run = false;
  96. static int detailed_run = 0;
  97. static bool transaction_run;
  98. static bool big_num = true;
  99. static int big_num_opt = -1;
  100. static const char *csv_sep = NULL;
  101. static bool csv_output = false;
  102. static bool group = false;
  103. static const char *pre_cmd = NULL;
  104. static const char *post_cmd = NULL;
  105. static bool sync_run = false;
  106. static unsigned int initial_delay = 0;
  107. static unsigned int unit_width = 4; /* strlen("unit") */
  108. static bool forever = false;
  109. static struct timespec ref_time;
  110. static struct cpu_map *aggr_map;
  111. static aggr_get_id_t aggr_get_id;
  112. static bool append_file;
  113. static const char *output_name;
  114. static int output_fd;
  115. struct perf_stat {
  116. bool record;
  117. struct perf_data_file file;
  118. struct perf_session *session;
  119. u64 bytes_written;
  120. struct perf_tool tool;
  121. bool maps_allocated;
  122. struct cpu_map *cpus;
  123. struct thread_map *threads;
  124. enum aggr_mode aggr_mode;
  125. };
  126. static struct perf_stat perf_stat;
  127. #define STAT_RECORD perf_stat.record
  128. static volatile int done = 0;
  129. static struct perf_stat_config stat_config = {
  130. .aggr_mode = AGGR_GLOBAL,
  131. .scale = true,
  132. };
  133. static inline void diff_timespec(struct timespec *r, struct timespec *a,
  134. struct timespec *b)
  135. {
  136. r->tv_sec = a->tv_sec - b->tv_sec;
  137. if (a->tv_nsec < b->tv_nsec) {
  138. r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
  139. r->tv_sec--;
  140. } else {
  141. r->tv_nsec = a->tv_nsec - b->tv_nsec ;
  142. }
  143. }
  144. static void perf_stat__reset_stats(void)
  145. {
  146. perf_evlist__reset_stats(evsel_list);
  147. perf_stat__reset_shadow_stats();
  148. }
  149. static int create_perf_stat_counter(struct perf_evsel *evsel)
  150. {
  151. struct perf_event_attr *attr = &evsel->attr;
  152. if (stat_config.scale)
  153. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  154. PERF_FORMAT_TOTAL_TIME_RUNNING;
  155. attr->inherit = !no_inherit;
  156. /*
  157. * Some events get initialized with sample_(period/type) set,
  158. * like tracepoints. Clear it up for counting.
  159. */
  160. attr->sample_period = 0;
  161. /*
  162. * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
  163. * while avoiding that older tools show confusing messages.
  164. *
  165. * However for pipe sessions we need to keep it zero,
  166. * because script's perf_evsel__check_attr is triggered
  167. * by attr->sample_type != 0, and we can't run it on
  168. * stat sessions.
  169. */
  170. if (!(STAT_RECORD && perf_stat.file.is_pipe))
  171. attr->sample_type = PERF_SAMPLE_IDENTIFIER;
  172. /*
  173. * Disabling all counters initially, they will be enabled
  174. * either manually by us or by kernel via enable_on_exec
  175. * set later.
  176. */
  177. if (perf_evsel__is_group_leader(evsel)) {
  178. attr->disabled = 1;
  179. /*
  180. * In case of initial_delay we enable tracee
  181. * events manually.
  182. */
  183. if (target__none(&target) && !initial_delay)
  184. attr->enable_on_exec = 1;
  185. }
  186. if (target__has_cpu(&target))
  187. return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
  188. return perf_evsel__open_per_thread(evsel, evsel_list->threads);
  189. }
  190. /*
  191. * Does the counter have nsecs as a unit?
  192. */
  193. static inline int nsec_counter(struct perf_evsel *evsel)
  194. {
  195. if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
  196. perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
  197. return 1;
  198. return 0;
  199. }
  200. static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
  201. union perf_event *event,
  202. struct perf_sample *sample __maybe_unused,
  203. struct machine *machine __maybe_unused)
  204. {
  205. if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
  206. pr_err("failed to write perf data, error: %m\n");
  207. return -1;
  208. }
  209. perf_stat.bytes_written += event->header.size;
  210. return 0;
  211. }
  212. static int write_stat_round_event(u64 tm, u64 type)
  213. {
  214. return perf_event__synthesize_stat_round(NULL, tm, type,
  215. process_synthesized_event,
  216. NULL);
  217. }
  218. #define WRITE_STAT_ROUND_EVENT(time, interval) \
  219. write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
  220. #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
  221. static int
  222. perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
  223. struct perf_counts_values *count)
  224. {
  225. struct perf_sample_id *sid = SID(counter, cpu, thread);
  226. return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
  227. process_synthesized_event, NULL);
  228. }
  229. /*
  230. * Read out the results of a single counter:
  231. * do not aggregate counts across CPUs in system-wide mode
  232. */
  233. static int read_counter(struct perf_evsel *counter)
  234. {
  235. int nthreads = thread_map__nr(evsel_list->threads);
  236. int ncpus = perf_evsel__nr_cpus(counter);
  237. int cpu, thread;
  238. if (!counter->supported)
  239. return -ENOENT;
  240. if (counter->system_wide)
  241. nthreads = 1;
  242. for (thread = 0; thread < nthreads; thread++) {
  243. for (cpu = 0; cpu < ncpus; cpu++) {
  244. struct perf_counts_values *count;
  245. count = perf_counts(counter->counts, cpu, thread);
  246. if (perf_evsel__read(counter, cpu, thread, count))
  247. return -1;
  248. if (STAT_RECORD) {
  249. if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
  250. pr_err("failed to write stat event\n");
  251. return -1;
  252. }
  253. }
  254. }
  255. }
  256. return 0;
  257. }
  258. static void read_counters(bool close_counters)
  259. {
  260. struct perf_evsel *counter;
  261. evlist__for_each(evsel_list, counter) {
  262. if (read_counter(counter))
  263. pr_debug("failed to read counter %s\n", counter->name);
  264. if (perf_stat_process_counter(&stat_config, counter))
  265. pr_warning("failed to process counter %s\n", counter->name);
  266. if (close_counters) {
  267. perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
  268. thread_map__nr(evsel_list->threads));
  269. }
  270. }
  271. }
  272. static void process_interval(void)
  273. {
  274. struct timespec ts, rs;
  275. read_counters(false);
  276. clock_gettime(CLOCK_MONOTONIC, &ts);
  277. diff_timespec(&rs, &ts, &ref_time);
  278. if (STAT_RECORD) {
  279. if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
  280. pr_err("failed to write stat round event\n");
  281. }
  282. print_counters(&rs, 0, NULL);
  283. }
  284. static void enable_counters(void)
  285. {
  286. if (initial_delay)
  287. usleep(initial_delay * 1000);
  288. /*
  289. * We need to enable counters only if:
  290. * - we don't have tracee (attaching to task or cpu)
  291. * - we have initial delay configured
  292. */
  293. if (!target__none(&target) || initial_delay)
  294. perf_evlist__enable(evsel_list);
  295. }
  296. static volatile int workload_exec_errno;
  297. /*
  298. * perf_evlist__prepare_workload will send a SIGUSR1
  299. * if the fork fails, since we asked by setting its
  300. * want_signal to true.
  301. */
  302. static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
  303. void *ucontext __maybe_unused)
  304. {
  305. workload_exec_errno = info->si_value.sival_int;
  306. }
  307. static bool has_unit(struct perf_evsel *counter)
  308. {
  309. return counter->unit && *counter->unit;
  310. }
  311. static bool has_scale(struct perf_evsel *counter)
  312. {
  313. return counter->scale != 1;
  314. }
  315. static int perf_stat_synthesize_config(bool is_pipe)
  316. {
  317. struct perf_evsel *counter;
  318. int err;
  319. if (is_pipe) {
  320. err = perf_event__synthesize_attrs(NULL, perf_stat.session,
  321. process_synthesized_event);
  322. if (err < 0) {
  323. pr_err("Couldn't synthesize attrs.\n");
  324. return err;
  325. }
  326. }
  327. /*
  328. * Synthesize other events stuff not carried within
  329. * attr event - unit, scale, name
  330. */
  331. evlist__for_each(evsel_list, counter) {
  332. if (!counter->supported)
  333. continue;
  334. /*
  335. * Synthesize unit and scale only if it's defined.
  336. */
  337. if (has_unit(counter)) {
  338. err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
  339. if (err < 0) {
  340. pr_err("Couldn't synthesize evsel unit.\n");
  341. return err;
  342. }
  343. }
  344. if (has_scale(counter)) {
  345. err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
  346. if (err < 0) {
  347. pr_err("Couldn't synthesize evsel scale.\n");
  348. return err;
  349. }
  350. }
  351. if (counter->own_cpus) {
  352. err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
  353. if (err < 0) {
  354. pr_err("Couldn't synthesize evsel scale.\n");
  355. return err;
  356. }
  357. }
  358. /*
  359. * Name is needed only for pipe output,
  360. * perf.data carries event names.
  361. */
  362. if (is_pipe) {
  363. err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
  364. if (err < 0) {
  365. pr_err("Couldn't synthesize evsel name.\n");
  366. return err;
  367. }
  368. }
  369. }
  370. err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
  371. process_synthesized_event,
  372. NULL);
  373. if (err < 0) {
  374. pr_err("Couldn't synthesize thread map.\n");
  375. return err;
  376. }
  377. err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
  378. process_synthesized_event, NULL);
  379. if (err < 0) {
  380. pr_err("Couldn't synthesize thread map.\n");
  381. return err;
  382. }
  383. err = perf_event__synthesize_stat_config(NULL, &stat_config,
  384. process_synthesized_event, NULL);
  385. if (err < 0) {
  386. pr_err("Couldn't synthesize config.\n");
  387. return err;
  388. }
  389. return 0;
  390. }
  391. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  392. static int __store_counter_ids(struct perf_evsel *counter,
  393. struct cpu_map *cpus,
  394. struct thread_map *threads)
  395. {
  396. int cpu, thread;
  397. for (cpu = 0; cpu < cpus->nr; cpu++) {
  398. for (thread = 0; thread < threads->nr; thread++) {
  399. int fd = FD(counter, cpu, thread);
  400. if (perf_evlist__id_add_fd(evsel_list, counter,
  401. cpu, thread, fd) < 0)
  402. return -1;
  403. }
  404. }
  405. return 0;
  406. }
  407. static int store_counter_ids(struct perf_evsel *counter)
  408. {
  409. struct cpu_map *cpus = counter->cpus;
  410. struct thread_map *threads = counter->threads;
  411. if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
  412. return -ENOMEM;
  413. return __store_counter_ids(counter, cpus, threads);
  414. }
  415. static int __run_perf_stat(int argc, const char **argv)
  416. {
  417. int interval = stat_config.interval;
  418. char msg[512];
  419. unsigned long long t0, t1;
  420. struct perf_evsel *counter;
  421. struct timespec ts;
  422. size_t l;
  423. int status = 0;
  424. const bool forks = (argc > 0);
  425. bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
  426. if (interval) {
  427. ts.tv_sec = interval / 1000;
  428. ts.tv_nsec = (interval % 1000) * 1000000;
  429. } else {
  430. ts.tv_sec = 1;
  431. ts.tv_nsec = 0;
  432. }
  433. if (forks) {
  434. if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
  435. workload_exec_failed_signal) < 0) {
  436. perror("failed to prepare workload");
  437. return -1;
  438. }
  439. child_pid = evsel_list->workload.pid;
  440. }
  441. if (group)
  442. perf_evlist__set_leader(evsel_list);
  443. evlist__for_each(evsel_list, counter) {
  444. if (create_perf_stat_counter(counter) < 0) {
  445. /*
  446. * PPC returns ENXIO for HW counters until 2.6.37
  447. * (behavior changed with commit b0a873e).
  448. */
  449. if (errno == EINVAL || errno == ENOSYS ||
  450. errno == ENOENT || errno == EOPNOTSUPP ||
  451. errno == ENXIO) {
  452. if (verbose)
  453. ui__warning("%s event is not supported by the kernel.\n",
  454. perf_evsel__name(counter));
  455. counter->supported = false;
  456. if ((counter->leader != counter) ||
  457. !(counter->leader->nr_members > 1))
  458. continue;
  459. }
  460. perf_evsel__open_strerror(counter, &target,
  461. errno, msg, sizeof(msg));
  462. ui__error("%s\n", msg);
  463. if (child_pid != -1)
  464. kill(child_pid, SIGTERM);
  465. return -1;
  466. }
  467. counter->supported = true;
  468. l = strlen(counter->unit);
  469. if (l > unit_width)
  470. unit_width = l;
  471. if (STAT_RECORD && store_counter_ids(counter))
  472. return -1;
  473. }
  474. if (perf_evlist__apply_filters(evsel_list, &counter)) {
  475. error("failed to set filter \"%s\" on event %s with %d (%s)\n",
  476. counter->filter, perf_evsel__name(counter), errno,
  477. strerror_r(errno, msg, sizeof(msg)));
  478. return -1;
  479. }
  480. if (STAT_RECORD) {
  481. int err, fd = perf_data_file__fd(&perf_stat.file);
  482. if (is_pipe) {
  483. err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
  484. } else {
  485. err = perf_session__write_header(perf_stat.session, evsel_list,
  486. fd, false);
  487. }
  488. if (err < 0)
  489. return err;
  490. err = perf_stat_synthesize_config(is_pipe);
  491. if (err < 0)
  492. return err;
  493. }
  494. /*
  495. * Enable counters and exec the command:
  496. */
  497. t0 = rdclock();
  498. clock_gettime(CLOCK_MONOTONIC, &ref_time);
  499. if (forks) {
  500. perf_evlist__start_workload(evsel_list);
  501. enable_counters();
  502. if (interval) {
  503. while (!waitpid(child_pid, &status, WNOHANG)) {
  504. nanosleep(&ts, NULL);
  505. process_interval();
  506. }
  507. }
  508. wait(&status);
  509. if (workload_exec_errno) {
  510. const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
  511. pr_err("Workload failed: %s\n", emsg);
  512. return -1;
  513. }
  514. if (WIFSIGNALED(status))
  515. psignal(WTERMSIG(status), argv[0]);
  516. } else {
  517. enable_counters();
  518. while (!done) {
  519. nanosleep(&ts, NULL);
  520. if (interval)
  521. process_interval();
  522. }
  523. }
  524. t1 = rdclock();
  525. update_stats(&walltime_nsecs_stats, t1 - t0);
  526. read_counters(true);
  527. return WEXITSTATUS(status);
  528. }
  529. static int run_perf_stat(int argc, const char **argv)
  530. {
  531. int ret;
  532. if (pre_cmd) {
  533. ret = system(pre_cmd);
  534. if (ret)
  535. return ret;
  536. }
  537. if (sync_run)
  538. sync();
  539. ret = __run_perf_stat(argc, argv);
  540. if (ret)
  541. return ret;
  542. if (post_cmd) {
  543. ret = system(post_cmd);
  544. if (ret)
  545. return ret;
  546. }
  547. return ret;
  548. }
  549. static void print_running(u64 run, u64 ena)
  550. {
  551. if (csv_output) {
  552. fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
  553. csv_sep,
  554. run,
  555. csv_sep,
  556. ena ? 100.0 * run / ena : 100.0);
  557. } else if (run != ena) {
  558. fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
  559. }
  560. }
  561. static void print_noise_pct(double total, double avg)
  562. {
  563. double pct = rel_stddev_stats(total, avg);
  564. if (csv_output)
  565. fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
  566. else if (pct)
  567. fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
  568. }
  569. static void print_noise(struct perf_evsel *evsel, double avg)
  570. {
  571. struct perf_stat_evsel *ps;
  572. if (run_count == 1)
  573. return;
  574. ps = evsel->priv;
  575. print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
  576. }
  577. static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
  578. {
  579. switch (stat_config.aggr_mode) {
  580. case AGGR_CORE:
  581. fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
  582. cpu_map__id_to_socket(id),
  583. csv_output ? 0 : -8,
  584. cpu_map__id_to_cpu(id),
  585. csv_sep,
  586. csv_output ? 0 : 4,
  587. nr,
  588. csv_sep);
  589. break;
  590. case AGGR_SOCKET:
  591. fprintf(stat_config.output, "S%*d%s%*d%s",
  592. csv_output ? 0 : -5,
  593. id,
  594. csv_sep,
  595. csv_output ? 0 : 4,
  596. nr,
  597. csv_sep);
  598. break;
  599. case AGGR_NONE:
  600. fprintf(stat_config.output, "CPU%*d%s",
  601. csv_output ? 0 : -4,
  602. perf_evsel__cpus(evsel)->map[id], csv_sep);
  603. break;
  604. case AGGR_THREAD:
  605. fprintf(stat_config.output, "%*s-%*d%s",
  606. csv_output ? 0 : 16,
  607. thread_map__comm(evsel->threads, id),
  608. csv_output ? 0 : -8,
  609. thread_map__pid(evsel->threads, id),
  610. csv_sep);
  611. break;
  612. case AGGR_GLOBAL:
  613. case AGGR_UNSET:
  614. default:
  615. break;
  616. }
  617. }
  618. static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
  619. {
  620. FILE *output = stat_config.output;
  621. double msecs = avg / 1e6;
  622. const char *fmt_v, *fmt_n;
  623. char name[25];
  624. fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
  625. fmt_n = csv_output ? "%s" : "%-25s";
  626. aggr_printout(evsel, id, nr);
  627. scnprintf(name, sizeof(name), "%s%s",
  628. perf_evsel__name(evsel), csv_output ? "" : " (msec)");
  629. fprintf(output, fmt_v, msecs, csv_sep);
  630. if (csv_output)
  631. fprintf(output, "%s%s", evsel->unit, csv_sep);
  632. else
  633. fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
  634. fprintf(output, fmt_n, name);
  635. if (evsel->cgrp)
  636. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  637. }
  638. static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
  639. {
  640. FILE *output = stat_config.output;
  641. double sc = evsel->scale;
  642. const char *fmt;
  643. if (csv_output) {
  644. fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
  645. } else {
  646. if (big_num)
  647. fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
  648. else
  649. fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
  650. }
  651. aggr_printout(evsel, id, nr);
  652. fprintf(output, fmt, avg, csv_sep);
  653. if (evsel->unit)
  654. fprintf(output, "%-*s%s",
  655. csv_output ? 0 : unit_width,
  656. evsel->unit, csv_sep);
  657. fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
  658. if (evsel->cgrp)
  659. fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
  660. }
  661. static void printout(int id, int nr, struct perf_evsel *counter, double uval)
  662. {
  663. int cpu = cpu_map__id_to_cpu(id);
  664. if (stat_config.aggr_mode == AGGR_GLOBAL)
  665. cpu = 0;
  666. if (nsec_counter(counter))
  667. nsec_printout(id, nr, counter, uval);
  668. else
  669. abs_printout(id, nr, counter, uval);
  670. if (!csv_output && !stat_config.interval)
  671. perf_stat__print_shadow_stats(stat_config.output, counter,
  672. uval, cpu,
  673. stat_config.aggr_mode);
  674. }
  675. static void print_aggr(char *prefix)
  676. {
  677. FILE *output = stat_config.output;
  678. struct perf_evsel *counter;
  679. int cpu, s, s2, id, nr;
  680. double uval;
  681. u64 ena, run, val;
  682. if (!(aggr_map || aggr_get_id))
  683. return;
  684. for (s = 0; s < aggr_map->nr; s++) {
  685. id = aggr_map->map[s];
  686. evlist__for_each(evsel_list, counter) {
  687. val = ena = run = 0;
  688. nr = 0;
  689. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  690. s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
  691. if (s2 != id)
  692. continue;
  693. val += perf_counts(counter->counts, cpu, 0)->val;
  694. ena += perf_counts(counter->counts, cpu, 0)->ena;
  695. run += perf_counts(counter->counts, cpu, 0)->run;
  696. nr++;
  697. }
  698. if (prefix)
  699. fprintf(output, "%s", prefix);
  700. if (run == 0 || ena == 0) {
  701. aggr_printout(counter, id, nr);
  702. fprintf(output, "%*s%s",
  703. csv_output ? 0 : 18,
  704. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  705. csv_sep);
  706. fprintf(output, "%-*s%s",
  707. csv_output ? 0 : unit_width,
  708. counter->unit, csv_sep);
  709. fprintf(output, "%*s",
  710. csv_output ? 0 : -25,
  711. perf_evsel__name(counter));
  712. if (counter->cgrp)
  713. fprintf(output, "%s%s",
  714. csv_sep, counter->cgrp->name);
  715. print_running(run, ena);
  716. fputc('\n', output);
  717. continue;
  718. }
  719. uval = val * counter->scale;
  720. printout(id, nr, counter, uval);
  721. if (!csv_output)
  722. print_noise(counter, 1.0);
  723. print_running(run, ena);
  724. fputc('\n', output);
  725. }
  726. }
  727. }
  728. static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
  729. {
  730. FILE *output = stat_config.output;
  731. int nthreads = thread_map__nr(counter->threads);
  732. int ncpus = cpu_map__nr(counter->cpus);
  733. int cpu, thread;
  734. double uval;
  735. for (thread = 0; thread < nthreads; thread++) {
  736. u64 ena = 0, run = 0, val = 0;
  737. for (cpu = 0; cpu < ncpus; cpu++) {
  738. val += perf_counts(counter->counts, cpu, thread)->val;
  739. ena += perf_counts(counter->counts, cpu, thread)->ena;
  740. run += perf_counts(counter->counts, cpu, thread)->run;
  741. }
  742. if (prefix)
  743. fprintf(output, "%s", prefix);
  744. uval = val * counter->scale;
  745. printout(thread, 0, counter, uval);
  746. if (!csv_output)
  747. print_noise(counter, 1.0);
  748. print_running(run, ena);
  749. fputc('\n', output);
  750. }
  751. }
  752. /*
  753. * Print out the results of a single counter:
  754. * aggregated counts in system-wide mode
  755. */
  756. static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
  757. {
  758. FILE *output = stat_config.output;
  759. struct perf_stat_evsel *ps = counter->priv;
  760. double avg = avg_stats(&ps->res_stats[0]);
  761. int scaled = counter->counts->scaled;
  762. double uval;
  763. double avg_enabled, avg_running;
  764. avg_enabled = avg_stats(&ps->res_stats[1]);
  765. avg_running = avg_stats(&ps->res_stats[2]);
  766. if (prefix)
  767. fprintf(output, "%s", prefix);
  768. if (scaled == -1 || !counter->supported) {
  769. fprintf(output, "%*s%s",
  770. csv_output ? 0 : 18,
  771. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  772. csv_sep);
  773. fprintf(output, "%-*s%s",
  774. csv_output ? 0 : unit_width,
  775. counter->unit, csv_sep);
  776. fprintf(output, "%*s",
  777. csv_output ? 0 : -25,
  778. perf_evsel__name(counter));
  779. if (counter->cgrp)
  780. fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
  781. print_running(avg_running, avg_enabled);
  782. fputc('\n', output);
  783. return;
  784. }
  785. uval = avg * counter->scale;
  786. printout(-1, 0, counter, uval);
  787. print_noise(counter, avg);
  788. print_running(avg_running, avg_enabled);
  789. fprintf(output, "\n");
  790. }
  791. /*
  792. * Print out the results of a single counter:
  793. * does not use aggregated count in system-wide
  794. */
  795. static void print_counter(struct perf_evsel *counter, char *prefix)
  796. {
  797. FILE *output = stat_config.output;
  798. u64 ena, run, val;
  799. double uval;
  800. int cpu;
  801. for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
  802. val = perf_counts(counter->counts, cpu, 0)->val;
  803. ena = perf_counts(counter->counts, cpu, 0)->ena;
  804. run = perf_counts(counter->counts, cpu, 0)->run;
  805. if (prefix)
  806. fprintf(output, "%s", prefix);
  807. if (run == 0 || ena == 0) {
  808. fprintf(output, "CPU%*d%s%*s%s",
  809. csv_output ? 0 : -4,
  810. perf_evsel__cpus(counter)->map[cpu], csv_sep,
  811. csv_output ? 0 : 18,
  812. counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
  813. csv_sep);
  814. fprintf(output, "%-*s%s",
  815. csv_output ? 0 : unit_width,
  816. counter->unit, csv_sep);
  817. fprintf(output, "%*s",
  818. csv_output ? 0 : -25,
  819. perf_evsel__name(counter));
  820. if (counter->cgrp)
  821. fprintf(output, "%s%s",
  822. csv_sep, counter->cgrp->name);
  823. print_running(run, ena);
  824. fputc('\n', output);
  825. continue;
  826. }
  827. uval = val * counter->scale;
  828. printout(cpu, 0, counter, uval);
  829. if (!csv_output)
  830. print_noise(counter, 1.0);
  831. print_running(run, ena);
  832. fputc('\n', output);
  833. }
  834. }
  835. static void print_interval(char *prefix, struct timespec *ts)
  836. {
  837. FILE *output = stat_config.output;
  838. static int num_print_interval;
  839. sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
  840. if (num_print_interval == 0 && !csv_output) {
  841. switch (stat_config.aggr_mode) {
  842. case AGGR_SOCKET:
  843. fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
  844. break;
  845. case AGGR_CORE:
  846. fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
  847. break;
  848. case AGGR_NONE:
  849. fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
  850. break;
  851. case AGGR_THREAD:
  852. fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
  853. break;
  854. case AGGR_GLOBAL:
  855. default:
  856. fprintf(output, "# time counts %*s events\n", unit_width, "unit");
  857. case AGGR_UNSET:
  858. break;
  859. }
  860. }
  861. if (++num_print_interval == 25)
  862. num_print_interval = 0;
  863. }
  864. static void print_header(int argc, const char **argv)
  865. {
  866. FILE *output = stat_config.output;
  867. int i;
  868. fflush(stdout);
  869. if (!csv_output) {
  870. fprintf(output, "\n");
  871. fprintf(output, " Performance counter stats for ");
  872. if (target.system_wide)
  873. fprintf(output, "\'system wide");
  874. else if (target.cpu_list)
  875. fprintf(output, "\'CPU(s) %s", target.cpu_list);
  876. else if (!target__has_task(&target)) {
  877. fprintf(output, "\'%s", argv ? argv[0] : "pipe");
  878. for (i = 1; argv && (i < argc); i++)
  879. fprintf(output, " %s", argv[i]);
  880. } else if (target.pid)
  881. fprintf(output, "process id \'%s", target.pid);
  882. else
  883. fprintf(output, "thread id \'%s", target.tid);
  884. fprintf(output, "\'");
  885. if (run_count > 1)
  886. fprintf(output, " (%d runs)", run_count);
  887. fprintf(output, ":\n\n");
  888. }
  889. }
  890. static void print_footer(void)
  891. {
  892. FILE *output = stat_config.output;
  893. if (!null_run)
  894. fprintf(output, "\n");
  895. fprintf(output, " %17.9f seconds time elapsed",
  896. avg_stats(&walltime_nsecs_stats)/1e9);
  897. if (run_count > 1) {
  898. fprintf(output, " ");
  899. print_noise_pct(stddev_stats(&walltime_nsecs_stats),
  900. avg_stats(&walltime_nsecs_stats));
  901. }
  902. fprintf(output, "\n\n");
  903. }
  904. static void print_counters(struct timespec *ts, int argc, const char **argv)
  905. {
  906. int interval = stat_config.interval;
  907. struct perf_evsel *counter;
  908. char buf[64], *prefix = NULL;
  909. /* Do not print anything if we record to the pipe. */
  910. if (STAT_RECORD && perf_stat.file.is_pipe)
  911. return;
  912. if (interval)
  913. print_interval(prefix = buf, ts);
  914. else
  915. print_header(argc, argv);
  916. switch (stat_config.aggr_mode) {
  917. case AGGR_CORE:
  918. case AGGR_SOCKET:
  919. print_aggr(prefix);
  920. break;
  921. case AGGR_THREAD:
  922. evlist__for_each(evsel_list, counter)
  923. print_aggr_thread(counter, prefix);
  924. break;
  925. case AGGR_GLOBAL:
  926. evlist__for_each(evsel_list, counter)
  927. print_counter_aggr(counter, prefix);
  928. break;
  929. case AGGR_NONE:
  930. evlist__for_each(evsel_list, counter)
  931. print_counter(counter, prefix);
  932. break;
  933. case AGGR_UNSET:
  934. default:
  935. break;
  936. }
  937. if (!interval && !csv_output)
  938. print_footer();
  939. fflush(stat_config.output);
  940. }
  941. static volatile int signr = -1;
  942. static void skip_signal(int signo)
  943. {
  944. if ((child_pid == -1) || stat_config.interval)
  945. done = 1;
  946. signr = signo;
  947. /*
  948. * render child_pid harmless
  949. * won't send SIGTERM to a random
  950. * process in case of race condition
  951. * and fast PID recycling
  952. */
  953. child_pid = -1;
  954. }
  955. static void sig_atexit(void)
  956. {
  957. sigset_t set, oset;
  958. /*
  959. * avoid race condition with SIGCHLD handler
  960. * in skip_signal() which is modifying child_pid
  961. * goal is to avoid send SIGTERM to a random
  962. * process
  963. */
  964. sigemptyset(&set);
  965. sigaddset(&set, SIGCHLD);
  966. sigprocmask(SIG_BLOCK, &set, &oset);
  967. if (child_pid != -1)
  968. kill(child_pid, SIGTERM);
  969. sigprocmask(SIG_SETMASK, &oset, NULL);
  970. if (signr == -1)
  971. return;
  972. signal(signr, SIG_DFL);
  973. kill(getpid(), signr);
  974. }
  975. static int stat__set_big_num(const struct option *opt __maybe_unused,
  976. const char *s __maybe_unused, int unset)
  977. {
  978. big_num_opt = unset ? 0 : 1;
  979. return 0;
  980. }
  981. static const struct option stat_options[] = {
  982. OPT_BOOLEAN('T', "transaction", &transaction_run,
  983. "hardware transaction statistics"),
  984. OPT_CALLBACK('e', "event", &evsel_list, "event",
  985. "event selector. use 'perf list' to list available events",
  986. parse_events_option),
  987. OPT_CALLBACK(0, "filter", &evsel_list, "filter",
  988. "event filter", parse_filter),
  989. OPT_BOOLEAN('i', "no-inherit", &no_inherit,
  990. "child tasks do not inherit counters"),
  991. OPT_STRING('p', "pid", &target.pid, "pid",
  992. "stat events on existing process id"),
  993. OPT_STRING('t', "tid", &target.tid, "tid",
  994. "stat events on existing thread id"),
  995. OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
  996. "system-wide collection from all CPUs"),
  997. OPT_BOOLEAN('g', "group", &group,
  998. "put the counters into a counter group"),
  999. OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
  1000. OPT_INCR('v', "verbose", &verbose,
  1001. "be more verbose (show counter open errors, etc)"),
  1002. OPT_INTEGER('r', "repeat", &run_count,
  1003. "repeat command and print average + stddev (max: 100, forever: 0)"),
  1004. OPT_BOOLEAN('n', "null", &null_run,
  1005. "null run - dont start any counters"),
  1006. OPT_INCR('d', "detailed", &detailed_run,
  1007. "detailed run - start a lot of events"),
  1008. OPT_BOOLEAN('S', "sync", &sync_run,
  1009. "call sync() before starting a run"),
  1010. OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
  1011. "print large numbers with thousands\' separators",
  1012. stat__set_big_num),
  1013. OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
  1014. "list of cpus to monitor in system-wide"),
  1015. OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
  1016. "disable CPU count aggregation", AGGR_NONE),
  1017. OPT_STRING('x', "field-separator", &csv_sep, "separator",
  1018. "print counts with custom separator"),
  1019. OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
  1020. "monitor event in cgroup name only", parse_cgroups),
  1021. OPT_STRING('o', "output", &output_name, "file", "output file name"),
  1022. OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
  1023. OPT_INTEGER(0, "log-fd", &output_fd,
  1024. "log output to fd, instead of stderr"),
  1025. OPT_STRING(0, "pre", &pre_cmd, "command",
  1026. "command to run prior to the measured command"),
  1027. OPT_STRING(0, "post", &post_cmd, "command",
  1028. "command to run after to the measured command"),
  1029. OPT_UINTEGER('I', "interval-print", &stat_config.interval,
  1030. "print counts at regular interval in ms (>= 10)"),
  1031. OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
  1032. "aggregate counts per processor socket", AGGR_SOCKET),
  1033. OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
  1034. "aggregate counts per physical processor core", AGGR_CORE),
  1035. OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
  1036. "aggregate counts per thread", AGGR_THREAD),
  1037. OPT_UINTEGER('D', "delay", &initial_delay,
  1038. "ms to wait before starting measurement after program start"),
  1039. OPT_END()
  1040. };
  1041. static int perf_stat__get_socket(struct cpu_map *map, int cpu)
  1042. {
  1043. return cpu_map__get_socket(map, cpu, NULL);
  1044. }
  1045. static int perf_stat__get_core(struct cpu_map *map, int cpu)
  1046. {
  1047. return cpu_map__get_core(map, cpu, NULL);
  1048. }
  1049. static int cpu_map__get_max(struct cpu_map *map)
  1050. {
  1051. int i, max = -1;
  1052. for (i = 0; i < map->nr; i++) {
  1053. if (map->map[i] > max)
  1054. max = map->map[i];
  1055. }
  1056. return max;
  1057. }
  1058. static struct cpu_map *cpus_aggr_map;
  1059. static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
  1060. {
  1061. int cpu;
  1062. if (idx >= map->nr)
  1063. return -1;
  1064. cpu = map->map[idx];
  1065. if (cpus_aggr_map->map[cpu] == -1)
  1066. cpus_aggr_map->map[cpu] = get_id(map, idx);
  1067. return cpus_aggr_map->map[cpu];
  1068. }
  1069. static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
  1070. {
  1071. return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
  1072. }
  1073. static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
  1074. {
  1075. return perf_stat__get_aggr(perf_stat__get_core, map, idx);
  1076. }
  1077. static int perf_stat_init_aggr_mode(void)
  1078. {
  1079. int nr;
  1080. switch (stat_config.aggr_mode) {
  1081. case AGGR_SOCKET:
  1082. if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
  1083. perror("cannot build socket map");
  1084. return -1;
  1085. }
  1086. aggr_get_id = perf_stat__get_socket_cached;
  1087. break;
  1088. case AGGR_CORE:
  1089. if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
  1090. perror("cannot build core map");
  1091. return -1;
  1092. }
  1093. aggr_get_id = perf_stat__get_core_cached;
  1094. break;
  1095. case AGGR_NONE:
  1096. case AGGR_GLOBAL:
  1097. case AGGR_THREAD:
  1098. case AGGR_UNSET:
  1099. default:
  1100. break;
  1101. }
  1102. /*
  1103. * The evsel_list->cpus is the base we operate on,
  1104. * taking the highest cpu number to be the size of
  1105. * the aggregation translate cpumap.
  1106. */
  1107. nr = cpu_map__get_max(evsel_list->cpus);
  1108. cpus_aggr_map = cpu_map__empty_new(nr + 1);
  1109. return cpus_aggr_map ? 0 : -ENOMEM;
  1110. }
  1111. static void perf_stat__exit_aggr_mode(void)
  1112. {
  1113. cpu_map__put(aggr_map);
  1114. cpu_map__put(cpus_aggr_map);
  1115. aggr_map = NULL;
  1116. cpus_aggr_map = NULL;
  1117. }
  1118. static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
  1119. {
  1120. int cpu;
  1121. if (idx > map->nr)
  1122. return -1;
  1123. cpu = map->map[idx];
  1124. if (cpu >= env->nr_cpus_online)
  1125. return -1;
  1126. return cpu;
  1127. }
  1128. static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
  1129. {
  1130. struct perf_env *env = data;
  1131. int cpu = perf_env__get_cpu(env, map, idx);
  1132. return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
  1133. }
  1134. static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
  1135. {
  1136. struct perf_env *env = data;
  1137. int core = -1, cpu = perf_env__get_cpu(env, map, idx);
  1138. if (cpu != -1) {
  1139. int socket_id = env->cpu[cpu].socket_id;
  1140. /*
  1141. * Encode socket in upper 16 bits
  1142. * core_id is relative to socket, and
  1143. * we need a global id. So we combine
  1144. * socket + core id.
  1145. */
  1146. core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
  1147. }
  1148. return core;
  1149. }
  1150. static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
  1151. struct cpu_map **sockp)
  1152. {
  1153. return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
  1154. }
  1155. static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
  1156. struct cpu_map **corep)
  1157. {
  1158. return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
  1159. }
  1160. static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
  1161. {
  1162. return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
  1163. }
  1164. static int perf_stat__get_core_file(struct cpu_map *map, int idx)
  1165. {
  1166. return perf_env__get_core(map, idx, &perf_stat.session->header.env);
  1167. }
  1168. static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
  1169. {
  1170. struct perf_env *env = &st->session->header.env;
  1171. switch (stat_config.aggr_mode) {
  1172. case AGGR_SOCKET:
  1173. if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
  1174. perror("cannot build socket map");
  1175. return -1;
  1176. }
  1177. aggr_get_id = perf_stat__get_socket_file;
  1178. break;
  1179. case AGGR_CORE:
  1180. if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
  1181. perror("cannot build core map");
  1182. return -1;
  1183. }
  1184. aggr_get_id = perf_stat__get_core_file;
  1185. break;
  1186. case AGGR_NONE:
  1187. case AGGR_GLOBAL:
  1188. case AGGR_THREAD:
  1189. case AGGR_UNSET:
  1190. default:
  1191. break;
  1192. }
  1193. return 0;
  1194. }
  1195. /*
  1196. * Add default attributes, if there were no attributes specified or
  1197. * if -d/--detailed, -d -d or -d -d -d is used:
  1198. */
  1199. static int add_default_attributes(void)
  1200. {
  1201. struct perf_event_attr default_attrs[] = {
  1202. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  1203. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
  1204. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  1205. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  1206. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  1207. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
  1208. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
  1209. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  1210. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
  1211. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
  1212. };
  1213. /*
  1214. * Detailed stats (-d), covering the L1 and last level data caches:
  1215. */
  1216. struct perf_event_attr detailed_attrs[] = {
  1217. { .type = PERF_TYPE_HW_CACHE,
  1218. .config =
  1219. PERF_COUNT_HW_CACHE_L1D << 0 |
  1220. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1221. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1222. { .type = PERF_TYPE_HW_CACHE,
  1223. .config =
  1224. PERF_COUNT_HW_CACHE_L1D << 0 |
  1225. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1226. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1227. { .type = PERF_TYPE_HW_CACHE,
  1228. .config =
  1229. PERF_COUNT_HW_CACHE_LL << 0 |
  1230. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1231. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1232. { .type = PERF_TYPE_HW_CACHE,
  1233. .config =
  1234. PERF_COUNT_HW_CACHE_LL << 0 |
  1235. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1236. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1237. };
  1238. /*
  1239. * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
  1240. */
  1241. struct perf_event_attr very_detailed_attrs[] = {
  1242. { .type = PERF_TYPE_HW_CACHE,
  1243. .config =
  1244. PERF_COUNT_HW_CACHE_L1I << 0 |
  1245. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1246. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1247. { .type = PERF_TYPE_HW_CACHE,
  1248. .config =
  1249. PERF_COUNT_HW_CACHE_L1I << 0 |
  1250. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1251. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1252. { .type = PERF_TYPE_HW_CACHE,
  1253. .config =
  1254. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1255. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1256. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1257. { .type = PERF_TYPE_HW_CACHE,
  1258. .config =
  1259. PERF_COUNT_HW_CACHE_DTLB << 0 |
  1260. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1261. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1262. { .type = PERF_TYPE_HW_CACHE,
  1263. .config =
  1264. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1265. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1266. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1267. { .type = PERF_TYPE_HW_CACHE,
  1268. .config =
  1269. PERF_COUNT_HW_CACHE_ITLB << 0 |
  1270. (PERF_COUNT_HW_CACHE_OP_READ << 8) |
  1271. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1272. };
  1273. /*
  1274. * Very, very detailed stats (-d -d -d), adding prefetch events:
  1275. */
  1276. struct perf_event_attr very_very_detailed_attrs[] = {
  1277. { .type = PERF_TYPE_HW_CACHE,
  1278. .config =
  1279. PERF_COUNT_HW_CACHE_L1D << 0 |
  1280. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1281. (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
  1282. { .type = PERF_TYPE_HW_CACHE,
  1283. .config =
  1284. PERF_COUNT_HW_CACHE_L1D << 0 |
  1285. (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
  1286. (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
  1287. };
  1288. /* Set attrs if no event is selected and !null_run: */
  1289. if (null_run)
  1290. return 0;
  1291. if (transaction_run) {
  1292. int err;
  1293. if (pmu_have_event("cpu", "cycles-ct") &&
  1294. pmu_have_event("cpu", "el-start"))
  1295. err = parse_events(evsel_list, transaction_attrs, NULL);
  1296. else
  1297. err = parse_events(evsel_list, transaction_limited_attrs, NULL);
  1298. if (err) {
  1299. fprintf(stderr, "Cannot set up transaction events\n");
  1300. return -1;
  1301. }
  1302. return 0;
  1303. }
  1304. if (!evsel_list->nr_entries) {
  1305. if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
  1306. return -1;
  1307. }
  1308. /* Detailed events get appended to the event list: */
  1309. if (detailed_run < 1)
  1310. return 0;
  1311. /* Append detailed run extra attributes: */
  1312. if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
  1313. return -1;
  1314. if (detailed_run < 2)
  1315. return 0;
  1316. /* Append very detailed run extra attributes: */
  1317. if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
  1318. return -1;
  1319. if (detailed_run < 3)
  1320. return 0;
  1321. /* Append very, very detailed run extra attributes: */
  1322. return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
  1323. }
  1324. static const char * const stat_record_usage[] = {
  1325. "perf stat record [<options>]",
  1326. NULL,
  1327. };
  1328. static void init_features(struct perf_session *session)
  1329. {
  1330. int feat;
  1331. for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
  1332. perf_header__set_feat(&session->header, feat);
  1333. perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
  1334. perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
  1335. perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
  1336. perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
  1337. }
  1338. static int __cmd_record(int argc, const char **argv)
  1339. {
  1340. struct perf_session *session;
  1341. struct perf_data_file *file = &perf_stat.file;
  1342. argc = parse_options(argc, argv, stat_options, stat_record_usage,
  1343. PARSE_OPT_STOP_AT_NON_OPTION);
  1344. if (output_name)
  1345. file->path = output_name;
  1346. if (run_count != 1 || forever) {
  1347. pr_err("Cannot use -r option with perf stat record.\n");
  1348. return -1;
  1349. }
  1350. session = perf_session__new(file, false, NULL);
  1351. if (session == NULL) {
  1352. pr_err("Perf session creation failed.\n");
  1353. return -1;
  1354. }
  1355. init_features(session);
  1356. session->evlist = evsel_list;
  1357. perf_stat.session = session;
  1358. perf_stat.record = true;
  1359. return argc;
  1360. }
  1361. static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
  1362. union perf_event *event,
  1363. struct perf_session *session)
  1364. {
  1365. struct stat_round_event *round = &event->stat_round;
  1366. struct perf_evsel *counter;
  1367. struct timespec tsh, *ts = NULL;
  1368. const char **argv = session->header.env.cmdline_argv;
  1369. int argc = session->header.env.nr_cmdline;
  1370. evlist__for_each(evsel_list, counter)
  1371. perf_stat_process_counter(&stat_config, counter);
  1372. if (round->type == PERF_STAT_ROUND_TYPE__FINAL)
  1373. update_stats(&walltime_nsecs_stats, round->time);
  1374. if (stat_config.interval && round->time) {
  1375. tsh.tv_sec = round->time / NSECS_PER_SEC;
  1376. tsh.tv_nsec = round->time % NSECS_PER_SEC;
  1377. ts = &tsh;
  1378. }
  1379. print_counters(ts, argc, argv);
  1380. return 0;
  1381. }
  1382. static
  1383. int process_stat_config_event(struct perf_tool *tool __maybe_unused,
  1384. union perf_event *event,
  1385. struct perf_session *session __maybe_unused)
  1386. {
  1387. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1388. perf_event__read_stat_config(&stat_config, &event->stat_config);
  1389. if (cpu_map__empty(st->cpus)) {
  1390. if (st->aggr_mode != AGGR_UNSET)
  1391. pr_warning("warning: processing task data, aggregation mode not set\n");
  1392. return 0;
  1393. }
  1394. if (st->aggr_mode != AGGR_UNSET)
  1395. stat_config.aggr_mode = st->aggr_mode;
  1396. if (perf_stat.file.is_pipe)
  1397. perf_stat_init_aggr_mode();
  1398. else
  1399. perf_stat_init_aggr_mode_file(st);
  1400. return 0;
  1401. }
  1402. static int set_maps(struct perf_stat *st)
  1403. {
  1404. if (!st->cpus || !st->threads)
  1405. return 0;
  1406. if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
  1407. return -EINVAL;
  1408. perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
  1409. if (perf_evlist__alloc_stats(evsel_list, true))
  1410. return -ENOMEM;
  1411. st->maps_allocated = true;
  1412. return 0;
  1413. }
  1414. static
  1415. int process_thread_map_event(struct perf_tool *tool __maybe_unused,
  1416. union perf_event *event,
  1417. struct perf_session *session __maybe_unused)
  1418. {
  1419. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1420. if (st->threads) {
  1421. pr_warning("Extra thread map event, ignoring.\n");
  1422. return 0;
  1423. }
  1424. st->threads = thread_map__new_event(&event->thread_map);
  1425. if (!st->threads)
  1426. return -ENOMEM;
  1427. return set_maps(st);
  1428. }
  1429. static
  1430. int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
  1431. union perf_event *event,
  1432. struct perf_session *session __maybe_unused)
  1433. {
  1434. struct perf_stat *st = container_of(tool, struct perf_stat, tool);
  1435. struct cpu_map *cpus;
  1436. if (st->cpus) {
  1437. pr_warning("Extra cpu map event, ignoring.\n");
  1438. return 0;
  1439. }
  1440. cpus = cpu_map__new_data(&event->cpu_map.data);
  1441. if (!cpus)
  1442. return -ENOMEM;
  1443. st->cpus = cpus;
  1444. return set_maps(st);
  1445. }
  1446. static const char * const stat_report_usage[] = {
  1447. "perf stat report [<options>]",
  1448. NULL,
  1449. };
  1450. static struct perf_stat perf_stat = {
  1451. .tool = {
  1452. .attr = perf_event__process_attr,
  1453. .event_update = perf_event__process_event_update,
  1454. .thread_map = process_thread_map_event,
  1455. .cpu_map = process_cpu_map_event,
  1456. .stat_config = process_stat_config_event,
  1457. .stat = perf_event__process_stat_event,
  1458. .stat_round = process_stat_round_event,
  1459. },
  1460. .aggr_mode = AGGR_UNSET,
  1461. };
  1462. static int __cmd_report(int argc, const char **argv)
  1463. {
  1464. struct perf_session *session;
  1465. const struct option options[] = {
  1466. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1467. OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
  1468. "aggregate counts per processor socket", AGGR_SOCKET),
  1469. OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
  1470. "aggregate counts per physical processor core", AGGR_CORE),
  1471. OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
  1472. "disable CPU count aggregation", AGGR_NONE),
  1473. OPT_END()
  1474. };
  1475. struct stat st;
  1476. int ret;
  1477. argc = parse_options(argc, argv, options, stat_report_usage, 0);
  1478. if (!input_name || !strlen(input_name)) {
  1479. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  1480. input_name = "-";
  1481. else
  1482. input_name = "perf.data";
  1483. }
  1484. perf_stat.file.path = input_name;
  1485. perf_stat.file.mode = PERF_DATA_MODE_READ;
  1486. session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
  1487. if (session == NULL)
  1488. return -1;
  1489. perf_stat.session = session;
  1490. stat_config.output = stderr;
  1491. evsel_list = session->evlist;
  1492. ret = perf_session__process_events(session);
  1493. if (ret)
  1494. return ret;
  1495. perf_session__delete(session);
  1496. return 0;
  1497. }
  1498. int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
  1499. {
  1500. const char * const stat_usage[] = {
  1501. "perf stat [<options>] [<command>]",
  1502. NULL
  1503. };
  1504. int status = -EINVAL, run_idx;
  1505. const char *mode;
  1506. FILE *output = stderr;
  1507. unsigned int interval;
  1508. const char * const stat_subcommands[] = { "record", "report" };
  1509. setlocale(LC_ALL, "");
  1510. evsel_list = perf_evlist__new();
  1511. if (evsel_list == NULL)
  1512. return -ENOMEM;
  1513. argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
  1514. (const char **) stat_usage,
  1515. PARSE_OPT_STOP_AT_NON_OPTION);
  1516. if (csv_sep) {
  1517. csv_output = true;
  1518. if (!strcmp(csv_sep, "\\t"))
  1519. csv_sep = "\t";
  1520. } else
  1521. csv_sep = DEFAULT_SEPARATOR;
  1522. if (argc && !strncmp(argv[0], "rec", 3)) {
  1523. argc = __cmd_record(argc, argv);
  1524. if (argc < 0)
  1525. return -1;
  1526. } else if (argc && !strncmp(argv[0], "rep", 3))
  1527. return __cmd_report(argc, argv);
  1528. interval = stat_config.interval;
  1529. /*
  1530. * For record command the -o is already taken care of.
  1531. */
  1532. if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
  1533. output = NULL;
  1534. if (output_name && output_fd) {
  1535. fprintf(stderr, "cannot use both --output and --log-fd\n");
  1536. parse_options_usage(stat_usage, stat_options, "o", 1);
  1537. parse_options_usage(NULL, stat_options, "log-fd", 0);
  1538. goto out;
  1539. }
  1540. if (output_fd < 0) {
  1541. fprintf(stderr, "argument to --log-fd must be a > 0\n");
  1542. parse_options_usage(stat_usage, stat_options, "log-fd", 0);
  1543. goto out;
  1544. }
  1545. if (!output) {
  1546. struct timespec tm;
  1547. mode = append_file ? "a" : "w";
  1548. output = fopen(output_name, mode);
  1549. if (!output) {
  1550. perror("failed to create output file");
  1551. return -1;
  1552. }
  1553. clock_gettime(CLOCK_REALTIME, &tm);
  1554. fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
  1555. } else if (output_fd > 0) {
  1556. mode = append_file ? "a" : "w";
  1557. output = fdopen(output_fd, mode);
  1558. if (!output) {
  1559. perror("Failed opening logfd");
  1560. return -errno;
  1561. }
  1562. }
  1563. stat_config.output = output;
  1564. /*
  1565. * let the spreadsheet do the pretty-printing
  1566. */
  1567. if (csv_output) {
  1568. /* User explicitly passed -B? */
  1569. if (big_num_opt == 1) {
  1570. fprintf(stderr, "-B option not supported with -x\n");
  1571. parse_options_usage(stat_usage, stat_options, "B", 1);
  1572. parse_options_usage(NULL, stat_options, "x", 1);
  1573. goto out;
  1574. } else /* Nope, so disable big number formatting */
  1575. big_num = false;
  1576. } else if (big_num_opt == 0) /* User passed --no-big-num */
  1577. big_num = false;
  1578. if (!argc && target__none(&target))
  1579. usage_with_options(stat_usage, stat_options);
  1580. if (run_count < 0) {
  1581. pr_err("Run count must be a positive number\n");
  1582. parse_options_usage(stat_usage, stat_options, "r", 1);
  1583. goto out;
  1584. } else if (run_count == 0) {
  1585. forever = true;
  1586. run_count = 1;
  1587. }
  1588. if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
  1589. fprintf(stderr, "The --per-thread option is only available "
  1590. "when monitoring via -p -t options.\n");
  1591. parse_options_usage(NULL, stat_options, "p", 1);
  1592. parse_options_usage(NULL, stat_options, "t", 1);
  1593. goto out;
  1594. }
  1595. /*
  1596. * no_aggr, cgroup are for system-wide only
  1597. * --per-thread is aggregated per thread, we dont mix it with cpu mode
  1598. */
  1599. if (((stat_config.aggr_mode != AGGR_GLOBAL &&
  1600. stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
  1601. !target__has_cpu(&target)) {
  1602. fprintf(stderr, "both cgroup and no-aggregation "
  1603. "modes only available in system-wide mode\n");
  1604. parse_options_usage(stat_usage, stat_options, "G", 1);
  1605. parse_options_usage(NULL, stat_options, "A", 1);
  1606. parse_options_usage(NULL, stat_options, "a", 1);
  1607. goto out;
  1608. }
  1609. if (add_default_attributes())
  1610. goto out;
  1611. target__validate(&target);
  1612. if (perf_evlist__create_maps(evsel_list, &target) < 0) {
  1613. if (target__has_task(&target)) {
  1614. pr_err("Problems finding threads of monitor\n");
  1615. parse_options_usage(stat_usage, stat_options, "p", 1);
  1616. parse_options_usage(NULL, stat_options, "t", 1);
  1617. } else if (target__has_cpu(&target)) {
  1618. perror("failed to parse CPUs map");
  1619. parse_options_usage(stat_usage, stat_options, "C", 1);
  1620. parse_options_usage(NULL, stat_options, "a", 1);
  1621. }
  1622. goto out;
  1623. }
  1624. /*
  1625. * Initialize thread_map with comm names,
  1626. * so we could print it out on output.
  1627. */
  1628. if (stat_config.aggr_mode == AGGR_THREAD)
  1629. thread_map__read_comms(evsel_list->threads);
  1630. if (interval && interval < 100) {
  1631. if (interval < 10) {
  1632. pr_err("print interval must be >= 10ms\n");
  1633. parse_options_usage(stat_usage, stat_options, "I", 1);
  1634. goto out;
  1635. } else
  1636. pr_warning("print interval < 100ms. "
  1637. "The overhead percentage could be high in some cases. "
  1638. "Please proceed with caution.\n");
  1639. }
  1640. if (perf_evlist__alloc_stats(evsel_list, interval))
  1641. goto out;
  1642. if (perf_stat_init_aggr_mode())
  1643. goto out;
  1644. /*
  1645. * We dont want to block the signals - that would cause
  1646. * child tasks to inherit that and Ctrl-C would not work.
  1647. * What we want is for Ctrl-C to work in the exec()-ed
  1648. * task, but being ignored by perf stat itself:
  1649. */
  1650. atexit(sig_atexit);
  1651. if (!forever)
  1652. signal(SIGINT, skip_signal);
  1653. signal(SIGCHLD, skip_signal);
  1654. signal(SIGALRM, skip_signal);
  1655. signal(SIGABRT, skip_signal);
  1656. status = 0;
  1657. for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
  1658. if (run_count != 1 && verbose)
  1659. fprintf(output, "[ perf stat: executing run #%d ... ]\n",
  1660. run_idx + 1);
  1661. status = run_perf_stat(argc, argv);
  1662. if (forever && status != -1) {
  1663. print_counters(NULL, argc, argv);
  1664. perf_stat__reset_stats();
  1665. }
  1666. }
  1667. if (!forever && status != -1 && !interval)
  1668. print_counters(NULL, argc, argv);
  1669. if (STAT_RECORD) {
  1670. /*
  1671. * We synthesize the kernel mmap record just so that older tools
  1672. * don't emit warnings about not being able to resolve symbols
  1673. * due to /proc/sys/kernel/kptr_restrict settings and instear provide
  1674. * a saner message about no samples being in the perf.data file.
  1675. *
  1676. * This also serves to suppress a warning about f_header.data.size == 0
  1677. * in header.c at the moment 'perf stat record' gets introduced, which
  1678. * is not really needed once we start adding the stat specific PERF_RECORD_
  1679. * records, but the need to suppress the kptr_restrict messages in older
  1680. * tools remain -acme
  1681. */
  1682. int fd = perf_data_file__fd(&perf_stat.file);
  1683. int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
  1684. process_synthesized_event,
  1685. &perf_stat.session->machines.host);
  1686. if (err) {
  1687. pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
  1688. "older tools may produce warnings about this file\n.");
  1689. }
  1690. if (!interval) {
  1691. if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
  1692. pr_err("failed to write stat round event\n");
  1693. }
  1694. if (!perf_stat.file.is_pipe) {
  1695. perf_stat.session->header.data_size += perf_stat.bytes_written;
  1696. perf_session__write_header(perf_stat.session, evsel_list, fd, true);
  1697. }
  1698. perf_session__delete(perf_stat.session);
  1699. }
  1700. perf_stat__exit_aggr_mode();
  1701. perf_evlist__free_stats(evsel_list);
  1702. out:
  1703. perf_evlist__delete(evsel_list);
  1704. return status;
  1705. }