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 | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 9 | #include "comm.h" |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 10 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 11 | struct thread *thread__new(pid_t pid, pid_t tid) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 12 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 13 | char *comm_str; |
| 14 | struct comm *comm; |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 15 | struct thread *thread = zalloc(sizeof(*thread)); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 17 | if (thread != NULL) { |
| 18 | map_groups__init(&thread->mg); |
| 19 | thread->pid_ = pid; |
| 20 | thread->tid = tid; |
| 21 | thread->ppid = -1; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 22 | INIT_LIST_HEAD(&thread->comm_list); |
| 23 | |
| 24 | comm_str = malloc(32); |
| 25 | if (!comm_str) |
| 26 | goto err_thread; |
| 27 | |
| 28 | snprintf(comm_str, 32, ":%d", tid); |
| 29 | comm = comm__new(comm_str, 0); |
| 30 | free(comm_str); |
| 31 | if (!comm) |
| 32 | goto err_thread; |
| 33 | |
| 34 | list_add(&comm->list, &thread->comm_list); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 37 | return thread; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 38 | |
| 39 | err_thread: |
| 40 | free(thread); |
| 41 | return NULL; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 44 | void thread__delete(struct thread *thread) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 45 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 46 | struct comm *comm, *tmp; |
| 47 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 48 | map_groups__exit(&thread->mg); |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 49 | list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) { |
| 50 | list_del(&comm->list); |
| 51 | comm__free(comm); |
| 52 | } |
| 53 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 54 | free(thread); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 55 | } |
| 56 | |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 57 | struct comm *thread__comm(const struct thread *thread) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 58 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 59 | if (list_empty(&thread->comm_list)) |
| 60 | return NULL; |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 61 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 62 | return list_first_entry(&thread->comm_list, struct comm, list); |
| 63 | } |
| 64 | |
| 65 | /* CHECKME: time should always be 0 if event aren't ordered */ |
| 66 | int thread__set_comm(struct thread *thread, const char *str, u64 timestamp) |
| 67 | { |
| 68 | struct comm *new, *curr = thread__comm(thread); |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 69 | int err; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 70 | |
| 71 | /* Override latest entry if it had no specific time coverage */ |
| 72 | if (!curr->start) { |
Frederic Weisbecker | 3178f58 | 2014-01-14 16:37:14 +0100 | [diff] [blame] | 73 | err = comm__override(curr, str, timestamp); |
| 74 | if (err) |
| 75 | return err; |
Frederic Weisbecker | a5285ad | 2013-11-16 02:02:09 +0100 | [diff] [blame] | 76 | } else { |
| 77 | new = comm__new(str, timestamp); |
| 78 | if (!new) |
| 79 | return -ENOMEM; |
| 80 | list_add(&new->list, &thread->comm_list); |
David S. Miller | 4385d58 | 2010-02-26 12:08:34 -0300 | [diff] [blame] | 81 | } |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 82 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 83 | thread->comm_set = true; |
| 84 | |
| 85 | return 0; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 88 | const char *thread__comm_str(const struct thread *thread) |
| 89 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 90 | const struct comm *comm = thread__comm(thread); |
| 91 | |
| 92 | if (!comm) |
| 93 | return NULL; |
| 94 | |
| 95 | return comm__str(comm); |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 98 | /* CHECKME: it should probably better return the max comm len from its comm list */ |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 99 | int thread__comm_len(struct thread *thread) |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 100 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 101 | if (!thread->comm_len) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 102 | const char *comm = thread__comm_str(thread); |
| 103 | if (!comm) |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 104 | return 0; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 105 | thread->comm_len = strlen(comm); |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 108 | return thread->comm_len; |
Frederic Weisbecker | a4fb581 | 2009-10-22 23:23:23 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 111 | size_t thread__fprintf(struct thread *thread, FILE *fp) |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 112 | { |
Frederic Weisbecker | b9c5143 | 2013-09-11 14:46:56 +0200 | [diff] [blame] | 113 | return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 114 | map_groups__fprintf(&thread->mg, verbose, fp); |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 117 | void thread__insert_map(struct thread *thread, struct map *map) |
Arnaldo Carvalho de Melo | 1b46cdd | 2009-09-28 14:48:46 -0300 | [diff] [blame] | 118 | { |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 119 | map_groups__fixup_overlappings(&thread->mg, map, verbose, stderr); |
| 120 | map_groups__insert(&thread->mg, map); |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 121 | } |
| 122 | |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 123 | int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 124 | { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 125 | int i, err; |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 126 | |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 127 | if (parent->comm_set) { |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 128 | const char *comm = thread__comm_str(parent); |
| 129 | if (!comm) |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 130 | return -ENOMEM; |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 131 | err = thread__set_comm(thread, comm, timestamp); |
David Ahern | 8d00be8 | 2013-12-10 21:35:38 -0700 | [diff] [blame] | 132 | if (err) |
Frederic Weisbecker | 1902efe | 2013-09-11 16:56:44 +0200 | [diff] [blame] | 133 | return err; |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 134 | thread->comm_set = true; |
Arnaldo Carvalho de Melo | faa5c5c | 2010-02-19 23:02:07 -0200 | [diff] [blame] | 135 | } |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 136 | |
Arnaldo Carvalho de Melo | 95011c6 | 2009-11-27 16:29:20 -0200 | [diff] [blame] | 137 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 138 | if (map_groups__clone(&thread->mg, &parent->mg, i) < 0) |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 139 | return -ENOMEM; |
David Ahern | 70c57ef | 2013-05-25 22:47:10 -0600 | [diff] [blame] | 140 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 141 | thread->ppid = parent->tid; |
David Ahern | 70c57ef | 2013-05-25 22:47:10 -0600 | [diff] [blame] | 142 | |
Frederic Weisbecker | 6baa0a5 | 2009-08-14 12:21:53 +0200 | [diff] [blame] | 143 | return 0; |
| 144 | } |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 145 | |
| 146 | void thread__find_cpumode_addr_location(struct thread *thread, |
| 147 | struct machine *machine, |
| 148 | enum map_type type, u64 addr, |
| 149 | struct addr_location *al) |
| 150 | { |
| 151 | size_t i; |
| 152 | const u8 const cpumodes[] = { |
| 153 | PERF_RECORD_MISC_USER, |
| 154 | PERF_RECORD_MISC_KERNEL, |
| 155 | PERF_RECORD_MISC_GUEST_USER, |
| 156 | PERF_RECORD_MISC_GUEST_KERNEL |
| 157 | }; |
| 158 | |
| 159 | for (i = 0; i < ARRAY_SIZE(cpumodes); i++) { |
| 160 | thread__find_addr_location(thread, machine, cpumodes[i], type, |
| 161 | addr, al); |
| 162 | if (al->map) |
| 163 | break; |
| 164 | } |
| 165 | } |