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