Skip to contentSkip to conversation

perf: Dwarf cfi based user callchains

RFC In review Frederic Weisbecker 4 participants 33 messages Oct 13, 2010 – Oct 20, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi,

This brings dwarf cfi based callchain for userspace apps that don't have frame pointers.

To test it, you can try:

perf record -g dwarf,24000 -e cycles:u ./hackbench 2 perf report

It seems to work but there are of course many things to improve:

- do only userspace profiling with that mode for now (the :u flag as above). The reason is that if you profile also the kernel, the user callchains will often start from vdso if the user made a syscall, and vdso doesn't have cfi informations, so we get stuck there. I need to find a solution for that, like doing a single frame pointer deref on the first entry (vdso) and continue with dwarves, but I need to know if we came from a syscall for that. Not sure yet how I'll handle that.

- it only works with .eh_frame, I think there is an elf section that is made almost the same but with few differences. I don't remember the name at that time but that needs a look.

- it's slow. A first improvement to make it faster is to support binary search from .eh_frame_hdr. This will probably be one of the next things I'll focus in. And the whole needs perhaps more caching and so on.

- only support for x86-32. I need to split some arch specific code from generic and add at least x86-64 support.

- there are still some callchains that are not unwind. I need to investigate.

This can be found in:

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
perf/unwind-v1

Thanks,
Frederic
---

Frederic Weisbecker (9):
uaccess: Make copy_from_user_nmi() globally available
perf: Add ability to dump user regs
perf: Add ability to dump part of the user stack
perf: Don't record frame pointer based user stacktraces if we dump stack and regs
perf: Support for dwarf mode callchain on perf record
perf: Build with dwarf cfi
perf: Support for error passed over pointers
perf: Add libunwind dependency for dwarf cfi unwinding
perf: Support for dwarf cfi unwinding on post processing

 arch/x86/include/asm/uaccess.h      |    5 +
 arch/x86/kernel/cpu/perf_event.c    |    4 +-
 include/asm-generic/uaccess.h       |    4 +
 include/linux/perf_event.h          |   15 +-
 kernel/perf_event.c                 |  182 +++++-
 tools/perf/Makefile                 |   23 +-
 tools/perf/builtin-record.c         |   76 +++-
 tools/perf/builtin-report.c         |    9 +-
 tools/perf/feature-tests.mak        |   14 +
 tools/perf/perf.h                   |    5 +
 tools/perf/util/callchain.c         |   35 +-
 tools/perf/util/callchain.h         |   19 +-
 tools/perf/util/event.c             |   29 +
 tools/perf/util/event.h             |    7 +
 tools/perf/util/include/linux/err.h |   24 +
 tools/perf/util/unwind.c            | 1077 +++++++++++++++++++++++++++++++++++
 16 files changed, 1485 insertions(+), 43 deletions(-)
#1 lore raw

RFC PATCH 1/9 uaccess: Make copy_from_user_nmi() globally available

In order to support user stack dump safely in perf samples from generic code, export copy_from_user_nmi() from x86 and make it generally available. For most archs it will map to copy_from_user_inatomic, but for x86 we need to take care of not faulting from NMIs.

Since perf is the first user for now, let the overriden x86 implementation in the perf source file.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 arch/x86/include/asm/uaccess.h   |    5 +++++
 arch/x86/kernel/cpu/perf_event.c |    4 ++--
 include/asm-generic/uaccess.h    |    4 ++++
 3 files changed, 11 insertions(+), 2 deletions(-)
arch/x86/include/asm/uaccess.h +5 -0
index abd3e0e..901daf3 100644
@@ -229,6 +229,11 @@ extern void __put_user_2(void);
 extern void __put_user_4(void);
 extern void __put_user_8(void);
 
+extern unsigned long
+__copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
+
+#define copy_from_user_nmi(to, from, n)	__copy_from_user_nmi(to, from, n)
+
 #ifdef CONFIG_X86_WP_WORKS_OK
 
 /**
arch/x86/kernel/cpu/perf_event.c +2 -2
index e2513f2..d3278da 100644
@@ -45,8 +45,8 @@ do {								\
 /*
  * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
  */
