thread.c 4.5 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
#include "comm.h"
10
#include "unwind.h"
11

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

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

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

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

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

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

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

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

		list_add(&comm->list, &thread->comm_list);
55

56 57
	}

58
	return thread;
59 60 61 62

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

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

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

79
	free(thread);
80 81
}

82
struct comm *thread__comm(const struct thread *thread)
83
{
84 85
	if (list_empty(&thread->comm_list))
		return NULL;
86

87 88 89
	return list_first_entry(&thread->comm_list, struct comm, list);
}

90 91 92 93 94 95 96 97 98 99 100 101 102
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;
}

103
/* CHECKME: time should always be 0 if event aren't ordered */
104 105
int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
		       bool exec)
106 107
{
	struct comm *new, *curr = thread__comm(thread);
108
	int err;
109 110

	/* Override latest entry if it had no specific time coverage */
111 112
	if (!curr->start && !curr->exec) {
		err = comm__override(curr, str, timestamp, exec);
113 114
		if (err)
			return err;
115
	} else {
116
		new = comm__new(str, timestamp, exec);
117 118 119
		if (!new)
			return -ENOMEM;
		list_add(&new->list, &thread->comm_list);
120 121 122

		if (exec)
			unwind__flush_access(thread);
123
	}
124 125 126 127

	thread->comm_set = true;

	return 0;
128 129
}

130 131
const char *thread__comm_str(const struct thread *thread)
{
132 133 134 135 136 137
	const struct comm *comm = thread__comm(thread);

	if (!comm)
		return NULL;

	return comm__str(comm);
138 139
}

140
/* CHECKME: it should probably better return the max comm len from its comm list */
141
int thread__comm_len(struct thread *thread)
142
{
143
	if (!thread->comm_len) {
144 145
		const char *comm = thread__comm_str(thread);
		if (!comm)
146
			return 0;
147
		thread->comm_len = strlen(comm);
148 149
	}

150
	return thread->comm_len;
151 152
}

153
size_t thread__fprintf(struct thread *thread, FILE *fp)
154
{
155
	return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
156
	       map_groups__fprintf(thread->mg, fp);
157 158
}

159
void thread__insert_map(struct thread *thread, struct map *map)
160
{
161
	map_groups__fixup_overlappings(thread->mg, map, stderr);
162
	map_groups__insert(thread->mg, map);
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
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;
}

182
int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
183
{
184
	int err;
185

186
	if (parent->comm_set) {
187 188
		const char *comm = thread__comm_str(parent);
		if (!comm)
189
			return -ENOMEM;
190
		err = thread__set_comm(thread, comm, timestamp);
191
		if (err)
192
			return err;
193
		thread->comm_set = true;
194
	}
195

196
	thread->ppid = parent->tid;
197
	return thread__clone_map_groups(thread, parent);
198
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212

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++) {
213
		thread__find_addr_location(thread, cpumodes[i], type, addr, al);
214 215 216 217
		if (al->map)
			break;
	}
}