1 // SPDX-License-Identifier: GPL-2.0
14 #include <api/fs/fs.h>
15 #include <linux/string.h>
16 #include <linux/zalloc.h>
18 #include "thread_map.h"
22 /* Skip "." and ".." directories */
23 static int filter(const struct dirent *dir)
25 if (dir->d_name[0] == '.')
31 static void thread_map__reset(struct thread_map *map, int start, int nr)
33 size_t size = (nr - start) * sizeof(map->map[0]);
35 memset(&map->map[start], 0, size);
39 static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
41 size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
42 int start = map ? map->nr : 0;
44 map = realloc(map, size);
46 * We only realloc to add more items, let's reset new items.
49 thread_map__reset(map, start, nr);
54 #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
56 struct thread_map *thread_map__new_by_pid(pid_t pid)
58 struct thread_map *threads;
61 struct dirent **namelist = NULL;
64 sprintf(name, "/proc/%d/task", pid);
65 items = scandir(name, &namelist, filter, NULL);
69 threads = thread_map__alloc(items);
70 if (threads != NULL) {
71 for (i = 0; i < items; i++)
72 thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
74 refcount_set(&threads->refcnt, 1);
77 for (i=0; i<items; i++)
84 struct thread_map *thread_map__new_by_tid(pid_t tid)
86 struct thread_map *threads = thread_map__alloc(1);
88 if (threads != NULL) {
89 thread_map__set_pid(threads, 0, tid);
91 refcount_set(&threads->refcnt, 1);
97 static struct thread_map *__thread_map__new_all_cpus(uid_t uid)
100 int max_threads = 32, items, i;
101 char path[NAME_MAX + 1 + 6];
102 struct dirent *dirent, **namelist = NULL;
103 struct thread_map *threads = thread_map__alloc(max_threads);
108 proc = opendir("/proc");
110 goto out_free_threads;
113 refcount_set(&threads->refcnt, 1);
115 while ((dirent = readdir(proc)) != NULL) {
118 pid_t pid = strtol(dirent->d_name, &end, 10);
120 if (*end) /* only interested in proper numerical dirents */
123 snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
125 if (uid != UINT_MAX) {
128 if (stat(path, &st) != 0 || st.st_uid != uid)
132 snprintf(path, sizeof(path), "/proc/%d/task", pid);
133 items = scandir(path, &namelist, filter, NULL);
135 goto out_free_closedir;
137 while (threads->nr + items >= max_threads) {
143 struct thread_map *tmp;
145 tmp = thread_map__realloc(threads, max_threads);
147 goto out_free_namelist;
152 for (i = 0; i < items; i++) {
153 thread_map__set_pid(threads, threads->nr + i,
154 atoi(namelist[i]->d_name));
157 for (i = 0; i < items; i++)
161 threads->nr += items;
174 for (i = 0; i < items; i++)
183 struct thread_map *thread_map__new_all_cpus(void)
185 return __thread_map__new_all_cpus(UINT_MAX);
188 struct thread_map *thread_map__new_by_uid(uid_t uid)
190 return __thread_map__new_all_cpus(uid);
193 struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
196 return thread_map__new_by_pid(pid);
198 if (tid == -1 && uid != UINT_MAX)
199 return thread_map__new_by_uid(uid);
201 return thread_map__new_by_tid(tid);
204 static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
206 struct thread_map *threads = NULL, *nt;
208 int items, total_tasks = 0;
209 struct dirent **namelist = NULL;
211 pid_t pid, prev_pid = INT_MAX;
213 struct str_node *pos;
214 struct strlist_config slist_config = { .dont_dupstr = true, };
215 struct strlist *slist = strlist__new(pid_str, &slist_config);
220 strlist__for_each_entry(pos, slist) {
221 pid = strtol(pos->s, &end_ptr, 10);
223 if (pid == INT_MIN || pid == INT_MAX ||
224 (*end_ptr != '\0' && *end_ptr != ','))
225 goto out_free_threads;
230 sprintf(name, "/proc/%d/task", pid);
231 items = scandir(name, &namelist, filter, NULL);
233 goto out_free_threads;
235 total_tasks += items;
236 nt = thread_map__realloc(threads, total_tasks);
238 goto out_free_namelist;
242 for (i = 0; i < items; i++) {
243 thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
246 threads->nr = total_tasks;
251 strlist__delete(slist);
253 refcount_set(&threads->refcnt, 1);
257 for (i = 0; i < items; i++)
266 struct thread_map *thread_map__new_dummy(void)
268 struct thread_map *threads = thread_map__alloc(1);
270 if (threads != NULL) {
271 thread_map__set_pid(threads, 0, -1);
273 refcount_set(&threads->refcnt, 1);
278 struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
280 struct thread_map *threads = NULL, *nt;
282 pid_t tid, prev_tid = INT_MAX;
284 struct str_node *pos;
285 struct strlist_config slist_config = { .dont_dupstr = true, };
286 struct strlist *slist;
288 /* perf-stat expects threads to be generated even if tid not given */
290 return thread_map__new_dummy();
292 slist = strlist__new(tid_str, &slist_config);
296 strlist__for_each_entry(pos, slist) {
297 tid = strtol(pos->s, &end_ptr, 10);
299 if (tid == INT_MIN || tid == INT_MAX ||
300 (*end_ptr != '\0' && *end_ptr != ','))
301 goto out_free_threads;
307 nt = thread_map__realloc(threads, ntasks);
310 goto out_free_threads;
313 thread_map__set_pid(threads, ntasks - 1, tid);
314 threads->nr = ntasks;
318 refcount_set(&threads->refcnt, 1);
323 strlist__delete(slist);
327 struct thread_map *thread_map__new_str(const char *pid, const char *tid,
328 uid_t uid, bool all_threads)
331 return thread_map__new_by_pid_str(pid);
333 if (!tid && uid != UINT_MAX)
334 return thread_map__new_by_uid(uid);
337 return thread_map__new_all_cpus();
339 return thread_map__new_by_tid_str(tid);
342 static void thread_map__delete(struct thread_map *threads)
347 WARN_ONCE(refcount_read(&threads->refcnt) != 0,
348 "thread map refcnt unbalanced\n");
349 for (i = 0; i < threads->nr; i++)
350 free(thread_map__comm(threads, i));
355 struct thread_map *thread_map__get(struct thread_map *map)
358 refcount_inc(&map->refcnt);
362 void thread_map__put(struct thread_map *map)
364 if (map && refcount_dec_and_test(&map->refcnt))
365 thread_map__delete(map);
368 size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
371 size_t printed = fprintf(fp, "%d thread%s: ",
372 threads->nr, threads->nr > 1 ? "s" : "");
373 for (i = 0; i < threads->nr; ++i)
374 printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
376 return printed + fprintf(fp, "\n");
379 static int get_comm(char **comm, pid_t pid)
385 if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
388 err = filename__read_str(path, comm, &size);
391 * We're reading 16 bytes, while filename__read_str
392 * allocates data per BUFSIZ bytes, so we can safely
393 * mark the end of the string.
403 static void comm_init(struct thread_map *map, int i)
405 pid_t pid = thread_map__pid(map, i);
408 /* dummy pid comm initialization */
410 map->map[i].comm = strdup("dummy");
415 * The comm name is like extra bonus ;-),
416 * so just warn if we fail for any reason.
418 if (get_comm(&comm, pid))
419 pr_warning("Couldn't resolve comm name for pid %d\n", pid);
421 map->map[i].comm = comm;
424 void thread_map__read_comms(struct thread_map *threads)
428 for (i = 0; i < threads->nr; ++i)
429 comm_init(threads, i);
432 static void thread_map__copy_event(struct thread_map *threads,
433 struct thread_map_event *event)
437 threads->nr = (int) event->nr;
439 for (i = 0; i < event->nr; i++) {
440 thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
441 threads->map[i].comm = strndup(event->entries[i].comm, 16);
444 refcount_set(&threads->refcnt, 1);
447 struct thread_map *thread_map__new_event(struct thread_map_event *event)
449 struct thread_map *threads;
451 threads = thread_map__alloc(event->nr);
453 thread_map__copy_event(threads, event);
458 bool thread_map__has(struct thread_map *threads, pid_t pid)
462 for (i = 0; i < threads->nr; ++i) {
463 if (threads->map[i].pid == pid)
470 int thread_map__remove(struct thread_map *threads, int idx)
477 if (idx >= threads->nr)
481 * Free the 'idx' item and shift the rest up.
483 zfree(&threads->map[idx].comm);
485 for (i = idx; i < threads->nr - 1; i++)
486 threads->map[i] = threads->map[i + 1];