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(void)
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/%d/task", pid);
102 items = scandir(path, &namelist, filter, NULL);
104 pr_debug("scandir for %d returned empty, skipping\n", pid);
107 while (threads->nr + items >= max_threads) {
113 struct perf_thread_map *tmp;
115 tmp = perf_thread_map__realloc(threads, max_threads);
117 goto out_free_namelist;
122 for (i = 0; i < items; i++) {
123 perf_thread_map__set_pid(threads, threads->nr + i,
124 atoi(namelist[i]->d_name));
127 for (i = 0; i < items; i++)
131 threads->nr += items;
144 for (i = 0; i < items; i++)
151 struct perf_thread_map *thread_map__new(pid_t pid, pid_t tid)
154 return thread_map__new_by_pid(pid);
156 return thread_map__new_by_tid(tid);
159 static struct perf_thread_map *thread_map__new_by_pid_str(const char *pid_str)
161 struct perf_thread_map *threads = NULL, *nt;
163 int items, total_tasks = 0;
164 struct dirent **namelist = NULL;
166 pid_t pid, prev_pid = INT_MAX;
168 struct str_node *pos;
169 struct strlist_config slist_config = { .dont_dupstr = true, };
170 struct strlist *slist = strlist__new(pid_str, &slist_config);
175 strlist__for_each_entry(pos, slist) {
176 pid = strtol(pos->s, &end_ptr, 10);
178 if (pid == INT_MIN || pid == INT_MAX ||
179 (*end_ptr != '\0' && *end_ptr != ','))
180 goto out_free_threads;
185 sprintf(name, "/proc/%d/task", pid);
186 items = scandir(name, &namelist, filter, NULL);
188 goto out_free_threads;
190 total_tasks += items;
191 nt = perf_thread_map__realloc(threads, total_tasks);
193 goto out_free_namelist;
197 for (i = 0; i < items; i++) {
198 perf_thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
201 threads->nr = total_tasks;
206 strlist__delete(slist);
208 refcount_set(&threads->refcnt, 1);
212 for (i = 0; i < items; i++)
221 struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
223 struct perf_thread_map *threads = NULL, *nt;
225 pid_t tid, prev_tid = INT_MAX;
227 struct str_node *pos;
228 struct strlist_config slist_config = { .dont_dupstr = true, };
229 struct strlist *slist;
231 /* perf-stat expects threads to be generated even if tid not given */
233 return perf_thread_map__new_dummy();
235 slist = strlist__new(tid_str, &slist_config);
239 strlist__for_each_entry(pos, slist) {
240 tid = strtol(pos->s, &end_ptr, 10);
242 if (tid == INT_MIN || tid == INT_MAX ||
243 (*end_ptr != '\0' && *end_ptr != ','))
244 goto out_free_threads;
250 nt = perf_thread_map__realloc(threads, ntasks);
253 goto out_free_threads;
256 perf_thread_map__set_pid(threads, ntasks - 1, tid);
257 threads->nr = ntasks;
260 strlist__delete(slist);
262 refcount_set(&threads->refcnt, 1);
270 struct perf_thread_map *thread_map__new_str(const char *pid, const char *tid, bool all_threads)
273 return thread_map__new_by_pid_str(pid);
276 return thread_map__new_all_cpus();
278 return thread_map__new_by_tid_str(tid);
281 size_t thread_map__fprintf(struct perf_thread_map *threads, FILE *fp)
284 size_t printed = fprintf(fp, "%d thread%s: ",
285 threads->nr, threads->nr > 1 ? "s" : "");
286 for (i = 0; i < threads->nr; ++i)
287 printed += fprintf(fp, "%s%d", i ? ", " : "", perf_thread_map__pid(threads, i));
289 return printed + fprintf(fp, "\n");
292 static int get_comm(char **comm, pid_t pid)
298 if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
301 err = filename__read_str(path, comm, &size);
304 * We're reading 16 bytes, while filename__read_str
305 * allocates data per BUFSIZ bytes, so we can safely
306 * mark the end of the string.
316 static void comm_init(struct perf_thread_map *map, int i)
318 pid_t pid = perf_thread_map__pid(map, i);
321 /* dummy pid comm initialization */
323 map->map[i].comm = strdup("dummy");
328 * The comm name is like extra bonus ;-),
329 * so just warn if we fail for any reason.
331 if (get_comm(&comm, pid))
332 pr_warning("Couldn't resolve comm name for pid %d\n", pid);
334 map->map[i].comm = comm;
337 void thread_map__read_comms(struct perf_thread_map *threads)
341 for (i = 0; i < threads->nr; ++i)
342 comm_init(threads, i);
345 static void thread_map__copy_event(struct perf_thread_map *threads,
346 struct perf_record_thread_map *event)
350 threads->nr = (int) event->nr;
352 for (i = 0; i < event->nr; i++) {
353 perf_thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
354 threads->map[i].comm = strndup(event->entries[i].comm, 16);
357 refcount_set(&threads->refcnt, 1);
360 struct perf_thread_map *thread_map__new_event(struct perf_record_thread_map *event)
362 struct perf_thread_map *threads;
364 threads = thread_map__alloc(event->nr);
366 thread_map__copy_event(threads, event);
371 bool thread_map__has(struct perf_thread_map *threads, pid_t pid)
375 for (i = 0; i < threads->nr; ++i) {
376 if (threads->map[i].pid == pid)
383 int thread_map__remove(struct perf_thread_map *threads, int idx)
390 if (idx >= threads->nr)
394 * Free the 'idx' item and shift the rest up.
396 zfree(&threads->map[idx].comm);
398 for (i = idx; i < threads->nr - 1; i++)
399 threads->map[i] = threads->map[i + 1];