x86, apic: Handle a bad TSC more gracefully
[cascardo/linux.git] / tools / perf / util / thread.c
1 #include "../perf.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "session.h"
6 #include "thread.h"
7 #include "util.h"
8 #include "debug.h"
9 #include "comm.h"
10
11 int thread__init_map_groups(struct thread *thread, struct machine *machine)
12 {
13         struct thread *leader;
14         pid_t pid = thread->pid_;
15
16         if (pid == thread->tid || pid == -1) {
17                 thread->mg = map_groups__new();
18         } else {
19                 leader = machine__findnew_thread(machine, pid, pid);
20                 if (leader)
21                         thread->mg = map_groups__get(leader->mg);
22         }
23
24         return thread->mg ? 0 : -1;
25 }
26
27 struct thread *thread__new(pid_t pid, pid_t tid)
28 {
29         char *comm_str;
30         struct comm *comm;
31         struct thread *thread = zalloc(sizeof(*thread));
32
33         if (thread != NULL) {
34                 thread->pid_ = pid;
35                 thread->tid = tid;
36                 thread->ppid = -1;
37                 thread->cpu = -1;
38                 INIT_LIST_HEAD(&thread->comm_list);
39
40                 comm_str = malloc(32);
41                 if (!comm_str)
42                         goto err_thread;
43
44                 snprintf(comm_str, 32, ":%d", tid);
45                 comm = comm__new(comm_str, 0, false);
46                 free(comm_str);
47                 if (!comm)
48                         goto err_thread;
49
50                 list_add(&comm->list, &thread->comm_list);
51         }
52
53         return thread;
54
55 err_thread:
56         free(thread);
57         return NULL;
58 }
59
60 void thread__delete(struct thread *thread)
61 {
62         struct comm *comm, *tmp;
63
64         if (thread->mg) {
65                 map_groups__put(thread->mg);
66                 thread->mg = NULL;
67         }
68         list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
69                 list_del(&comm->list);
70                 comm__free(comm);
71         }
72
73         free(thread);
74 }
75
76 struct comm *thread__comm(const struct thread *thread)
77 {
78         if (list_empty(&thread->comm_list))
79                 return NULL;
80
81         return list_first_entry(&thread->comm_list, struct comm, list);
82 }
83
84 struct comm *thread__exec_comm(const struct thread *thread)
85 {
86         struct comm *comm, *last = NULL;
87
88         list_for_each_entry(comm, &thread->comm_list, list) {
89                 if (comm->exec)
90                         return comm;
91                 last = comm;
92         }
93
94         return last;
95 }
96
97 /* CHECKME: time should always be 0 if event aren't ordered */
98 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
99                        bool exec)
100 {
101         struct comm *new, *curr = thread__comm(thread);
102         int err;
103
104         /* Override latest entry if it had no specific time coverage */
105         if (!curr->start && !curr->exec) {
106                 err = comm__override(curr, str, timestamp, exec);
107                 if (err)
108                         return err;
109         } else {
110                 new = comm__new(str, timestamp, exec);
111                 if (!new)
112                         return -ENOMEM;
113                 list_add(&new->list, &thread->comm_list);
114         }
115
116         thread->comm_set = true;
117
118         return 0;
119 }
120
121 const char *thread__comm_str(const struct thread *thread)
122 {
123         const struct comm *comm = thread__comm(thread);
124
125         if (!comm)
126                 return NULL;
127
128         return comm__str(comm);
129 }
130
131 /* CHECKME: it should probably better return the max comm len from its comm list */
132 int thread__comm_len(struct thread *thread)
133 {
134         if (!thread->comm_len) {
135                 const char *comm = thread__comm_str(thread);
136                 if (!comm)
137                         return 0;
138                 thread->comm_len = strlen(comm);
139         }
140
141         return thread->comm_len;
142 }
143
144 size_t thread__fprintf(struct thread *thread, FILE *fp)
145 {
146         return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
147                map_groups__fprintf(thread->mg, fp);
148 }
149
150 void thread__insert_map(struct thread *thread, struct map *map)
151 {
152         map_groups__fixup_overlappings(thread->mg, map, stderr);
153         map_groups__insert(thread->mg, map);
154 }
155
156 static int thread__clone_map_groups(struct thread *thread,
157                                     struct thread *parent)
158 {
159         int i;
160
161         /* This is new thread, we share map groups for process. */
162         if (thread->pid_ == parent->pid_)
163                 return 0;
164
165         /* But this one is new process, copy maps. */
166         for (i = 0; i < MAP__NR_TYPES; ++i)
167                 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
168                         return -ENOMEM;
169
170         return 0;
171 }
172
173 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
174 {
175         int err;
176
177         if (parent->comm_set) {
178                 const char *comm = thread__comm_str(parent);
179                 if (!comm)
180                         return -ENOMEM;
181                 err = thread__set_comm(thread, comm, timestamp);
182                 if (err)
183                         return err;
184                 thread->comm_set = true;
185         }
186
187         thread->ppid = parent->tid;
188         return thread__clone_map_groups(thread, parent);
189 }
190
191 void thread__find_cpumode_addr_location(struct thread *thread,
192                                         struct machine *machine,
193                                         enum map_type type, u64 addr,
194                                         struct addr_location *al)
195 {
196         size_t i;
197         const u8 const cpumodes[] = {
198                 PERF_RECORD_MISC_USER,
199                 PERF_RECORD_MISC_KERNEL,
200                 PERF_RECORD_MISC_GUEST_USER,
201                 PERF_RECORD_MISC_GUEST_KERNEL
202         };
203
204         for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
205                 thread__find_addr_location(thread, machine, cpumodes[i], type,
206                                            addr, al);
207                 if (al->map)
208                         break;
209         }
210 }