blob: fe3bb1ec188767de6e2a37d586af24313d216a71 [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
Arnaldo Carvalho de Melo9d2f8e22012-10-06 15:43:20 -030010struct 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 Melo1b46cdd2009-09-28 14:48:46 -030064void thread__insert_map(struct thread *self, struct map *map)
65{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -030066 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020067 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020068}
69
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020070int thread__fork(struct thread *self, struct thread *parent)
71{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020072 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020073
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020074 if (parent->comm_set) {
75 if (self->comm)
76 free(self->comm);
77 self->comm = strdup(parent->comm);
78 if (!self->comm)
79 return -ENOMEM;
80 self->comm_set = true;
81 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020082
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020083 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020084 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020085 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020086 return 0;
87}
88
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -020089size_t machine__fprintf(struct machine *machine, FILE *fp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020090{
91 size_t ret = 0;
92 struct rb_node *nd;
93
Arnaldo Carvalho de Melob424eba2011-11-09 13:24:25 -020094 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020095 struct thread *pos = rb_entry(nd, struct thread, rb_node);
96
97 ret += thread__fprintf(pos, fp);
98 }
99
100 return ret;
101}