]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/util/thread.c
10 /* Skip "." and ".." directories */
11 static int filter(const struct dirent
*dir
)
13 if (dir
->d_name
[0] == '.')
19 int find_all_tid(int pid
, pid_t
** all_tid
)
23 struct dirent
**namelist
= NULL
;
27 sprintf(name
, "/proc/%d/task", pid
);
28 items
= scandir(name
, &namelist
, filter
, NULL
);
31 *all_tid
= malloc(sizeof(pid_t
) * items
);
37 for (i
= 0; i
< items
; i
++)
38 (*all_tid
)[i
] = atoi(namelist
[i
]->d_name
);
43 for (i
=0; i
<items
; i
++)
50 static struct thread
*thread__new(pid_t pid
)
52 struct thread
*self
= zalloc(sizeof(*self
));
55 map_groups__init(&self
->mg
);
57 self
->comm
= malloc(32);
59 snprintf(self
->comm
, 32, ":%d", self
->pid
);
65 void thread__delete(struct thread
*self
)
67 map_groups__exit(&self
->mg
);
72 int thread__set_comm(struct thread
*self
, const char *comm
)
78 self
->comm
= strdup(comm
);
79 err
= self
->comm
== NULL
? -ENOMEM
: 0;
81 self
->comm_set
= true;
82 map_groups__flush(&self
->mg
);
87 int thread__comm_len(struct thread
*self
)
89 if (!self
->comm_len
) {
92 self
->comm_len
= strlen(self
->comm
);
95 return self
->comm_len
;
98 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
100 return fprintf(fp
, "Thread %d %s\n", self
->pid
, self
->comm
) +
101 map_groups__fprintf(&self
->mg
, verbose
, fp
);
104 struct thread
*perf_session__findnew(struct perf_session
*self
, pid_t pid
)
106 struct rb_node
**p
= &self
->threads
.rb_node
;
107 struct rb_node
*parent
= NULL
;
111 * Font-end cache - PID lookups come in blocks,
112 * so most of the time we dont have to look up
115 if (self
->last_match
&& self
->last_match
->pid
== pid
)
116 return self
->last_match
;
120 th
= rb_entry(parent
, struct thread
, rb_node
);
122 if (th
->pid
== pid
) {
123 self
->last_match
= th
;
133 th
= thread__new(pid
);
135 rb_link_node(&th
->rb_node
, parent
, p
);
136 rb_insert_color(&th
->rb_node
, &self
->threads
);
137 self
->last_match
= th
;
143 void thread__insert_map(struct thread
*self
, struct map
*map
)
145 map_groups__fixup_overlappings(&self
->mg
, map
, verbose
, stderr
);
146 map_groups__insert(&self
->mg
, map
);
149 int thread__fork(struct thread
*self
, struct thread
*parent
)
153 if (parent
->comm_set
) {
156 self
->comm
= strdup(parent
->comm
);
159 self
->comm_set
= true;
162 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
163 if (map_groups__clone(&self
->mg
, &parent
->mg
, i
) < 0)
168 size_t perf_session__fprintf(struct perf_session
*self
, FILE *fp
)
173 for (nd
= rb_first(&self
->threads
); nd
; nd
= rb_next(nd
)) {
174 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
176 ret
+= thread__fprintf(pos
, fp
);