thread_map.c 5.9 KB

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