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