thread_map.c 9.5 KB

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