| 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 |  | 
| Gui Jianfeng | c214909 | 2010-06-16 13:21:44 +0800 | [diff] [blame] | 10 | /* Skip "." and ".." directories */ | 
|  | 11 | static int filter(const struct dirent *dir) | 
|  | 12 | { | 
|  | 13 | if (dir->d_name[0] == '.') | 
|  | 14 | return 0; | 
|  | 15 | else | 
|  | 16 | return 1; | 
|  | 17 | } | 
|  | 18 |  | 
| Arnaldo Carvalho de Melo | 5c98d46 | 2011-01-03 17:53:33 -0200 | [diff] [blame] | 19 | struct thread_map *thread_map__new_by_pid(pid_t pid) | 
| Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 20 | { | 
| Arnaldo Carvalho de Melo | 5c98d46 | 2011-01-03 17:53:33 -0200 | [diff] [blame] | 21 | struct thread_map *threads; | 
| Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 22 | char name[256]; | 
|  | 23 | int items; | 
|  | 24 | struct dirent **namelist = NULL; | 
| Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 25 | int i; | 
|  | 26 |  | 
|  | 27 | sprintf(name, "/proc/%d/task", pid); | 
| Gui Jianfeng | c214909 | 2010-06-16 13:21:44 +0800 | [diff] [blame] | 28 | items = scandir(name, &namelist, filter, NULL); | 
| Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 29 | if (items <= 0) | 
| Arnaldo Carvalho de Melo | 5c98d46 | 2011-01-03 17:53:33 -0200 | [diff] [blame] | 30 | return NULL; | 
|  | 31 |  | 
|  | 32 | threads = malloc(sizeof(*threads) + sizeof(pid_t) * items); | 
|  | 33 | if (threads != NULL) { | 
|  | 34 | for (i = 0; i < items; i++) | 
|  | 35 | threads->map[i] = atoi(namelist[i]->d_name); | 
|  | 36 | threads->nr = items; | 
| Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 39 | for (i=0; i<items; i++) | 
|  | 40 | free(namelist[i]); | 
|  | 41 | free(namelist); | 
|  | 42 |  | 
| Arnaldo Carvalho de Melo | 5c98d46 | 2011-01-03 17:53:33 -0200 | [diff] [blame] | 43 | return threads; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | struct thread_map *thread_map__new_by_tid(pid_t tid) | 
|  | 47 | { | 
|  | 48 | struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t)); | 
|  | 49 |  | 
|  | 50 | if (threads != NULL) { | 
|  | 51 | threads->map[0] = tid; | 
|  | 52 | threads->nr	= 1; | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | return threads; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | struct thread_map *thread_map__new(pid_t pid, pid_t tid) | 
|  | 59 | { | 
|  | 60 | if (pid != -1) | 
|  | 61 | return thread_map__new_by_pid(pid); | 
|  | 62 | return thread_map__new_by_tid(tid); | 
| Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 65 | static struct thread *thread__new(pid_t pid) | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 66 | { | 
| Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 67 | struct thread *self = zalloc(sizeof(*self)); | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 68 |  | 
|  | 69 | if (self != NULL) { | 
| Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 70 | map_groups__init(&self->mg); | 
|  | 71 | self->pid = pid; | 
| Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 72 | self->comm = malloc(32); | 
|  | 73 | if (self->comm) | 
|  | 74 | snprintf(self->comm, 32, ":%d", self->pid); | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
|  | 77 | return self; | 
|  | 78 | } | 
|  | 79 |  | 
| Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 80 | void thread__delete(struct thread *self) | 
|  | 81 | { | 
|  | 82 | map_groups__exit(&self->mg); | 
|  | 83 | free(self->comm); | 
|  | 84 | free(self); | 
|  | 85 | } | 
|  | 86 |  | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 87 | int thread__set_comm(struct thread *self, const char *comm) | 
|  | 88 | { | 
| David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 89 | int err; | 
|  | 90 |  | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 91 | if (self->comm) | 
|  | 92 | free(self->comm); | 
|  | 93 | self->comm = strdup(comm); | 
| David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 94 | err = self->comm == NULL ? -ENOMEM : 0; | 
|  | 95 | if (!err) { | 
|  | 96 | self->comm_set = true; | 
|  | 97 | map_groups__flush(&self->mg); | 
|  | 98 | } | 
|  | 99 | return err; | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 102 | int thread__comm_len(struct thread *self) | 
|  | 103 | { | 
|  | 104 | if (!self->comm_len) { | 
|  | 105 | if (!self->comm) | 
|  | 106 | return 0; | 
|  | 107 | self->comm_len = strlen(self->comm); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | return self->comm_len; | 
|  | 111 | } | 
|  | 112 |  | 
| Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 113 | static size_t thread__fprintf(struct thread *self, FILE *fp) | 
|  | 114 | { | 
| Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 115 | 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] | 116 | map_groups__fprintf(&self->mg, verbose, fp); | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 119 | struct thread *perf_session__findnew(struct perf_session *self, pid_t pid) | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 120 | { | 
| Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 121 | struct rb_node **p = &self->threads.rb_node; | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 122 | struct rb_node *parent = NULL; | 
|  | 123 | struct thread *th; | 
|  | 124 |  | 
|  | 125 | /* | 
|  | 126 | * Font-end cache - PID lookups come in blocks, | 
|  | 127 | * so most of the time we dont have to look up | 
|  | 128 | * the full rbtree: | 
|  | 129 | */ | 
| Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 130 | if (self->last_match && self->last_match->pid == pid) | 
|  | 131 | return self->last_match; | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 132 |  | 
|  | 133 | while (*p != NULL) { | 
|  | 134 | parent = *p; | 
|  | 135 | th = rb_entry(parent, struct thread, rb_node); | 
|  | 136 |  | 
|  | 137 | if (th->pid == pid) { | 
| Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 138 | self->last_match = th; | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 139 | return th; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | if (pid < th->pid) | 
|  | 143 | p = &(*p)->rb_left; | 
|  | 144 | else | 
|  | 145 | p = &(*p)->rb_right; | 
|  | 146 | } | 
|  | 147 |  | 
| Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 148 | th = thread__new(pid); | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 149 | if (th != NULL) { | 
|  | 150 | rb_link_node(&th->rb_node, parent, p); | 
| Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 151 | rb_insert_color(&th->rb_node, &self->threads); | 
|  | 152 | self->last_match = th; | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
|  | 155 | return th; | 
|  | 156 | } | 
|  | 157 |  | 
| Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 158 | void thread__insert_map(struct thread *self, struct map *map) | 
|  | 159 | { | 
| Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 160 | map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); | 
| Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 161 | map_groups__insert(&self->mg, map); | 
| Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 164 | int thread__fork(struct thread *self, struct thread *parent) | 
|  | 165 | { | 
| Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 166 | int i; | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 167 |  | 
| Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 168 | if (parent->comm_set) { | 
|  | 169 | if (self->comm) | 
|  | 170 | free(self->comm); | 
|  | 171 | self->comm = strdup(parent->comm); | 
|  | 172 | if (!self->comm) | 
|  | 173 | return -ENOMEM; | 
|  | 174 | self->comm_set = true; | 
|  | 175 | } | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 176 |  | 
| Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 177 | for (i = 0; i < MAP__NR_TYPES; ++i) | 
| Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 178 | if (map_groups__clone(&self->mg, &parent->mg, i) < 0) | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 179 | return -ENOMEM; | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 180 | return 0; | 
|  | 181 | } | 
|  | 182 |  | 
| Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 183 | size_t perf_session__fprintf(struct perf_session *self, FILE *fp) | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 184 | { | 
|  | 185 | size_t ret = 0; | 
|  | 186 | struct rb_node *nd; | 
|  | 187 |  | 
| Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 188 | for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) { | 
| Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 189 | struct thread *pos = rb_entry(nd, struct thread, rb_node); | 
|  | 190 |  | 
|  | 191 | ret += thread__fprintf(pos, fp); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | return ret; | 
|  | 195 | } |