builtin-record.c 10 KB

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