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