Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 1 | #include "../perf.h" |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 5 | #include "session.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 6 | #include "thread.h" |
| 7 | #include "util.h" |
Frederic Weisbecker | 6e08643 | 2009-08-18 17:04:03 +0200 | [diff] [blame] | 8 | #include "debug.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 9 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 10 | struct thread *thread__new(pid_t tid) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 11 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 12 | struct thread *self = zalloc(sizeof(*self)); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 13 | |
| 14 | if (self != NULL) { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 15 | map_groups__init(&self->mg); |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 16 | self->tid = tid; |
David Ahern | 70c57ef | 2013-05-25 22:47:10 -0600 | [diff] [blame] | 17 | self->ppid = -1; |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 18 | self->comm = malloc(32); |
| 19 | if (self->comm) |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 20 | snprintf(self->comm, 32, ":%d", self->tid); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | return self; |
| 24 | } |
| 25 | |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 26 | void thread__delete(struct thread *self) |
| 27 | { |
| 28 | map_groups__exit(&self->mg); |
| 29 | free(self->comm); |
| 30 | free(self); |
| 31 | } |
| 32 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 33 | int thread__set_comm(struct thread *self, const char *comm) |
| 34 | { |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 35 | int err; |
| 36 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 37 | if (self->comm) |
| 38 | free(self->comm); |
| 39 | self->comm = strdup(comm); |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 40 | err = self->comm == NULL ? -ENOMEM : 0; |
| 41 | if (!err) { |
| 42 | self->comm_set = true; |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 43 | } |
| 44 | return err; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 47 | int 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 Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 58 | size_t thread__fprintf(struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 59 | { |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 60 | return fprintf(fp, "Thread %d %s\n", thread->tid, thread->comm) + |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 61 | map_groups__fprintf(&thread->mg, verbose, fp); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 64 | void thread__insert_map(struct thread *self, struct map *map) |
| 65 | { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 66 | map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 67 | map_groups__insert(&self->mg, map); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 68 | } |
| 69 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 70 | int thread__fork(struct thread *self, struct thread *parent) |
| 71 | { |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 72 | int i; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 73 | |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 74 | 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 Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 82 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 83 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 84 | if (map_groups__clone(&self->mg, &parent->mg, i) < 0) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 85 | return -ENOMEM; |
David Ahern | 70c57ef | 2013-05-25 22:47:10 -0600 | [diff] [blame] | 86 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 87 | self->ppid = parent->tid; |
David Ahern | 70c57ef | 2013-05-25 22:47:10 -0600 | [diff] [blame] | 88 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 89 | return 0; |
| 90 | } |