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);
37 static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
39 size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
40 int start = map ? map->nr : 0;
42 map = realloc(map, size);
44 * We only realloc to add more items, let's reset new items.
47 thread_map__reset(map, start, nr);
52 #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
54 struct thread_map *thread_map__new_by_pid(pid_t pid)
56 struct thread_map *threads;
59 struct dirent **namelist = NULL;
62 sprintf(name, "/proc/%d/task", pid);
63 items = scandir(name, &namelist, filter, NULL);
67 threads = thread_map__alloc(items);
68 if (threads != NULL) {
69 for (i = 0; i < items; i++)
70 thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
72 refcount_set(&threads->refcnt, 1);
75 for (i=0; i<items; i++)
82 struct thread_map *thread_map__new_by_tid(pid_t tid)
84 struct thread_map *threads = thread_map__alloc(1);
86 if (threads != NULL) {
87 thread_map__set_pid(threads, 0, tid);
89 refcount_set(&threads->refcnt, 1);
95 struct thread_map *thread_map__new_by_uid(uid_t uid)
98 int max_threads = 32, items, i;
99 char path[NAME_MAX + 1 + 6];
100 struct dirent *dirent, **namelist = NULL;
101 struct thread_map *threads = thread_map__alloc(max_threads);
106 proc = opendir("/proc");
108 goto out_free_threads;
111 refcount_set(&threads->refcnt, 1);
113 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 (stat(path, &st) != 0)
127 if (st.st_uid != uid)
130 snprintf(path, sizeof(path), "/proc/%d/task", pid);
131 items = scandir(path, &namelist, filter, NULL);
133 goto out_free_closedir;
135 while (threads->nr + items >= max_threads) {
141 struct thread_map *tmp;
143 tmp = thread_map__realloc(threads, max_threads);
145 goto out_free_namelist;
150 for (i = 0; i < items; i++) {
151 thread_map__set_pid(threads, threads->nr + i,
152 atoi(namelist[i]->d_name));
155 for (i = 0; i < items; i++)
159 threads->nr += items;
172 for (i = 0; i < items; i++)
181 struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
184 return thread_map__new_by_pid(pid);
186 if (tid == -1 && uid != UINT_MAX)
187 return thread_map__new_by_uid(uid);
189 return thread_map__new_by_tid(tid);
192 static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
194 struct thread_map *threads = NULL, *nt;
196 int items, total_tasks = 0;
197 struct dirent **namelist = NULL;
199 pid_t pid, prev_pid = INT_MAX;
201 struct str_node *pos;
202 struct strlist_config slist_config = { .dont_dupstr = true, };
203 struct strlist *slist = strlist__new(pid_str, &slist_config);
208 strlist__for_each_entry(pos, slist) {
209 pid = strtol(pos->s, &end_ptr, 10);
211 if (pid == INT_MIN || pid == INT_MAX ||
212 (*end_ptr != '\0' && *end_ptr != ','))
213 goto out_free_threads;
218 sprintf(name, "/proc/%d/task", pid);
219 items = scandir(name, &namelist, filter, NULL);
221 goto out_free_threads;
223 total_tasks += items;
224 nt = thread_map__realloc(threads, total_tasks);
226 goto out_free_namelist;
230 for (i = 0; i < items; i++) {
231 thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
234 threads->nr = total_tasks;
239 strlist__delete(slist);
241 refcount_set(&threads->refcnt, 1);
245 for (i = 0; i < items; i++)
254 struct thread_map *thread_map__new_dummy(void)
256 struct thread_map *threads = thread_map__alloc(1);
258 if (threads != NULL) {
259 thread_map__set_pid(threads, 0, -1);
261 refcount_set(&threads->refcnt, 1);
266 struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
268 struct thread_map *threads = NULL, *nt;
270 pid_t tid, prev_tid = INT_MAX;
272 struct str_node *pos;
273 struct strlist_config slist_config = { .dont_dupstr = true, };
274 struct strlist *slist;
276 /* perf-stat expects threads to be generated even if tid not given */
278 return thread_map__new_dummy();
280 slist = strlist__new(tid_str, &slist_config);
284 strlist__for_each_entry(pos, slist) {
285 tid = strtol(pos->s, &end_ptr, 10);
287 if (tid == INT_MIN || tid == INT_MAX ||
288 (*end_ptr != '\0' && *end_ptr != ','))
289 goto out_free_threads;
295 nt = thread_map__realloc(threads, ntasks);
298 goto out_free_threads;
301 thread_map__set_pid(threads, ntasks - 1, tid);
302 threads->nr = ntasks;
306 refcount_set(&threads->refcnt, 1);
311 strlist__delete(slist);
315 struct thread_map *thread_map__new_str(const char *pid, const char *tid,
319 return thread_map__new_by_pid_str(pid);
321 if (!tid && uid != UINT_MAX)
322 return thread_map__new_by_uid(uid);
324 return thread_map__new_by_tid_str(tid);
327 static void thread_map__delete(struct thread_map *threads)
332 WARN_ONCE(refcount_read(&threads->refcnt) != 0,
333 "thread map refcnt unbalanced\n");
334 for (i = 0; i < threads->nr; i++)
335 free(thread_map__comm(threads, i));
340 struct thread_map *thread_map__get(struct thread_map *map)
343 refcount_inc(&map->refcnt);
347 void thread_map__put(struct thread_map *map)
349 if (map && refcount_dec_and_test(&map->refcnt))
350 thread_map__delete(map);
353 size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
356 size_t printed = fprintf(fp, "%d thread%s: ",
357 threads->nr, threads->nr > 1 ? "s" : "");
358 for (i = 0; i < threads->nr; ++i)
359 printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
361 return printed + fprintf(fp, "\n");
364 static int get_comm(char **comm, pid_t pid)
370 if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
373 err = filename__read_str(path, comm, &size);
376 * We're reading 16 bytes, while filename__read_str
377 * allocates data per BUFSIZ bytes, so we can safely
378 * mark the end of the string.
388 static void comm_init(struct thread_map *map, int i)
390 pid_t pid = thread_map__pid(map, i);
393 /* dummy pid comm initialization */
395 map->map[i].comm = strdup("dummy");
400 * The comm name is like extra bonus ;-),
401 * so just warn if we fail for any reason.
403 if (get_comm(&comm, pid))
404 pr_warning("Couldn't resolve comm name for pid %d\n", pid);
406 map->map[i].comm = comm;
409 void thread_map__read_comms(struct thread_map *threads)
413 for (i = 0; i < threads->nr; ++i)
414 comm_init(threads, i);
417 static void thread_map__copy_event(struct thread_map *threads,
418 struct thread_map_event *event)
422 threads->nr = (int) event->nr;
424 for (i = 0; i < event->nr; i++) {
425 thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
426 threads->map[i].comm = strndup(event->entries[i].comm, 16);
429 refcount_set(&threads->refcnt, 1);
432 struct thread_map *thread_map__new_event(struct thread_map_event *event)
434 struct thread_map *threads;
436 threads = thread_map__alloc(event->nr);
438 thread_map__copy_event(threads, event);
443 bool thread_map__has(struct thread_map *threads, pid_t pid)
447 for (i = 0; i < threads->nr; ++i) {
448 if (threads->map[i].pid == pid)
455 int thread_map__remove(struct thread_map *threads, int idx)
462 if (idx >= threads->nr)
466 * Free the 'idx' item and shift the rest up.
468 free(threads->map[idx].comm);
470 for (i = idx; i < threads->nr - 1; i++)
471 threads->map[i] = threads->map[i + 1];