builtin-record.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /*
  2. * builtin-record.c
  3. *
  4. * Builtin record command: Record the profile of a workload
  5. * (or a CPU, or a PID) into the perf.data output file - for
  6. * later analysis via perf report.
  7. */
  8. #include "builtin.h"
  9. #include "perf.h"
  10. #include "util/build-id.h"
  11. #include "util/util.h"
  12. #include <subcmd/parse-options.h>
  13. #include "util/parse-events.h"
  14. #include "util/config.h"
  15. #include "util/callchain.h"
  16. #include "util/cgroup.h"
  17. #include "util/header.h"
  18. #include "util/event.h"
  19. #include "util/evlist.h"
  20. #include "util/evsel.h"
  21. #include "util/debug.h"
  22. #include "util/drv_configs.h"
  23. #include "util/session.h"
  24. #include "util/tool.h"
  25. #include "util/symbol.h"
  26. #include "util/cpumap.h"
  27. #include "util/thread_map.h"
  28. #include "util/data.h"
  29. #include "util/perf_regs.h"
  30. #include "util/auxtrace.h"
  31. #include "util/tsc.h"
  32. #include "util/parse-branch-options.h"
  33. #include "util/parse-regs-options.h"
  34. #include "util/llvm-utils.h"
  35. #include "util/bpf-loader.h"
  36. #include "util/trigger.h"
  37. #include "asm/bug.h"
  38. #include <unistd.h>
  39. #include <sched.h>
  40. #include <sys/mman.h>
  41. #include <asm/bug.h>
  42. #include <linux/time64.h>
  43. struct record {
  44. struct perf_tool tool;
  45. struct record_opts opts;
  46. u64 bytes_written;
  47. struct perf_data_file file;
  48. struct auxtrace_record *itr;
  49. struct perf_evlist *evlist;
  50. struct perf_session *session;
  51. const char *progname;
  52. int realtime_prio;
  53. bool no_buildid;
  54. bool no_buildid_set;
  55. bool no_buildid_cache;
  56. bool no_buildid_cache_set;
  57. bool buildid_all;
  58. bool timestamp_filename;
  59. bool switch_output;
  60. unsigned long long samples;
  61. };
  62. static int record__write(struct record *rec, void *bf, size_t size)
  63. {
  64. if (perf_data_file__write(rec->session->file, bf, size) < 0) {
  65. pr_err("failed to write perf data, error: %m\n");
  66. return -1;
  67. }
  68. rec->bytes_written += size;
  69. return 0;
  70. }
  71. static int process_synthesized_event(struct perf_tool *tool,
  72. union perf_event *event,
  73. struct perf_sample *sample __maybe_unused,
  74. struct machine *machine __maybe_unused)
  75. {
  76. struct record *rec = container_of(tool, struct record, tool);
  77. return record__write(rec, event, event->header.size);
  78. }
  79. static int
  80. backward_rb_find_range(void *buf, int mask, u64 head, u64 *start, u64 *end)
  81. {
  82. struct perf_event_header *pheader;
  83. u64 evt_head = head;
  84. int size = mask + 1;
  85. pr_debug2("backward_rb_find_range: buf=%p, head=%"PRIx64"\n", buf, head);
  86. pheader = (struct perf_event_header *)(buf + (head & mask));
  87. *start = head;
  88. while (true) {
  89. if (evt_head - head >= (unsigned int)size) {
  90. pr_debug("Finished reading backward ring buffer: rewind\n");
  91. if (evt_head - head > (unsigned int)size)
  92. evt_head -= pheader->size;
  93. *end = evt_head;
  94. return 0;
  95. }
  96. pheader = (struct perf_event_header *)(buf + (evt_head & mask));
  97. if (pheader->size == 0) {
  98. pr_debug("Finished reading backward ring buffer: get start\n");
  99. *end = evt_head;
  100. return 0;
  101. }
  102. evt_head += pheader->size;
  103. pr_debug3("move evt_head: %"PRIx64"\n", evt_head);
  104. }
  105. WARN_ONCE(1, "Shouldn't get here\n");
  106. return -1;
  107. }
  108. static int
  109. rb_find_range(void *data, int mask, u64 head, u64 old,
  110. u64 *start, u64 *end, bool backward)
  111. {
  112. if (!backward) {
  113. *start = old;
  114. *end = head;
  115. return 0;
  116. }
  117. return backward_rb_find_range(data, mask, head, start, end);
  118. }
  119. static int
  120. record__mmap_read(struct record *rec, struct perf_mmap *md,
  121. bool overwrite, bool backward)
  122. {
  123. u64 head = perf_mmap__read_head(md);
  124. u64 old = md->prev;
  125. u64 end = head, start = old;
  126. unsigned char *data = md->base + page_size;
  127. unsigned long size;
  128. void *buf;
  129. int rc = 0;
  130. if (rb_find_range(data, md->mask, head,
  131. old, &start, &end, backward))
  132. return -1;
  133. if (start == end)
  134. return 0;
  135. rec->samples++;
  136. size = end - start;
  137. if (size > (unsigned long)(md->mask) + 1) {
  138. WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
  139. md->prev = head;
  140. perf_mmap__consume(md, overwrite || backward);
  141. return 0;
  142. }
  143. if ((start & md->mask) + size != (end & md->mask)) {
  144. buf = &data[start & md->mask];
  145. size = md->mask + 1 - (start & md->mask);
  146. start += size;
  147. if (record__write(rec, buf, size) < 0) {
  148. rc = -1;
  149. goto out;
  150. }
  151. }
  152. buf = &data[start & md->mask];
  153. size = end - start;
  154. start += size;
  155. if (record__write(rec, buf, size) < 0) {
  156. rc = -1;
  157. goto out;
  158. }
  159. md->prev = head;
  160. perf_mmap__consume(md, overwrite || backward);
  161. out:
  162. return rc;
  163. }
  164. static volatile int done;
  165. static volatile int signr = -1;
  166. static volatile int child_finished;
  167. static volatile int auxtrace_record__snapshot_started;
  168. static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
  169. static DEFINE_TRIGGER(switch_output_trigger);
  170. static void sig_handler(int sig)
  171. {
  172. if (sig == SIGCHLD)
  173. child_finished = 1;
  174. else
  175. signr = sig;
  176. done = 1;
  177. }
  178. static void record__sig_exit(void)
  179. {
  180. if (signr == -1)
  181. return;
  182. signal(signr, SIG_DFL);
  183. raise(signr);
  184. }
  185. #ifdef HAVE_AUXTRACE_SUPPORT
  186. static int record__process_auxtrace(struct perf_tool *tool,
  187. union perf_event *event, void *data1,
  188. size_t len1, void *data2, size_t len2)
  189. {
  190. struct record *rec = container_of(tool, struct record, tool);
  191. struct perf_data_file *file = &rec->file;
  192. size_t padding;
  193. u8 pad[8] = {0};
  194. if (!perf_data_file__is_pipe(file)) {
  195. off_t file_offset;
  196. int fd = perf_data_file__fd(file);
  197. int err;
  198. file_offset = lseek(fd, 0, SEEK_CUR);
  199. if (file_offset == -1)
  200. return -1;
  201. err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
  202. event, file_offset);
  203. if (err)
  204. return err;
  205. }
  206. /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
  207. padding = (len1 + len2) & 7;
  208. if (padding)
  209. padding = 8 - padding;
  210. record__write(rec, event, event->header.size);
  211. record__write(rec, data1, len1);
  212. if (len2)
  213. record__write(rec, data2, len2);
  214. record__write(rec, &pad, padding);
  215. return 0;
  216. }
  217. static int record__auxtrace_mmap_read(struct record *rec,
  218. struct auxtrace_mmap *mm)
  219. {
  220. int ret;
  221. ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool,
  222. record__process_auxtrace);
  223. if (ret < 0)
  224. return ret;
  225. if (ret)
  226. rec->samples++;
  227. return 0;
  228. }
  229. static int record__auxtrace_mmap_read_snapshot(struct record *rec,
  230. struct auxtrace_mmap *mm)
  231. {
  232. int ret;
  233. ret = auxtrace_mmap__read_snapshot(mm, rec->itr, &rec->tool,
  234. record__process_auxtrace,
  235. rec->opts.auxtrace_snapshot_size);
  236. if (ret < 0)
  237. return ret;
  238. if (ret)
  239. rec->samples++;
  240. return 0;
  241. }
  242. static int record__auxtrace_read_snapshot_all(struct record *rec)
  243. {
  244. int i;
  245. int rc = 0;
  246. for (i = 0; i < rec->evlist->nr_mmaps; i++) {
  247. struct auxtrace_mmap *mm =
  248. &rec->evlist->mmap[i].auxtrace_mmap;
  249. if (!mm->base)
  250. continue;
  251. if (record__auxtrace_mmap_read_snapshot(rec, mm) != 0) {
  252. rc = -1;
  253. goto out;
  254. }
  255. }
  256. out:
  257. return rc;
  258. }
  259. static void record__read_auxtrace_snapshot(struct record *rec)
  260. {
  261. pr_debug("Recording AUX area tracing snapshot\n");
  262. if (record__auxtrace_read_snapshot_all(rec) < 0) {
  263. trigger_error(&auxtrace_snapshot_trigger);
  264. } else {
  265. if (auxtrace_record__snapshot_finish(rec->itr))
  266. trigger_error(&auxtrace_snapshot_trigger);
  267. else
  268. trigger_ready(&auxtrace_snapshot_trigger);
  269. }
  270. }
  271. #else
  272. static inline
  273. int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
  274. struct auxtrace_mmap *mm __maybe_unused)
  275. {
  276. return 0;
  277. }
  278. static inline
  279. void record__read_auxtrace_snapshot(struct record *rec __maybe_unused)
  280. {
  281. }
  282. static inline
  283. int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
  284. {
  285. return 0;
  286. }
  287. #endif
  288. static int record__mmap_evlist(struct record *rec,
  289. struct perf_evlist *evlist)
  290. {
  291. struct record_opts *opts = &rec->opts;
  292. char msg[512];
  293. if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false,
  294. opts->auxtrace_mmap_pages,
  295. opts->auxtrace_snapshot_mode) < 0) {
  296. if (errno == EPERM) {
  297. pr_err("Permission error mapping pages.\n"
  298. "Consider increasing "
  299. "/proc/sys/kernel/perf_event_mlock_kb,\n"
  300. "or try again with a smaller value of -m/--mmap_pages.\n"
  301. "(current value: %u,%u)\n",
  302. opts->mmap_pages, opts->auxtrace_mmap_pages);
  303. return -errno;
  304. } else {
  305. pr_err("failed to mmap with %d (%s)\n", errno,
  306. str_error_r(errno, msg, sizeof(msg)));
  307. if (errno)
  308. return -errno;
  309. else
  310. return -EINVAL;
  311. }
  312. }
  313. return 0;
  314. }
  315. static int record__mmap(struct record *rec)
  316. {
  317. return record__mmap_evlist(rec, rec->evlist);
  318. }
  319. static int record__open(struct record *rec)
  320. {
  321. char msg[512];
  322. struct perf_evsel *pos;
  323. struct perf_evlist *evlist = rec->evlist;
  324. struct perf_session *session = rec->session;
  325. struct record_opts *opts = &rec->opts;
  326. struct perf_evsel_config_term *err_term;
  327. int rc = 0;
  328. perf_evlist__config(evlist, opts, &callchain_param);
  329. evlist__for_each_entry(evlist, pos) {
  330. try_again:
  331. if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) {
  332. if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) {
  333. if (verbose)
  334. ui__warning("%s\n", msg);
  335. goto try_again;
  336. }
  337. rc = -errno;
  338. perf_evsel__open_strerror(pos, &opts->target,
  339. errno, msg, sizeof(msg));
  340. ui__error("%s\n", msg);
  341. goto out;
  342. }
  343. }
  344. if (perf_evlist__apply_filters(evlist, &pos)) {
  345. error("failed to set filter \"%s\" on event %s with %d (%s)\n",
  346. pos->filter, perf_evsel__name(pos), errno,
  347. str_error_r(errno, msg, sizeof(msg)));
  348. rc = -1;
  349. goto out;
  350. }
  351. if (perf_evlist__apply_drv_configs(evlist, &pos, &err_term)) {
  352. error("failed to set config \"%s\" on event %s with %d (%s)\n",
  353. err_term->val.drv_cfg, perf_evsel__name(pos), errno,
  354. str_error_r(errno, msg, sizeof(msg)));
  355. rc = -1;
  356. goto out;
  357. }
  358. rc = record__mmap(rec);
  359. if (rc)
  360. goto out;
  361. session->evlist = evlist;
  362. perf_session__set_id_hdr_size(session);
  363. out:
  364. return rc;
  365. }
  366. static int process_sample_event(struct perf_tool *tool,
  367. union perf_event *event,
  368. struct perf_sample *sample,
  369. struct perf_evsel *evsel,
  370. struct machine *machine)
  371. {
  372. struct record *rec = container_of(tool, struct record, tool);
  373. rec->samples++;
  374. return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
  375. }
  376. static int process_buildids(struct record *rec)
  377. {
  378. struct perf_data_file *file = &rec->file;
  379. struct perf_session *session = rec->session;
  380. if (file->size == 0)
  381. return 0;
  382. /*
  383. * During this process, it'll load kernel map and replace the
  384. * dso->long_name to a real pathname it found. In this case
  385. * we prefer the vmlinux path like
  386. * /lib/modules/3.16.4/build/vmlinux
  387. *
  388. * rather than build-id path (in debug directory).
  389. * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
  390. */
  391. symbol_conf.ignore_vmlinux_buildid = true;
  392. /*
  393. * If --buildid-all is given, it marks all DSO regardless of hits,
  394. * so no need to process samples.
  395. */
  396. if (rec->buildid_all)
  397. rec->tool.sample = NULL;
  398. return perf_session__process_events(session);
  399. }
  400. static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
  401. {
  402. int err;
  403. struct perf_tool *tool = data;
  404. /*
  405. *As for guest kernel when processing subcommand record&report,
  406. *we arrange module mmap prior to guest kernel mmap and trigger
  407. *a preload dso because default guest module symbols are loaded
  408. *from guest kallsyms instead of /lib/modules/XXX/XXX. This
  409. *method is used to avoid symbol missing when the first addr is
  410. *in module instead of in guest kernel.
  411. */
  412. err = perf_event__synthesize_modules(tool, process_synthesized_event,
  413. machine);
  414. if (err < 0)
  415. pr_err("Couldn't record guest kernel [%d]'s reference"
  416. " relocation symbol.\n", machine->pid);
  417. /*
  418. * We use _stext for guest kernel because guest kernel's /proc/kallsyms
  419. * have no _text sometimes.
  420. */
  421. err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
  422. machine);
  423. if (err < 0)
  424. pr_err("Couldn't record guest kernel [%d]'s reference"
  425. " relocation symbol.\n", machine->pid);
  426. }
  427. static struct perf_event_header finished_round_event = {
  428. .size = sizeof(struct perf_event_header),
  429. .type = PERF_RECORD_FINISHED_ROUND,
  430. };
  431. static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist,
  432. bool backward)
  433. {
  434. u64 bytes_written = rec->bytes_written;
  435. int i;
  436. int rc = 0;
  437. struct perf_mmap *maps;
  438. if (!evlist)
  439. return 0;
  440. maps = backward ? evlist->backward_mmap : evlist->mmap;
  441. if (!maps)
  442. return 0;
  443. if (backward && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
  444. return 0;
  445. for (i = 0; i < evlist->nr_mmaps; i++) {
  446. struct auxtrace_mmap *mm = &maps[i].auxtrace_mmap;
  447. if (maps[i].base) {
  448. if (record__mmap_read(rec, &maps[i],
  449. evlist->overwrite, backward) != 0) {
  450. rc = -1;
  451. goto out;
  452. }
  453. }
  454. if (mm->base && !rec->opts.auxtrace_snapshot_mode &&
  455. record__auxtrace_mmap_read(rec, mm) != 0) {
  456. rc = -1;
  457. goto out;
  458. }
  459. }
  460. /*
  461. * Mark the round finished in case we wrote
  462. * at least one event.
  463. */
  464. if (bytes_written != rec->bytes_written)
  465. rc = record__write(rec, &finished_round_event, sizeof(finished_round_event));
  466. if (backward)
  467. perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
  468. out:
  469. return rc;
  470. }
  471. static int record__mmap_read_all(struct record *rec)
  472. {
  473. int err;
  474. err = record__mmap_read_evlist(rec, rec->evlist, false);
  475. if (err)
  476. return err;
  477. return record__mmap_read_evlist(rec, rec->evlist, true);
  478. }
  479. static void record__init_features(struct record *rec)
  480. {
  481. struct perf_session *session = rec->session;
  482. int feat;
  483. for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
  484. perf_header__set_feat(&session->header, feat);
  485. if (rec->no_buildid)
  486. perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
  487. if (!have_tracepoints(&rec->evlist->entries))
  488. perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
  489. if (!rec->opts.branch_stack)
  490. perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
  491. if (!rec->opts.full_auxtrace)
  492. perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
  493. perf_header__clear_feat(&session->header, HEADER_STAT);
  494. }
  495. static void
  496. record__finish_output(struct record *rec)
  497. {
  498. struct perf_data_file *file = &rec->file;
  499. int fd = perf_data_file__fd(file);
  500. if (file->is_pipe)
  501. return;
  502. rec->session->header.data_size += rec->bytes_written;
  503. file->size = lseek(perf_data_file__fd(file), 0, SEEK_CUR);
  504. if (!rec->no_buildid) {
  505. process_buildids(rec);
  506. if (rec->buildid_all)
  507. dsos__hit_all(rec->session);
  508. }
  509. perf_session__write_header(rec->session, rec->evlist, fd, true);
  510. return;
  511. }
  512. static int record__synthesize_workload(struct record *rec, bool tail)
  513. {
  514. struct {
  515. struct thread_map map;
  516. struct thread_map_data map_data;
  517. } thread_map;
  518. if (rec->opts.tail_synthesize != tail)
  519. return 0;
  520. thread_map.map.nr = 1;
  521. thread_map.map.map[0].pid = rec->evlist->workload.pid;
  522. thread_map.map.map[0].comm = NULL;
  523. return perf_event__synthesize_thread_map(&rec->tool, &thread_map.map,
  524. process_synthesized_event,
  525. &rec->session->machines.host,
  526. rec->opts.sample_address,
  527. rec->opts.proc_map_timeout);
  528. }
  529. static int record__synthesize(struct record *rec, bool tail);
  530. static int
  531. record__switch_output(struct record *rec, bool at_exit)
  532. {
  533. struct perf_data_file *file = &rec->file;
  534. int fd, err;
  535. /* Same Size: "2015122520103046"*/
  536. char timestamp[] = "InvalidTimestamp";
  537. record__synthesize(rec, true);
  538. if (target__none(&rec->opts.target))
  539. record__synthesize_workload(rec, true);
  540. rec->samples = 0;
  541. record__finish_output(rec);
  542. err = fetch_current_timestamp(timestamp, sizeof(timestamp));
  543. if (err) {
  544. pr_err("Failed to get current timestamp\n");
  545. return -EINVAL;
  546. }
  547. fd = perf_data_file__switch(file, timestamp,
  548. rec->session->header.data_offset,
  549. at_exit);
  550. if (fd >= 0 && !at_exit) {
  551. rec->bytes_written = 0;
  552. rec->session->header.data_size = 0;
  553. }
  554. if (!quiet)
  555. fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
  556. file->path, timestamp);
  557. /* Output tracking events */
  558. if (!at_exit) {
  559. record__synthesize(rec, false);
  560. /*
  561. * In 'perf record --switch-output' without -a,
  562. * record__synthesize() in record__switch_output() won't
  563. * generate tracking events because there's no thread_map
  564. * in evlist. Which causes newly created perf.data doesn't
  565. * contain map and comm information.
  566. * Create a fake thread_map and directly call
  567. * perf_event__synthesize_thread_map() for those events.
  568. */
  569. if (target__none(&rec->opts.target))
  570. record__synthesize_workload(rec, false);
  571. }
  572. return fd;
  573. }
  574. static volatile int workload_exec_errno;
  575. /*
  576. * perf_evlist__prepare_workload will send a SIGUSR1
  577. * if the fork fails, since we asked by setting its
  578. * want_signal to true.
  579. */
  580. static void workload_exec_failed_signal(int signo __maybe_unused,
  581. siginfo_t *info,
  582. void *ucontext __maybe_unused)
  583. {
  584. workload_exec_errno = info->si_value.sival_int;
  585. done = 1;
  586. child_finished = 1;
  587. }
  588. static void snapshot_sig_handler(int sig);
  589. int __weak
  590. perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused,
  591. struct perf_tool *tool __maybe_unused,
  592. perf_event__handler_t process __maybe_unused,
  593. struct machine *machine __maybe_unused)
  594. {
  595. return 0;
  596. }
  597. static const struct perf_event_mmap_page *
  598. perf_evlist__pick_pc(struct perf_evlist *evlist)
  599. {
  600. if (evlist) {
  601. if (evlist->mmap && evlist->mmap[0].base)
  602. return evlist->mmap[0].base;
  603. if (evlist->backward_mmap && evlist->backward_mmap[0].base)
  604. return evlist->backward_mmap[0].base;
  605. }
  606. return NULL;
  607. }
  608. static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
  609. {
  610. const struct perf_event_mmap_page *pc;
  611. pc = perf_evlist__pick_pc(rec->evlist);
  612. if (pc)
  613. return pc;
  614. return NULL;
  615. }
  616. static int record__synthesize(struct record *rec, bool tail)
  617. {
  618. struct perf_session *session = rec->session;
  619. struct machine *machine = &session->machines.host;
  620. struct perf_data_file *file = &rec->file;
  621. struct record_opts *opts = &rec->opts;
  622. struct perf_tool *tool = &rec->tool;
  623. int fd = perf_data_file__fd(file);
  624. int err = 0;
  625. if (rec->opts.tail_synthesize != tail)
  626. return 0;
  627. if (file->is_pipe) {
  628. err = perf_event__synthesize_attrs(tool, session,
  629. process_synthesized_event);
  630. if (err < 0) {
  631. pr_err("Couldn't synthesize attrs.\n");
  632. goto out;
  633. }
  634. if (have_tracepoints(&rec->evlist->entries)) {
  635. /*
  636. * FIXME err <= 0 here actually means that
  637. * there were no tracepoints so its not really
  638. * an error, just that we don't need to
  639. * synthesize anything. We really have to
  640. * return this more properly and also
  641. * propagate errors that now are calling die()
  642. */
  643. err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist,
  644. process_synthesized_event);
  645. if (err <= 0) {
  646. pr_err("Couldn't record tracing data.\n");
  647. goto out;
  648. }
  649. rec->bytes_written += err;
  650. }
  651. }
  652. err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
  653. process_synthesized_event, machine);
  654. if (err)
  655. goto out;
  656. if (rec->opts.full_auxtrace) {
  657. err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
  658. session, process_synthesized_event);
  659. if (err)
  660. goto out;
  661. }
  662. err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
  663. machine);
  664. WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
  665. "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
  666. "Check /proc/kallsyms permission or run as root.\n");
  667. err = perf_event__synthesize_modules(tool, process_synthesized_event,
  668. machine);
  669. WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
  670. "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
  671. "Check /proc/modules permission or run as root.\n");
  672. if (perf_guest) {
  673. machines__process_guests(&session->machines,
  674. perf_event__synthesize_guest_os, tool);
  675. }
  676. err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads,
  677. process_synthesized_event, opts->sample_address,
  678. opts->proc_map_timeout);
  679. out:
  680. return err;
  681. }
  682. static int __cmd_record(struct record *rec, int argc, const char **argv)
  683. {
  684. int err;
  685. int status = 0;
  686. unsigned long waking = 0;
  687. const bool forks = argc > 0;
  688. struct machine *machine;
  689. struct perf_tool *tool = &rec->tool;
  690. struct record_opts *opts = &rec->opts;
  691. struct perf_data_file *file = &rec->file;
  692. struct perf_session *session;
  693. bool disabled = false, draining = false;
  694. int fd;
  695. rec->progname = argv[0];
  696. atexit(record__sig_exit);
  697. signal(SIGCHLD, sig_handler);
  698. signal(SIGINT, sig_handler);
  699. signal(SIGTERM, sig_handler);
  700. if (rec->opts.auxtrace_snapshot_mode || rec->switch_output) {
  701. signal(SIGUSR2, snapshot_sig_handler);
  702. if (rec->opts.auxtrace_snapshot_mode)
  703. trigger_on(&auxtrace_snapshot_trigger);
  704. if (rec->switch_output)
  705. trigger_on(&switch_output_trigger);
  706. } else {
  707. signal(SIGUSR2, SIG_IGN);
  708. }
  709. session = perf_session__new(file, false, tool);
  710. if (session == NULL) {
  711. pr_err("Perf session creation failed.\n");
  712. return -1;
  713. }
  714. fd = perf_data_file__fd(file);
  715. rec->session = session;
  716. record__init_features(rec);
  717. if (forks) {
  718. err = perf_evlist__prepare_workload(rec->evlist, &opts->target,
  719. argv, file->is_pipe,
  720. workload_exec_failed_signal);
  721. if (err < 0) {
  722. pr_err("Couldn't run the workload!\n");
  723. status = err;
  724. goto out_delete_session;
  725. }
  726. }
  727. if (record__open(rec) != 0) {
  728. err = -1;
  729. goto out_child;
  730. }
  731. err = bpf__apply_obj_config();
  732. if (err) {
  733. char errbuf[BUFSIZ];
  734. bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
  735. pr_err("ERROR: Apply config to BPF failed: %s\n",
  736. errbuf);
  737. goto out_child;
  738. }
  739. /*
  740. * Normally perf_session__new would do this, but it doesn't have the
  741. * evlist.
  742. */
  743. if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) {
  744. pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
  745. rec->tool.ordered_events = false;
  746. }
  747. if (!rec->evlist->nr_groups)
  748. perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
  749. if (file->is_pipe) {
  750. err = perf_header__write_pipe(fd);
  751. if (err < 0)
  752. goto out_child;
  753. } else {
  754. err = perf_session__write_header(session, rec->evlist, fd, false);
  755. if (err < 0)
  756. goto out_child;
  757. }
  758. if (!rec->no_buildid
  759. && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
  760. pr_err("Couldn't generate buildids. "
  761. "Use --no-buildid to profile anyway.\n");
  762. err = -1;
  763. goto out_child;
  764. }
  765. machine = &session->machines.host;
  766. err = record__synthesize(rec, false);
  767. if (err < 0)
  768. goto out_child;
  769. if (rec->realtime_prio) {
  770. struct sched_param param;
  771. param.sched_priority = rec->realtime_prio;
  772. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  773. pr_err("Could not set realtime priority.\n");
  774. err = -1;
  775. goto out_child;
  776. }
  777. }
  778. /*
  779. * When perf is starting the traced process, all the events
  780. * (apart from group members) have enable_on_exec=1 set,
  781. * so don't spoil it by prematurely enabling them.
  782. */
  783. if (!target__none(&opts->target) && !opts->initial_delay)
  784. perf_evlist__enable(rec->evlist);
  785. /*
  786. * Let the child rip
  787. */
  788. if (forks) {
  789. union perf_event *event;
  790. event = malloc(sizeof(event->comm) + machine->id_hdr_size);
  791. if (event == NULL) {
  792. err = -ENOMEM;
  793. goto out_child;
  794. }
  795. /*
  796. * Some H/W events are generated before COMM event
  797. * which is emitted during exec(), so perf script
  798. * cannot see a correct process name for those events.
  799. * Synthesize COMM event to prevent it.
  800. */
  801. perf_event__synthesize_comm(tool, event,
  802. rec->evlist->workload.pid,
  803. process_synthesized_event,
  804. machine);
  805. free(event);
  806. perf_evlist__start_workload(rec->evlist);
  807. }
  808. if (opts->initial_delay) {
  809. usleep(opts->initial_delay * USEC_PER_MSEC);
  810. perf_evlist__enable(rec->evlist);
  811. }
  812. trigger_ready(&auxtrace_snapshot_trigger);
  813. trigger_ready(&switch_output_trigger);
  814. for (;;) {
  815. unsigned long long hits = rec->samples;
  816. /*
  817. * rec->evlist->bkw_mmap_state is possible to be
  818. * BKW_MMAP_EMPTY here: when done == true and
  819. * hits != rec->samples in previous round.
  820. *
  821. * perf_evlist__toggle_bkw_mmap ensure we never
  822. * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
  823. */
  824. if (trigger_is_hit(&switch_output_trigger) || done || draining)
  825. perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
  826. if (record__mmap_read_all(rec) < 0) {
  827. trigger_error(&auxtrace_snapshot_trigger);
  828. trigger_error(&switch_output_trigger);
  829. err = -1;
  830. goto out_child;
  831. }
  832. if (auxtrace_record__snapshot_started) {
  833. auxtrace_record__snapshot_started = 0;
  834. if (!trigger_is_error(&auxtrace_snapshot_trigger))
  835. record__read_auxtrace_snapshot(rec);
  836. if (trigger_is_error(&auxtrace_snapshot_trigger)) {
  837. pr_err("AUX area tracing snapshot failed\n");
  838. err = -1;
  839. goto out_child;
  840. }
  841. }
  842. if (trigger_is_hit(&switch_output_trigger)) {
  843. /*
  844. * If switch_output_trigger is hit, the data in
  845. * overwritable ring buffer should have been collected,
  846. * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
  847. *
  848. * If SIGUSR2 raise after or during record__mmap_read_all(),
  849. * record__mmap_read_all() didn't collect data from
  850. * overwritable ring buffer. Read again.
  851. */
  852. if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
  853. continue;
  854. trigger_ready(&switch_output_trigger);
  855. /*
  856. * Reenable events in overwrite ring buffer after
  857. * record__mmap_read_all(): we should have collected
  858. * data from it.
  859. */
  860. perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
  861. if (!quiet)
  862. fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
  863. waking);
  864. waking = 0;
  865. fd = record__switch_output(rec, false);
  866. if (fd < 0) {
  867. pr_err("Failed to switch to new file\n");
  868. trigger_error(&switch_output_trigger);
  869. err = fd;
  870. goto out_child;
  871. }
  872. }
  873. if (hits == rec->samples) {
  874. if (done || draining)
  875. break;
  876. err = perf_evlist__poll(rec->evlist, -1);
  877. /*
  878. * Propagate error, only if there's any. Ignore positive
  879. * number of returned events and interrupt error.
  880. */
  881. if (err > 0 || (err < 0 && errno == EINTR))
  882. err = 0;
  883. waking++;
  884. if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
  885. draining = true;
  886. }
  887. /*
  888. * When perf is starting the traced process, at the end events
  889. * die with the process and we wait for that. Thus no need to
  890. * disable events in this case.
  891. */
  892. if (done && !disabled && !target__none(&opts->target)) {
  893. trigger_off(&auxtrace_snapshot_trigger);
  894. perf_evlist__disable(rec->evlist);
  895. disabled = true;
  896. }
  897. }
  898. trigger_off(&auxtrace_snapshot_trigger);
  899. trigger_off(&switch_output_trigger);
  900. if (forks && workload_exec_errno) {
  901. char msg[STRERR_BUFSIZE];
  902. const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
  903. pr_err("Workload failed: %s\n", emsg);
  904. err = -1;
  905. goto out_child;
  906. }
  907. if (!quiet)
  908. fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
  909. if (target__none(&rec->opts.target))
  910. record__synthesize_workload(rec, true);
  911. out_child:
  912. if (forks) {
  913. int exit_status;
  914. if (!child_finished)
  915. kill(rec->evlist->workload.pid, SIGTERM);
  916. wait(&exit_status);
  917. if (err < 0)
  918. status = err;
  919. else if (WIFEXITED(exit_status))
  920. status = WEXITSTATUS(exit_status);
  921. else if (WIFSIGNALED(exit_status))
  922. signr = WTERMSIG(exit_status);
  923. } else
  924. status = err;
  925. record__synthesize(rec, true);
  926. /* this will be recalculated during process_buildids() */
  927. rec->samples = 0;
  928. if (!err) {
  929. if (!rec->timestamp_filename) {
  930. record__finish_output(rec);
  931. } else {
  932. fd = record__switch_output(rec, true);
  933. if (fd < 0) {
  934. status = fd;
  935. goto out_delete_session;
  936. }
  937. }
  938. }
  939. if (!err && !quiet) {
  940. char samples[128];
  941. const char *postfix = rec->timestamp_filename ?
  942. ".<timestamp>" : "";
  943. if (rec->samples && !rec->opts.full_auxtrace)
  944. scnprintf(samples, sizeof(samples),
  945. " (%" PRIu64 " samples)", rec->samples);
  946. else
  947. samples[0] = '\0';
  948. fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n",
  949. perf_data_file__size(file) / 1024.0 / 1024.0,
  950. file->path, postfix, samples);
  951. }
  952. out_delete_session:
  953. perf_session__delete(session);
  954. return status;
  955. }
  956. static void callchain_debug(struct callchain_param *callchain)
  957. {
  958. static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
  959. pr_debug("callchain: type %s\n", str[callchain->record_mode]);
  960. if (callchain->record_mode == CALLCHAIN_DWARF)
  961. pr_debug("callchain: stack dump size %d\n",
  962. callchain->dump_size);
  963. }
  964. int record_opts__parse_callchain(struct record_opts *record,
  965. struct callchain_param *callchain,
  966. const char *arg, bool unset)
  967. {
  968. int ret;
  969. callchain->enabled = !unset;
  970. /* --no-call-graph */
  971. if (unset) {
  972. callchain->record_mode = CALLCHAIN_NONE;
  973. pr_debug("callchain: disabled\n");
  974. return 0;
  975. }
  976. ret = parse_callchain_record_opt(arg, callchain);
  977. if (!ret) {
  978. /* Enable data address sampling for DWARF unwind. */
  979. if (callchain->record_mode == CALLCHAIN_DWARF)
  980. record->sample_address = true;
  981. callchain_debug(callchain);
  982. }
  983. return ret;
  984. }
  985. int record_parse_callchain_opt(const struct option *opt,
  986. const char *arg,
  987. int unset)
  988. {
  989. return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
  990. }
  991. int record_callchain_opt(const struct option *opt,
  992. const char *arg __maybe_unused,
  993. int unset __maybe_unused)
  994. {
  995. struct callchain_param *callchain = opt->value;
  996. callchain->enabled = true;
  997. if (callchain->record_mode == CALLCHAIN_NONE)
  998. callchain->record_mode = CALLCHAIN_FP;
  999. callchain_debug(callchain);
  1000. return 0;
  1001. }
  1002. static int perf_record_config(const char *var, const char *value, void *cb)
  1003. {
  1004. struct record *rec = cb;
  1005. if (!strcmp(var, "record.build-id")) {
  1006. if (!strcmp(value, "cache"))
  1007. rec->no_buildid_cache = false;
  1008. else if (!strcmp(value, "no-cache"))
  1009. rec->no_buildid_cache = true;
  1010. else if (!strcmp(value, "skip"))
  1011. rec->no_buildid = true;
  1012. else
  1013. return -1;
  1014. return 0;
  1015. }
  1016. if (!strcmp(var, "record.call-graph"))
  1017. var = "call-graph.record-mode"; /* fall-through */
  1018. return perf_default_config(var, value, cb);
  1019. }
  1020. struct clockid_map {
  1021. const char *name;
  1022. int clockid;
  1023. };
  1024. #define CLOCKID_MAP(n, c) \
  1025. { .name = n, .clockid = (c), }
  1026. #define CLOCKID_END { .name = NULL, }
  1027. /*
  1028. * Add the missing ones, we need to build on many distros...
  1029. */
  1030. #ifndef CLOCK_MONOTONIC_RAW
  1031. #define CLOCK_MONOTONIC_RAW 4
  1032. #endif
  1033. #ifndef CLOCK_BOOTTIME
  1034. #define CLOCK_BOOTTIME 7
  1035. #endif
  1036. #ifndef CLOCK_TAI
  1037. #define CLOCK_TAI 11
  1038. #endif
  1039. static const struct clockid_map clockids[] = {
  1040. /* available for all events, NMI safe */
  1041. CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
  1042. CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
  1043. /* available for some events */
  1044. CLOCKID_MAP("realtime", CLOCK_REALTIME),
  1045. CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
  1046. CLOCKID_MAP("tai", CLOCK_TAI),
  1047. /* available for the lazy */
  1048. CLOCKID_MAP("mono", CLOCK_MONOTONIC),
  1049. CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
  1050. CLOCKID_MAP("real", CLOCK_REALTIME),
  1051. CLOCKID_MAP("boot", CLOCK_BOOTTIME),
  1052. CLOCKID_END,
  1053. };
  1054. static int parse_clockid(const struct option *opt, const char *str, int unset)
  1055. {
  1056. struct record_opts *opts = (struct record_opts *)opt->value;
  1057. const struct clockid_map *cm;
  1058. const char *ostr = str;
  1059. if (unset) {
  1060. opts->use_clockid = 0;
  1061. return 0;
  1062. }
  1063. /* no arg passed */
  1064. if (!str)
  1065. return 0;
  1066. /* no setting it twice */
  1067. if (opts->use_clockid)
  1068. return -1;
  1069. opts->use_clockid = true;
  1070. /* if its a number, we're done */
  1071. if (sscanf(str, "%d", &opts->clockid) == 1)
  1072. return 0;
  1073. /* allow a "CLOCK_" prefix to the name */
  1074. if (!strncasecmp(str, "CLOCK_", 6))
  1075. str += 6;
  1076. for (cm = clockids; cm->name; cm++) {
  1077. if (!strcasecmp(str, cm->name)) {
  1078. opts->clockid = cm->clockid;
  1079. return 0;
  1080. }
  1081. }
  1082. opts->use_clockid = false;
  1083. ui__warning("unknown clockid %s, check man page\n", ostr);
  1084. return -1;
  1085. }
  1086. static int record__parse_mmap_pages(const struct option *opt,
  1087. const char *str,
  1088. int unset __maybe_unused)
  1089. {
  1090. struct record_opts *opts = opt->value;
  1091. char *s, *p;
  1092. unsigned int mmap_pages;
  1093. int ret;
  1094. if (!str)
  1095. return -EINVAL;
  1096. s = strdup(str);
  1097. if (!s)
  1098. return -ENOMEM;
  1099. p = strchr(s, ',');
  1100. if (p)
  1101. *p = '\0';
  1102. if (*s) {
  1103. ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s);
  1104. if (ret)
  1105. goto out_free;
  1106. opts->mmap_pages = mmap_pages;
  1107. }
  1108. if (!p) {
  1109. ret = 0;
  1110. goto out_free;
  1111. }
  1112. ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1);
  1113. if (ret)
  1114. goto out_free;
  1115. opts->auxtrace_mmap_pages = mmap_pages;
  1116. out_free:
  1117. free(s);
  1118. return ret;
  1119. }
  1120. static const char * const __record_usage[] = {
  1121. "perf record [<options>] [<command>]",
  1122. "perf record [<options>] -- <command> [<options>]",
  1123. NULL
  1124. };
  1125. const char * const *record_usage = __record_usage;
  1126. /*
  1127. * XXX Ideally would be local to cmd_record() and passed to a record__new
  1128. * because we need to have access to it in record__exit, that is called
  1129. * after cmd_record() exits, but since record_options need to be accessible to
  1130. * builtin-script, leave it here.
  1131. *
  1132. * At least we don't ouch it in all the other functions here directly.
  1133. *
  1134. * Just say no to tons of global variables, sigh.
  1135. */
  1136. static struct record record = {
  1137. .opts = {
  1138. .sample_time = true,
  1139. .mmap_pages = UINT_MAX,
  1140. .user_freq = UINT_MAX,
  1141. .user_interval = ULLONG_MAX,
  1142. .freq = 4000,
  1143. .target = {
  1144. .uses_mmap = true,
  1145. .default_per_cpu = true,
  1146. },
  1147. .proc_map_timeout = 500,
  1148. },
  1149. .tool = {
  1150. .sample = process_sample_event,
  1151. .fork = perf_event__process_fork,
  1152. .exit = perf_event__process_exit,
  1153. .comm = perf_event__process_comm,
  1154. .mmap = perf_event__process_mmap,
  1155. .mmap2 = perf_event__process_mmap2,
  1156. .ordered_events = true,
  1157. },
  1158. };
  1159. const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
  1160. "\n\t\t\t\tDefault: fp";
  1161. static bool dry_run;
  1162. /*
  1163. * XXX Will stay a global variable till we fix builtin-script.c to stop messing
  1164. * with it and switch to use the library functions in perf_evlist that came
  1165. * from builtin-record.c, i.e. use record_opts,
  1166. * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
  1167. * using pipes, etc.
  1168. */
  1169. struct option __record_options[] = {
  1170. OPT_CALLBACK('e', "event", &record.evlist, "event",
  1171. "event selector. use 'perf list' to list available events",
  1172. parse_events_option),
  1173. OPT_CALLBACK(0, "filter", &record.evlist, "filter",
  1174. "event filter", parse_filter),
  1175. OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
  1176. NULL, "don't record events from perf itself",
  1177. exclude_perf),
  1178. OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
  1179. "record events on existing process id"),
  1180. OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
  1181. "record events on existing thread id"),
  1182. OPT_INTEGER('r', "realtime", &record.realtime_prio,
  1183. "collect data with this RT SCHED_FIFO priority"),
  1184. OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
  1185. "collect data without buffering"),
  1186. OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
  1187. "collect raw sample records from all opened counters"),
  1188. OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
  1189. "system-wide collection from all CPUs"),
  1190. OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
  1191. "list of cpus to monitor"),
  1192. OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
  1193. OPT_STRING('o', "output", &record.file.path, "file",
  1194. "output file name"),
  1195. OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
  1196. &record.opts.no_inherit_set,
  1197. "child tasks do not inherit counters"),
  1198. OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
  1199. "synthesize non-sample events at the end of output"),
  1200. OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
  1201. OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
  1202. OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
  1203. "number of mmap data pages and AUX area tracing mmap pages",
  1204. record__parse_mmap_pages),
  1205. OPT_BOOLEAN(0, "group", &record.opts.group,
  1206. "put the counters into a counter group"),
  1207. OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
  1208. NULL, "enables call-graph recording" ,
  1209. &record_callchain_opt),
  1210. OPT_CALLBACK(0, "call-graph", &record.opts,
  1211. "record_mode[,record_size]", record_callchain_help,
  1212. &record_parse_callchain_opt),
  1213. OPT_INCR('v', "verbose", &verbose,
  1214. "be more verbose (show counter open errors, etc)"),
  1215. OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
  1216. OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
  1217. "per thread counts"),
  1218. OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
  1219. OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
  1220. OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
  1221. &record.opts.sample_time_set,
  1222. "Record the sample timestamps"),
  1223. OPT_BOOLEAN('P', "period", &record.opts.period, "Record the sample period"),
  1224. OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
  1225. "don't sample"),
  1226. OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
  1227. &record.no_buildid_cache_set,
  1228. "do not update the buildid cache"),
  1229. OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
  1230. &record.no_buildid_set,
  1231. "do not collect buildids in perf.data"),
  1232. OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
  1233. "monitor event in cgroup name only",
  1234. parse_cgroups),
  1235. OPT_UINTEGER('D', "delay", &record.opts.initial_delay,
  1236. "ms to wait before starting measurement after program start"),
  1237. OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
  1238. "user to profile"),
  1239. OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
  1240. "branch any", "sample any taken branches",
  1241. parse_branch_stack),
  1242. OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
  1243. "branch filter mask", "branch stack filter modes",
  1244. parse_branch_stack),
  1245. OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
  1246. "sample by weight (on special events only)"),
  1247. OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
  1248. "sample transaction flags (special events only)"),
  1249. OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
  1250. "use per-thread mmaps"),
  1251. OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
  1252. "sample selected machine registers on interrupt,"
  1253. " use -I ? to list register names", parse_regs),
  1254. OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
  1255. "Record running/enabled time of read (:S) events"),
  1256. OPT_CALLBACK('k', "clockid", &record.opts,
  1257. "clockid", "clockid to use for events, see clock_gettime()",
  1258. parse_clockid),
  1259. OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
  1260. "opts", "AUX area tracing Snapshot Mode", ""),
  1261. OPT_UINTEGER(0, "proc-map-timeout", &record.opts.proc_map_timeout,
  1262. "per thread proc mmap processing timeout in ms"),
  1263. OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events,
  1264. "Record context switch events"),
  1265. OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
  1266. "Configure all used events to run in kernel space.",
  1267. PARSE_OPT_EXCLUSIVE),
  1268. OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
  1269. "Configure all used events to run in user space.",
  1270. PARSE_OPT_EXCLUSIVE),
  1271. OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
  1272. "clang binary to use for compiling BPF scriptlets"),
  1273. OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
  1274. "options passed to clang when compiling BPF scriptlets"),
  1275. OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
  1276. "file", "vmlinux pathname"),
  1277. OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
  1278. "Record build-id of all DSOs regardless of hits"),
  1279. OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
  1280. "append timestamp to output filename"),
  1281. OPT_BOOLEAN(0, "switch-output", &record.switch_output,
  1282. "Switch output when receive SIGUSR2"),
  1283. OPT_BOOLEAN(0, "dry-run", &dry_run,
  1284. "Parse options then exit"),
  1285. OPT_END()
  1286. };
  1287. struct option *record_options = __record_options;
  1288. int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
  1289. {
  1290. int err;
  1291. struct record *rec = &record;
  1292. char errbuf[BUFSIZ];
  1293. #ifndef HAVE_LIBBPF_SUPPORT
  1294. # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
  1295. set_nobuild('\0', "clang-path", true);
  1296. set_nobuild('\0', "clang-opt", true);
  1297. # undef set_nobuild
  1298. #endif
  1299. #ifndef HAVE_BPF_PROLOGUE
  1300. # if !defined (HAVE_DWARF_SUPPORT)
  1301. # define REASON "NO_DWARF=1"
  1302. # elif !defined (HAVE_LIBBPF_SUPPORT)
  1303. # define REASON "NO_LIBBPF=1"
  1304. # else
  1305. # define REASON "this architecture doesn't support BPF prologue"
  1306. # endif
  1307. # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
  1308. set_nobuild('\0', "vmlinux", true);
  1309. # undef set_nobuild
  1310. # undef REASON
  1311. #endif
  1312. rec->evlist = perf_evlist__new();
  1313. if (rec->evlist == NULL)
  1314. return -ENOMEM;
  1315. perf_config(perf_record_config, rec);
  1316. argc = parse_options(argc, argv, record_options, record_usage,
  1317. PARSE_OPT_STOP_AT_NON_OPTION);
  1318. if (!argc && target__none(&rec->opts.target))
  1319. usage_with_options(record_usage, record_options);
  1320. if (nr_cgroups && !rec->opts.target.system_wide) {
  1321. usage_with_options_msg(record_usage, record_options,
  1322. "cgroup monitoring only available in system-wide mode");
  1323. }
  1324. if (rec->opts.record_switch_events &&
  1325. !perf_can_record_switch_events()) {
  1326. ui__error("kernel does not support recording context switch events\n");
  1327. parse_options_usage(record_usage, record_options, "switch-events", 0);
  1328. return -EINVAL;
  1329. }
  1330. if (rec->switch_output)
  1331. rec->timestamp_filename = true;
  1332. if (!rec->itr) {
  1333. rec->itr = auxtrace_record__init(rec->evlist, &err);
  1334. if (err)
  1335. goto out;
  1336. }
  1337. err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
  1338. rec->opts.auxtrace_snapshot_opts);
  1339. if (err)
  1340. goto out;
  1341. /*
  1342. * Allow aliases to facilitate the lookup of symbols for address
  1343. * filters. Refer to auxtrace_parse_filters().
  1344. */
  1345. symbol_conf.allow_aliases = true;
  1346. symbol__init(NULL);
  1347. err = auxtrace_parse_filters(rec->evlist);
  1348. if (err)
  1349. goto out;
  1350. if (dry_run)
  1351. goto out;
  1352. err = bpf__setup_stdout(rec->evlist);
  1353. if (err) {
  1354. bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
  1355. pr_err("ERROR: Setup BPF stdout failed: %s\n",
  1356. errbuf);
  1357. goto out;
  1358. }
  1359. err = -ENOMEM;
  1360. if (symbol_conf.kptr_restrict)
  1361. pr_warning(
  1362. "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
  1363. "check /proc/sys/kernel/kptr_restrict.\n\n"
  1364. "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
  1365. "file is not found in the buildid cache or in the vmlinux path.\n\n"
  1366. "Samples in kernel modules won't be resolved at all.\n\n"
  1367. "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
  1368. "even with a suitable vmlinux or kallsyms file.\n\n");
  1369. if (rec->no_buildid_cache || rec->no_buildid) {
  1370. disable_buildid_cache();
  1371. } else if (rec->switch_output) {
  1372. /*
  1373. * In 'perf record --switch-output', disable buildid
  1374. * generation by default to reduce data file switching
  1375. * overhead. Still generate buildid if they are required
  1376. * explicitly using
  1377. *
  1378. * perf record --signal-trigger --no-no-buildid \
  1379. * --no-no-buildid-cache
  1380. *
  1381. * Following code equals to:
  1382. *
  1383. * if ((rec->no_buildid || !rec->no_buildid_set) &&
  1384. * (rec->no_buildid_cache || !rec->no_buildid_cache_set))
  1385. * disable_buildid_cache();
  1386. */
  1387. bool disable = true;
  1388. if (rec->no_buildid_set && !rec->no_buildid)
  1389. disable = false;
  1390. if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
  1391. disable = false;
  1392. if (disable) {
  1393. rec->no_buildid = true;
  1394. rec->no_buildid_cache = true;
  1395. disable_buildid_cache();
  1396. }
  1397. }
  1398. if (record.opts.overwrite)
  1399. record.opts.tail_synthesize = true;
  1400. if (rec->evlist->nr_entries == 0 &&
  1401. perf_evlist__add_default(rec->evlist) < 0) {
  1402. pr_err("Not enough memory for event selector list\n");
  1403. goto out;
  1404. }
  1405. if (rec->opts.target.tid && !rec->opts.no_inherit_set)
  1406. rec->opts.no_inherit = true;
  1407. err = target__validate(&rec->opts.target);
  1408. if (err) {
  1409. target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
  1410. ui__warning("%s", errbuf);
  1411. }
  1412. err = target__parse_uid(&rec->opts.target);
  1413. if (err) {
  1414. int saved_errno = errno;
  1415. target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
  1416. ui__error("%s", errbuf);
  1417. err = -saved_errno;
  1418. goto out;
  1419. }
  1420. err = -ENOMEM;
  1421. if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0)
  1422. usage_with_options(record_usage, record_options);
  1423. err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
  1424. if (err)
  1425. goto out;
  1426. /*
  1427. * We take all buildids when the file contains
  1428. * AUX area tracing data because we do not decode the
  1429. * trace because it would take too long.
  1430. */
  1431. if (rec->opts.full_auxtrace)
  1432. rec->buildid_all = true;
  1433. if (record_opts__config(&rec->opts)) {
  1434. err = -EINVAL;
  1435. goto out;
  1436. }
  1437. err = __cmd_record(&record, argc, argv);
  1438. out:
  1439. perf_evlist__delete(rec->evlist);
  1440. symbol__exit();
  1441. auxtrace_record__free(rec->itr);
  1442. return err;
  1443. }
  1444. static void snapshot_sig_handler(int sig __maybe_unused)
  1445. {
  1446. if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
  1447. trigger_hit(&auxtrace_snapshot_trigger);
  1448. auxtrace_record__snapshot_started = 1;
  1449. if (auxtrace_record__snapshot_start(record.itr))
  1450. trigger_error(&auxtrace_snapshot_trigger);
  1451. }
  1452. if (trigger_is_ready(&switch_output_trigger))
  1453. trigger_hit(&switch_output_trigger);
  1454. }