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 #include "comm.h" 10 11 int thread__init_map_groups(struct thread *thread, struct machine *machine) 12 { 13 struct thread *leader; 14 pid_t pid = thread->pid_; 15 16 if (pid == thread->tid) { 17 thread->mg = map_groups__new(); 18 } else { 19 leader = machine__findnew_thread(machine, pid, pid); 20 if (leader) 21 thread->mg = map_groups__get(leader->mg); 22 } 23 24 return thread->mg ? 0 : -1; 25 } 26 27 struct thread *thread__new(pid_t pid, pid_t tid) 28 { 29 char *comm_str; 30 struct comm *comm; 31 struct thread *thread = zalloc(sizeof(*thread)); 32 33 if (thread != NULL) { 34 thread->pid_ = pid; 35 thread->tid = tid; 36 thread->ppid = -1; 37 INIT_LIST_HEAD(&thread->comm_list); 38 39 comm_str = malloc(32); 40 if (!comm_str) 41 goto err_thread; 42 43 snprintf(comm_str, 32, ":%d", tid); 44 comm = comm__new(comm_str, 0); 45 free(comm_str); 46 if (!comm) 47 goto err_thread; 48 49 list_add(&comm->list, &thread->comm_list); 50 } 51 52 return thread; 53 54 err_thread: 55 free(thread); 56 return NULL; 57 } 58 59 void thread__delete(struct thread *thread) 60 { 61 struct comm *comm, *tmp; 62 63 map_groups__put(thread->mg); 64 thread->mg = NULL; 65 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) { 66 list_del(&comm->list); 67 comm__free(comm); 68 } 69 70 free(thread); 71 } 72 73 struct comm *thread__comm(const struct thread *thread) 74 { 75 if (list_empty(&thread->comm_list)) 76 return NULL; 77 78 return list_first_entry(&thread->comm_list, struct comm, list); 79 } 80 81 /* CHECKME: time should always be 0 if event aren't ordered */ 82 int thread__set_comm(struct thread *thread, const char *str, u64 timestamp) 83 { 84 struct comm *new, *curr = thread__comm(thread); 85 int err; 86 87 /* Override latest entry if it had no specific time coverage */ 88 if (!curr->start) { 89 err = comm__override(curr, str, timestamp); 90 if (err) 91 return err; 92 } else { 93 new = comm__new(str, timestamp); 94 if (!new) 95 return -ENOMEM; 96 list_add(&new->list, &thread->comm_list); 97 } 98 99 thread->comm_set = true; 100 101 return 0; 102 } 103 104 const char *thread__comm_str(const struct thread *thread) 105 { 106 const struct comm *comm = thread__comm(thread); 107 108 if (!comm) 109 return NULL; 110 111 return comm__str(comm); 112 } 113 114 /* CHECKME: it should probably better return the max comm len from its comm list */ 115 int thread__comm_len(struct thread *thread) 116 { 117 if (!thread->comm_len) { 118 const char *comm = thread__comm_str(thread); 119 if (!comm) 120 return 0; 121 thread->comm_len = strlen(comm); 122 } 123 124 return thread->comm_len; 125 } 126 127 size_t thread__fprintf(struct thread *thread, FILE *fp) 128 { 129 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + 130 map_groups__fprintf(thread->mg, verbose, fp); 131 } 132 133 void thread__insert_map(struct thread *thread, struct map *map) 134 { 135 map_groups__fixup_overlappings(thread->mg, map, verbose, stderr); 136 map_groups__insert(thread->mg, map); 137 } 138 139 static int thread__clone_map_groups(struct thread *thread, 140 struct thread *parent) 141 { 142 int i; 143 144 /* This is new thread, we share map groups for process. */ 145 if (thread->pid_ == parent->pid_) 146 return 0; 147 148 /* But this one is new process, copy maps. */ 149 for (i = 0; i < MAP__NR_TYPES; ++i) 150 if (map_groups__clone(thread->mg, parent->mg, i) < 0) 151 return -ENOMEM; 152 153 return 0; 154 } 155 156 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp) 157 { 158 int err; 159 160 if (parent->comm_set) { 161 const char *comm = thread__comm_str(parent); 162 if (!comm) 163 return -ENOMEM; 164 err = thread__set_comm(thread, comm, timestamp); 165 if (err) 166 return err; 167 thread->comm_set = true; 168 } 169 170 thread->ppid = parent->tid; 171 return thread__clone_map_groups(thread, parent); 172 } 173 174 void thread__find_cpumode_addr_location(struct thread *thread, 175 struct machine *machine, 176 enum map_type type, u64 addr, 177 struct addr_location *al) 178 { 179 size_t i; 180 const u8 const cpumodes[] = { 181 PERF_RECORD_MISC_USER, 182 PERF_RECORD_MISC_KERNEL, 183 PERF_RECORD_MISC_GUEST_USER, 184 PERF_RECORD_MISC_GUEST_KERNEL 185 }; 186 187 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { 188 thread__find_addr_location(thread, machine, cpumodes[i], type, 189 addr, al); 190 if (al->map) 191 break; 192 } 193 } 194