perfstat.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 int nr_cpus = 0;
  49. static int system_wide = 0;
  50. static void display_help(void)
  51. {
  52. unsigned int i;
  53. int e;
  54. printf(
  55. "Usage: perfstat [<events...>] <cmd...>\n\n"
  56. "PerfStat Options (up to %d event types can be specified):\n\n",
  57. MAX_COUNTERS);
  58. printf(
  59. " -e EVENT --event=EVENT # symbolic-name abbreviations");
  60. for (i = 0, e = PERF_HW_EVENTS_MAX; i < ARRAY_SIZE(event_symbols); i++) {
  61. if (e != event_symbols[i].event) {
  62. e = event_symbols[i].event;
  63. printf(
  64. "\n %2d: %-20s", e, event_symbols[i].symbol);
  65. } else
  66. printf(" %s", event_symbols[i].symbol);
  67. }
  68. printf("\n"
  69. " rNNN: raw event type\n\n"
  70. " -s # system-wide collection\n\n"
  71. " -c <cmd..> --command=<cmd..> # command+arguments to be timed.\n"
  72. "\n");
  73. exit(0);
  74. }
  75. static void process_options(int argc, char *argv[])
  76. {
  77. for (;;) {
  78. int option_index = 0;
  79. /** Options for getopt */
  80. static struct option long_options[] = {
  81. {"event", required_argument, NULL, 'e'},
  82. {"help", no_argument, NULL, 'h'},
  83. {"command", no_argument, NULL, 'c'},
  84. {NULL, 0, NULL, 0 }
  85. };
  86. int c = getopt_long(argc, argv, "+:e:c:s",
  87. long_options, &option_index);
  88. if (c == -1)
  89. break;
  90. switch (c) {
  91. case 'c':
  92. break;
  93. case 's':
  94. system_wide = 1;
  95. break;
  96. case 'e':
  97. parse_events(optarg);
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. if (optind == argc)
  104. goto err;
  105. if (!nr_counters)
  106. nr_counters = 8;
  107. return;
  108. err:
  109. display_help();
  110. }
  111. char fault_here[1000000];
  112. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  113. static void create_counter(int counter)
  114. {
  115. struct perf_counter_hw_event hw_event;
  116. memset(&hw_event, 0, sizeof(hw_event));
  117. hw_event.type = event_id[counter];
  118. hw_event.raw = event_raw[counter];
  119. hw_event.record_type = PERF_RECORD_SIMPLE;
  120. hw_event.nmi = 0;
  121. if (system_wide) {
  122. int cpu;
  123. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  124. fd[cpu][counter] = sys_perf_counter_open(&hw_event, -1, cpu, -1, 0);
  125. if (fd[cpu][counter] < 0) {
  126. printf("perfstat error: syscall returned with %d (%s)\n",
  127. fd[cpu][counter], strerror(errno));
  128. exit(-1);
  129. }
  130. }
  131. } else {
  132. hw_event.inherit = 1;
  133. hw_event.disabled = 1;
  134. fd[0][counter] = sys_perf_counter_open(&hw_event, 0, -1, -1, 0);
  135. if (fd[0][counter] < 0) {
  136. printf("perfstat error: syscall returned with %d (%s)\n",
  137. fd[0][counter], strerror(errno));
  138. exit(-1);
  139. }
  140. }
  141. }
  142. int main(int argc, char *argv[])
  143. {
  144. unsigned long long t0, t1;
  145. int counter;
  146. ssize_t res;
  147. int status;
  148. int pid;
  149. process_options(argc, argv);
  150. if (system_wide) {
  151. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  152. assert(nr_cpus <= MAX_NR_CPUS);
  153. assert(nr_cpus >= 0);
  154. } else
  155. nr_cpus = 1;
  156. for (counter = 0; counter < nr_counters; counter++)
  157. create_counter(counter);
  158. argc -= optind;
  159. argv += optind;
  160. /*
  161. * Enable counters and exec the command:
  162. */
  163. t0 = rdclock();
  164. prctl(PR_TASK_PERF_COUNTERS_ENABLE);
  165. if ((pid = fork()) < 0)
  166. perror("failed to fork");
  167. if (!pid) {
  168. if (execvp(argv[0], argv)) {
  169. perror(argv[0]);
  170. exit(-1);
  171. }
  172. }
  173. while (wait(&status) >= 0)
  174. ;
  175. prctl(PR_TASK_PERF_COUNTERS_DISABLE);
  176. t1 = rdclock();
  177. fflush(stdout);
  178. fprintf(stderr, "\n");
  179. fprintf(stderr, " Performance counter stats for \'%s\':\n",
  180. argv[0]);
  181. fprintf(stderr, "\n");
  182. for (counter = 0; counter < nr_counters; counter++) {
  183. int cpu;
  184. __u64 count, single_count;
  185. count = 0;
  186. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  187. res = read(fd[cpu][counter],
  188. (char *) &single_count, sizeof(single_count));
  189. assert(res == sizeof(single_count));
  190. count += single_count;
  191. }
  192. if (!event_raw[counter] &&
  193. (event_id[counter] == PERF_COUNT_CPU_CLOCK ||
  194. event_id[counter] == PERF_COUNT_TASK_CLOCK)) {
  195. double msecs = (double)count / 1000000;
  196. fprintf(stderr, " %14.6f %-20s (msecs)\n",
  197. msecs, event_name(counter));
  198. } else {
  199. fprintf(stderr, " %14Ld %-20s (events)\n",
  200. count, event_name(counter));
  201. }
  202. if (!counter)
  203. fprintf(stderr, "\n");
  204. }
  205. fprintf(stderr, "\n");
  206. fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
  207. (double)(t1-t0)/1e6);
  208. fprintf(stderr, "\n");
  209. return 0;
  210. }