diff options
Diffstat (limited to 'tools/perf/builtin-trace.c')
| -rw-r--r-- | tools/perf/builtin-trace.c | 418 |
1 files changed, 258 insertions, 160 deletions
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 0c5e4f72f2ba..abb914aa7be6 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c | |||
| @@ -5,66 +5,73 @@ | |||
| 5 | #include "util/symbol.h" | 5 | #include "util/symbol.h" |
| 6 | #include "util/thread.h" | 6 | #include "util/thread.h" |
| 7 | #include "util/header.h" | 7 | #include "util/header.h" |
| 8 | #include "util/exec_cmd.h" | ||
| 9 | #include "util/trace-event.h" | ||
| 8 | 10 | ||
| 9 | #include "util/parse-options.h" | 11 | static char const *script_name; |
| 12 | static char const *generate_script_lang; | ||
| 10 | 13 | ||
| 11 | #include "perf.h" | 14 | static int default_start_script(const char *script __attribute((unused))) |
| 12 | #include "util/debug.h" | 15 | { |
| 16 | return 0; | ||
| 17 | } | ||
| 13 | 18 | ||
| 14 | #include "util/trace-event.h" | 19 | static int default_stop_script(void) |
| 20 | { | ||
| 21 | return 0; | ||
| 22 | } | ||
| 15 | 23 | ||
| 16 | static char const *input_name = "perf.data"; | 24 | static int default_generate_script(const char *outfile __attribute ((unused))) |
| 17 | static int input; | 25 | { |
| 18 | static unsigned long page_size; | 26 | return 0; |
| 19 | static unsigned long mmap_window = 32; | 27 | } |
| 20 | 28 | ||
| 21 | static unsigned long total = 0; | 29 | static struct scripting_ops default_scripting_ops = { |
| 22 | static unsigned long total_comm = 0; | 30 | .start_script = default_start_script, |
| 31 | .stop_script = default_stop_script, | ||
| 32 | .process_event = print_event, | ||
| 33 | .generate_script = default_generate_script, | ||
| 34 | }; | ||
| 35 | |||
| 36 | static struct scripting_ops *scripting_ops; | ||
| 23 | 37 | ||
| 24 | static struct rb_root threads; | 38 | static void setup_scripting(void) |
| 25 | static struct thread *last_match; | 39 | { |
| 40 | /* make sure PERF_EXEC_PATH is set for scripts */ | ||
| 41 | perf_set_argv_exec_path(perf_exec_path()); | ||
| 26 | 42 | ||
| 27 | static struct perf_header *header; | 43 | setup_perl_scripting(); |
| 28 | static u64 sample_type; | ||
| 29 | 44 | ||
| 45 | scripting_ops = &default_scripting_ops; | ||
| 46 | } | ||
| 30 | 47 | ||
| 31 | static int | 48 | static int cleanup_scripting(void) |
| 32 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 33 | { | 49 | { |
| 34 | struct thread *thread; | 50 | return scripting_ops->stop_script(); |
| 51 | } | ||
| 35 | 52 | ||
| 36 | thread = threads__findnew(event->comm.pid, &threads, &last_match); | 53 | #include "util/parse-options.h" |
| 37 | 54 | ||
| 38 | dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", | 55 | #include "perf.h" |
| 39 | (void *)(offset + head), | 56 | #include "util/debug.h" |
| 40 | (void *)(long)(event->header.size), | ||
| 41 | event->comm.comm, event->comm.pid); | ||
| 42 | 57 | ||
| 43 | if (thread == NULL || | 58 | #include "util/trace-event.h" |
| 44 | thread__set_comm(thread, event->comm.comm)) { | 59 | #include "util/data_map.h" |
| 45 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); | 60 | #include "util/exec_cmd.h" |
| 46 | return -1; | ||
| 47 | } | ||
| 48 | total_comm++; | ||
| 49 | 61 | ||
| 50 | return 0; | 62 | static char const *input_name = "perf.data"; |
| 51 | } | ||
| 52 | 63 | ||
| 53 | static int | 64 | static struct perf_header *header; |
| 54 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) | 65 | static u64 sample_type; |
| 66 | |||
| 67 | static int process_sample_event(event_t *event) | ||
| 55 | { | 68 | { |
| 56 | char level; | ||
| 57 | int show = 0; | ||
| 58 | struct dso *dso = NULL; | ||
| 59 | struct thread *thread; | ||
| 60 | u64 ip = event->ip.ip; | 69 | u64 ip = event->ip.ip; |
| 61 | u64 timestamp = -1; | 70 | u64 timestamp = -1; |
| 62 | u32 cpu = -1; | 71 | u32 cpu = -1; |
| 63 | u64 period = 1; | 72 | u64 period = 1; |
| 64 | void *more_data = event->ip.__more_data; | 73 | void *more_data = event->ip.__more_data; |
| 65 | int cpumode; | 74 | struct thread *thread = threads__findnew(event->ip.pid); |
| 66 | |||
| 67 | thread = threads__findnew(event->ip.pid, &threads, &last_match); | ||
| 68 | 75 | ||
| 69 | if (sample_type & PERF_SAMPLE_TIME) { | 76 | if (sample_type & PERF_SAMPLE_TIME) { |
| 70 | timestamp = *(u64 *)more_data; | 77 | timestamp = *(u64 *)more_data; |
| @@ -82,45 +89,19 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |||
| 82 | more_data += sizeof(u64); | 89 | more_data += sizeof(u64); |
| 83 | } | 90 | } |
| 84 | 91 | ||
| 85 | dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", | 92 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
| 86 | (void *)(offset + head), | ||
| 87 | (void *)(long)(event->header.size), | ||
| 88 | event->header.misc, | 93 | event->header.misc, |
| 89 | event->ip.pid, event->ip.tid, | 94 | event->ip.pid, event->ip.tid, |
| 90 | (void *)(long)ip, | 95 | (void *)(long)ip, |
| 91 | (long long)period); | 96 | (long long)period); |
| 92 | 97 | ||
| 93 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | ||
| 94 | |||
| 95 | if (thread == NULL) { | 98 | if (thread == NULL) { |
| 96 | eprintf("problem processing %d event, skipping it.\n", | 99 | pr_debug("problem processing %d event, skipping it.\n", |
| 97 | event->header.type); | 100 | event->header.type); |
| 98 | return -1; | 101 | return -1; |
| 99 | } | 102 | } |
| 100 | 103 | ||
| 101 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | 104 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 102 | |||
| 103 | if (cpumode == PERF_RECORD_MISC_KERNEL) { | ||
| 104 | show = SHOW_KERNEL; | ||
| 105 | level = 'k'; | ||
| 106 | |||
| 107 | dso = kernel_dso; | ||
| 108 | |||
| 109 | dump_printf(" ...... dso: %s\n", dso->name); | ||
| 110 | |||
| 111 | } else if (cpumode == PERF_RECORD_MISC_USER) { | ||
| 112 | |||
| 113 | show = SHOW_USER; | ||
| 114 | level = '.'; | ||
| 115 | |||
| 116 | } else { | ||
| 117 | show = SHOW_HV; | ||
| 118 | level = 'H'; | ||
| 119 | |||
| 120 | dso = hypervisor_dso; | ||
| 121 | |||
| 122 | dump_printf(" ...... dso: [hypervisor]\n"); | ||
| 123 | } | ||
| 124 | 105 | ||
| 125 | if (sample_type & PERF_SAMPLE_RAW) { | 106 | if (sample_type & PERF_SAMPLE_RAW) { |
| 126 | struct { | 107 | struct { |
| @@ -133,128 +114,189 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head) | |||
| 133 | * field, although it should be the same than this perf | 114 | * field, although it should be the same than this perf |
| 134 | * event pid | 115 | * event pid |
| 135 | */ | 116 | */ |
| 136 | print_event(cpu, raw->data, raw->size, timestamp, thread->comm); | 117 | scripting_ops->process_event(cpu, raw->data, raw->size, |
| 118 | timestamp, thread->comm); | ||
| 137 | } | 119 | } |
| 138 | total += period; | 120 | event__stats.total += period; |
| 139 | 121 | ||
| 140 | return 0; | 122 | return 0; |
| 141 | } | 123 | } |
| 142 | 124 | ||
| 143 | static int | 125 | static int sample_type_check(u64 type) |
| 144 | process_event(event_t *event, unsigned long offset, unsigned long head) | ||
| 145 | { | 126 | { |
| 146 | trace_event(event); | 127 | sample_type = type; |
| 147 | |||
| 148 | switch (event->header.type) { | ||
| 149 | case PERF_RECORD_MMAP ... PERF_RECORD_LOST: | ||
| 150 | return 0; | ||
| 151 | |||
| 152 | case PERF_RECORD_COMM: | ||
| 153 | return process_comm_event(event, offset, head); | ||
| 154 | |||
| 155 | case PERF_RECORD_EXIT ... PERF_RECORD_READ: | ||
| 156 | return 0; | ||
| 157 | |||
| 158 | case PERF_RECORD_SAMPLE: | ||
| 159 | return process_sample_event(event, offset, head); | ||
| 160 | 128 | ||
| 161 | case PERF_RECORD_MAX: | 129 | if (!(sample_type & PERF_SAMPLE_RAW)) { |
| 162 | default: | 130 | fprintf(stderr, |
| 131 | "No trace sample to read. Did you call perf record " | ||
| 132 | "without -R?"); | ||
| 163 | return -1; | 133 | return -1; |
| 164 | } | 134 | } |
| 165 | 135 | ||
| 166 | return 0; | 136 | return 0; |
| 167 | } | 137 | } |
| 168 | 138 | ||
| 139 | static struct perf_file_handler file_handler = { | ||
| 140 | .process_sample_event = process_sample_event, | ||
| 141 | .process_comm_event = event__process_comm, | ||
| 142 | .sample_type_check = sample_type_check, | ||
| 143 | }; | ||
| 144 | |||
| 169 | static int __cmd_trace(void) | 145 | static int __cmd_trace(void) |
| 170 | { | 146 | { |
| 171 | int ret, rc = EXIT_FAILURE; | 147 | register_idle_thread(); |
| 172 | unsigned long offset = 0; | 148 | register_perf_file_handler(&file_handler); |
| 173 | unsigned long head = 0; | ||
| 174 | struct stat perf_stat; | ||
| 175 | event_t *event; | ||
| 176 | uint32_t size; | ||
| 177 | char *buf; | ||
| 178 | |||
| 179 | trace_report(); | ||
| 180 | register_idle_thread(&threads, &last_match); | ||
| 181 | |||
| 182 | input = open(input_name, O_RDONLY); | ||
| 183 | if (input < 0) { | ||
| 184 | perror("failed to open file"); | ||
| 185 | exit(-1); | ||
| 186 | } | ||
| 187 | 149 | ||
| 188 | ret = fstat(input, &perf_stat); | 150 | return mmap_dispatch_perf_file(&header, input_name, |
| 189 | if (ret < 0) { | 151 | 0, 0, &event__cwdlen, &event__cwd); |
| 190 | perror("failed to stat file"); | 152 | } |
| 191 | exit(-1); | ||
| 192 | } | ||
| 193 | 153 | ||
| 194 | if (!perf_stat.st_size) { | 154 | struct script_spec { |
| 195 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | 155 | struct list_head node; |
| 196 | exit(0); | 156 | struct scripting_ops *ops; |
| 197 | } | 157 | char spec[0]; |
| 198 | header = perf_header__read(input); | 158 | }; |
| 199 | head = header->data_offset; | ||
| 200 | sample_type = perf_header__sample_type(header); | ||
| 201 | 159 | ||
| 202 | if (!(sample_type & PERF_SAMPLE_RAW)) | 160 | LIST_HEAD(script_specs); |
| 203 | die("No trace sample to read. Did you call perf record " | ||
| 204 | "without -R?"); | ||
| 205 | 161 | ||
| 206 | if (load_kernel() < 0) { | 162 | static struct script_spec *script_spec__new(const char *spec, |
| 207 | perror("failed to load kernel symbols"); | 163 | struct scripting_ops *ops) |
| 208 | return EXIT_FAILURE; | 164 | { |
| 209 | } | 165 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); |
| 210 | 166 | ||
| 211 | remap: | 167 | if (s != NULL) { |
| 212 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | 168 | strcpy(s->spec, spec); |
| 213 | MAP_SHARED, input, offset); | 169 | s->ops = ops; |
| 214 | if (buf == MAP_FAILED) { | ||
| 215 | perror("failed to mmap file"); | ||
| 216 | exit(-1); | ||
| 217 | } | 170 | } |
| 218 | 171 | ||
| 219 | more: | 172 | return s; |
| 220 | event = (event_t *)(buf + head); | 173 | } |
| 221 | 174 | ||
| 222 | if (head + event->header.size >= page_size * mmap_window) { | 175 | static void script_spec__delete(struct script_spec *s) |
| 223 | unsigned long shift = page_size * (head / page_size); | 176 | { |
| 224 | int res; | 177 | free(s->spec); |
| 178 | free(s); | ||
| 179 | } | ||
| 225 | 180 | ||
| 226 | res = munmap(buf, page_size * mmap_window); | 181 | static void script_spec__add(struct script_spec *s) |
| 227 | assert(res == 0); | 182 | { |
| 183 | list_add_tail(&s->node, &script_specs); | ||
| 184 | } | ||
| 228 | 185 | ||
| 229 | offset += shift; | 186 | static struct script_spec *script_spec__find(const char *spec) |
| 230 | head -= shift; | 187 | { |
| 231 | goto remap; | 188 | struct script_spec *s; |
| 232 | } | ||
| 233 | 189 | ||
| 234 | size = event->header.size; | 190 | list_for_each_entry(s, &script_specs, node) |
| 191 | if (strcasecmp(s->spec, spec) == 0) | ||
| 192 | return s; | ||
| 193 | return NULL; | ||
| 194 | } | ||
| 235 | 195 | ||
| 236 | if (!size || process_event(event, offset, head) < 0) { | 196 | static struct script_spec *script_spec__findnew(const char *spec, |
| 197 | struct scripting_ops *ops) | ||
| 198 | { | ||
| 199 | struct script_spec *s = script_spec__find(spec); | ||
| 237 | 200 | ||
| 238 | /* | 201 | if (s) |
| 239 | * assume we lost track of the stream, check alignment, and | 202 | return s; |
| 240 | * increment a single u64 in the hope to catch on again 'soon'. | ||
| 241 | */ | ||
| 242 | 203 | ||
| 243 | if (unlikely(head & 7)) | 204 | s = script_spec__new(spec, ops); |
| 244 | head &= ~7ULL; | 205 | if (!s) |
| 206 | goto out_delete_spec; | ||
| 245 | 207 | ||
| 246 | size = 8; | 208 | script_spec__add(s); |
| 247 | } | 209 | |
| 210 | return s; | ||
| 248 | 211 | ||
| 249 | head += size; | 212 | out_delete_spec: |
| 213 | script_spec__delete(s); | ||
| 214 | |||
| 215 | return NULL; | ||
| 216 | } | ||
| 250 | 217 | ||
| 251 | if (offset + head < (unsigned long)perf_stat.st_size) | 218 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
| 252 | goto more; | 219 | { |
| 220 | struct script_spec *s; | ||
| 221 | |||
| 222 | s = script_spec__find(spec); | ||
| 223 | if (s) | ||
| 224 | return -1; | ||
| 253 | 225 | ||
| 254 | rc = EXIT_SUCCESS; | 226 | s = script_spec__findnew(spec, ops); |
| 255 | close(input); | 227 | if (!s) |
| 228 | return -1; | ||
| 229 | |||
| 230 | return 0; | ||
| 231 | } | ||
| 232 | |||
| 233 | static struct scripting_ops *script_spec__lookup(const char *spec) | ||
| 234 | { | ||
| 235 | struct script_spec *s = script_spec__find(spec); | ||
| 236 | if (!s) | ||
| 237 | return NULL; | ||
| 256 | 238 | ||
| 257 | return rc; | 239 | return s->ops; |
| 240 | } | ||
| 241 | |||
| 242 | static void list_available_languages(void) | ||
| 243 | { | ||
| 244 | struct script_spec *s; | ||
| 245 | |||
| 246 | fprintf(stderr, "\n"); | ||
| 247 | fprintf(stderr, "Scripting language extensions (used in " | ||
| 248 | "perf trace -s [spec:]script.[spec]):\n\n"); | ||
| 249 | |||
| 250 | list_for_each_entry(s, &script_specs, node) | ||
| 251 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); | ||
| 252 | |||
| 253 | fprintf(stderr, "\n"); | ||
| 254 | } | ||
| 255 | |||
| 256 | static int parse_scriptname(const struct option *opt __used, | ||
| 257 | const char *str, int unset __used) | ||
| 258 | { | ||
| 259 | char spec[PATH_MAX]; | ||
| 260 | const char *script, *ext; | ||
| 261 | int len; | ||
| 262 | |||
| 263 | if (strcmp(str, "list") == 0) { | ||
| 264 | list_available_languages(); | ||
| 265 | return 0; | ||
| 266 | } | ||
| 267 | |||
| 268 | script = strchr(str, ':'); | ||
| 269 | if (script) { | ||
| 270 | len = script - str; | ||
| 271 | if (len >= PATH_MAX) { | ||
| 272 | fprintf(stderr, "invalid language specifier"); | ||
| 273 | return -1; | ||
| 274 | } | ||
| 275 | strncpy(spec, str, len); | ||
| 276 | spec[len] = '\0'; | ||
| 277 | scripting_ops = script_spec__lookup(spec); | ||
| 278 | if (!scripting_ops) { | ||
| 279 | fprintf(stderr, "invalid language specifier"); | ||
| 280 | return -1; | ||
| 281 | } | ||
| 282 | script++; | ||
| 283 | } else { | ||
| 284 | script = str; | ||
| 285 | ext = strchr(script, '.'); | ||
| 286 | if (!ext) { | ||
| 287 | fprintf(stderr, "invalid script extension"); | ||
| 288 | return -1; | ||
| 289 | } | ||
| 290 | scripting_ops = script_spec__lookup(++ext); | ||
| 291 | if (!scripting_ops) { | ||
| 292 | fprintf(stderr, "invalid script extension"); | ||
| 293 | return -1; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | script_name = strdup(script); | ||
| 298 | |||
| 299 | return 0; | ||
| 258 | } | 300 | } |
| 259 | 301 | ||
| 260 | static const char * const annotate_usage[] = { | 302 | static const char * const annotate_usage[] = { |
| @@ -267,13 +309,24 @@ static const struct option options[] = { | |||
| 267 | "dump raw trace in ASCII"), | 309 | "dump raw trace in ASCII"), |
| 268 | OPT_BOOLEAN('v', "verbose", &verbose, | 310 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 269 | "be more verbose (show symbol address, etc)"), | 311 | "be more verbose (show symbol address, etc)"), |
| 312 | OPT_BOOLEAN('l', "latency", &latency_format, | ||
| 313 | "show latency attributes (irqs/preemption disabled, etc)"), | ||
| 314 | OPT_CALLBACK('s', "script", NULL, "name", | ||
| 315 | "script file name (lang:script name, script name, or *)", | ||
| 316 | parse_scriptname), | ||
| 317 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | ||
| 318 | "generate perf-trace.xx script in specified language"), | ||
| 319 | |||
| 270 | OPT_END() | 320 | OPT_END() |
| 271 | }; | 321 | }; |
| 272 | 322 | ||
| 273 | int cmd_trace(int argc, const char **argv, const char *prefix __used) | 323 | int cmd_trace(int argc, const char **argv, const char *prefix __used) |
| 274 | { | 324 | { |
| 275 | symbol__init(); | 325 | int err; |
| 276 | page_size = getpagesize(); | 326 | |
| 327 | symbol__init(0); | ||
| 328 | |||
| 329 | setup_scripting(); | ||
| 277 | 330 | ||
| 278 | argc = parse_options(argc, argv, options, annotate_usage, 0); | 331 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 279 | if (argc) { | 332 | if (argc) { |
| @@ -287,5 +340,50 @@ int cmd_trace(int argc, const char **argv, const char *prefix __used) | |||
| 287 | 340 | ||
| 288 | setup_pager(); | 341 | setup_pager(); |
| 289 | 342 | ||
| 290 | return __cmd_trace(); | 343 | if (generate_script_lang) { |
| 344 | struct stat perf_stat; | ||
| 345 | |||
| 346 | int input = open(input_name, O_RDONLY); | ||
| 347 | if (input < 0) { | ||
| 348 | perror("failed to open file"); | ||
| 349 | exit(-1); | ||
| 350 | } | ||
| 351 | |||
| 352 | err = fstat(input, &perf_stat); | ||
| 353 | if (err < 0) { | ||
| 354 | perror("failed to stat file"); | ||
| 355 | exit(-1); | ||
| 356 | } | ||
| 357 | |||
| 358 | if (!perf_stat.st_size) { | ||
| 359 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | ||
| 360 | exit(0); | ||
| 361 | } | ||
| 362 | |||
| 363 | scripting_ops = script_spec__lookup(generate_script_lang); | ||
| 364 | if (!scripting_ops) { | ||
| 365 | fprintf(stderr, "invalid language specifier"); | ||
| 366 | return -1; | ||
| 367 | } | ||
| 368 | |||
| 369 | header = perf_header__new(); | ||
| 370 | if (header == NULL) | ||
| 371 | return -1; | ||
| 372 | |||
| 373 | perf_header__read(header, input); | ||
| 374 | err = scripting_ops->generate_script("perf-trace"); | ||
| 375 | goto out; | ||
| 376 | } | ||
| 377 | |||
| 378 | if (script_name) { | ||
| 379 | err = scripting_ops->start_script(script_name); | ||
| 380 | if (err) | ||
| 381 | goto out; | ||
| 382 | } | ||
| 383 | |||
| 384 | err = __cmd_trace(); | ||
| 385 | |||
| 386 | cleanup_scripting(); | ||
| 387 | out: | ||
| 388 | return err; | ||
| 291 | } | 389 | } |
