builtin-record.c 24 KB

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