kerneltop.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * kerneltop.c: show top kernel functions - performance counters showcase
  3. Build with:
  4. cc -O6 -Wall `pkg-config --cflags --libs glib-2.0` -o kerneltop kerneltop.c
  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. Started by Ingo Molnar <mingo@redhat.com>
  24. Improvements and fixes by:
  25. Arjan van de Ven <arjan@linux.intel.com>
  26. Yanmin Zhang <yanmin.zhang@intel.com>
  27. Mike Galbraith <efault@gmx.de>
  28. Released under the GPL v2. (and only v2, not any later version)
  29. */
  30. #define _GNU_SOURCE
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <sys/time.h>
  34. #include <unistd.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <getopt.h>
  39. #include <assert.h>
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <errno.h>
  43. #include <ctype.h>
  44. #include <time.h>
  45. #include <glib.h>
  46. #include <sys/syscall.h>
  47. #include <sys/ioctl.h>
  48. #include <sys/poll.h>
  49. #include <sys/prctl.h>
  50. #include <sys/wait.h>
  51. #include <sys/uio.h>
  52. #include <linux/unistd.h>
  53. #include "perfcounters.h"
  54. const char *event_types [] = {
  55. "CPU cycles",
  56. "instructions",
  57. "cache-refs",
  58. "cache-misses",
  59. "branches",
  60. "branch-misses",
  61. "bus cycles"
  62. };
  63. const unsigned int default_count[] = {
  64. 1000000,
  65. 1000000,
  66. 10000,
  67. 10000,
  68. 1000000,
  69. 10000,
  70. };
  71. static int nr_counters = -1;
  72. static __u64 count_filter = 100;
  73. static int event_count[MAX_COUNTERS];
  74. static unsigned long event_id[MAX_COUNTERS];
  75. static int event_raw[MAX_COUNTERS];
  76. static int tid = -1;
  77. static int profile_cpu = -1;
  78. static int nr_cpus = 0;
  79. static int nmi = 1;
  80. static int group = 0;
  81. static char *vmlinux;
  82. static char *sym_filter;
  83. static unsigned long filter_start;
  84. static unsigned long filter_end;
  85. static int delay_secs = 2;
  86. static int zero;
  87. static int dump_symtab;
  88. struct source_line {
  89. uint64_t EIP;
  90. unsigned long count;
  91. char *line;
  92. };
  93. static GList *lines;
  94. static void display_help(void)
  95. {
  96. printf(
  97. "Usage: kerneltop [<options>]\n\n"
  98. "KernelTop Options (up to %d event types can be specified at once):\n\n",
  99. MAX_COUNTERS);
  100. printf(
  101. " -e EID --event_id=EID # event type ID [default: 0]\n"
  102. " 0: CPU cycles\n"
  103. " 1: instructions\n"
  104. " 2: cache accesses\n"
  105. " 3: cache misses\n"
  106. " 4: branch instructions\n"
  107. " 5: branch prediction misses\n"
  108. " 6: bus cycles\n\n"
  109. " rNNN: raw PMU events (eventsel+umask)\n\n"
  110. " -c CNT --count=CNT # event period to sample\n\n"
  111. " -C CPU --cpu=CPU # CPU (-1 for all) [default: -1]\n"
  112. " -p PID --pid=PID # PID of sampled task (-1 for all) [default: -1]\n\n"
  113. " -d delay --delay=<seconds> # sampling/display delay [default: 2]\n"
  114. " -f CNT --filter=CNT # min-event-count filter [default: 100]\n\n"
  115. " -s symbol --symbol=<symbol> # function to be showed annotated one-shot\n"
  116. " -x path --vmlinux=<path> # the vmlinux binary, required for -s use:\n"
  117. " -z --zero # zero counts after display\n"
  118. " -D --dump_symtab # dump symbol table to stderr on startup\n"
  119. "\n");
  120. exit(0);
  121. }
  122. static void process_options(int argc, char *argv[])
  123. {
  124. int error = 0, counter;
  125. for (;;) {
  126. int option_index = 0;
  127. /** Options for getopt */
  128. static struct option long_options[] = {
  129. {"count", required_argument, NULL, 'c'},
  130. {"cpu", required_argument, NULL, 'C'},
  131. {"delay", required_argument, NULL, 'd'},
  132. {"dump_symtab", no_argument, NULL, 'D'},
  133. {"event_id", required_argument, NULL, 'e'},
  134. {"filter", required_argument, NULL, 'f'},
  135. {"group", required_argument, NULL, 'g'},
  136. {"help", no_argument, NULL, 'h'},
  137. {"nmi", required_argument, NULL, 'n'},
  138. {"pid", required_argument, NULL, 'p'},
  139. {"vmlinux", required_argument, NULL, 'x'},
  140. {"symbol", required_argument, NULL, 's'},
  141. {"zero", no_argument, NULL, 'z'},
  142. {NULL, 0, NULL, 0 }
  143. };
  144. int c = getopt_long(argc, argv, "c:C:d:De:f:g:hn:p:s:x:z",
  145. long_options, &option_index);
  146. if (c == -1)
  147. break;
  148. switch (c) {
  149. case 'c':
  150. if (nr_counters == -1)
  151. nr_counters = 0;
  152. event_count[nr_counters] = atoi(optarg); break;
  153. case 'C':
  154. /* CPU and PID are mutually exclusive */
  155. if (tid != -1) {
  156. printf("WARNING: CPU switch overriding PID\n");
  157. sleep(1);
  158. tid = -1;
  159. }
  160. profile_cpu = atoi(optarg); break;
  161. case 'd': delay_secs = atoi(optarg); break;
  162. case 'D': dump_symtab = 1; break;
  163. case 'e':
  164. nr_counters++;
  165. if (nr_counters == MAX_COUNTERS) {
  166. error = 1;
  167. break;
  168. }
  169. if (*optarg == 'r') {
  170. event_raw[nr_counters] = 1;
  171. ++optarg;
  172. }
  173. event_id[nr_counters] = strtol(optarg, NULL, 16);
  174. break;
  175. case 'f': count_filter = atoi(optarg); break;
  176. case 'g': group = atoi(optarg); break;
  177. case 'h': display_help(); break;
  178. case 'n': nmi = atoi(optarg); break;
  179. case 'p':
  180. /* CPU and PID are mutually exclusive */
  181. if (profile_cpu != -1) {
  182. printf("WARNING: PID switch overriding CPU\n");
  183. sleep(1);
  184. profile_cpu = -1;
  185. }
  186. tid = atoi(optarg); break;
  187. case 's': sym_filter = strdup(optarg); break;
  188. case 'x': vmlinux = strdup(optarg); break;
  189. case 'z': zero = 1; break;
  190. default: error = 1; break;
  191. }
  192. }
  193. if (error)
  194. display_help();
  195. nr_counters++;
  196. if (nr_counters < 1)
  197. nr_counters = 1;
  198. for (counter = 0; counter < nr_counters; counter++) {
  199. if (event_count[counter])
  200. continue;
  201. if (event_id[counter] < PERF_HW_EVENTS_MAX)
  202. event_count[counter] = default_count[event_id[counter]];
  203. else
  204. event_count[counter] = 100000;
  205. }
  206. }
  207. static uint64_t min_ip;
  208. static uint64_t max_ip = -1ll;
  209. struct sym_entry {
  210. unsigned long long addr;
  211. char *sym;
  212. unsigned long count[MAX_COUNTERS];
  213. int skip;
  214. GList *source;
  215. };
  216. #define MAX_SYMS 100000
  217. static int sym_table_count;
  218. struct sym_entry *sym_filter_entry;
  219. static struct sym_entry sym_table[MAX_SYMS];
  220. static void show_details(struct sym_entry *sym);
  221. /*
  222. * Ordering weight: count-1 * count-1 * ... / count-n
  223. */
  224. static double sym_weight(const struct sym_entry *sym)
  225. {
  226. double weight;
  227. int counter;
  228. weight = sym->count[0];
  229. for (counter = 1; counter < nr_counters-1; counter++)
  230. weight *= sym->count[counter];
  231. weight /= (sym->count[counter] + 1);
  232. return weight;
  233. }
  234. static int compare(const void *__sym1, const void *__sym2)
  235. {
  236. const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
  237. return sym_weight(sym1) < sym_weight(sym2);
  238. }
  239. static time_t last_refresh;
  240. static long events;
  241. static long userspace_events;
  242. static const char CONSOLE_CLEAR[] = "";
  243. static struct sym_entry tmp[MAX_SYMS];
  244. static void print_sym_table(void)
  245. {
  246. int i, printed;
  247. int counter;
  248. float events_per_sec = events/delay_secs;
  249. float kevents_per_sec = (events-userspace_events)/delay_secs;
  250. memcpy(tmp, sym_table, sizeof(sym_table[0])*sym_table_count);
  251. qsort(tmp, sym_table_count, sizeof(tmp[0]), compare);
  252. write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR));
  253. printf(
  254. "------------------------------------------------------------------------------\n");
  255. printf( " KernelTop:%8.0f irqs/sec kernel:%3.1f%% [%s, ",
  256. events_per_sec,
  257. 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec)),
  258. nmi ? "NMI" : "IRQ");
  259. if (nr_counters == 1)
  260. printf("%d ", event_count[0]);
  261. for (counter = 0; counter < nr_counters; counter++) {
  262. if (counter)
  263. printf("/");
  264. if (event_id[counter] < PERF_HW_EVENTS_MAX)
  265. printf( "%s", event_types[event_id[counter]]);
  266. else
  267. printf( "raw:%04lx", event_id[counter]);
  268. }
  269. printf( "], ");
  270. if (tid != -1)
  271. printf(" (tid: %d", tid);
  272. else
  273. printf(" (all");
  274. if (profile_cpu != -1)
  275. printf(", cpu: %d)\n", profile_cpu);
  276. else {
  277. if (tid != -1)
  278. printf(")\n");
  279. else
  280. printf(", %d CPUs)\n", nr_cpus);
  281. }
  282. printf("------------------------------------------------------------------------------\n\n");
  283. if (nr_counters == 1)
  284. printf(" events");
  285. else
  286. printf(" weight events");
  287. printf(" RIP kernel function\n"
  288. " ______ ______ ________________ _______________\n\n"
  289. );
  290. printed = 0;
  291. for (i = 0; i < sym_table_count; i++) {
  292. int count;
  293. if (nr_counters == 1) {
  294. if (printed <= 18 &&
  295. tmp[i].count[0] >= count_filter) {
  296. printf("%19.2f - %016llx : %s\n",
  297. sym_weight(tmp + i), tmp[i].addr, tmp[i].sym);
  298. printed++;
  299. }
  300. } else {
  301. if (printed <= 18 &&
  302. tmp[i].count[0] >= count_filter) {
  303. printf("%8.1f %10ld - %016llx : %s\n",
  304. sym_weight(tmp + i),
  305. tmp[i].count[0],
  306. tmp[i].addr, tmp[i].sym);
  307. printed++;
  308. }
  309. }
  310. /*
  311. * Add decay to the counts:
  312. */
  313. for (count = 0; count < nr_counters; count++)
  314. sym_table[i].count[count] = zero ? 0 : sym_table[i].count[count] * 7 / 8;
  315. }
  316. if (sym_filter_entry)
  317. show_details(sym_filter_entry);
  318. last_refresh = time(NULL);
  319. {
  320. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  321. if (poll(&stdin_poll, 1, 0) == 1) {
  322. printf("key pressed - exiting.\n");
  323. exit(0);
  324. }
  325. }
  326. }
  327. static int read_symbol(FILE *in, struct sym_entry *s)
  328. {
  329. static int filter_match = 0;
  330. char *sym, stype;
  331. char str[500];
  332. int rc, pos;
  333. rc = fscanf(in, "%llx %c %499s", &s->addr, &stype, str);
  334. if (rc == EOF)
  335. return -1;
  336. assert(rc == 3);
  337. /* skip until end of line: */
  338. pos = strlen(str);
  339. do {
  340. rc = fgetc(in);
  341. if (rc == '\n' || rc == EOF || pos >= 499)
  342. break;
  343. str[pos] = rc;
  344. pos++;
  345. } while (1);
  346. str[pos] = 0;
  347. sym = str;
  348. /* Filter out known duplicates and non-text symbols. */
  349. if (!strcmp(sym, "_text"))
  350. return 1;
  351. if (!min_ip && !strcmp(sym, "_stext"))
  352. return 1;
  353. if (!strcmp(sym, "_etext") || !strcmp(sym, "_sinittext"))
  354. return 1;
  355. if (stype != 'T' && stype != 't')
  356. return 1;
  357. if (!strncmp("init_module", sym, 11) || !strncmp("cleanup_module", sym, 14))
  358. return 1;
  359. if (strstr(sym, "_text_start") || strstr(sym, "_text_end"))
  360. return 1;
  361. s->sym = malloc(strlen(str));
  362. assert(s->sym);
  363. strcpy((char *)s->sym, str);
  364. s->skip = 0;
  365. /* Tag events to be skipped. */
  366. if (!strcmp("default_idle", s->sym) || !strcmp("cpu_idle", s->sym))
  367. s->skip = 1;
  368. if (!strcmp("enter_idle", s->sym) || !strcmp("exit_idle", s->sym))
  369. s->skip = 1;
  370. if (filter_match == 1) {
  371. filter_end = s->addr;
  372. filter_match = -1;
  373. if (filter_end - filter_start > 10000) {
  374. printf("hm, too large filter symbol <%s> - skipping.\n",
  375. sym_filter);
  376. printf("symbol filter start: %016lx\n", filter_start);
  377. printf(" end: %016lx\n", filter_end);
  378. filter_end = filter_start = 0;
  379. sym_filter = NULL;
  380. sleep(1);
  381. }
  382. }
  383. if (filter_match == 0 && sym_filter && !strcmp(s->sym, sym_filter)) {
  384. filter_match = 1;
  385. filter_start = s->addr;
  386. }
  387. return 0;
  388. }
  389. int compare_addr(const void *__sym1, const void *__sym2)
  390. {
  391. const struct sym_entry *sym1 = __sym1, *sym2 = __sym2;
  392. return sym1->addr > sym2->addr;
  393. }
  394. static void sort_symbol_table(void)
  395. {
  396. int i, dups;
  397. do {
  398. qsort(sym_table, sym_table_count, sizeof(sym_table[0]), compare_addr);
  399. for (i = 0, dups = 0; i < sym_table_count; i++) {
  400. if (sym_table[i].addr == sym_table[i+1].addr) {
  401. sym_table[i+1].addr = -1ll;
  402. dups++;
  403. }
  404. }
  405. sym_table_count -= dups;
  406. } while(dups);
  407. }
  408. static void parse_symbols(void)
  409. {
  410. struct sym_entry *last;
  411. FILE *kallsyms = fopen("/proc/kallsyms", "r");
  412. if (!kallsyms) {
  413. printf("Could not open /proc/kallsyms - no CONFIG_KALLSYMS_ALL=y?\n");
  414. exit(-1);
  415. }
  416. while (!feof(kallsyms)) {
  417. if (read_symbol(kallsyms, &sym_table[sym_table_count]) == 0) {
  418. sym_table_count++;
  419. assert(sym_table_count <= MAX_SYMS);
  420. }
  421. }
  422. sort_symbol_table();
  423. min_ip = sym_table[0].addr;
  424. max_ip = sym_table[sym_table_count-1].addr;
  425. last = sym_table + sym_table_count++;
  426. last->addr = -1ll;
  427. last->sym = "<end>";
  428. if (filter_end) {
  429. int count;
  430. for (count=0; count < sym_table_count; count ++) {
  431. if (!strcmp(sym_table[count].sym, sym_filter)) {
  432. sym_filter_entry = &sym_table[count];
  433. break;
  434. }
  435. }
  436. }
  437. if (dump_symtab) {
  438. int i;
  439. for (i = 0; i < sym_table_count; i++)
  440. fprintf(stderr, "%llx %s\n",
  441. sym_table[i].addr, sym_table[i].sym);
  442. }
  443. }
  444. static void parse_vmlinux(char *filename)
  445. {
  446. FILE *file;
  447. char command[PATH_MAX*2];
  448. if (!filename)
  449. return;
  450. sprintf(command, "objdump --start-address=0x%016lx --stop-address=0x%016lx -dS %s", filter_start, filter_end, filename);
  451. file = popen(command, "r");
  452. if (!file)
  453. return;
  454. while (!feof(file)) {
  455. struct source_line *src;
  456. size_t dummy = 0;
  457. char *c;
  458. src = malloc(sizeof(struct source_line));
  459. assert(src != NULL);
  460. memset(src, 0, sizeof(struct source_line));
  461. if (getline(&src->line, &dummy, file) < 0)
  462. break;
  463. if (!src->line)
  464. break;
  465. c = strchr(src->line, '\n');
  466. if (c)
  467. *c = 0;
  468. lines = g_list_prepend(lines, src);
  469. if (strlen(src->line)>8 && src->line[8] == ':')
  470. src->EIP = strtoull(src->line, NULL, 16);
  471. if (strlen(src->line)>8 && src->line[16] == ':')
  472. src->EIP = strtoull(src->line, NULL, 16);
  473. }
  474. pclose(file);
  475. lines = g_list_reverse(lines);
  476. }
  477. static void record_precise_ip(uint64_t ip)
  478. {
  479. struct source_line *line;
  480. GList *item;
  481. item = g_list_first(lines);
  482. while (item) {
  483. line = item->data;
  484. if (line->EIP == ip)
  485. line->count++;
  486. if (line->EIP > ip)
  487. break;
  488. item = g_list_next(item);
  489. }
  490. }
  491. static void lookup_sym_in_vmlinux(struct sym_entry *sym)
  492. {
  493. struct source_line *line;
  494. GList *item;
  495. char pattern[PATH_MAX];
  496. sprintf(pattern, "<%s>:", sym->sym);
  497. item = g_list_first(lines);
  498. while (item) {
  499. line = item->data;
  500. if (strstr(line->line, pattern)) {
  501. sym->source = item;
  502. break;
  503. }
  504. item = g_list_next(item);
  505. }
  506. }
  507. void show_lines(GList *item_queue, int item_queue_count)
  508. {
  509. int i;
  510. struct source_line *line;
  511. for (i = 0; i < item_queue_count; i++) {
  512. line = item_queue->data;
  513. printf("%8li\t%s\n", line->count, line->line);
  514. item_queue = g_list_next(item_queue);
  515. }
  516. }
  517. #define TRACE_COUNT 3
  518. static void show_details(struct sym_entry *sym)
  519. {
  520. struct source_line *line;
  521. GList *item;
  522. int displayed = 0;
  523. GList *item_queue = NULL;
  524. int item_queue_count = 0;
  525. if (!sym->source)
  526. lookup_sym_in_vmlinux(sym);
  527. if (!sym->source)
  528. return;
  529. printf("Showing details for %s\n", sym->sym);
  530. item = sym->source;
  531. while (item) {
  532. line = item->data;
  533. if (displayed && strstr(line->line, ">:"))
  534. break;
  535. if (!item_queue_count)
  536. item_queue = item;
  537. item_queue_count ++;
  538. if (line->count >= count_filter) {
  539. show_lines(item_queue, item_queue_count);
  540. item_queue_count = 0;
  541. item_queue = NULL;
  542. } else if (item_queue_count > TRACE_COUNT) {
  543. item_queue = g_list_next(item_queue);
  544. item_queue_count --;
  545. }
  546. line->count = 0;
  547. displayed++;
  548. if (displayed > 300)
  549. break;
  550. item = g_list_next(item);
  551. }
  552. }
  553. /*
  554. * Binary search in the histogram table and record the hit:
  555. */
  556. static void record_ip(uint64_t ip, int counter)
  557. {
  558. int left_idx, middle_idx, right_idx, idx;
  559. unsigned long left, middle, right;
  560. record_precise_ip(ip);
  561. left_idx = 0;
  562. right_idx = sym_table_count-1;
  563. assert(ip <= max_ip && ip >= min_ip);
  564. while (left_idx + 1 < right_idx) {
  565. middle_idx = (left_idx + right_idx) / 2;
  566. left = sym_table[ left_idx].addr;
  567. middle = sym_table[middle_idx].addr;
  568. right = sym_table[ right_idx].addr;
  569. if (!(left <= middle && middle <= right)) {
  570. printf("%016lx...\n%016lx...\n%016lx\n", left, middle, right);
  571. printf("%d %d %d\n", left_idx, middle_idx, right_idx);
  572. }
  573. assert(left <= middle && middle <= right);
  574. if (!(left <= ip && ip <= right)) {
  575. printf(" left: %016lx\n", left);
  576. printf(" ip: %016lx\n", ip);
  577. printf("right: %016lx\n", right);
  578. }
  579. assert(left <= ip && ip <= right);
  580. /*
  581. * [ left .... target .... middle .... right ]
  582. * => right := middle
  583. */
  584. if (ip < middle) {
  585. right_idx = middle_idx;
  586. continue;
  587. }
  588. /*
  589. * [ left .... middle ... target ... right ]
  590. * => left := middle
  591. */
  592. left_idx = middle_idx;
  593. }
  594. idx = left_idx;
  595. if (!sym_table[idx].skip)
  596. sym_table[idx].count[counter]++;
  597. else events--;
  598. }
  599. static void process_event(uint64_t ip, int counter)
  600. {
  601. events++;
  602. if (ip < min_ip || ip > max_ip) {
  603. userspace_events++;
  604. return;
  605. }
  606. record_ip(ip, counter);
  607. }
  608. int main(int argc, char *argv[])
  609. {
  610. struct pollfd event_array[MAX_NR_CPUS][MAX_COUNTERS];
  611. struct perf_counter_hw_event hw_event;
  612. int fd[MAX_NR_CPUS][MAX_COUNTERS];
  613. int i, counter, group_fd;
  614. unsigned int cpu;
  615. uint64_t ip;
  616. ssize_t res;
  617. int ret;
  618. process_options(argc, argv);
  619. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  620. if (tid != -1 || profile_cpu != -1)
  621. nr_cpus = 1;
  622. assert(nr_cpus <= MAX_NR_CPUS);
  623. for (i = 0; i < nr_cpus; i++) {
  624. group_fd = -1;
  625. for (counter = 0; counter < nr_counters; counter++) {
  626. cpu = profile_cpu;
  627. if (tid == -1 && profile_cpu == -1)
  628. cpu = i;
  629. memset(&hw_event, 0, sizeof(hw_event));
  630. hw_event.type = event_id[counter];
  631. hw_event.raw = event_raw[counter];
  632. hw_event.irq_period = event_count[counter];
  633. hw_event.record_type = PERF_RECORD_IRQ;
  634. hw_event.nmi = nmi;
  635. fd[i][counter] = sys_perf_counter_open(&hw_event, tid, cpu, group_fd, 0);
  636. fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
  637. if (fd[i][counter] < 0) {
  638. printf("kerneltop error: syscall returned with %d (%s)\n",
  639. fd[i][counter], strerror(-fd[i][counter]));
  640. if (fd[i][counter] == -1)
  641. printf("Are you root?\n");
  642. exit(-1);
  643. }
  644. assert(fd[i][counter] >= 0);
  645. /*
  646. * First counter acts as the group leader:
  647. */
  648. if (group && group_fd == -1)
  649. group_fd = fd[i][counter];
  650. event_array[i][counter].fd = fd[i][counter];
  651. event_array[i][counter].events = POLLIN;
  652. }
  653. }
  654. parse_symbols();
  655. if (vmlinux && sym_filter_entry)
  656. parse_vmlinux(vmlinux);
  657. printf("KernelTop refresh period: %d seconds\n", delay_secs);
  658. last_refresh = time(NULL);
  659. while (1) {
  660. int hits = events;
  661. for (i = 0; i < nr_cpus; i++) {
  662. for (counter = 0; counter < nr_counters; counter++) {
  663. res = read(fd[i][counter], (char *) &ip, sizeof(ip));
  664. if (res > 0) {
  665. assert(res == sizeof(ip));
  666. process_event(ip, counter);
  667. }
  668. }
  669. }
  670. if (time(NULL) >= last_refresh + delay_secs) {
  671. print_sym_table();
  672. events = userspace_events = 0;
  673. }
  674. if (hits == events)
  675. ret = poll(event_array[0], nr_cpus, 1000);
  676. hits = events;
  677. }
  678. return 0;
  679. }