Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 1 | #include <dirent.h> |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 2 | #include <limits.h> |
| 3 | #include <stdbool.h> |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <unistd.h> |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 9 | #include "strlist.h" |
| 10 | #include <string.h> |
Jiri Olsa | 792402f | 2015-06-26 11:29:07 +0200 | [diff] [blame] | 11 | #include <api/fs/fs.h> |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 12 | #include "asm/bug.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 13 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 14 | #include "util.h" |
Jiri Olsa | 792402f | 2015-06-26 11:29:07 +0200 | [diff] [blame] | 15 | #include "debug.h" |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 16 | #include "event.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 17 | |
| 18 | /* Skip "." and ".." directories */ |
| 19 | static int filter(const struct dirent *dir) |
| 20 | { |
| 21 | if (dir->d_name[0] == '.') |
| 22 | return 0; |
| 23 | else |
| 24 | return 1; |
| 25 | } |
| 26 | |
Jiri Olsa | 62eea46 | 2015-06-26 11:29:06 +0200 | [diff] [blame] | 27 | static void thread_map__reset(struct thread_map *map, int start, int nr) |
| 28 | { |
| 29 | size_t size = (nr - start) * sizeof(map->map[0]); |
| 30 | |
| 31 | memset(&map->map[start], 0, size); |
| 32 | } |
| 33 | |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 34 | static struct thread_map *thread_map__realloc(struct thread_map *map, int nr) |
| 35 | { |
Arnaldo Carvalho de Melo | 060664f | 2015-06-25 14:48:49 -0300 | [diff] [blame] | 36 | size_t size = sizeof(*map) + sizeof(map->map[0]) * nr; |
Jiri Olsa | 62eea46 | 2015-06-26 11:29:06 +0200 | [diff] [blame] | 37 | int start = map ? map->nr : 0; |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 38 | |
Jiri Olsa | 62eea46 | 2015-06-26 11:29:06 +0200 | [diff] [blame] | 39 | map = realloc(map, size); |
| 40 | /* |
| 41 | * We only realloc to add more items, let's reset new items. |
| 42 | */ |
| 43 | if (map) |
| 44 | thread_map__reset(map, start, nr); |
| 45 | |
| 46 | return map; |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr) |
| 50 | |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 51 | struct thread_map *thread_map__new_by_pid(pid_t pid) |
| 52 | { |
| 53 | struct thread_map *threads; |
| 54 | char name[256]; |
| 55 | int items; |
| 56 | struct dirent **namelist = NULL; |
| 57 | int i; |
| 58 | |
| 59 | sprintf(name, "/proc/%d/task", pid); |
| 60 | items = scandir(name, &namelist, filter, NULL); |
| 61 | if (items <= 0) |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 62 | return NULL; |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 63 | |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 64 | threads = thread_map__alloc(items); |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 65 | if (threads != NULL) { |
| 66 | for (i = 0; i < items; i++) |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 67 | thread_map__set_pid(threads, i, atoi(namelist[i]->d_name)); |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 68 | threads->nr = items; |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 69 | atomic_set(&threads->refcnt, 1); |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | for (i=0; i<items; i++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 73 | zfree(&namelist[i]); |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 74 | free(namelist); |
| 75 | |
| 76 | return threads; |
| 77 | } |
| 78 | |
| 79 | struct thread_map *thread_map__new_by_tid(pid_t tid) |
| 80 | { |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 81 | struct thread_map *threads = thread_map__alloc(1); |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 82 | |
| 83 | if (threads != NULL) { |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 84 | thread_map__set_pid(threads, 0, tid); |
| 85 | threads->nr = 1; |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 86 | atomic_set(&threads->refcnt, 1); |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return threads; |
| 90 | } |
| 91 | |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 92 | struct thread_map *thread_map__new_by_uid(uid_t uid) |
| 93 | { |
| 94 | DIR *proc; |
| 95 | int max_threads = 32, items, i; |
| 96 | char path[256]; |
| 97 | struct dirent dirent, *next, **namelist = NULL; |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 98 | struct thread_map *threads = thread_map__alloc(max_threads); |
| 99 | |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 100 | if (threads == NULL) |
| 101 | goto out; |
| 102 | |
| 103 | proc = opendir("/proc"); |
| 104 | if (proc == NULL) |
| 105 | goto out_free_threads; |
| 106 | |
| 107 | threads->nr = 0; |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 108 | atomic_set(&threads->refcnt, 1); |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 109 | |
| 110 | while (!readdir_r(proc, &dirent, &next) && next) { |
| 111 | char *end; |
| 112 | bool grow = false; |
| 113 | struct stat st; |
| 114 | pid_t pid = strtol(dirent.d_name, &end, 10); |
| 115 | |
| 116 | if (*end) /* only interested in proper numerical dirents */ |
| 117 | continue; |
| 118 | |
| 119 | snprintf(path, sizeof(path), "/proc/%s", dirent.d_name); |
| 120 | |
| 121 | if (stat(path, &st) != 0) |
| 122 | continue; |
| 123 | |
| 124 | if (st.st_uid != uid) |
| 125 | continue; |
| 126 | |
| 127 | snprintf(path, sizeof(path), "/proc/%d/task", pid); |
| 128 | items = scandir(path, &namelist, filter, NULL); |
| 129 | if (items <= 0) |
| 130 | goto out_free_closedir; |
| 131 | |
| 132 | while (threads->nr + items >= max_threads) { |
| 133 | max_threads *= 2; |
| 134 | grow = true; |
| 135 | } |
| 136 | |
| 137 | if (grow) { |
| 138 | struct thread_map *tmp; |
| 139 | |
Arnaldo Carvalho de Melo | 08ae217 | 2015-07-09 12:14:43 -0300 | [diff] [blame] | 140 | tmp = thread_map__realloc(threads, max_threads); |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 141 | if (tmp == NULL) |
| 142 | goto out_free_namelist; |
| 143 | |
| 144 | threads = tmp; |
| 145 | } |
| 146 | |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 147 | for (i = 0; i < items; i++) { |
| 148 | thread_map__set_pid(threads, threads->nr + i, |
| 149 | atoi(namelist[i]->d_name)); |
| 150 | } |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 151 | |
| 152 | for (i = 0; i < items; i++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 153 | zfree(&namelist[i]); |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 154 | free(namelist); |
| 155 | |
| 156 | threads->nr += items; |
| 157 | } |
| 158 | |
| 159 | out_closedir: |
| 160 | closedir(proc); |
| 161 | out: |
| 162 | return threads; |
| 163 | |
| 164 | out_free_threads: |
| 165 | free(threads); |
| 166 | return NULL; |
| 167 | |
| 168 | out_free_namelist: |
| 169 | for (i = 0; i < items; i++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 170 | zfree(&namelist[i]); |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 171 | free(namelist); |
| 172 | |
| 173 | out_free_closedir: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 174 | zfree(&threads); |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 175 | goto out_closedir; |
| 176 | } |
| 177 | |
| 178 | struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid) |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 179 | { |
| 180 | if (pid != -1) |
| 181 | return thread_map__new_by_pid(pid); |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 182 | |
| 183 | if (tid == -1 && uid != UINT_MAX) |
| 184 | return thread_map__new_by_uid(uid); |
| 185 | |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 186 | return thread_map__new_by_tid(tid); |
| 187 | } |
| 188 | |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 189 | static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) |
| 190 | { |
| 191 | struct thread_map *threads = NULL, *nt; |
| 192 | char name[256]; |
| 193 | int items, total_tasks = 0; |
| 194 | struct dirent **namelist = NULL; |
| 195 | int i, j = 0; |
| 196 | pid_t pid, prev_pid = INT_MAX; |
| 197 | char *end_ptr; |
| 198 | struct str_node *pos; |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 199 | struct strlist_config slist_config = { .dont_dupstr = true, }; |
| 200 | struct strlist *slist = strlist__new(pid_str, &slist_config); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 201 | |
| 202 | if (!slist) |
| 203 | return NULL; |
| 204 | |
| 205 | strlist__for_each(pos, slist) { |
| 206 | pid = strtol(pos->s, &end_ptr, 10); |
| 207 | |
| 208 | if (pid == INT_MIN || pid == INT_MAX || |
| 209 | (*end_ptr != '\0' && *end_ptr != ',')) |
| 210 | goto out_free_threads; |
| 211 | |
| 212 | if (pid == prev_pid) |
| 213 | continue; |
| 214 | |
| 215 | sprintf(name, "/proc/%d/task", pid); |
| 216 | items = scandir(name, &namelist, filter, NULL); |
| 217 | if (items <= 0) |
| 218 | goto out_free_threads; |
| 219 | |
| 220 | total_tasks += items; |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 221 | nt = thread_map__realloc(threads, total_tasks); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 222 | if (nt == NULL) |
Franck Bui-Huu | e8cdd94 | 2012-05-25 15:21:49 +0200 | [diff] [blame] | 223 | goto out_free_namelist; |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 224 | |
| 225 | threads = nt; |
| 226 | |
Franck Bui-Huu | e8cdd94 | 2012-05-25 15:21:49 +0200 | [diff] [blame] | 227 | for (i = 0; i < items; i++) { |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 228 | thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name)); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 229 | zfree(&namelist[i]); |
Franck Bui-Huu | e8cdd94 | 2012-05-25 15:21:49 +0200 | [diff] [blame] | 230 | } |
| 231 | threads->nr = total_tasks; |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 232 | free(namelist); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | out: |
| 236 | strlist__delete(slist); |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 237 | if (threads) |
| 238 | atomic_set(&threads->refcnt, 1); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 239 | return threads; |
| 240 | |
Franck Bui-Huu | e8cdd94 | 2012-05-25 15:21:49 +0200 | [diff] [blame] | 241 | out_free_namelist: |
| 242 | for (i = 0; i < items; i++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 243 | zfree(&namelist[i]); |
Franck Bui-Huu | e8cdd94 | 2012-05-25 15:21:49 +0200 | [diff] [blame] | 244 | free(namelist); |
| 245 | |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 246 | out_free_threads: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 247 | zfree(&threads); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 248 | goto out; |
| 249 | } |
| 250 | |
Arnaldo Carvalho de Melo | 641556c | 2014-10-10 12:03:46 -0300 | [diff] [blame] | 251 | struct thread_map *thread_map__new_dummy(void) |
| 252 | { |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 253 | struct thread_map *threads = thread_map__alloc(1); |
Arnaldo Carvalho de Melo | 641556c | 2014-10-10 12:03:46 -0300 | [diff] [blame] | 254 | |
| 255 | if (threads != NULL) { |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 256 | thread_map__set_pid(threads, 0, -1); |
| 257 | threads->nr = 1; |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 258 | atomic_set(&threads->refcnt, 1); |
Arnaldo Carvalho de Melo | 641556c | 2014-10-10 12:03:46 -0300 | [diff] [blame] | 259 | } |
| 260 | return threads; |
| 261 | } |
| 262 | |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 263 | static struct thread_map *thread_map__new_by_tid_str(const char *tid_str) |
| 264 | { |
| 265 | struct thread_map *threads = NULL, *nt; |
| 266 | int ntasks = 0; |
| 267 | pid_t tid, prev_tid = INT_MAX; |
| 268 | char *end_ptr; |
| 269 | struct str_node *pos; |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 270 | struct strlist_config slist_config = { .dont_dupstr = true, }; |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 271 | struct strlist *slist; |
| 272 | |
| 273 | /* perf-stat expects threads to be generated even if tid not given */ |
Arnaldo Carvalho de Melo | 641556c | 2014-10-10 12:03:46 -0300 | [diff] [blame] | 274 | if (!tid_str) |
| 275 | return thread_map__new_dummy(); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 276 | |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 277 | slist = strlist__new(tid_str, &slist_config); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 278 | if (!slist) |
| 279 | return NULL; |
| 280 | |
| 281 | strlist__for_each(pos, slist) { |
| 282 | tid = strtol(pos->s, &end_ptr, 10); |
| 283 | |
| 284 | if (tid == INT_MIN || tid == INT_MAX || |
| 285 | (*end_ptr != '\0' && *end_ptr != ',')) |
| 286 | goto out_free_threads; |
| 287 | |
| 288 | if (tid == prev_tid) |
| 289 | continue; |
| 290 | |
| 291 | ntasks++; |
Jiri Olsa | 9d7e8c3 | 2015-06-14 10:19:17 +0200 | [diff] [blame] | 292 | nt = thread_map__realloc(threads, ntasks); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 293 | |
| 294 | if (nt == NULL) |
| 295 | goto out_free_threads; |
| 296 | |
| 297 | threads = nt; |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 298 | thread_map__set_pid(threads, ntasks - 1, tid); |
| 299 | threads->nr = ntasks; |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 300 | } |
| 301 | out: |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 302 | if (threads) |
| 303 | atomic_set(&threads->refcnt, 1); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 304 | return threads; |
| 305 | |
| 306 | out_free_threads: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 307 | zfree(&threads); |
Namhyung Kim | 7ecb48f | 2015-12-10 12:00:58 +0900 | [diff] [blame] | 308 | strlist__delete(slist); |
David Ahern | b52956c | 2012-02-08 09:32:52 -0700 | [diff] [blame] | 309 | goto out; |
| 310 | } |
| 311 | |
| 312 | struct thread_map *thread_map__new_str(const char *pid, const char *tid, |
| 313 | uid_t uid) |
| 314 | { |
| 315 | if (pid) |
| 316 | return thread_map__new_by_pid_str(pid); |
| 317 | |
| 318 | if (!tid && uid != UINT_MAX) |
| 319 | return thread_map__new_by_uid(uid); |
| 320 | |
| 321 | return thread_map__new_by_tid_str(tid); |
| 322 | } |
| 323 | |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 324 | static void thread_map__delete(struct thread_map *threads) |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 325 | { |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 326 | if (threads) { |
Jiri Olsa | 792402f | 2015-06-26 11:29:07 +0200 | [diff] [blame] | 327 | int i; |
| 328 | |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 329 | WARN_ONCE(atomic_read(&threads->refcnt) != 0, |
| 330 | "thread map refcnt unbalanced\n"); |
Jiri Olsa | 792402f | 2015-06-26 11:29:07 +0200 | [diff] [blame] | 331 | for (i = 0; i < threads->nr; i++) |
| 332 | free(thread_map__comm(threads, i)); |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 333 | free(threads); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | struct thread_map *thread_map__get(struct thread_map *map) |
| 338 | { |
| 339 | if (map) |
| 340 | atomic_inc(&map->refcnt); |
| 341 | return map; |
| 342 | } |
| 343 | |
| 344 | void thread_map__put(struct thread_map *map) |
| 345 | { |
| 346 | if (map && atomic_dec_and_test(&map->refcnt)) |
| 347 | thread_map__delete(map); |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 348 | } |
Arnaldo Carvalho de Melo | 9ae7d33 | 2012-01-19 14:07:23 -0200 | [diff] [blame] | 349 | |
| 350 | size_t thread_map__fprintf(struct thread_map *threads, FILE *fp) |
| 351 | { |
| 352 | int i; |
| 353 | size_t printed = fprintf(fp, "%d thread%s: ", |
| 354 | threads->nr, threads->nr > 1 ? "s" : ""); |
| 355 | for (i = 0; i < threads->nr; ++i) |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 356 | printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i)); |
Arnaldo Carvalho de Melo | 9ae7d33 | 2012-01-19 14:07:23 -0200 | [diff] [blame] | 357 | |
| 358 | return printed + fprintf(fp, "\n"); |
| 359 | } |
Jiri Olsa | 792402f | 2015-06-26 11:29:07 +0200 | [diff] [blame] | 360 | |
| 361 | static int get_comm(char **comm, pid_t pid) |
| 362 | { |
| 363 | char *path; |
| 364 | size_t size; |
| 365 | int err; |
| 366 | |
| 367 | if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1) |
| 368 | return -ENOMEM; |
| 369 | |
| 370 | err = filename__read_str(path, comm, &size); |
| 371 | if (!err) { |
| 372 | /* |
| 373 | * We're reading 16 bytes, while filename__read_str |
| 374 | * allocates data per BUFSIZ bytes, so we can safely |
| 375 | * mark the end of the string. |
| 376 | */ |
| 377 | (*comm)[size] = 0; |
| 378 | rtrim(*comm); |
| 379 | } |
| 380 | |
| 381 | free(path); |
| 382 | return err; |
| 383 | } |
| 384 | |
| 385 | static void comm_init(struct thread_map *map, int i) |
| 386 | { |
| 387 | pid_t pid = thread_map__pid(map, i); |
| 388 | char *comm = NULL; |
| 389 | |
| 390 | /* dummy pid comm initialization */ |
| 391 | if (pid == -1) { |
| 392 | map->map[i].comm = strdup("dummy"); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * The comm name is like extra bonus ;-), |
| 398 | * so just warn if we fail for any reason. |
| 399 | */ |
| 400 | if (get_comm(&comm, pid)) |
| 401 | pr_warning("Couldn't resolve comm name for pid %d\n", pid); |
| 402 | |
| 403 | map->map[i].comm = comm; |
| 404 | } |
| 405 | |
| 406 | void thread_map__read_comms(struct thread_map *threads) |
| 407 | { |
| 408 | int i; |
| 409 | |
| 410 | for (i = 0; i < threads->nr; ++i) |
| 411 | comm_init(threads, i); |
| 412 | } |
Jiri Olsa | 5966094 | 2015-10-25 15:51:21 +0100 | [diff] [blame] | 413 | |
| 414 | static void thread_map__copy_event(struct thread_map *threads, |
| 415 | struct thread_map_event *event) |
| 416 | { |
| 417 | unsigned i; |
| 418 | |
| 419 | threads->nr = (int) event->nr; |
| 420 | |
| 421 | for (i = 0; i < event->nr; i++) { |
| 422 | thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid); |
| 423 | threads->map[i].comm = strndup(event->entries[i].comm, 16); |
| 424 | } |
| 425 | |
| 426 | atomic_set(&threads->refcnt, 1); |
| 427 | } |
| 428 | |
| 429 | struct thread_map *thread_map__new_event(struct thread_map_event *event) |
| 430 | { |
| 431 | struct thread_map *threads; |
| 432 | |
| 433 | threads = thread_map__alloc(event->nr); |
| 434 | if (threads) |
| 435 | thread_map__copy_event(threads, event); |
| 436 | |
| 437 | return threads; |
| 438 | } |