-static unsigned long
-copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
+unsigned long
+__copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
 {
 	unsigned long offset, addr = (unsigned long)from;
 	int type = in_nmi() ? KM_NMI : KM_IRQ0;
include/asm-generic/uaccess.h +4 -0
index b218b85..a8d5525 100644
@@ -240,6 +240,10 @@ extern int __get_user_bad(void) __attribute__((noreturn));
 #define __copy_to_user_inatomic __copy_to_user
 #endif
 
+#ifndef copy_from_user_nmi
+#define copy_from_user_nmi __copy_from_user_inatomic
+#endif
+
 static inline long copy_from_user(void *to,
 		const void __user * from, unsigned long n)
 {
Signature · 1 line
1.6.2.3
maintainer
#2 lore raw

Re: [RFC PATCH 1/9] uaccess: Make copy_from_user_nmi() globally available

Quoted 8 lines · Frederic Weisbecker

In order to support user stack dump safely in perf samples from generic code, export copy_from_user_nmi() from x86 and make it generally available. For most archs it will map to copy_from_user_inatomic, but for x86 we need to take care of not faulting from NMIs.

Since perf is the first user for now, let the overriden x86 implementation in the perf source file.

It might make sense to call it copy_from_user_gup() because that's bascially what it does, it doesn't rely on NMI context anymore, its just NMI-safe.

Its a best effort software page table walk, and with the stacked kmap_atomic bits Andrew took it should work from any context.

#3 lore raw

Re: [RFC PATCH 1/9] uaccess: Make copy_from_user_nmi() globally available

Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker

In order to support user stack dump safely in perf samples from generic code, export copy_from_user_nmi() from x86 and make it generally available. For most archs it will map to copy_from_user_inatomic, but for x86 we need to take care of not faulting from NMIs.

Since perf is the first user for now, let the overriden x86 implementation in the perf source file.

Quoted 6 lines

It might make sense to call it copy_from_user_gup() because that's bascially what it does, it doesn't rely on NMI context anymore, its just NMI-safe.

Its a best effort software page table walk, and with the stacked kmap_atomic bits Andrew took it should work from any context.

Ok, I'll do the rename. Does that work on any arch?

Thanks.

maintainer
#4 lore raw

Re: [RFC PATCH 1/9] uaccess: Make copy_from_user_nmi() globally available

Quoted 1 line · Frederic Weisbecker

Ok, I'll do the rename. Does that work on any arch?

On any arch that implements __get_user_pages_fast() which is only x86 atm, but any arch that supports get_user_pages_fast() could do it.

#5 lore raw

RFC PATCH 2/9 perf: Add ability to dump user regs

In reply to Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 include/linux/perf_event.h |    4 +++-
 kernel/perf_event.c        |   42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 1 deletions(-)
include/linux/perf_event.h +3 -1
index 61b1e2d..71b108b 100644
@@ -125,8 +125,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_PERIOD			= 1U << 8,
 	PERF_SAMPLE_STREAM_ID			= 1U << 9,
 	PERF_SAMPLE_RAW				= 1U << 10,
+	PERF_SAMPLE_UREGS			= 1U << 11,
 
-	PERF_SAMPLE_MAX = 1U << 11,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 12,		/* non-ABI */
 };
 
 /*
@@ -932,6 +933,7 @@ struct perf_sample_data {
 	u64				period;
 	struct perf_callchain_entry	*callchain;
 	struct perf_raw_record		*raw;
+	struct pt_regs			*uregs;
 };
 
 static inline
kernel/perf_event.c +42 -0

60 lines not on this page · raw message

Signature · 1 line
1.6.2.3
maintainer
#6 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 6 lines · Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

This doesn't address any of the issues that were raised previously.

There's a reason we don't have PERF_SAMPLE_*REGS like things.

See: http://lkml.org/lkml/2010/3/3/308

#7 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

Quoted 8 lines
Quoted 2 lines

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

This doesn't address any of the issues that were raised previously.

There's a reason we don't have PERF_SAMPLE_*REGS like things.

See: http://lkml.org/lkml/2010/3/3/308

Right, we indeed have no way currently to know where these regs came from. So we need to dump the kernel arch informations.

Probably we should do that from the kernel, so that compat archs really can't get it wrong.

But I suspect we should do it once and not on every sample, so it must be no sample type.

A silly idea would be to implement a minimal counter which counting value gives a code that tells about the arch. No sample at all for this event, just a count.

Hmm?

#8 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

In reply to Peter Zijlstra
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

Quoted 8 lines
Quoted 2 lines

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

This doesn't address any of the issues that were raised previously.

There's a reason we don't have PERF_SAMPLE_*REGS like things.

See: http://lkml.org/lkml/2010/3/3/308

But the best would be to get it from sysfs I think.

#9 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

In reply to Peter Zijlstra

Hi,

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

Quoted 7 lines
Quoted 2 lines

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

This doesn't address any of the issues that were raised previously.

There's a reason we don't have PERF_SAMPLE_*REGS like things.

We definitively need to find a solution to this problem. It is important to export this kind of information to users when using PEBS, for instance

What is exported depends on what is monitored and not just the ABI of the kernel. On a 64-bit kernel, you may capture samples from i386 or x86_64. Somehow the record needs to be self describing.

What about something like:

struct  {
      int type; /* 32-bit, 64-bit */
      int nr;    /* number of regs */
      struct {
          int reg_name; /* taken from an enum with all possible regs */
          u64 reg_value;
      } [0]
};
#10 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 13 lines · Stephane Eranian

Hi,

Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

Quoted 6 lines
Quoted 4 lines

This doesn't address any of the issues that were raised previously.

There's a reason we don't have PERF_SAMPLE_*REGS like things.

We definitively need to find a solution to this problem. It is important to export this kind of information to users when using PEBS, for instance

Would you need to export only a part of the regs for cases like PEBS?

Quoted 6 lines

What is exported depends on what is monitored and not just the ABI of the kernel. On a 64-bit kernel, you may capture samples from i386 or x86_64. Somehow the record needs to be self describing.

What about something like:

Quoted 8 lines
struct  {
      int type; /* 32-bit, 64-bit */
      int nr;    /* number of regs */
      struct {
          int reg_name; /* taken from an enum with all possible regs */
          u64 reg_value;
      } [0]
};

Yeah but in this case we can probably avoid to embed all the regnames in every dumps. This can be retrieved from what we asked in the attrs, which could be a u64 bitmap that tells which regs we want? (that only if we want a per reg granularity).

#11 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Frederic,

Quoted 16 lines · Frederic Weisbecker
Quoted 15 lines · Stephane Eranian

Hi,

Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

This doesn't address any of the issues that were raised previously.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

There's a reason we don't have PERF_SAMPLE_*REGS like things.

We definitively need to find a solution to this problem. It is important to export this kind of information to users when using PEBS, for instance

Would you need to export only a part of the regs for cases like PEBS?

Yes, PEBS does not capture the entire state.

Here is what you get on Intel Core:

        u64 flags, ip;
        u64 ax, bx, cx, dx;
        u64 si, di, bp, sp;
        u64 r8,  r9,  r10, r11;
        u64 r12, r13, r14, r15;

In 32-bit, the rXX are zero and would not need to be exposed.

Quoted 19 lines
Quoted 7 lines

What is exported depends on what is monitored and not just the ABI of the kernel. On a 64-bit kernel, you may capture samples from i386 or x86_64. Somehow the record needs to be self describing.

Quoted 9 lines

What about something like:
struct  {
      int type; /* 32-bit, 64-bit */
      int nr;    /* number of regs */
      struct {
          int reg_name; /* taken from an enum with all possible regs */ u64 reg_value;
      } [0]
};

Quoted 5 lines

Yeah but in this case we can probably avoid to embed all the regnames in every dumps. This can be retrieved from what we asked in the attrs, which could be a u64 bitmap that tells which regs we want? (that only if we want a per reg granularity).

Yes, that's another possibility and it may be better because if you want only one register,e.g,, EAX, then you can ask for it. That would limit the memory consumption in the sampling buffer.

You need a bitmask to name the registers you want. If the register is not accessible in the sampling mode you're requesting, then you should get an error.

I would not necessarily use the attr.sample_type because we might run out of bits on architecture with lots of registers. Another reason is that the register
names are arch-specific, unlike what's in attr.sample_type.

#12 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 28 lines · Stephane Eranian

Frederic,

Quoted 16 lines · Frederic Weisbecker
Quoted 15 lines · Stephane Eranian

Hi,

Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Frederic Weisbecker

Add new PERF_SAMPLE_UREGS to perf sample type. This will dump the user space context as it was before the user entered the kernel for whatever reason.

