builtin-top.c 18 KB

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