11 #include <api/fs/fs.h>
13 #include "thread_map.h"
17 /* Skip "." and ".." directories */
18 static int filter(const struct dirent *dir)
20 if (dir->d_name[0] == '.')
26 static void thread_map__reset(struct thread_map *map, int start, int nr)
28 size_t size = (nr - start) * sizeof(map->map[0]);
30 memset(&map->map[start], 0, size);
33 static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
35 size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
36 int start = map ? map->nr : 0;
38 map = realloc(map, size);
40 * We only realloc to add more items, let's reset new items.
43 thread_map__reset(map, start, nr);
48 #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
50 struct thread_map *thread_map__new_by_pid(pid_t pid)
52 struct thread_map *threads;
55 struct dirent **namelist = NULL;
58 sprintf(name, "/proc/%d/task", pid);
59 items = scandir(name, &namelist, filter, NULL);
63 threads = thread_map__alloc(items);
64 if (threads != NULL) {
65 for (i = 0; i < items; i++)
66 thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
68 atomic_set(&threads->refcnt, 1);
71 for (i=0; i<items; i++)
78 struct thread_map *thread_map__new_by_tid(pid_t tid)
80 struct thread_map *threads = thread_map__alloc(1);
82 if (threads != NULL) {
83 thread_map__set_pid(threads, 0, tid);
85 atomic_set(&threads->refcnt, 1);
91 struct thread_map *thread_map__new_by_uid(uid_t uid)
94 int max_threads = 32, items, i;
96 struct dirent dirent, *next, **namelist = NULL;
97 struct thread_map *threads = thread_map__alloc(max_threads);
102 proc = opendir("/proc");
104 goto out_free_threads;
107 atomic_set(&threads->refcnt, 1);
109 while (!readdir_r(proc, &dirent, &next) && next) {
113 pid_t pid = strtol(dirent.d_name, &end, 10);
115 if (*end) /* only interested in proper numerical dirents */
118 snprintf(path, sizeof(path), "/proc/%s", dirent.d_name);
120 if (stat(path, &st) != 0)
123 if (st.st_uid != uid)
126 snprintf(path, sizeof(path), "/proc/%d/task", pid);
127 items = scandir(path, &namelist, filter, NULL);
129 goto out_free_closedir;
131 while (threads->nr + items >= max_threads) {
137 struct thread_map *tmp;
139 tmp = thread_map__realloc(threads, max_threads);
141 goto out_free_namelist;
146 for (i = 0; i < items; i++) {
147 thread_map__set_pid(threads, threads->nr + i,
148 atoi(namelist[i]->d_name));
151 for (i = 0; i < items; i++)
155 threads->nr += items;
168 for (i = 0; i < items; i++)
177 struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
180 return thread_map__new_by_pid(pid);
182 if (tid == -1 && uid != UINT_MAX)
183 return thread_map__new_by_uid(uid);
185 return thread_map__new_by_tid(tid);
188 static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
190 struct thread_map *threads = NULL, *nt;
192 int items, total_tasks = 0;
193 struct dirent **namelist = NULL;
195 pid_t pid, prev_pid = INT_MAX;
197 struct str_node *pos;
198 struct strlist *slist = strlist__new(false, pid_str);
203 strlist__for_each(pos, slist) {
204 pid = strtol(pos->s, &end_ptr, 10);
206 if (pid == INT_MIN || pid == INT_MAX ||
207 (*end_ptr != '\0' && *end_ptr != ','))
208 goto out_free_threads;
213 sprintf(name, "/proc/%d/task", pid);
214 items = scandir(name, &namelist, filter, NULL);
216 goto out_free_threads;
218 total_tasks += items;
219 nt = thread_map__realloc(threads, total_tasks);
221 goto out_free_namelist;
225 for (i = 0; i < items; i++) {
226 thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
229 threads->nr = total_tasks;
234 strlist__delete(slist);
236 atomic_set(&threads->refcnt, 1);
240 for (i = 0; i < items; i++)
249 struct thread_map *thread_map__new_dummy(void)
251 struct thread_map *threads = thread_map__alloc(1);
253 if (threads != NULL) {
254 thread_map__set_pid(threads, 0, -1);
256 atomic_set(&threads->refcnt, 1);
261 static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
263 struct thread_map *threads = NULL, *nt;
265 pid_t tid, prev_tid = INT_MAX;
267 struct str_node *pos;
268 struct strlist *slist;
270 /* perf-stat expects threads to be generated even if tid not given */
272 return thread_map__new_dummy();
274 slist = strlist__new(false, tid_str);
278 strlist__for_each(pos, slist) {
279 tid = strtol(pos->s, &end_ptr, 10);
281 if (tid == INT_MIN || tid == INT_MAX ||
282 (*end_ptr != '\0' && *end_ptr != ','))
283 goto out_free_threads;
289 nt = thread_map__realloc(threads, ntasks);
292 goto out_free_threads;
295 thread_map__set_pid(threads, ntasks - 1, tid);
296 threads->nr = ntasks;
300 atomic_set(&threads->refcnt, 1);
308 struct thread_map *thread_map__new_str(const char *pid, const char *tid,
312 return thread_map__new_by_pid_str(pid);
314 if (!tid && uid != UINT_MAX)
315 return thread_map__new_by_uid(uid);
317 return thread_map__new_by_tid_str(tid);
320 static void thread_map__delete(struct thread_map *threads)
325 WARN_ONCE(atomic_read(&threads->refcnt) != 0,
326 "thread map refcnt unbalanced\n");
327 for (i = 0; i < threads->nr; i++)
328 free(thread_map__comm(threads, i));
333 struct thread_map *thread_map__get(struct thread_map *map)
336 atomic_inc(&map->refcnt);
340 void thread_map__put(struct thread_map *map)
342 if (map && atomic_dec_and_test(&map->refcnt))
343 thread_map__delete(map);
346 size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
349 size_t printed = fprintf(fp, "%d thread%s: ",
350 threads->nr, threads->nr > 1 ? "s" : "");
351 for (i = 0; i < threads->nr; ++i)
352 printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
354 return printed + fprintf(fp, "\n");
357 static int get_comm(char **comm, pid_t pid)
363 if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
366 err = filename__read_str(path, comm, &size);
369 * We're reading 16 bytes, while filename__read_str
370 * allocates data per BUFSIZ bytes, so we can safely
371 * mark the end of the string.
381 static void comm_init(struct thread_map *map, int i)
383 pid_t pid = thread_map__pid(map, i);
386 /* dummy pid comm initialization */
388 map->map[i].comm = strdup("dummy");
393 * The comm name is like extra bonus ;-),
394 * so just warn if we fail for any reason.
396 if (get_comm(&comm, pid))
397 pr_warning("Couldn't resolve comm name for pid %d\n", pid);
399 map->map[i].comm = comm;
402 void thread_map__read_comms(struct thread_map *threads)
406 for (i = 0; i < threads->nr; ++i)
407 comm_init(threads, i);