14 #include <api/fs/fs.h> 24 if (dir->d_name[0] ==
'.')
40 size_t size =
sizeof(*map) +
sizeof(map->
map[0]) * nr;
43 map = realloc(map, size);
53 #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr) 60 struct dirent **namelist = NULL;
63 sprintf(name,
"/proc/%d/task", pid);
64 items = scandir(name, &namelist,
filter, NULL);
69 if (threads != NULL) {
70 for (i = 0; i < items; i++)
73 refcount_set(&threads->
refcnt, 1);
76 for (i=0; i<items; i++)
87 if (threads != NULL) {
90 refcount_set(&threads->
refcnt, 1);
99 int max_threads = 32, items, i;
100 char path[NAME_MAX + 1 + 6];
101 struct dirent *dirent, **namelist = NULL;
107 proc = opendir(
"/proc");
109 goto out_free_threads;
112 refcount_set(&threads->
refcnt, 1);
114 while ((dirent = readdir(proc)) != NULL) {
117 pid_t
pid = strtol(dirent->d_name, &end, 10);
122 snprintf(path,
sizeof(path),
"/proc/%s", dirent->d_name);
124 if (uid != UINT_MAX) {
127 if (stat(path, &st) != 0 || st.st_uid != uid)
131 snprintf(path,
sizeof(path),
"/proc/%d/task", pid);
132 items = scandir(path, &namelist,
filter, NULL);
134 goto out_free_closedir;
136 while (threads->
nr + items >= max_threads) {
146 goto out_free_namelist;
151 for (i = 0; i < items; i++) {
153 atoi(namelist[i]->d_name));
156 for (i = 0; i < items; i++)
160 threads->
nr += items;
173 for (i = 0; i < items; i++)
197 if (tid == -1 && uid != UINT_MAX)
207 int items, total_tasks = 0;
208 struct dirent **namelist = NULL;
210 pid_t
pid, prev_pid = INT_MAX;
220 pid = strtol(pos->
s, &end_ptr, 10);
222 if (pid == INT_MIN || pid == INT_MAX ||
223 (*end_ptr !=
'\0' && *end_ptr !=
','))
224 goto out_free_threads;
229 sprintf(name,
"/proc/%d/task", pid);
230 items = scandir(name, &namelist,
filter, NULL);
232 goto out_free_threads;
234 total_tasks += items;
237 goto out_free_namelist;
241 for (i = 0; i < items; i++) {
245 threads->
nr = total_tasks;
252 refcount_set(&threads->
refcnt, 1);
256 for (i = 0; i < items; i++)
269 if (threads != NULL) {
272 refcount_set(&threads->
refcnt, 1);
281 pid_t tid, prev_tid = INT_MAX;
296 tid = strtol(pos->
s, &end_ptr, 10);
298 if (tid == INT_MIN || tid == INT_MAX ||
299 (*end_ptr !=
'\0' && *end_ptr !=
','))
300 goto out_free_threads;
309 goto out_free_threads;
313 threads->
nr = ntasks;
317 refcount_set(&threads->
refcnt, 1);
327 uid_t uid,
bool all_threads)
332 if (!tid && uid != UINT_MAX)
346 WARN_ONCE(refcount_read(&threads->
refcnt) != 0,
347 "thread map refcnt unbalanced\n");
348 for (i = 0; i < threads->
nr; i++)
357 refcount_inc(&map->
refcnt);
363 if (map && refcount_dec_and_test(&map->
refcnt))
370 size_t printed = fprintf(fp,
"%d thread%s: ",
371 threads->
nr, threads->
nr > 1 ?
"s" :
"");
372 for (i = 0; i < threads->
nr; ++i)
373 printed += fprintf(fp,
"%s%d", i ?
", " :
"",
thread_map__pid(threads, i));
375 return printed + fprintf(fp,
"\n");
384 if (asprintf(&path,
"%s/%d/comm", procfs__mountpoint(), pid) == -1)
387 err = filename__read_str(path, comm, &size);
409 map->
map[i].
comm = strdup(
"dummy");
418 pr_warning(
"Couldn't resolve comm name for pid %d\n", pid);
427 for (i = 0; i < threads->
nr; ++i)
436 threads->
nr = (int) event->
nr;
438 for (i = 0; i <
event->nr; i++) {
443 refcount_set(&threads->
refcnt, 1);
461 for (i = 0; i < threads->
nr; ++i) {
462 if (threads->
map[i].
pid == pid)
476 if (idx >= threads->
nr)
484 for (i = idx; i < threads->
nr - 1; i++)
485 threads->
map[i] = threads->
map[i + 1];
struct thread_map * thread_map__new_by_tid_str(const char *tid_str)
static struct thread_map * thread_map__realloc(struct thread_map *map, int nr)
int thread_map__remove(struct thread_map *threads, int idx)
struct thread_map * thread_map__new_dummy(void)
static struct thread_map * __thread_map__new_all_cpus(uid_t uid)
struct thread_map_event_entry entries[]
static int filter(const struct dirent *dir)
struct thread_map * thread_map__new_str(const char *pid, const char *tid, uid_t uid, bool all_threads)
struct thread_map * thread_map__get(struct thread_map *map)
static void thread_map__set_pid(struct thread_map *map, int thread, pid_t pid)
void thread_map__read_comms(struct thread_map *threads)
struct thread_map * thread_map__new_by_tid(pid_t tid)
struct thread_map * thread_map__new_by_pid(pid_t pid)
x86 movsq based memset() in arch/x86/lib/memset_64.S") MEMSET_FN(memset_erms
static void thread_map__copy_event(struct thread_map *threads, struct thread_map_event *event)
static void thread_map__reset(struct thread_map *map, int start, int nr)
static int get_comm(char **comm, pid_t pid)
struct thread_map * thread_map__new_event(struct thread_map_event *event)
bool thread_map__has(struct thread_map *threads, pid_t pid)
#define strlist__for_each_entry(pos, slist)
static char * thread_map__comm(struct thread_map *map, int thread)
struct thread_map * thread_map__new_all_cpus(void)
struct thread_map_data map[]
size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
static void thread_map__delete(struct thread_map *threads)
static struct thread_data threads[THREADS]
void strlist__delete(struct strlist *slist)
static void comm_init(struct thread_map *map, int i)
static pid_t thread_map__pid(struct thread_map *map, int thread)
static struct thread_map * thread_map__new_by_pid_str(const char *pid_str)
#define thread_map__alloc(__nr)
void thread_map__put(struct thread_map *map)
struct thread_map * thread_map__new_by_uid(uid_t uid)
struct thread_map * thread_map__new(pid_t pid, pid_t tid, uid_t uid)
#define pr_warning(fmt,...)
struct strlist * strlist__new(const char *list, const struct strlist_config *config)