builtin-record.c 22 KB

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