builtin-stat.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * builtin-stat.c
  3. *
  4. * Builtin stat command: Give a precise performance counters summary
  5. * overview about any workload, CPU or specific PID.
  6. *
  7. * Sample output:
  8. $ perf stat ~/hackbench 10
  9. Time: 0.104
  10. Performance counter stats for '/home/mingo/hackbench':
  11. 1255.538611 task clock ticks # 10.143 CPU utilization factor
  12. 54011 context switches # 0.043 M/sec
  13. 385 CPU migrations # 0.000 M/sec
  14. 17755 pagefaults # 0.014 M/sec
  15. 3808323185 CPU cycles # 3033.219 M/sec
  16. 1575111190 instructions # 1254.530 M/sec
  17. 17367895 cache references # 13.833 M/sec
  18. 7674421 cache misses # 6.112 M/sec
  19. Wall-clock time elapsed: 123.786620 msecs
  20. *
  21. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  22. *
  23. * Improvements and fixes by:
  24. *
  25. * Arjan van de Ven <arjan@linux.intel.com>
  26. * Yanmin Zhang <yanmin.zhang@intel.com>
  27. * Wu Fengguang <fengguang.wu@intel.com>
  28. * Mike Galbraith <efault@gmx.de>
  29. * Paul Mackerras <paulus@samba.org>
  30. *
  31. * Released under the GPL v2. (and only v2, not any later version)
  32. */
  33. #include "perf.h"
  34. #include "builtin.h"
  35. #include "util/util.h"
  36. #include "util/parse-options.h"
  37. #include "util/parse-events.h"
  38. #include <sys/prctl.h>
  39. static struct perf_counter_attr default_attrs[MAX_COUNTERS] = {
  40. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
  41. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES},
  42. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
  43. { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
  44. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
  45. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
  46. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES},
  47. { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES },
  48. };
  49. static int system_wide = 0;
  50. static int inherit = 1;
  51. static int verbose = 0;
  52. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  53. static int target_pid = -1;
  54. static int nr_cpus = 0;
  55. static unsigned int page_size;
  56. static int scale = 1;
  57. static const unsigned int default_count[] = {
  58. 1000000,
  59. 1000000,
  60. 10000,
  61. 10000,
  62. 1000000,
  63. 10000,
  64. };
  65. static __u64 event_res[MAX_COUNTERS][3];
  66. static __u64 event_scaled[MAX_COUNTERS];
  67. static __u64 runtime_nsecs;
  68. static __u64 walltime_nsecs;
  69. static __u64 runtime_cycles;
  70. static void create_perf_stat_counter(int counter)
  71. {
  72. struct perf_counter_attr *attr = attrs + counter;
  73. if (scale)
  74. attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
  75. PERF_FORMAT_TOTAL_TIME_RUNNING;
  76. if (system_wide) {
  77. int cpu;
  78. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  79. fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
  80. if (fd[cpu][counter] < 0 && verbose) {
  81. printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[cpu][counter], strerror(errno));
  82. }
  83. }
  84. } else {
  85. attr->inherit = inherit;
  86. attr->disabled = 1;
  87. fd[0][counter] = sys_perf_counter_open(attr, 0, -1, -1, 0);
  88. if (fd[0][counter] < 0 && verbose) {
  89. printf("Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n", counter, fd[0][counter], strerror(errno));
  90. }
  91. }
  92. }
  93. /*
  94. * Does the counter have nsecs as a unit?
  95. */
  96. static inline int nsec_counter(int counter)
  97. {
  98. if (attrs[counter].type != PERF_TYPE_SOFTWARE)
  99. return 0;
  100. if (attrs[counter].config == PERF_COUNT_SW_CPU_CLOCK)
  101. return 1;
  102. if (attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
  103. return 1;
  104. return 0;
  105. }
  106. /*
  107. * Read out the results of a single counter:
  108. */
  109. static void read_counter(int counter)
  110. {
  111. __u64 *count, single_count[3];
  112. ssize_t res;
  113. int cpu, nv;
  114. int scaled;
  115. count = event_res[counter];
  116. count[0] = count[1] = count[2] = 0;
  117. nv = scale ? 3 : 1;
  118. for (cpu = 0; cpu < nr_cpus; cpu ++) {
  119. if (fd[cpu][counter] < 0)
  120. continue;
  121. res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
  122. assert(res == nv * sizeof(__u64));
  123. count[0] += single_count[0];
  124. if (scale) {
  125. count[1] += single_count[1];
  126. count[2] += single_count[2];
  127. }
  128. }
  129. scaled = 0;
  130. if (scale) {
  131. if (count[2] == 0) {
  132. event_scaled[counter] = -1;
  133. count[0] = 0;
  134. return;
  135. }
  136. if (count[2] < count[1]) {
  137. event_scaled[counter] = 1;
  138. count[0] = (unsigned long long)
  139. ((double)count[0] * count[1] / count[2] + 0.5);
  140. }
  141. }
  142. /*
  143. * Save the full runtime - to allow normalization during printout:
  144. */
  145. if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
  146. attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK)
  147. runtime_nsecs = count[0];
  148. if (attrs[counter].type == PERF_TYPE_HARDWARE &&
  149. attrs[counter].config == PERF_COUNT_HW_CPU_CYCLES)
  150. runtime_cycles = count[0];
  151. }
  152. /*
  153. * Print out the results of a single counter:
  154. */
  155. static void print_counter(int counter)
  156. {
  157. __u64 *count;
  158. int scaled;
  159. count = event_res[counter];
  160. scaled = event_scaled[counter];
  161. if (scaled == -1) {
  162. fprintf(stderr, " %14s %-20s\n",
  163. "<not counted>", event_name(counter));
  164. return;
  165. }
  166. if (nsec_counter(counter)) {
  167. double msecs = (double)count[0] / 1000000;
  168. fprintf(stderr, " %14.6f %-20s",
  169. msecs, event_name(counter));
  170. if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
  171. attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK) {
  172. if (walltime_nsecs)
  173. fprintf(stderr, " # %11.3f CPU utilization factor",
  174. (double)count[0] / (double)walltime_nsecs);
  175. }
  176. } else {
  177. fprintf(stderr, " %14Ld %-20s",
  178. count[0], event_name(counter));
  179. if (runtime_nsecs)
  180. fprintf(stderr, " # %11.3f M/sec",
  181. (double)count[0]/runtime_nsecs*1000.0);
  182. if (runtime_cycles &&
  183. attrs[counter].type == PERF_TYPE_HARDWARE &&
  184. attrs[counter].config == PERF_COUNT_HW_INSTRUCTIONS) {
  185. fprintf(stderr, " # %1.3f per cycle",
  186. (double)count[0] / (double)runtime_cycles);
  187. }
  188. }
  189. if (scaled)
  190. fprintf(stderr, " (scaled from %.2f%%)",
  191. (double) count[2] / count[1] * 100);
  192. fprintf(stderr, "\n");
  193. }
  194. static int do_perf_stat(int argc, const char **argv)
  195. {
  196. unsigned long long t0, t1;
  197. int counter;
  198. int status;
  199. int pid;
  200. int i;
  201. if (!system_wide)
  202. nr_cpus = 1;
  203. for (counter = 0; counter < nr_counters; counter++)
  204. create_perf_stat_counter(counter);
  205. /*
  206. * Enable counters and exec the command:
  207. */
  208. t0 = rdclock();
  209. prctl(PR_TASK_PERF_COUNTERS_ENABLE);
  210. if ((pid = fork()) < 0)
  211. perror("failed to fork");
  212. if (!pid) {
  213. if (execvp(argv[0], (char **)argv)) {
  214. perror(argv[0]);
  215. exit(-1);
  216. }
  217. }
  218. while (wait(&status) >= 0)
  219. ;
  220. prctl(PR_TASK_PERF_COUNTERS_DISABLE);
  221. t1 = rdclock();
  222. walltime_nsecs = t1 - t0;
  223. fflush(stdout);
  224. fprintf(stderr, "\n");
  225. fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
  226. for (i = 1; i < argc; i++)
  227. fprintf(stderr, " %s", argv[i]);
  228. fprintf(stderr, "\':\n");
  229. fprintf(stderr, "\n");
  230. for (counter = 0; counter < nr_counters; counter++)
  231. read_counter(counter);
  232. for (counter = 0; counter < nr_counters; counter++)
  233. print_counter(counter);
  234. fprintf(stderr, "\n");
  235. fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
  236. (double)(t1-t0)/1e6);
  237. fprintf(stderr, "\n");
  238. return 0;
  239. }
  240. static volatile int signr = -1;
  241. static void skip_signal(int signo)
  242. {
  243. signr = signo;
  244. }
  245. static void sig_atexit(void)
  246. {
  247. if (signr == -1)
  248. return;
  249. signal(signr, SIG_DFL);
  250. kill(getpid(), signr);
  251. }
  252. static const char * const stat_usage[] = {
  253. "perf stat [<options>] <command>",
  254. NULL
  255. };
  256. static const struct option options[] = {
  257. OPT_CALLBACK('e', "event", NULL, "event",
  258. "event selector. use 'perf list' to list available events",
  259. parse_events),
  260. OPT_BOOLEAN('i', "inherit", &inherit,
  261. "child tasks inherit counters"),
  262. OPT_INTEGER('p', "pid", &target_pid,
  263. "stat events on existing pid"),
  264. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  265. "system-wide collection from all CPUs"),
  266. OPT_BOOLEAN('S', "scale", &scale,
  267. "scale/normalize counters"),
  268. OPT_BOOLEAN('v', "verbose", &verbose,
  269. "be more verbose (show counter open errors, etc)"),
  270. OPT_END()
  271. };
  272. int cmd_stat(int argc, const char **argv, const char *prefix)
  273. {
  274. page_size = sysconf(_SC_PAGE_SIZE);
  275. memcpy(attrs, default_attrs, sizeof(attrs));
  276. argc = parse_options(argc, argv, options, stat_usage, 0);
  277. if (!argc)
  278. usage_with_options(stat_usage, options);
  279. if (!nr_counters)
  280. nr_counters = 8;
  281. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  282. assert(nr_cpus <= MAX_NR_CPUS);
  283. assert(nr_cpus >= 0);
  284. /*
  285. * We dont want to block the signals - that would cause
  286. * child tasks to inherit that and Ctrl-C would not work.
  287. * What we want is for Ctrl-C to work in the exec()-ed
  288. * task, but being ignored by perf stat itself:
  289. */
  290. atexit(sig_atexit);
  291. signal(SIGINT, skip_signal);
  292. signal(SIGALRM, skip_signal);
  293. signal(SIGABRT, skip_signal);
  294. return do_perf_stat(argc, argv);
  295. }