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