builtin-record.c 22 KB

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