builtin-record.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
  24. static long default_interval = 0;
  25. static int nr_cpus = 0;
  26. static unsigned int page_size;
  27. static unsigned int mmap_pages = 128;
  28. static int freq = 1000;
  29. static int output;
  30. static int pipe_output = 0;
  31. static const char *output_name = "perf.data";
  32. static int group = 0;
  33. static unsigned int realtime_prio = 0;
  34. static bool raw_samples = false;
  35. static bool system_wide = false;
  36. static int profile_cpu = -1;
  37. static pid_t target_pid = -1;
  38. static pid_t target_tid = -1;
  39. static pid_t *all_tids = NULL;
  40. static int thread_num = 0;
  41. static pid_t child_pid = -1;
  42. static bool inherit = true;
  43. static bool force = false;
  44. static bool append_file = false;
  45. static bool call_graph = false;
  46. static bool inherit_stat = false;
  47. static bool no_samples = false;
  48. static bool sample_address = false;
  49. static bool multiplex = false;
  50. static int multiplex_fd = -1;
  51. static long samples = 0;
  52. static struct timeval last_read;
  53. static struct timeval this_read;
  54. static u64 bytes_written = 0;
  55. static struct pollfd *event_array;
  56. static int nr_poll = 0;
  57. static int nr_cpu = 0;
  58. static int file_new = 1;
  59. static off_t post_processing_offset;
  60. static struct perf_session *session;
  61. struct mmap_data {
  62. int counter;
  63. void *base;
  64. unsigned int mask;
  65. unsigned int prev;
  66. };
  67. static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  68. static unsigned long mmap_read_head(struct mmap_data *md)
  69. {
  70. struct perf_event_mmap_page *pc = md->base;
  71. long head;
  72. head = pc->data_head;
  73. rmb();
  74. return head;
  75. }
  76. static void mmap_write_tail(struct mmap_data *md, unsigned long tail)
  77. {
  78. struct perf_event_mmap_page *pc = md->base;
  79. /*
  80. * ensure all reads are done before we write the tail out.
  81. */
  82. /* mb(); */
  83. pc->data_tail = tail;
  84. }
  85. static void advance_output(size_t size)
  86. {
  87. bytes_written += size;
  88. }
  89. static void write_output(void *buf, size_t size)
  90. {
  91. while (size) {
  92. int ret = write(output, buf, size);
  93. if (ret < 0)
  94. die("failed to write");
  95. size -= ret;
  96. buf += ret;
  97. bytes_written += ret;
  98. }
  99. }
  100. static int process_synthesized_event(event_t *event,
  101. struct perf_session *self __used)
  102. {
  103. write_output(event, event->header.size);
  104. return 0;
  105. }
  106. static void mmap_read(struct mmap_data *md)
  107. {
  108. unsigned int head = mmap_read_head(md);
  109. unsigned int old = md->prev;
  110. unsigned char *data = md->base + page_size;
  111. unsigned long size;
  112. void *buf;
  113. int diff;
  114. gettimeofday(&this_read, NULL);
  115. /*
  116. * If we're further behind than half the buffer, there's a chance
  117. * the writer will bite our tail and mess up the samples under us.
  118. *
  119. * If we somehow ended up ahead of the head, we got messed up.
  120. *
  121. * In either case, truncate and restart at head.
  122. */
  123. diff = head - old;
  124. if (diff < 0) {
  125. struct timeval iv;
  126. unsigned long msecs;
  127. timersub(&this_read, &last_read, &iv);
  128. msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
  129. fprintf(stderr, "WARNING: failed to keep up with mmap data."
  130. " Last read %lu msecs ago.\n", msecs);
  131. /*
  132. * head points to a known good entry, start there.
  133. */
  134. old = head;
  135. }
  136. last_read = this_read;
  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 != -1)
  163. kill(child_pid, SIGTERM);
  164. if (signr == -1)
  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(int counter, int cpu)
  186. {
  187. char *filter = filters[counter];
  188. struct perf_event_attr *attr = attrs + counter;
  189. struct perf_header_attr *h_attr;
  190. int track = !counter; /* 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. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  200. PERF_FORMAT_TOTAL_TIME_RUNNING |
  201. PERF_FORMAT_ID;
  202. attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  203. if (nr_counters > 1)
  204. attr->sample_type |= PERF_SAMPLE_ID;
  205. if (freq) {
  206. attr->sample_type |= PERF_SAMPLE_PERIOD;
  207. attr->freq = 1;
  208. attr->sample_freq = freq;
  209. }
  210. if (no_samples)
  211. attr->sample_freq = 0;
  212. if (inherit_stat)
  213. attr->inherit_stat = 1;
  214. if (sample_address)
  215. attr->sample_type |= PERF_SAMPLE_ADDR;
  216. if (call_graph)
  217. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  218. if (raw_samples) {
  219. attr->sample_type |= PERF_SAMPLE_TIME;
  220. attr->sample_type |= PERF_SAMPLE_RAW;
  221. attr->sample_type |= PERF_SAMPLE_CPU;
  222. }
  223. attr->mmap = track;
  224. attr->comm = track;
  225. attr->inherit = inherit;
  226. if (target_pid == -1 && !system_wide) {
  227. attr->disabled = 1;
  228. attr->enable_on_exec = 1;
  229. }
  230. for (thread_index = 0; thread_index < thread_num; thread_index++) {
  231. try_again:
  232. fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr,
  233. all_tids[thread_index], cpu, group_fd, 0);
  234. if (fd[nr_cpu][counter][thread_index] < 0) {
  235. int err = errno;
  236. if (err == EPERM || err == EACCES)
  237. die("Permission error - are you root?\n"
  238. "\t Consider tweaking"
  239. " /proc/sys/kernel/perf_event_paranoid.\n");
  240. else if (err == ENODEV && profile_cpu != -1) {
  241. die("No such device - did you specify"
  242. " an out-of-range profile CPU?\n");
  243. }
  244. /*
  245. * If it's cycles then fall back to hrtimer
  246. * based cpu-clock-tick sw counter, which
  247. * is always available even if no PMU support:
  248. */
  249. if (attr->type == PERF_TYPE_HARDWARE
  250. && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  251. if (verbose)
  252. warning(" ... trying to fall back to cpu-clock-ticks\n");
  253. attr->type = PERF_TYPE_SOFTWARE;
  254. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  255. goto try_again;
  256. }
  257. printf("\n");
  258. error("perfcounter syscall returned with %d (%s)\n",
  259. fd[nr_cpu][counter][thread_index], strerror(err));
  260. #if defined(__i386__) || defined(__x86_64__)
  261. if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
  262. die("No hardware sampling interrupt available."
  263. " No APIC? If so then you can boot the kernel"
  264. " with the \"lapic\" boot parameter to"
  265. " force-enable it.\n");
  266. #endif
  267. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  268. exit(-1);
  269. }
  270. h_attr = get_header_attr(attr, counter);
  271. if (h_attr == NULL)
  272. die("nomem\n");
  273. if (!file_new) {
  274. if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
  275. fprintf(stderr, "incompatible append\n");
  276. exit(-1);
  277. }
  278. }
  279. if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) {
  280. perror("Unable to read perf file descriptor\n");
  281. exit(-1);
  282. }
  283. if (perf_header_attr__add_id(h_attr, read_data.id) < 0) {
  284. pr_warning("Not enough memory to add id\n");
  285. exit(-1);
  286. }
  287. assert(fd[nr_cpu][counter][thread_index] >= 0);
  288. fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK);
  289. /*
  290. * First counter acts as the group leader:
  291. */
  292. if (group && group_fd == -1)
  293. group_fd = fd[nr_cpu][counter][thread_index];
  294. if (multiplex && multiplex_fd == -1)
  295. multiplex_fd = fd[nr_cpu][counter][thread_index];
  296. if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) {
  297. ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd);
  298. assert(ret != -1);
  299. } else {
  300. event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
  301. event_array[nr_poll].events = POLLIN;
  302. nr_poll++;
  303. mmap_array[nr_cpu][counter][thread_index].counter = counter;
  304. mmap_array[nr_cpu][counter][thread_index].prev = 0;
  305. mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1;
  306. mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
  307. PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
  308. if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) {
  309. error("failed to mmap with %d (%s)\n", errno, strerror(errno));
  310. exit(-1);
  311. }
  312. }
  313. if (filter != NULL) {
  314. ret = ioctl(fd[nr_cpu][counter][thread_index],
  315. PERF_EVENT_IOC_SET_FILTER, filter);
  316. if (ret) {
  317. error("failed to set filter with %d (%s)\n", errno,
  318. strerror(errno));
  319. exit(-1);
  320. }
  321. }
  322. }
  323. }
  324. static void open_counters(int cpu)
  325. {
  326. int counter;
  327. group_fd = -1;
  328. for (counter = 0; counter < nr_counters; counter++)
  329. create_counter(counter, cpu);
  330. nr_cpu++;
  331. }
  332. static int process_buildids(void)
  333. {
  334. u64 size = lseek(output, 0, SEEK_CUR);
  335. if (size == 0)
  336. return 0;
  337. session->fd = output;
  338. return __perf_session__process_events(session, post_processing_offset,
  339. size - post_processing_offset,
  340. size, &build_id__mark_dso_hit_ops);
  341. }
  342. static void atexit_header(void)
  343. {
  344. session->header.data_size += bytes_written;
  345. process_buildids();
  346. perf_header__write(&session->header, output, true);
  347. }
  348. static int __cmd_record(int argc, const char **argv)
  349. {
  350. int i, counter;
  351. struct stat st;
  352. pid_t pid = 0;
  353. int flags;
  354. int err;
  355. unsigned long waking = 0;
  356. int child_ready_pipe[2], go_pipe[2];
  357. const bool forks = argc > 0;
  358. char buf;
  359. page_size = sysconf(_SC_PAGE_SIZE);
  360. atexit(sig_atexit);
  361. signal(SIGCHLD, sig_handler);
  362. signal(SIGINT, sig_handler);
  363. if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
  364. perror("failed to create pipes");
  365. exit(-1);
  366. }
  367. if (!strcmp(output_name, "-"))
  368. pipe_output = 1;
  369. else if (!stat(output_name, &st) && st.st_size) {
  370. if (!force) {
  371. if (!append_file) {
  372. pr_err("Error, output file %s exists, use -A "
  373. "to append or -f to overwrite.\n",
  374. output_name);
  375. exit(-1);
  376. }
  377. } else {
  378. char oldname[PATH_MAX];
  379. snprintf(oldname, sizeof(oldname), "%s.old",
  380. output_name);
  381. unlink(oldname);
  382. rename(output_name, oldname);
  383. }
  384. } else {
  385. append_file = false;
  386. }
  387. flags = O_CREAT|O_RDWR;
  388. if (append_file)
  389. file_new = 0;
  390. else
  391. flags |= O_TRUNC;
  392. if (pipe_output)
  393. output = STDOUT_FILENO;
  394. else
  395. output = open(output_name, flags, S_IRUSR | S_IWUSR);
  396. if (output < 0) {
  397. perror("failed to create output file");
  398. exit(-1);
  399. }
  400. session = perf_session__new(output_name, O_WRONLY, force);
  401. if (session == NULL) {
  402. pr_err("Not enough memory for reading perf file header\n");
  403. return -1;
  404. }
  405. if (!file_new) {
  406. err = perf_header__read(session, output);
  407. if (err < 0)
  408. return err;
  409. }
  410. if (raw_samples) {
  411. perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
  412. } else {
  413. for (i = 0; i < nr_counters; i++) {
  414. if (attrs[i].sample_type & PERF_SAMPLE_RAW) {
  415. perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
  416. break;
  417. }
  418. }
  419. }
  420. atexit(atexit_header);
  421. if (forks) {
  422. child_pid = fork();
  423. if (pid < 0) {
  424. perror("failed to fork");
  425. exit(-1);
  426. }
  427. if (!child_pid) {
  428. if (pipe_output)
  429. dup2(2, 1);
  430. close(child_ready_pipe[0]);
  431. close(go_pipe[1]);
  432. fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
  433. /*
  434. * Do a dummy execvp to get the PLT entry resolved,
  435. * so we avoid the resolver overhead on the real
  436. * execvp call.
  437. */
  438. execvp("", (char **)argv);
  439. /*
  440. * Tell the parent we're ready to go
  441. */
  442. close(child_ready_pipe[1]);
  443. /*
  444. * Wait until the parent tells us to go.
  445. */
  446. if (read(go_pipe[0], &buf, 1) == -1)
  447. perror("unable to read pipe");
  448. execvp(argv[0], (char **)argv);
  449. perror(argv[0]);
  450. exit(-1);
  451. }
  452. if (!system_wide && target_tid == -1 && target_pid == -1)
  453. all_tids[0] = child_pid;
  454. close(child_ready_pipe[1]);
  455. close(go_pipe[0]);
  456. /*
  457. * wait for child to settle
  458. */
  459. if (read(child_ready_pipe[0], &buf, 1) == -1) {
  460. perror("unable to read pipe");
  461. exit(-1);
  462. }
  463. close(child_ready_pipe[0]);
  464. }
  465. if ((!system_wide && !inherit) || profile_cpu != -1) {
  466. open_counters(profile_cpu);
  467. } else {
  468. nr_cpus = read_cpu_map();
  469. for (i = 0; i < nr_cpus; i++)
  470. open_counters(cpumap[i]);
  471. }
  472. if (pipe_output) {
  473. err = perf_header__write_pipe(output);
  474. if (err < 0)
  475. return err;
  476. } else if (file_new) {
  477. err = perf_header__write(&session->header, output, false);
  478. if (err < 0)
  479. return err;
  480. }
  481. post_processing_offset = lseek(output, 0, SEEK_CUR);
  482. if (pipe_output) {
  483. err = event__synthesize_attrs(&session->header,
  484. process_synthesized_event,
  485. session);
  486. if (err < 0) {
  487. pr_err("Couldn't synthesize attrs.\n");
  488. return err;
  489. }
  490. err = event__synthesize_event_types(process_synthesized_event,
  491. session);
  492. if (err < 0) {
  493. pr_err("Couldn't synthesize event_types.\n");
  494. return err;
  495. }
  496. err = event__synthesize_tracing_data(output, attrs,
  497. nr_counters,
  498. process_synthesized_event,
  499. session);
  500. if (err <= 0) {
  501. pr_err("Couldn't record tracing data.\n");
  502. return err;
  503. }
  504. advance_output(err);
  505. }
  506. err = event__synthesize_kernel_mmap(process_synthesized_event,
  507. session, "_text");
  508. if (err < 0)
  509. err = event__synthesize_kernel_mmap(process_synthesized_event,
  510. session, "_stext");
  511. if (err < 0) {
  512. pr_err("Couldn't record kernel reference relocation symbol.\n");
  513. return err;
  514. }
  515. err = event__synthesize_modules(process_synthesized_event, session);
  516. if (err < 0) {
  517. pr_err("Couldn't record kernel reference relocation symbol.\n");
  518. return err;
  519. }
  520. if (!system_wide && profile_cpu == -1)
  521. event__synthesize_thread(target_tid, process_synthesized_event,
  522. session);
  523. else
  524. event__synthesize_threads(process_synthesized_event, session);
  525. if (realtime_prio) {
  526. struct sched_param param;
  527. param.sched_priority = realtime_prio;
  528. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  529. pr_err("Could not set realtime priority.\n");
  530. exit(-1);
  531. }
  532. }
  533. /*
  534. * Let the child rip
  535. */
  536. if (forks)
  537. close(go_pipe[1]);
  538. for (;;) {
  539. int hits = samples;
  540. int thread;
  541. for (i = 0; i < nr_cpu; i++) {
  542. for (counter = 0; counter < nr_counters; counter++) {
  543. for (thread = 0;
  544. thread < thread_num; thread++) {
  545. if (mmap_array[i][counter][thread].base)
  546. mmap_read(&mmap_array[i][counter][thread]);
  547. }
  548. }
  549. }
  550. if (hits == samples) {
  551. if (done)
  552. break;
  553. err = poll(event_array, nr_poll, -1);
  554. waking++;
  555. }
  556. if (done) {
  557. for (i = 0; i < nr_cpu; i++) {
  558. for (counter = 0;
  559. counter < nr_counters;
  560. counter++) {
  561. for (thread = 0;
  562. thread < thread_num;
  563. thread++)
  564. ioctl(fd[i][counter][thread],
  565. PERF_EVENT_IOC_DISABLE);
  566. }
  567. }
  568. }
  569. }
  570. fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
  571. /*
  572. * Approximate RIP event size: 24 bytes.
  573. */
  574. fprintf(stderr,
  575. "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n",
  576. (double)bytes_written / 1024.0 / 1024.0,
  577. output_name,
  578. bytes_written / 24);
  579. return 0;
  580. }
  581. static const char * const record_usage[] = {
  582. "perf record [<options>] [<command>]",
  583. "perf record [<options>] -- <command> [<options>]",
  584. NULL
  585. };
  586. static const struct option options[] = {
  587. OPT_CALLBACK('e', "event", NULL, "event",
  588. "event selector. use 'perf list' to list available events",
  589. parse_events),
  590. OPT_CALLBACK(0, "filter", NULL, "filter",
  591. "event filter", parse_filter),
  592. OPT_INTEGER('p', "pid", &target_pid,
  593. "record events on existing process id"),
  594. OPT_INTEGER('t', "tid", &target_tid,
  595. "record events on existing thread id"),
  596. OPT_INTEGER('r', "realtime", &realtime_prio,
  597. "collect data with this RT SCHED_FIFO priority"),
  598. OPT_BOOLEAN('R', "raw-samples", &raw_samples,
  599. "collect raw sample records from all opened counters"),
  600. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  601. "system-wide collection from all CPUs"),
  602. OPT_BOOLEAN('A', "append", &append_file,
  603. "append to the output file to do incremental profiling"),
  604. OPT_INTEGER('C', "profile_cpu", &profile_cpu,
  605. "CPU to profile on"),
  606. OPT_BOOLEAN('f', "force", &force,
  607. "overwrite existing data file"),
  608. OPT_LONG('c', "count", &default_interval,
  609. "event period to sample"),
  610. OPT_STRING('o', "output", &output_name, "file",
  611. "output file name"),
  612. OPT_BOOLEAN('i', "inherit", &inherit,
  613. "child tasks inherit counters"),
  614. OPT_INTEGER('F', "freq", &freq,
  615. "profile at this frequency"),
  616. OPT_INTEGER('m', "mmap-pages", &mmap_pages,
  617. "number of mmap data pages"),
  618. OPT_BOOLEAN('g', "call-graph", &call_graph,
  619. "do call-graph (stack chain/backtrace) recording"),
  620. OPT_INCR('v', "verbose", &verbose,
  621. "be more verbose (show counter open errors, etc)"),
  622. OPT_BOOLEAN('s', "stat", &inherit_stat,
  623. "per thread counts"),
  624. OPT_BOOLEAN('d', "data", &sample_address,
  625. "Sample addresses"),
  626. OPT_BOOLEAN('n', "no-samples", &no_samples,
  627. "don't sample"),
  628. OPT_BOOLEAN('M', "multiplex", &multiplex,
  629. "multiplex counter output in a single channel"),
  630. OPT_END()
  631. };
  632. int cmd_record(int argc, const char **argv, const char *prefix __used)
  633. {
  634. int counter;
  635. int i,j;
  636. argc = parse_options(argc, argv, options, record_usage,
  637. PARSE_OPT_STOP_AT_NON_OPTION);
  638. if (!argc && target_pid == -1 && target_tid == -1 &&
  639. !system_wide && profile_cpu == -1)
  640. usage_with_options(record_usage, options);
  641. symbol__init();
  642. if (!nr_counters) {
  643. nr_counters = 1;
  644. attrs[0].type = PERF_TYPE_HARDWARE;
  645. attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
  646. }
  647. if (target_pid != -1) {
  648. target_tid = target_pid;
  649. thread_num = find_all_tid(target_pid, &all_tids);
  650. if (thread_num <= 0) {
  651. fprintf(stderr, "Can't find all threads of pid %d\n",
  652. target_pid);
  653. usage_with_options(record_usage, options);
  654. }
  655. } else {
  656. all_tids=malloc(sizeof(pid_t));
  657. if (!all_tids)
  658. return -ENOMEM;
  659. all_tids[0] = target_tid;
  660. thread_num = 1;
  661. }
  662. for (i = 0; i < MAX_NR_CPUS; i++) {
  663. for (j = 0; j < MAX_COUNTERS; j++) {
  664. fd[i][j] = malloc(sizeof(int)*thread_num);
  665. mmap_array[i][j] = zalloc(
  666. sizeof(struct mmap_data)*thread_num);
  667. if (!fd[i][j] || !mmap_array[i][j])
  668. return -ENOMEM;
  669. }
  670. }
  671. event_array = malloc(
  672. sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
  673. if (!event_array)
  674. return -ENOMEM;
  675. /*
  676. * User specified count overrides default frequency.
  677. */
  678. if (default_interval)
  679. freq = 0;
  680. else if (freq) {
  681. default_interval = freq;
  682. } else {
  683. fprintf(stderr, "frequency and count are zero, aborting\n");
  684. exit(EXIT_FAILURE);
  685. }
  686. for (counter = 0; counter < nr_counters; counter++) {
  687. if (attrs[counter].sample_period)
  688. continue;
  689. attrs[counter].sample_period = default_interval;
  690. }
  691. return __cmd_record(argc, argv);
  692. }