10 static struct thread
*thread__new(pid_t pid
)
12 struct thread
*self
= zalloc(sizeof(*self
));
15 map_groups__init(&self
->mg
);
17 self
->comm
= malloc(32);
19 snprintf(self
->comm
, 32, ":%d", self
->pid
);
25 void thread__delete(struct thread
*self
)
27 map_groups__exit(&self
->mg
);
32 int thread__set_comm(struct thread
*self
, const char *comm
)
38 self
->comm
= strdup(comm
);
39 err
= self
->comm
== NULL
? -ENOMEM
: 0;
41 self
->comm_set
= true;
42 map_groups__flush(&self
->mg
);
47 int thread__comm_len(struct thread
*self
)
49 if (!self
->comm_len
) {
52 self
->comm_len
= strlen(self
->comm
);
55 return self
->comm_len
;
58 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
60 return fprintf(fp
, "Thread %d %s\n", self
->pid
, self
->comm
) +
61 map_groups__fprintf(&self
->mg
, verbose
, fp
);
64 struct thread
*perf_session__findnew(struct perf_session
*self
, pid_t pid
)
66 struct rb_node
**p
= &self
->threads
.rb_node
;
67 struct rb_node
*parent
= NULL
;
71 * Font-end cache - PID lookups come in blocks,
72 * so most of the time we dont have to look up
75 if (self
->last_match
&& self
->last_match
->pid
== pid
)
76 return self
->last_match
;
80 th
= rb_entry(parent
, struct thread
, rb_node
);
83 self
->last_match
= th
;
93 th
= thread__new(pid
);
95 rb_link_node(&th
->rb_node
, parent
, p
);
96 rb_insert_color(&th
->rb_node
, &self
->threads
);
97 self
->last_match
= th
;
103 void thread__insert_map(struct thread
*self
, struct map
*map
)
105 map_groups__fixup_overlappings(&self
->mg
, map
, verbose
, stderr
);
106 map_groups__insert(&self
->mg
, map
);
109 int thread__fork(struct thread
*self
, struct thread
*parent
)
113 if (parent
->comm_set
) {
116 self
->comm
= strdup(parent
->comm
);
119 self
->comm_set
= true;
122 for (i
= 0; i
< MAP__NR_TYPES
; ++i
)
123 if (map_groups__clone(&self
->mg
, &parent
->mg
, i
) < 0)
128 size_t perf_session__fprintf(struct perf_session
*self
, FILE *fp
)
133 for (nd
= rb_first(&self
->threads
); nd
; nd
= rb_next(nd
)) {
134 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
136 ret
+= thread__fprintf(pos
, fp
);