thread_map.c 9.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 <api/fs/fs.h>
12
#include "asm/bug.h"
13
#include "thread_map.h"
14
#include "util.h"
15
#include "debug.h"
16
#include "event.h"
17 18 19 20 21 22 23 24 25 26

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

27 28 29 30 31 32 33
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);
}

34 35
static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
{
36
	size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
37 38 39 40 41 42 43 44
	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);
45

46
	return map;
47 48 49 50
}

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

51 52 53 54 55 56 57 58 59 60 61
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)
62
		return NULL;
63

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

	for (i=0; i<items; i++)
73
		zfree(&namelist[i]);
74 75 76 77 78 79 80
	free(namelist);

	return threads;
}

struct thread_map *thread_map__new_by_tid(pid_t tid)
{
81
	struct thread_map *threads = thread_map__alloc(1);
82 83

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

	return threads;
}

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

100 101 102 103 104 105 106 107
	if (threads == NULL)
		goto out;

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

	threads->nr = 0;
108
	atomic_set(&threads->refcnt, 1);
109

110
	while ((dirent = readdir(proc)) != NULL) {
111 112 113
		char *end;
		bool grow = false;
		struct stat st;
114
		pid_t pid = strtol(dirent->d_name, &end, 10);
115 116 117 118

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

119
		snprintf(path, sizeof(path), "/proc/%s", dirent->d_name);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

		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;

140
			tmp = thread_map__realloc(threads, max_threads);
141 142 143 144 145 146
			if (tmp == NULL)
				goto out_free_namelist;

			threads = tmp;
		}

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

		for (i = 0; i < items; i++)
153
			zfree(&namelist[i]);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
		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++)
170
		zfree(&namelist[i]);
171 172 173
	free(namelist);

out_free_closedir:
174
	zfree(&threads);
175 176 177 178
	goto out_closedir;
}

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

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

186 187 188
	return thread_map__new_by_tid(tid);
}

189 190 191 192 193 194 195 196 197 198
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;
199 200
	struct strlist_config slist_config = { .dont_dupstr = true, };
	struct strlist *slist = strlist__new(pid_str, &slist_config);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	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;
221
		nt = thread_map__realloc(threads, total_tasks);
222
		if (nt == NULL)
223
			goto out_free_namelist;
224 225 226

		threads = nt;

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

out:
	strlist__delete(slist);
237 238
	if (threads)
		atomic_set(&threads->refcnt, 1);
239 240
	return threads;

241 242
out_free_namelist:
	for (i = 0; i < items; i++)
243
		zfree(&namelist[i]);
244 245
	free(namelist);

246
out_free_threads:
247
	zfree(&threads);
248 249 250
	goto out;
}

251 252
struct thread_map *thread_map__new_dummy(void)
{
253
	struct thread_map *threads = thread_map__alloc(1);
254 255

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

263
struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
264 265 266 267 268 269
{
	struct thread_map *threads = NULL, *nt;
	int ntasks = 0;
	pid_t tid, prev_tid = INT_MAX;
	char *end_ptr;
	struct str_node *pos;
270
	struct strlist_config slist_config = { .dont_dupstr = true, };
271 272 273
	struct strlist *slist;

	/* perf-stat expects threads to be generated even if tid not given */
274 275
	if (!tid_str)
		return thread_map__new_dummy();
276

277
	slist = strlist__new(tid_str, &slist_config);
278 279 280 281 282 283 284 285 286 287 288 289 290 291
	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++;
292
		nt = thread_map__realloc(threads, ntasks);
293 294 295 296 297

		if (nt == NULL)
			goto out_free_threads;

		threads = nt;
298 299
		thread_map__set_pid(threads, ntasks - 1, tid);
		threads->nr = ntasks;
300 301
	}
out:
302 303
	if (threads)
		atomic_set(&threads->refcnt, 1);
304 305 306
	return threads;

out_free_threads:
307
	zfree(&threads);
308
	strlist__delete(slist);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	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);
}

324
static void thread_map__delete(struct thread_map *threads)
325
{
326
	if (threads) {
327 328
		int i;

329 330
		WARN_ONCE(atomic_read(&threads->refcnt) != 0,
			  "thread map refcnt unbalanced\n");
331 332
		for (i = 0; i < threads->nr; i++)
			free(thread_map__comm(threads, i));
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
		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);
348
}
349 350 351 352 353 354 355

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)
356
		printed += fprintf(fp, "%s%d", i ? ", " : "", thread_map__pid(threads, i));
357 358 359

	return printed + fprintf(fp, "\n");
}
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

static int get_comm(char **comm, pid_t pid)
{
	char *path;
	size_t size;
	int err;

	if (asprintf(&path, "%s/%d/comm", procfs__mountpoint(), pid) == -1)
		return -ENOMEM;

	err = filename__read_str(path, comm, &size);
	if (!err) {
		/*
		 * We're reading 16 bytes, while filename__read_str
		 * allocates data per BUFSIZ bytes, so we can safely
		 * mark the end of the string.
		 */
		(*comm)[size] = 0;
		rtrim(*comm);
	}

	free(path);
	return err;
}

static void comm_init(struct thread_map *map, int i)
{
	pid_t pid = thread_map__pid(map, i);
	char *comm = NULL;

	/* dummy pid comm initialization */
	if (pid == -1) {
		map->map[i].comm = strdup("dummy");
		return;
	}

	/*
	 * The comm name is like extra bonus ;-),
	 * so just warn if we fail for any reason.
	 */
	if (get_comm(&comm, pid))
		pr_warning("Couldn't resolve comm name for pid %d\n", pid);

	map->map[i].comm = comm;
}

void thread_map__read_comms(struct thread_map *threads)
{
	int i;

	for (i = 0; i < threads->nr; ++i)
		comm_init(threads, i);
}
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

static void thread_map__copy_event(struct thread_map *threads,
				   struct thread_map_event *event)
{
	unsigned i;

	threads->nr = (int) event->nr;

	for (i = 0; i < event->nr; i++) {
		thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
		threads->map[i].comm = strndup(event->entries[i].comm, 16);
	}

	atomic_set(&threads->refcnt, 1);
}

struct thread_map *thread_map__new_event(struct thread_map_event *event)
{
	struct thread_map *threads;

	threads = thread_map__alloc(event->nr);
	if (threads)
		thread_map__copy_event(threads, event);

	return threads;
}
J
Jiri Olsa 已提交
439 440 441 442 443 444 445 446 447 448 449 450

bool thread_map__has(struct thread_map *threads, pid_t pid)
{
	int i;

	for (i = 0; i < threads->nr; ++i) {
		if (threads->map[i].pid == pid)
			return true;
	}

	return false;
}