thread.c 1.8 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 *thread = zalloc(sizeof(*thread));
13

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

24
	return thread;
25 26
}

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

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

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

48
int thread__comm_len(struct thread *thread)
49
{
50 51
	if (!thread->comm_len) {
		if (!thread->comm)
52
			return 0;
53
		thread->comm_len = strlen(thread->comm);
54 55
	}

56
	return thread->comm_len;
57 58
}

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
void thread__insert_map(struct thread *thread, struct map *map)
66
{
67 68
	map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr);
	map_groups__insert(&thread->mg, map);
69 70
}

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

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

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

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

90 91
	return 0;
}