builtin-record.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "perf.h"
  2. #include "builtin.h"
  3. #include "util/util.h"
  4. #include "util/parse-options.h"
  5. #include "util/parse-events.h"
  6. #include "util/string.h"
  7. #include <sched.h>
  8. #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  9. #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  10. static int default_interval = 100000;
  11. static int event_count[MAX_COUNTERS];
  12. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  13. static int nr_cpus = 0;
  14. static unsigned int page_size;
  15. static unsigned int mmap_pages = 16;
  16. static int output;
  17. static const char *output_name = "perf.data";
  18. static int group = 0;
  19. static unsigned int realtime_prio = 0;
  20. static int system_wide = 0;
  21. static pid_t target_pid = -1;
  22. static int inherit = 1;
  23. static int nmi = 1;
  24. const unsigned int default_count[] = {
  25. 1000000,
  26. 1000000,
  27. 10000,
  28. 10000,
  29. 1000000,
  30. 10000,
  31. };
  32. struct mmap_data {
  33. int counter;
  34. void *base;
  35. unsigned int mask;
  36. unsigned int prev;
  37. };
  38. static unsigned int mmap_read_head(struct mmap_data *md)
  39. {
  40. struct perf_counter_mmap_page *pc = md->base;
  41. int head;
  42. head = pc->data_head;
  43. rmb();
  44. return head;
  45. }
  46. static long events;
  47. static struct timeval last_read, this_read;
  48. static void mmap_read(struct mmap_data *md)
  49. {
  50. unsigned int head = mmap_read_head(md);
  51. unsigned int old = md->prev;
  52. unsigned char *data = md->base + page_size;
  53. unsigned long size;
  54. void *buf;
  55. int diff;
  56. gettimeofday(&this_read, NULL);
  57. /*
  58. * If we're further behind than half the buffer, there's a chance
  59. * the writer will bite our tail and screw up the events under us.
  60. *
  61. * If we somehow ended up ahead of the head, we got messed up.
  62. *
  63. * In either case, truncate and restart at head.
  64. */
  65. diff = head - old;
  66. if (diff > md->mask / 2 || diff < 0) {
  67. struct timeval iv;
  68. unsigned long msecs;
  69. timersub(&this_read, &last_read, &iv);
  70. msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
  71. fprintf(stderr, "WARNING: failed to keep up with mmap data."
  72. " Last read %lu msecs ago.\n", msecs);
  73. /*
  74. * head points to a known good entry, start there.
  75. */
  76. old = head;
  77. }
  78. last_read = this_read;
  79. if (old != head)
  80. events++;
  81. size = head - old;
  82. if ((old & md->mask) + size != (head & md->mask)) {
  83. buf = &data[old & md->mask];
  84. size = md->mask + 1 - (old & md->mask);
  85. old += size;
  86. while (size) {
  87. int ret = write(output, buf, size);
  88. if (ret < 0) {
  89. perror("failed to write");
  90. exit(-1);
  91. }
  92. size -= ret;
  93. buf += ret;
  94. }
  95. }
  96. buf = &data[old & md->mask];
  97. size = head - old;
  98. old += size;
  99. while (size) {
  100. int ret = write(output, buf, size);
  101. if (ret < 0) {
  102. perror("failed to write");
  103. exit(-1);
  104. }
  105. size -= ret;
  106. buf += ret;
  107. }
  108. md->prev = old;
  109. }
  110. static volatile int done = 0;
  111. static void sig_handler(int sig)
  112. {
  113. done = 1;
  114. }
  115. static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
  116. static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  117. static int nr_poll;
  118. static int nr_cpu;
  119. struct mmap_event {
  120. struct perf_event_header header;
  121. __u32 pid;
  122. __u32 tid;
  123. __u64 start;
  124. __u64 len;
  125. __u64 pgoff;
  126. char filename[PATH_MAX];
  127. };
  128. struct comm_event {
  129. struct perf_event_header header;
  130. __u32 pid;
  131. __u32 tid;
  132. char comm[16];
  133. };
  134. static void pid_synthesize_comm_event(pid_t pid, int full)
  135. {
  136. struct comm_event comm_ev;
  137. char filename[PATH_MAX];
  138. char bf[BUFSIZ];
  139. int fd, ret;
  140. size_t size;
  141. char *field, *sep;
  142. DIR *tasks;
  143. struct dirent dirent, *next;
  144. snprintf(filename, sizeof(filename), "/proc/%d/stat", pid);
  145. fd = open(filename, O_RDONLY);
  146. if (fd < 0) {
  147. fprintf(stderr, "couldn't open %s\n", filename);
  148. exit(EXIT_FAILURE);
  149. }
  150. if (read(fd, bf, sizeof(bf)) < 0) {
  151. fprintf(stderr, "couldn't read %s\n", filename);
  152. exit(EXIT_FAILURE);
  153. }
  154. close(fd);
  155. /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */
  156. memset(&comm_ev, 0, sizeof(comm_ev));
  157. field = strchr(bf, '(');
  158. if (field == NULL)
  159. goto out_failure;
  160. sep = strchr(++field, ')');
  161. if (sep == NULL)
  162. goto out_failure;
  163. size = sep - field;
  164. memcpy(comm_ev.comm, field, size++);
  165. comm_ev.pid = pid;
  166. comm_ev.header.type = PERF_EVENT_COMM;
  167. size = ALIGN(size, sizeof(uint64_t));
  168. comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size);
  169. if (!full) {
  170. comm_ev.tid = pid;
  171. ret = write(output, &comm_ev, comm_ev.header.size);
  172. if (ret < 0) {
  173. perror("failed to write");
  174. exit(-1);
  175. }
  176. return;
  177. }
  178. snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
  179. tasks = opendir(filename);
  180. while (!readdir_r(tasks, &dirent, &next) && next) {
  181. char *end;
  182. pid = strtol(dirent.d_name, &end, 10);
  183. if (*end)
  184. continue;
  185. comm_ev.tid = pid;
  186. ret = write(output, &comm_ev, comm_ev.header.size);
  187. if (ret < 0) {
  188. perror("failed to write");
  189. exit(-1);
  190. }
  191. }
  192. closedir(tasks);
  193. return;
  194. out_failure:
  195. fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n",
  196. filename);
  197. exit(EXIT_FAILURE);
  198. }
  199. static void pid_synthesize_mmap_events(pid_t pid)
  200. {
  201. char filename[PATH_MAX];
  202. FILE *fp;
  203. snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
  204. fp = fopen(filename, "r");
  205. if (fp == NULL) {
  206. fprintf(stderr, "couldn't open %s\n", filename);
  207. exit(EXIT_FAILURE);
  208. }
  209. while (1) {
  210. char bf[BUFSIZ], *pbf = bf;
  211. struct mmap_event mmap_ev = {
  212. .header.type = PERF_EVENT_MMAP,
  213. };
  214. int n;
  215. size_t size;
  216. if (fgets(bf, sizeof(bf), fp) == NULL)
  217. break;
  218. /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
  219. n = hex2u64(pbf, &mmap_ev.start);
  220. if (n < 0)
  221. continue;
  222. pbf += n + 1;
  223. n = hex2u64(pbf, &mmap_ev.len);
  224. if (n < 0)
  225. continue;
  226. pbf += n + 3;
  227. if (*pbf == 'x') { /* vm_exec */
  228. char *execname = strrchr(bf, ' ');
  229. if (execname == NULL || execname[1] != '/')
  230. continue;
  231. execname += 1;
  232. size = strlen(execname);
  233. execname[size - 1] = '\0'; /* Remove \n */
  234. memcpy(mmap_ev.filename, execname, size);
  235. size = ALIGN(size, sizeof(uint64_t));
  236. mmap_ev.len -= mmap_ev.start;
  237. mmap_ev.header.size = (sizeof(mmap_ev) -
  238. (sizeof(mmap_ev.filename) - size));
  239. mmap_ev.pid = pid;
  240. mmap_ev.tid = pid;
  241. if (write(output, &mmap_ev, mmap_ev.header.size) < 0) {
  242. perror("failed to write");
  243. exit(-1);
  244. }
  245. }
  246. }
  247. fclose(fp);
  248. }
  249. static void synthesize_events(void)
  250. {
  251. DIR *proc;
  252. struct dirent dirent, *next;
  253. proc = opendir("/proc");
  254. while (!readdir_r(proc, &dirent, &next) && next) {
  255. char *end;
  256. pid_t pid;
  257. pid = strtol(dirent.d_name, &end, 10);
  258. if (*end) /* only interested in proper numerical dirents */
  259. continue;
  260. pid_synthesize_comm_event(pid, 1);
  261. pid_synthesize_mmap_events(pid);
  262. }
  263. closedir(proc);
  264. }
  265. static void open_counters(int cpu, pid_t pid)
  266. {
  267. struct perf_counter_hw_event hw_event;
  268. int counter, group_fd;
  269. int track = 1;
  270. if (pid > 0) {
  271. pid_synthesize_comm_event(pid, 0);
  272. pid_synthesize_mmap_events(pid);
  273. }
  274. group_fd = -1;
  275. for (counter = 0; counter < nr_counters; counter++) {
  276. memset(&hw_event, 0, sizeof(hw_event));
  277. hw_event.config = event_id[counter];
  278. hw_event.irq_period = event_count[counter];
  279. hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID;
  280. hw_event.nmi = nmi;
  281. hw_event.mmap = track;
  282. hw_event.comm = track;
  283. hw_event.inherit = (cpu < 0) && inherit;
  284. track = 0; // only the first counter needs these
  285. fd[nr_cpu][counter] =
  286. sys_perf_counter_open(&hw_event, pid, cpu, group_fd, 0);
  287. if (fd[nr_cpu][counter] < 0) {
  288. int err = errno;
  289. printf("kerneltop error: syscall returned with %d (%s)\n",
  290. fd[nr_cpu][counter], strerror(err));
  291. if (err == EPERM)
  292. printf("Are you root?\n");
  293. exit(-1);
  294. }
  295. assert(fd[nr_cpu][counter] >= 0);
  296. fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK);
  297. /*
  298. * First counter acts as the group leader:
  299. */
  300. if (group && group_fd == -1)
  301. group_fd = fd[nr_cpu][counter];
  302. event_array[nr_poll].fd = fd[nr_cpu][counter];
  303. event_array[nr_poll].events = POLLIN;
  304. nr_poll++;
  305. mmap_array[nr_cpu][counter].counter = counter;
  306. mmap_array[nr_cpu][counter].prev = 0;
  307. mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1;
  308. mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
  309. PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0);
  310. if (mmap_array[nr_cpu][counter].base == MAP_FAILED) {
  311. printf("kerneltop error: failed to mmap with %d (%s)\n",
  312. errno, strerror(errno));
  313. exit(-1);
  314. }
  315. }
  316. nr_cpu++;
  317. }
  318. static int __cmd_record(int argc, const char **argv)
  319. {
  320. int i, counter;
  321. pid_t pid;
  322. int ret;
  323. page_size = sysconf(_SC_PAGE_SIZE);
  324. nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
  325. assert(nr_cpus <= MAX_NR_CPUS);
  326. assert(nr_cpus >= 0);
  327. output = open(output_name, O_CREAT|O_EXCL|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR);
  328. if (output < 0) {
  329. perror("failed to create output file");
  330. exit(-1);
  331. }
  332. if (!system_wide) {
  333. open_counters(-1, target_pid != -1 ? target_pid : 0);
  334. } else for (i = 0; i < nr_cpus; i++)
  335. open_counters(i, target_pid);
  336. signal(SIGCHLD, sig_handler);
  337. signal(SIGINT, sig_handler);
  338. if (target_pid == -1 && argc) {
  339. pid = fork();
  340. if (pid < 0)
  341. perror("failed to fork");
  342. if (!pid) {
  343. if (execvp(argv[0], (char **)argv)) {
  344. perror(argv[0]);
  345. exit(-1);
  346. }
  347. }
  348. }
  349. if (realtime_prio) {
  350. struct sched_param param;
  351. param.sched_priority = realtime_prio;
  352. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  353. printf("Could not set realtime priority.\n");
  354. exit(-1);
  355. }
  356. }
  357. if (system_wide)
  358. synthesize_events();
  359. while (!done) {
  360. int hits = events;
  361. for (i = 0; i < nr_cpu; i++) {
  362. for (counter = 0; counter < nr_counters; counter++)
  363. mmap_read(&mmap_array[i][counter]);
  364. }
  365. if (hits == events)
  366. ret = poll(event_array, nr_poll, 100);
  367. }
  368. return 0;
  369. }
  370. static const char * const record_usage[] = {
  371. "perf record [<options>] [<command>]",
  372. "perf record [<options>] -- <command> [<options>]",
  373. NULL
  374. };
  375. static char events_help_msg[EVENTS_HELP_MAX];
  376. static const struct option options[] = {
  377. OPT_CALLBACK('e', "event", NULL, "event",
  378. events_help_msg, parse_events),
  379. OPT_INTEGER('c', "count", &default_interval,
  380. "event period to sample"),
  381. OPT_INTEGER('m', "mmap-pages", &mmap_pages,
  382. "number of mmap data pages"),
  383. OPT_STRING('o', "output", &output_name, "file",
  384. "output file name"),
  385. OPT_BOOLEAN('i', "inherit", &inherit,
  386. "child tasks inherit counters"),
  387. OPT_INTEGER('p', "pid", &target_pid,
  388. "record events on existing pid"),
  389. OPT_INTEGER('r', "realtime", &realtime_prio,
  390. "collect data with this RT SCHED_FIFO priority"),
  391. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  392. "system-wide collection from all CPUs"),
  393. OPT_END()
  394. };
  395. int cmd_record(int argc, const char **argv, const char *prefix)
  396. {
  397. int counter;
  398. create_events_help(events_help_msg);
  399. argc = parse_options(argc, argv, options, record_usage, 0);
  400. if (!argc && target_pid == -1 && !system_wide)
  401. usage_with_options(record_usage, options);
  402. if (!nr_counters) {
  403. nr_counters = 1;
  404. event_id[0] = 0;
  405. }
  406. for (counter = 0; counter < nr_counters; counter++) {
  407. if (event_count[counter])
  408. continue;
  409. event_count[counter] = default_interval;
  410. }
  411. return __cmd_record(argc, argv);
  412. }