builtin-record.c 22 KB

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