This is going to be useful to bring Dwarf CFI based stack unwinding on top of samples.

This doesn't address any of the issues that were raised previously.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

There's a reason we don't have PERF_SAMPLE_*REGS like things.

We definitively need to find a solution to this problem. It is important to export this kind of information to users when using PEBS, for instance

Would you need to export only a part of the regs for cases like PEBS?

Quoted 8 lines

Yes, PEBS does not capture the entire state.

Here is what you get on Intel Core:

        u64 flags, ip;
        u64 ax, bx, cx, dx;
        u64 si, di, bp, sp;
        u64 r8,  r9,  r10, r11;
        u64 r12, r13, r14, r15;

Ok, that seems to cover most of the state. I guess few people care about cs, ds, es, fs, gs, most of the time.

Quoted 23 lines

In 32-bit, the rXX are zero and would not need to be exposed.

Quoted 19 lines
Quoted 16 lines

What is exported depends on what is monitored and not just the ABI of the kernel. On a 64-bit kernel, you may capture samples from i386 or x86_64. Somehow the record needs to be self describing.

What about something like:
struct  {
      int type; /* 32-bit, 64-bit */
      int nr;    /* number of regs */
      struct {
          int reg_name; /* taken from an enum with all possible regs */ u64 reg_value;
      } [0]
};

Quoted 8 lines
Quoted 5 lines

Yeah but in this case we can probably avoid to embed all the regnames in every dumps. This can be retrieved from what we asked in the attrs, which could be a u64 bitmap that tells which regs we want? (that only if we want a per reg granularity).

Yes, that's another possibility and it may be better because if you want only one register,e.g,, EAX, then you can ask for it. That would limit the memory consumption in the sampling buffer.

But then I wonder who needs EAX only? If you need eax, then you almost certainly need most the other general registers.

May be we can group them by "family"? Like one group for general registers (r0 - r15), one for segment registers (cs - gs) and one for eflags. We can perhaps isolate single groups for stack pointer, frame pointer and instruction pointer.

But I can't imagine every possible uses of the regs dump, may be we should just have one flag per register to enforce a maximum flexibility and don't bother further.

Hm?

Quoted 3 lines

You need a bitmask to name the registers you want. If the register is not accessible in the sampling mode you're requesting, then you should get an error.

Yep.

Quoted 4 lines

I would not necessarily use the attr.sample_type because we might run out of bits on architecture with lots of registers. Another reason is that the register
names are arch-specific, unlike what's in attr.sample_type.

Yeah we really need a new field for that, u64 so that we have enough bits for every possible regs set (at least I hope...).

maintainer
#13 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 11 lines · Frederic Weisbecker
Quoted 8 lines

Yes, PEBS does not capture the entire state.

Here is what you get on Intel Core:

        u64 flags, ip;
        u64 ax, bx, cx, dx;
        u64 si, di, bp, sp;
        u64 r8,  r9,  r10, r11;
        u64 r12, r13, r14, r15;

Ok, that seems to cover most of the state. I guess few people care about cs, ds, es, fs, gs, most of the time.

Yeah, except if you want to profile wine or something like that ;-)

#14 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker
Quoted 8 lines

Yes, PEBS does not capture the entire state.

Here is what you get on Intel Core:
        u64 flags, ip;
        u64 ax, bx, cx, dx;
        u64 si, di, bp, sp;
        u64 r8,  r9,  r10, r11;
        u64 r12, r13, r14, r15;

Quoted 5 lines
Quoted 2 lines

Ok, that seems to cover most of the state. I guess few people care about cs, ds, es, fs, gs, most of the time.

Yeah, except if you want to profile wine or something like that ;-)

That means that if you want the segment registers, then you cannot use PEBS. I think you could catch that when the event is created.

The other problem here is how to name registers at the API level. You would be introducing architecture-specific register names in perf_event.h. There is no such a thing today.

#15 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 14 lines · Stephane Eranian
Quoted 13 lines · Peter Zijlstra
Quoted 11 lines · Frederic Weisbecker
Quoted 8 lines

Yes, PEBS does not capture the entire state.

Here is what you get on Intel Core:
        u64 flags, ip;
        u64 ax, bx, cx, dx;
        u64 si, di, bp, sp;
        u64 r8,  r9,  r10, r11;
        u64 r12, r13, r14, r15;

Ok, that seems to cover most of the state. I guess few people care about cs, ds, es, fs, gs, most of the time.

Quoted 8 lines
Quoted 2 lines

Yeah, except if you want to profile wine or something like that ;-)

That means that if you want the segment registers, then you cannot use PEBS. I think you could catch that when the event is created.

The other problem here is how to name registers at the API level. You would be introducing architecture-specific register names in perf_event.h. There is no such a thing today.

That can go into an asm/perf_regs.h or something. It's up to the arch to name its registers.

#16 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 20 lines · Frederic Weisbecker
Quoted 19 lines · Stephane Eranian
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker
Quoted 8 lines

Yes, PEBS does not capture the entire state.

Here is what you get on Intel Core:
        u64 flags, ip;
        u64 ax, bx, cx, dx;
        u64 si, di, bp, sp;
        u64 r8,  r9,  r10, r11;
        u64 r12, r13, r14, r15;

Quoted 5 lines
Quoted 2 lines

Ok, that seems to cover most of the state. I guess few people care about cs, ds, es, fs, gs, most of the time.

Yeah, except if you want to profile wine or something like that ;-)

That means that if you want the segment registers, then you cannot use PEBS. I think you could catch that when the event is created.

Quoted 8 lines
Quoted 3 lines

The other problem here is how to name registers at the API level. You would be introducing architecture-specific register names in perf_event.h. There is no such a thing today.

That can go into an asm/perf_regs.h or something. It's up to the arch to name its registers.

I am fine with that.

Starting with Nehalem, there is a PEBS mode where HW captures not just actual register state but also information about cache misses such as the data address, miss latency, data source. Those are stored in the PEBS record as u64. I believe we could also expose this thru this register bitmask mechanism. Of course, you'd get a failure if PEBS is not programmed correctly.

The alternative would be to invent yet another generic abstraction to sample cache misses. Note that PEBS cache miss sampling cannot be attached to an existing generic cache miss event. It uses a dedicated event which does not count all cache misses.

#17 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

Quoted 30 lines · Stephane Eranian
Quoted 20 lines · Frederic Weisbecker
Quoted 19 lines · Stephane Eranian
Quoted 10 lines · Peter Zijlstra
Quoted 8 lines · Frederic Weisbecker
Quoted 8 lines

