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 | |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 10 | static 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 | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 57 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
| 58 | { |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 59 | return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) + |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 60 | map_groups__fprintf(&self->mg, verbose, fp); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 63 | struct thread *machine__findnew_thread(struct machine *self, pid_t pid) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 64 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 65 | struct rb_node **p = &self->threads.rb_node; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 66 | struct rb_node *parent = NULL; |
| 67 | struct thread *th; |
| 68 | |
| 69 | /* |
| 70 | * Font-end cache - PID lookups come in blocks, |
| 71 | * so most of the time we dont have to look up |
| 72 | * the full rbtree: |
| 73 | */ |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 74 | if (self->last_match && self->last_match->pid == pid) |
| 75 | return self->last_match; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 76 | |
| 77 | while (*p != NULL) { |
| 78 | parent = *p; |
| 79 | th = rb_entry(parent, struct thread, rb_node); |
| 80 | |
| 81 | if (th->pid == pid) { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 82 | self->last_match = th; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 83 | return th; |
| 84 | } |
| 85 | |
| 86 | if (pid < th->pid) |
| 87 | p = &(*p)->rb_left; |
| 88 | else |
| 89 | p = &(*p)->rb_right; |
| 90 | } |
| 91 | |
Frederic Weisbecker | 97ea1a7 | 2009-10-08 21:04:17 +0200 | [diff] [blame] | 92 | th = thread__new(pid); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 93 | if (th != NULL) { |
| 94 | rb_link_node(&th->rb_node, parent, p); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 95 | rb_insert_color(&th->rb_node, &self->threads); |
| 96 | self->last_match = th; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return th; |
| 100 | } |
| 101 | |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 102 | void thread__insert_map(struct thread *self, struct map *map) |
| 103 | { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 104 | map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 105 | map_groups__insert(&self->mg, map); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 106 | } |
| 107 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 108 | int thread__fork(struct thread *self, struct thread *parent) |
| 109 | { |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 110 | int i; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 111 | |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 112 | if (parent->comm_set) { |
| 113 | if (self->comm) |
| 114 | free(self->comm); |
| 115 | self->comm = strdup(parent->comm); |
| 116 | if (!self->comm) |
| 117 | return -ENOMEM; |
| 118 | self->comm_set = true; |
| 119 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 120 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 121 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 9958e1f | 2009-12-11 14:50:36 -0200 | [diff] [blame] | 122 | if (map_groups__clone(&self->mg, &parent->mg, i) < 0) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 123 | return -ENOMEM; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 127 | size_t machine__fprintf(struct machine *machine, FILE *fp) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 128 | { |
| 129 | size_t ret = 0; |
| 130 | struct rb_node *nd; |
| 131 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 132 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 133 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 134 | |
| 135 | ret += thread__fprintf(pos, fp); |
| 136 | } |
| 137 | |
| 138 | return ret; |
| 139 | } |