1 #include "../perf.h" 2 #include <stdlib.h> 3 #include <stdio.h> 4 #include <string.h> 5 #include "session.h" 6 #include "thread.h" 7 #include "util.h" 8 #include "debug.h" 9 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 19 int find_all_tid(int pid, pid_t ** all_tid) 20 { 21 char name[256]; 22 int items; 23 struct dirent **namelist = NULL; 24 int ret = 0; 25 int i; 26 27 sprintf(name, "/proc/%d/task", pid); 28 items = scandir(name, &namelist, filter, NULL); 29 if (items <= 0) 30 return -ENOENT; 31 *all_tid = malloc(sizeof(pid_t) * items); 32 if (!*all_tid) { 33 ret = -ENOMEM; 34 goto failure; 35 } 36 37 for (i = 0; i < items; i++) 38 (*all_tid)[i] = atoi(namelist[i]->d_name); 39 40 ret = items; 41 42 failure: 43 for (i=0; i<items; i++) 44 free(namelist[i]); 45 free(namelist); 46 47 return ret; 48 } 49 50 static struct thread *thread__new(pid_t pid) 51 { 52 struct thread *self = zalloc(sizeof(*self)); 53 54 if (self != NULL) { 55 map_groups__init(&self->mg); 56 self->pid = pid; 57 self->comm = malloc(32); 58 if (self->comm) 59 snprintf(self->comm, 32, ":%d", self->pid); 60 } 61 62 return self; 63 } 64 65 int thread__set_comm(struct thread *self, const char *comm) 66 { 67 int err; 68 69 if (self->comm) 70 free(self->comm); 71 self->comm = strdup(comm); 72 err = self->comm == NULL ? -ENOMEM : 0; 73 if (!err) { 74 self->comm_set = true; 75 map_groups__flush(&self->mg); 76 } 77 return err; 78 } 79 80 int thread__comm_len(struct thread *self) 81 { 82 if (!self->comm_len) { 83 if (!self->comm) 84 return 0; 85 self->comm_len = strlen(self->comm); 86 } 87 88 return self->comm_len; 89 } 90 91 static size_t thread__fprintf(struct thread *self, FILE *fp) 92 { 93 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) + 94 map_groups__fprintf(&self->mg, verbose, fp); 95 } 96 97 struct thread *perf_session__findnew(struct perf_session *self, pid_t pid) 98 { 99 struct rb_node **p = &self->threads.rb_node; 100 struct rb_node *parent = NULL; 101 struct thread *th; 102 103 /* 104 * Font-end cache - PID lookups come in blocks, 105 * so most of the time we dont have to look up 106 * the full rbtree: 107 */ 108 if (self->last_match && self->last_match->pid == pid) 109 return self->last_match; 110 111 while (*p != NULL) { 112 parent = *p; 113 th = rb_entry(parent, struct thread, rb_node); 114 115 if (th->pid == pid) { 116 self->last_match = th; 117 return th; 118 } 119 120 if (pid < th->pid) 121 p = &(*p)->rb_left; 122 else 123 p = &(*p)->rb_right; 124 } 125 126 th = thread__new(pid); 127 if (th != NULL) { 128 rb_link_node(&th->rb_node, parent, p); 129 rb_insert_color(&th->rb_node, &self->threads); 130 self->last_match = th; 131 } 132 133 return th; 134 } 135 136 void thread__insert_map(struct thread *self, struct map *map) 137 { 138 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); 139 map_groups__insert(&self->mg, map); 140 } 141 142 int thread__fork(struct thread *self, struct thread *parent) 143 { 144 int i; 145 146 if (parent->comm_set) { 147 if (self->comm) 148 free(self->comm); 149 self->comm = strdup(parent->comm); 150 if (!self->comm) 151 return -ENOMEM; 152 self->comm_set = true; 153 } 154 155 for (i = 0; i < MAP__NR_TYPES; ++i) 156 if (map_groups__clone(&self->mg, &parent->mg, i) < 0) 157 return -ENOMEM; 158 return 0; 159 } 160 161 size_t perf_session__fprintf(struct perf_session *self, FILE *fp) 162 { 163 size_t ret = 0; 164 struct rb_node *nd; 165 166 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) { 167 struct thread *pos = rb_entry(nd, struct thread, rb_node); 168 169 ret += thread__fprintf(pos, fp); 170 } 171 172 return ret; 173 } 174