thread_map.c 8.3 KB

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