thread.c 4.7 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 21 22 23 24 25 26 27 28
	} else {
		leader = machine__findnew_thread(machine, pid, pid);
		if (leader)
			thread->mg = map_groups__get(leader->mg);
	}

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

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

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

42 43 44
		if (unwind__prepare_access(thread) < 0)
			goto err_thread;

45 46 47 48 49
		comm_str = malloc(32);
		if (!comm_str)
			goto err_thread;

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

		list_add(&comm->list, &thread->comm_list);
56
		atomic_set(&thread->refcnt, 0);
57 58
	}

59
	return thread;
60 61 62 63

err_thread:
	free(thread);
	return NULL;
64 65
}

66
void thread__delete(struct thread *thread)
67
{
68 69
	struct comm *comm, *tmp;

70 71
	thread_stack__free(thread);

72 73 74 75
	if (thread->mg) {
		map_groups__put(thread->mg);
		thread->mg = NULL;
	}
76 77 78 79
	list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
		list_del(&comm->list);
		comm__free(comm);
	}
80
	unwind__finish_access(thread);
81

82
	free(thread);
83 84
}

85 86
struct thread *thread__get(struct thread *thread)
{
87
	atomic_inc(&thread->refcnt);
88 89 90 91 92
	return thread;
}

void thread__put(struct thread *thread)
{
93
	if (thread && atomic_dec_and_test(&thread->refcnt)) {
94 95 96 97 98
		list_del_init(&thread->node);
		thread__delete(thread);
	}
}

99
struct comm *thread__comm(const struct thread *thread)
100
{
101 102
	if (list_empty(&thread->comm_list))
		return NULL;
103

104 105 106
	return list_first_entry(&thread->comm_list, struct comm, list);
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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)
122 123
{
	struct comm *new, *curr = thread__comm(thread);
124
	int err;
125

126 127
	/* Override the default :tid entry */
	if (!thread->comm_set) {
128
		err = comm__override(curr, str, timestamp, exec);
129 130
		if (err)
			return err;
131
	} else {
132
		new = comm__new(str, timestamp, exec);
133 134 135
		if (!new)
			return -ENOMEM;
		list_add(&new->list, &thread->comm_list);
136 137 138

		if (exec)
			unwind__flush_access(thread);
139
	}
140 141 142 143

	thread->comm_set = true;

	return 0;
144 145
}

146 147
const char *thread__comm_str(const struct thread *thread)
{
148 149 150 151 152 153
	const struct comm *comm = thread__comm(thread);

	if (!comm)
		return NULL;

	return comm__str(comm);
154 155
}

156
/* CHECKME: it should probably better return the max comm len from its comm list */
157
int thread__comm_len(struct thread *thread)
158
{
159
	if (!thread->comm_len) {
160 161
		const char *comm = thread__comm_str(thread);
		if (!comm)
162
			return 0;
163
		thread->comm_len = strlen(comm);
164 165
	}

166
	return thread->comm_len;
167 168
}

169
size_t thread__fprintf(struct thread *thread, FILE *fp)
170
{
171
	return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
172
	       map_groups__fprintf(thread->mg, fp);
173 174
}

175
void thread__insert_map(struct thread *thread, struct map *map)
176
{
177
	map_groups__fixup_overlappings(thread->mg, map, stderr);
178
	map_groups__insert(thread->mg, map);
179 180
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
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;

	/* 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;
}

198
int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
199
{
200
	int err;
201

202
	if (parent->comm_set) {
203 204
		const char *comm = thread__comm_str(parent);
		if (!comm)
205
			return -ENOMEM;
206
		err = thread__set_comm(thread, comm, timestamp);
207
		if (err)
208
			return err;
209
	}
210

211
	thread->ppid = parent->tid;
212
	return thread__clone_map_groups(thread, parent);
213
}
214 215 216 217 218 219 220 221 222 223 224 225 226 227

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++) {
228
		thread__find_addr_location(thread, cpumodes[i], type, addr, al);
229 230 231 232
		if (al->map)
			break;
	}
}