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