Yes, PEBS does not capture the entire state.

Here is what you get on Intel Core:
        u64 flags, ip;
        u64 ax, bx, cx, dx;
        u64 si, di, bp, sp;
        u64 r8,  r9,  r10, r11;
        u64 r12, r13, r14, r15;

Quoted 5 lines
Quoted 2 lines

Ok, that seems to cover most of the state. I guess few people care about cs, ds, es, fs, gs, most of the time.

Yeah, except if you want to profile wine or something like that ;-)

That means that if you want the segment registers, then you cannot use PEBS. I think you could catch that when the event is created.

Quoted 8 lines
Quoted 3 lines

The other problem here is how to name registers at the API level. You would be introducing architecture-specific register names in perf_event.h. There is no such a thing today.

That can go into an asm/perf_regs.h or something. It's up to the arch to name its registers.

Quoted 8 lines

I am fine with that.

Starting with Nehalem, there is a PEBS mode where HW captures not just actual register state but also information about cache misses such as the data address, miss latency, data source. Those are stored in the PEBS record as u64. I believe we could also expose this thru this register bitmask mechanism. Of course, you'd get a failure if PEBS is not programmed correctly.

I'm not sure the registers are the right place for that. This is too oriented toward a specific mechanism.

I would rather put that into a PERF_SAMPLE_RAW dump or a specific pebs sample.

The problem with PERF_SAMPLE_RAW is that perf tools always think it's trace event content. It should look at what event it is looking at before making that assumption.

We'd need to look at the event that triggered the sample to interpret the sample raw. That's fixable.

Quoted 4 lines

The alternative would be to invent yet another generic abstraction to sample cache misses. Note that PEBS cache miss sampling cannot be attached to an existing generic cache miss event. It uses a dedicated event which does not count all cache misses.

Then perhaps that should be abstracted into a different event yeah.

maintainer
#18 lore raw

Re: [RFC PATCH 2/9] perf: Add ability to dump user regs

In reply to Stephane Eranian
Quoted 5 lines · Stephane Eranian

The alternative would be to invent yet another generic abstraction to sample cache misses. Note that PEBS cache miss sampling cannot be attached to an existing generic cache miss event. It uses a dedicated event which does not count all cache misses.

Yeah, the IBS trainwreck comes to mind, I wish the PMU designers wouldn't create such a mess.

I really dislike using PERF_SAMPLE_RAW for things like this, because it fully arch dependent.

#19 lore raw

RFC PATCH 3/9 perf: Add ability to dump part of the user stack

In reply to Frederic Weisbecker

Beeing able to dump parts of the user stack, starting from the stack pointer, will be useful to make a post mortem dwarf CFI based stack unwinding.

This is done through the new ustack_dump_size perf attribute. If it is non zero, the user stack will dumped in samples following the requested size in bytes.

The longer is the dump, the deeper will be the resulting retrieved callchain.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 include/linux/perf_event.h |   11 +++-
 kernel/perf_event.c        |  123 ++++++++++++++++++++++++++++++++++++--------
 2 files changed, 110 insertions(+), 24 deletions(-)
include/linux/perf_event.h +9 -2
index 71b108b..0b1b039 100644
@@ -227,6 +227,12 @@ struct perf_event_attr {
 	__u32			bp_type;
 	__u64			bp_addr;
 	__u64			bp_len;
+
+	__u64			__reserved_2;
+	__u64			__reserved_3;
+
+	__u32			ustack_dump_size;
+	__u32			__reserved_4;
 };
 
 /*
@@ -1061,8 +1067,9 @@ extern int perf_output_begin(struct perf_output_handle *handle,
 			     struct perf_event *event, unsigned int size,
 			     int nmi, int sample);
 extern void perf_output_end(struct perf_output_handle *handle);
-extern void perf_output_copy(struct perf_output_handle *handle,
-			     const void *buf, unsigned int len);
+extern unsigned int
+perf_output_copy(struct perf_output_handle *handle,
+		 const void *buf, unsigned int len);
 extern int perf_swevent_get_recursion_context(void);
 extern void perf_swevent_put_recursion_context(int rctx);
 extern void perf_event_enable(struct perf_event *event);
kernel/perf_event.c +101 -22

141 lines not on this page · raw message

Signature · 1 line
1.6.2.3
maintainer
#20 lore raw

Re: [RFC PATCH 3/9] perf: Add ability to dump part of the user stack

include/linux/perf_event.h struct perf_event_attr { · line 236

Quoted 12 lines · Frederic Weisbecker

+++ b/include/linux/perf_event.h

include/linux/perf_event.h +6 -0
@@ -227,6 +227,12 @@ struct perf_event_attr {
        __u32                   bp_type;
        __u64                   bp_addr;
        __u64                   bp_len;
+
+       __u64                   __reserved_2;
+       __u64                   __reserved_3;
+
+       __u32                   ustack_dump_size;
+       __u32                   __reserved_4;
 }; 

Why add those two __u64 fields?

#21 lore raw

Re: [RFC PATCH 3/9] perf: Add ability to dump part of the user stack

Quoted 15 lines · Peter Zijlstra

include/linux/perf_event.h struct perf_event_attr { · line 236

Quoted 12 lines · Frederic Weisbecker

+++ b/include/linux/perf_event.h

include/linux/perf_event.h +6 -0
@@ -227,6 +227,12 @@ struct perf_event_attr {
        __u32                   bp_type;
        __u64                   bp_addr;
        __u64                   bp_len;
+
+       __u64                   __reserved_2;
+       __u64                   __reserved_3;
+
+       __u32                   ustack_dump_size;
+       __u32                   __reserved_4;
 }; 

Why add those two __u64 fields?

Because I suspect one day the breakpoint fields will need to be extended. Some archs have quite fancy features for breakpoints that include value comparison for example. So a field could be used for the comparison operator and the other for the value to be compared against.

#22 lore raw

RFC PATCH 4/9 perf: Don't record frame pointer based user stacktraces if we dump stack and regs

In reply to Frederic Weisbecker

If we record user stack and regs, then don't record frame pointer based user callchain, because it means we will do the user callchain with the dwarf CFI information.

I'm not sure this is a good thing. I don't even know why I'm sending this patch.

User and kernel stack might be selected for other uses than callchain in the future, this probably shouldn't mess with the regular callchain code. Instead we should probably have an exclude_callchain_user attribute, that could be also useful to filter out user callchains when people don't want them.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 kernel/perf_event.c |   17 +++++++++++++----
 1 files changed, 13 insertions(+), 4 deletions(-)
kernel/perf_event.c +13 -4
index 0a1f0df..018a098 100644
@@ -1958,7 +1958,8 @@ put_callchain_entry(int rctx)
 	put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
 }
 
-static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
+static struct perf_callchain_entry *
+perf_callchain(struct pt_regs *regs, struct perf_event *event, u64 sample_type)
 {
 	int rctx;
 	struct perf_callchain_entry *entry;
@@ -1983,8 +1984,16 @@ static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
 	}
 
 	if (regs) {
-		perf_callchain_store(entry, PERF_CONTEXT_USER);
-		perf_callchain_user(entry, regs);
+		/*
+		 * CHECKME: if user regs and user stack dumps are used
+		 * for something else than callchains one day, we may
+		 * want a exclude_callchain_user?
+		 */
+		if (!(sample_type & PERF_SAMPLE_UREGS &&
+		      event->attr.ustack_dump_size)) {
+			perf_callchain_store(entry, PERF_CONTEXT_USER);
+			perf_callchain_user(entry, regs);
+		}
 	}
 
 exit_put:
