blob: 6feeb88eb5b0165f86c535601f0250553df4aaba [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
Adrian Hunter38051232013-07-04 16:20:31 +030010struct thread *thread__new(pid_t tid)
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);
Adrian Hunter38051232013-07-04 16:20:31 +030016 self->tid = tid;
David Ahern70c57ef2013-05-25 22:47:10 -060017 self->ppid = -1;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020018 self->comm = malloc(32);
19 if (self->comm)
Adrian Hunter38051232013-07-04 16:20:31 +030020 snprintf(self->comm, 32, ":%d", self->tid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020021 }
22
23 return self;
24}
25
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030026void thread__delete(struct thread *self)
27{
28 map_groups__exit(&self->mg);
29 free(self->comm);
30 free(self);
31}
32
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020033int thread__set_comm(struct thread *self, const char *comm)
34{
David S. Miller4385d582010-02-26 12:08:34 -030035 int err;
36
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020037 if (self->comm)
38 free(self->comm);
39 self->comm = strdup(comm);
David S. Miller4385d582010-02-26 12:08:34 -030040 err = self->comm == NULL ? -ENOMEM : 0;
41 if (!err) {
42 self->comm_set = true;
David S. Miller4385d582010-02-26 12:08:34 -030043 }
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 Melo3f067dc2012-12-07 17:39:39 -030058size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020059{
Adrian Hunter38051232013-07-04 16:20:31 +030060 return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) +
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030061 map_groups__fprintf(&thread->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;
David Ahern70c57ef2013-05-25 22:47:10 -060086
Adrian Hunter38051232013-07-04 16:20:31 +030087 self->ppid = parent->tid;
David Ahern70c57ef2013-05-25 22:47:10 -060088
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020089 return 0;
90}