thread_map.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "thread_map.h"
  12. /* Skip "." and ".." directories */
  13. static int filter(const struct dirent *dir)
  14. {
  15. if (dir->d_name[0] == '.')
  16. return 0;
  17. else
  18. return 1;
  19. }
  20. struct thread_map *thread_map__new_by_pid(pid_t pid)
  21. {
  22. struct thread_map *threads;
  23. char name[256];
  24. int items;
  25. struct dirent **namelist = NULL;
  26. int i;
  27. sprintf(name, "/proc/%d/task", pid);
  28. items = scandir(name, &namelist, filter, NULL);
  29. if (items <= 0)
  30. return NULL;
  31. threads = malloc(sizeof(*threads) + sizeof(pid_t) * items);
  32. if (threads != NULL) {
  33. for (i = 0; i < items; i++)
  34. threads->map[i] = atoi(namelist[i]->d_name);
  35. threads->nr = items;
  36. }
  37. for (i=0; i<items; i++)
  38. free(namelist[i]);
  39. free(namelist);
  40. return threads;
  41. }
  42. struct thread_map *thread_map__new_by_tid(pid_t tid)
  43. {
  44. struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t));
  45. if (threads != NULL) {
  46. threads->map[0] = tid;
  47. threads->nr = 1;
  48. }
  49. return threads;
  50. }
  51. struct thread_map *thread_map__new_by_uid(uid_t uid)
  52. {
  53. DIR *proc;
  54. int max_threads = 32, items, i;
  55. char path[256];
  56. struct dirent dirent, *next, **namelist = NULL;
  57. struct thread_map *threads = malloc(sizeof(*threads) +
  58. max_threads * sizeof(pid_t));
  59. if (threads == NULL)
  60. goto out;
  61. proc = opendir("/proc");
  62. if (proc == NULL)
  63. goto out_free_threads;
  64. threads->nr = 0;
  65. while (!readdir_r(proc, &dirent, &next) && next) {
  66. char *end;
  67. bool grow = false;
  68. struct stat st;
  69. pid_t pid = strtol(dirent.d_name, &end, 10);
  70. if (*end) /* only interested in proper numerical dirents */
  71. continue;
  72. snprintf(path, sizeof(path), "/proc/%s", dirent.d_name);
  73. if (stat(path, &st) != 0)
  74. continue;
  75. if (st.st_uid != uid)
  76. continue;
  77. snprintf(path, sizeof(path), "/proc/%d/task", pid);
  78. items = scandir(path, &namelist, filter, NULL);
  79. if (items <= 0)
  80. goto out_free_closedir;
  81. while (threads->nr + items >= max_threads) {
  82. max_threads *= 2;
  83. grow = true;
  84. }
  85. if (grow) {
  86. struct thread_map *tmp;
  87. tmp = realloc(threads, (sizeof(*threads) +
  88. max_threads * sizeof(pid_t)));
  89. if (tmp == NULL)
  90. goto out_free_namelist;
  91. threads = tmp;
  92. }
  93. for (i = 0; i < items; i++)
  94. threads->map[threads->nr + i] = atoi(namelist[i]->d_name);
  95. for (i = 0; i < items; i++)
  96. free(namelist[i]);
  97. free(namelist);
  98. threads->nr += items;
  99. }
  100. out_closedir:
  101. closedir(proc);
  102. out:
  103. return threads;
  104. out_free_threads:
  105. free(threads);
  106. return NULL;
  107. out_free_namelist:
  108. for (i = 0; i < items; i++)
  109. free(namelist[i]);
  110. free(namelist);
  111. out_free_closedir:
  112. free(threads);
  113. threads = NULL;
  114. goto out_closedir;
  115. }
  116. struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
  117. {
  118. if (pid != -1)
  119. return thread_map__new_by_pid(pid);
  120. if (tid == -1 && uid != UINT_MAX)
  121. return thread_map__new_by_uid(uid);
  122. return thread_map__new_by_tid(tid);
  123. }
  124. static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
  125. {
  126. struct thread_map *threads = NULL, *nt;
  127. char name[256];
  128. int items, total_tasks = 0;
  129. struct dirent **namelist = NULL;
  130. int i, j = 0;
  131. pid_t pid, prev_pid = INT_MAX;
  132. char *end_ptr;
  133. struct str_node *pos;
  134. struct strlist *slist = strlist__new(false, pid_str);
  135. if (!slist)
  136. return NULL;
  137. strlist__for_each(pos, slist) {
  138. pid = strtol(pos->s, &end_ptr, 10);
  139. if (pid == INT_MIN || pid == INT_MAX ||
  140. (*end_ptr != '\0' && *end_ptr != ','))
  141. goto out_free_threads;
  142. if (pid == prev_pid)
  143. continue;
  144. sprintf(name, "/proc/%d/task", pid);
  145. items = scandir(name, &namelist, filter, NULL);
  146. if (items <= 0)
  147. goto out_free_threads;
  148. total_tasks += items;
  149. nt = realloc(threads, (sizeof(*threads) +
  150. sizeof(pid_t) * total_tasks));
  151. if (nt == NULL)
  152. goto out_free_threads;
  153. threads = nt;
  154. if (threads) {
  155. for (i = 0; i < items; i++)
  156. threads->map[j++] = atoi(namelist[i]->d_name);
  157. threads->nr = total_tasks;
  158. }
  159. for (i = 0; i < items; i++)
  160. free(namelist[i]);
  161. free(namelist);
  162. if (!threads)
  163. break;
  164. }
  165. out:
  166. strlist__delete(slist);
  167. return threads;
  168. out_free_threads:
  169. free(threads);
  170. threads = NULL;
  171. goto out;
  172. }
  173. static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
  174. {
  175. struct thread_map *threads = NULL, *nt;
  176. int ntasks = 0;
  177. pid_t tid, prev_tid = INT_MAX;
  178. char *end_ptr;
  179. struct str_node *pos;
  180. struct strlist *slist;
  181. /* perf-stat expects threads to be generated even if tid not given */
  182. if (!tid_str) {
  183. threads = malloc(sizeof(*threads) + sizeof(pid_t));
  184. if (threads != NULL) {
  185. threads->map[0] = -1;
  186. threads->nr = 1;
  187. }
  188. return threads;
  189. }
  190. slist = strlist__new(false, tid_str);
  191. if (!slist)
  192. return NULL;
  193. strlist__for_each(pos, slist) {
  194. tid = strtol(pos->s, &end_ptr, 10);
  195. if (tid == INT_MIN || tid == INT_MAX ||
  196. (*end_ptr != '\0' && *end_ptr != ','))
  197. goto out_free_threads;
  198. if (tid == prev_tid)
  199. continue;
  200. ntasks++;
  201. nt = realloc(threads, sizeof(*threads) + sizeof(pid_t) * ntasks);
  202. if (nt == NULL)
  203. goto out_free_threads;
  204. threads = nt;
  205. threads->map[ntasks - 1] = tid;
  206. threads->nr = ntasks;
  207. }
  208. out:
  209. return threads;
  210. out_free_threads:
  211. free(threads);
  212. threads = NULL;
  213. goto out;
  214. }
  215. struct thread_map *thread_map__new_str(const char *pid, const char *tid,
  216. uid_t uid)
  217. {
  218. if (pid)
  219. return thread_map__new_by_pid_str(pid);
  220. if (!tid && uid != UINT_MAX)
  221. return thread_map__new_by_uid(uid);
  222. return thread_map__new_by_tid_str(tid);
  223. }
  224. void thread_map__delete(struct thread_map *threads)
  225. {
  226. free(threads);
  227. }
  228. size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
  229. {
  230. int i;
  231. size_t printed = fprintf(fp, "%d thread%s: ",
  232. threads->nr, threads->nr > 1 ? "s" : "");
  233. for (i = 0; i < threads->nr; ++i)
  234. printed += fprintf(fp, "%s%d", i ? ", " : "", threads->map[i]);
  235. return printed + fprintf(fp, "\n");
  236. }