Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 1 | #include "../perf.h" |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 5 | #include "session.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 6 | #include "thread.h" |
| 7 | #include "util.h" |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 8 | #include "debug.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 9 | |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 10 | static struct thread *thread__new(pid_t pid) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 11 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 12 | struct thread *self = zalloc(sizeof(*self)); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 13 | |
| 14 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 15 | map_groups__init(&self->mg); |
| 16 | self->pid = pid; |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 17 | self->comm = malloc(32); |
| 18 | if (self->comm) |
| 19 | snprintf(self->comm, 32, ":%d", self->pid); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | return self; |
| 23 | } |
| 24 | |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 25 | void thread__delete(struct thread *self) |
| 26 | { |
| 27 | map_groups__exit(&self->mg); |
| 28 | free(self->comm); |
| 29 | free(self); |
| 30 | } |
| 31 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 32 | int thread__set_comm(struct thread *self, const char *comm) |
| 33 | { |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 34 | int err; |
| 35 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 36 | if (self->comm) |
| 37 | free(self->comm); |
| 38 | self->comm = strdup(comm); |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 39 | err = self->comm == NULL ? -ENOMEM : 0; |
| 40 | if (!err) { |
| 41 | self->comm_set = true; |
| 42 | map_groups__flush(&self->mg); |
| 43 | } |
| 44 | return err; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 47 | int thread__comm_len(struct thread *self) |
| 48 | { |
| 49 | if (!self->comm_len) { |
| 50 | if (!self->comm) |
| 51 | return 0; |
| 52 | self->comm_len = strlen(self->comm); |
| 53 | } |
| 54 | |
| 55 | return self->comm_len; |
| 56 | } |
| 57 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 58 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 59 | { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 60 | return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) + |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 61 | map_groups__fprintf(&self->mg, verbose, fp); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 64 | struct thread *perf_session__findnew(struct perf_session *self, pid_t pid) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 65 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 66 | struct rb_node **p = &self->threads.rb_node; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 67 | struct rb_node *parent = NULL; |
| 68 | struct thread *th; |
| 69 | |
| 70 | /* |
| 71 | * Font-end cache - PID lookups come in blocks, |
| 72 | * so most of the time we dont have to look up |
| 73 | * the full rbtree: |
| 74 | */ |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 75 | if (self->last_match && self->last_match->pid == pid) |
| 76 | return self->last_match; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 77 | |
| 78 | while (*p != NULL) { |
| 79 | parent = *p; |
| 80 | th = rb_entry(parent, struct thread, rb_node); |
| 81 | |
| 82 | if (th->pid == pid) { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 83 | self->last_match = th; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 84 | return th; |
| 85 | } |
| 86 | |
| 87 | if (pid < th->pid) |
| 88 | p = &(*p)->rb_left; |
| 89 | else |
| 90 | p = &(*p)->rb_right; |
| 91 | } |
| 92 | |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 93 | th = thread__new(pid); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 94 | if (th != NULL) { |
| 95 | rb_link_node(&th->rb_node, parent, p); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 96 | rb_insert_color(&th->rb_node, &self->threads); |
| 97 | self->last_match = th; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return th; |
| 101 | } |
| 102 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 103 | void thread__insert_map(struct thread *self, struct map *map) |
| 104 | { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 105 | map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 106 | map_groups__insert(&self->mg, map); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 107 | } |
| 108 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 109 | int thread__fork(struct thread *self, struct thread *parent) |
| 110 | { |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 111 | int i; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 112 | |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 113 | if (parent->comm_set) { |
| 114 | if (self->comm) |
| 115 | free(self->comm); |
| 116 | self->comm = strdup(parent->comm); |
| 117 | if (!self->comm) |
| 118 | return -ENOMEM; |
| 119 | self->comm_set = true; |
| 120 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 121 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 122 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 123 | if (map_groups__clone(&self->mg, &parent->mg, i) < 0) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 124 | return -ENOMEM; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 128 | size_t perf_session__fprintf(struct perf_session *self, FILE *fp) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 129 | { |
| 130 | size_t ret = 0; |
| 131 | struct rb_node *nd; |
| 132 | |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 133 | for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) { |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 134 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 135 | |
| 136 | ret += thread__fprintf(pos, fp); |
| 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |