builtin-top.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * builtin-top.c
  3. *
  4. * Builtin top command: Display a continuously updated profile of
  5. * any workload, CPU or specific PID.
  6. *
  7. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  8. *
  9. * Improvements and fixes by:
  10. *
  11. * Arjan van de Ven <arjan@linux.intel.com>
  12. * Yanmin Zhang <yanmin.zhang@intel.com>
  13. * Wu Fengguang <fengguang.wu@intel.com>
  14. * Mike Galbraith <efault@gmx.de>
  15. * Paul Mackerras <paulus@samba.org>
  16. *
  17. * Released under the GPL v2. (and only v2, not any later version)
  18. */
  19. #include "builtin.h"
  20. #include "perf.h"
  21. #include "util/symbol.h"
  22. #include "util/color.h"
  23. #include "util/util.h"
  24. #include <linux/rbtree.h>
  25. #include "util/parse-options.h"
  26. #include "util/parse-events.h"
  27. #include <assert.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <time.h>
  32. #include <sched.h>
  33. #include <pthread.h>
  34. #include <sys/syscall.h>
  35. #include <sys/ioctl.h>
  36. #include <sys/poll.h>
  37. #include <sys/prctl.h>
  38. #include <sys/wait.h>
  39. #include <sys/uio.h>
  40. #include <sys/mman.h>
  41. #include <linux/unistd.h>
  42. #include <linux/types.h>
  43. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  44. static int system_wide = 0;
  45. static int default_interval = 100000;
  46. static u64 count_filter = 5;
  47. static int print_entries = 15;
  48. static int target_pid = -1;
  49. static int inherit = 0;
  50. static int profile_cpu = -1;
  51. static int nr_cpus = 0;
  52. static unsigned int realtime_prio = 0;
  53. static int group = 0;
  54. static unsigned int page_size;
  55. static unsigned int mmap_pages = 16;
  56. static int freq = 0;
  57. static int verbose = 0;
  58. static char *vmlinux = NULL;
  59. static char *sym_filter;
  60. static unsigned long filter_start;
  61. static unsigned long filter_end;
  62. static int delay_secs = 2;
  63. static int zero;
  64. static int dump_symtab;
  65. /*
  66. * Symbols
  67. */
  68. static u64 min_ip;
  69. static u64 max_ip = -1ll;
  70. struct sym_entry {
  71. struct rb_node rb_node;
  72. struct list_head node;
  73. unsigned long count[MAX_COUNTERS];
  74. unsigned long snap_count;
  75. double weight;
  76. int skip;
  77. };
  78. struct sym_entry *sym_filter_entry;
  79. struct dso *kernel_dso;
  80. /*
  81. * Symbols will be added here in record_ip and will get out
  82. * after decayed.
  83. */
  84. static LIST_HEAD(active_symbols);
  85. static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
  86. /*
  87. * Ordering weight: count-1 * count-2 * ... / count-n
  88. */
  89. static double sym_weight(const struct sym_entry *sym)
  90. {
  91. double weight = sym->snap_count;
  92. int counter;
  93. for (counter = 1; counter < nr_counters-1; counter++)
  94. weight *= sym->count[counter];
  95. weight /= (sym->count[counter] + 1);
  96. return weight;
  97. }
  98. static long samples;
  99. static long userspace_samples;
  100. static const char CONSOLE_CLEAR[] = "";
  101. static void __list_insert_active_sym(struct sym_entry *syme)
  102. {
  103. list_add(&syme->node, &active_symbols);
  104. }
  105. static void list_remove_active_sym(struct sym_entry *syme)
  106. {
  107. pthread_mutex_lock(&active_symbols_lock);
  108. list_del_init(&syme->node);
  109. pthread_mutex_unlock(&active_symbols_lock);
  110. }
  111. static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
  112. {
  113. struct rb_node **p = &tree->rb_node;
  114. struct rb_node *parent = NULL;
  115. struct sym_entry *iter;
  116. while (*p != NULL) {
  117. parent = *p;
  118. iter = rb_entry(parent, struct sym_entry, rb_node);
  119. if (se->weight > iter->weight)
  120. p = &(*p)->rb_left;
  121. else
  122. p = &(*p)->rb_right;
  123. }
  124. rb_link_node(&se->rb_node, parent, p);
  125. rb_insert_color(&se->rb_node, tree);
  126. }
  127. static void print_sym_table(void)
  128. {
  129. int printed = 0, j;
  130. int counter;
  131. float samples_per_sec = samples/delay_secs;
  132. float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
  133. float sum_ksamples = 0.0;
  134. struct sym_entry *syme, *n;
  135. struct rb_root tmp = RB_ROOT;
  136. struct rb_node *nd;
  137. samples = userspace_samples = 0;
  138. /* Sort the active symbols */
  139. pthread_mutex_lock(&active_symbols_lock);
  140. syme = list_entry(active_symbols.next, struct sym_entry, node);
  141. pthread_mutex_unlock(&active_symbols_lock);
  142. list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
  143. syme->snap_count = syme->count[0];
  144. if (syme->snap_count != 0) {
  145. syme->weight = sym_weight(syme);
  146. rb_insert_active_sym(&tmp, syme);
  147. sum_ksamples += syme->snap_count;
  148. for (j = 0; j < nr_counters; j++)
  149. syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
  150. } else
  151. list_remove_active_sym(syme);
  152. }
  153. puts(CONSOLE_CLEAR);
  154. printf(
  155. "------------------------------------------------------------------------------\n");
  156. printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
  157. samples_per_sec,
  158. 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
  159. if (nr_counters == 1) {
  160. printf("%Ld", (u64)attrs[0].sample_period);
  161. if (freq)
  162. printf("Hz ");
  163. else
  164. printf(" ");
  165. }
  166. for (counter = 0; counter < nr_counters; counter++) {
  167. if (counter)
  168. printf("/");
  169. printf("%s", event_name(counter));
  170. }
  171. printf( "], ");
  172. if (target_pid != -1)
  173. printf(" (target_pid: %d", target_pid);
  174. else
  175. printf(" (all");
  176. if (profile_cpu != -1)
  177. printf(", cpu: %d)\n", profile_cpu);
  178. else {
  179. if (target_pid != -1)
  180. printf(")\n");
  181. else
  182. printf(", %d CPUs)\n", nr_cpus);
  183. }
  184. printf("------------------------------------------------------------------------------\n\n");
  185. if (nr_counters == 1)
  186. printf(" samples pcnt");
  187. else
  188. printf(" weight samples pcnt");
  189. printf(" RIP kernel function\n"
  190. " ______ _______ _____ ________________ _______________\n\n"
  191. );
  192. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  193. struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
  194. struct symbol *sym = (struct symbol *)(syme + 1);
  195. double pcnt;
  196. if (++printed > print_entries || syme->snap_count < count_filter)
  197. continue;
  198. pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
  199. sum_ksamples));
  200. if (nr_counters == 1)
  201. printf("%20.2f - ", syme->weight);
  202. else
  203. printf("%9.1f %10ld - ", syme->weight, syme->snap_count);
  204. percent_color_fprintf(stdout, "%4.1f%%", pcnt);
  205. printf(" - %016llx : %s", sym->start, sym->name);
  206. if (sym->module)
  207. printf("\t[%s]", sym->module->name);
  208. printf("\n");
  209. }
  210. }
  211. static void *display_thread(void *arg __used)
  212. {
  213. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  214. int delay_msecs = delay_secs * 1000;
  215. printf("PerfTop refresh period: %d seconds\n", delay_secs);
  216. do {
  217. print_sym_table();
  218. } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
  219. printf("key pressed - exiting.\n");
  220. exit(0);
  221. return NULL;
  222. }
  223. /* Tag samples to be skipped. */
  224. static const char *skip_symbols[] = {
  225. "default_idle",
  226. "cpu_idle",
  227. "enter_idle",
  228. "exit_idle",
  229. "mwait_idle",
  230. "ppc64_runlatch_off",
  231. "pseries_dedicated_idle_sleep",
  232. NULL
  233. };
  234. static int symbol_filter(struct dso *self, struct symbol *sym)
  235. {
  236. static int filter_match;
  237. struct sym_entry *syme;
  238. const char *name = sym->name;
  239. int i;
  240. /*
  241. * ppc64 uses function descriptors and appends a '.' to the
  242. * start of every instruction address. Remove it.
  243. */
  244. if (name[0] == '.')
  245. name++;
  246. if (!strcmp(name, "_text") ||
  247. !strcmp(name, "_etext") ||
  248. !strcmp(name, "_sinittext") ||
  249. !strncmp("init_module", name, 11) ||
  250. !strncmp("cleanup_module", name, 14) ||
  251. strstr(name, "_text_start") ||
  252. strstr(name, "_text_end"))
  253. return 1;
  254. syme = dso__sym_priv(self, sym);
  255. for (i = 0; skip_symbols[i]; i++) {
  256. if (!strcmp(skip_symbols[i], name)) {
  257. syme->skip = 1;
  258. break;
  259. }
  260. }
  261. if (filter_match == 1) {
  262. filter_end = sym->start;
  263. filter_match = -1;
  264. if (filter_end - filter_start > 10000) {
  265. fprintf(stderr,
  266. "hm, too large filter symbol <%s> - skipping.\n",
  267. sym_filter);
  268. fprintf(stderr, "symbol filter start: %016lx\n",
  269. filter_start);
  270. fprintf(stderr, " end: %016lx\n",
  271. filter_end);
  272. filter_end = filter_start = 0;
  273. sym_filter = NULL;
  274. sleep(1);
  275. }
  276. }
  277. if (filter_match == 0 && sym_filter && !strcmp(name, sym_filter)) {
  278. filter_match = 1;
  279. filter_start = sym->start;
  280. }
  281. return 0;
  282. }
  283. static int parse_symbols(void)
  284. {
  285. struct rb_node *node;
  286. struct symbol *sym;
  287. int modules = vmlinux ? 1 : 0;
  288. kernel_dso = dso__new("[kernel]", sizeof(struct sym_entry));
  289. if (kernel_dso == NULL)
  290. return -1;
  291. if (dso__load_kernel(kernel_dso, vmlinux, symbol_filter, verbose, modules) <= 0)
  292. goto out_delete_dso;
  293. node = rb_first(&kernel_dso->syms);
  294. sym = rb_entry(node, struct symbol, rb_node);
  295. min_ip = sym->start;
  296. node = rb_last(&kernel_dso->syms);
  297. sym = rb_entry(node, struct symbol, rb_node);
  298. max_ip = sym->end;
  299. if (dump_symtab)
  300. dso__fprintf(kernel_dso, stderr);
  301. return 0;
  302. out_delete_dso:
  303. dso__delete(kernel_dso);
  304. kernel_dso = NULL;
  305. return -1;
  306. }
  307. #define TRACE_COUNT 3
  308. /*
  309. * Binary search in the histogram table and record the hit:
  310. */
  311. static void record_ip(u64 ip, int counter)
  312. {
  313. struct symbol *sym = dso__find_symbol(kernel_dso, ip);
  314. if (sym != NULL) {
  315. struct sym_entry *syme = dso__sym_priv(kernel_dso, sym);
  316. if (!syme->skip) {
  317. syme->count[counter]++;
  318. pthread_mutex_lock(&active_symbols_lock);
  319. if (list_empty(&syme->node) || !syme->node.next)
  320. __list_insert_active_sym(syme);
  321. pthread_mutex_unlock(&active_symbols_lock);
  322. return;
  323. }
  324. }
  325. samples--;
  326. }
  327. static void process_event(u64 ip, int counter, int user)
  328. {
  329. samples++;
  330. if (user) {
  331. userspace_samples++;
  332. return;
  333. }
  334. record_ip(ip, counter);
  335. }
  336. struct mmap_data {
  337. int counter;
  338. void *base;
  339. int mask;
  340. unsigned int prev;
  341. };
  342. static unsigned int mmap_read_head(struct mmap_data *md)
  343. {
  344. struct perf_counter_mmap_page *pc = md->base;
  345. int head;
  346. head = pc->data_head;
  347. rmb();
  348. return head;
  349. }
  350. struct timeval last_read, this_read;
  351. static void mmap_read_counter(struct mmap_data *md)
  352. {
  353. unsigned int head = mmap_read_head(md);
  354. unsigned int old = md->prev;
  355. unsigned char *data = md->base + page_size;
  356. int diff;
  357. gettimeofday(&this_read, NULL);
  358. /*
  359. * If we're further behind than half the buffer, there's a chance
  360. * the writer will bite our tail and mess up the samples under us.
  361. *
  362. * If we somehow ended up ahead of the head, we got messed up.
  363. *
  364. * In either case, truncate and restart at head.
  365. */
  366. diff = head - old;
  367. if (diff > md->mask / 2 || diff < 0) {
  368. struct timeval iv;
  369. unsigned long msecs;
  370. timersub(&this_read, &last_read, &iv);
  371. msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
  372. fprintf(stderr, "WARNING: failed to keep up with mmap data."
  373. " Last read %lu msecs ago.\n", msecs);
  374. /*
  375. * head points to a known good entry, start there.
  376. */
  377. old = head;
  378. }
  379. last_read = this_read;
  380. for (; old != head;) {
  381. struct ip_event {
  382. struct perf_event_header header;
  383. u64 ip;
  384. u32 pid, target_pid;
  385. };
  386. struct mmap_event {
  387. struct perf_event_header header;
  388. u32 pid, target_pid;
  389. u64 start;
  390. u64 len;
  391. u64 pgoff;
  392. char filename[PATH_MAX];
  393. };
  394. typedef union event_union {
  395. struct perf_event_header header;
  396. struct ip_event ip;
  397. struct mmap_event mmap;
  398. } event_t;
  399. event_t *event = (event_t *)&data[old & md->mask];
  400. event_t event_copy;
  401. size_t size = event->header.size;
  402. /*
  403. * Event straddles the mmap boundary -- header should always
  404. * be inside due to u64 alignment of output.
  405. */
  406. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  407. unsigned int offset = old;
  408. unsigned int len = min(sizeof(*event), size), cpy;
  409. void *dst = &event_copy;
  410. do {
  411. cpy = min(md->mask + 1 - (offset & md->mask), len);
  412. memcpy(dst, &data[offset & md->mask], cpy);
  413. offset += cpy;
  414. dst += cpy;
  415. len -= cpy;
  416. } while (len);
  417. event = &event_copy;
  418. }
  419. old += size;
  420. if (event->header.type == PERF_EVENT_SAMPLE) {
  421. int user =
  422. (event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK) == PERF_EVENT_MISC_USER;
  423. process_event(event->ip.ip, md->counter, user);
  424. }
  425. }
  426. md->prev = old;
  427. }
  428. static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
  429. static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  430. static void mmap_read(void)
  431. {
  432. int i, counter;
  433. for (i = 0; i < nr_cpus; i++) {
  434. for (counter = 0; counter < nr_counters; counter++)
  435. mmap_read_counter(&mmap_array[i][counter]);
  436. }
  437. }
  438. int nr_poll;
  439. int group_fd;
  440. static void start_counter(int i, int counter)
  441. {
  442. struct perf_counter_attr *attr;
  443. int cpu;
  444. cpu = profile_cpu;
  445. if (target_pid == -1 && profile_cpu == -1)
  446. cpu = i;
  447. attr = attrs + counter;
  448. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  449. attr->freq = freq;
  450. attr->inherit = (cpu < 0) && inherit;
  451. try_again:
  452. fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0);
  453. if (fd[i][counter] < 0) {
  454. int err = errno;
  455. if (err == EPERM)
  456. die("No permission - are you root?\n");
  457. /*
  458. * If it's cycles then fall back to hrtimer
  459. * based cpu-clock-tick sw counter, which
  460. * is always available even if no PMU support:
  461. */
  462. if (attr->type == PERF_TYPE_HARDWARE
  463. && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  464. if (verbose)
  465. warning(" ... trying to fall back to cpu-clock-ticks\n");
  466. attr->type = PERF_TYPE_SOFTWARE;
  467. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  468. goto try_again;
  469. }
  470. printf("\n");
  471. error("perfcounter syscall returned with %d (%s)\n",
  472. fd[i][counter], strerror(err));
  473. die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n");
  474. exit(-1);
  475. }
  476. assert(fd[i][counter] >= 0);
  477. fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
  478. /*
  479. * First counter acts as the group leader:
  480. */
  481. if (group && group_fd == -1)
  482. group_fd = fd[i][counter];
  483. event_array[nr_poll].fd = fd[i][counter];
  484. event_array[nr_poll].events = POLLIN;
  485. nr_poll++;
  486. mmap_array[i][counter].counter = counter;
  487. mmap_array[i][counter].prev = 0;
  488. mmap_array[i][counter].mask = mmap_pages*page_size - 1;
  489. mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
  490. PROT_READ, MAP_SHARED, fd[i][counter], 0);
  491. if (mmap_array[i][counter].base == MAP_FAILED)
  492. die("failed to mmap with %d (%s)\n", errno, strerror(errno));
  493. }
  494. static int __cmd_top(void)
  495. {
  496. pthread_t thread;
  497. int i, counter;
  498. int ret;
  499. for (i = 0; i < nr_cpus; i++) {
  500. group_fd = -1;
  501. for (counter = 0; counter < nr_counters; counter++)
  502. start_counter(i, counter);
  503. }
  504. /* Wait for a minimal set of events before starting the snapshot */
  505. poll(event_array, nr_poll, 100);
  506. mmap_read();
  507. if (pthread_create(&thread, NULL, display_thread, NULL)) {
  508. printf("Could not create display thread.\n");
  509. exit(-1);
  510. }
  511. if (realtime_prio) {
  512. struct sched_param param;
  513. param.sched_priority = realtime_prio;
  514. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  515. printf("Could not set realtime priority.\n");
  516. exit(-1);
  517. }
  518. }
  519. while (1) {
  520. int hits = samples;
  521. mmap_read();
  522. if (hits == samples)
  523. ret = poll(event_array, nr_poll, 100);
  524. }
  525. return 0;
  526. }
  527. static const char * const top_usage[] = {
  528. "perf top [<options>]",
  529. NULL
  530. };
  531. static const struct option options[] = {
  532. OPT_CALLBACK('e', "event", NULL, "event",
  533. "event selector. use 'perf list' to list available events",
  534. parse_events),
  535. OPT_INTEGER('c', "count", &default_interval,
  536. "event period to sample"),
  537. OPT_INTEGER('p', "pid", &target_pid,
  538. "profile events on existing pid"),
  539. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  540. "system-wide collection from all CPUs"),
  541. OPT_INTEGER('C', "CPU", &profile_cpu,
  542. "CPU to profile on"),
  543. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  544. OPT_INTEGER('m', "mmap-pages", &mmap_pages,
  545. "number of mmap data pages"),
  546. OPT_INTEGER('r', "realtime", &realtime_prio,
  547. "collect data with this RT SCHED_FIFO priority"),
  548. OPT_INTEGER('d', "delay", &delay_secs,
  549. "number of seconds to delay between refreshes"),
  550. OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
  551. "dump the symbol table used for profiling"),
  552. OPT_INTEGER('f', "count-filter", &count_filter,
  553. "only display functions with more events than this"),
  554. OPT_BOOLEAN('g', "group", &group,
  555. "put the counters into a counter group"),
  556. OPT_BOOLEAN('i', "inherit", &inherit,
  557. "child tasks inherit counters"),
  558. OPT_STRING('s', "sym-filter", &sym_filter, "pattern",
  559. "only display symbols matchig this pattern"),
  560. OPT_BOOLEAN('z', "zero", &zero,
  561. "zero history across updates"),
  562. OPT_INTEGER('F', "freq", &freq,
  563. "profile at this frequency"),
  564. OPT_INTEGER('E', "entries", &print_entries,
  565. "display this many functions"),
  566. OPT_BOOLEAN('v', "verbose", &verbose,
  567. "be more verbose (show counter open errors, etc)"),
  568. OPT_END()
  569. };
  570. int cmd_top(int argc, const char **argv, const char *prefix __used)
  571. {
  572. int counter;
  573. symbol__init();
  574. page_size = sysconf(_SC_PAGE_SIZE);
  575. argc = parse_options(argc, argv, options, top_usage, 0);
  576. if (argc)
  577. usage_with_options(top_usage, options);
  578. if (freq) {
  579. default_interval = freq;
  580. freq = 1;
  581. }
  582. /* CPU and PID are mutually exclusive */
  583. if (target_pid != -1 && profile_cpu != -1) {
  584. printf("WARNING: PID switch overriding CPU\n");
  585. sleep(1);
  586. profile_cpu = -1;
  587. }
  588. if (!nr_counters)
  589. nr_counters = 1;
  590. if (delay_secs < 1)
  591. delay_secs = 1;
  592. parse_symbols();
  593. /*
  594. * Fill in the ones not specifically initialized via -c:
  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. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  602. assert(nr_cpus <= MAX_NR_CPUS);
  603. assert(nr_cpus >= 0);
  604. if (target_pid != -1 || profile_cpu != -1)
  605. nr_cpus = 1;
  606. return __cmd_top();
  607. }