]> bbs.cooldavid.org Git - net-next-2.6.git/blame - tools/perf/util/thread.c
perf tools: Release thread resources on PERF_RECORD_EXIT
[net-next-2.6.git] / tools / perf / util / thread.c
CommitLineData
6baa0a5a
FW
1#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
b3165f41 5#include "session.h"
6baa0a5a
FW
6#include "thread.h"
7#include "util.h"
6e086437 8#include "debug.h"
6baa0a5a 9
c214909b
GJ
10/* Skip "." and ".." directories */
11static int filter(const struct dirent *dir)
12{
13 if (dir->d_name[0] == '.')
14 return 0;
15 else
16 return 1;
17}
18
d6d901c2
ZY
19int find_all_tid(int pid, pid_t ** all_tid)
20{
21 char name[256];
22 int items;
23 struct dirent **namelist = NULL;
24 int ret = 0;
25 int i;
26
27 sprintf(name, "/proc/%d/task", pid);
c214909b 28 items = scandir(name, &namelist, filter, NULL);
d6d901c2
ZY
29 if (items <= 0)
30 return -ENOENT;
31 *all_tid = malloc(sizeof(pid_t) * items);
32 if (!*all_tid) {
33 ret = -ENOMEM;
34 goto failure;
35 }
36
37 for (i = 0; i < items; i++)
38 (*all_tid)[i] = atoi(namelist[i]->d_name);
39
40 ret = items;
41
42failure:
43 for (i=0; i<items; i++)
44 free(namelist[i]);
45 free(namelist);
46
47 return ret;
48}
49
97ea1a7f 50static struct thread *thread__new(pid_t pid)
6baa0a5a 51{
36479484 52 struct thread *self = zalloc(sizeof(*self));
6baa0a5a
FW
53
54 if (self != NULL) {
9958e1f0
ACM
55 map_groups__init(&self->mg);
56 self->pid = pid;
97ea1a7f
FW
57 self->comm = malloc(32);
58 if (self->comm)
59 snprintf(self->comm, 32, ":%d", self->pid);
6baa0a5a
FW
60 }
61
62 return self;
63}
64
591765fd
ACM
65void thread__delete(struct thread *self)
66{
67 map_groups__exit(&self->mg);
68 free(self->comm);
69 free(self);
70}
71
6baa0a5a
FW
72int thread__set_comm(struct thread *self, const char *comm)
73{
4385d580
DM
74 int err;
75
6baa0a5a
FW
76 if (self->comm)
77 free(self->comm);
78 self->comm = strdup(comm);
4385d580
DM
79 err = self->comm == NULL ? -ENOMEM : 0;
80 if (!err) {
81 self->comm_set = true;
82 map_groups__flush(&self->mg);
83 }
84 return err;
6baa0a5a
FW
85}
86
a4fb581b
FW
87int thread__comm_len(struct thread *self)
88{
89 if (!self->comm_len) {
90 if (!self->comm)
91 return 0;
92 self->comm_len = strlen(self->comm);
93 }
94
95 return self->comm_len;
96}
97
9958e1f0
ACM
98static size_t thread__fprintf(struct thread *self, FILE *fp)
99{
100 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
c6e718ff 101 map_groups__fprintf(&self->mg, verbose, fp);
6baa0a5a
FW
102}
103
b3165f41 104struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
6baa0a5a 105{
b3165f41 106 struct rb_node **p = &self->threads.rb_node;
6baa0a5a
FW
107 struct rb_node *parent = NULL;
108 struct thread *th;
109
110 /*
111 * Font-end cache - PID lookups come in blocks,
112 * so most of the time we dont have to look up
113 * the full rbtree:
114 */
b3165f41
ACM
115 if (self->last_match && self->last_match->pid == pid)
116 return self->last_match;
6baa0a5a
FW
117
118 while (*p != NULL) {
119 parent = *p;
120 th = rb_entry(parent, struct thread, rb_node);
121
122 if (th->pid == pid) {
b3165f41 123 self->last_match = th;
6baa0a5a
FW
124 return th;
125 }
126
127 if (pid < th->pid)
128 p = &(*p)->rb_left;
129 else
130 p = &(*p)->rb_right;
131 }
132
97ea1a7f 133 th = thread__new(pid);
6baa0a5a
FW
134 if (th != NULL) {
135 rb_link_node(&th->rb_node, parent, p);
b3165f41
ACM
136 rb_insert_color(&th->rb_node, &self->threads);
137 self->last_match = th;
6baa0a5a
FW
138 }
139
140 return th;
141}
142
1b46cddf
ACM
143void thread__insert_map(struct thread *self, struct map *map)
144{
c6e718ff 145 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
9958e1f0 146 map_groups__insert(&self->mg, map);
6baa0a5a
FW
147}
148
95011c60
ACM
149int thread__fork(struct thread *self, struct thread *parent)
150{
151 int i;
6baa0a5a 152
faa5c5c3
ACM
153 if (parent->comm_set) {
154 if (self->comm)
155 free(self->comm);
156 self->comm = strdup(parent->comm);
157 if (!self->comm)
158 return -ENOMEM;
159 self->comm_set = true;
160 }
6baa0a5a 161
95011c60 162 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 163 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
6baa0a5a 164 return -ENOMEM;
6baa0a5a
FW
165 return 0;
166}
167
b3165f41 168size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
6baa0a5a
FW
169{
170 size_t ret = 0;
171 struct rb_node *nd;
172
b3165f41 173 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
6baa0a5a
FW
174 struct thread *pos = rb_entry(nd, struct thread, rb_node);
175
176 ret += thread__fprintf(pos, fp);
177 }
178
179 return ret;
180}