builtin-record.c 22 KB

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