@@ -3760,7 +3769,7 @@ void perf_prepare_sample(struct perf_event_header *header,
 	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
 		int size = 1;
 
-		data->callchain = perf_callchain(regs);
+		data->callchain = perf_callchain(regs, event, sample_type);
 
 		if (data->callchain)
 			size += data->callchain->nr;
Signature · 1 line
1.6.2.3
maintainer
#23 lore raw

Re: [RFC PATCH 4/9] perf: Don't record frame pointer based user stacktraces if we dump stack and regs

Quoted 5 lines · Frederic Weisbecker

User and kernel stack might be selected for other uses than callchain in the future, this probably shouldn't mess with the regular callchain code. Instead we should probably have an exclude_callchain_user attribute, that could be also useful to filter out user callchains when people don't want them.

Probably ;-)

#24 lore raw

Re: [RFC PATCH 4/9] perf: Don't record frame pointer based user stacktraces if we dump stack and regs

Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Frederic Weisbecker

User and kernel stack might be selected for other uses than callchain in the future, this probably shouldn't mess with the regular callchain code. Instead we should probably have an exclude_callchain_user attribute, that could be also useful to filter out user callchains when people don't want them.

Probably ;-)

Will change that then :)

Thanks.

#25 lore raw

Re: [RFC PATCH 4/9] perf: Don't record frame pointer based user stacktraces if we dump stack and regs

In reply to Peter Zijlstra
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Frederic Weisbecker

User and kernel stack might be selected for other uses than callchain in the future, this probably shouldn't mess with the regular callchain code. Instead we should probably have an exclude_callchain_user attribute, that could be also useful to filter out user callchains when people don't want them.

Probably ;-)

There is another solution that would solve my vdso problem in the meantime.

The problem with vdso is that if we entered the kernel with a syscall, the first user entry in the callchain will be a vdso address. But vdso doesn't have dwarf informations so we can't unwind further.

One solution would be having max_callchain_user as an attribute. If we do a normal frame pointer based callchain, set it to -1 and you won't have limitations in your callchain. Or you can set it to n so that your callchains are bound to a maximum depth.
If you don't want user callchains, set it to 0.
If you do a dwarf based callchain, set it to 2, so that if userspace was in a vdso, we just deref the frame pointer to find what called the vdso, and then we can start the dwarf unwinding from there.

I think I'll try that.

#26 lore raw

RFC PATCH 5/9 perf: Support for dwarf mode callchain on perf record

In reply to Frederic Weisbecker

"perf record -g" is the command to record frame pointer based callchains. This patch extends the "-g" option to support the dwarf cfi mode.

The new behaviour is:

- "perf record -g" will record frame pointer based callchains as it did before.

- "perf record -g fp" is the same as "-g" alone

- "perf record -g dwarf" will record frame pointer based kernel callchains but will ignore the user part. Instead it will dump user regs and 4000 bytes of stack by default to each samples.

- "perf record -g dwarf,x" does the same but overrides the default 4000 bytes of user stack dumped to x bytes instead. The higher is the size, the deeper will be the callchain but the higher will be the overhead of the profiling and the size of the output file.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 tools/perf/builtin-record.c |   76 ++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 72 insertions(+), 4 deletions(-)
tools/perf/builtin-record.c +72 -4
index ff77b80..132d710 100644
@@ -32,6 +32,12 @@ enum write_mode_t {
 	WRITE_APPEND
 };
 
+enum call_graph_mode {
+	CALLCHAIN_NONE,
+	CALLCHAIN_FP,
+	CALLCHAIN_DWARF
+};
+
 static int			*fd[MAX_NR_CPUS][MAX_COUNTERS];
 
 static u64			user_interval			= ULLONG_MAX;
@@ -56,7 +62,6 @@ static int			thread_num			=      0;
 static pid_t			child_pid			=     -1;
 static bool			no_inherit			=  false;
 static enum write_mode_t	write_mode			= WRITE_FORCE;
-static bool			call_graph			=  false;
 static bool			inherit_stat			=  false;
 static bool			no_samples			=  false;
 static bool			sample_address			=  false;
@@ -76,6 +81,10 @@ static off_t			post_processing_offset;
 static struct perf_session	*session;
 static const char		*cpu_list;
 
+static char			callchain_default_opt[]		=      "fp";
+static unsigned long		stack_dump_size			=	4000;
+static enum call_graph_mode	call_graph			= CALLCHAIN_NONE;
+
 struct mmap_data {
 	int			counter;
 	void			*base;
@@ -274,9 +283,15 @@ static void create_counter(int counter, int cpu)
 		attr->mmap_data = track;
 	}
 
-	if (call_graph)
+	if (call_graph) {
 		attr->sample_type	|= PERF_SAMPLE_CALLCHAIN;
 
+		if (call_graph == CALLCHAIN_DWARF) {
+			attr->sample_type |= PERF_SAMPLE_UREGS;
+			attr->ustack_dump_size = stack_dump_size;
+		}
+	}
+
 	if (system_wide)
 		attr->sample_type	|= PERF_SAMPLE_CPU;
 
@@ -779,6 +794,58 @@ out_delete_session:
 	return err;
 }
 
