builtin-record.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #include "util/util.h"
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <limits.h>
  10. #include <getopt.h>
  11. #include <assert.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <ctype.h>
  16. #include <time.h>
  17. #include <sched.h>
  18. #include <pthread.h>
  19. #include <sys/syscall.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/poll.h>
  22. #include <sys/prctl.h>
  23. #include <sys/wait.h>
  24. #include <sys/uio.h>
  25. #include <sys/mman.h>
  26. #include <linux/unistd.h>
  27. #include <linux/types.h>
  28. #include "../../include/linux/perf_counter.h"
  29. #include "perf.h"
  30. static int nr_counters = 0;
  31. static __u64 event_id[MAX_COUNTERS] = { };
  32. static int default_interval = 100000;
  33. static int event_count[MAX_COUNTERS];
  34. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  35. static int nr_cpus = 0;
  36. static unsigned int page_size;
  37. static unsigned int mmap_pages = 16;
  38. static int output;
  39. static char *output_name = "output.perf";
  40. static int group = 0;
  41. static unsigned int realtime_prio = 0;
  42. const unsigned int default_count[] = {
  43. 1000000,
  44. 1000000,
  45. 10000,
  46. 10000,
  47. 1000000,
  48. 10000,
  49. };
  50. struct event_symbol {
  51. __u64 event;
  52. char *symbol;
  53. };
  54. static struct event_symbol event_symbols[] = {
  55. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cpu-cycles", },
  56. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cycles", },
  57. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS), "instructions", },
  58. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES), "cache-references", },
  59. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES), "cache-misses", },
  60. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branch-instructions", },
  61. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branches", },
  62. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_MISSES), "branch-misses", },
  63. {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BUS_CYCLES), "bus-cycles", },
  64. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK), "cpu-clock", },
  65. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK), "task-clock", },
  66. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "page-faults", },
  67. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "faults", },
  68. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MIN), "minor-faults", },
  69. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MAJ), "major-faults", },
  70. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "context-switches", },
  71. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "cs", },
  72. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "cpu-migrations", },
  73. {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "migrations", },
  74. };
  75. /*
  76. * Each event can have multiple symbolic names.
  77. * Symbolic names are (almost) exactly matched.
  78. */
  79. static __u64 match_event_symbols(char *str)
  80. {
  81. __u64 config, id;
  82. int type;
  83. unsigned int i;
  84. if (sscanf(str, "r%llx", &config) == 1)
  85. return config | PERF_COUNTER_RAW_MASK;
  86. if (sscanf(str, "%d:%llu", &type, &id) == 2)
  87. return EID(type, id);
  88. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  89. if (!strncmp(str, event_symbols[i].symbol,
  90. strlen(event_symbols[i].symbol)))
  91. return event_symbols[i].event;
  92. }
  93. return ~0ULL;
  94. }
  95. static int parse_events(char *str)
  96. {
  97. __u64 config;
  98. again:
  99. if (nr_counters == MAX_COUNTERS)
  100. return -1;
  101. config = match_event_symbols(str);
  102. if (config == ~0ULL)
  103. return -1;
  104. event_id[nr_counters] = config;
  105. nr_counters++;
  106. str = strstr(str, ",");
  107. if (str) {
  108. str++;
  109. goto again;
  110. }
  111. return 0;
  112. }
  113. #define __PERF_COUNTER_FIELD(config, name) \
  114. ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
  115. #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
  116. #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
  117. #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
  118. #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
  119. static void display_events_help(void)
  120. {
  121. unsigned int i;
  122. __u64 e;
  123. printf(
  124. " -e EVENT --event=EVENT # symbolic-name abbreviations");
  125. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  126. int type, id;
  127. e = event_symbols[i].event;
  128. type = PERF_COUNTER_TYPE(e);
  129. id = PERF_COUNTER_ID(e);
  130. printf("\n %d:%d: %-20s",
  131. type, id, event_symbols[i].symbol);
  132. }
  133. printf("\n"
  134. " rNNN: raw PMU events (eventsel+umask)\n\n");
  135. }
  136. static void display_help(void)
  137. {
  138. printf(
  139. "Usage: perf-record [<options>]\n"
  140. "perf-record Options (up to %d event types can be specified at once):\n\n",
  141. MAX_COUNTERS);
  142. display_events_help();
  143. printf(
  144. " -c CNT --count=CNT # event period to sample\n"
  145. " -m pages --mmap_pages=<pages> # number of mmap data pages\n"
  146. " -o file --output=<file> # output file\n"
  147. " -r prio --realtime=<prio> # use RT prio\n"
  148. );
  149. exit(0);
  150. }
  151. static void process_options(int argc, char *argv[])
  152. {
  153. int error = 0, counter;
  154. for (;;) {
  155. int option_index = 0;
  156. /** Options for getopt */
  157. static struct option long_options[] = {
  158. {"count", required_argument, NULL, 'c'},
  159. {"event", required_argument, NULL, 'e'},
  160. {"mmap_pages", required_argument, NULL, 'm'},
  161. {"output", required_argument, NULL, 'o'},
  162. {"realtime", required_argument, NULL, 'r'},
  163. {NULL, 0, NULL, 0 }
  164. };
  165. int c = getopt_long(argc, argv, "+:c:e:m:o:r:",
  166. long_options, &option_index);
  167. if (c == -1)
  168. break;
  169. switch (c) {
  170. case 'c': default_interval = atoi(optarg); break;
  171. case 'e': error = parse_events(optarg); break;
  172. case 'm': mmap_pages = atoi(optarg); break;
  173. case 'o': output_name = strdup(optarg); break;
  174. case 'r': realtime_prio = atoi(optarg); break;
  175. default: error = 1; break;
  176. }
  177. }
  178. if (error)
  179. display_help();
  180. if (!nr_counters) {
  181. nr_counters = 1;
  182. event_id[0] = 0;
  183. }
  184. for (counter = 0; counter < nr_counters; counter++) {
  185. if (event_count[counter])
  186. continue;
  187. event_count[counter] = default_interval;
  188. }
  189. }
  190. struct mmap_data {
  191. int counter;
  192. void *base;
  193. unsigned int mask;
  194. unsigned int prev;
  195. };
  196. static unsigned int mmap_read_head(struct mmap_data *md)
  197. {
  198. struct perf_counter_mmap_page *pc = md->base;
  199. int head;
  200. head = pc->data_head;
  201. rmb();
  202. return head;
  203. }
  204. static long events;
  205. static struct timeval last_read, this_read;
  206. static void mmap_read(struct mmap_data *md)
  207. {
  208. unsigned int head = mmap_read_head(md);
  209. unsigned int old = md->prev;
  210. unsigned char *data = md->base + page_size;
  211. unsigned long size;
  212. void *buf;
  213. int diff;
  214. gettimeofday(&this_read, NULL);
  215. /*
  216. * If we're further behind than half the buffer, there's a chance
  217. * the writer will bite our tail and screw up the events under us.
  218. *
  219. * If we somehow ended up ahead of the head, we got messed up.
  220. *
  221. * In either case, truncate and restart at head.
  222. */
  223. diff = head - old;
  224. if (diff > md->mask / 2 || diff < 0) {
  225. struct timeval iv;
  226. unsigned long msecs;
  227. timersub(&this_read, &last_read, &iv);
  228. msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
  229. fprintf(stderr, "WARNING: failed to keep up with mmap data."
  230. " Last read %lu msecs ago.\n", msecs);
  231. /*
  232. * head points to a known good entry, start there.
  233. */
  234. old = head;
  235. }
  236. last_read = this_read;
  237. if (old != head)
  238. events++;
  239. size = head - old;
  240. if ((old & md->mask) + size != (head & md->mask)) {
  241. buf = &data[old & md->mask];
  242. size = md->mask + 1 - (old & md->mask);
  243. old += size;
  244. while (size) {
  245. int ret = write(output, buf, size);
  246. if (ret < 0) {
  247. perror("failed to write");
  248. exit(-1);
  249. }
  250. size -= ret;
  251. buf += ret;
  252. }
  253. }
  254. buf = &data[old & md->mask];
  255. size = head - old;
  256. old += size;
  257. while (size) {
  258. int ret = write(output, buf, size);
  259. if (ret < 0) {
  260. perror("failed to write");
  261. exit(-1);
  262. }
  263. size -= ret;
  264. buf += ret;
  265. }
  266. md->prev = old;
  267. }
  268. static volatile int done = 0;
  269. static void sigchld_handler(int sig)
  270. {
  271. if (sig == SIGCHLD)
  272. done = 1;
  273. }
  274. int cmd_record(int argc, char **argv)
  275. {
  276. struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
  277. struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  278. struct perf_counter_hw_event hw_event;
  279. int i, counter, group_fd, nr_poll = 0;
  280. pid_t pid;
  281. int ret;
  282. page_size = sysconf(_SC_PAGE_SIZE);
  283. process_options(argc, argv);
  284. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  285. assert(nr_cpus <= MAX_NR_CPUS);
  286. assert(nr_cpus >= 0);
  287. output = open(output_name, O_CREAT|O_RDWR, S_IRWXU);
  288. if (output < 0) {
  289. perror("failed to create output file");
  290. exit(-1);
  291. }
  292. argc -= optind;
  293. argv += optind;
  294. for (i = 0; i < nr_cpus; i++) {
  295. group_fd = -1;
  296. for (counter = 0; counter < nr_counters; counter++) {
  297. memset(&hw_event, 0, sizeof(hw_event));
  298. hw_event.config = event_id[counter];
  299. hw_event.irq_period = event_count[counter];
  300. hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID;
  301. hw_event.nmi = 1;
  302. hw_event.mmap = 1;
  303. hw_event.comm = 1;
  304. fd[i][counter] = sys_perf_counter_open(&hw_event, -1, i, group_fd, 0);
  305. if (fd[i][counter] < 0) {
  306. int err = errno;
  307. printf("kerneltop error: syscall returned with %d (%s)\n",
  308. fd[i][counter], strerror(err));
  309. if (err == EPERM)
  310. printf("Are you root?\n");
  311. exit(-1);
  312. }
  313. assert(fd[i][counter] >= 0);
  314. fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
  315. /*
  316. * First counter acts as the group leader:
  317. */
  318. if (group && group_fd == -1)
  319. group_fd = fd[i][counter];
  320. event_array[nr_poll].fd = fd[i][counter];
  321. event_array[nr_poll].events = POLLIN;
  322. nr_poll++;
  323. mmap_array[i][counter].counter = counter;
  324. mmap_array[i][counter].prev = 0;
  325. mmap_array[i][counter].mask = mmap_pages*page_size - 1;
  326. mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
  327. PROT_READ, MAP_SHARED, fd[i][counter], 0);
  328. if (mmap_array[i][counter].base == MAP_FAILED) {
  329. printf("kerneltop error: failed to mmap with %d (%s)\n",
  330. errno, strerror(errno));
  331. exit(-1);
  332. }
  333. }
  334. }
  335. signal(SIGCHLD, sigchld_handler);
  336. pid = fork();
  337. if (pid < 0)
  338. perror("failed to fork");
  339. if (!pid) {
  340. if (execvp(argv[0], argv)) {
  341. perror(argv[0]);
  342. exit(-1);
  343. }
  344. }
  345. if (realtime_prio) {
  346. struct sched_param param;
  347. param.sched_priority = realtime_prio;
  348. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  349. printf("Could not set realtime priority.\n");
  350. exit(-1);
  351. }
  352. }
  353. /*
  354. * TODO: store the current /proc/$/maps information somewhere
  355. */
  356. while (!done) {
  357. int hits = events;
  358. for (i = 0; i < nr_cpus; i++) {
  359. for (counter = 0; counter < nr_counters; counter++)
  360. mmap_read(&mmap_array[i][counter]);
  361. }
  362. if (hits == events)
  363. ret = poll(event_array, nr_poll, 100);
  364. }
  365. return 0;
  366. }