builtin-record.c 17 KB

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