kerneltop.c 18 KB

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