builtin-record.c 22 KB

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