thread_map.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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[256];
  80. struct dirent dirent, *next, **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 (!readdir_r(proc, &dirent, &next) && next) {
  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 *slist = strlist__new(false, pid_str);
  159. if (!slist)
  160. return NULL;
  161. strlist__for_each(pos, slist) {
  162. pid = strtol(pos->s, &end_ptr, 10);
  163. if (pid == INT_MIN || pid == INT_MAX ||
  164. (*end_ptr != '\0' && *end_ptr != ','))
  165. goto out_free_threads;
  166. if (pid == prev_pid)
  167. continue;
  168. sprintf(name, "/proc/%d/task", pid);
  169. items = scandir(name, &namelist, filter, NULL);
  170. if (items <= 0)
  171. goto out_free_threads;
  172. total_tasks += items;
  173. nt = thread_map__realloc(threads, total_tasks);
  174. if (nt == NULL)
  175. goto out_free_namelist;
  176. threads = nt;
  177. for (i = 0; i < items; i++) {
  178. thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
  179. zfree(&namelist[i]);
  180. }
  181. threads->nr = total_tasks;
  182. free(namelist);
  183. }
  184. out:
  185. strlist__delete(slist);
  186. if (threads)
  187. atomic_set(&threads->refcnt, 1);
  188. return threads;
  189. out_free_namelist:
  190. for (i = 0; i < items; i++)
  191. zfree(&namelist[i]);
  192. free(namelist);
  193. out_free_threads:
  194. zfree(&threads);
  195. goto out;
  196. }
  197. struct thread_map *thread_map__new_dummy(void)
  198. {
  199. struct thread_map *threads = thread_map__alloc(1);
  200. if (threads != NULL) {
  201. thread_map__set_pid(threads, 0, -1);
  202. threads->nr = 1;
  203. atomic_set(&threads->refcnt, 1);
  204. }
  205. return threads;
  206. }
  207. static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
  208. {
  209. struct thread_map *threads = NULL, *nt;
  210. int ntasks = 0;
  211. pid_t tid, prev_tid = INT_MAX;
  212. char *end_ptr;
  213. struct str_node *pos;
  214. struct strlist *slist;
  215. /* perf-stat expects threads to be generated even if tid not given */
  216. if (!tid_str)
  217. return thread_map__new_dummy();
  218. slist = strlist__new(false, tid_str);
  219. if (!slist)
  220. return NULL;
  221. strlist__for_each(pos, slist) {
  222. tid = strtol(pos->s, &end_ptr, 10);
  223. if (tid == INT_MIN || tid == INT_MAX ||
  224. (*end_ptr != '\0' && *end_ptr != ','))
  225. goto out_free_threads;
  226. if (tid == prev_tid)
  227. continue;
  228. ntasks++;
  229. nt = thread_map__realloc(threads, ntasks);
  230. if (nt == NULL)
  231. goto out_free_threads;
  232. threads = nt;
  233. thread_map__set_pid(threads, ntasks - 1, tid);
  234. threads->nr = ntasks;
  235. }
  236. out:
  237. if (threads)
  238. atomic_set(&threads->refcnt, 1);
  239. return threads;
  240. out_free_threads:
  241. zfree(&threads);
  242. goto out;
  243. }
  244. struct thread_map *thread_map__new_str(const char *pid, const char *tid,
  245. uid_t uid)
  246. {
  247. if (pid)
  248. return thread_map__new_by_pid_str(pid);
  249. if (!tid && uid != UINT_MAX)
  250. return thread_map__new_by_uid(uid);
  251. return thread_map__new_by_tid_str(tid);
  252. }
  253. static void thread_map__delete(struct thread_map *threads)
  254. {
  255. if (threads) {
  256. int i;
  257. WARN_ONCE(atomic_read(&threads->refcnt) != 0,
  258. "thread map refcnt unbalanced\n");
  259. for (i = 0; i < threads->nr; i++)
  260. free(thread_map__comm(threads, i));
  261. free(threads);
  262. }
  263. }
  264. struct thread_map *thread_map__get(struct thread_map *map)
  265. {
  266. if (map)
  267. atomic_inc(&map->refcnt);
  268. return map;
  269. }
  270. void thread_map__put(struct thread_map *map)
  271. {
  272. if (map && atomic_dec_and_test(&map->refcnt))
  273. thread_map__delete(map);
  274. }
  275. size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
  276. {
  277. int i;
  278. size_t printed = fprintf(fp, "%d thread%s: ",
  279. threads->nr, threads->nr > 1 ? "s" : "");
  280. for (i = 0; i < threads->nr; ++i)
  281. printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
  282. return printed + fprintf(fp, "\n");
  283. }
  284. static int get_comm(char **comm, pid_t pid)
  285. {
  286. char *path;
  287. size_t size;
  288. int err;
  289. if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
  290. return -ENOMEM;
  291. err = filename__read_str(path, comm, &size);
  292. if (!err) {
  293. /*
  294. * We're reading 16 bytes, while filename__read_str
  295. * allocates data per BUFSIZ bytes, so we can safely
  296. * mark the end of the string.
  297. */
  298. (*comm)[size] = 0;
  299. rtrim(*comm);
  300. }
  301. free(path);
  302. return err;
  303. }
  304. static void comm_init(struct thread_map *map, int i)
  305. {
  306. pid_t pid = thread_map__pid(map, i);
  307. char *comm = NULL;
  308. /* dummy pid comm initialization */
  309. if (pid == -1) {
  310. map->map[i].comm = strdup("dummy");
  311. return;
  312. }
  313. /*
  314. * The comm name is like extra bonus ;-),
  315. * so just warn if we fail for any reason.
  316. */
  317. if (get_comm(&comm, pid))
  318. pr_warning("Couldn't resolve comm name for pid %d\n", pid);
  319. map->map[i].comm = comm;
  320. }
  321. void thread_map__read_comms(struct thread_map *threads)
  322. {
  323. int i;
  324. for (i = 0; i < threads->nr; ++i)
  325. comm_init(threads, i);
  326. }