thread_map.c 9.0 KB

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