thread_map.c 7.0 KB
Newer Older
1
#include <dirent.h>
2 3
#include <limits.h>
#include <stdbool.h>
4 5
#include <stdlib.h>
#include <stdio.h>
6 7 8
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
9 10
#include "strlist.h"
#include <string.h>
11
#include "asm/bug.h"
12
#include "thread_map.h"
13
#include "util.h"
14 15 16 17 18 19 20 21 22 23

/* Skip "." and ".." directories */
static int filter(const struct dirent *dir)
{
	if (dir->d_name[0] == '.')
		return 0;
	else
		return 1;
}

24 25 26 27 28 29 30
static void thread_map__reset(struct thread_map *map, int start, int nr)
{
	size_t size = (nr - start) * sizeof(map->map[0]);

	memset(&map->map[start], 0, size);
}

31 32
static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
{
33
	size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
34 35 36 37 38 39 40 41
	int start = map ? map->nr : 0;

	map = realloc(map, size);
	/*
	 * We only realloc to add more items, let's reset new items.
	 */
	if (map)
		thread_map__reset(map, start, nr);
42

43
	return map;
44 45 46 47
}

#define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)

48 49 50 51 52 53 54 55 56 57 58
struct thread_map *thread_map__new_by_pid(pid_t pid)
{
	struct thread_map *threads;
	char name[256];
	int items;
	struct dirent **namelist = NULL;
	int i;

	sprintf(name, "/proc/%d/task", pid);
	items = scandir(name, &namelist, filter, NULL);
	if (items <= 0)
59
		return NULL;
60

61
	threads = thread_map__alloc(items);
62 63
	if (threads != NULL) {
		for (i = 0; i < items; i++)
64
			thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
65
		threads->nr = items;
66
		atomic_set(&threads->refcnt, 1);
67 68 69
	}

	for (i=0; i<items; i++)
70
		zfree(&namelist[i]);
71 72 73 74 75 76 77
	free(namelist);

	return threads;
}

struct thread_map *thread_map__new_by_tid(pid_t tid)
{
78
	struct thread_map *threads = thread_map__alloc(1);
79 80

	if (threads != NULL) {
81 82
		thread_map__set_pid(threads, 0, tid);
		threads->nr = 1;
83
		atomic_set(&threads->refcnt, 1);
84 85 86 87 88
	}

	return threads;
}

89 90 91 92 93 94
struct thread_map *thread_map__new_by_uid(uid_t uid)
{
	DIR *proc;
	int max_threads = 32, items, i;
	char path[256];
	struct dirent dirent, *next, **namelist = NULL;
95 96
	struct thread_map *threads = thread_map__alloc(max_threads);

97 98 99 100 101 102 103 104
	if (threads == NULL)
		goto out;

	proc = opendir("/proc");
	if (proc == NULL)
		goto out_free_threads;

	threads->nr = 0;
105
	atomic_set(&threads->refcnt, 1);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	while (!readdir_r(proc, &dirent, &next) && next) {
		char *end;
		bool grow = false;
		struct stat st;
		pid_t pid = strtol(dirent.d_name, &end, 10);

		if (*end) /* only interested in proper numerical dirents */
			continue;

		snprintf(path, sizeof(path), "/proc/%s", dirent.d_name);

		if (stat(path, &st) != 0)
			continue;

		if (st.st_uid != uid)
			continue;

		snprintf(path, sizeof(path), "/proc/%d/task", pid);
		items = scandir(path, &namelist, filter, NULL);
		if (items <= 0)
			goto out_free_closedir;

		while (threads->nr + items >= max_threads) {
			max_threads *= 2;
			grow = true;
		}

		if (grow) {
			struct thread_map *tmp;

			tmp = realloc(threads, (sizeof(*threads) +
						max_threads * sizeof(pid_t)));
			if (tmp == NULL)
				goto out_free_namelist;

			threads = tmp;
		}

145 146 147 148
		for (i = 0; i < items; i++) {
			thread_map__set_pid(threads, threads->nr + i,
					    atoi(namelist[i]->d_name));
		}
149 150

		for (i = 0; i < items; i++)
151
			zfree(&namelist[i]);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		free(namelist);

		threads->nr += items;
	}

out_closedir:
	closedir(proc);
out:
	return threads;

out_free_threads:
	free(threads);
	return NULL;

out_free_namelist:
	for (i = 0; i < items; i++)
168
		zfree(&namelist[i]);
169 170 171
	free(namelist);

out_free_closedir:
172
	zfree(&threads);
173 174 175 176
	goto out_closedir;
}

