builtin-record.c 16 KB

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