builtin-record.c 17 KB

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