builtin-stat.c 64 KB

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