blob: 632e40e5ceca6d301a764caab574628a6ca89c9a [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;
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 Melo3f067dc2012-12-07 17:39:39 -030057size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020058{
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030059 return fprintf(fp, "Thread %d %s\n", thread->pid, thread->comm) +
60 map_groups__fprintf(&thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020061}
62
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -030063void thread__insert_map(struct thread *self, struct map *map)
64{
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -030065 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020066 map_groups__insert(&self->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020067}
68
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020069int thread__fork(struct thread *self, struct thread *parent)
70{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020071 int i;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020072
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -020073 if (parent->comm_set) {
74 if (self->comm)
75 free(self->comm);
76 self->comm = strdup(parent->comm);
77 if (!self->comm)
78 return -ENOMEM;
79 self->comm_set = true;
80 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020081
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020082 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020083 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020084 return -ENOMEM;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020085 return 0;
86}