builtin-record.c 24 KB

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