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