builtin-record.c 21 KB

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