blob: 8b3e5939afb6ea00ef6cd74275eae99f35de0c25 [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;
David S. Miller4385d582010-02-26 12:08:34 -030042 }
43 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020044}
45
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020046int thread__comm_len(struct thread *self)
47{
48 if (!self->comm_len) {
49 if (!self->comm)
50 return 0;
51 self->comm_len = strlen(self->comm);
52 }
53
54 return self->comm_len;
55}
56
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020057static size_t thread__fprintf(struct thread *self, FILE *fp)
58{
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020059 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -030060 map_groups__fprintf(&self->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020061}
62
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -020063struct thread *machine__findnew_thread(struct machine *self, pid_t pid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020064{
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020065 struct rb_node **p = &self->threads.rb_node;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020066 struct rb_node *parent = NULL;
67 struct thread *th;
68
69 /*
70 * Font-end cache - PID lookups come in blocks,
71 * so most of the time we dont have to look up
72 * the full rbtree:
73 */
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020074 if (self->last_match && self->last_match->pid == pid)
75 return self->last_match;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020076
77 while (*p != NULL) {
78 parent = *p;
79 th = rb_entry(parent, struct thread, rb_node);
80
81 if (th->pid == pid) {
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020082 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020083 return th;
84 }
85
86 if (pid < th->pid)
87 p = &(*p)->rb_left;
88 else
89 p = &(*p)->rb_right;
90 }
91
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020092 th = thread__new(pid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020093 if (th != NULL) {
94 rb_link_node(&th->rb_node, parent, p);
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -020095 rb_insert_color(&th->rb_node, &self->threads);
96 self->last_match = th;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020097 }
98
99 return th;
100}
101
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300102void thread__insert_map(struct thread *self, struct map *map)
103{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300104 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200105 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200106}
107
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200108int thread__fork(struct thread *self, struct thread *parent)
109{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200110 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200111
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200112 if (parent->comm_set) {
113 if (self->comm)
114 free(self->comm);
115 self->comm = strdup(parent->comm);
116 if (!self->comm)
117 return -ENOMEM;
118 self->comm_set = true;
119 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200120
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200121 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200122 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200123 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200124 return 0;
125}
126
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200127size_t machine__fprintf(struct machine *machine, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200128{
129 size_t ret = 0;
130 struct rb_node *nd;
131
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -0200132 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200133 struct thread *pos = rb_entry(nd, struct thread, rb_node);
134
135 ret += thread__fprintf(pos, fp);
136 }
137
138 return ret;
139}