builtin-record.c 18 KB

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