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