10 int find_all_tid(int pid
, pid_t
** all_tid
)
14 struct dirent
**namelist
= NULL
;
18 sprintf(name
, "/proc/%d/task", pid
);
19 items
= scandir(name
, &namelist
, NULL
, NULL
);
22 *all_tid
= malloc(sizeof(pid_t
) * items
);
28 for (i
= 0; i
< items
; i
++)
29 (*all_tid
)[i
] = atoi(namelist
[i
]->d_name
);
34 for (i
=0; i
<items
; i
++)
41 static struct thread
*thread__new(pid_t pid
)
43 struct thread
*self
= zalloc(sizeof(*self
));
46 map_groups__init(&self
->mg
);
48 self
->comm
= malloc(32);
50 snprintf(self
->comm
, 32, ":%d", self
->pid
);
56 int thread__set_comm(struct thread
*self
, const char *comm
)
62 self
->comm
= strdup(comm
);
63 err
= self
->comm
== NULL
? -ENOMEM
: 0;
65 self
->comm_set
= true;
66 map_groups__flush(&self
->mg
);
71 int thread__comm_len(struct thread
*self
)
73 if (!self
->comm_len
) {
76 self
->comm_len
= strlen(self
->comm
);
79 return self
->comm_len
;
82 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
84 return fprintf(fp
, "Thread %d %s\n", self
->pid
, self
->comm
) +
85 map_groups__fprintf(&self
->mg
, verbose
, fp
);
88 struct thread
*perf_session__findnew(struct perf_session
*self
, pid_t pid
)
90 struct rb_node
**p
= &self
->threads
.rb_node
;
91 struct rb_node
*parent
= NULL
;
95 * Font-end cache - PID lookups come in blocks,
96 * so most of the time we dont have to look up
99 if (self
->last_match
&& self
->last_match
->pid
== pid
)
100 return self
->last_match
;
104 th
= rb_entry(parent
, struct thread
, rb_node
);
106 if (th
->pid
== pid
) {
107 self
->last_match
= th
;
117 th
= thread__new(pid
);
119 rb_link_node(&th
->rb_node
, parent
, p
);
120 rb_insert_color(&th
->rb_node
, &self
->threads
);
121 self
->last_match
= th
;
127 void thread__insert_map(struct thread
*self
, struct map
*map
)
129 map_groups__fixup_overlappings(&self
->mg
, map
, verbose
, stderr
);
130 map_groups__insert(&self
->mg
, map
);
133 int thread__fork(struct thread
*self
, struct thread
*parent
)
137 if (parent
->comm_set
) {
140 self
->comm
= strdup(parent
->comm
);
143 self
->comm_set
= true;
146 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
147 if (map_groups__clone(&self
->mg
, &parent
->mg
, i
) < 0)
152 size_t perf_session__fprintf(struct perf_session
*self
, FILE *fp
)
157 for (nd
= rb_first(&self
->threads
); nd
; nd
= rb_next(nd
)) {
158 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
160 ret
+= thread__fprintf(pos
, fp
);