builtin-record.c 18 KB

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