builtin-record.c 23 KB

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