thread.c 1.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 7
#include "thread.h"
#include "util.h"
8
#include "debug.h"
9

10
struct thread *thread__new(pid_t pid, pid_t tid)
11
{
12
	struct thread *self = zalloc(sizeof(*self));
13 14

	if (self != NULL) {
15
		map_groups__init(&self->mg);
16
		self->pid_ = pid;
17
		self->tid = tid;
18
		self->ppid = -1;
19 20
		self->comm = malloc(32);
		if (self->comm)
21
			snprintf(self->comm, 32, ":%d", self->tid);
22 23 24 25 26
	}

	return self;
}

27 28 29 30 31 32 33
void thread__delete(struct thread *self)
{
	map_groups__exit(&self->mg);
	free(self->comm);
	free(self);
}

34 35
int thread__set_comm(struct thread *self, const char *comm)
{
36 37
	int err;

38 39 40
	if (self->comm)
		free(self->comm);
	self->comm = strdup(comm);
41 42 43 44 45
	err = self->comm == NULL ? -ENOMEM : 0;
	if (!err) {
		self->comm_set = true;
	}
	return err;
46 47
}

48 49 50 51 52 53 54 55 56 57 58
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;
}

59
size_t thread__fprintf(struct thread *thread, FILE *fp)
60
{
61
	return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) +
62
	       map_groups__fprintf(&thread->mg, verbose, fp);
63 64
}

65 66
void thread__insert_map(struct thread *self, struct map *map)
{
67
	map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
68
	map_groups__insert(&self->mg, map);
69 70
}

71 72 73
int thread__fork(struct thread *self, struct thread *parent)
{
	int i;
74

75 76 77 78 79 80 81 82
	if (parent->comm_set) {
		if (self->comm)
			free(self->comm);
		self->comm = strdup(parent->comm);
		if (!self->comm)
			return -ENOMEM;
		self->comm_set = true;
	}
83

84
	for (i = 0; i < MAP__NR_TYPES; ++i)
85
		if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
86
			return -ENOMEM;
87

88
	self->ppid = parent->tid;
89

90 91
	return 0;
}