builtin-record.c 22 KB

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