builtin-top.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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 "util/util.h"
  39. #include "util/util.h"
  40. #include "util/parse-options.h"
  41. #include "util/parse-events.h"
  42. #include <assert.h>
  43. #include <fcntl.h>
  44. #include <stdio.h>
  45. #include <errno.h>
  46. #include <time.h>
  47. #include <sched.h>
  48. #include <pthread.h>
  49. #include <sys/syscall.h>
  50. #include <sys/ioctl.h>
  51. #include <sys/poll.h>
  52. #include <sys/prctl.h>
  53. #include <sys/wait.h>
  54. #include <sys/uio.h>
  55. #include <sys/mman.h>
  56. #include <linux/unistd.h>
  57. #include <linux/types.h>
  58. static int system_wide = 0;
  59. static __u64 default_event_id[MAX_COUNTERS] = {
  60. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
  61. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
  62. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
  63. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
  64. EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
  65. EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
  66. EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
  67. EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
  68. };
  69. static int default_interval = 100000;
  70. static int event_count[MAX_COUNTERS];
  71. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  72. static __u64 count_filter = 100;
  73. static int target_pid = -1;
  74. static int profile_cpu = -1;
  75. static int nr_cpus = 0;
  76. static unsigned int realtime_prio = 0;
  77. static int group = 0;
  78. static unsigned int page_size;
  79. static unsigned int mmap_pages = 16;
  80. static int use_mmap = 0;
  81. static int use_munmap = 0;
  82. static int freq = 0;
  83. static char *sym_filter;
  84. static unsigned long filter_start;
  85. static unsigned long filter_end;
  86. static int delay_secs = 2;
  87. static int zero;
  88. static int dump_symtab;
  89. static const unsigned int default_count[] = {
  90. 1000000,
  91. 1000000,
  92. 10000,
  93. 10000,
  94. 1000000,
  95. 10000,
  96. };
  97. /*
  98. * Symbols
  99. */
  100. static uint64_t min_ip;
  101. static uint64_t max_ip = -1ll;
  102. struct sym_entry {
  103. unsigned long long addr;
  104. char *sym;
  105. unsigned long count[MAX_COUNTERS];
  106. int skip;
  107. };
  108. #define MAX_SYMS 100000
  109. static int sym_table_count;
  110. struct sym_entry *sym_filter_entry;
  111. static struct sym_entry sym_table[MAX_SYMS];
  112. /*
  113. * Ordering weight: count-1 * count-2 * ... / count-n
  114. */
  115. static double sym_weight(const struct sym_entry *sym)
  116. {
  117. double weight;
  118. int counter;
  119. weight = sym->count[0];
  120. for (counter = 1; counter < nr_counters-1; counter++)
  121. weight *= sym->count[counter];
  122. weight /= (sym->count[counter] + 1);
  123. return weight;
  124. }
  125. static int compare(const void *__sym1, const void *__sym2)
  126. {
  127. const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
  128. return sym_weight(sym1) < sym_weight(sym2);
  129. }
  130. static long events;
  131. static long userspace_events;
  132. static const char CONSOLE_CLEAR[] = "";
  133. static struct sym_entry tmp[MAX_SYMS];
  134. static void print_sym_table(void)
  135. {
  136. int i, j, active_count, printed;
  137. int counter;
  138. float events_per_sec = events/delay_secs;
  139. float kevents_per_sec = (events-userspace_events)/delay_secs;
  140. float sum_kevents = 0.0;
  141. events = userspace_events = 0;
  142. /* Iterate over symbol table and copy/tally/decay active symbols. */
  143. for (i = 0, active_count = 0; i < sym_table_count; i++) {
  144. if (sym_table[i].count[0]) {
  145. tmp[active_count++] = sym_table[i];
  146. sum_kevents += sym_table[i].count[0];
  147. for (j = 0; j < nr_counters; j++)
  148. sym_table[i].count[j] = zero ? 0 : sym_table[i].count[j] * 7 / 8;
  149. }
  150. }
  151. qsort(tmp, active_count + 1, sizeof(tmp[0]), compare);
  152. write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR));
  153. printf(
  154. "------------------------------------------------------------------------------\n");
  155. printf( " KernelTop:%8.0f irqs/sec kernel:%4.1f%% [",
  156. events_per_sec,
  157. 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec)));
  158. if (nr_counters == 1)
  159. printf("%d ", event_count[0]);
  160. for (counter = 0; counter < nr_counters; counter++) {
  161. if (counter)
  162. printf("/");
  163. printf("%s", event_name(counter));
  164. }
  165. printf( "], ");
  166. if (target_pid != -1)
  167. printf(" (target_pid: %d", target_pid);
  168. else
  169. printf(" (all");
  170. if (profile_cpu != -1)
  171. printf(", cpu: %d)\n", profile_cpu);
  172. else {
  173. if (target_pid != -1)
  174. printf(")\n");
  175. else
  176. printf(", %d CPUs)\n", nr_cpus);
  177. }
  178. printf("------------------------------------------------------------------------------\n\n");
  179. if (nr_counters == 1)
  180. printf(" events pcnt");
  181. else
  182. printf(" weight events pcnt");
  183. printf(" RIP kernel function\n"
  184. " ______ ______ _____ ________________ _______________\n\n"
  185. );
  186. for (i = 0, printed = 0; i < active_count; i++) {
  187. float pcnt;
  188. if (++printed > 18 || tmp[i].count[0] < count_filter)
  189. break;
  190. pcnt = 100.0 - (100.0*((sum_kevents-tmp[i].count[0])/sum_kevents));
  191. if (nr_counters == 1)
  192. printf("%19.2f - %4.1f%% - %016llx : %s\n",
  193. sym_weight(tmp + i),
  194. pcnt, tmp[i].addr, tmp[i].sym);
  195. else
  196. printf("%8.1f %10ld - %4.1f%% - %016llx : %s\n",
  197. sym_weight(tmp + i),
  198. tmp[i].count[0],
  199. pcnt, tmp[i].addr, tmp[i].sym);
  200. }
  201. {
  202. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  203. if (poll(&stdin_poll, 1, 0) == 1) {
  204. printf("key pressed - exiting.\n");
  205. exit(0);
  206. }
  207. }
  208. }
  209. static void *display_thread(void *arg)
  210. {
  211. printf("KernelTop refresh period: %d seconds\n", delay_secs);
  212. while (!sleep(delay_secs))
  213. print_sym_table();
  214. return NULL;
  215. }
  216. static int read_symbol(FILE *in, struct sym_entry *s)
  217. {
  218. static int filter_match = 0;
  219. char *sym, stype;
  220. char str[500];
  221. int rc, pos;
  222. rc = fscanf(in, "%llx %c %499s", &s->addr, &stype, str);
  223. if (rc == EOF)
  224. return -1;
  225. assert(rc == 3);
  226. /* skip until end of line: */
  227. pos = strlen(str);
  228. do {
  229. rc = fgetc(in);
  230. if (rc == '\n' || rc == EOF || pos >= 499)
  231. break;
  232. str[pos] = rc;
  233. pos++;
  234. } while (1);
  235. str[pos] = 0;
  236. sym = str;
  237. /* Filter out known duplicates and non-text symbols. */
  238. if (!strcmp(sym, "_text"))
  239. return 1;
  240. if (!min_ip && !strcmp(sym, "_stext"))
  241. return 1;
  242. if (!strcmp(sym, "_etext") || !strcmp(sym, "_sinittext"))
  243. return 1;
  244. if (stype != 'T' && stype != 't')
  245. return 1;
  246. if (!strncmp("init_module", sym, 11) || !strncmp("cleanup_module", sym, 14))
  247. return 1;
  248. if (strstr(sym, "_text_start") || strstr(sym, "_text_end"))
  249. return 1;
  250. s->sym = malloc(strlen(str)+1);
  251. assert(s->sym);
  252. strcpy((char *)s->sym, str);
  253. s->skip = 0;
  254. /* Tag events to be skipped. */
  255. if (!strcmp("default_idle", s->sym) || !strcmp("cpu_idle", s->sym))
  256. s->skip = 1;
  257. else if (!strcmp("enter_idle", s->sym) || !strcmp("exit_idle", s->sym))
  258. s->skip = 1;
  259. else if (!strcmp("mwait_idle", s->sym))
  260. s->skip = 1;
  261. if (filter_match == 1) {
  262. filter_end = s->addr;
  263. filter_match = -1;
  264. if (filter_end - filter_start > 10000) {
  265. printf("hm, too large filter symbol <%s> - skipping.\n",
  266. sym_filter);
  267. printf("symbol filter start: %016lx\n", filter_start);
  268. printf(" end: %016lx\n", filter_end);
  269. filter_end = filter_start = 0;
  270. sym_filter = NULL;
  271. sleep(1);
  272. }
  273. }
  274. if (filter_match == 0 && sym_filter && !strcmp(s->sym, sym_filter)) {
  275. filter_match = 1;
  276. filter_start = s->addr;
  277. }
  278. return 0;
  279. }
  280. static int compare_addr(const void *__sym1, const void *__sym2)
  281. {
  282. const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
  283. return sym1->addr > sym2->addr;
  284. }
  285. static void sort_symbol_table(void)
  286. {
  287. int i, dups;
  288. do {
  289. qsort(sym_table, sym_table_count, sizeof(sym_table[0]), compare_addr);
  290. for (i = 0, dups = 0; i < sym_table_count; i++) {
  291. if (sym_table[i].addr == sym_table[i+1].addr) {
  292. sym_table[i+1].addr = -1ll;
  293. dups++;
  294. }
  295. }
  296. sym_table_count -= dups;
  297. } while(dups);
  298. }
  299. static void parse_symbols(void)
  300. {
  301. struct sym_entry *last;
  302. FILE *kallsyms = fopen("/proc/kallsyms", "r");
  303. if (!kallsyms) {
  304. printf("Could not open /proc/kallsyms - no CONFIG_KALLSYMS_ALL=y?\n");
  305. exit(-1);
  306. }
  307. while (!feof(kallsyms)) {
  308. if (read_symbol(kallsyms, &sym_table[sym_table_count]) == 0) {
  309. sym_table_count++;
  310. assert(sym_table_count <= MAX_SYMS);
  311. }
  312. }
  313. sort_symbol_table();
  314. min_ip = sym_table[0].addr;
  315. max_ip = sym_table[sym_table_count-1].addr;
  316. last = sym_table + sym_table_count++;
  317. last->addr = -1ll;
  318. last->sym = "<end>";
  319. if (filter_end) {
  320. int count;
  321. for (count=0; count < sym_table_count; count ++) {
  322. if (!strcmp(sym_table[count].sym, sym_filter)) {
  323. sym_filter_entry = &sym_table[count];
  324. break;
  325. }
  326. }
  327. }
  328. if (dump_symtab) {
  329. int i;
  330. for (i = 0; i < sym_table_count; i++)
  331. fprintf(stderr, "%llx %s\n",
  332. sym_table[i].addr, sym_table[i].sym);
  333. }
  334. }
  335. #define TRACE_COUNT 3
  336. /*
  337. * Binary search in the histogram table and record the hit:
  338. */
  339. static void record_ip(uint64_t ip, int counter)
  340. {
  341. int left_idx, middle_idx, right_idx, idx;
  342. unsigned long left, middle, right;
  343. left_idx = 0;
  344. right_idx = sym_table_count-1;
  345. assert(ip <= max_ip && ip >= min_ip);
  346. while (left_idx + 1 < right_idx) {
  347. middle_idx = (left_idx + right_idx) / 2;
  348. left = sym_table[ left_idx].addr;
  349. middle = sym_table[middle_idx].addr;
  350. right = sym_table[ right_idx].addr;
  351. if (!(left <= middle && middle <= right)) {
  352. printf("%016lx...\n%016lx...\n%016lx\n", left, middle, right);
  353. printf("%d %d %d\n", left_idx, middle_idx, right_idx);
  354. }
  355. assert(left <= middle && middle <= right);
  356. if (!(left <= ip && ip <= right)) {
  357. printf(" left: %016lx\n", left);
  358. printf(" ip: %016lx\n", (unsigned long)ip);
  359. printf("right: %016lx\n", right);
  360. }
  361. assert(left <= ip && ip <= right);
  362. /*
  363. * [ left .... target .... middle .... right ]
  364. * => right := middle
  365. */
  366. if (ip < middle) {
  367. right_idx = middle_idx;
  368. continue;
  369. }
  370. /*
  371. * [ left .... middle ... target ... right ]
  372. * => left := middle
  373. */
  374. left_idx = middle_idx;
  375. }
  376. idx = left_idx;
  377. if (!sym_table[idx].skip)
  378. sym_table[idx].count[counter]++;
  379. else events--;
  380. }
  381. static void process_event(uint64_t ip, int counter)
  382. {
  383. events++;
  384. if (ip < min_ip || ip > max_ip) {
  385. userspace_events++;
  386. return;
  387. }
  388. record_ip(ip, counter);
  389. }
  390. struct mmap_data {
  391. int counter;
  392. void *base;
  393. unsigned int mask;
  394. unsigned int prev;
  395. };
  396. static unsigned int mmap_read_head(struct mmap_data *md)
  397. {
  398. struct perf_counter_mmap_page *pc = md->base;
  399. int head;
  400. head = pc->data_head;
  401. rmb();
  402. return head;
  403. }
  404. struct timeval last_read, this_read;
  405. static void mmap_read(struct mmap_data *md)
  406. {
  407. unsigned int head = mmap_read_head(md);
  408. unsigned int old = md->prev;
  409. unsigned char *data = md->base + page_size;
  410. int diff;
  411. gettimeofday(&this_read, NULL);
  412. /*
  413. * If we're further behind than half the buffer, there's a chance
  414. * the writer will bite our tail and screw up the events under us.
  415. *
  416. * If we somehow ended up ahead of the head, we got messed up.
  417. *
  418. * In either case, truncate and restart at head.
  419. */
  420. diff = head - old;
  421. if (diff > md->mask / 2 || diff < 0) {
  422. struct timeval iv;
  423. unsigned long msecs;
  424. timersub(&this_read, &last_read, &iv);
  425. msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
  426. fprintf(stderr, "WARNING: failed to keep up with mmap data."
  427. " Last read %lu msecs ago.\n", msecs);
  428. /*
  429. * head points to a known good entry, start there.
  430. */
  431. old = head;
  432. }
  433. last_read = this_read;
  434. for (; old != head;) {
  435. struct ip_event {
  436. struct perf_event_header header;
  437. __u64 ip;
  438. __u32 pid, target_pid;
  439. };
  440. struct mmap_event {
  441. struct perf_event_header header;
  442. __u32 pid, target_pid;
  443. __u64 start;
  444. __u64 len;
  445. __u64 pgoff;
  446. char filename[PATH_MAX];
  447. };
  448. typedef union event_union {
  449. struct perf_event_header header;
  450. struct ip_event ip;
  451. struct mmap_event mmap;
  452. } event_t;
  453. event_t *event = (event_t *)&data[old & md->mask];
  454. event_t event_copy;
  455. size_t size = event->header.size;
  456. /*
  457. * Event straddles the mmap boundary -- header should always
  458. * be inside due to u64 alignment of output.
  459. */
  460. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  461. unsigned int offset = old;
  462. unsigned int len = min(sizeof(*event), size), cpy;
  463. void *dst = &event_copy;
  464. do {
  465. cpy = min(md->mask + 1 - (offset & md->mask), len);
  466. memcpy(dst, &data[offset & md->mask], cpy);
  467. offset += cpy;
  468. dst += cpy;
  469. len -= cpy;
  470. } while (len);
  471. event = &event_copy;
  472. }
  473. old += size;
  474. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  475. if (event->header.type & PERF_RECORD_IP)
  476. process_event(event->ip.ip, md->counter);
  477. } else {
  478. switch (event->header.type) {
  479. case PERF_EVENT_MMAP:
  480. case PERF_EVENT_MUNMAP:
  481. printf("%s: %Lu %Lu %Lu %s\n",
  482. event->header.type == PERF_EVENT_MMAP
  483. ? "mmap" : "munmap",
  484. event->mmap.start,
  485. event->mmap.len,
  486. event->mmap.pgoff,
  487. event->mmap.filename);
  488. break;
  489. }
  490. }
  491. }
  492. md->prev = old;
  493. }
  494. static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
  495. static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  496. static int __cmd_top(void)
  497. {
  498. struct perf_counter_hw_event hw_event;
  499. pthread_t thread;
  500. int i, counter, group_fd, nr_poll = 0;
  501. unsigned int cpu;
  502. int ret;
  503. for (i = 0; i < nr_cpus; i++) {
  504. group_fd = -1;
  505. for (counter = 0; counter < nr_counters; counter++) {
  506. cpu = profile_cpu;
  507. if (target_pid == -1 && profile_cpu == -1)
  508. cpu = i;
  509. memset(&hw_event, 0, sizeof(hw_event));
  510. hw_event.config = event_id[counter];
  511. hw_event.irq_period = event_count[counter];
  512. hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID;
  513. hw_event.nmi = 1;
  514. hw_event.mmap = use_mmap;
  515. hw_event.munmap = use_munmap;
  516. hw_event.freq = freq;
  517. fd[i][counter] = sys_perf_counter_open(&hw_event, target_pid, cpu, group_fd, 0);
  518. if (fd[i][counter] < 0) {
  519. int err = errno;
  520. printf("kerneltop error: syscall returned with %d (%s)\n",
  521. fd[i][counter], strerror(err));
  522. if (err == EPERM)
  523. printf("Are you root?\n");
  524. exit(-1);
  525. }
  526. assert(fd[i][counter] >= 0);
  527. fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
  528. /*
  529. * First counter acts as the group leader:
  530. */
  531. if (group && group_fd == -1)
  532. group_fd = fd[i][counter];
  533. event_array[nr_poll].fd = fd[i][counter];
  534. event_array[nr_poll].events = POLLIN;
  535. nr_poll++;
  536. mmap_array[i][counter].counter = counter;
  537. mmap_array[i][counter].prev = 0;
  538. mmap_array[i][counter].mask = mmap_pages*page_size - 1;
  539. mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
  540. PROT_READ, MAP_SHARED, fd[i][counter], 0);
  541. if (mmap_array[i][counter].base == MAP_FAILED) {
  542. printf("kerneltop error: failed to mmap with %d (%s)\n",
  543. errno, strerror(errno));
  544. exit(-1);
  545. }
  546. }
  547. }
  548. if (pthread_create(&thread, NULL, display_thread, NULL)) {
  549. printf("Could not create display thread.\n");
  550. exit(-1);
  551. }
  552. if (realtime_prio) {
  553. struct sched_param param;
  554. param.sched_priority = realtime_prio;
  555. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  556. printf("Could not set realtime priority.\n");
  557. exit(-1);
  558. }
  559. }
  560. while (1) {
  561. int hits = events;
  562. for (i = 0; i < nr_cpus; i++) {
  563. for (counter = 0; counter < nr_counters; counter++)
  564. mmap_read(&mmap_array[i][counter]);
  565. }
  566. if (hits == events)
  567. ret = poll(event_array, nr_poll, 100);
  568. }
  569. return 0;
  570. }
  571. static const char * const top_usage[] = {
  572. "perf top [<options>]",
  573. NULL
  574. };
  575. static char events_help_msg[EVENTS_HELP_MAX];
  576. static const struct option options[] = {
  577. OPT_CALLBACK('e', "event", NULL, "event",
  578. events_help_msg, parse_events),
  579. OPT_INTEGER('c', "count", &default_interval,
  580. "event period to sample"),
  581. OPT_INTEGER('p', "pid", &target_pid,
  582. "profile events on existing pid"),
  583. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  584. "system-wide collection from all CPUs"),
  585. OPT_INTEGER('C', "CPU", &profile_cpu,
  586. "CPU to profile on"),
  587. OPT_INTEGER('m', "mmap-pages", &mmap_pages,
  588. "number of mmap data pages"),
  589. OPT_INTEGER('r', "realtime", &realtime_prio,
  590. "collect data with this RT SCHED_FIFO priority"),
  591. OPT_INTEGER('d', "delay", &realtime_prio,
  592. "number of seconds to delay between refreshes"),
  593. OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
  594. "dump the symbol table used for profiling"),
  595. OPT_INTEGER('f', "--count-filter", &count_filter,
  596. "only display functions with more events than this"),
  597. OPT_BOOLEAN('g', "group", &group,
  598. "put the counters into a counter group"),
  599. OPT_STRING('s', "sym-filter", &sym_filter, "pattern",
  600. "only display symbols matchig this pattern"),
  601. OPT_BOOLEAN('z', "zero", &group,
  602. "zero history across updates"),
  603. OPT_BOOLEAN('M', "use-mmap", &use_mmap,
  604. "track mmap events"),
  605. OPT_BOOLEAN('U', "use-munmap", &use_munmap,
  606. "track munmap events"),
  607. OPT_INTEGER('F', "--freq", &freq,
  608. "profile at this frequency"),
  609. OPT_END()
  610. };
  611. int cmd_top(int argc, const char **argv, const char *prefix)
  612. {
  613. int counter;
  614. page_size = sysconf(_SC_PAGE_SIZE);
  615. create_events_help(events_help_msg);
  616. memcpy(event_id, default_event_id, sizeof(default_event_id));
  617. argc = parse_options(argc, argv, options, top_usage, 0);
  618. if (argc)
  619. usage_with_options(top_usage, options);
  620. if (freq) {
  621. default_interval = freq;
  622. freq = 1;
  623. }
  624. /* CPU and PID are mutually exclusive */
  625. if (target_pid != -1 && profile_cpu != -1) {
  626. printf("WARNING: PID switch overriding CPU\n");
  627. sleep(1);
  628. profile_cpu = -1;
  629. }
  630. if (!nr_counters) {
  631. nr_counters = 1;
  632. event_id[0] = 0;
  633. }
  634. for (counter = 0; counter < nr_counters; counter++) {
  635. if (event_count[counter])
  636. continue;
  637. event_count[counter] = default_interval;
  638. }
  639. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  640. assert(nr_cpus <= MAX_NR_CPUS);
  641. assert(nr_cpus >= 0);
  642. if (target_pid != -1 || profile_cpu != -1)
  643. nr_cpus = 1;
  644. parse_symbols();
  645. return __cmd_top();
  646. }