builtin-record.c 24 KB

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