builtin-record.c 22 KB

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