thread.c 3.1 KB
Newer Older
1 2 3 4
#include "../perf.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
5
#include "session.h"
6 7
#include "thread.h"
#include "util.h"
8
#include "debug.h"
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
int find_all_tid(int pid, pid_t ** all_tid)
{
	char name[256];
	int items;
	struct dirent **namelist = NULL;
	int ret = 0;
	int i;

	sprintf(name, "/proc/%d/task", pid);
	items = scandir(name, &namelist, NULL, NULL);
	if (items <= 0)
                return -ENOENT;
	*all_tid = malloc(sizeof(pid_t) * items);
	if (!*all_tid) {
		ret = -ENOMEM;
		goto failure;
	}

	for (i = 0; i < items; i++)
		(*all_tid)[i] = atoi(namelist[i]->d_name);

	ret = items;

failure:
	for (i=0; i<items; i++)
		free(namelist[i]);
	free(namelist);

	return ret;
}

41
static struct thread *thread__new(pid_t pid)
42
{
43
	struct thread *self = zalloc(sizeof(*self));
44 45

	if (self != NULL) {
46 47
		map_groups__init(&self->mg);
		self->pid = pid;
48 49 50
		self->comm = malloc(32);
		if (self->comm)
			snprintf(self->comm, 32, ":%d", self->pid);
51 52 53 54 55 56 57
	}

	return self;
}

int thread__set_comm(struct thread *self, const char *comm)
{
58 59
	int err;

60 61 62
	if (self->comm)
		free(self->comm);
	self->comm = strdup(comm);
63 64 65 66 67 68
	err = self->comm == NULL ? -ENOMEM : 0;
	if (!err) {
		self->comm_set = true;
		map_groups__flush(&self->mg);
	}
	return err;
69 70
}

71 72 73 74 75 76 77 78 79 80 81
int thread__comm_len(struct thread *self)
{
	if (!self->comm_len) {
		if (!self->comm)
			return 0;
		self->comm_len = strlen(self->comm);
	}

	return self->comm_len;
}

82 83 84
static size_t thread__fprintf(struct thread *self, FILE *fp)
{
	return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
85
	       map_groups__fprintf(&self->mg, verbose, fp);
86 87
}

88
struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
89
{
90
	struct rb_node **p = &self->threads.rb_node;
91 92 93 94 95 96 97 98
	struct rb_node *parent = NULL;
	struct thread *th;

	/*
	 * Font-end cache - PID lookups come in blocks,
	 * so most of the time we dont have to look up
	 * the full rbtree:
	 */
99 100
	if (self->last_match && self->last_match->pid == pid)
		return self->last_match;
101 102 103 104 105 106

	while (*p != NULL) {
		parent = *p;
		th = rb_entry(parent, struct thread, rb_node);

		if (th->pid == pid) {
107
			self->last_match = th;
108 109 110 111 112 113 114 115 116
			return th;
		}

		if (pid < th->pid)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}

117
	th = thread__new(pid);
118 119
	if (th != NULL) {
		rb_link_node(&th->rb_node, parent, p);
120 121
		rb_insert_color(&th->rb_node, &self->threads);
		self->last_match = th;
122 123 124 125 126
	}

	return th;
}

127 128
void thread__insert_map(struct thread *self, struct map *map)
{
129
	map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
130
	map_groups__insert(&self->mg, map);
131 132
}

133 134 135
int thread__fork(struct thread *self, struct thread *parent)
{
	int i;
136

137 138 139 140 141 142 143 144
	if (parent->comm_set) {
		if (self->comm)
			free(self->comm);
		self->comm = strdup(parent->comm);
		if (!self->comm)
			return -ENOMEM;
		self->comm_set = true;
	}
145

146
	for (i = 0; i < MAP__NR_TYPES; ++i)
147
		if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
148 149 150 151
			return -ENOMEM;
	return 0;
}

152
size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
153 154 155 156
{
	size_t ret = 0;
	struct rb_node *nd;

157
	for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
158 159 160 161 162 163 164
		struct thread *pos = rb_entry(nd, struct thread, rb_node);

		ret += thread__fprintf(pos, fp);
	}

	return ret;
}