1 // SPDX-License-Identifier: GPL-2.0
14 #include <api/fs/fs.h>
16 #include "thread_map.h"
21 /* Skip "." and ".." directories */
22 static int filter(const struct dirent *dir)
24 if (dir->d_name[0] == '.')
30 static void thread_map__reset(struct thread_map *map, int start, int nr)
32 size_t size = (nr - start) * sizeof(map->map[0]);
34 memset(&map->map[start], 0, size);
38 static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
40 size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
41 int start = map ? map->nr : 0;
43 map = realloc(map, size);
45 * We only realloc to add more items, let's reset new items.
48 thread_map__reset(map, start, nr);
53 #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
55 struct thread_map *thread_map__new_by_pid(pid_t pid)
57 struct thread_map *threads;
60 struct dirent **namelist = NULL;
63 sprintf(name, "/proc/%d/task", pid);
64 items = scandir(name, &namelist, filter, NULL);
68 threads = thread_map__alloc(items);
69 if (threads != NULL) {
70 for (i = 0; i < items; i++)
71 thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
73 refcount_set(&threads->refcnt, 1);
76 for (i=0; i<items; i++)
83 struct thread_map *thread_map__new_by_tid(pid_t tid)
85 struct thread_map *threads = thread_map__alloc(1);
87 if (threads != NULL) {
88 thread_map__set_pid(threads, 0, tid);
90 refcount_set(&threads->refcnt, 1);
96 static struct thread_map *__thread_map__new_all_cpus(uid_t uid)
99 int max_threads = 32, items, i;
100 char path[NAME_MAX + 1 + 6];
101 struct dirent *dirent, **namelist = NULL;
102 struct thread_map *threads = thread_map__alloc(max_threads);
107 proc = opendir("/proc");
109 goto out_free_threads;
112 refcount_set(&threads->refcnt, 1);
114 while ((dirent = readdir(proc)) != NULL) {
117 pid_t pid = strtol(dirent->d_name, &end, 10);
119 if (*end) /* only interested in proper numerical dirents */
122 snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
124 if (uid != UINT_MAX) {
127 if (stat(path, &st) != 0 || st.st_uid != uid)
131 snprintf(path, sizeof(path), "/proc/%d/task", pid);
132 items = scandir(path, &namelist, filter, NULL);
134 goto out_free_closedir;
136 while (threads->nr + items >= max_threads) {
142 struct thread_map *tmp;
144 tmp = thread_map__realloc(threads, max_threads);
146 goto out_free_namelist;
151 for (i = 0; i < items; i++) {
152 thread_map__set_pid(threads, threads->nr + i,
153 atoi(namelist[i]->d_name));
156 for (i = 0; i < items; i++)
160 threads->nr += items;
173 for (i = 0; i < items; i++)
182 struct thread_map *thread_map__new_all_cpus(void)
184 return __thread_map__new_all_cpus(UINT_MAX);
187 struct thread_map *thread_map__new_by_uid(uid_t uid)
189 return __thread_map__new_all_cpus(uid);
192 struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
195 return thread_map__new_by_pid(pid);
197 if (tid == -1 && uid != UINT_MAX)
198 return thread_map__new_by_uid(uid);
200 return thread_map__new_by_tid(tid);
203 static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
205 struct thread_map *threads = NULL, *nt;
207 int items, total_tasks = 0;
208 struct dirent **namelist = NULL;
210 pid_t pid, prev_pid = INT_MAX;
212 struct str_node *pos;
213 struct strlist_config slist_config = { .dont_dupstr = true, };
214 struct strlist *slist = strlist__new(pid_str, &slist_config);
219 strlist__for_each_entry(pos, slist) {
220 pid = strtol(pos->s, &end_ptr, 10);
222 if (pid == INT_MIN || pid == INT_MAX ||
223 (*end_ptr != '\0' && *end_ptr != ','))
224 goto out_free_threads;
229 sprintf(name, "/proc/%d/task", pid);
230 items = scandir(name, &namelist, filter, NULL);
232 goto out_free_threads;
234 total_tasks += items;
235 nt = thread_map__realloc(threads, total_tasks);
237 goto out_free_namelist;
241 for (i = 0; i < items; i++) {
242 thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
245 threads->nr = total_tasks;
250 strlist__delete(slist);
252 refcount_set(&threads->refcnt, 1);
256 for (i = 0; i < items; i++)
265 struct thread_map *thread_map__new_dummy(void)
267 struct thread_map *threads = thread_map__alloc(1);
269 if (threads != NULL) {
270 thread_map__set_pid(threads, 0, -1);
272 refcount_set(&threads->refcnt, 1);
277 struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
279 struct thread_map *threads = NULL, *nt;
281 pid_t tid, prev_tid = INT_MAX;
283 struct str_node *pos;
284 struct strlist_config slist_config = { .dont_dupstr = true, };
285 struct strlist *slist;
287 /* perf-stat expects threads to be generated even if tid not given */
289 return thread_map__new_dummy();
291 slist = strlist__new(tid_str, &slist_config);
295 strlist__for_each_entry(pos, slist) {
296 tid = strtol(pos->s, &end_ptr, 10);
298 if (tid == INT_MIN || tid == INT_MAX ||
299 (*end_ptr != '\0' && *end_ptr != ','))
300 goto out_free_threads;
306 nt = thread_map__realloc(threads, ntasks);
309 goto out_free_threads;
312 thread_map__set_pid(threads, ntasks - 1, tid);
313 threads->nr = ntasks;
317 refcount_set(&threads->refcnt, 1);
322 strlist__delete(slist);
326 struct thread_map *thread_map__new_str(const char *pid, const char *tid,
327 uid_t uid, bool all_threads)
330 return thread_map__new_by_pid_str(pid);
332 if (!tid && uid != UINT_MAX)
333 return thread_map__new_by_uid(uid);
336 return thread_map__new_all_cpus();
338 return thread_map__new_by_tid_str(tid);
341 static void thread_map__delete(struct thread_map *threads)
346 WARN_ONCE(refcount_read(&threads->refcnt) != 0,
347 "thread map refcnt unbalanced\n");
348 for (i = 0; i < threads->nr; i++)
349 free(thread_map__comm(threads, i));
354 struct thread_map *thread_map__get(struct thread_map *map)
357 refcount_inc(&map->refcnt);
361 void thread_map__put(struct thread_map *map)
363 if (map && refcount_dec_and_test(&map->refcnt))
364 thread_map__delete(map);
367 size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
370 size_t printed = fprintf(fp, "%d thread%s: ",
371 threads->nr, threads->nr > 1 ? "s" : "");
372 for (i = 0; i < threads->nr; ++i)
373 printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
375 return printed + fprintf(fp, "\n");
378 static int get_comm(char **comm, pid_t pid)
384 if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
387 err = filename__read_str(path, comm, &size);
390 * We're reading 16 bytes, while filename__read_str
391 * allocates data per BUFSIZ bytes, so we can safely
392 * mark the end of the string.
402 static void comm_init(struct thread_map *map, int i)
404 pid_t pid = thread_map__pid(map, i);
407 /* dummy pid comm initialization */
409 map->map[i].comm = strdup("dummy");
414 * The comm name is like extra bonus ;-),
415 * so just warn if we fail for any reason.
417 if (get_comm(&comm, pid))
418 pr_warning("Couldn't resolve comm name for pid %d\n", pid);
420 map->map[i].comm = comm;
423 void thread_map__read_comms(struct thread_map *threads)
427 for (i = 0; i < threads->nr; ++i)
428 comm_init(threads, i);
431 static void thread_map__copy_event(struct thread_map *threads,
432 struct thread_map_event *event)
436 threads->nr = (int) event->nr;
438 for (i = 0; i < event->nr; i++) {
439 thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
440 threads->map[i].comm = strndup(event->entries[i].comm, 16);
443 refcount_set(&threads->refcnt, 1);
446 struct thread_map *thread_map__new_event(struct thread_map_event *event)
448 struct thread_map *threads;
450 threads = thread_map__alloc(event->nr);
452 thread_map__copy_event(threads, event);
457 bool thread_map__has(struct thread_map *threads, pid_t pid)
461 for (i = 0; i < threads->nr; ++i) {
462 if (threads->map[i].pid == pid)
469 int thread_map__remove(struct thread_map *threads, int idx)
476 if (idx >= threads->nr)
480 * Free the 'idx' item and shift the rest up.
482 free(threads->map[idx].comm);
484 for (i = idx; i < threads->nr - 1; i++)
485 threads->map[i] = threads->map[i + 1];