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