builtin-record.c 24 KB

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