blob: e3d4a550a703211d8fabeb9807c28a86466fe6d8 [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 Hunter99d725f2013-08-26 16:00:19 +030010struct thread *thread__new(pid_t pid, 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 Hunter99d725f2013-08-26 16:00:19 +030016 self->pid_ = pid;
Adrian Hunter38051232013-07-04 16:20:31 +030017 self->tid = tid;
David Ahern70c57ef2013-05-25 22:47:10 -060018 self->ppid = -1;
Frederic Weisbecker97ea1a72009-10-08 21:04:17 +020019 self->comm = malloc(32);
20 if (self->comm)
Adrian Hunter38051232013-07-04 16:20:31 +030021 snprintf(self->comm, 32, ":%d", self->tid);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020022 }
23
24 return self;
25}
26
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030027void thread__delete(struct thread *self)
28{
29 map_groups__exit(&self->mg);
30 free(self->comm);
31 free(self);
32}
33
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020034int thread__set_comm(struct thread *self, const char *comm)
35{
David S. Miller4385d582010-02-26 12:08:34 -030036 int err;
37
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020038 if (self->comm)
39 free(self->comm);
40 self->comm = strdup(comm);
David S. Miller4385d582010-02-26 12:08:34 -030041 err = self->comm == NULL ? -ENOMEM : 0;
42 if (!err) {
43 self->comm_set = true;
David S. Miller4385d582010-02-26 12:08:34 -030044 }
45 return err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020046}
47
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +020048int thread__comm_len(struct thread *self)
49{
50 if (!self->comm_len) {
51 if (!self->comm)
52 return 0;
53 self->comm_len = strlen(self->comm);
54 }
55
56 return self->comm_len;
57}
58
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030059size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020060{
Adrian Hunter38051232013-07-04 16:20:31 +030061 return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) +
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030062 map_groups__fprintf(&thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020063}
64
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030065void thread__insert_map(struct thread *self, struct map *map)
66{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -030067 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020068 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020069}
70
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020071int thread__fork(struct thread *self, struct thread *parent)
72{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020073 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020074
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020075 if (parent->comm_set) {
76 if (self->comm)
77 free(self->comm);
78 self->comm = strdup(parent->comm);
79 if (!self->comm)
80 return -ENOMEM;
81 self->comm_set = true;
82 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020083
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020084 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020085 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020086 return -ENOMEM;
David Ahern70c57ef2013-05-25 22:47:10 -060087
Adrian Hunter38051232013-07-04 16:20:31 +030088 self->ppid = parent->tid;
David Ahern70c57ef2013-05-25 22:47:10 -060089
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020090 return 0;
91}