thread.c 3.3 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
/* Skip "." and ".." directories */
static int filter(const struct dirent *dir)
{
	if (dir->d_name[0] == '.')
		return 0;
	else
		return 1;
}

19 20 21 22 23 24 25 26 27
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);
28
	items = scandir(name, &namelist, filter, NULL);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	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;
}

50
static struct thread *thread__new(pid_t pid)
51
{
52
	struct thread *self = zalloc(sizeof(*self));
53 54

	if (self != NULL) {
55 56
		map_groups__init(&self->mg);
		self->pid = pid;
57 58 59
		self->comm = malloc(32);
		if (self->comm)
			snprintf(self->comm, 32, ":%d", self->pid);
60 61 62 63 64 65 66
	}

	return self;
}

int thread__set_comm(struct thread *self, const char *comm)
{
67 68
	int err;

69 70 71
	if (self->comm)
		free(self->comm);
	self->comm = strdup(comm);
72 73 74 75 76 77
	err = self->comm == NULL ? -ENOMEM : 0;
	if (!err) {
		self->comm_set = true;
		map_groups__flush(&self->mg);
	}
	return err;
78 79
}

80 81 82 83 84 85 86 87 88 89 90
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;
}

91 92 93
static size_t thread__fprintf(struct thread *self, FILE *fp)
{
	return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
94
	       map_groups__fprintf(&self->mg, verbose, fp);
95 96
}

97
struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
98
{
99
	struct rb_node **p = &self->threads.rb_node;
100 101 102 103 104 105 106 107
	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:
	 */
108 109
	if (self->last_match && self->last_match->pid == pid)
		return self->last_match;
110 111 112 113 114 115

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

		if (th->pid == pid) {
116
			self->last_match = th;
117 118 119 120 121 122 123 124 125
			return th;
		}

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

126
	th = thread__new(pid);
127 128
	if (th != NULL) {
		rb_link_node(&th->rb_node, parent, p);
129 130
		rb_insert_color(&th->rb_node, &self->threads);
		self->last_match = th;
131 132 133 134 135
	}

	return th;
}

136 137
void thread__insert_map(struct thread *self, struct map *map)
{
138
	map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
139
	map_groups__insert(&self->mg, map);
140 141
}

142 143 144
int thread__fork(struct thread *self, struct thread *parent)
{
	int i;
145

146 147 148 149 150 151 152 153
	if (parent->comm_set) {
		if (self->comm)
			free(self->comm);
		self->comm = strdup(parent->comm);
		if (!self->comm)
			return -ENOMEM;
		self->comm_set = true;
	}
154

155
	for (i = 0; i < MAP__NR_TYPES; ++i)
156
		if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
157 158 159 160
			return -ENOMEM;
	return 0;
}

161
size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
162 163 164 165
{
	size_t ret = 0;
	struct rb_node *nd;

166
	for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
167 168 169 170 171 172 173
		struct thread *pos = rb_entry(nd, struct thread, rb_node);

		ret += thread__fprintf(pos, fp);
	}

	return ret;
}