perfstat.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * perfstat: /usr/bin/time -alike performance counter statistics utility
  3. *
  4. * It summarizes the counter events of all tasks (and child tasks),
  5. * covering all CPUs that the command (or workload) executes on.
  6. * It only counts the per-task events of the workload started,
  7. * independent of how many other tasks run on those CPUs.
  8. *
  9. * Build with: cc -O2 -g -lrt -Wall -W -o perfstat perfstat.c
  10. *
  11. * Sample output:
  12. *
  13. $ ./perfstat -e 1 -e 3 -e 5 ls -lR /usr/include/ >/dev/null
  14. Performance counter stats for 'ls':
  15. 163516953 instructions
  16. 2295 cache-misses
  17. 2855182 branch-misses
  18. *
  19. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  20. *
  21. * Released under the GPLv2 (not later).
  22. *
  23. * Percpu counter support by: Yanmin Zhang <yanmin_zhang@linux.intel.com>
  24. * Symbolic event options by: Wu Fengguang <fengguang.wu@intel.com>
  25. */
  26. #define _GNU_SOURCE
  27. #include <assert.h>
  28. #include <getopt.h>
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <ctype.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <time.h>
  38. #include <sys/syscall.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/prctl.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <sys/time.h>
  44. #include <sys/wait.h>
  45. #include <sys/uio.h>
  46. #include <linux/unistd.h>
  47. #include "perfcounters.h"
  48. static char *hw_event_names [] = {
  49. "CPU cycles",
  50. "instructions",
  51. "cache references",
  52. "cache misses",
  53. "branches",
  54. "branch misses",
  55. "bus cycles",
  56. };
  57. static char *sw_event_names [] = {
  58. "cpu clock ticks",
  59. "task clock ticks",
  60. "pagefaults",
  61. "context switches",
  62. "CPU migrations",
  63. };
  64. struct event_symbol {
  65. int event;
  66. char *symbol;
  67. };
  68. static struct event_symbol event_symbols [] = {
  69. {PERF_COUNT_CPU_CYCLES, "cpu-cycles", },
  70. {PERF_COUNT_CPU_CYCLES, "cycles", },
  71. {PERF_COUNT_INSTRUCTIONS, "instructions", },
  72. {PERF_COUNT_CACHE_REFERENCES, "cache-references", },
  73. {PERF_COUNT_CACHE_MISSES, "cache-misses", },
  74. {PERF_COUNT_BRANCH_INSTRUCTIONS, "branch-instructions", },
  75. {PERF_COUNT_BRANCH_INSTRUCTIONS, "branches", },
  76. {PERF_COUNT_BRANCH_MISSES, "branch-misses", },
  77. {PERF_COUNT_BUS_CYCLES, "bus-cycles", },
  78. {PERF_COUNT_CPU_CLOCK, "cpu-ticks", },
  79. {PERF_COUNT_CPU_CLOCK, "ticks", },
  80. {PERF_COUNT_TASK_CLOCK, "task-ticks", },
  81. {PERF_COUNT_PAGE_FAULTS, "page-faults", },
  82. {PERF_COUNT_PAGE_FAULTS, "faults", },
  83. {PERF_COUNT_CONTEXT_SWITCHES, "context-switches", },
  84. {PERF_COUNT_CONTEXT_SWITCHES, "cs", },
  85. {PERF_COUNT_CPU_MIGRATIONS, "cpu-migrations", },
  86. {PERF_COUNT_CPU_MIGRATIONS, "migrations", },
  87. };
  88. static int nr_counters = 0;
  89. static int nr_cpus = 0;
  90. static int event_id[MAX_COUNTERS] =
  91. { -2, -5, -4, -3, 0, 1, 2, 3};
  92. static int event_raw[MAX_COUNTERS];
  93. static int system_wide = 0;
  94. static void display_help(void)
  95. {
  96. unsigned int i;
  97. int e;
  98. printf(
  99. "Usage: perfstat [<events...>] <cmd...>\n\n"
  100. "PerfStat Options (up to %d event types can be specified):\n\n",
  101. MAX_COUNTERS);
  102. printf(
  103. " -e EVENT --event=EVENT # symbolic-name abbreviations");
  104. for (i = 0, e = PERF_HW_EVENTS_MAX; i < ARRAY_SIZE(event_symbols); i++) {
  105. if (e != event_symbols[i].event) {
  106. e = event_symbols[i].event;
  107. printf(
  108. "\n %2d: %-20s", e, event_symbols[i].symbol);
  109. } else
  110. printf(" %s", event_symbols[i].symbol);
  111. }
  112. printf("\n"
  113. " rNNN: raw event type\n\n"
  114. " -s # system-wide collection\n\n"
  115. " -c <cmd..> --command=<cmd..> # command+arguments to be timed.\n"
  116. "\n");
  117. exit(0);
  118. }
  119. static int type_valid(int type)
  120. {
  121. if (type >= PERF_HW_EVENTS_MAX)
  122. return 0;
  123. if (type <= PERF_SW_EVENTS_MIN)
  124. return 0;
  125. return 1;
  126. }
  127. static char *event_name(int ctr)
  128. {
  129. int type = event_id[ctr];
  130. static char buf[32];
  131. if (event_raw[ctr]) {
  132. sprintf(buf, "raw 0x%x", type);
  133. return buf;
  134. }
  135. if (!type_valid(type))
  136. return "unknown";
  137. if (type >= 0)
  138. return hw_event_names[type];
  139. return sw_event_names[-type-1];
  140. }
  141. /*
  142. * Each event can have multiple symbolic names.
  143. * Symbolic names are (almost) exactly matched.
  144. */
  145. static int match_event_symbols(char *str)
  146. {
  147. unsigned int i;
  148. if (isdigit(str[0]) || str[0] == '-')
  149. return atoi(str);
  150. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  151. if (!strncmp(str, event_symbols[i].symbol,
  152. strlen(event_symbols[i].symbol)))
  153. return event_symbols[i].event;
  154. }
  155. return PERF_HW_EVENTS_MAX;
  156. }
  157. static void parse_events(char *str)
  158. {
  159. int type, raw;
  160. again:
  161. nr_counters++;
  162. if (nr_counters == MAX_COUNTERS)
  163. display_help();
  164. raw = 0;
  165. if (*str == 'r') {
  166. raw = 1;
  167. ++str;
  168. type = strtol(str, NULL, 16);
  169. } else {
  170. type = match_event_symbols(str);
  171. if (!type_valid(type))
  172. display_help();
  173. }
  174. event_id[nr_counters] = type;
  175. event_raw[nr_counters] = raw;
  176. str = strstr(str, ",");
  177. if (str) {
  178. str++;
  179. goto again;
  180. }
  181. }
  182. static void process_options(int argc, char *argv[])
  183. {
  184. for (;;) {
  185. int option_index = 0;
  186. /** Options for getopt */
  187. static struct option long_options[] = {
  188. {"event", required_argument, NULL, 'e'},
  189. {"help", no_argument, NULL, 'h'},
  190. {"command", no_argument, NULL, 'c'},
  191. {NULL, 0, NULL, 0 }
  192. };
  193. int c = getopt_long(argc, argv, "+:e:c:s",
  194. long_options, &option_index);
  195. if (c == -1)
  196. break;
  197. switch (c) {
  198. case 'c':
  199. break;
  200. case 's':
  201. system_wide = 1;
  202. break;
  203. case 'e':
  204. parse_events(optarg);
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. if (optind == argc)
  211. goto err;
  212. if (!nr_counters)
  213. nr_counters = 8;
  214. else
  215. nr_counters++;
  216. return;
  217. err:
  218. display_help();
  219. }
  220. char fault_here[1000000];
  221. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  222. static void create_counter(int counter)
  223. {
  224. struct perf_counter_hw_event hw_event;
  225. memset(&hw_event, 0, sizeof(hw_event));
  226. hw_event.type = event_id[counter];
  227. hw_event.raw = event_raw[counter];
  228. hw_event.record_type = PERF_RECORD_SIMPLE;
  229. hw_event.nmi = 0;
  230. if (system_wide) {
  231. int cpu;
  232. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  233. fd[cpu][counter] = sys_perf_counter_open(&hw_event, -1, cpu, -1, 0);
  234. if (fd[cpu][counter] < 0) {
  235. printf("perfstat error: syscall returned with %d (%s)\n",
  236. fd[cpu][counter], strerror(errno));
  237. exit(-1);
  238. }
  239. }
  240. } else {
  241. hw_event.inherit = 1;
  242. hw_event.disabled = 1;
  243. fd[0][counter] = sys_perf_counter_open(&hw_event, 0, -1, -1, 0);
  244. if (fd[0][counter] < 0) {
  245. printf("perfstat error: syscall returned with %d (%s)\n",
  246. fd[0][counter], strerror(errno));
  247. exit(-1);
  248. }
  249. }
  250. }
  251. #define rdclock() \
  252. ({ \
  253. struct timespec ts; \
  254. \
  255. clock_gettime(CLOCK_MONOTONIC, &ts); \
  256. ts.tv_sec * 1000000000ULL + ts.tv_nsec; \
  257. })
  258. int main(int argc, char *argv[])
  259. {
  260. unsigned long long t0, t1;
  261. int counter;
  262. ssize_t res;
  263. int status;
  264. int pid;
  265. process_options(argc, argv);
  266. if (system_wide) {
  267. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  268. assert(nr_cpus <= MAX_NR_CPUS);
  269. assert(nr_cpus >= 0);
  270. } else
  271. nr_cpus = 1;
  272. for (counter = 0; counter < nr_counters; counter++)
  273. create_counter(counter);
  274. argc -= optind;
  275. argv += optind;
  276. /*
  277. * Enable counters and exec the command:
  278. */
  279. t0 = rdclock();
  280. prctl(PR_TASK_PERF_COUNTERS_ENABLE);
  281. if ((pid = fork()) < 0)
  282. perror("failed to fork");
  283. if (!pid) {
  284. if (execvp(argv[0], argv)) {
  285. perror(argv[0]);
  286. exit(-1);
  287. }
  288. }
  289. while (wait(&status) >= 0)
  290. ;
  291. prctl(PR_TASK_PERF_COUNTERS_DISABLE);
  292. t1 = rdclock();
  293. fflush(stdout);
  294. fprintf(stderr, "\n");
  295. fprintf(stderr, " Performance counter stats for \'%s\':\n",
  296. argv[0]);
  297. fprintf(stderr, "\n");
  298. for (counter = 0; counter < nr_counters; counter++) {
  299. int cpu;
  300. __u64 count, single_count;
  301. count = 0;
  302. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  303. res = read(fd[cpu][counter],
  304. (char *) &single_count, sizeof(single_count));
  305. assert(res == sizeof(single_count));
  306. count += single_count;
  307. }
  308. if (!event_raw[counter] &&
  309. (event_id[counter] == PERF_COUNT_CPU_CLOCK ||
  310. event_id[counter] == PERF_COUNT_TASK_CLOCK)) {
  311. double msecs = (double)count / 1000000;
  312. fprintf(stderr, " %14.6f %-20s (msecs)\n",
  313. msecs, event_name(counter));
  314. } else {
  315. fprintf(stderr, " %14Ld %-20s (events)\n",
  316. count, event_name(counter));
  317. }
  318. if (!counter)
  319. fprintf(stderr, "\n");
  320. }
  321. fprintf(stderr, "\n");
  322. fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
  323. (double)(t1-t0)/1e6);
  324. fprintf(stderr, "\n");
  325. return 0;
  326. }