Linux 3.12.39
[linux/fpc-iii.git] / tools / perf / util / thread.c
blobe3d4a550a703211d8fabeb9807c28a86466fe6d8
1 #include "../perf.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "session.h"
6 #include "thread.h"
7 #include "util.h"
8 #include "debug.h"
10 struct thread *thread__new(pid_t pid, pid_t tid)
12 struct thread *self = zalloc(sizeof(*self));
14 if (self != NULL) {
15 map_groups__init(&self->mg);
16 self->pid_ = pid;
17 self->tid = tid;
18 self->ppid = -1;
19 self->comm = malloc(32);
20 if (self->comm)
21 snprintf(self->comm, 32, ":%d", self->tid);
24 return self;
27 void thread__delete(struct thread *self)
29 map_groups__exit(&self->mg);
30 free(self->comm);
31 free(self);
34 int thread__set_comm(struct thread *self, const char *comm)
36 int err;
38 if (self->comm)
39 free(self->comm);
40 self->comm = strdup(comm);
41 err = self->comm == NULL ? -ENOMEM : 0;
42 if (!err) {
43 self->comm_set = true;
45 return err;
48 int thread__comm_len(struct thread *self)
50 if (!self->comm_len) {
51 if (!self->comm)
52 return 0;
53 self->comm_len = strlen(self->comm);
56 return self->comm_len;
59 size_t thread__fprintf(struct thread *thread, FILE *fp)
61 return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) +
62 map_groups__fprintf(&thread->mg, verbose, fp);
65 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);
71 int thread__fork(struct thread *self, struct thread *parent)
73 int i;
75 if (parent->comm_set) {
76 if (self->comm)
77 free(self->comm);
78 self->comm = strdup(parent->comm);
79 if (!self->comm)
80 return -ENOMEM;
81 self->comm_set = true;
84 for (i = 0; i < MAP__NR_TYPES; ++i)
85 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
86 return -ENOMEM;
88 self->ppid = parent->tid;
90 return 0;