tizen 2.4 release
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / tools / perf / util / thread.c
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"
9
10 struct thread *thread__new(pid_t pid)
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->comm = malloc(32);
18                 if (self->comm)
19                         snprintf(self->comm, 32, ":%d", self->pid);
20         }
21
22         return self;
23 }
24
25 void thread__delete(struct thread *self)
26 {
27         map_groups__exit(&self->mg);
28         free(self->comm);
29         free(self);
30 }
31
32 int thread__set_comm(struct thread *self, const char *comm)
33 {
34         int err;
35
36         if (self->comm)
37                 free(self->comm);
38         self->comm = strdup(comm);
39         err = self->comm == NULL ? -ENOMEM : 0;
40         if (!err) {
41                 self->comm_set = true;
42         }
43         return err;
44 }
45
46 int thread__comm_len(struct thread *self)
47 {
48         if (!self->comm_len) {
49                 if (!self->comm)
50                         return 0;
51                 self->comm_len = strlen(self->comm);
52         }
53
54         return self->comm_len;
55 }
56
57 size_t thread__fprintf(struct thread *thread, FILE *fp)
58 {
59         return fprintf(fp, "Thread %d %s\n", thread->pid, thread->comm) +
60                map_groups__fprintf(&thread->mg, verbose, fp);
61 }
62
63 void thread__insert_map(struct thread *self, struct map *map)
64 {
65         map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
66         map_groups__insert(&self->mg, map);
67 }
68
69 int thread__fork(struct thread *self, struct thread *parent)
70 {
71         int i;
72
73         if (parent->comm_set) {
74                 if (self->comm)
75                         free(self->comm);
76                 self->comm = strdup(parent->comm);
77                 if (!self->comm)
78                         return -ENOMEM;
79                 self->comm_set = true;
80         }
81
82         for (i = 0; i < MAP__NR_TYPES; ++i)
83                 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
84                         return -ENOMEM;
85         return 0;
86 }