builtin-stat.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * kerneltop.c: show top kernel functions - performance counters showcase
  3. Build with:
  4. cc -O6 -Wall -c -o kerneltop.o kerneltop.c -lrt
  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. * perfstat: /usr/bin/time -alike performance counter statistics utility
  26. It summarizes the counter events of all tasks (and child tasks),
  27. covering all CPUs that the command (or workload) executes on.
  28. It only counts the per-task events of the workload started,
  29. independent of how many other tasks run on those CPUs.
  30. Sample output:
  31. $ ./perfstat -e 1 -e 3 -e 5 ls -lR /usr/include/ >/dev/null
  32. Performance counter stats for 'ls':
  33. 163516953 instructions
  34. 2295 cache-misses
  35. 2855182 branch-misses
  36. */
  37. /*
  38. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  39. *
  40. * Improvements and fixes by:
  41. *
  42. * Arjan van de Ven <arjan@linux.intel.com>
  43. * Yanmin Zhang <yanmin.zhang@intel.com>
  44. * Wu Fengguang <fengguang.wu@intel.com>
  45. * Mike Galbraith <efault@gmx.de>
  46. * Paul Mackerras <paulus@samba.org>
  47. *
  48. * Released under the GPL v2. (and only v2, not any later version)
  49. */
  50. #include "util.h"
  51. #include <getopt.h>
  52. #include <assert.h>
  53. #include <fcntl.h>
  54. #include <stdio.h>
  55. #include <errno.h>
  56. #include <ctype.h>
  57. #include <time.h>
  58. #include <sched.h>
  59. #include <pthread.h>
  60. #include <sys/syscall.h>
  61. #include <sys/ioctl.h>
  62. #include <sys/poll.h>
  63. #include <sys/prctl.h>
  64. #include <sys/wait.h>
  65. #include <sys/uio.h>
  66. #include <sys/mman.h>
  67. #include <linux/unistd.h>
  68. #include <linux/types.h>
  69. #include "../../include/linux/perf_counter.h"
  70. /*
  71. * prctl(PR_TASK_PERF_COUNTERS_DISABLE) will (cheaply) disable all
  72. * counters in the current task.
  73. */
  74. #define PR_TASK_PERF_COUNTERS_DISABLE 31
  75. #define PR_TASK_PERF_COUNTERS_ENABLE 32
  76. #define rdclock() \
  77. ({ \
  78. struct timespec ts; \
  79. \
  80. clock_gettime(CLOCK_MONOTONIC, &ts); \
  81. ts.tv_sec * 1000000000ULL + ts.tv_nsec; \
  82. })
  83. /*
  84. * Pick up some kernel type conventions:
  85. */
  86. #define __user
  87. #define asmlinkage
  88. #ifdef __x86_64__
  89. #define __NR_perf_counter_open 295
  90. #define rmb() asm volatile("lfence" ::: "memory")
  91. #define cpu_relax() asm volatile("rep; nop" ::: "memory");
  92. #endif
  93. #ifdef __i386__
  94. #define __NR_perf_counter_open 333
  95. #define rmb() asm volatile("lfence" ::: "memory")
  96. #define cpu_relax() asm volatile("rep; nop" ::: "memory");
  97. #endif
  98. #ifdef __powerpc__
  99. #define __NR_perf_counter_open 319
  100. #define rmb() asm volatile ("sync" ::: "memory")
  101. #define cpu_relax() asm volatile ("" ::: "memory");
  102. #endif
  103. #define unlikely(x) __builtin_expect(!!(x), 0)
  104. #define min(x, y) ({ \
  105. typeof(x) _min1 = (x); \
  106. typeof(y) _min2 = (y); \
  107. (void) (&_min1 == &_min2); \
  108. _min1 < _min2 ? _min1 : _min2; })
  109. extern asmlinkage int sys_perf_counter_open(
  110. struct perf_counter_hw_event *hw_event_uptr __user,
  111. pid_t pid,
  112. int cpu,
  113. int group_fd,
  114. unsigned long flags);
  115. #define MAX_COUNTERS 64
  116. #define MAX_NR_CPUS 256
  117. #define EID(type, id) (((__u64)(type) << PERF_COUNTER_TYPE_SHIFT) | (id))
  118. static int system_wide = 0;
  119. static int nr_counters = 0;
  120. static __u64 event_id[MAX_COUNTERS] = {
  121. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
  122. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
  123. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
  124. EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
  125. EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
  126. EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
  127. EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
  128. EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
  129. };
  130. static int default_interval = 100000;
  131. static int event_count[MAX_COUNTERS];
  132. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  133. static int tid = -1;
  134. static int profile_cpu = -1;
  135. static int nr_cpus = 0;
  136. static int nmi = 1;
  137. static int group = 0;
  138. static unsigned int page_size;
  139. static int zero;
  140. static int scale;
  141. static const unsigned int default_count[] = {
  142. 1000000,
  143. 1000000,
  144. 10000,
  145. 10000,
  146. 1000000,
  147. 10000,
  148. };
  149. static char *hw_event_names[] = {
  150. "CPU cycles",
  151. "instructions",
  152. "cache references",
  153. "cache misses",
  154. "branches",
  155. "branch misses",
  156. "bus cycles",
  157. };
  158. static char *sw_event_names[] = {
  159. "cpu clock ticks",
  160. "task clock ticks",
  161. "pagefaults",
  162. "context switches",
  163. "CPU migrations",
  164. "minor faults",
  165. "major faults",
  166. };
  167. struct event_symbol {
  168. __u64 event;
  169. char *symbol;
  170. };
  171. static struct event_symbol event_symbols[] = {
  172. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cpu-cycles", },
  173. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cycles", },
  174. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS), "instructions", },
  175. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES), "cache-references", },
  176. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES), "cache-misses", },
  177. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branch-instructions", },
  178. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branches", },
  179. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_MISSES), "branch-misses", },
  180. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BUS_CYCLES), "bus-cycles", },
  181. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK), "cpu-clock", },
  182. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK), "task-clock", },
  183. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "page-faults", },
  184. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "faults", },
  185. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MIN), "minor-faults", },
  186. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MAJ), "major-faults", },
  187. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "context-switches", },
  188. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "cs", },
  189. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "cpu-migrations", },
  190. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "migrations", },
  191. };
  192. #define __PERF_COUNTER_FIELD(config, name) \
  193. ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
  194. #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
  195. #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
  196. #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
  197. #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
  198. static void display_events_help(void)
  199. {
  200. unsigned int i;
  201. __u64 e;
  202. printf(
  203. " -e EVENT --event=EVENT # symbolic-name abbreviations");
  204. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  205. int type, id;
  206. e = event_symbols[i].event;
  207. type = PERF_COUNTER_TYPE(e);
  208. id = PERF_COUNTER_ID(e);
  209. printf("\n %d:%d: %-20s",
  210. type, id, event_symbols[i].symbol);
  211. }
  212. printf("\n"
  213. " rNNN: raw PMU events (eventsel+umask)\n\n");
  214. }
  215. static void display_help(void)
  216. {
  217. printf(
  218. "Usage: perfstat [<events...>] <cmd...>\n\n"
  219. "PerfStat Options (up to %d event types can be specified):\n\n",
  220. MAX_COUNTERS);
  221. display_events_help();
  222. printf(
  223. " -l # scale counter values\n"
  224. " -a # system-wide collection\n");
  225. exit(0);
  226. }
  227. static char *event_name(int ctr)
  228. {
  229. __u64 config = event_id[ctr];
  230. int type = PERF_COUNTER_TYPE(config);
  231. int id = PERF_COUNTER_ID(config);
  232. static char buf[32];
  233. if (PERF_COUNTER_RAW(config)) {
  234. sprintf(buf, "raw 0x%llx", PERF_COUNTER_CONFIG(config));
  235. return buf;
  236. }
  237. switch (type) {
  238. case PERF_TYPE_HARDWARE:
  239. if (id < PERF_HW_EVENTS_MAX)
  240. return hw_event_names[id];
  241. return "unknown-hardware";
  242. case PERF_TYPE_SOFTWARE:
  243. if (id < PERF_SW_EVENTS_MAX)
  244. return sw_event_names[id];
  245. return "unknown-software";
  246. default:
  247. break;
  248. }
  249. return "unknown";
  250. }
  251. /*
  252. * Each event can have multiple symbolic names.
  253. * Symbolic names are (almost) exactly matched.
  254. */
  255. static __u64 match_event_symbols(char *str)
  256. {
  257. __u64 config, id;
  258. int type;
  259. unsigned int i;
  260. if (sscanf(str, "r%llx", &config) == 1)
  261. return config | PERF_COUNTER_RAW_MASK;
  262. if (sscanf(str, "%d:%llu", &type, &id) == 2)
  263. return EID(type, id);
  264. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  265. if (!strncmp(str, event_symbols[i].symbol,
  266. strlen(event_symbols[i].symbol)))
  267. return event_symbols[i].event;
  268. }
  269. return ~0ULL;
  270. }
  271. static int parse_events(char *str)
  272. {
  273. __u64 config;
  274. again:
  275. if (nr_counters == MAX_COUNTERS)
  276. return -1;
  277. config = match_event_symbols(str);
  278. if (config == ~0ULL)
  279. return -1;
  280. event_id[nr_counters] = config;
  281. nr_counters++;
  282. str = strstr(str, ",");
  283. if (str) {
  284. str++;
  285. goto again;
  286. }
  287. return 0;
  288. }
  289. /*
  290. * perfstat
  291. */
  292. char fault_here[1000000];
  293. static void create_perfstat_counter(int counter)
  294. {
  295. struct perf_counter_hw_event hw_event;
  296. memset(&hw_event, 0, sizeof(hw_event));
  297. hw_event.config = event_id[counter];
  298. hw_event.record_type = 0;
  299. hw_event.nmi = 0;
  300. if (scale)
  301. hw_event.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  302. PERF_FORMAT_TOTAL_TIME_RUNNING;
  303. if (system_wide) {
  304. int cpu;
  305. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  306. fd[cpu][counter] = sys_perf_counter_open(&hw_event, -1, cpu, -1, 0);
  307. if (fd[cpu][counter] < 0) {
  308. printf("perfstat error: syscall returned with %d (%s)\n",
  309. fd[cpu][counter], strerror(errno));
  310. exit(-1);
  311. }
  312. }
  313. } else {
  314. hw_event.inherit = 1;
  315. hw_event.disabled = 1;
  316. fd[0][counter] = sys_perf_counter_open(&hw_event, 0, -1, -1, 0);
  317. if (fd[0][counter] < 0) {
  318. printf("perfstat error: syscall returned with %d (%s)\n",
  319. fd[0][counter], strerror(errno));
  320. exit(-1);
  321. }
  322. }
  323. }
  324. int do_perfstat(int argc, char *argv[])
  325. {
  326. unsigned long long t0, t1;
  327. int counter;
  328. ssize_t res;
  329. int status;
  330. int pid;
  331. if (!system_wide)
  332. nr_cpus = 1;
  333. for (counter = 0; counter < nr_counters; counter++)
  334. create_perfstat_counter(counter);
  335. argc -= optind;
  336. argv += optind;
  337. if (!argc)
  338. display_help();
  339. /*
  340. * Enable counters and exec the command:
  341. */
  342. t0 = rdclock();
  343. prctl(PR_TASK_PERF_COUNTERS_ENABLE);
  344. if ((pid = fork()) < 0)
  345. perror("failed to fork");
  346. if (!pid) {
  347. if (execvp(argv[0], argv)) {
  348. perror(argv[0]);
  349. exit(-1);
  350. }
  351. }
  352. while (wait(&status) >= 0)
  353. ;
  354. prctl(PR_TASK_PERF_COUNTERS_DISABLE);
  355. t1 = rdclock();
  356. fflush(stdout);
  357. fprintf(stderr, "\n");
  358. fprintf(stderr, " Performance counter stats for \'%s\':\n",
  359. argv[0]);
  360. fprintf(stderr, "\n");
  361. for (counter = 0; counter < nr_counters; counter++) {
  362. int cpu, nv;
  363. __u64 count[3], single_count[3];
  364. int scaled;
  365. count[0] = count[1] = count[2] = 0;
  366. nv = scale ? 3 : 1;
  367. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  368. res = read(fd[cpu][counter],
  369. single_count, nv * sizeof(__u64));
  370. assert(res == nv * sizeof(__u64));
  371. count[0] += single_count[0];
  372. if (scale) {
  373. count[1] += single_count[1];
  374. count[2] += single_count[2];
  375. }
  376. }
  377. scaled = 0;
  378. if (scale) {
  379. if (count[2] == 0) {
  380. fprintf(stderr, " %14s %-20s\n",
  381. "<not counted>", event_name(counter));
  382. continue;
  383. }
  384. if (count[2] < count[1]) {
  385. scaled = 1;
  386. count[0] = (unsigned long long)
  387. ((double)count[0] * count[1] / count[2] + 0.5);
  388. }
  389. }
  390. if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK) ||
  391. event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK)) {
  392. double msecs = (double)count[0] / 1000000;
  393. fprintf(stderr, " %14.6f %-20s (msecs)",
  394. msecs, event_name(counter));
  395. } else {
  396. fprintf(stderr, " %14Ld %-20s (events)",
  397. count[0], event_name(counter));
  398. }
  399. if (scaled)
  400. fprintf(stderr, " (scaled from %.2f%%)",
  401. (double) count[2] / count[1] * 100);
  402. fprintf(stderr, "\n");
  403. }
  404. fprintf(stderr, "\n");
  405. fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
  406. (double)(t1-t0)/1e6);
  407. fprintf(stderr, "\n");
  408. return 0;
  409. }
  410. static void process_options(int argc, char **argv)
  411. {
  412. int error = 0, counter;
  413. for (;;) {
  414. int option_index = 0;
  415. /** Options for getopt */
  416. static struct option long_options[] = {
  417. {"count", required_argument, NULL, 'c'},
  418. {"cpu", required_argument, NULL, 'C'},
  419. {"delay", required_argument, NULL, 'd'},
  420. {"dump_symtab", no_argument, NULL, 'D'},
  421. {"event", required_argument, NULL, 'e'},
  422. {"filter", required_argument, NULL, 'f'},
  423. {"group", required_argument, NULL, 'g'},
  424. {"help", no_argument, NULL, 'h'},
  425. {"nmi", required_argument, NULL, 'n'},
  426. {"munmap_info", no_argument, NULL, 'U'},
  427. {"pid", required_argument, NULL, 'p'},
  428. {"realtime", required_argument, NULL, 'r'},
  429. {"scale", no_argument, NULL, 'l'},
  430. {"symbol", required_argument, NULL, 's'},
  431. {"stat", no_argument, NULL, 'S'},
  432. {"vmlinux", required_argument, NULL, 'x'},
  433. {"zero", no_argument, NULL, 'z'},
  434. {NULL, 0, NULL, 0 }
  435. };
  436. int c = getopt_long(argc, argv, "+:ac:C:d:De:f:g:hln:m:p:r:s:Sx:zMU",
  437. long_options, &option_index);
  438. if (c == -1)
  439. break;
  440. switch (c) {
  441. case 'a': system_wide = 1; break;
  442. case 'c': default_interval = atoi(optarg); break;
  443. case 'C':
  444. /* CPU and PID are mutually exclusive */
  445. if (tid != -1) {
  446. printf("WARNING: CPU switch overriding PID\n");
  447. sleep(1);
  448. tid = -1;
  449. }
  450. profile_cpu = atoi(optarg); break;
  451. case 'e': error = parse_events(optarg); break;
  452. case 'g': group = atoi(optarg); break;
  453. case 'h': display_help(); break;
  454. case 'l': scale = 1; break;
  455. case 'n': nmi = atoi(optarg); break;
  456. case 'p':
  457. /* CPU and PID are mutually exclusive */
  458. if (profile_cpu != -1) {
  459. printf("WARNING: PID switch overriding CPU\n");
  460. sleep(1);
  461. profile_cpu = -1;
  462. }
  463. tid = atoi(optarg); break;
  464. case 'z': zero = 1; break;
  465. default: error = 1; break;
  466. }
  467. }
  468. if (error)
  469. display_help();
  470. if (!nr_counters) {
  471. nr_counters = 8;
  472. }
  473. for (counter = 0; counter < nr_counters; counter++) {
  474. if (event_count[counter])
  475. continue;
  476. event_count[counter] = default_interval;
  477. }
  478. }
  479. int cmd_stat(int argc, char **argv, const char *prefix)
  480. {
  481. page_size = sysconf(_SC_PAGE_SIZE);
  482. process_options(argc, argv);
  483. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  484. assert(nr_cpus <= MAX_NR_CPUS);
  485. assert(nr_cpus >= 0);
  486. return do_perfstat(argc, argv);
  487. }