builtin-record.c 22 KB

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