blob: 8c72d888e449989dcdf1c349fdc4a97ead6b694a [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
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030019int 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);
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)
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
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020050static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020051{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020052 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020053
54 if (self != NULL) {
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020055 map_groups__init(&self->mg);
56 self->pid = pid;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020057 self->comm = malloc(32);
58 if (self->comm)
59 snprintf(self->comm, 32, ":%d", self->pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020060 }
61
62 return self;
63}
64
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030065void thread__delete(struct thread *self)
66{
67 map_groups__exit(&self->mg);
68 free(self->comm);
69 free(self);
70}
71
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020072int thread__set_comm(struct thread *self, const char *comm)
73{
David S. Miller4385d582010-02-26 12:08:34 -030074 int err;
75
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020076 if (self->comm)
77 free(self->comm);
78 self->comm = strdup(comm);
David S. Miller4385d582010-02-26 12:08:34 -030079 err = self->comm == NULL ? -ENOMEM : 0;
80 if (!err) {
81 self->comm_set = true;
82 map_groups__flush(&self->mg);
83 }
84 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020085}
86
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020087int 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
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020098static size_t thread__fprintf(struct thread *self, FILE *fp)
99{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200100 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300101 map_groups__fprintf(&self->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200102}
103
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200104struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200105{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200106 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200107 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 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200115 if (self->last_match && self->last_match->pid == pid)
116 return self->last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200117
118 while (*p != NULL) {
119 parent = *p;
120 th = rb_entry(parent, struct thread, rb_node);
121
122 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200123 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200124 return th;
125 }
126
127 if (pid < th->pid)
128 p = &(*p)->rb_left;
129 else
130 p = &(*p)->rb_right;
131 }
132
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +0200133 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200134 if (th != NULL) {
135 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200136 rb_insert_color(&th->rb_node, &self->threads);
137 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200138 }
139
140 return th;
141}
142
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300143void thread__insert_map(struct thread *self, struct map *map)
144{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300145 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200146 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200147}
148
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200149int thread__fork(struct thread *self, struct thread *parent)
150{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200151 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200152
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200153 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 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200161
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200162 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200163 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200164 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200165 return 0;
166}
167
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200168size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200169{
170 size_t ret = 0;
171 struct rb_node *nd;
172
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200173 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200174 struct thread *pos = rb_entry(nd, struct thread, rb_node);
175
176 ret += thread__fprintf(pos, fp);
177 }
178
179 return ret;
180}