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"
21 #include <internal/threadmap.h>
23 /* Skip "." and ".." directories */
24 static int filter(const struct dirent *dir)
26 if (dir->d_name[0] == '.')
32 #define thread_map__alloc(__nr) perf_thread_map__realloc(NULL, __nr)
34 struct perf_thread_map *thread_map__new_by_pid(pid_t pid)
36 struct perf_thread_map *threads;
39 struct dirent **namelist = NULL;
42 sprintf(name, "/proc/%d/task", pid);
43 items = scandir(name, &namelist, filter, NULL);
47 threads = thread_map__alloc(items);
48 if (threads != NULL) {
49 for (i = 0; i < items; i++)
50 perf_thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
52 refcount_set(&threads->refcnt, 1);
55 for (i=0; i<items; i++)
62 struct perf_thread_map *thread_map__new_by_tid(pid_t tid)
64 struct perf_thread_map *threads = thread_map__alloc(1);
66 if (threads != NULL) {
67 perf_thread_map__set_pid(threads, 0, tid);
69 refcount_set(&threads->refcnt, 1);
75 static struct perf_thread_map *__thread_map__new_all_cpus(uid_t uid)
78 int max_threads = 32, items, i;
79 char path[NAME_MAX + 1 + 6];
80 struct dirent *dirent, **namelist = NULL;
81 struct perf_thread_map *threads = thread_map__alloc(max_threads);
86 proc = opendir("/proc");
88 goto out_free_threads;
91 refcount_set(&threads->refcnt, 1);
93 while ((dirent = readdir(proc)) != NULL) {
96 pid_t pid = strtol(dirent->d_name, &end, 10);
98 if (*end) /* only interested in proper numerical dirents */
101 snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
103 if (uid != UINT_MAX) {
106 if (stat(path, &st) != 0 || st.st_uid != uid)
110 snprintf(path, sizeof(path), "/proc/%d/task", pid);
111 items = scandir(path, &namelist, filter, NULL);
113 goto out_free_closedir;
115 while (threads->nr + items >= max_threads) {
121 struct perf_thread_map *tmp;
123 tmp = perf_thread_map__realloc(threads, max_threads);
125 goto out_free_namelist;
130 for (i = 0; i < items; i++) {
131 perf_thread_map__set_pid(threads, threads->nr + i,
132 atoi(namelist[i]->d_name));
135 for (i = 0; i < items; i++)
139 threads->nr += items;
152 for (i = 0; i < items; i++)
161 struct perf_thread_map *thread_map__new_all_cpus(void)
163 return __thread_map__new_all_cpus(UINT_MAX);
166 struct perf_thread_map *thread_map__new_by_uid(uid_t uid)
168 return __thread_map__new_all_cpus(uid);
171 struct perf_thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
174 return thread_map__new_by_pid(pid);
176 if (tid == -1 && uid != UINT_MAX)
177 return thread_map__new_by_uid(uid);
179 return thread_map__new_by_tid(tid);
182 static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
184 struct perf_thread_map *threads = NULL, *nt;
186 int items, total_tasks = 0;
187 struct dirent **namelist = NULL;
189 pid_t pid, prev_pid = INT_MAX;
191 struct str_node *pos;
192 struct strlist_config slist_config = { .dont_dupstr = true, };
193 struct strlist *slist = strlist__new(pid_str, &slist_config);
198 strlist__for_each_entry(pos, slist) {
199 pid = strtol(pos->s, &end_ptr, 10);
201 if (pid == INT_MIN || pid == INT_MAX ||
202 (*end_ptr != '\0' && *end_ptr != ','))
203 goto out_free_threads;
208 sprintf(name, "/proc/%d/task", pid);
209 items = scandir(name, &namelist, filter, NULL);
211 goto out_free_threads;
213 total_tasks += items;
214 nt = perf_thread_map__realloc(threads, total_tasks);
216 goto out_free_namelist;
220 for (i = 0; i < items; i++) {
221 perf_thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
224 threads->nr = total_tasks;
229 strlist__delete(slist);
231 refcount_set(&threads->refcnt, 1);
235 for (i = 0; i < items; i++)
244 struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
246 struct perf_thread_map *threads = NULL, *nt;
248 pid_t tid, prev_tid = INT_MAX;
250 struct str_node *pos;
251 struct strlist_config slist_config = { .dont_dupstr = true, };
252 struct strlist *slist;
254 /* perf-stat expects threads to be generated even if tid not given */
256 return perf_thread_map__new_dummy();
258 slist = strlist__new(tid_str, &slist_config);
262 strlist__for_each_entry(pos, slist) {
263 tid = strtol(pos->s, &end_ptr, 10);
265 if (tid == INT_MIN || tid == INT_MAX ||
266 (*end_ptr != '\0' && *end_ptr != ','))
267 goto out_free_threads;
273 nt = perf_thread_map__realloc(threads, ntasks);
276 goto out_free_threads;
279 perf_thread_map__set_pid(threads, ntasks - 1, tid);
280 threads->nr = ntasks;
284 refcount_set(&threads->refcnt, 1);
289 strlist__delete(slist);
293 struct perf_thread_map *thread_map__new_str(const char *pid, const char *tid,
294 uid_t uid, bool all_threads)
297 return thread_map__new_by_pid_str(pid);
299 if (!tid && uid != UINT_MAX)
300 return thread_map__new_by_uid(uid);
303 return thread_map__new_all_cpus();
305 return thread_map__new_by_tid_str(tid);
308 size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp)
311 size_t printed = fprintf(fp, "%d thread%s: ",
312 threads->nr, threads->nr > 1 ? "s" : "");
313 for (i = 0; i < threads->nr; ++i)
314 printed += fprintf(fp, "%s%d", i ? ", " : "", perf_thread_map__pid(threads, i));
316 return printed + fprintf(fp, "\n");
319 static int get_comm(char **comm, pid_t pid)
325 if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
328 err = filename__read_str(path, comm, &size);
331 * We're reading 16 bytes, while filename__read_str
332 * allocates data per BUFSIZ bytes, so we can safely
333 * mark the end of the string.
343 static void comm_init(struct perf_thread_map *map, int i)
345 pid_t pid = perf_thread_map__pid(map, i);
348 /* dummy pid comm initialization */
350 map->map[i].comm = strdup("dummy");
355 * The comm name is like extra bonus ;-),
356 * so just warn if we fail for any reason.
358 if (get_comm(&comm, pid))
359 pr_warning("Couldn't resolve comm name for pid %d\n", pid);
361 map->map[i].comm = comm;
364 void thread_map__read_comms(struct perf_thread_map *threads)
368 for (i = 0; i < threads->nr; ++i)
369 comm_init(threads, i);
372 static void thread_map__copy_event(struct perf_thread_map *threads,
373 struct perf_record_thread_map *event)
377 threads->nr = (int) event->nr;
379 for (i = 0; i < event->nr; i++) {
380 perf_thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
381 threads->map[i].comm = strndup(event->entries[i].comm, 16);
384 refcount_set(&threads->refcnt, 1);
387 struct perf_thread_map *thread_map__new_event(struct perf_record_thread_map *event)
389 struct perf_thread_map *threads;
391 threads = thread_map__alloc(event->nr);
393 thread_map__copy_event(threads, event);
398 bool thread_map__has(struct perf_thread_map *threads, pid_t pid)
402 for (i = 0; i < threads->nr; ++i) {
403 if (threads->map[i].pid == pid)
410 int thread_map__remove(struct perf_thread_map *threads, int idx)
417 if (idx >= threads->nr)
421 * Free the 'idx' item and shift the rest up.
423 zfree(&threads->map[idx].comm);
425 for (i = idx; i < threads->nr - 1; i++)
426 threads->map[i] = threads->map[i + 1];