blob: d5d3b22250f36e397f15c1298a47fcc6d2ccf360 [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
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020010static struct thread *thread__new(pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020011{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -020012 struct thread *self = zalloc(sizeof(*self));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020013
14 if (self != NULL) {
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020015 map_groups__init(&self->mg);
16 self->pid = pid;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020017 self->comm = malloc(32);
18 if (self->comm)
19 snprintf(self->comm, 32, ":%d", self->pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020020 }
21
22 return self;
23}
24
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030025void thread__delete(struct thread *self)
26{
27 map_groups__exit(&self->mg);
28 free(self->comm);
29 free(self);
30}
31
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020032int thread__set_comm(struct thread *self, const char *comm)
33{
David S. Miller4385d582010-02-26 12:08:34 -030034 int err;
35
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020036 if (self->comm)
37 free(self->comm);
38 self->comm = strdup(comm);
David S. Miller4385d582010-02-26 12:08:34 -030039 err = self->comm == NULL ? -ENOMEM : 0;
40 if (!err) {
41 self->comm_set = true;
42 map_groups__flush(&self->mg);
43 }
44 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020045}
46
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020047int thread__comm_len(struct thread *self)
48{
49 if (!self->comm_len) {
50 if (!self->comm)
51 return 0;
52 self->comm_len = strlen(self->comm);
53 }
54
55 return self->comm_len;
56}
57
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020058static size_t thread__fprintf(struct thread *self, FILE *fp)
59{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020060 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -030061 map_groups__fprintf(&self->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020062}
63
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020064struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020065{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020066 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020067 struct rb_node *parent = NULL;
68 struct thread *th;
69
70 /*
71 * Font-end cache - PID lookups come in blocks,
72 * so most of the time we dont have to look up
73 * the full rbtree:
74 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020075 if (self->last_match && self->last_match->pid == pid)
76 return self->last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020077
78 while (*p != NULL) {
79 parent = *p;
80 th = rb_entry(parent, struct thread, rb_node);
81
82 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020083 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020084 return th;
85 }
86
87 if (pid < th->pid)
88 p = &(*p)->rb_left;
89 else
90 p = &(*p)->rb_right;
91 }
92
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020093 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020094 if (th != NULL) {
95 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020096 rb_insert_color(&th->rb_node, &self->threads);
97 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020098 }
99
100 return th;
101}
102
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300103void thread__insert_map(struct thread *self, struct map *map)
104{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300105 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200106 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200107}
108
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200109int thread__fork(struct thread *self, struct thread *parent)
110{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200111 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200112
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200113 if (parent->comm_set) {
114 if (self->comm)
115 free(self->comm);
116 self->comm = strdup(parent->comm);
117 if (!self->comm)
118 return -ENOMEM;
119 self->comm_set = true;
120 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200121
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200122 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200123 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200124 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200125 return 0;
126}
127
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200128size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200129{
130 size_t ret = 0;
131 struct rb_node *nd;
132
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200133 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200134 struct thread *pos = rb_entry(nd, struct thread, rb_node);
135
136 ret += thread__fprintf(pos, fp);
137 }
138
139 return ret;
140}