builtin-record.c 23 KB

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