Commit | Line | Data |
---|---|---|
fd78260b | 1 | #include <dirent.h> |
0d37aa34 ACM |
2 | #include <limits.h> |
3 | #include <stdbool.h> | |
fd78260b ACM |
4 | #include <stdlib.h> |
5 | #include <stdio.h> | |
0d37aa34 ACM |
6 | #include <sys/types.h> |
7 | #include <sys/stat.h> | |
8 | #include <unistd.h> | |
b52956c9 DA |
9 | #include "strlist.h" |
10 | #include <string.h> | |
792402fd | 11 | #include <api/fs/fs.h> |
186fbb74 | 12 | #include "asm/bug.h" |
fd78260b | 13 | #include "thread_map.h" |
04662523 | 14 | #include "util.h" |
792402fd | 15 | #include "debug.h" |
fd78260b ACM |
16 | |
17 | /* Skip "." and ".." directories */ | |
18 | static int filter(const struct dirent *dir) | |
19 | { | |
20 | if (dir->d_name[0] == '.') | |
21 | return 0; | |
22 | else | |
23 | return 1; | |
24 | } | |
25 | ||
62eea464 JO |
26 | static void thread_map__reset(struct thread_map *map, int start, int nr) |
27 | { | |
28 | size_t size = (nr - start) * sizeof(map->map[0]); | |
29 | ||
30 | memset(&map->map[start], 0, size); | |
31 | } | |
32 | ||
9d7e8c3a JO |
33 | static struct thread_map *thread_map__realloc(struct thread_map *map, int nr) |
34 | { | |
060664f3 | 35 | size_t size = sizeof(*map) + sizeof(map->map[0]) * nr; |
62eea464 JO |
36 | int start = map ? map->nr : 0; |
37 | ||
38 | map = realloc(map, size); | |
39 | /* | |
40 | * We only realloc to add more items, let's reset new items. | |
41 | */ | |
42 | if (map) | |
43 | thread_map__reset(map, start, nr); | |
9d7e8c3a | 44 | |
62eea464 | 45 | return map; |
9d7e8c3a JO |
46 | } |
47 | ||
48 | #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr) | |
49 | ||
fd78260b ACM |
50 | struct thread_map *thread_map__new_by_pid(pid_t pid) |
51 | { | |
52 | struct thread_map *threads; | |
53 | char name[256]; | |
54 | int items; | |
55 | struct dirent **namelist = NULL; | |
56 | int i; | |
57 | ||
58 | sprintf(name, "/proc/%d/task", pid); | |
59 | items = scandir(name, &namelist, filter, NULL); | |
60 | if (items <= 0) | |
0d37aa34 | 61 | return NULL; |
fd78260b | 62 | |
9d7e8c3a | 63 | threads = thread_map__alloc(items); |
fd78260b ACM |
64 | if (threads != NULL) { |
65 | for (i = 0; i < items; i++) | |
e13798c7 | 66 | thread_map__set_pid(threads, i, atoi(namelist[i]->d_name)); |
fd78260b | 67 | threads->nr = items; |
186fbb74 | 68 | atomic_set(&threads->refcnt, 1); |
fd78260b ACM |
69 | } |
70 | ||
71 | for (i=0; i<items; i++) | |
74cf249d | 72 | zfree(&namelist[i]); |
fd78260b ACM |
73 | free(namelist); |
74 | ||
75 | return threads; | |
76 | } | |
77 | ||
78 | struct thread_map *thread_map__new_by_tid(pid_t tid) | |
79 | { | |
9d7e8c3a | 80 | struct thread_map *threads = thread_map__alloc(1); |
fd78260b ACM |
81 | |
82 | if (threads != NULL) { | |
e13798c7 JO |
83 | thread_map__set_pid(threads, 0, tid); |
84 | threads->nr = 1; | |
186fbb74 | 85 | atomic_set(&threads->refcnt, 1); |
fd78260b ACM |
86 | } |
87 | ||
88 | return threads; | |
89 | } | |
90 | ||
0d37aa34 ACM |
91 | struct thread_map *thread_map__new_by_uid(uid_t uid) |
92 | { | |
93 | DIR *proc; | |
94 | int max_threads = 32, items, i; | |
95 | char path[256]; | |
96 | struct dirent dirent, *next, **namelist = NULL; | |
9d7e8c3a JO |
97 | struct thread_map *threads = thread_map__alloc(max_threads); |
98 | ||
0d37aa34 ACM |
99 | if (threads == NULL) |
100 | goto out; | |
101 | ||
102 | proc = opendir("/proc"); | |
103 | if (proc == NULL) | |
104 | goto out_free_threads; | |
105 | ||
106 | threads->nr = 0; | |
186fbb74 | 107 | atomic_set(&threads->refcnt, 1); |
0d37aa34 ACM |
108 | |
109 | while (!readdir_r(proc, &dirent, &next) && next) { | |
110 | char *end; | |
111 | bool grow = false; | |
112 | struct stat st; | |
113 | pid_t pid = strtol(dirent.d_name, &end, 10); | |
114 | ||
115 | if (*end) /* only interested in proper numerical dirents */ | |
116 | continue; | |
117 | ||
118 | snprintf(path, sizeof(path), "/proc/%s", dirent.d_name); | |
119 | ||
120 | if (stat(path, &st) != 0) | |
121 | continue; | |
122 | ||
123 | if (st.st_uid != uid) | |
124 | continue; | |
125 | ||
126 | snprintf(path, sizeof(path), "/proc/%d/task", pid); | |
127 | items = scandir(path, &namelist, filter, NULL); | |
128 | if (items <= 0) | |
129 | goto out_free_closedir; | |
130 | ||
131 | while (threads->nr + items >= max_threads) { | |
132 | max_threads *= 2; | |
133 | grow = true; | |
134 | } | |
135 | ||
136 | if (grow) { | |
137 | struct thread_map *tmp; | |
138 | ||
08ae217b | 139 | tmp = thread_map__realloc(threads, max_threads); |
0d37aa34 ACM |
140 | if (tmp == NULL) |
141 | goto out_free_namelist; | |
142 | ||
143 | threads = tmp; | |
144 | } | |
145 | ||
e13798c7 JO |
146 | for (i = 0; i < items; i++) { |
147 | thread_map__set_pid(threads, threads->nr + i, | |
148 | atoi(namelist[i]->d_name)); | |
149 | } | |
0d37aa34 ACM |
150 | |
151 | for (i = 0; i < items; i++) | |
74cf249d | 152 | zfree(&namelist[i]); |
0d37aa34 ACM |
153 | free(namelist); |
154 | ||
155 | threads->nr += items; | |
156 | } | |
157 | ||
158 | out_closedir: | |
159 | closedir(proc); | |
160 | out: | |
161 | return threads; | |
162 | ||
163 | out_free_threads: | |
164 | free(threads); | |
165 | return NULL; | |
166 | ||
167 | out_free_namelist: | |
168 | for (i = 0; i < items; i++) | |
74cf249d | 169 | zfree(&namelist[i]); |
0d37aa34 ACM |
170 | free(namelist); |
171 | ||
172 | out_free_closedir: | |
04662523 | 173 | zfree(&threads); |
0d37aa34 ACM |
174 | goto out_closedir; |
175 | } | |
176 | ||
177 | struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid) | |
fd78260b ACM |
178 | { |
179 | if (pid != -1) | |
180 | return thread_map__new_by_pid(pid); | |
0d37aa34 ACM |
181 | |
182 | if (tid == -1 && uid != UINT_MAX) | |
183 | return thread_map__new_by_uid(uid); | |
184 | ||
fd78260b ACM |
185 | return thread_map__new_by_tid(tid); |
186 | } | |
187 | ||
b52956c9 DA |
188 | static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) |
189 | { | |
190 | struct thread_map *threads = NULL, *nt; | |
191 | char name[256]; | |
192 | int items, total_tasks = 0; | |
193 | struct dirent **namelist = NULL; | |
194 | int i, j = 0; | |
195 | pid_t pid, prev_pid = INT_MAX; | |
196 | char *end_ptr; | |
197 | struct str_node *pos; | |
4a77e218 ACM |
198 | struct strlist_config slist_config = { .dont_dupstr = true, }; |
199 | struct strlist *slist = strlist__new(pid_str, &slist_config); | |
b52956c9 DA |
200 | |
201 | if (!slist) | |
202 | return NULL; | |
203 | ||
204 | strlist__for_each(pos, slist) { | |
205 | pid = strtol(pos->s, &end_ptr, 10); | |
206 | ||
207 | if (pid == INT_MIN || pid == INT_MAX || | |
208 | (*end_ptr != '\0' && *end_ptr != ',')) | |
209 | goto out_free_threads; | |
210 | ||
211 | if (pid == prev_pid) | |
212 | continue; | |
213 | ||
214 | sprintf(name, "/proc/%d/task", pid); | |
215 | items = scandir(name, &namelist, filter, NULL); | |
216 | if (items <= 0) | |
217 | goto out_free_threads; | |
218 | ||
219 | total_tasks += items; | |
9d7e8c3a | 220 | nt = thread_map__realloc(threads, total_tasks); |
b52956c9 | 221 | if (nt == NULL) |
e8cdd947 | 222 | goto out_free_namelist; |
b52956c9 DA |
223 | |
224 | threads = nt; | |
225 | ||
e8cdd947 | 226 | for (i = 0; i < items; i++) { |
e13798c7 | 227 | thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name)); |
74cf249d | 228 | zfree(&namelist[i]); |
e8cdd947 FBH |
229 | } |
230 | threads->nr = total_tasks; | |
b52956c9 | 231 | free(namelist); |
b52956c9 DA |
232 | } |
233 | ||
234 | out: | |
235 | strlist__delete(slist); | |
186fbb74 JO |
236 | if (threads) |
237 | atomic_set(&threads->refcnt, 1); | |
b52956c9 DA |
238 | return threads; |
239 | ||
e8cdd947 FBH |
240 | out_free_namelist: |
241 | for (i = 0; i < items; i++) | |
74cf249d | 242 | zfree(&namelist[i]); |
e8cdd947 FBH |
243 | free(namelist); |
244 | ||
b52956c9 | 245 | out_free_threads: |
04662523 | 246 | zfree(&threads); |
b52956c9 DA |
247 | goto out; |
248 | } | |
249 | ||
641556c9 ACM |
250 | struct thread_map *thread_map__new_dummy(void) |
251 | { | |
9d7e8c3a | 252 | struct thread_map *threads = thread_map__alloc(1); |
641556c9 ACM |
253 | |
254 | if (threads != NULL) { | |
e13798c7 JO |
255 | thread_map__set_pid(threads, 0, -1); |
256 | threads->nr = 1; | |
186fbb74 | 257 | atomic_set(&threads->refcnt, 1); |
641556c9 ACM |
258 | } |
259 | return threads; | |
260 | } | |
261 | ||
b52956c9 DA |
262 | static struct thread_map *thread_map__new_by_tid_str(const char *tid_str) |
263 | { | |
264 | struct thread_map *threads = NULL, *nt; | |
265 | int ntasks = 0; | |
266 | pid_t tid, prev_tid = INT_MAX; | |
267 | char *end_ptr; | |
268 | struct str_node *pos; | |
4a77e218 | 269 | struct strlist_config slist_config = { .dont_dupstr = true, }; |
b52956c9 DA |
270 | struct strlist *slist; |
271 | ||
272 | /* perf-stat expects threads to be generated even if tid not given */ | |
641556c9 ACM |
273 | if (!tid_str) |
274 | return thread_map__new_dummy(); | |
b52956c9 | 275 | |
4a77e218 | 276 | slist = strlist__new(tid_str, &slist_config); |
b52956c9 DA |
277 | if (!slist) |
278 | return NULL; | |
279 | ||
280 | strlist__for_each(pos, slist) { | |
281 | tid = strtol(pos->s, &end_ptr, 10); | |
282 | ||
283 | if (tid == INT_MIN || tid == INT_MAX || | |
284 | (*end_ptr != '\0' && *end_ptr != ',')) | |
285 | goto out_free_threads; | |
286 | ||
287 | if (tid == prev_tid) | |
288 | continue; | |
289 | ||
290 | ntasks++; | |
9d7e8c3a | 291 | nt = thread_map__realloc(threads, ntasks); |
b52956c9 DA |
292 | |
293 | if (nt == NULL) | |
294 | goto out_free_threads; | |
295 | ||
296 | threads = nt; | |
e13798c7 JO |
297 | thread_map__set_pid(threads, ntasks - 1, tid); |
298 | threads->nr = ntasks; | |
b52956c9 DA |
299 | } |
300 | out: | |
186fbb74 JO |
301 | if (threads) |
302 | atomic_set(&threads->refcnt, 1); | |
b52956c9 DA |
303 | return threads; |
304 | ||
305 | out_free_threads: | |
04662523 | 306 | zfree(&threads); |
7ecb48fd | 307 | strlist__delete(slist); |
b52956c9 DA |
308 | goto out; |
309 | } | |
310 | ||
311 | struct thread_map *thread_map__new_str(const char *pid, const char *tid, | |
312 | uid_t uid) | |
313 | { | |
314 | if (pid) | |
315 | return thread_map__new_by_pid_str(pid); | |
316 | ||
317 | if (!tid && uid != UINT_MAX) | |
318 | return thread_map__new_by_uid(uid); | |
319 | ||
320 | return thread_map__new_by_tid_str(tid); | |
321 | } | |
322 | ||
186fbb74 | 323 | static void thread_map__delete(struct thread_map *threads) |
fd78260b | 324 | { |
186fbb74 | 325 | if (threads) { |
792402fd JO |
326 | int i; |
327 | ||
186fbb74 JO |
328 | WARN_ONCE(atomic_read(&threads->refcnt) != 0, |
329 | "thread map refcnt unbalanced\n"); | |
792402fd JO |
330 | for (i = 0; i < threads->nr; i++) |
331 | free(thread_map__comm(threads, i)); | |
186fbb74 JO |
332 | free(threads); |
333 | } | |
334 | } | |
335 | ||
336 | struct thread_map *thread_map__get(struct thread_map *map) | |
337 | { | |
338 | if (map) | |
339 | atomic_inc(&map->refcnt); | |
340 | return map; | |
341 | } | |
342 | ||
343 | void thread_map__put(struct thread_map *map) | |
344 | { | |
345 | if (map && atomic_dec_and_test(&map->refcnt)) | |
346 | thread_map__delete(map); | |
fd78260b | 347 | } |
9ae7d335 ACM |
348 | |
349 | size_t thread_map__fprintf(struct thread_map *threads, FILE *fp) | |
350 | { | |
351 | int i; | |
352 | size_t printed = fprintf(fp, "%d thread%s: ", | |
353 | threads->nr, threads->nr > 1 ? "s" : ""); | |
354 | for (i = 0; i < threads->nr; ++i) | |
e13798c7 | 355 | printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i)); |
9ae7d335 ACM |
356 | |
357 | return printed + fprintf(fp, "\n"); | |
358 | } | |
792402fd JO |
359 | |
360 | static int get_comm(char **comm, pid_t pid) | |
361 | { | |
362 | char *path; | |
363 | size_t size; | |
364 | int err; | |
365 | ||
366 | if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1) | |
367 | return -ENOMEM; | |
368 | ||
369 | err = filename__read_str(path, comm, &size); | |
370 | if (!err) { | |
371 | /* | |
372 | * We're reading 16 bytes, while filename__read_str | |
373 | * allocates data per BUFSIZ bytes, so we can safely | |
374 | * mark the end of the string. | |
375 | */ | |
376 | (*comm)[size] = 0; | |
377 | rtrim(*comm); | |
378 | } | |
379 | ||
380 | free(path); | |
381 | return err; | |
382 | } | |
383 | ||
384 | static void comm_init(struct thread_map *map, int i) | |
385 | { | |
386 | pid_t pid = thread_map__pid(map, i); | |
387 | char *comm = NULL; | |
388 | ||
389 | /* dummy pid comm initialization */ | |
390 | if (pid == -1) { | |
391 | map->map[i].comm = strdup("dummy"); | |
392 | return; | |
393 | } | |
394 | ||
395 | /* | |
396 | * The comm name is like extra bonus ;-), | |
397 | * so just warn if we fail for any reason. | |
398 | */ | |
399 | if (get_comm(&comm, pid)) | |
400 | pr_warning("Couldn't resolve comm name for pid %d\n", pid); | |
401 | ||
402 | map->map[i].comm = comm; | |
403 | } | |
404 | ||
405 | void thread_map__read_comms(struct thread_map *threads) | |
406 | { | |
407 | int i; | |
408 | ||
409 | for (i = 0; i < threads->nr; ++i) | |
410 | comm_init(threads, i); | |
411 | } |