aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
authorAdrian Hunter <adrian.hunter@intel.com>2014-10-30 10:09:42 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2014-11-03 15:10:59 -0500
commit00447ccdf3335ea467841fc3c7d65ffd30748895 (patch)
treedca3b71692d561e9b6a3e7f721f9f814bc5dfde4 /tools/perf
parent05b2537e8dc778bb00284f8fc86b83797b8f1d37 (diff)
perf tools: Add a thread stack for synthesizing call chains
Add a thread stack for synthesizing call chains from call and return events. Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@gmail.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/r/1414678188-14946-2-git-send-email-adrian.hunter@intel.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/Makefile.perf2
-rw-r--r--tools/perf/util/event.h26
-rw-r--r--tools/perf/util/thread-stack.c172
-rw-r--r--tools/perf/util/thread-stack.h32
-rw-r--r--tools/perf/util/thread.c3
-rw-r--r--tools/perf/util/thread.h3
6 files changed, 238 insertions, 0 deletions
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 3caf7dab50e8..0ebcc4ad0244 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -317,6 +317,7 @@ LIB_H += ui/util.h
317LIB_H += ui/ui.h 317LIB_H += ui/ui.h
318LIB_H += util/data.h 318LIB_H += util/data.h
319LIB_H += util/kvm-stat.h 319LIB_H += util/kvm-stat.h
320LIB_H += util/thread-stack.h
320 321
321LIB_OBJS += $(OUTPUT)util/abspath.o 322LIB_OBJS += $(OUTPUT)util/abspath.o
322LIB_OBJS += $(OUTPUT)util/alias.o 323LIB_OBJS += $(OUTPUT)util/alias.o
@@ -394,6 +395,7 @@ LIB_OBJS += $(OUTPUT)util/srcline.o
394LIB_OBJS += $(OUTPUT)util/data.o 395LIB_OBJS += $(OUTPUT)util/data.o
395LIB_OBJS += $(OUTPUT)util/tsc.o 396LIB_OBJS += $(OUTPUT)util/tsc.o
396LIB_OBJS += $(OUTPUT)util/cloexec.o 397LIB_OBJS += $(OUTPUT)util/cloexec.o
398LIB_OBJS += $(OUTPUT)util/thread-stack.o
397 399
398LIB_OBJS += $(OUTPUT)ui/setup.o 400LIB_OBJS += $(OUTPUT)ui/setup.o
399LIB_OBJS += $(OUTPUT)ui/helpline.o 401LIB_OBJS += $(OUTPUT)ui/helpline.o
diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h
index 8c7fe9d64e79..7be389735402 100644
--- a/tools/perf/util/event.h
+++ b/tools/perf/util/event.h
@@ -143,6 +143,32 @@ struct branch_stack {
143 struct branch_entry entries[0]; 143 struct branch_entry entries[0];
144}; 144};
145 145
146enum {
147 PERF_IP_FLAG_BRANCH = 1ULL << 0,
148 PERF_IP_FLAG_CALL = 1ULL << 1,
149 PERF_IP_FLAG_RETURN = 1ULL << 2,
150 PERF_IP_FLAG_CONDITIONAL = 1ULL << 3,
151 PERF_IP_FLAG_SYSCALLRET = 1ULL << 4,
152 PERF_IP_FLAG_ASYNC = 1ULL << 5,
153 PERF_IP_FLAG_INTERRUPT = 1ULL << 6,
154 PERF_IP_FLAG_TX_ABORT = 1ULL << 7,
155 PERF_IP_FLAG_TRACE_BEGIN = 1ULL << 8,
156 PERF_IP_FLAG_TRACE_END = 1ULL << 9,
157 PERF_IP_FLAG_IN_TX = 1ULL << 10,
158};
159
160#define PERF_BRANCH_MASK (\
161 PERF_IP_FLAG_BRANCH |\
162 PERF_IP_FLAG_CALL |\
163 PERF_IP_FLAG_RETURN |\
164 PERF_IP_FLAG_CONDITIONAL |\
165 PERF_IP_FLAG_SYSCALLRET |\
166 PERF_IP_FLAG_ASYNC |\
167 PERF_IP_FLAG_INTERRUPT |\
168 PERF_IP_FLAG_TX_ABORT |\
169 PERF_IP_FLAG_TRACE_BEGIN |\
170 PERF_IP_FLAG_TRACE_END)
171
146struct perf_sample { 172struct perf_sample {
147 u64 ip; 173 u64 ip;
148 u32 pid, tid; 174 u32 pid, tid;
diff --git a/tools/perf/util/thread-stack.c b/tools/perf/util/thread-stack.c
new file mode 100644
index 000000000000..85b60d2e738f
--- /dev/null
+++ b/tools/perf/util/thread-stack.c
@@ -0,0 +1,172 @@
1/*
2 * thread-stack.c: Synthesize a thread's stack using call / return events
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#include "thread.h"
17#include "event.h"
18#include "util.h"
19#include "debug.h"
20#include "thread-stack.h"
21
22#define STACK_GROWTH 4096
23
24struct thread_stack_entry {
25 u64 ret_addr;
26};
27
28struct thread_stack {
29 struct thread_stack_entry *stack;
30 size_t cnt;
31 size_t sz;
32 u64 trace_nr;
33};
34
35static int thread_stack__grow(struct thread_stack *ts)
36{
37 struct thread_stack_entry *new_stack;
38 size_t sz, new_sz;
39
40 new_sz = ts->sz + STACK_GROWTH;
41 sz = new_sz * sizeof(struct thread_stack_entry);
42
43 new_stack = realloc(ts->stack, sz);
44 if (!new_stack)
45 return -ENOMEM;
46
47 ts->stack = new_stack;
48 ts->sz = new_sz;
49
50 return 0;
51}
52
53static struct thread_stack *thread_stack__new(void)
54{
55 struct thread_stack *ts;
56
57 ts = zalloc(sizeof(struct thread_stack));
58 if (!ts)
59 return NULL;
60
61 if (thread_stack__grow(ts)) {
62 free(ts);
63 return NULL;
64 }
65
66 return ts;
67}
68
69static int thread_stack__push(struct thread_stack *ts, u64 ret_addr)
70{
71 int err = 0;
72
73 if (ts->cnt == ts->sz) {
74 err = thread_stack__grow(ts);
75 if (err) {
76 pr_warning("Out of memory: discarding thread stack\n");
77 ts->cnt = 0;
78 }
79 }
80
81 ts->stack[ts->cnt++].ret_addr = ret_addr;
82
83 return err;
84}
85
86static void thread_stack__pop(struct thread_stack *ts, u64 ret_addr)
87{
88 size_t i;
89
90 /*
91 * In some cases there may be functions which are not seen to return.
92 * For example when setjmp / longjmp has been used. Or the perf context
93 * switch in the kernel which doesn't stop and start tracing in exactly
94 * the same code path. When that happens the return address will be
95 * further down the stack. If the return address is not found at all,
96 * we assume the opposite (i.e. this is a return for a call that wasn't
97 * seen for some reason) and leave the stack alone.
98 */
99 for (i = ts->cnt; i; ) {
100 if (ts->stack[--i].ret_addr == ret_addr) {
101 ts->cnt = i;
102 return;
103 }
104 }
105}
106
107int thread_stack__event(struct thread *thread, u32 flags, u64 from_ip,
108 u64 to_ip, u16 insn_len, u64 trace_nr)
109{
110 if (!thread)
111 return -EINVAL;
112
113 if (!thread->ts) {
114 thread->ts = thread_stack__new();
115 if (!thread->ts) {
116 pr_warning("Out of memory: no thread stack\n");
117 return -ENOMEM;
118 }
119 thread->ts->trace_nr = trace_nr;
120 }
121
122 /*
123 * When the trace is discontinuous, the trace_nr changes. In that case
124 * the stack might be completely invalid. Better to report nothing than
125 * to report something misleading, so reset the stack count to zero.
126 */
127 if (trace_nr != thread->ts->trace_nr) {
128 thread->ts->trace_nr = trace_nr;
129 thread->ts->cnt = 0;
130 }
131
132 if (flags & PERF_IP_FLAG_CALL) {
133 u64 ret_addr;
134
135 if (!to_ip)
136 return 0;
137 ret_addr = from_ip + insn_len;
138 if (ret_addr == to_ip)
139 return 0; /* Zero-length calls are excluded */
140 return thread_stack__push(thread->ts, ret_addr);
141 } else if (flags & PERF_IP_FLAG_RETURN) {
142 if (!from_ip)
143 return 0;
144 thread_stack__pop(thread->ts, to_ip);
145 }
146
147 return 0;
148}
149
150void thread_stack__free(struct thread *thread)
151{
152 if (thread->ts) {
153 zfree(&thread->ts->stack);
154 zfree(&thread->ts);
155 }
156}
157
158void thread_stack__sample(struct thread *thread, struct ip_callchain *chain,
159 size_t sz, u64 ip)
160{
161 size_t i;
162
163 if (!thread || !thread->ts)
164 chain->nr = 1;
165 else
166 chain->nr = min(sz, thread->ts->cnt + 1);
167
168 chain->ips[0] = ip;
169
170 for (i = 1; i < chain->nr; i++)
171 chain->ips[i] = thread->ts->stack[thread->ts->cnt - i].ret_addr;
172}
diff --git a/tools/perf/util/thread-stack.h b/tools/perf/util/thread-stack.h
new file mode 100644
index 000000000000..7c41579aec74
--- /dev/null
+++ b/tools/perf/util/thread-stack.h
@@ -0,0 +1,32 @@
1/*
2 * thread-stack.h: Synthesize a thread's stack using call / return events
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#ifndef __PERF_THREAD_STACK_H
17#define __PERF_THREAD_STACK_H
18
19#include <sys/types.h>
20
21#include <linux/types.h>
22
23struct thread;
24struct ip_callchain;
25
26int thread_stack__event(struct thread *thread, u32 flags, u64 from_ip,
27 u64 to_ip, u16 insn_len, u64 trace_nr);
28void thread_stack__sample(struct thread *thread, struct ip_callchain *chain,
29 size_t sz, u64 ip);
30void thread_stack__free(struct thread *thread);
31
32#endif
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index bf5bf858b7f6..a2157f0ef1df 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -4,6 +4,7 @@
4#include <string.h> 4#include <string.h>
5#include "session.h" 5#include "session.h"
6#include "thread.h" 6#include "thread.h"
7#include "thread-stack.h"
7#include "util.h" 8#include "util.h"
8#include "debug.h" 9#include "debug.h"
9#include "comm.h" 10#include "comm.h"
@@ -66,6 +67,8 @@ void thread__delete(struct thread *thread)
66{ 67{
67 struct comm *comm, *tmp; 68 struct comm *comm, *tmp;
68 69
70 thread_stack__free(thread);
71
69 if (thread->mg) { 72 if (thread->mg) {
70 map_groups__put(thread->mg); 73 map_groups__put(thread->mg);
71 thread->mg = NULL; 74 thread->mg = NULL;
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index d34cf5c0d0d9..160fd066a7d1 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -8,6 +8,8 @@
8#include "symbol.h" 8#include "symbol.h"
9#include <strlist.h> 9#include <strlist.h>
10 10
11struct thread_stack;
12
11struct thread { 13struct thread {
12 union { 14 union {
13 struct rb_node rb_node; 15 struct rb_node rb_node;
@@ -26,6 +28,7 @@ struct thread {
26 u64 db_id; 28 u64 db_id;
27 29
28 void *priv; 30 void *priv;
31 struct thread_stack *ts;
29}; 32};
30 33
31struct machine; 34struct machine;