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 | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 9 | #include "comm.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 10 | |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 11 | int thread__init_map_groups(struct thread *thread, struct machine *machine) |
| 12 | { |
| 13 | struct thread *leader; |
| 14 | pid_t pid = thread->pid_; |
| 15 | |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame^] | 16 | if (pid == thread->tid || pid == -1) { |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 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 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 27 | struct thread *thread__new(pid_t pid, pid_t tid) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 28 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 29 | char *comm_str; |
| 30 | struct comm *comm; |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 31 | struct thread *thread = zalloc(sizeof(*thread)); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 32 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 33 | if (thread != NULL) { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 34 | thread->pid_ = pid; |
| 35 | thread->tid = tid; |
| 36 | thread->ppid = -1; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 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); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 52 | return thread; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 53 | |
| 54 | err_thread: |
| 55 | free(thread); |
| 56 | return NULL; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 59 | void thread__delete(struct thread *thread) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 60 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 61 | struct comm *comm, *tmp; |
| 62 | |
Arnaldo Carvalho de Melo | a26ca67 | 2014-03-25 15:26:44 -0300 | [diff] [blame] | 63 | map_groups__put(thread->mg); |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 64 | thread->mg = NULL; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 65 | list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) { |
| 66 | list_del(&comm->list); |
| 67 | comm__free(comm); |
| 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 70 | free(thread); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 71 | } |
| 72 | |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 73 | struct comm *thread__comm(const struct thread *thread) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 74 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 75 | if (list_empty(&thread->comm_list)) |
| 76 | return NULL; |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 77 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 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); |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 85 | int err; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 86 | |
| 87 | /* Override latest entry if it had no specific time coverage */ |
| 88 | if (!curr->start) { |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 89 | err = comm__override(curr, str, timestamp); |
| 90 | if (err) |
| 91 | return err; |
Frederic Weisbecker | a5285ad | 2013-11-16 02:02:09 +0100 | [diff] [blame] | 92 | } else { |
| 93 | new = comm__new(str, timestamp); |
| 94 | if (!new) |
| 95 | return -ENOMEM; |
| 96 | list_add(&new->list, &thread->comm_list); |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 97 | } |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 98 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 99 | thread->comm_set = true; |
| 100 | |
| 101 | return 0; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 104 | const char *thread__comm_str(const struct thread *thread) |
| 105 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 106 | const struct comm *comm = thread__comm(thread); |
| 107 | |
| 108 | if (!comm) |
| 109 | return NULL; |
| 110 | |
| 111 | return comm__str(comm); |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 114 | /* CHECKME: it should probably better return the max comm len from its comm list */ |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 115 | int thread__comm_len(struct thread *thread) |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 116 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 117 | if (!thread->comm_len) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 118 | const char *comm = thread__comm_str(thread); |
| 119 | if (!comm) |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 120 | return 0; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 121 | thread->comm_len = strlen(comm); |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 124 | return thread->comm_len; |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 127 | size_t thread__fprintf(struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 128 | { |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 129 | return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 130 | map_groups__fprintf(thread->mg, verbose, fp); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 133 | void thread__insert_map(struct thread *thread, struct map *map) |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 134 | { |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 135 | map_groups__fixup_overlappings(thread->mg, map, verbose, stderr); |
| 136 | map_groups__insert(thread->mg, map); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 137 | } |
| 138 | |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 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 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 156 | int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 157 | { |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 158 | int err; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 159 | |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 160 | if (parent->comm_set) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 161 | const char *comm = thread__comm_str(parent); |
| 162 | if (!comm) |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 163 | return -ENOMEM; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 164 | err = thread__set_comm(thread, comm, timestamp); |
David Ahern | 8d00be8 | 2013-12-10 21:35:38 -0700 | [diff] [blame] | 165 | if (err) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 166 | return err; |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 167 | thread->comm_set = true; |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 168 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 169 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 170 | thread->ppid = parent->tid; |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 171 | return thread__clone_map_groups(thread, parent); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 172 | } |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 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 | } |