builtin-top.c 19 KB

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