+static int
+parse_callchain_opt(const struct option *opt __used, const char *arg,
+		    int unset)
+{
+	char *tok;
+
+	/*
+	 * --no-call-graph
+	 */
+	if (unset)
+		return 0;
+
+	if (!arg)
+		return 0;
+
+	tok = strtok((char *)arg, ",");
+	if (!tok)
+		return -1;
+
+	/* get the output mode */
+	if (!strncmp(tok, "fp", strlen(arg)))
+		call_graph = CALLCHAIN_FP;
+
+	else if (!strncmp(tok, "dwarf", strlen(arg)))
+		call_graph = CALLCHAIN_DWARF;
+
+	else if (!strncmp(tok, "none", strlen(arg)))
+		return 0;
+
+	else
+		return -1;
+
+	/* get the stack dump size */
+	tok = strtok(NULL, ",");
+	if (!tok)
+		return 0;
+
+	/* No stack dump size if we record using frame pointers */
+	if (call_graph == CALLCHAIN_FP) {
+		fprintf(stderr, "Stack dump size is only necessary for -g dwarf\n");
+		return -1;
+	}
+
+	stack_dump_size = strtoul(tok, NULL, 0);
+	if (stack_dump_size == ULONG_MAX) {
+		perror("Incorrect stack dump size\n");
+		return -1;
+	}
+
+	return 0;
+}
+
 static const char * const record_usage[] = {
 	"perf record [<options>] [<command>]",
 	"perf record [<options>] -- <command> [<options>]",
@@ -816,8 +883,9 @@ static const struct option options[] = {
 		    "child tasks do not inherit counters"),
 	OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"),
 	OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
-	OPT_BOOLEAN('g', "call-graph", &call_graph,
-		    "do call-graph (stack chain/backtrace) recording"),
+	OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "mode,dump_size",
+		     "do call-graph (stack chain/backtrace) recording"
+		     "Default: fp", &parse_callchain_opt, callchain_default_opt),
 	OPT_INCR('v', "verbose", &verbose,
 		    "be more verbose (show counter open errors, etc)"),
 	OPT_BOOLEAN('s', "stat", &inherit_stat,
Signature · 1 line
1.6.2.3
#27 lore raw

RFC PATCH 6/9 perf: Build with dwarf cfi

In reply to Frederic Weisbecker

Build perf with the dwarf call frame informations so that we can unwind callchains in perf itself.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 tools/perf/Makefile |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/perf/Makefile +1 -1
index d1db0f6..28de60b 100644
@@ -224,7 +224,7 @@ ifndef PERF_DEBUG
   CFLAGS_OPTIMIZE = -O6
 endif
 
-CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS)
+CFLAGS = -ggdb3 -funwind-tables -Wall -Wextra -std=gnu99 -Werror $(CFLAGS_OPTIMIZE) -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS)
 EXTLIBS = -lpthread -lrt -lelf -lm
 ALL_CFLAGS = $(CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
 ALL_LDFLAGS = $(LDFLAGS)
Signature · 1 line
1.6.2.3
#28 lore raw

RFC PATCH 7/9 perf: Support for error passed over pointers

In reply to Frederic Weisbecker

Export the linux/err.h that carries the ERR_PTR/PTR_ERR/IS_ERR macros so that they also become usable by perf.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 tools/perf/Makefile                 |    1 +
 tools/perf/util/include/linux/err.h |   24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)
 create mode 100644 tools/perf/util/include/linux/err.h
tools/perf/Makefile +1 -0
index 28de60b..1146195 100644
@@ -375,6 +375,7 @@ LIB_H += util/include/linux/prefetch.h
 LIB_H += util/include/linux/rbtree.h
 LIB_H += util/include/linux/string.h
 LIB_H += util/include/linux/types.h
+LIB_H += util/include/linux/err.h
 LIB_H += util/include/asm/asm-offsets.h
 LIB_H += util/include/asm/bug.h
 LIB_H += util/include/asm/byteorder.h
tools/perf/util/include/linux/err.h new file +24 -0
new file mode 100644
index 0000000..4e6dc36
@@ -0,0 +1,24 @@
+#ifndef PERF_ERR_H
+#define PERF_ERR_H
+
+
+#define MAX_ERRNO	4095
+
+#define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
+
+static inline void *ERR_PTR(long err)
+{
+	return (void *) err;
+}
+
+static inline long PTR_ERR(const void *ptr)
+{
+	return (long) ptr;
+}
+
+static inline long IS_ERR(const void *ptr)
+{
+	return IS_ERR_VALUE((unsigned long)ptr);
+}
+
+#endif /* PERF_ERR_H */
Signature · 1 line
1.6.2.3
#29 lore raw

RFC PATCH 8/9 perf: Add libunwind dependency for dwarf cfi unwinding

In reply to Frederic Weisbecker

This is not mandatory, but required to get dwarf cfi unwinding support.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 tools/perf/Makefile          |   19 +++++++++++++++++++
 tools/perf/feature-tests.mak |   14 ++++++++++++++
 2 files changed, 33 insertions(+), 0 deletions(-)
tools/perf/Makefile +19 -0
index 1146195..ca4cde1 100644
@@ -515,6 +515,20 @@ ifneq ($(call try-cc,$(SOURCE_DWARF),$(FLAGS_DWARF)),y)
 endif # Dwarf support
 endif # NO_DWARF
 
+
+# Only x86-32 is supported for now
+ifneq ($(ARCH),x86)
+	NO_LIBUNWIND := 1
+endif
+
+ifndef NO_LIBUNWIND
+FLAGS_UNWIND=$(ALL_CFLAGS) -lunwind-x86 -lunwind-ptrace $(ALL_LDFLAGS) $(EXTLIBS)
+ifneq ($(call try-cc,$(SOURCE_LIBUNWIND),$(FLAGS_UNWIND)),y)
+	msg := $(warning No libunwind found. Please install libunwind >= 0.99);
+	NO_LIBUNWIND := 1
+endif # Libunwind support
+endif # NO_LIBUNWIND
+
 -include arch/$(ARCH)/Makefile
 
 ifeq ($(uname_S),Darwin)
@@ -561,6 +575,11 @@ else
 endif # PERF_HAVE_DWARF_REGS
 endif # NO_DWARF
 
+ifndef NO_LIBUNWIND
+	BASIC_CFLAGS += -DLIBUNWIND_SUPPORT
+	EXTLIBS += -lunwind-ptrace -lunwind-x86
+endif
+
 ifdef NO_NEWT
 	BASIC_CFLAGS += -DNO_NEWT_SUPPORT
 else
