Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 2 | * builtin-record.c |
| 3 | * |
| 4 | * Builtin record command: Record the profile of a workload |
| 5 | * (or a CPU, or a PID) into the perf.data output file - for |
| 6 | * later analysis via perf report. |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 7 | */ |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 8 | #include "builtin.h" |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 9 | |
| 10 | #include "perf.h" |
| 11 | |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 12 | #include "util/util.h" |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 13 | #include "util/parse-options.h" |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 14 | #include "util/parse-events.h" |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 15 | #include "util/string.h" |
Thomas Gleixner | 6eda583 | 2009-05-01 18:29:57 +0200 | [diff] [blame] | 16 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 17 | #include "util/header.h" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 18 | #include "util/event.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 19 | #include "util/debug.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 20 | #include "util/trace-event.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 21 | |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 23 | #include <sched.h> |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 24 | |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 25 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) |
| 26 | #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 27 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 28 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 29 | |
| 30 | static long default_interval = 100000; |
| 31 | |
Ingo Molnar | 3cf165f | 2009-06-02 23:02:59 +0200 | [diff] [blame] | 32 | static int nr_cpus = 0; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 33 | static unsigned int page_size; |
Ingo Molnar | 3cf165f | 2009-06-02 23:02:59 +0200 | [diff] [blame] | 34 | static unsigned int mmap_pages = 128; |
Ingo Molnar | cf1f457 | 2009-06-05 13:27:02 +0200 | [diff] [blame] | 35 | static int freq = 0; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 36 | static int output; |
Ingo Molnar | 23ac9cb | 2009-05-27 09:33:18 +0200 | [diff] [blame] | 37 | static const char *output_name = "perf.data"; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 38 | static int group = 0; |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 39 | static unsigned int realtime_prio = 0; |
Frederic Weisbecker | daac07b | 2009-08-13 10:27:19 +0200 | [diff] [blame] | 40 | static int raw_samples = 0; |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 41 | static int system_wide = 0; |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 42 | static int profile_cpu = -1; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 43 | static pid_t target_pid = -1; |
Chris Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 44 | static pid_t child_pid = -1; |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 45 | static int inherit = 1; |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 46 | static int force = 0; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 47 | static int append_file = 0; |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 48 | static int call_graph = 0; |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 49 | static int inherit_stat = 0; |
| 50 | static int no_samples = 0; |
Anton Blanchard | 4bba828 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 51 | static int sample_address = 0; |
Frederic Weisbecker | d130252 | 2009-09-14 08:57:15 +0200 | [diff] [blame] | 52 | static int multiplex = 0; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 53 | static int multiplex_fd = -1; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 54 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 55 | static long samples; |
| 56 | static struct timeval last_read; |
| 57 | static struct timeval this_read; |
| 58 | |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 59 | static u64 bytes_written; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 60 | |
| 61 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; |
| 62 | |
| 63 | static int nr_poll; |
| 64 | static int nr_cpu; |
| 65 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 66 | static int file_new = 1; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 67 | |
| 68 | struct perf_header *header; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 69 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 70 | struct mmap_data { |
| 71 | int counter; |
| 72 | void *base; |
| 73 | unsigned int mask; |
| 74 | unsigned int prev; |
| 75 | }; |
| 76 | |
| 77 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
| 78 | |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 79 | static unsigned long mmap_read_head(struct mmap_data *md) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 80 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 81 | struct perf_event_mmap_page *pc = md->base; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 82 | long head; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 83 | |
| 84 | head = pc->data_head; |
| 85 | rmb(); |
| 86 | |
| 87 | return head; |
| 88 | } |
| 89 | |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 90 | static void mmap_write_tail(struct mmap_data *md, unsigned long tail) |
| 91 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 92 | struct perf_event_mmap_page *pc = md->base; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * ensure all reads are done before we write the tail out. |
| 96 | */ |
| 97 | /* mb(); */ |
| 98 | pc->data_tail = tail; |
| 99 | } |
| 100 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 101 | static void write_output(void *buf, size_t size) |
| 102 | { |
| 103 | while (size) { |
| 104 | int ret = write(output, buf, size); |
| 105 | |
| 106 | if (ret < 0) |
| 107 | die("failed to write"); |
| 108 | |
| 109 | size -= ret; |
| 110 | buf += ret; |
| 111 | |
| 112 | bytes_written += ret; |
| 113 | } |
| 114 | } |
| 115 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 116 | static void mmap_read(struct mmap_data *md) |
| 117 | { |
| 118 | unsigned int head = mmap_read_head(md); |
| 119 | unsigned int old = md->prev; |
| 120 | unsigned char *data = md->base + page_size; |
| 121 | unsigned long size; |
| 122 | void *buf; |
| 123 | int diff; |
| 124 | |
| 125 | gettimeofday(&this_read, NULL); |
| 126 | |
| 127 | /* |
| 128 | * If we're further behind than half the buffer, there's a chance |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 129 | * the writer will bite our tail and mess up the samples under us. |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 130 | * |
| 131 | * If we somehow ended up ahead of the head, we got messed up. |
| 132 | * |
| 133 | * In either case, truncate and restart at head. |
| 134 | */ |
| 135 | diff = head - old; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 136 | if (diff < 0) { |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 137 | struct timeval iv; |
| 138 | unsigned long msecs; |
| 139 | |
| 140 | timersub(&this_read, &last_read, &iv); |
| 141 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; |
| 142 | |
| 143 | fprintf(stderr, "WARNING: failed to keep up with mmap data." |
| 144 | " Last read %lu msecs ago.\n", msecs); |
| 145 | |
| 146 | /* |
| 147 | * head points to a known good entry, start there. |
| 148 | */ |
| 149 | old = head; |
| 150 | } |
| 151 | |
| 152 | last_read = this_read; |
| 153 | |
| 154 | if (old != head) |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 155 | samples++; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 156 | |
| 157 | size = head - old; |
| 158 | |
| 159 | if ((old & md->mask) + size != (head & md->mask)) { |
| 160 | buf = &data[old & md->mask]; |
| 161 | size = md->mask + 1 - (old & md->mask); |
| 162 | old += size; |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 163 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 164 | write_output(buf, size); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | buf = &data[old & md->mask]; |
| 168 | size = head - old; |
| 169 | old += size; |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 170 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 171 | write_output(buf, size); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 172 | |
| 173 | md->prev = old; |
Peter Zijlstra | 9d91a6f | 2009-06-18 11:40:28 +0200 | [diff] [blame] | 174 | mmap_write_tail(md, old); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static volatile int done = 0; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 178 | static volatile int signr = -1; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 179 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 180 | static void sig_handler(int sig) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 181 | { |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 182 | done = 1; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 183 | signr = sig; |
| 184 | } |
| 185 | |
| 186 | static void sig_atexit(void) |
| 187 | { |
Chris Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 188 | if (child_pid != -1) |
| 189 | kill(child_pid, SIGTERM); |
| 190 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 191 | if (signr == -1) |
| 192 | return; |
| 193 | |
| 194 | signal(signr, SIG_DFL); |
| 195 | kill(getpid(), signr); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 198 | static pid_t pid_synthesize_comm_event(pid_t pid, int full) |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 199 | { |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 200 | struct comm_event comm_ev; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 201 | char filename[PATH_MAX]; |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 202 | char bf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 203 | FILE *fp; |
| 204 | size_t size = 0; |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 205 | DIR *tasks; |
| 206 | struct dirent dirent, *next; |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 207 | pid_t tgid = 0; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 208 | |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 209 | snprintf(filename, sizeof(filename), "/proc/%d/status", pid); |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 210 | |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 211 | fp = fopen(filename, "r"); |
Arnaldo Carvalho de Melo | 39e6dd7 | 2009-08-14 15:26:32 -0300 | [diff] [blame] | 212 | if (fp == NULL) { |
Ingo Molnar | 613d860 | 2009-06-15 08:17:12 +0200 | [diff] [blame] | 213 | /* |
| 214 | * We raced with a task exiting - just return: |
| 215 | */ |
| 216 | if (verbose) |
| 217 | fprintf(stderr, "couldn't open %s\n", filename); |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 218 | return 0; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 219 | } |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 220 | |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 221 | memset(&comm_ev, 0, sizeof(comm_ev)); |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 222 | while (!comm_ev.comm[0] || !comm_ev.pid) { |
| 223 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 224 | goto out_failure; |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 225 | |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 226 | if (memcmp(bf, "Name:", 5) == 0) { |
| 227 | char *name = bf + 5; |
| 228 | while (*name && isspace(*name)) |
| 229 | ++name; |
| 230 | size = strlen(name) - 1; |
| 231 | memcpy(comm_ev.comm, name, size++); |
| 232 | } else if (memcmp(bf, "Tgid:", 5) == 0) { |
| 233 | char *tgids = bf + 5; |
| 234 | while (*tgids && isspace(*tgids)) |
| 235 | ++tgids; |
| 236 | tgid = comm_ev.pid = atoi(tgids); |
| 237 | } |
| 238 | } |
| 239 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 240 | comm_ev.header.type = PERF_RECORD_COMM; |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 241 | size = ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 242 | comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size); |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 243 | |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 244 | if (!full) { |
| 245 | comm_ev.tid = pid; |
| 246 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 247 | write_output(&comm_ev, comm_ev.header.size); |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 248 | goto out_fclose; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 249 | } |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 250 | |
| 251 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); |
| 252 | |
| 253 | tasks = opendir(filename); |
| 254 | while (!readdir_r(tasks, &dirent, &next) && next) { |
| 255 | char *end; |
| 256 | pid = strtol(dirent.d_name, &end, 10); |
| 257 | if (*end) |
| 258 | continue; |
| 259 | |
| 260 | comm_ev.tid = pid; |
| 261 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 262 | write_output(&comm_ev, comm_ev.header.size); |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 263 | } |
| 264 | closedir(tasks); |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 265 | |
| 266 | out_fclose: |
| 267 | fclose(fp); |
| 268 | return tgid; |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 269 | |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 270 | out_failure: |
| 271 | fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n", |
| 272 | filename); |
| 273 | exit(EXIT_FAILURE); |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 274 | } |
| 275 | |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 276 | static void pid_synthesize_mmap_samples(pid_t pid, pid_t tgid) |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 277 | { |
| 278 | char filename[PATH_MAX]; |
| 279 | FILE *fp; |
| 280 | |
| 281 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); |
| 282 | |
| 283 | fp = fopen(filename, "r"); |
| 284 | if (fp == NULL) { |
Ingo Molnar | 613d860 | 2009-06-15 08:17:12 +0200 | [diff] [blame] | 285 | /* |
| 286 | * We raced with a task exiting - just return: |
| 287 | */ |
| 288 | if (verbose) |
| 289 | fprintf(stderr, "couldn't open %s\n", filename); |
| 290 | return; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 291 | } |
| 292 | while (1) { |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 293 | char bf[BUFSIZ], *pbf = bf; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 294 | struct mmap_event mmap_ev = { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 295 | .header = { .type = PERF_RECORD_MMAP }, |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 296 | }; |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 297 | int n; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 298 | size_t size; |
| 299 | if (fgets(bf, sizeof(bf), fp) == NULL) |
| 300 | break; |
| 301 | |
| 302 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ |
Arnaldo Carvalho de Melo | a0055ae | 2009-06-01 17:50:19 -0300 | [diff] [blame] | 303 | n = hex2u64(pbf, &mmap_ev.start); |
| 304 | if (n < 0) |
| 305 | continue; |
| 306 | pbf += n + 1; |
| 307 | n = hex2u64(pbf, &mmap_ev.len); |
| 308 | if (n < 0) |
| 309 | continue; |
| 310 | pbf += n + 3; |
| 311 | if (*pbf == 'x') { /* vm_exec */ |
Johannes Weiner | 76c64c5 | 2009-06-24 21:08:36 +0200 | [diff] [blame] | 312 | char *execname = strchr(bf, '/'); |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 313 | |
Anton Blanchard | 11b5f81 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 314 | /* Catch VDSO */ |
| 315 | if (execname == NULL) |
| 316 | execname = strstr(bf, "[vdso]"); |
| 317 | |
Johannes Weiner | 76c64c5 | 2009-06-24 21:08:36 +0200 | [diff] [blame] | 318 | if (execname == NULL) |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 319 | continue; |
| 320 | |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 321 | size = strlen(execname); |
| 322 | execname[size - 1] = '\0'; /* Remove \n */ |
| 323 | memcpy(mmap_ev.filename, execname, size); |
Paul Mackerras | 9cffa8d | 2009-06-19 22:21:42 +1000 | [diff] [blame] | 324 | size = ALIGN(size, sizeof(u64)); |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 325 | mmap_ev.len -= mmap_ev.start; |
| 326 | mmap_ev.header.size = (sizeof(mmap_ev) - |
| 327 | (sizeof(mmap_ev.filename) - size)); |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 328 | mmap_ev.pid = tgid; |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 329 | mmap_ev.tid = pid; |
| 330 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 331 | write_output(&mmap_ev, mmap_ev.header.size); |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | fclose(fp); |
| 336 | } |
| 337 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 338 | static void synthesize_all(void) |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 339 | { |
| 340 | DIR *proc; |
| 341 | struct dirent dirent, *next; |
| 342 | |
| 343 | proc = opendir("/proc"); |
| 344 | |
| 345 | while (!readdir_r(proc, &dirent, &next) && next) { |
| 346 | char *end; |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 347 | pid_t pid, tgid; |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 348 | |
| 349 | pid = strtol(dirent.d_name, &end, 10); |
| 350 | if (*end) /* only interested in proper numerical dirents */ |
| 351 | continue; |
| 352 | |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 353 | tgid = pid_synthesize_comm_event(pid, 1); |
| 354 | pid_synthesize_mmap_samples(pid, tgid); |
Peter Zijlstra | f70e87d | 2009-06-02 14:13:24 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | closedir(proc); |
| 358 | } |
| 359 | |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 360 | static int group_fd; |
| 361 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 362 | static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 363 | { |
| 364 | struct perf_header_attr *h_attr; |
| 365 | |
| 366 | if (nr < header->attrs) { |
| 367 | h_attr = header->attr[nr]; |
| 368 | } else { |
| 369 | h_attr = perf_header_attr__new(a); |
| 370 | perf_header__add_attr(header, h_attr); |
| 371 | } |
| 372 | |
| 373 | return h_attr; |
| 374 | } |
| 375 | |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 376 | static void create_counter(int counter, int cpu, pid_t pid) |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 377 | { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 378 | struct perf_event_attr *attr = attrs + counter; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 379 | struct perf_header_attr *h_attr; |
| 380 | int track = !counter; /* only the first counter needs these */ |
| 381 | struct { |
| 382 | u64 count; |
| 383 | u64 time_enabled; |
| 384 | u64 time_running; |
| 385 | u64 id; |
| 386 | } read_data; |
| 387 | |
| 388 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
| 389 | PERF_FORMAT_TOTAL_TIME_RUNNING | |
| 390 | PERF_FORMAT_ID; |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 391 | |
Frederic Weisbecker | 3a9f131 | 2009-08-13 10:27:18 +0200 | [diff] [blame] | 392 | attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 393 | |
Ingo Molnar | 1dba15e | 2009-06-05 18:37:22 +0200 | [diff] [blame] | 394 | if (freq) { |
Peter Zijlstra | ea1900e | 2009-06-10 21:45:22 +0200 | [diff] [blame] | 395 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 396 | attr->freq = 1; |
| 397 | attr->sample_freq = freq; |
Ingo Molnar | 1dba15e | 2009-06-05 18:37:22 +0200 | [diff] [blame] | 398 | } |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 399 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 400 | if (no_samples) |
| 401 | attr->sample_freq = 0; |
| 402 | |
| 403 | if (inherit_stat) |
| 404 | attr->inherit_stat = 1; |
| 405 | |
Anton Blanchard | 4bba828 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 406 | if (sample_address) |
| 407 | attr->sample_type |= PERF_SAMPLE_ADDR; |
| 408 | |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 409 | if (call_graph) |
| 410 | attr->sample_type |= PERF_SAMPLE_CALLCHAIN; |
| 411 | |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 412 | if (raw_samples) { |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 413 | attr->sample_type |= PERF_SAMPLE_TIME; |
Frederic Weisbecker | daac07b | 2009-08-13 10:27:19 +0200 | [diff] [blame] | 414 | attr->sample_type |= PERF_SAMPLE_RAW; |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 415 | attr->sample_type |= PERF_SAMPLE_CPU; |
| 416 | } |
Frederic Weisbecker | f413cdb | 2009-08-07 01:25:54 +0200 | [diff] [blame] | 417 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 418 | attr->mmap = track; |
| 419 | attr->comm = track; |
| 420 | attr->inherit = (cpu < 0) && inherit; |
Peter Zijlstra | 4502d77 | 2009-06-10 15:03:06 +0200 | [diff] [blame] | 421 | attr->disabled = 1; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 422 | |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 423 | try_again: |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 424 | fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0); |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 425 | |
| 426 | if (fd[nr_cpu][counter] < 0) { |
| 427 | int err = errno; |
| 428 | |
Pekka Enberg | c10edee | 2009-11-08 18:01:06 +0200 | [diff] [blame] | 429 | if (err == EPERM || err == EACCES) |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 430 | die("Permission error - are you root?\n"); |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 431 | else if (err == ENODEV && profile_cpu != -1) |
| 432 | die("No such device - did you specify an out-of-range profile CPU?\n"); |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 433 | |
| 434 | /* |
| 435 | * If it's cycles then fall back to hrtimer |
| 436 | * based cpu-clock-tick sw counter, which |
| 437 | * is always available even if no PMU support: |
| 438 | */ |
| 439 | if (attr->type == PERF_TYPE_HARDWARE |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 440 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 441 | |
| 442 | if (verbose) |
| 443 | warning(" ... trying to fall back to cpu-clock-ticks\n"); |
| 444 | attr->type = PERF_TYPE_SOFTWARE; |
Peter Zijlstra | f4dbfa8 | 2009-06-11 14:06:28 +0200 | [diff] [blame] | 445 | attr->config = PERF_COUNT_SW_CPU_CLOCK; |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 446 | goto try_again; |
| 447 | } |
Ingo Molnar | 30c806a | 2009-06-07 17:46:24 +0200 | [diff] [blame] | 448 | printf("\n"); |
| 449 | error("perfcounter syscall returned with %d (%s)\n", |
| 450 | fd[nr_cpu][counter], strerror(err)); |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 451 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 452 | exit(-1); |
| 453 | } |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 454 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 455 | h_attr = get_header_attr(attr, counter); |
| 456 | |
| 457 | if (!file_new) { |
| 458 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { |
| 459 | fprintf(stderr, "incompatible append\n"); |
| 460 | exit(-1); |
| 461 | } |
| 462 | } |
| 463 | |
Frederic Weisbecker | 3928ddb | 2009-06-25 22:21:27 +0200 | [diff] [blame] | 464 | if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) { |
| 465 | perror("Unable to read perf file descriptor\n"); |
| 466 | exit(-1); |
| 467 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 468 | |
| 469 | perf_header_attr__add_id(h_attr, read_data.id); |
| 470 | |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 471 | assert(fd[nr_cpu][counter] >= 0); |
| 472 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); |
| 473 | |
| 474 | /* |
| 475 | * First counter acts as the group leader: |
| 476 | */ |
| 477 | if (group && group_fd == -1) |
| 478 | group_fd = fd[nr_cpu][counter]; |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 479 | if (multiplex && multiplex_fd == -1) |
| 480 | multiplex_fd = fd[nr_cpu][counter]; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 481 | |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 482 | if (multiplex && fd[nr_cpu][counter] != multiplex_fd) { |
| 483 | int ret; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 484 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 485 | ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd); |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 486 | assert(ret != -1); |
| 487 | } else { |
| 488 | event_array[nr_poll].fd = fd[nr_cpu][counter]; |
| 489 | event_array[nr_poll].events = POLLIN; |
| 490 | nr_poll++; |
| 491 | |
| 492 | mmap_array[nr_cpu][counter].counter = counter; |
| 493 | mmap_array[nr_cpu][counter].prev = 0; |
| 494 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; |
| 495 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, |
| 496 | PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); |
| 497 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { |
| 498 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); |
| 499 | exit(-1); |
| 500 | } |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 501 | } |
Peter Zijlstra | 4502d77 | 2009-06-10 15:03:06 +0200 | [diff] [blame] | 502 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 503 | ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE); |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static void open_counters(int cpu, pid_t pid) |
| 507 | { |
| 508 | int counter; |
| 509 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 510 | group_fd = -1; |
Ingo Molnar | f250c030 | 2009-06-05 13:18:41 +0200 | [diff] [blame] | 511 | for (counter = 0; counter < nr_counters; counter++) |
| 512 | create_counter(counter, cpu, pid); |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 513 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 514 | nr_cpu++; |
| 515 | } |
| 516 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 517 | static void atexit_header(void) |
| 518 | { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 519 | header->data_size += bytes_written; |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 520 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 521 | perf_header__write(header, output); |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 524 | static int __cmd_record(int argc, const char **argv) |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 525 | { |
| 526 | int i, counter; |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 527 | struct stat st; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 528 | pid_t pid = 0; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 529 | int flags; |
| 530 | int ret; |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 531 | unsigned long waking = 0; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 532 | |
| 533 | page_size = sysconf(_SC_PAGE_SIZE); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 534 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 535 | assert(nr_cpus <= MAX_NR_CPUS); |
| 536 | assert(nr_cpus >= 0); |
| 537 | |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 538 | atexit(sig_atexit); |
| 539 | signal(SIGCHLD, sig_handler); |
| 540 | signal(SIGINT, sig_handler); |
| 541 | |
Pierre Habouzit | 266e0e2 | 2009-08-07 14:16:01 +0200 | [diff] [blame] | 542 | if (!stat(output_name, &st) && st.st_size) { |
| 543 | if (!force && !append_file) { |
| 544 | fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n", |
| 545 | output_name); |
| 546 | exit(-1); |
| 547 | } |
| 548 | } else { |
| 549 | append_file = 0; |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 552 | flags = O_CREAT|O_RDWR; |
| 553 | if (append_file) |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 554 | file_new = 0; |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 555 | else |
| 556 | flags |= O_TRUNC; |
| 557 | |
| 558 | output = open(output_name, flags, S_IRUSR|S_IWUSR); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 559 | if (output < 0) { |
| 560 | perror("failed to create output file"); |
| 561 | exit(-1); |
| 562 | } |
| 563 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 564 | if (!file_new) |
| 565 | header = perf_header__read(output); |
| 566 | else |
| 567 | header = perf_header__new(); |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 568 | |
Frederic Weisbecker | 9df37dd | 2009-08-17 23:07:50 +0200 | [diff] [blame] | 569 | |
| 570 | if (raw_samples) { |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 571 | read_tracing_data(attrs, nr_counters); |
Frederic Weisbecker | 9df37dd | 2009-08-17 23:07:50 +0200 | [diff] [blame] | 572 | } else { |
| 573 | for (i = 0; i < nr_counters; i++) { |
| 574 | if (attrs[i].sample_type & PERF_SAMPLE_RAW) { |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 575 | read_tracing_data(attrs, nr_counters); |
Frederic Weisbecker | 9df37dd | 2009-08-17 23:07:50 +0200 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | } |
| 579 | } |
Peter Zijlstra | f597055 | 2009-06-18 23:22:55 +0200 | [diff] [blame] | 580 | atexit(atexit_header); |
| 581 | |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 582 | if (!system_wide) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 583 | pid = target_pid; |
| 584 | if (pid == -1) |
| 585 | pid = getpid(); |
| 586 | |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 587 | open_counters(profile_cpu, pid); |
| 588 | } else { |
| 589 | if (profile_cpu != -1) { |
| 590 | open_counters(profile_cpu, target_pid); |
| 591 | } else { |
| 592 | for (i = 0; i < nr_cpus; i++) |
| 593 | open_counters(i, target_pid); |
| 594 | } |
| 595 | } |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 596 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 597 | if (file_new) |
| 598 | perf_header__write(header, output); |
| 599 | |
| 600 | if (!system_wide) { |
Arnaldo Carvalho de Melo | 2a8083f | 2009-08-11 16:22:00 -0300 | [diff] [blame] | 601 | pid_t tgid = pid_synthesize_comm_event(pid, 0); |
| 602 | pid_synthesize_mmap_samples(pid, tgid); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 603 | } else |
| 604 | synthesize_all(); |
| 605 | |
Mike Galbraith | ef65b2a | 2009-05-27 10:10:51 +0200 | [diff] [blame] | 606 | if (target_pid == -1 && argc) { |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 607 | pid = fork(); |
| 608 | if (pid < 0) |
| 609 | perror("failed to fork"); |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 610 | |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 611 | if (!pid) { |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 612 | if (execvp(argv[0], (char **)argv)) { |
Arnaldo Carvalho de Melo | 1a853e3 | 2009-05-14 22:50:46 -0300 | [diff] [blame] | 613 | perror(argv[0]); |
| 614 | exit(-1); |
| 615 | } |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 616 | } |
Chris Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 617 | |
| 618 | child_pid = pid; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | if (realtime_prio) { |
| 622 | struct sched_param param; |
| 623 | |
| 624 | param.sched_priority = realtime_prio; |
| 625 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
| 626 | printf("Could not set realtime priority.\n"); |
| 627 | exit(-1); |
| 628 | } |
| 629 | } |
| 630 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 631 | for (;;) { |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 632 | int hits = samples; |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 633 | |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 634 | for (i = 0; i < nr_cpu; i++) { |
Ingo Molnar | ea57c4f | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 635 | for (counter = 0; counter < nr_counters; counter++) { |
| 636 | if (mmap_array[i][counter].base) |
| 637 | mmap_read(&mmap_array[i][counter]); |
| 638 | } |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 641 | if (hits == samples) { |
| 642 | if (done) |
| 643 | break; |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 644 | ret = poll(event_array, nr_poll, -1); |
| 645 | waking++; |
| 646 | } |
| 647 | |
| 648 | if (done) { |
| 649 | for (i = 0; i < nr_cpu; i++) { |
| 650 | for (counter = 0; counter < nr_counters; counter++) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 651 | ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE); |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 652 | } |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 653 | } |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 654 | } |
| 655 | |
Peter Zijlstra | 8b41266 | 2009-09-17 19:59:05 +0200 | [diff] [blame] | 656 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
| 657 | |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 658 | /* |
| 659 | * Approximate RIP event size: 24 bytes. |
| 660 | */ |
| 661 | fprintf(stderr, |
Ingo Molnar | 2debbc8 | 2009-06-05 14:29:10 +0200 | [diff] [blame] | 662 | "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", |
Ingo Molnar | 021e9f4 | 2009-06-03 19:27:19 +0200 | [diff] [blame] | 663 | (double)bytes_written / 1024.0 / 1024.0, |
| 664 | output_name, |
| 665 | bytes_written / 24); |
Ingo Molnar | addc278 | 2009-06-02 23:43:11 +0200 | [diff] [blame] | 666 | |
Peter Zijlstra | de9ac07 | 2009-04-08 15:01:31 +0200 | [diff] [blame] | 667 | return 0; |
| 668 | } |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 669 | |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 670 | static const char * const record_usage[] = { |
Mike Galbraith | 9e09675 | 2009-05-28 16:25:34 +0200 | [diff] [blame] | 671 | "perf record [<options>] [<command>]", |
| 672 | "perf record [<options>] -- <command> [<options>]", |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 673 | NULL |
| 674 | }; |
| 675 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 676 | static const struct option options[] = { |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 677 | OPT_CALLBACK('e', "event", NULL, "event", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 678 | "event selector. use 'perf list' to list available events", |
| 679 | parse_events), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 680 | OPT_INTEGER('p', "pid", &target_pid, |
| 681 | "record events on existing pid"), |
| 682 | OPT_INTEGER('r', "realtime", &realtime_prio, |
| 683 | "collect data with this RT SCHED_FIFO priority"), |
Frederic Weisbecker | daac07b | 2009-08-13 10:27:19 +0200 | [diff] [blame] | 684 | OPT_BOOLEAN('R', "raw-samples", &raw_samples, |
| 685 | "collect raw sample records from all opened counters"), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 686 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
| 687 | "system-wide collection from all CPUs"), |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 688 | OPT_BOOLEAN('A', "append", &append_file, |
| 689 | "append to the output file to do incremental profiling"), |
Jens Axboe | 0a5ac84 | 2009-08-12 11:18:01 +0200 | [diff] [blame] | 690 | OPT_INTEGER('C', "profile_cpu", &profile_cpu, |
| 691 | "CPU to profile on"), |
Peter Zijlstra | 97124d5 | 2009-06-02 15:52:24 +0200 | [diff] [blame] | 692 | OPT_BOOLEAN('f', "force", &force, |
| 693 | "overwrite existing data file"), |
Peter Zijlstra | e61078a | 2009-06-03 11:24:33 +0200 | [diff] [blame] | 694 | OPT_LONG('c', "count", &default_interval, |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 695 | "event period to sample"), |
| 696 | OPT_STRING('o', "output", &output_name, "file", |
| 697 | "output file name"), |
| 698 | OPT_BOOLEAN('i', "inherit", &inherit, |
| 699 | "child tasks inherit counters"), |
Ingo Molnar | cf1f457 | 2009-06-05 13:27:02 +0200 | [diff] [blame] | 700 | OPT_INTEGER('F', "freq", &freq, |
| 701 | "profile at this frequency"), |
Ingo Molnar | abaff32 | 2009-06-02 22:59:57 +0200 | [diff] [blame] | 702 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
| 703 | "number of mmap data pages"), |
Ingo Molnar | 3efa1cc9 | 2009-06-14 15:04:15 +0200 | [diff] [blame] | 704 | OPT_BOOLEAN('g', "call-graph", &call_graph, |
| 705 | "do call-graph (stack chain/backtrace) recording"), |
Ingo Molnar | 3da297a | 2009-06-07 17:39:02 +0200 | [diff] [blame] | 706 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 707 | "be more verbose (show counter open errors, etc)"), |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 708 | OPT_BOOLEAN('s', "stat", &inherit_stat, |
| 709 | "per thread counts"), |
Anton Blanchard | 4bba828 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 710 | OPT_BOOLEAN('d', "data", &sample_address, |
| 711 | "Sample addresses"), |
Peter Zijlstra | 649c48a | 2009-06-24 21:12:48 +0200 | [diff] [blame] | 712 | OPT_BOOLEAN('n', "no-samples", &no_samples, |
| 713 | "don't sample"), |
Frederic Weisbecker | d130252 | 2009-09-14 08:57:15 +0200 | [diff] [blame] | 714 | OPT_BOOLEAN('M', "multiplex", &multiplex, |
| 715 | "multiplex counter output in a single channel"), |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 716 | OPT_END() |
| 717 | }; |
| 718 | |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 719 | int cmd_record(int argc, const char **argv, const char *prefix __used) |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 720 | { |
| 721 | int counter; |
| 722 | |
Anton Blanchard | a054123 | 2009-07-22 23:04:12 +1000 | [diff] [blame] | 723 | argc = parse_options(argc, argv, options, record_usage, |
| 724 | PARSE_OPT_STOP_AT_NON_OPTION); |
Mike Galbraith | ef65b2a | 2009-05-27 10:10:51 +0200 | [diff] [blame] | 725 | if (!argc && target_pid == -1 && !system_wide) |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 726 | usage_with_options(record_usage, options); |
| 727 | |
Peter Zijlstra | bbd36e5 | 2009-06-11 23:11:50 +0200 | [diff] [blame] | 728 | if (!nr_counters) { |
| 729 | nr_counters = 1; |
| 730 | attrs[0].type = PERF_TYPE_HARDWARE; |
| 731 | attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; |
| 732 | } |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 733 | |
| 734 | for (counter = 0; counter < nr_counters; counter++) { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 735 | if (attrs[counter].sample_period) |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 736 | continue; |
| 737 | |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 738 | attrs[counter].sample_period = default_interval; |
Ingo Molnar | 0e9b20b | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | return __cmd_record(argc, argv); |
| 742 | } |