struct thread_map *thread_map__new(pid_t pid, pid_t tid, uid_t uid)
177 178 179
{
	if (pid != -1)
		return thread_map__new_by_pid(pid);
180 181 182 183

	if (tid == -1 && uid != UINT_MAX)
		return thread_map__new_by_uid(uid);

184 185 186
	return thread_map__new_by_tid(tid);
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
{
	struct thread_map *threads = NULL, *nt;
	char name[256];
	int items, total_tasks = 0;
	struct dirent **namelist = NULL;
	int i, j = 0;
	pid_t pid, prev_pid = INT_MAX;
	char *end_ptr;
	struct str_node *pos;
	struct strlist *slist = strlist__new(false, pid_str);

	if (!slist)
		return NULL;

	strlist__for_each(pos, slist) {
		pid = strtol(pos->s, &end_ptr, 10);

		if (pid == INT_MIN || pid == INT_MAX ||
		    (*end_ptr != '\0' && *end_ptr != ','))
			goto out_free_threads;

		if (pid == prev_pid)
			continue;

		sprintf(name, "/proc/%d/task", pid);
		items = scandir(name, &namelist, filter, NULL);
		if (items <= 0)
			goto out_free_threads;

		total_tasks += items;
218
		nt = thread_map__realloc(threads, total_tasks);
219
		if (nt == NULL)
220
			goto out_free_namelist;
221 222 223

		threads = nt;

224
		for (i = 0; i < items; i++) {
225
			thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
226
			zfree(&namelist[i]);
227 228
		}
		threads->nr = total_tasks;
229 230 231 232 233
		free(namelist);
	}

out:
	strlist__delete(slist);
234 235
	if (threads)
		atomic_set(&threads->refcnt, 1);
236 237
	return threads;

238 239
out_free_namelist:
	for (i = 0; i < items; i++)
240
		zfree(&namelist[i]);
241 242
	free(namelist);

243
out_free_threads:
244
	zfree(&threads);
245 246 247
	goto out;
}

248 249
struct thread_map *thread_map__new_dummy(void)
{
250
	struct thread_map *threads = thread_map__alloc(1);
251 252

	if (threads != NULL) {
253 254
		thread_map__set_pid(threads, 0, -1);
		threads->nr = 1;
255
		atomic_set(&threads->refcnt, 1);
256 257 258 259
	}
	return threads;
}

260 261 262 263 264 265 266 267 268 269
static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
{
	struct thread_map *threads = NULL, *nt;
	int ntasks = 0;
	pid_t tid, prev_tid = INT_MAX;
	char *end_ptr;
	struct str_node *pos;
	struct strlist *slist;

	/* perf-stat expects threads to be generated even if tid not given */
270 271
	if (!tid_str)
		return thread_map__new_dummy();
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

	slist = strlist__new(false, tid_str);
	if (!slist)
		return NULL;

	strlist__for_each(pos, slist) {
		tid = strtol(pos->s, &end_ptr, 10);

		if (tid == INT_MIN || tid == INT_MAX ||
		    (*end_ptr != '\0' && *end_ptr != ','))
			goto out_free_threads;

		if (tid == prev_tid)
			continue;

		ntasks++;
288
		nt = thread_map__realloc(threads, ntasks);
289 290 291 292 293

		if (nt == NULL)
			goto out_free_threads;

		threads = nt;
294 295
		thread_map__set_pid(threads, ntasks - 1, tid);
		threads->nr = ntasks;
296 297
	}
out:
298 299
	if (threads)
		atomic_set(&threads->refcnt, 1);
300 301 302
	return threads;

out_free_threads:
303
	zfree(&threads);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	goto out;
}

struct thread_map *thread_map__new_str(const char *pid, const char *tid,
				       uid_t uid)
{
	if (pid)
		return thread_map__new_by_pid_str(pid);

	if (!tid && uid != UINT_MAX)
		return thread_map__new_by_uid(uid);

	return thread_map__new_by_tid_str(tid);
}

319
static void thread_map__delete(struct thread_map *threads)
320
{
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	if (threads) {
		WARN_ONCE(atomic_read(&threads->refcnt) != 0,
			  "thread map refcnt unbalanced\n");
		free(threads);
	}
}

struct thread_map *thread_map__get(struct thread_map *map)
{
	if (map)
		atomic_inc(&map->refcnt);
	return map;
}

void thread_map__put(struct thread_map *map)
{
	if (map && atomic_dec_and_test(&map->refcnt))
		thread_map__delete(map);
339
}
340 341 342 343 344 345 346

size_t thread_map__fprintf(struct thread_map *threads, FILE *fp)
{
	int i;
	size_t printed = fprintf(fp, "%d thread%s: ",
				 threads->nr, threads->nr > 1 ? "s" : "");
	for (i = 0; i < threads->nr; ++i)
347
		printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
348 349 350

	return printed + fprintf(fp, "\n");
}