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