thread.c 5.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
#include "thread.h"
7
#include "thread-stack.h"
8
#include "util.h"
9
#include "debug.h"
10
#include "comm.h"
11
#include "unwind.h"
12

13 14 15 16 17
int thread__init_map_groups(struct thread *thread, struct machine *machine)
{
	struct thread *leader;
	pid_t pid = thread->pid_;

18
	if (pid == thread->tid || pid == -1) {
19
		thread->mg = map_groups__new(machine);
20
	} else {
21
		leader = __machine__findnew_thread(machine, pid, pid);
22
		if (leader) {
23
			thread->mg = map_groups__get(leader->mg);
24 25
			thread__put(leader);
		}
26 27 28 29 30
	}

	return thread->mg ? 0 : -1;
}

31
struct thread *thread__new(pid_t pid, pid_t tid)
32
{
33 34
	char *comm_str;
	struct comm *comm;
35
	struct thread *thread = zalloc(sizeof(*thread));
36

37 38 39 40
	if (thread != NULL) {
		thread->pid_ = pid;
		thread->tid = tid;
		thread->ppid = -1;
41
		thread->cpu = -1;
42 43
		INIT_LIST_HEAD(&thread->comm_list);

44 45 46
		if (unwind__prepare_access(thread) < 0)
			goto err_thread;

47 48 49 50 51
		comm_str = malloc(32);
		if (!comm_str)
			goto err_thread;

		snprintf(comm_str, 32, ":%d", tid);
52
		comm = comm__new(comm_str, 0, false);
53 54 55 56 57
		free(comm_str);
		if (!comm)
			goto err_thread;

		list_add(&comm->list, &thread->comm_list);
58
		atomic_set(&thread->refcnt, 1);
59
		RB_CLEAR_NODE(&thread->rb_node);
60 61
	}

62
	return thread;
63 64 65 66

err_thread:
	free(thread);
	return NULL;
67 68
}

69
void thread__delete(struct thread *thread)
70
{
71 72
	struct comm *comm, *tmp;

73 74
	BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));

75 76
	thread_stack__free(thread);

77 78 79 80
	if (thread->mg) {
		map_groups__put(thread->mg);
		thread->mg = NULL;
	}
81 82 83 84
	list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
		list_del(&comm->list);
		comm__free(comm);
	}
85
	unwind__finish_access(thread);
86

87
	free(thread);
88 89
}

90 91
struct thread *thread__get(struct thread *thread)
{
92 93
	if (thread)
		atomic_inc(&thread->refcnt);
94 95 96 97 98
	return thread;
}

void thread__put(struct thread *thread)
{
99
	if (thread && atomic_dec_and_test(&thread->refcnt)) {
100 101 102 103
		/*
		 * Remove it from the dead_threads list, as last reference
		 * is gone.
		 */
104 105 106 107 108
		list_del_init(&thread->node);
		thread__delete(thread);
	}
}

109
struct comm *thread__comm(const struct thread *thread)
110
{
111 112
	if (list_empty(&thread->comm_list))
		return NULL;
113

114 115 116
	return list_first_entry(&thread->comm_list, struct comm, list);
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
struct comm *thread__exec_comm(const struct thread *thread)
{
	struct comm *comm, *last = NULL;

	list_for_each_entry(comm, &thread->comm_list, list) {
		if (comm->exec)
			return comm;
		last = comm;
	}

	return last;
}

int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
		       bool exec)
132 133
{
	struct comm *new, *curr = thread__comm(thread);
134
	int err;
135

136 137
	/* Override the default :tid entry */
	if (!thread->comm_set) {
138
		err = comm__override(curr, str, timestamp, exec);
139 140
		if (err)
			return err;
141
	} else {
142
		new = comm__new(str, timestamp, exec);
143 144 145
		if (!new)
			return -ENOMEM;
		list_add(&new->list, &thread->comm_list);
146 147 148

		if (exec)
			unwind__flush_access(thread);
149
	}
150 151 152 153

	thread->comm_set = true;

	return 0;
154 155
}

156 157
const char *thread__comm_str(const struct thread *thread)
{
158 159 160 161 162 163
	const struct comm *comm = thread__comm(thread);

	if (!comm)
		return NULL;

	return comm__str(comm);
164 165
}

166
/* CHECKME: it should probably better return the max comm len from its comm list */
167
int thread__comm_len(struct thread *thread)
168
{
169
	if (!thread->comm_len) {
170 171
		const char *comm = thread__comm_str(thread);
		if (!comm)
172
			return 0;
173
		thread->comm_len = strlen(comm);
174 175
	}

176
	return thread->comm_len;
177 178
}

179
size_t thread__fprintf(struct thread *thread, FILE *fp)
180
{
181
	return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
182
	       map_groups__fprintf(thread->mg, fp);
183 184
}

185
void thread__insert_map(struct thread *thread, struct map *map)
186
{
187
	map_groups__fixup_overlappings(thread->mg, map, stderr);
188
	map_groups__insert(thread->mg, map);
189 190
}

191 192 193 194 195 196 197 198 199
static int thread__clone_map_groups(struct thread *thread,
				    struct thread *parent)
{
	int i;

	/* This is new thread, we share map groups for process. */
	if (thread->pid_ == parent->pid_)
		return 0;

200 201 202 203 204 205
	if (thread->mg == parent->mg) {
		pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
			 thread->pid_, thread->tid, parent->pid_, parent->tid);
		return 0;
	}

206 207 208 209 210 211 212 213
	/* But this one is new process, copy maps. */
	for (i = 0; i < MAP__NR_TYPES; ++i)
		if (map_groups__clone(thread->mg, parent->mg, i) < 0)
			return -ENOMEM;

	return 0;
}

214
int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
215
{
216
	int err;
217

218
	if (parent->comm_set) {
219 220
		const char *comm = thread__comm_str(parent);
		if (!comm)
221
			return -ENOMEM;
222
		err = thread__set_comm(thread, comm, timestamp);
223
		if (err)
224
			return err;
225
	}
226

227
	thread->ppid = parent->tid;
228
	return thread__clone_map_groups(thread, parent);
229
}
230 231 232 233 234 235 236 237 238 239 240 241 242 243

void thread__find_cpumode_addr_location(struct thread *thread,
					enum map_type type, u64 addr,
					struct addr_location *al)
{
	size_t i;
	const u8 const cpumodes[] = {
		PERF_RECORD_MISC_USER,
		PERF_RECORD_MISC_KERNEL,
		PERF_RECORD_MISC_GUEST_USER,
		PERF_RECORD_MISC_GUEST_KERNEL
	};

	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
244
		thread__find_addr_location(thread, cpumodes[i], type, addr, al);
245 246 247 248
		if (al->map)
			break;
	}
}