13 #include <api/fs/fs.h>
15 #include "thread_map.h"
20 /* Skip "." and ".." directories */
21 static int filter(const struct dirent *dir)
23 if (dir->d_name[0] == '.')
29 static void thread_map__reset(struct thread_map *map, int start, int nr)
31 size_t size = (nr - start) * sizeof(map->map[0]);
33 memset(&map->map[start], 0, size);
36 static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
38 size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
39 int start = map ? map->nr : 0;
41 map = realloc(map, size);
43 * We only realloc to add more items, let's reset new items.
46 thread_map__reset(map, start, nr);
51 #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
53 struct thread_map *thread_map__new_by_pid(pid_t pid)
55 struct thread_map *threads;
58 struct dirent **namelist = NULL;
61 sprintf(name, "/proc/%d/task", pid);
62 items = scandir(name, &namelist, filter, NULL);
66 threads = thread_map__alloc(items);
67 if (threads != NULL) {
68 for (i = 0; i < items; i++)
69 thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
71 refcount_set(&threads->refcnt, 1);
74 for (i=0; i<items; i++)
81 struct thread_map *thread_map__new_by_tid(pid_t tid)
83 struct thread_map *threads = thread_map__alloc(1);
85 if (threads != NULL) {
86 thread_map__set_pid(threads, 0, tid);
88 refcount_set(&threads->refcnt, 1);
94 struct thread_map *thread_map__new_by_uid(uid_t uid)
97 int max_threads = 32, items, i;
98 char path[NAME_MAX + 1 + 6];
99 struct dirent *dirent, **namelist = NULL;
100 struct thread_map *threads = thread_map__alloc(max_threads);
105 proc = opendir("/proc");
107 goto out_free_threads;
110 refcount_set(&threads->refcnt, 1);
112 while ((dirent = readdir(proc)) != NULL) {
116 pid_t pid = strtol(dirent->d_name, &end, 10);
118 if (*end) /* only interested in proper numerical dirents */
121 snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
123 if (stat(path, &st) != 0)
126 if (st.st_uid != uid)
129 snprintf(path, sizeof(path), "/proc/%d/task", pid);
130 items = scandir(path, &namelist, filter, NULL);
132 goto out_free_closedir;
134 while (threads->nr + items >= max_threads) {
140 struct thread_map *tmp;
142 tmp = thread_map__realloc(threads, max_threads);
144 goto out_free_namelist;
149 for (i = 0; i < items; i++) {
150 thread_map__set_pid(threads, threads->nr + i,
151 atoi(namelist[i]->d_name));
154 for (i = 0; i < items; i++)
158 threads->nr += items;
171 for (i = 0; i < items; i++)
180 struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
183 return thread_map__new_by_pid(pid);
185 if (tid == -1 && uid != UINT_MAX)
186 return thread_map__new_by_uid(uid);
188 return thread_map__new_by_tid(tid);
191 static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
193 struct thread_map *threads = NULL, *nt;
195 int items, total_tasks = 0;
196 struct dirent **namelist = NULL;
198 pid_t pid, prev_pid = INT_MAX;
200 struct str_node *pos;
201 struct strlist_config slist_config = { .dont_dupstr = true, };
202 struct strlist *slist = strlist__new(pid_str, &slist_config);
207 strlist__for_each_entry(pos, slist) {
208 pid = strtol(pos->s, &end_ptr, 10);
210 if (pid == INT_MIN || pid == INT_MAX ||
211 (*end_ptr != '\0' && *end_ptr != ','))
212 goto out_free_threads;
217 sprintf(name, "/proc/%d/task", pid);
218 items = scandir(name, &namelist, filter, NULL);
220 goto out_free_threads;
222 total_tasks += items;
223 nt = thread_map__realloc(threads, total_tasks);
225 goto out_free_namelist;
229 for (i = 0; i < items; i++) {
230 thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
233 threads->nr = total_tasks;
238 strlist__delete(slist);
240 refcount_set(&threads->refcnt, 1);
244 for (i = 0; i < items; i++)
253 struct thread_map *thread_map__new_dummy(void)
255 struct thread_map *threads = thread_map__alloc(1);
257 if (threads != NULL) {
258 thread_map__set_pid(threads, 0, -1);
260 refcount_set(&threads->refcnt, 1);
265 struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
267 struct thread_map *threads = NULL, *nt;
269 pid_t tid, prev_tid = INT_MAX;
271 struct str_node *pos;
272 struct strlist_config slist_config = { .dont_dupstr = true, };
273 struct strlist *slist;
275 /* perf-stat expects threads to be generated even if tid not given */
277 return thread_map__new_dummy();
279 slist = strlist__new(tid_str, &slist_config);
283 strlist__for_each_entry(pos, slist) {
284 tid = strtol(pos->s, &end_ptr, 10);
286 if (tid == INT_MIN || tid == INT_MAX ||
287 (*end_ptr != '\0' && *end_ptr != ','))
288 goto out_free_threads;
294 nt = thread_map__realloc(threads, ntasks);
297 goto out_free_threads;
300 thread_map__set_pid(threads, ntasks - 1, tid);
301 threads->nr = ntasks;
305 refcount_set(&threads->refcnt, 1);
310 strlist__delete(slist);
314 struct thread_map *thread_map__new_str(const char *pid, const char *tid,
318 return thread_map__new_by_pid_str(pid);
320 if (!tid && uid != UINT_MAX)
321 return thread_map__new_by_uid(uid);
323 return thread_map__new_by_tid_str(tid);
326 static void thread_map__delete(struct thread_map *threads)
331 WARN_ONCE(refcount_read(&threads->refcnt) != 0,
332 "thread map refcnt unbalanced\n");
333 for (i = 0; i < threads->nr; i++)
334 free(thread_map__comm(threads, i));
339 struct thread_map *thread_map__get(struct thread_map *map)
342 refcount_inc(&map->refcnt);
346 void thread_map__put(struct thread_map *map)
348 if (map && refcount_dec_and_test(&map->refcnt))
349 thread_map__delete(map);
352 size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
355 size_t printed = fprintf(fp, "%d thread%s: ",
356 threads->nr, threads->nr > 1 ? "s" : "");
357 for (i = 0; i < threads->nr; ++i)
358 printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
360 return printed + fprintf(fp, "\n");
363 static int get_comm(char **comm, pid_t pid)
369 if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
372 err = filename__read_str(path, comm, &size);
375 * We're reading 16 bytes, while filename__read_str
376 * allocates data per BUFSIZ bytes, so we can safely
377 * mark the end of the string.
387 static void comm_init(struct thread_map *map, int i)
389 pid_t pid = thread_map__pid(map, i);
392 /* dummy pid comm initialization */
394 map->map[i].comm = strdup("dummy");
399 * The comm name is like extra bonus ;-),
400 * so just warn if we fail for any reason.
402 if (get_comm(&comm, pid))
403 pr_warning("Couldn't resolve comm name for pid %d\n", pid);
405 map->map[i].comm = comm;
408 void thread_map__read_comms(struct thread_map *threads)
412 for (i = 0; i < threads->nr; ++i)
413 comm_init(threads, i);
416 static void thread_map__copy_event(struct thread_map *threads,
417 struct thread_map_event *event)
421 threads->nr = (int) event->nr;
423 for (i = 0; i < event->nr; i++) {
424 thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
425 threads->map[i].comm = strndup(event->entries[i].comm, 16);
428 refcount_set(&threads->refcnt, 1);
431 struct thread_map *thread_map__new_event(struct thread_map_event *event)
433 struct thread_map *threads;
435 threads = thread_map__alloc(event->nr);
437 thread_map__copy_event(threads, event);
442 bool thread_map__has(struct thread_map *threads, pid_t pid)
446 for (i = 0; i < threads->nr; ++i) {
447 if (threads->map[i].pid == pid)
454 int thread_map__remove(struct thread_map *threads, int idx)
461 if (idx >= threads->nr)
465 * Free the 'idx' item and shift the rest up.
467 free(threads->map[idx].comm);
469 for (i = idx; i < threads->nr - 1; i++)
470 threads->map[i] = threads->map[i + 1];