tools/perf/feature-tests.mak +14 -0
index b253db6..367b213 100644
@@ -90,6 +90,20 @@ int main(void)
 endef
 endif
 
+ifndef NO_LIBUNWIND
+define SOURCE_LIBUNWIND
+#include <libunwind.h>
+#include <stdlib.h>
+
+int main(void)
+{
+	unw_addr_space_t addr_space;
+	addr_space = unw_create_addr_space(NULL, 0);
+	return 0;
+}
+endef
+endif
+
 define SOURCE_BFD
 #include <bfd.h>
 
Signature · 1 line
1.6.2.3
#30 lore raw

RFC PATCH 9/9 perf: Support for dwarf cfi unwinding on post processing

In reply to Frederic Weisbecker

This brings the support for dwarf cfi unwinding on perf post processing. Call frame informations are retrieved and then passed to libunwind that requests memory and register content from the applications. We use the stack and regs dumped with samples to retrieve the context state necessary for this unwinding.

No specific options are needed for perf report to handle this, just run it as usual.

Only x86-32 is supported for now.

Cc Ingo Molnar <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Tom Zanussi <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Robert Richter <[email protected]>
---
 tools/perf/Makefile         |    1 +
 tools/perf/builtin-report.c |    9 +-
 tools/perf/perf.h           |    5 +
 tools/perf/util/callchain.c |   35 ++-
 tools/perf/util/callchain.h |   19 +-
 tools/perf/util/event.c     |   29 ++
 tools/perf/util/event.h     |    7 +
 tools/perf/util/unwind.c    | 1077 +++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 1175 insertions(+), 7 deletions(-)
 create mode 100644 tools/perf/util/unwind.c
tools/perf/Makefile +1 -0
index ca4cde1..469d694 100644
@@ -465,6 +465,7 @@ LIB_OBJS += $(OUTPUT)util/hist.o
 LIB_OBJS += $(OUTPUT)util/probe-event.o
 LIB_OBJS += $(OUTPUT)util/util.o
 LIB_OBJS += $(OUTPUT)util/cpumap.o
+LIB_OBJS += $(OUTPUT)util/unwind.o
 
 BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
 
tools/perf/builtin-report.c +8 -1
index 5de405d..b48e218 100644
@@ -30,6 +30,8 @@
 #include "util/sort.h"
 #include "util/hist.h"
 
+#include <linux/err.h>
+
 static char		const *input_name = "perf.data";
 
 static bool		force, use_tui, use_stdio;
@@ -87,6 +89,7 @@ static int perf_session__add_hist_entry(struct perf_session *self,
 	struct hist_entry *he;
 	struct hists *hists;
 	struct perf_event_attr *attr;
+	struct dwarf_callchain *dc;
 
 	if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) {
 		syms = perf_session__resolve_callchain(self, al->thread,
@@ -95,6 +98,10 @@ static int perf_session__add_hist_entry(struct perf_session *self,
 			return -ENOMEM;
 	}
 
+	dc = callchain_unwind(self, al->thread, data);
+	if (IS_ERR(dc))
+		return PTR_ERR(dc);
+
 	attr = perf_header__find_attr(data->id, &self->header);
 	if (attr)
 		hists = perf_session__hists_findnew(self, data->id, attr->type, attr->config);
@@ -108,7 +115,7 @@ static int perf_session__add_hist_entry(struct perf_session *self,
 	err = 0;
 	if (symbol_conf.use_callchain) {
 		err = callchain_append(he->callchain, data->callchain, syms,
-				       data->period);
+				       dc, data->period);
 		if (err)
 			goto out_free_syms;
 	}
tools/perf/perf.h +5 -0
index ef7aa0a..2f7a6eb 100644
@@ -132,6 +132,11 @@ struct ip_callchain {
 	u64 ips[0];
 };
 
+struct raw_stack_dump {
+	u64 size;
+	char stack[0];
+};
+
 extern bool perf_host, perf_guest;
 
 #endif
tools/perf/util/callchain.c +30 -5
index e12d539..54ca387 100644
@@ -365,8 +365,10 @@ append_chain(struct callchain_node *root, struct resolved_chain *chain,
 }
 
 static void filter_context(struct ip_callchain *old, struct resolved_chain *new,
-			   struct map_symbol *syms)
+			   struct map_symbol *syms,
+			   struct dwarf_callchain *dwarf_chain)
 {
+	struct dwarf_callchain_entry *entry, *tmp;
 	int i, j = 0;
 
 	for (i = 0; i < (int)old->nr; i++) {
@@ -379,23 +381,46 @@ static void filter_context(struct ip_callchain *old, struct resolved_chain *new,
 	}
 
 	new->nr = j;
+
+	if (!dwarf_chain)
+		return;
+
+	list_for_each_entry_safe(entry, tmp, &dwarf_chain->chain_head, list) {
+		new->ips[j].ip = entry->ip;
+		new->ips[j].ms = entry->ms;
+		j++;
+
+		list_del(&entry->list);
+		free(entry);
+	}
+
+	new->nr += dwarf_chain->nb;
+
+	free(dwarf_chain);
 }
 
 
 int callchain_append(struct callchain_root *root, struct ip_callchain *chain,
-		     struct map_symbol *syms, u64 period)
+		     struct map_symbol *syms,
+		     struct dwarf_callchain *dwarf_chain, u64 period)
 {
 	struct resolved_chain *filtered;
+	int entries;
+
+	entries = chain->nr;
+
+	if (dwarf_chain)
+		entries += dwarf_chain->nb;
 
-	if (!chain->nr)
+	if (!entries)
 		return 0;
 
 	filtered = zalloc(sizeof(*filtered) +
-			  chain->nr * sizeof(struct resolved_ip));
+			  entries * sizeof(struct resolved_ip));
 	if (!filtered)
 		return -ENOMEM;
 
-	filter_context(chain, filtered, syms);
+	filter_context(chain, filtered, syms, dwarf_chain);
 
 	if (!filtered->nr)
 		goto end;
tools/perf/util/callchain.h +18 -1
index c15fb8c..9fb55b4 100644
@@ -31,6 +31,17 @@ struct callchain_root {
 	struct callchain_node	node;
 };
 
+struct dwarf_callchain_entry {
+	struct list_head	list;
+	u64			ip;
+	struct map_symbol	ms;
+};
+
+struct dwarf_callchain {
+	int			nb;
+	struct list_head	chain_head;
+};
+
 struct callchain_param;
 
 typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
@@ -68,8 +79,14 @@ static inline u64 cumul_hits(struct callchain_node *node)
 
 int register_callchain_param(struct callchain_param *param);
 int callchain_append(struct callchain_root *root, struct ip_callchain *chain,
-		     struct map_symbol *syms, u64 period);
+		     struct map_symbol *syms,
+		     struct dwarf_callchain *dwarf_chain, u64 period);
+
 int callchain_merge(struct callchain_root *dst, struct callchain_root *src);
 
 bool ip_callchain__valid(struct ip_callchain *chain, const event_t *event);
