perf tools: Add cpu to struct thread
[deliverable/linux.git] / tools / perf / util / thread.c
CommitLineData
6baa0a5a
FW
1#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
b3165f41 5#include "session.h"
6baa0a5a
FW
6#include "thread.h"
7#include "util.h"
6e086437 8#include "debug.h"
1902efe7 9#include "comm.h"
6baa0a5a 10
cddcef60
JO
11int thread__init_map_groups(struct thread *thread, struct machine *machine)
12{
13 struct thread *leader;
14 pid_t pid = thread->pid_;
15
1fcb8768 16 if (pid == thread->tid || pid == -1) {
cddcef60
JO
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
99d725fc 27struct thread *thread__new(pid_t pid, pid_t tid)
6baa0a5a 28{
1902efe7
FW
29 char *comm_str;
30 struct comm *comm;
c824c433 31 struct thread *thread = zalloc(sizeof(*thread));
6baa0a5a 32
c824c433 33 if (thread != NULL) {
c824c433
ACM
34 thread->pid_ = pid;
35 thread->tid = tid;
36 thread->ppid = -1;
bf49c35f 37 thread->cpu = -1;
1902efe7
FW
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);
46 free(comm_str);
47 if (!comm)
48 goto err_thread;
49
50 list_add(&comm->list, &thread->comm_list);
6baa0a5a
FW
51 }
52
c824c433 53 return thread;
1902efe7
FW
54
55err_thread:
56 free(thread);
57 return NULL;
6baa0a5a
FW
58}
59
c824c433 60void thread__delete(struct thread *thread)
591765fd 61{
1902efe7
FW
62 struct comm *comm, *tmp;
63
9608b84e
AH
64 if (thread->mg) {
65 map_groups__put(thread->mg);
66 thread->mg = NULL;
67 }
1902efe7
FW
68 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
69 list_del(&comm->list);
70 comm__free(comm);
71 }
72
c824c433 73 free(thread);
591765fd
ACM
74}
75
4dfced35 76struct comm *thread__comm(const struct thread *thread)
6baa0a5a 77{
1902efe7
FW
78 if (list_empty(&thread->comm_list))
79 return NULL;
4385d580 80
1902efe7
FW
81 return list_first_entry(&thread->comm_list, struct comm, list);
82}
83
84/* CHECKME: time should always be 0 if event aren't ordered */
85int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
86{
87 struct comm *new, *curr = thread__comm(thread);
3178f58b 88 int err;
1902efe7
FW
89
90 /* Override latest entry if it had no specific time coverage */
91 if (!curr->start) {
3178f58b
FW
92 err = comm__override(curr, str, timestamp);
93 if (err)
94 return err;
a5285ad9
FW
95 } else {
96 new = comm__new(str, timestamp);
97 if (!new)
98 return -ENOMEM;
99 list_add(&new->list, &thread->comm_list);
4385d580 100 }
1902efe7 101
1902efe7
FW
102 thread->comm_set = true;
103
104 return 0;
6baa0a5a
FW
105}
106
b9c5143a
FW
107const char *thread__comm_str(const struct thread *thread)
108{
1902efe7
FW
109 const struct comm *comm = thread__comm(thread);
110
111 if (!comm)
112 return NULL;
113
114 return comm__str(comm);
b9c5143a
FW
115}
116
1902efe7 117/* CHECKME: it should probably better return the max comm len from its comm list */
c824c433 118int thread__comm_len(struct thread *thread)
a4fb581b 119{
c824c433 120 if (!thread->comm_len) {
1902efe7
FW
121 const char *comm = thread__comm_str(thread);
122 if (!comm)
a4fb581b 123 return 0;
1902efe7 124 thread->comm_len = strlen(comm);
a4fb581b
FW
125 }
126
c824c433 127 return thread->comm_len;
a4fb581b
FW
128}
129
3f067dca 130size_t thread__fprintf(struct thread *thread, FILE *fp)
9958e1f0 131{
b9c5143a 132 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
acebd408 133 map_groups__fprintf(thread->mg, fp);
6baa0a5a
FW
134}
135
c824c433 136void thread__insert_map(struct thread *thread, struct map *map)
1b46cddf 137{
acebd408 138 map_groups__fixup_overlappings(thread->mg, map, stderr);
93d5731d 139 map_groups__insert(thread->mg, map);
6baa0a5a
FW
140}
141
cddcef60
JO
142static int thread__clone_map_groups(struct thread *thread,
143 struct thread *parent)
144{
145 int i;
146
147 /* This is new thread, we share map groups for process. */
148 if (thread->pid_ == parent->pid_)
149 return 0;
150
151 /* But this one is new process, copy maps. */
152 for (i = 0; i < MAP__NR_TYPES; ++i)
153 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
154 return -ENOMEM;
155
156 return 0;
157}
158
1902efe7 159int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
95011c60 160{
cddcef60 161 int err;
6baa0a5a 162
faa5c5c3 163 if (parent->comm_set) {
1902efe7
FW
164 const char *comm = thread__comm_str(parent);
165 if (!comm)
faa5c5c3 166 return -ENOMEM;
1902efe7 167 err = thread__set_comm(thread, comm, timestamp);
8d00be81 168 if (err)
1902efe7 169 return err;
c824c433 170 thread->comm_set = true;
faa5c5c3 171 }
6baa0a5a 172
c824c433 173 thread->ppid = parent->tid;
cddcef60 174 return thread__clone_map_groups(thread, parent);
6baa0a5a 175}
52a3cb8c
ACM
176
177void thread__find_cpumode_addr_location(struct thread *thread,
178 struct machine *machine,
179 enum map_type type, u64 addr,
180 struct addr_location *al)
181{
182 size_t i;
183 const u8 const cpumodes[] = {
184 PERF_RECORD_MISC_USER,
185 PERF_RECORD_MISC_KERNEL,
186 PERF_RECORD_MISC_GUEST_USER,
187 PERF_RECORD_MISC_GUEST_KERNEL
188 };
189
190 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
191 thread__find_addr_location(thread, machine, cpumodes[i], type,
192 addr, al);
193 if (al->map)
194 break;
195 }
196}
This page took 0.194208 seconds and 5 git commands to generate.