blob: 00f4eade2e3e9c1fdd2e9c92c83222a10c1555fd [file] [log] [blame]
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02005#include "session.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02006#include "thread.h"
7#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02008#include "debug.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02009
Gui Jianfengc2149092010-06-16 13:21:44 +080010/* 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
Arnaldo Carvalho de Melo5c98d462011-01-03 17:53:33 -020019struct thread_map *thread_map__new_by_pid(pid_t pid)
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030020{
Arnaldo Carvalho de Melo5c98d462011-01-03 17:53:33 -020021 struct thread_map *threads;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030022 char name[256];
23 int items;
24 struct dirent **namelist = NULL;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030025 int i;
26
27 sprintf(name, "/proc/%d/task", pid);
Gui Jianfengc2149092010-06-16 13:21:44 +080028 items = scandir(name, &namelist, filter, NULL);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030029 if (items <= 0)
Arnaldo Carvalho de Melo5c98d462011-01-03 17:53:33 -020030 return NULL;
31
32 threads = malloc(sizeof(*threads) + sizeof(pid_t) * items);
33 if (threads != NULL) {
34 for (i = 0; i < items; i++)
35 threads->map[i] = atoi(namelist[i]->d_name);
36 threads->nr = items;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030037 }
38
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030039 for (i=0; i<items; i++)
40 free(namelist[i]);
41 free(namelist);
42
Arnaldo Carvalho de Melo5c98d462011-01-03 17:53:33 -020043 return threads;
44}
45
46struct thread_map *thread_map__new_by_tid(pid_t tid)
47{
48 struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t));
49
50 if (threads != NULL) {
51 threads->map[0] = tid;
52 threads->nr = 1;
53 }
54
55 return threads;
56}
57
58struct thread_map *thread_map__new(pid_t pid, pid_t tid)
59{
60 if (pid != -1)
61 return thread_map__new_by_pid(pid);
62 return thread_map__new_by_tid(tid);
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030063}
64
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020065static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020066{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020067 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020068
69 if (self != NULL) {
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020070 map_groups__init(&self->mg);
71 self->pid = pid;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020072 self->comm = malloc(32);
73 if (self->comm)
74 snprintf(self->comm, 32, ":%d", self->pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020075 }
76
77 return self;
78}
79
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030080void thread__delete(struct thread *self)
81{
82 map_groups__exit(&self->mg);
83 free(self->comm);
84 free(self);
85}
86
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020087int thread__set_comm(struct thread *self, const char *comm)
88{
David S. Miller4385d582010-02-26 12:08:34 -030089 int err;
90
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020091 if (self->comm)
92 free(self->comm);
93 self->comm = strdup(comm);
David S. Miller4385d582010-02-26 12:08:34 -030094 err = self->comm == NULL ? -ENOMEM : 0;
95 if (!err) {
96 self->comm_set = true;
97 map_groups__flush(&self->mg);
98 }
99 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200100}
101
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200102int thread__comm_len(struct thread *self)
103{
104 if (!self->comm_len) {
105 if (!self->comm)
106 return 0;
107 self->comm_len = strlen(self->comm);
108 }
109
110 return self->comm_len;
111}
112
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200113static size_t thread__fprintf(struct thread *self, FILE *fp)
114{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200115 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300116 map_groups__fprintf(&self->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200117}
118
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200119struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200120{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200121 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200122 struct rb_node *parent = NULL;
123 struct thread *th;
124
125 /*
126 * Font-end cache - PID lookups come in blocks,
127 * so most of the time we dont have to look up
128 * the full rbtree:
129 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200130 if (self->last_match && self->last_match->pid == pid)
131 return self->last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200132
133 while (*p != NULL) {
134 parent = *p;
135 th = rb_entry(parent, struct thread, rb_node);
136
137 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200138 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200139 return th;
140 }
141
142 if (pid < th->pid)
143 p = &(*p)->rb_left;
144 else
145 p = &(*p)->rb_right;
146 }
147
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200148 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200149 if (th != NULL) {
150 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200151 rb_insert_color(&th->rb_node, &self->threads);
152 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200153 }
154
155 return th;
156}
157
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300158void thread__insert_map(struct thread *self, struct map *map)
159{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300160 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200161 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200162}
163
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200164int thread__fork(struct thread *self, struct thread *parent)
165{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200166 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200167
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200168 if (parent->comm_set) {
169 if (self->comm)
170 free(self->comm);
171 self->comm = strdup(parent->comm);
172 if (!self->comm)
173 return -ENOMEM;
174 self->comm_set = true;
175 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200176
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200177 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200178 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200179 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200180 return 0;
181}
182
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200183size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200184{
185 size_t ret = 0;
186 struct rb_node *nd;
187
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200188 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200189 struct thread *pos = rb_entry(nd, struct thread, rb_node);
190
191 ret += thread__fprintf(pos, fp);
192 }
193
194 return ret;
195}