thread_map.c 8.0 KB

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