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