+
+struct dwarf_callchain *callchain_unwind(struct perf_session *session,
+					 struct thread *thread,
+					 struct sample_data *data);
 #endif	/* __PERF_CALLCHAIN_H */
tools/perf/util/event.c +29 -0
index dab9e75..5e9013b 100644
@@ -766,6 +766,8 @@ out_filtered:
 	return 0;
 }
 
+struct pt_regs;
+
 int event__parse_sample(const event_t *event, u64 type, struct sample_data *data)
 {
 	const u64 *array = event->sample.array;
@@ -830,6 +832,33 @@ int event__parse_sample(const event_t *event, u64 type, struct sample_data *data
 		data->raw_size = *p;
 		p++;
 		data->raw_data = p;
+		array += 1 + (data->raw_size * sizeof(u64));
+	}
+
+	if (type & PERF_SAMPLE_UREGS) {
+		u64 size = *array;
+		array++;
+		if (!size) {
+			data->regs = NULL;
+		} else {
+			data->regs = (struct pt_regs *)array;
+			array += size / sizeof(*array);
+		}
+
+		size = *array++;
+
+		/*
+		 * FIXME: Even if user regs and user stack dumps are
+		 * always paired right now, one might be recorded without the
+		 * other.
+		 */
+		if (!size) {
+			data->stack.size = 0;
+		} else {
+			data->stack.data = (char *)array;
+			array += size / sizeof(*array);
+			data->stack.size = *array;
+		}
 	}
 
 	return 0;
tools/perf/util/event.h +7 -0
index 8e790da..9c43cd0 100644
@@ -61,6 +61,11 @@ struct sample_event {
 	u64 array[];
 };
 
+struct user_stack_dump {
+	u64 size;
+	char *data;
+};
+
 struct sample_data {
 	u64 ip;
 	u32 pid, tid;
@@ -73,6 +78,8 @@ struct sample_data {
 	u32 raw_size;
 	void *raw_data;
 	struct ip_callchain *callchain;
+	struct pt_regs *regs;
+	struct user_stack_dump stack;
 };
 
 #define BUILD_ID_SIZE 20
tools/perf/util/unwind.c new file +1077 -0

1,077 lines not on this page · raw message

Signature · 1 line
1.6.2.3
#31 lore raw
In reply to Frederic Weisbecker
Quoted 3 lines · Frederic Weisbecker

[...]
This brings dwarf cfi based callchain for userspace apps that don't have frame pointers.

Interesting approach!

Quoted 3 lines

[...]
- it's slow. A first improvement to make it faster is to support binary search from .eh_frame_hdr.

In systemtap land, we did find a dramatic improvement from this too.

Have you measured the cost of transcribing of potentially large chunks of the user stacks? We did not seriously evaluate this path, since we encounter megabyte+ stacks in larger userspace apps, and copying THAT out seemed absurd.

- FChE

#32 lore raw
Quoted 7 lines · Frank Ch. Eigler
Quoted 3 lines · Frederic Weisbecker

[...]
This brings dwarf cfi based callchain for userspace apps that don't have frame pointers.

Quoted 7 lines

Interesting approach!

Quoted 3 lines

[...]
- it's slow. A first improvement to make it faster is to support binary search from .eh_frame_hdr.

In systemtap land, we did find a dramatic improvement from this too.

Yeah, linear walking on .eh_frame is not a good thing other than for testing and debugging.

Quoted 4 lines

Have you measured the cost of transcribing of potentially large chunks of the user stacks? We did not seriously evaluate this path, since we encounter megabyte+ stacks in larger userspace apps, and copying THAT out seemed absurd.

Actually that's quite affordable. And you don't need to dump all the stack of a process on every samples, that would indeed be absurd. A small chunk, starting from the stack pointer, is enough.

What is interesting is that you can play with several different sizes of dump, the higher it is and the deeper you'll be able to unwind, that also means more profiling overhead.

I can't measure significant throughput issues with hackbench for example:

Normal run:

	$ time ./hackbench 10
	Time: 3.415
	real	0m3.506s
	user	0m0.257s
	sys	0m6.519s

With perf record (default is cycles counter with 1000 HZ samples frequency):

	$ time ./perf record ./hackbench 10
	Time: 3.584
	[ perf record: Woken up 1 times to write data ]
	[ perf record: Captured and wrote 0.381 MB perf.data (~16654 samples) ]
	real	0m3.748s
	user	0m0.028s
	sys	0m0.022s

With perf record + frame pointer based callchain capture

	$ time ./perf record -g fp ./hackbench 10
	Time: 3.666
	[ perf record: Woken up 3 times to write data ]
	[ perf record: Captured and wrote 1.426 MB perf.data (~62281 samples) ]
	real	0m3.834s
	user	0m0.033s
	sys	0m0.046s

With perf record + 4096 bytes of stack dump in every sample

	$ time ./perf record -g dwarf,4096 ./hackbench 10
	Time: 3.697
	[ perf record: Woken up 15 times to write data ]
	[ perf record: Captured and wrote 5.156 MB perf.data (~225251 samples) ]
	real	0m3.931s
	user	0m0.026s
	sys	0m0.135s

With perf record + 20000 bytes of stack dump in every sample

	$ time ./perf record -g dwarf,20000 ./hackbench 10
	Time: 3.847
	[ perf record: Woken up 9 times to write data ]
	[ perf record: Captured and wrote 13.349 MB perf.data (~583219 samples) ]
	real	0m4.559s
	user	0m0.028s
	sys	0m0.329s

So there is no big differences. May be hackbench is not a good example to highlight the possible impact though. And some tuning with higher frequencies would make the difference more visible.

Perhaps the real impact is more on the amount to record, the data file tends to grow quickly, obviously.