blob: dc51d1632e92a4664225cac3e87ac6c1f0a825b7 [file] [log] [blame]
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02005#include "session.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02006#include "thread.h"
7#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02008#include "debug.h"
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02009#include "comm.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020010
Adrian Hunter99d725f2013-08-26 16:00:19 +030011struct thread *thread__new(pid_t pid, pid_t tid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020012{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020013 char *comm_str;
14 struct comm *comm;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030015 struct thread *thread = zalloc(sizeof(*thread));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020016
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030017 if (thread != NULL) {
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -030018 thread->mg = map_groups__new();
19 if (thread->mg == NULL)
20 goto out_free;
21
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030022 thread->pid_ = pid;
23 thread->tid = tid;
24 thread->ppid = -1;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020025 INIT_LIST_HEAD(&thread->comm_list);
26
27 comm_str = malloc(32);
28 if (!comm_str)
29 goto err_thread;
30
31 snprintf(comm_str, 32, ":%d", tid);
32 comm = comm__new(comm_str, 0);
33 free(comm_str);
34 if (!comm)
35 goto err_thread;
36
37 list_add(&comm->list, &thread->comm_list);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020038 }
39
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030040 return thread;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020041
42err_thread:
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -030043 map_groups__delete(thread->mg);
44out_free:
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020045 free(thread);
46 return NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020047}
48
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030049void thread__delete(struct thread *thread)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030050{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020051 struct comm *comm, *tmp;
52
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -030053 map_groups__delete(thread->mg);
54 thread->mg = NULL;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020055 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
56 list_del(&comm->list);
57 comm__free(comm);
58 }
59
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030060 free(thread);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030061}
62
Namhyung Kim4dfced32013-09-13 16:28:57 +090063struct comm *thread__comm(const struct thread *thread)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020064{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020065 if (list_empty(&thread->comm_list))
66 return NULL;
David S. Miller4385d582010-02-26 12:08:34 -030067
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020068 return list_first_entry(&thread->comm_list, struct comm, list);
69}
70
71/* CHECKME: time should always be 0 if event aren't ordered */
72int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
73{
74 struct comm *new, *curr = thread__comm(thread);
Frederic Weisbecker3178f582014-01-14 16:37:14 +010075 int err;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020076
77 /* Override latest entry if it had no specific time coverage */
78 if (!curr->start) {
Frederic Weisbecker3178f582014-01-14 16:37:14 +010079 err = comm__override(curr, str, timestamp);
80 if (err)
81 return err;
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +010082 } else {
83 new = comm__new(str, timestamp);
84 if (!new)
85 return -ENOMEM;
86 list_add(&new->list, &thread->comm_list);
David S. Miller4385d582010-02-26 12:08:34 -030087 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020088
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020089 thread->comm_set = true;
90
91 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020092}
93
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +020094const char *thread__comm_str(const struct thread *thread)
95{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020096 const struct comm *comm = thread__comm(thread);
97
98 if (!comm)
99 return NULL;
100
101 return comm__str(comm);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200102}
103
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200104/* CHECKME: it should probably better return the max comm len from its comm list */
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300105int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200106{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300107 if (!thread->comm_len) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200108 const char *comm = thread__comm_str(thread);
109 if (!comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200110 return 0;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200111 thread->comm_len = strlen(comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200112 }
113
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300114 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200115}
116
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300117size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200118{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200119 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300120 map_groups__fprintf(thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200121}
122
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300123void thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300124{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300125 map_groups__fixup_overlappings(thread->mg, map, verbose, stderr);
126 map_groups__insert(thread->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200127}
128
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200129int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200130{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200131 int i, err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200132
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200133 if (parent->comm_set) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200134 const char *comm = thread__comm_str(parent);
135 if (!comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200136 return -ENOMEM;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200137 err = thread__set_comm(thread, comm, timestamp);
David Ahern8d00be82013-12-10 21:35:38 -0700138 if (err)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200139 return err;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300140 thread->comm_set = true;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200141 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200142
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200143 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300144 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200145 return -ENOMEM;
David Ahern70c57ef2013-05-25 22:47:10 -0600146
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300147 thread->ppid = parent->tid;
David Ahern70c57ef2013-05-25 22:47:10 -0600148
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200149 return 0;
150}
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300151
152void thread__find_cpumode_addr_location(struct thread *thread,
153 struct machine *machine,
154 enum map_type type, u64 addr,
155 struct addr_location *al)
156{
157 size_t i;
158 const u8 const cpumodes[] = {
159 PERF_RECORD_MISC_USER,
160 PERF_RECORD_MISC_KERNEL,
161 PERF_RECORD_MISC_GUEST_USER,
162 PERF_RECORD_MISC_GUEST_KERNEL
163 };
164
165 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
166 thread__find_addr_location(thread, machine, cpumodes[i], type,
167 addr, al);
168 if (al->map)
169 break;
170 }
171}