Skip to content

Commit 00447cc

Browse files
ahunter6acmel
authored andcommitted
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>
1 parent 05b2537 commit 00447cc

6 files changed

Lines changed: 238 additions & 0 deletions

File tree

‎tools/perf/Makefile.perf‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ LIB_H += ui/util.h
317317
LIB_H += ui/ui.h
318318
LIB_H += util/data.h
319319
LIB_H += util/kvm-stat.h
320+
LIB_H += util/thread-stack.h
320321

321322
LIB_OBJS += $(OUTPUT)util/abspath.o
322323
LIB_OBJS += $(OUTPUT)util/alias.o
@@ -394,6 +395,7 @@ LIB_OBJS += $(OUTPUT)util/srcline.o
394395
LIB_OBJS += $(OUTPUT)util/data.o
395396
LIB_OBJS += $(OUTPUT)util/tsc.o
396397
LIB_OBJS += $(OUTPUT)util/cloexec.o
398+
LIB_OBJS += $(OUTPUT)util/thread-stack.o
397399

398400
LIB_OBJS += $(OUTPUT)ui/setup.o
399401
LIB_OBJS += $(OUTPUT)ui/helpline.o

‎tools/perf/util/event.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ struct branch_stack {
143143
struct branch_entry entries[0];
144144
};
145145

146+
enum {
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+
146172
struct perf_sample {
147173
u64 ip;
148174
u32 pid, tid;

‎tools/perf/util/thread-stack.c‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
24+
struct thread_stack_entry {
25+
u64 ret_addr;
26+
};
27+
28+
struct thread_stack {
29+
struct thread_stack_entry *stack;
30+
size_t cnt;
31+
size_t sz;
32+
u64 trace_nr;
33+
};
34+
35+
static 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+
53+
static 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+
69+
static 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+
86+
static 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+
107+
int 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+
150+
void thread_stack__free(struct thread *thread)
151+
{
152+
if (thread->ts) {
153+
zfree(&thread->ts->stack);
154+
zfree(&thread->ts);
155+
}
156+
}
157+
158+
void 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+
}

‎tools/perf/util/thread-stack.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
23+
struct thread;
24+
struct ip_callchain;
25+
26+
int thread_stack__event(struct thread *thread, u32 flags, u64 from_ip,
27+
u64 to_ip, u16 insn_len, u64 trace_nr);
28+
void thread_stack__sample(struct thread *thread, struct ip_callchain *chain,
29+
size_t sz, u64 ip);
30+
void thread_stack__free(struct thread *thread);
31+
32+
#endif

‎tools/perf/util/thread.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string.h>
55
#include "session.h"
66
#include "thread.h"
7+
#include "thread-stack.h"
78
#include "util.h"
89
#include "debug.h"
910
#include "comm.h"
@@ -66,6 +67,8 @@ void thread__delete(struct thread *thread)
6667
{
6768
struct comm *comm, *tmp;
6869

70+
thread_stack__free(thread);
71+
6972
if (thread->mg) {
7073
map_groups__put(thread->mg);
7174
thread->mg = NULL;

‎tools/perf/util/thread.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "symbol.h"
99
#include <strlist.h>
1010

11+
struct thread_stack;
12+
1113
struct thread {
1214
union {
1315
struct rb_node rb_node;
@@ -26,6 +28,7 @@ struct thread {
2628
u64 db_id;
2729

2830
void *priv;
31+
struct thread_stack *ts;
2932
};
3033

3134
struct machine;

0 commit comments

Comments
 (0)