builtin-record.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. #define _FILE_OFFSET_BITS 64
  9. #include "builtin.h"
  10. #include "perf.h"
  11. #include "util/build-id.h"
  12. #include "util/util.h"
  13. #include "util/parse-options.h"
  14. #include "util/parse-events.h"
  15. #include "util/header.h"
  16. #include "util/event.h"
  17. #include "util/evlist.h"
  18. #include "util/evsel.h"
  19. #include "util/debug.h"
  20. #include "util/session.h"
  21. #include "util/tool.h"
  22. #include "util/symbol.h"
  23. #include "util/cpumap.h"
  24. #include "util/thread_map.h"
  25. #include <unistd.h>
  26. #include <sched.h>
  27. #include <sys/mman.h>
  28. enum write_mode_t {
  29. WRITE_FORCE,
  30. WRITE_APPEND
  31. };
  32. struct perf_record {
  33. struct perf_tool tool;
  34. struct perf_record_opts opts;
  35. u64 bytes_written;
  36. const char *output_name;
  37. struct perf_evlist *evlist;
  38. struct perf_session *session;
  39. const char *progname;
  40. int output;
  41. unsigned int page_size;
  42. int realtime_prio;
  43. enum write_mode_t write_mode;
  44. bool no_buildid;
  45. bool no_buildid_cache;
  46. bool force;
  47. bool file_new;
  48. bool append_file;
  49. long samples;
  50. off_t post_processing_offset;
  51. };
  52. static void advance_output(struct perf_record *rec, size_t size)
  53. {
  54. rec->bytes_written += size;
  55. }
  56. static void write_output(struct perf_record *rec, void *buf, size_t size)
  57. {
  58. while (size) {
  59. int ret = write(rec->output, buf, size);
  60. if (ret < 0)
  61. die("failed to write");
  62. size -= ret;
  63. buf += ret;
  64. rec->bytes_written += ret;
  65. }
  66. }
  67. static int process_synthesized_event(struct perf_tool *tool,
  68. union perf_event *event,
  69. struct perf_sample *sample __used,
  70. struct machine *machine __used)
  71. {
  72. struct perf_record *rec = container_of(tool, struct perf_record, tool);
  73. write_output(rec, event, event->header.size);
  74. return 0;
  75. }
  76. static void perf_record__mmap_read(struct perf_record *rec,
  77. struct perf_mmap *md)
  78. {
  79. unsigned int head = perf_mmap__read_head(md);
  80. unsigned int old = md->prev;
  81. unsigned char *data = md->base + rec->page_size;
  82. unsigned long size;
  83. void *buf;
  84. if (old == head)
  85. return;
  86. rec->samples++;
  87. size = head - old;
  88. if ((old & md->mask) + size != (head & md->mask)) {
  89. buf = &data[old & md->mask];
  90. size = md->mask + 1 - (old & md->mask);
  91. old += size;
  92. write_output(rec, buf, size);
  93. }
  94. buf = &data[old & md->mask];
  95. size = head - old;
  96. old += size;
  97. write_output(rec, buf, size);
  98. md->prev = old;
  99. perf_mmap__write_tail(md, old);
  100. }
  101. static volatile int done = 0;
  102. static volatile int signr = -1;
  103. static volatile int child_finished = 0;
  104. static void sig_handler(int sig)
  105. {
  106. if (sig == SIGCHLD)
  107. child_finished = 1;
  108. done = 1;
  109. signr = sig;
  110. }
  111. static void perf_record__sig_exit(int exit_status __used, void *arg)
  112. {
  113. struct perf_record *rec = arg;
  114. int status;
  115. if (rec->evlist->workload.pid > 0) {
  116. if (!child_finished)
  117. kill(rec->evlist->workload.pid, SIGTERM);
  118. wait(&status);
  119. if (WIFSIGNALED(status))
  120. psignal(WTERMSIG(status), rec->progname);
  121. }
  122. if (signr == -1 || signr == SIGUSR1)
  123. return;
  124. signal(signr, SIG_DFL);
  125. kill(getpid(), signr);
  126. }
  127. static bool perf_evlist__equal(struct perf_evlist *evlist,
  128. struct perf_evlist *other)
  129. {
  130. struct perf_evsel *pos, *pair;
  131. if (evlist->nr_entries != other->nr_entries)
  132. return false;
  133. pair = list_entry(other->entries.next, struct perf_evsel, node);
  134. list_for_each_entry(pos, &evlist->entries, node) {
  135. if (memcmp(&pos->attr, &pair->attr, sizeof(pos->attr) != 0))
  136. return false;
  137. pair = list_entry(pair->node.next, struct perf_evsel, node);
  138. }
  139. return true;
  140. }
  141. static void perf_record__open(struct perf_record *rec)
  142. {
  143. struct perf_evsel *pos, *first;
  144. struct perf_evlist *evlist = rec->evlist;
  145. struct perf_session *session = rec->session;
  146. struct perf_record_opts *opts = &rec->opts;
  147. first = list_entry(evlist->entries.next, struct perf_evsel, node);
  148. perf_evlist__config_attrs(evlist, opts);
  149. list_for_each_entry(pos, &evlist->entries, node) {
  150. struct perf_event_attr *attr = &pos->attr;
  151. struct xyarray *group_fd = NULL;
  152. /*
  153. * Check if parse_single_tracepoint_event has already asked for
  154. * PERF_SAMPLE_TIME.
  155. *
  156. * XXX this is kludgy but short term fix for problems introduced by
  157. * eac23d1c that broke 'perf script' by having different sample_types
  158. * when using multiple tracepoint events when we use a perf binary
  159. * that tries to use sample_id_all on an older kernel.
  160. *
  161. * We need to move counter creation to perf_session, support
  162. * different sample_types, etc.
  163. */
  164. bool time_needed = attr->sample_type & PERF_SAMPLE_TIME;
  165. if (opts->group && pos != first)
  166. group_fd = first->fd;
  167. retry_sample_id:
  168. attr->sample_id_all = opts->sample_id_all_avail ? 1 : 0;
  169. try_again:
  170. if (perf_evsel__open(pos, evlist->cpus, evlist->threads,
  171. opts->group, group_fd) < 0) {
  172. int err = errno;
  173. if (err == EPERM || err == EACCES) {
  174. ui__error_paranoid();
  175. exit(EXIT_FAILURE);
  176. } else if (err == ENODEV && opts->cpu_list) {
  177. die("No such device - did you specify"
  178. " an out-of-range profile CPU?\n");
  179. } else if (err == EINVAL && opts->sample_id_all_avail) {
  180. /*
  181. * Old kernel, no attr->sample_id_type_all field
  182. */
  183. opts->sample_id_all_avail = false;
  184. if (!opts->sample_time && !opts->raw_samples && !time_needed)
  185. attr->sample_type &= ~PERF_SAMPLE_TIME;
  186. goto retry_sample_id;
  187. }
  188. /*
  189. * If it's cycles then fall back to hrtimer
  190. * based cpu-clock-tick sw counter, which
  191. * is always available even if no PMU support:
  192. */
  193. if (attr->type == PERF_TYPE_HARDWARE
  194. && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  195. if (verbose)
  196. ui__warning("The cycles event is not supported, "
  197. "trying to fall back to cpu-clock-ticks\n");
  198. attr->type = PERF_TYPE_SOFTWARE;
  199. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  200. goto try_again;
  201. }
  202. if (err == ENOENT) {
  203. ui__warning("The %s event is not supported.\n",
  204. event_name(pos));
  205. exit(EXIT_FAILURE);
  206. }
  207. printf("\n");
  208. error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
  209. err, strerror(err));
  210. #if defined(__i386__) || defined(__x86_64__)
  211. if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
  212. die("No hardware sampling interrupt available."
  213. " No APIC? If so then you can boot the kernel"
  214. " with the \"lapic\" boot parameter to"
  215. " force-enable it.\n");
  216. #endif
  217. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  218. }
  219. }
  220. if (perf_evlist__set_filters(evlist)) {
  221. error("failed to set filter with %d (%s)\n", errno,
  222. strerror(errno));
  223. exit(-1);
  224. }
  225. if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) {
  226. if (errno == EPERM)
  227. die("Permission error mapping pages.\n"
  228. "Consider increasing "
  229. "/proc/sys/kernel/perf_event_mlock_kb,\n"
  230. "or try again with a smaller value of -m/--mmap_pages.\n"
  231. "(current value: %d)\n", opts->mmap_pages);
  232. else if (!is_power_of_2(opts->mmap_pages))
  233. die("--mmap_pages/-m value must be a power of two.");
  234. die("failed to mmap with %d (%s)\n", errno, strerror(errno));
  235. }
  236. if (rec->file_new)
  237. session->evlist = evlist;
  238. else {
  239. if (!perf_evlist__equal(session->evlist, evlist)) {
  240. fprintf(stderr, "incompatible append\n");
  241. exit(-1);
  242. }
  243. }
  244. perf_session__update_sample_type(session);
  245. }
  246. static int process_buildids(struct perf_record *rec)
  247. {
  248. u64 size = lseek(rec->output, 0, SEEK_CUR);
  249. if (size == 0)
  250. return 0;
  251. rec->session->fd = rec->output;
  252. return __perf_session__process_events(rec->session, rec->post_processing_offset,
  253. size - rec->post_processing_offset,
  254. size, &build_id__mark_dso_hit_ops);
  255. }
  256. static void perf_record__exit(int status __used, void *arg)
  257. {
  258. struct perf_record *rec = arg;
  259. if (!rec->opts.pipe_output) {
  260. rec->session->header.data_size += rec->bytes_written;
  261. if (!rec->no_buildid)
  262. process_buildids(rec);
  263. perf_session__write_header(rec->session, rec->evlist,
  264. rec->output, true);
  265. perf_session__delete(rec->session);
  266. perf_evlist__delete(rec->evlist);
  267. symbol__exit();
  268. }
  269. }
  270. static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
  271. {
  272. int err;
  273. struct perf_tool *tool = data;
  274. if (machine__is_host(machine))
  275. return;
  276. /*
  277. *As for guest kernel when processing subcommand record&report,
  278. *we arrange module mmap prior to guest kernel mmap and trigger
  279. *a preload dso because default guest module symbols are loaded
  280. *from guest kallsyms instead of /lib/modules/XXX/XXX. This
  281. *method is used to avoid symbol missing when the first addr is
  282. *in module instead of in guest kernel.
  283. */
  284. err = perf_event__synthesize_modules(tool, process_synthesized_event,
  285. machine);
  286. if (err < 0)
  287. pr_err("Couldn't record guest kernel [%d]'s reference"
  288. " relocation symbol.\n", machine->pid);
  289. /*
  290. * We use _stext for guest kernel because guest kernel's /proc/kallsyms
  291. * have no _text sometimes.
  292. */
  293. err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
  294. machine, "_text");
  295. if (err < 0)
  296. err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
  297. machine, "_stext");
  298. if (err < 0)
  299. pr_err("Couldn't record guest kernel [%d]'s reference"
  300. " relocation symbol.\n", machine->pid);
  301. }
  302. static struct perf_event_header finished_round_event = {
  303. .size = sizeof(struct perf_event_header),
  304. .type = PERF_RECORD_FINISHED_ROUND,
  305. };
  306. static void perf_record__mmap_read_all(struct perf_record *rec)
  307. {
  308. int i;
  309. for (i = 0; i < rec->evlist->nr_mmaps; i++) {
  310. if (rec->evlist->mmap[i].base)
  311. perf_record__mmap_read(rec, &rec->evlist->mmap[i]);
  312. }
  313. if (perf_header__has_feat(&rec->session->header, HEADER_TRACE_INFO))
  314. write_output(rec, &finished_round_event, sizeof(finished_round_event));
  315. }
  316. static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
  317. {
  318. struct stat st;
  319. int flags;
  320. int err, output;
  321. unsigned long waking = 0;
  322. const bool forks = argc > 0;
  323. struct machine *machine;
  324. struct perf_tool *tool = &rec->tool;
  325. struct perf_record_opts *opts = &rec->opts;
  326. struct perf_evlist *evsel_list = rec->evlist;
  327. const char *output_name = rec->output_name;
  328. struct perf_session *session;
  329. rec->progname = argv[0];
  330. rec->page_size = sysconf(_SC_PAGE_SIZE);
  331. on_exit(perf_record__sig_exit, rec);
  332. signal(SIGCHLD, sig_handler);
  333. signal(SIGINT, sig_handler);
  334. signal(SIGUSR1, sig_handler);
  335. if (!output_name) {
  336. if (!fstat(STDOUT_FILENO, &st) && S_ISFIFO(st.st_mode))
  337. opts->pipe_output = true;
  338. else
  339. rec->output_name = output_name = "perf.data";
  340. }
  341. if (output_name) {
  342. if (!strcmp(output_name, "-"))
  343. opts->pipe_output = true;
  344. else if (!stat(output_name, &st) && st.st_size) {
  345. if (rec->write_mode == WRITE_FORCE) {
  346. char oldname[PATH_MAX];
  347. snprintf(oldname, sizeof(oldname), "%s.old",
  348. output_name);
  349. unlink(oldname);
  350. rename(output_name, oldname);
  351. }
  352. } else if (rec->write_mode == WRITE_APPEND) {
  353. rec->write_mode = WRITE_FORCE;
  354. }
  355. }
  356. flags = O_CREAT|O_RDWR;
  357. if (rec->write_mode == WRITE_APPEND)
  358. rec->file_new = 0;
  359. else
  360. flags |= O_TRUNC;
  361. if (opts->pipe_output)
  362. output = STDOUT_FILENO;
  363. else
  364. output = open(output_name, flags, S_IRUSR | S_IWUSR);
  365. if (output < 0) {
  366. perror("failed to create output file");
  367. exit(-1);
  368. }
  369. rec->output = output;
  370. session = perf_session__new(output_name, O_WRONLY,
  371. rec->write_mode == WRITE_FORCE, false, NULL);
  372. if (session == NULL) {
  373. pr_err("Not enough memory for reading perf file header\n");
  374. return -1;
  375. }
  376. rec->session = session;
  377. if (!rec->no_buildid)
  378. perf_header__set_feat(&session->header, HEADER_BUILD_ID);
  379. if (!rec->file_new) {
  380. err = perf_session__read_header(session, output);
  381. if (err < 0)
  382. goto out_delete_session;
  383. }
  384. if (have_tracepoints(&evsel_list->entries))
  385. perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
  386. perf_header__set_feat(&session->header, HEADER_HOSTNAME);
  387. perf_header__set_feat(&session->header, HEADER_OSRELEASE);
  388. perf_header__set_feat(&session->header, HEADER_ARCH);
  389. perf_header__set_feat(&session->header, HEADER_CPUDESC);
  390. perf_header__set_feat(&session->header, HEADER_NRCPUS);
  391. perf_header__set_feat(&session->header, HEADER_EVENT_DESC);
  392. perf_header__set_feat(&session->header, HEADER_CMDLINE);
  393. perf_header__set_feat(&session->header, HEADER_VERSION);
  394. perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
  395. perf_header__set_feat(&session->header, HEADER_TOTAL_MEM);
  396. perf_header__set_feat(&session->header, HEADER_NUMA_TOPOLOGY);
  397. perf_header__set_feat(&session->header, HEADER_CPUID);
  398. if (forks) {
  399. err = perf_evlist__prepare_workload(evsel_list, opts, argv);
  400. if (err < 0) {
  401. pr_err("Couldn't run the workload!\n");
  402. goto out_delete_session;
  403. }
  404. }
  405. perf_record__open(rec);
  406. /*
  407. * perf_session__delete(session) will be called at perf_record__exit()
  408. */
  409. on_exit(perf_record__exit, rec);
  410. if (opts->pipe_output) {
  411. err = perf_header__write_pipe(output);
  412. if (err < 0)
  413. return err;
  414. } else if (rec->file_new) {
  415. err = perf_session__write_header(session, evsel_list,
  416. output, false);
  417. if (err < 0)
  418. return err;
  419. }
  420. if (!!rec->no_buildid
  421. && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
  422. pr_err("Couldn't generating buildids. "
  423. "Use --no-buildid to profile anyway.\n");
  424. return -1;
  425. }
  426. rec->post_processing_offset = lseek(output, 0, SEEK_CUR);
  427. machine = perf_session__find_host_machine(session);
  428. if (!machine) {
  429. pr_err("Couldn't find native kernel information.\n");
  430. return -1;
  431. }
  432. if (opts->pipe_output) {
  433. err = perf_event__synthesize_attrs(tool, session,
  434. process_synthesized_event);
  435. if (err < 0) {
  436. pr_err("Couldn't synthesize attrs.\n");
  437. return err;
  438. }
  439. err = perf_event__synthesize_event_types(tool, process_synthesized_event,
  440. machine);
  441. if (err < 0) {
  442. pr_err("Couldn't synthesize event_types.\n");
  443. return err;
  444. }
  445. if (have_tracepoints(&evsel_list->entries)) {
  446. /*
  447. * FIXME err <= 0 here actually means that
  448. * there were no tracepoints so its not really
  449. * an error, just that we don't need to
  450. * synthesize anything. We really have to
  451. * return this more properly and also
  452. * propagate errors that now are calling die()
  453. */
  454. err = perf_event__synthesize_tracing_data(tool, output, evsel_list,
  455. process_synthesized_event);
  456. if (err <= 0) {
  457. pr_err("Couldn't record tracing data.\n");
  458. return err;
  459. }
  460. advance_output(rec, err);
  461. }
  462. }
  463. err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
  464. machine, "_text");
  465. if (err < 0)
  466. err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
  467. machine, "_stext");
  468. if (err < 0)
  469. pr_err("Couldn't record kernel reference relocation symbol\n"
  470. "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
  471. "Check /proc/kallsyms permission or run as root.\n");
  472. err = perf_event__synthesize_modules(tool, process_synthesized_event,
  473. machine);
  474. if (err < 0)
  475. pr_err("Couldn't record kernel module information.\n"
  476. "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
  477. "Check /proc/modules permission or run as root.\n");
  478. if (perf_guest)
  479. perf_session__process_machines(session, tool,
  480. perf_event__synthesize_guest_os);
  481. if (!opts->system_wide)
  482. perf_event__synthesize_thread_map(tool, evsel_list->threads,
  483. process_synthesized_event,
  484. machine);
  485. else
  486. perf_event__synthesize_threads(tool, process_synthesized_event,
  487. machine);
  488. if (rec->realtime_prio) {
  489. struct sched_param param;
  490. param.sched_priority = rec->realtime_prio;
  491. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  492. pr_err("Could not set realtime priority.\n");
  493. exit(-1);
  494. }
  495. }
  496. perf_evlist__enable(evsel_list);
  497. /*
  498. * Let the child rip
  499. */
  500. if (forks)
  501. perf_evlist__start_workload(evsel_list);
  502. for (;;) {
  503. int hits = rec->samples;
  504. perf_record__mmap_read_all(rec);
  505. if (hits == rec->samples) {
  506. if (done)
  507. break;
  508. err = poll(evsel_list->pollfd, evsel_list->nr_fds, -1);
  509. waking++;
  510. }
  511. if (done)
  512. perf_evlist__disable(evsel_list);
  513. }
  514. if (quiet || signr == SIGUSR1)
  515. return 0;
  516. fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
  517. /*
  518. * Approximate RIP event size: 24 bytes.
  519. */
  520. fprintf(stderr,
  521. "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n",
  522. (double)rec->bytes_written / 1024.0 / 1024.0,
  523. output_name,
  524. rec->bytes_written / 24);
  525. return 0;
  526. out_delete_session:
  527. perf_session__delete(session);
  528. return err;
  529. }
  530. static const char * const record_usage[] = {
  531. "perf record [<options>] [<command>]",
  532. "perf record [<options>] -- <command> [<options>]",
  533. NULL
  534. };
  535. /*
  536. * XXX Ideally would be local to cmd_record() and passed to a perf_record__new
  537. * because we need to have access to it in perf_record__exit, that is called
  538. * after cmd_record() exits, but since record_options need to be accessible to
  539. * builtin-script, leave it here.
  540. *
  541. * At least we don't ouch it in all the other functions here directly.
  542. *
  543. * Just say no to tons of global variables, sigh.
  544. */
  545. static struct perf_record record = {
  546. .opts = {
  547. .target_pid = -1,
  548. .target_tid = -1,
  549. .mmap_pages = UINT_MAX,
  550. .user_freq = UINT_MAX,
  551. .user_interval = ULLONG_MAX,
  552. .freq = 1000,
  553. .sample_id_all_avail = true,
  554. },
  555. .write_mode = WRITE_FORCE,
  556. .file_new = true,
  557. };
  558. /*
  559. * XXX Will stay a global variable till we fix builtin-script.c to stop messing
  560. * with it and switch to use the library functions in perf_evlist that came
  561. * from builtin-record.c, i.e. use perf_record_opts,
  562. * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
  563. * using pipes, etc.
  564. */
  565. const struct option record_options[] = {
  566. OPT_CALLBACK('e', "event", &record.evlist, "event",
  567. "event selector. use 'perf list' to list available events",
  568. parse_events_option),
  569. OPT_CALLBACK(0, "filter", &record.evlist, "filter",
  570. "event filter", parse_filter),
  571. OPT_INTEGER('p', "pid", &record.opts.target_pid,
  572. "record events on existing process id"),
  573. OPT_INTEGER('t', "tid", &record.opts.target_tid,
  574. "record events on existing thread id"),
  575. OPT_INTEGER('r', "realtime", &record.realtime_prio,
  576. "collect data with this RT SCHED_FIFO priority"),
  577. OPT_BOOLEAN('D', "no-delay", &record.opts.no_delay,
  578. "collect data without buffering"),
  579. OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
  580. "collect raw sample records from all opened counters"),
  581. OPT_BOOLEAN('a', "all-cpus", &record.opts.system_wide,
  582. "system-wide collection from all CPUs"),
  583. OPT_BOOLEAN('A', "append", &record.append_file,
  584. "append to the output file to do incremental profiling"),
  585. OPT_STRING('C', "cpu", &record.opts.cpu_list, "cpu",
  586. "list of cpus to monitor"),
  587. OPT_BOOLEAN('f', "force", &record.force,
  588. "overwrite existing data file (deprecated)"),
  589. OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
  590. OPT_STRING('o', "output", &record.output_name, "file",
  591. "output file name"),
  592. OPT_BOOLEAN('i', "no-inherit", &record.opts.no_inherit,
  593. "child tasks do not inherit counters"),
  594. OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
  595. OPT_UINTEGER('m', "mmap-pages", &record.opts.mmap_pages,
  596. "number of mmap data pages"),
  597. OPT_BOOLEAN(0, "group", &record.opts.group,
  598. "put the counters into a counter group"),
  599. OPT_BOOLEAN('g', "call-graph", &record.opts.call_graph,
  600. "do call-graph (stack chain/backtrace) recording"),
  601. OPT_INCR('v', "verbose", &verbose,
  602. "be more verbose (show counter open errors, etc)"),
  603. OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
  604. OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
  605. "per thread counts"),
  606. OPT_BOOLEAN('d', "data", &record.opts.sample_address,
  607. "Sample addresses"),
  608. OPT_BOOLEAN('T', "timestamp", &record.opts.sample_time, "Sample timestamps"),
  609. OPT_BOOLEAN('P', "period", &record.opts.period, "Sample period"),
  610. OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
  611. "don't sample"),
  612. OPT_BOOLEAN('N', "no-buildid-cache", &record.no_buildid_cache,
  613. "do not update the buildid cache"),
  614. OPT_BOOLEAN('B', "no-buildid", &record.no_buildid,
  615. "do not collect buildids in perf.data"),
  616. OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
  617. "monitor event in cgroup name only",
  618. parse_cgroups),
  619. OPT_END()
  620. };
  621. int cmd_record(int argc, const char **argv, const char *prefix __used)
  622. {
  623. int err = -ENOMEM;
  624. struct perf_evsel *pos;
  625. struct perf_evlist *evsel_list;
  626. struct perf_record *rec = &record;
  627. perf_header__set_cmdline(argc, argv);
  628. evsel_list = perf_evlist__new(NULL, NULL);
  629. if (evsel_list == NULL)
  630. return -ENOMEM;
  631. rec->evlist = evsel_list;
  632. argc = parse_options(argc, argv, record_options, record_usage,
  633. PARSE_OPT_STOP_AT_NON_OPTION);
  634. if (!argc && rec->opts.target_pid == -1 && rec->opts.target_tid == -1 &&
  635. !rec->opts.system_wide && !rec->opts.cpu_list)
  636. usage_with_options(record_usage, record_options);
  637. if (rec->force && rec->append_file) {
  638. fprintf(stderr, "Can't overwrite and append at the same time."
  639. " You need to choose between -f and -A");
  640. usage_with_options(record_usage, record_options);
  641. } else if (rec->append_file) {
  642. rec->write_mode = WRITE_APPEND;
  643. } else {
  644. rec->write_mode = WRITE_FORCE;
  645. }
  646. if (nr_cgroups && !rec->opts.system_wide) {
  647. fprintf(stderr, "cgroup monitoring only available in"
  648. " system-wide mode\n");
  649. usage_with_options(record_usage, record_options);
  650. }
  651. symbol__init();
  652. if (symbol_conf.kptr_restrict)
  653. pr_warning(
  654. "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
  655. "check /proc/sys/kernel/kptr_restrict.\n\n"
  656. "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
  657. "file is not found in the buildid cache or in the vmlinux path.\n\n"
  658. "Samples in kernel modules won't be resolved at all.\n\n"
  659. "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
  660. "even with a suitable vmlinux or kallsyms file.\n\n");
  661. if (rec->no_buildid_cache || rec->no_buildid)
  662. disable_buildid_cache();
  663. if (evsel_list->nr_entries == 0 &&
  664. perf_evlist__add_default(evsel_list) < 0) {
  665. pr_err("Not enough memory for event selector list\n");
  666. goto out_symbol_exit;
  667. }
  668. if (rec->opts.target_pid != -1)
  669. rec->opts.target_tid = rec->opts.target_pid;
  670. if (perf_evlist__create_maps(evsel_list, rec->opts.target_pid,
  671. rec->opts.target_tid, rec->opts.cpu_list) < 0)
  672. usage_with_options(record_usage, record_options);
  673. list_for_each_entry(pos, &evsel_list->entries, node) {
  674. if (perf_header__push_event(pos->attr.config, event_name(pos)))
  675. goto out_free_fd;
  676. }
  677. if (rec->opts.user_interval != ULLONG_MAX)
  678. rec->opts.default_interval = rec->opts.user_interval;
  679. if (rec->opts.user_freq != UINT_MAX)
  680. rec->opts.freq = rec->opts.user_freq;
  681. /*
  682. * User specified count overrides default frequency.
  683. */
  684. if (rec->opts.default_interval)
  685. rec->opts.freq = 0;
  686. else if (rec->opts.freq) {
  687. rec->opts.default_interval = rec->opts.freq;
  688. } else {
  689. fprintf(stderr, "frequency and count are zero, aborting\n");
  690. err = -EINVAL;
  691. goto out_free_fd;
  692. }
  693. err = __cmd_record(&record, argc, argv);
  694. out_free_fd:
  695. perf_evlist__delete_maps(evsel_list);
  696. out_symbol_exit:
  697. symbol__exit();
  698. return err;
  699. }