thread_map.c 9.8 KB

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