Skip to contentSkip to conversation

perf tools: Speedup DWARF unwind

PATCH In review Jiri Olsa 5 participants 24 messages Apr 17, 2014 – May 7, 2014 perf events

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

cover perf tools: Speedup DWARF unwind

hi,
trying to speedup DWARF unwind report code by factoring related code:
- caching sample's registers access
- keep dso data file descriptor open for the life of the dso object
- replace dso cache code by mapping dso data file directly for the life of the dso object

The speedup is mainly for libunwind unwind. The libdw will benefit mainly from cached registers access, because it handles dso data accesses by itself.. and anyway it's still faster ;-).

Also reachable in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_unwind_speedup

thanks,
jirka

Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jean Pihet <[email protected]>

---
Jiri Olsa (3):
perf tools: Cache register accesses for unwind processing perf tools: Cache dso data file descriptor
perf tools: Replace dso data cache with mapped data

 tools/perf/tests/dso-data.c        |   7 ++++
 tools/perf/util/dso.c              | 200 +++++++++++++++++++++++++++---------------------------------------------------------------------
 tools/perf/util/dso.h              |  14 ++-----
 tools/perf/util/event.h            |   5 +++
 tools/perf/util/perf_regs.c        |  10 ++++-
 tools/perf/util/perf_regs.h        |   4 +-
 tools/perf/util/unwind-libunwind.c |   2 -
 7 files changed, 83 insertions(+), 159 deletions(-)
author
#1 lore raw

PATCH 1/3 perf tools: Cache register accesses for unwind processing

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

Output from report over 1.5 GB data with DWARF unwind stacks:
(TODO fix perf diff)

current code:
6.81% perf.old perf.old [.] perf_reg_value

change:
2.24% perf perf [.] perf_reg_value

And little bit of speed up:

Performance counter stats for './perf.old report -i perf-test.data --stdio':

   134,664,011,577      cycles:u                  #    2.472 GHz
   189,677,227,475      instructions:u            #    1.41  insns per cycle
      54465.096050      task-clock (msec)         #    0.998 CPUs utilized
      54.598339009 seconds time elapsed

Performance counter stats for './perf report -i perf-test.data --stdio':

   124,478,681,672      cycles:u                  #    2.466 GHz
   168,998,379,866      instructions:u            #    1.36  insns per cycle
      50487.110482      task-clock (msec)         #    0.997 CPUs utilized
      50.635824229 seconds time elapsed
Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jean Pihet <[email protected]>
---
 tools/perf/util/event.h     |  5 +++++
 tools/perf/util/perf_regs.c | 10 +++++++++-
 tools/perf/util/perf_regs.h |  4 +++-
 3 files changed, 17 insertions(+), 2 deletions(-)
tools/perf/util/event.h +5 -0
index 38457d4..970d4eb 100644
@@ -7,6 +7,7 @@
 #include "../perf.h"
 #include "map.h"
 #include "build-id.h"
+#include "perf_regs.h"
 
 struct mmap_event {
 	struct perf_event_header header;
@@ -87,6 +88,10 @@ struct regs_dump {
 	u64 abi;
 	u64 mask;
 	u64 *regs;
+
+	/* Cached values/mask filled by first register access. */
+	u64 cache_regs[PERF_REGS_MAX];
+	u64 cache_mask;
 };
 
 struct stack_dump {
tools/perf/util/perf_regs.c +9 -1
index a3539ef..43168fb 100644
@@ -1,11 +1,15 @@
 #include <errno.h>
 #include "perf_regs.h"
+#include "event.h"
 
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 {
 	int i, idx = 0;
 	u64 mask = regs->mask;
 
+	if (regs->cache_mask & (1 << id))
+		goto out;
+
 	if (!(mask & (1 << id)))
 		return -EINVAL;
 
@@ -14,6 +18,10 @@ int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 			idx++;
 	}
 
-	*valp = regs->regs[idx];
+	regs->cache_mask |= (1 << id);
+	regs->cache_regs[id] = regs->regs[idx];
+
+out:
+	*valp = regs->cache_regs[id];
 	return 0;
 }
tools/perf/util/perf_regs.h +3 -1
index d6e8b6a..80d8ab1 100644
@@ -2,15 +2,17 @@
 #define __PERF_REGS_H
 
 #include "types.h"
-#include "event.h"
 
 #ifdef HAVE_PERF_REGS_SUPPORT
 #include <perf_regs.h>
 
+struct regs_dump;
+
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
 
 #else
 #define PERF_REGS_MASK	0
+#define PERF_REGS_MAX	0
 
 static inline const char *perf_reg_name(int id __maybe_unused)
 {
Signature · 1 line
1.8.3.1
maintainer
#2 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

Hi Jiri,

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 3 lines

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

I'm not familiar with the code base, so probably silly questions: Where does the speed up come from? IOW I don't know what's the difference between the regs->regs and regs->cached_regs. And does the cached_regs contain correct values of registers for each frame?

Thanks,
Namhyung

Quoted 122 lines

Output from report over 1.5 GB data with DWARF unwind stacks:
(TODO fix perf diff)

current code:
6.81% perf.old perf.old [.] perf_reg_value

change:
2.24% perf perf [.] perf_reg_value

And little bit of speed up:

Performance counter stats for './perf.old report -i perf-test.data --stdio':

   134,664,011,577      cycles:u                  #    2.472 GHz
   189,677,227,475      instructions:u            #    1.41  insns per cycle
      54465.096050      task-clock (msec)         #    0.998 CPUs utilized
      54.598339009 seconds time elapsed

Performance counter stats for './perf report -i perf-test.data --stdio':

   124,478,681,672      cycles:u                  #    2.466 GHz
   168,998,379,866      instructions:u            #    1.36  insns per cycle
      50487.110482      task-clock (msec)         #    0.997 CPUs utilized
      50.635824229 seconds time elapsed
Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jean Pihet <[email protected]>
---
 tools/perf/util/event.h     |  5 +++++
 tools/perf/util/perf_regs.c | 10 +++++++++-
 tools/perf/util/perf_regs.h |  4 +++-
 3 files changed, 17 insertions(+), 2 deletions(-)
tools/perf/util/event.h +5 -0
index 38457d4..970d4eb 100644
@@ -7,6 +7,7 @@
 #include "../perf.h"
 #include "map.h"
 #include "build-id.h"
+#include "perf_regs.h"
 
 struct mmap_event {
 	struct perf_event_header header;
@@ -87,6 +88,10 @@ struct regs_dump {
 	u64 abi;
 	u64 mask;
 	u64 *regs;
+
+	/* Cached values/mask filled by first register access. */
+	u64 cache_regs[PERF_REGS_MAX];
+	u64 cache_mask;
 };
 
 struct stack_dump {
tools/perf/util/perf_regs.c +9 -1
index a3539ef..43168fb 100644
@@ -1,11 +1,15 @@
 #include <errno.h>
 #include "perf_regs.h"
+#include "event.h"
 
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 {
 	int i, idx = 0;
 	u64 mask = regs->mask;
 
+	if (regs->cache_mask & (1 << id))
+		goto out;
+
 	if (!(mask & (1 << id)))
 		return -EINVAL;
 
@@ -14,6 +18,10 @@ int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 			idx++;
 	}
 
-	*valp = regs->regs[idx];
+	regs->cache_mask |= (1 << id);
+	regs->cache_regs[id] = regs->regs[idx];
+
+out:
+	*valp = regs->cache_regs[id];
 	return 0;
 }
tools/perf/util/perf_regs.h +3 -1
index d6e8b6a..80d8ab1 100644
@@ -2,15 +2,17 @@
 #define __PERF_REGS_H
 
 #include "types.h"
-#include "event.h"
 
 #ifdef HAVE_PERF_REGS_SUPPORT
 #include <perf_regs.h>
 
+struct regs_dump;
+
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
 
 #else
 #define PERF_REGS_MASK	0
+#define PERF_REGS_MAX	0
 
 static inline const char *perf_reg_name(int id __maybe_unused)
 {
author
#3 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

Quoted 11 lines · Namhyung Kim

Hi Jiri,

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 3 lines

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

I'm not familiar with the code base, so probably silly questions: Where does the speed up come from? IOW I don't know what's the difference between the regs->regs and regs->cached_regs. And does the cached_regs contain correct values of registers for each frame?

the current way register's value is accessed is to get its index in the sample's regs array.. based on register's id and the registers mask

so each time you want register value you traverse the registers mask and count reg's index for the sample regs array

this patch does this only once for each register (at the time it's first accessed) and cache its value in the array (cache_regs). The cache_mask is used to identify which regs are already cached.

jirka

maintainer
#4 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

Hi Jiri,

2014-04-28 (월), 11:48 +0200, Jiri Olsa:

Quoted 17 lines · Namhyung Kim
Quoted 11 lines

Hi Jiri,

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 3 lines

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

I'm not familiar with the code base, so probably silly questions: Where does the speed up come from? IOW I don't know what's the difference between the regs->regs and regs->cached_regs. And does the cached_regs contain correct values of registers for each frame?

the current way register's value is accessed is to get its index in the sample's regs array.. based on register's id and the registers mask

Quoted 6 lines

so each time you want register value you traverse the registers mask and count reg's index for the sample regs array

this patch does this only once for each register (at the time it's first accessed) and cache its value in the array (cache_regs). The cache_mask is used to identify which regs are already cached.

That means it'll get the same value everytime it accesses a register in frames in a sample?

Thanks,
Namhyung

author
#5 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

Quoted 23 lines · Namhyung Kim

Hi Jiri,

2014-04-28 (월), 11:48 +0200, Jiri Olsa:

Quoted 20 lines · Namhyung Kim
Quoted 11 lines

Hi Jiri,

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 3 lines

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

I'm not familiar with the code base, so probably silly questions: Where does the speed up come from? IOW I don't know what's the difference between the regs->regs and regs->cached_regs. And does the cached_regs contain correct values of registers for each frame?

the current way register's value is accessed is to get its index in the sample's regs array.. based on register's id and the registers mask

so each time you want register value you traverse the registers mask and count reg's index for the sample regs array

Quoted 6 lines
Quoted 3 lines

this patch does this only once for each register (at the time it's first accessed) and cache its value in the array (cache_regs). The cache_mask is used to identify which regs are already cached.

That means it'll get the same value everytime it accesses a register in frames in a sample?

right..

jirka

maintainer
#6 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

Quoted 24 lines · Jiri Olsa
Quoted 23 lines · Namhyung Kim

Hi Jiri,

2014-04-28 (월), 11:48 +0200, Jiri Olsa:

Quoted 20 lines · Namhyung Kim
Quoted 11 lines

Hi Jiri,

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 3 lines

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

I'm not familiar with the code base, so probably silly questions: Where does the speed up come from? IOW I don't know what's the difference between the regs->regs and regs->cached_regs. And does the cached_regs contain correct values of registers for each frame?

the current way register's value is accessed is to get its index in the sample's regs array.. based on register's id and the registers mask

so each time you want register value you traverse the registers mask and count reg's index for the sample regs array

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

this patch does this only once for each register (at the time it's first accessed) and cache its value in the array (cache_regs). The cache_mask is used to identify which regs are already cached.

That means it'll get the same value everytime it accesses a register in frames in a sample?

right..

Hmm.. I thought it'd be changed somehow as it unwinds frames.

Thanks,
Namhyung

author
#7 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

Quoted 29 lines · Namhyung Kim
Quoted 28 lines · Jiri Olsa
Quoted 27 lines · Namhyung Kim

Hi Jiri,

2014-04-28 (월), 11:48 +0200, Jiri Olsa:

Quoted 23 lines · Namhyung Kim
Quoted 11 lines

Hi Jiri,

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 3 lines

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

I'm not familiar with the code base, so probably silly questions: Where does the speed up come from? IOW I don't know what's the difference between the regs->regs and regs->cached_regs. And does the cached_regs contain correct values of registers for each frame?

the current way register's value is accessed is to get its index in the sample's regs array.. based on register's id and the registers mask

so each time you want register value you traverse the registers mask and count reg's index for the sample regs array

this patch does this only once for each register (at the time it's first accessed) and cache its value in the array (cache_regs). The cache_mask is used to identify which regs are already cached.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

That means it'll get the same value everytime it accesses a register in frames in a sample?

right..

Hmm.. I thought it'd be changed somehow as it unwinds frames.

nope, it's just sample's user space registers values from the time sample was taken

both libunwind and libdw unwinders keep the registers state through the frames unwinding internally

jirka

#8 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

In reply to Jiri Olsa
Quoted 116 lines · Jiri Olsa

Caching registers value into an array. Got about 4% speed up of perf_reg_value function for report command processing dwarf unwind stacks.

Output from report over 1.5 GB data with DWARF unwind stacks:
(TODO fix perf diff)

current code:
6.81% perf.old perf.old [.] perf_reg_value

change:
2.24% perf perf [.] perf_reg_value

And little bit of speed up:

Performance counter stats for './perf.old report -i perf-test.data --stdio':

   134,664,011,577      cycles:u                  #    2.472 GHz
   189,677,227,475      instructions:u            #    1.41  insns per cycle
      54465.096050      task-clock (msec)         #    0.998 CPUs utilized
      54.598339009 seconds time elapsed

Performance counter stats for './perf report -i perf-test.data --stdio':

   124,478,681,672      cycles:u                  #    2.466 GHz
   168,998,379,866      instructions:u            #    1.36  insns per cycle
      50487.110482      task-clock (msec)         #    0.997 CPUs utilized
      50.635824229 seconds time elapsed
Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jean Pihet <[email protected]>
---
 tools/perf/util/event.h     |  5 +++++
 tools/perf/util/perf_regs.c | 10 +++++++++-
 tools/perf/util/perf_regs.h |  4 +++-
 3 files changed, 17 insertions(+), 2 deletions(-)
tools/perf/util/event.h +5 -0
index 38457d4..970d4eb 100644
@@ -7,6 +7,7 @@
 #include "../perf.h"
 #include "map.h"
 #include "build-id.h"
+#include "perf_regs.h"
 
 struct mmap_event {
 	struct perf_event_header header;
@@ -87,6 +88,10 @@ struct regs_dump {
 	u64 abi;
 	u64 mask;
 	u64 *regs;
+
+	/* Cached values/mask filled by first register access. */
+	u64 cache_regs[PERF_REGS_MAX];
+	u64 cache_mask;
 };
 
 struct stack_dump {
tools/perf/util/perf_regs.c +9 -1
index a3539ef..43168fb 100644
@@ -1,11 +1,15 @@
 #include <errno.h>
 #include "perf_regs.h"
+#include "event.h"
 
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 {
 	int i, idx = 0;
 	u64 mask = regs->mask;
 
+	if (regs->cache_mask & (1 << id))
+		goto out;
+
 	if (!(mask & (1 << id)))
 		return -EINVAL;
 
@@ -14,6 +18,10 @@ int perf_reg_value(u64 *valp, struct regs_dump *regs, int id)
 			idx++;
 	}
 
-	*valp = regs->regs[idx];
+	regs->cache_mask |= (1 << id);
+	regs->cache_regs[id] = regs->regs[idx];
+
+out:
+	*valp = regs->cache_regs[id];
 	return 0;
 }
tools/perf/util/perf_regs.h +0 -1
index d6e8b6a..80d8ab1 100644
@@ -2,15 +2,17 @@
 #define __PERF_REGS_H
 
 #include "types.h"
-#include "event.h"
 
 #ifdef HAVE_PERF_REGS_SUPPORT
 #include <perf_regs.h>
 

tools/perf/util/perf_regs.h line 18

Quoted 10 lines
tools/perf/util/perf_regs.h +3 -0
+struct regs_dump;
+
 int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);
 
 #else
 #define PERF_REGS_MASK	0
+#define PERF_REGS_MAX	0
 
 static inline const char *perf_reg_name(int id __maybe_unused)
 {

Want such a speedup,
but it does not compile on my s390x system:

CC util/top.o
In file included from util/event.h:10:0,
from util/event.c:2:
util/perf_regs.h:24:6: error: ‘struct regs_dump’ declared inside parameter list [-Werror] util/perf_regs.h:24:6: error: its scope is only this definition or declaration, which is probably not what you want [-Werror] In file included from util/event.h:10:0,

                 from util/callchain.h:7,
                 from util/hist.h:6,
                 from util/evsel.h:11,
                 from util/evsel.c:18:
util/perf_regs.h:24:6: error: ‘struct regs_dump’ declared inside parameter list [-Werror]
util/perf_regs.h:24:6: error: its scope is only this definition or declaration, which is probably not what you want [-Werror]
In file included from /home/cborntra/REPOS/linux/tools/perf/util/event.h:10:0,
                 from /home/cborntra/REPOS/linux/tools/perf/util/debug.h:6,
                 from util/cpumap.h:8,
                 from util/top.c:9:
/home/cborntra/REPOS/linux/tools/perf/util/perf_regs.h:24:6: error: ‘struct regs_dump’ declared inside parameter list [-Werror]
/home/cborntra/REPOS/linux/tools/perf/util/perf_regs.h:24:6: error: its scope is only this definition or declaration, which is probably not what you want [-Werror]
In file included from /home/cborntra/REPOS/linux/tools/perf/util/event.h:10:0,
                 from /home/cborntra/REPOS/linux/tools/perf/util/debug.h:6,
                 from util/cpumap.h:8,
                 from util/evlist.c:12:
/home/cborntra/REPOS/linux/tools/perf/util/perf_regs.h:24:6: error: ‘struct regs_dump’ declared inside parameter list [-Werror]
/home/cborntra/REPOS/linux/tools/perf/util/perf_regs.h:24:6: error: its scope is only this definition or declaration, which is probably not what you want [-Werror]
  CC       util/usage.o
  CC       util/wrapper.o
  CC       util/sigchain.o

In file included from util/event.h:10:0,
from util/header.h:8,
from util/parse-options.c:4:
util/perf_regs.h:24:6: error: ‘struct regs_dump’ declared inside parameter list [-Werror] util/perf_regs.h:24:6: error: its scope is only this definition or declaration, which is probably not what you want [-Werror]
cc1: all warnings being treated as errors
make[1]: *** [util/parse-options.o] Error 1
make[1]: *** Waiting for unfinished jobs....
cc1: all warnings being treated as errors
cc1: all warnings being treated as errors
make[1]: *** [util/event.o] Error 1
make[1]: *** [util/evsel.o] Error 1
In file included from util/event.h:10:0,

                 from util/debug.h:6,
                 from util/usage.c:10:

util/perf_regs.h:24:6: error: ‘struct regs_dump’ declared inside parameter list [-Werror] util/perf_regs.h:24:6: error: its scope is only this definition or declaration, which is probably not what you want [-Werror]
cc1: all warnings being treated as errors
make[1]: *** [util/usage.o] Error 1
cc1: all warnings being treated as errors
make[1]: *** [util/evlist.o] Error 1
cc1: all warnings being treated as errors
make[1]: *** [util/top.o] Error 1

author
#9 lore raw

Re: [PATCH 1/3] perf tools: Cache register accesses for unwind processing

On Mon, Apr 28, 2014 at 12:39:24PM +0200, Christian Borntraeger wrote:

SNIP

Quoted 5 lines
Quoted 2 lines

{

Want such a speedup,
but it does not compile on my s390x system:

the speed up is for DWARF unwind report, which is not yet supported on s390x perf.. still it should compile ;-)

I'll try to get some s390x and make a fix

thanks,
jirka

author
#10 lore raw

PATCH 2/3 perf tools: Cache dso data file descriptor

In reply to Jiri Olsa

Keeping the data file description open for the whole life of the dso object.

The report shows just little speedup in dso__data_fd function for report command processing dwarf unwind stacks.

Output from report over 1.5 GB data with DWARF unwind stacks:
(TODO fix perf diff)

current code:
0.22% perf.old perf.old [.] dso__data_fd

change:
0.15% perf perf [.] dso__data_fd

But a bigger overall speedup:

Performance counter stats for './perf.old report -i perf-test.data --stdio':

   126,055,895,573      cycles:u                  #    2.463 GHz
   168,964,795,208      instructions:u            #    1.34  insns per cycle
      51174.366434      task-clock (msec)         #    0.997 CPUs utilized
      51.306236943 seconds time elapsed

Performance counter stats for './perf report -i perf-test.data --stdio':

   112,531,906,656      cycles:u                  #    2.680 GHz
   163,466,037,207      instructions:u            #    1.45  insns per cycle
      41991.297576      task-clock (msec)         #    1.000 CPUs utilized
      41.985142753 seconds time elapsed
Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jean Pihet <[email protected]>
---
 tools/perf/util/dso.c              | 15 +++++++++++----
 tools/perf/util/dso.h              |  1 +
 tools/perf/util/unwind-libunwind.c |  2 --
 3 files changed, 12 insertions(+), 6 deletions(-)
tools/perf/util/dso.c +11 -4
index 64453d6..0dca5d6 100644
@@ -159,6 +159,12 @@ static int open_dso(struct dso *dso, struct machine *machine)
 	return fd;
 }
 
+static void dso__data_close(struct dso *dso)
+{
+	if (dso->data_fd >= 0)
+		close(dso->data_fd);
+}
+
 int dso__data_fd(struct dso *dso, struct machine *machine)
 {
 	enum dso_binary_type binary_type_data[] = {
@@ -168,8 +174,8 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 	};
 	int i = 0;
 
-	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND)
-		return open_dso(dso, machine);
+	if (dso->data_fd >= 0)
+		return dso->data_fd;
 
 	do {
 		int fd;
@@ -178,7 +184,7 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 
 		fd = open_dso(dso, machine);
 		if (fd >= 0)
-			return fd;
+			return dso->data_fd = fd;
 
 	} while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
 
@@ -301,7 +307,6 @@ dso_cache__read(struct dso *dso, struct machine *machine,
 	if (ret <= 0)
 		free(cache);
 
-	close(fd);
 	return ret;
 }
 
@@ -485,6 +490,7 @@ struct dso *dso__new(const char *name)
 		dso->kernel = DSO_TYPE_USER;
 		dso->needs_swap = DSO_SWAP__UNSET;
 		INIT_LIST_HEAD(&dso->node);
+		dso->data_fd = -1;
 	}
 
 	return dso;
@@ -506,6 +512,7 @@ void dso__delete(struct dso *dso)
 		dso->long_name_allocated = false;
 	}
 
+	dso__data_close(dso);
 	dso_cache__free(&dso->cache);
 	dso__free_a2l(dso);
 	zfree(&dso->symsrc_filename);
tools/perf/util/dso.h +1 -0
index ab06f1c..6e48cdc 100644
@@ -99,6 +99,7 @@ struct dso {
 	const char	 *long_name;
 	u16		 long_name_len;
 	u16		 short_name_len;
+	int		 data_fd;
 	char		 name[0];
 };
 
tools/perf/util/unwind-libunwind.c +0 -2
index bd5768d..25578b9 100644
@@ -250,7 +250,6 @@ static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
 
 	/* Check the .eh_frame section for unwinding info */
 	offset = elf_section_offset(fd, ".eh_frame_hdr");
-	close(fd);
 
 	if (offset)
 		ret = unwind_spec_ehframe(dso, machine, offset,
@@ -271,7 +270,6 @@ static int read_unwind_spec_debug_frame(struct dso *dso,
 
 	/* Check the .debug_frame section for unwinding info */
 	*offset = elf_section_offset(fd, ".debug_frame");
-	close(fd);
 
 	if (*offset)
 		return 0;
Signature · 1 line
1.8.3.1
maintainer
#11 lore raw

Re: [PATCH 2/3] perf tools: Cache dso data file descriptor

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 2 lines

Keeping the data file description open for the whole life of the dso object.

I suspect there might be an issue for reporting very large data file with this approach - like open file limit?

[SNIP]

tools/perf/util/dso.c int dso__data_fd(struct dso *dso, struct machine *machine) · line 172

Quoted 6 lines
tools/perf/util/dso.c +0 -2
@@ -168,8 +174,8 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 	};
 	int i = 0;
 
-	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND)
-		return open_dso(dso, machine);

Why did you remove this line?

Thanks,
Namhyung

Quoted 13 lines
tools/perf/util/dso.c +3 -1
+	if (dso->data_fd >= 0)
+		return dso->data_fd;
 
 	do {
 		int fd;
@@ -178,7 +184,7 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 
 		fd = open_dso(dso, machine);
 		if (fd >= 0)
-			return fd;
+			return dso->data_fd = fd;
 
 	} while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
author
#12 lore raw

Re: [PATCH 2/3] perf tools: Cache dso data file descriptor

Quoted 6 lines · Namhyung Kim

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 2 lines

Keeping the data file description open for the whole life of the dso object.

I suspect there might be an issue for reporting very large data file with this approach - like open file limit?

I've got as high as ~200 openned file descriptors for ~2GB data of system wide monitoring

but right that could be an issue.. I wonder we could workaround this somehow, because the speed up is quite noticable

how about we monitor number of openned dso file descriptor and once we cross this we close some portion of them

or something along those lines ;-)

Quoted 11 lines

[SNIP]

tools/perf/util/dso.c int dso__data_fd(struct dso *dso, struct machine *machine) · line 172

Quoted 6 lines
tools/perf/util/dso.c +0 -2
@@ -168,8 +174,8 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 	};
 	int i = 0;
 
-	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND)
-		return open_dso(dso, machine);

Why did you remove this line?

that code reopens already openned (and closed) file.. instead I return (not closed) descriptor from previous open

thanks,
jirka

maintainer
#13 lore raw

Re: [PATCH 2/3] perf tools: Cache dso data file descriptor

2014-04-28 (월), 12:01 +0200, Jiri Olsa:

Quoted 11 lines · Namhyung Kim
Quoted 6 lines

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 2 lines

Keeping the data file description open for the whole life of the dso object.

I suspect there might be an issue for reporting very large data file with this approach - like open file limit?

I've got as high as ~200 openned file descriptors for ~2GB data of system wide monitoring

Quoted 8 lines

but right that could be an issue.. I wonder we could workaround this somehow, because the speed up is quite noticable

how about we monitor number of openned dso file descriptor and once we cross this we close some portion of them

or something along those lines ;-)

Yeah, we'll need some way to control those eventually.

Quoted 8 lines
Quoted 8 lines

[SNIP]

Quoted 4 lines
tools/perf/util/dso.c +0 -0
@@ -168,8 +174,8 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 	};
 	int i = 0;
 
Quoted 7 lines
Quoted 4 lines

tools/perf/util/dso.c int dso__data_fd(struct dso *dso, struct machine *machine) · line 172

Quoted 2 lines
tools/perf/util/dso.c +0 -2
-	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND)
-		return open_dso(dso, machine);

Why did you remove this line?

that code reopens already openned (and closed) file.. instead I return (not closed) descriptor from previous open

But it'll overwrite the dso->binary_type then. What about this?

	if (dso->data_fd >= 0)
		return dso->data_fd;

	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
		dso->data_fd = open_dso(dso, machine);
		return dso->data_fd;
	}

Thanks,
Namhyung

author
#14 lore raw

Re: [PATCH 2/3] perf tools: Cache dso data file descriptor

Quoted 41 lines · Namhyung Kim

2014-04-28 (월), 12:01 +0200, Jiri Olsa:

Quoted 11 lines · Namhyung Kim
Quoted 6 lines

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 2 lines

Keeping the data file description open for the whole life of the dso object.

I suspect there might be an issue for reporting very large data file with this approach - like open file limit?

I've got as high as ~200 openned file descriptors for ~2GB data of system wide monitoring

Quoted 8 lines

but right that could be an issue.. I wonder we could workaround this somehow, because the speed up is quite noticable

how about we monitor number of openned dso file descriptor and once we cross this we close some portion of them

or something along those lines ;-)

Yeah, we'll need some way to control those eventually.

Quoted 8 lines
Quoted 8 lines

[SNIP]

Quoted 4 lines
tools/perf/util/dso.c +0 -0
@@ -168,8 +174,8 @@ int dso__data_fd(struct dso *dso, struct machine *machine)
 	};
 	int i = 0;
 
Quoted 7 lines
Quoted 4 lines

tools/perf/util/dso.c int dso__data_fd(struct dso *dso, struct machine *machine) · line 172

Quoted 2 lines
tools/perf/util/dso.c +0 -2
-	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND)
-		return open_dso(dso, machine);

Why did you remove this line?

that code reopens already openned (and closed) file.. instead I return (not closed) descriptor from previous open

But it'll overwrite the dso->binary_type then. What about this?

Quoted 7 lines
	if (dso->data_fd >= 0)
		return dso->data_fd;

	if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
		dso->data_fd = open_dso(dso, machine);
		return dso->data_fd;
	}

right, makes sense.. I'll add it with the control code for the number of openned descriptors

thanks,
jirka

#15 lore raw

Re: [PATCH 2/3] perf tools: Cache dso data file descriptor

In reply to Namhyung Kim
Quoted 16 lines · Namhyung Kim

2014-04-28 (월), 12:01 +0200, Jiri Olsa:

Quoted 15 lines · Namhyung Kim
Quoted 6 lines

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 2 lines

Keeping the data file description open for the whole life of the dso object.

I suspect there might be an issue for reporting very large data file with this approach - like open file limit?

I've got as high as ~200 openned file descriptors for ~2GB data of system wide monitoring

but right that could be an issue.. I wonder we could workaround this somehow, because the speed up is quite noticable

Quoted 6 lines
Quoted 4 lines

how about we monitor number of openned dso file descriptor and once we cross this we close some portion of them

or something along those lines ;-)

Yeah, we'll need some way to control those eventually.

Handle EMFILE failures. Find an "old" one and close it to let the new one succeed.

David

maintainer
#16 lore raw

Re: [PATCH 2/3] perf tools: Cache dso data file descriptor

Hi David,

Quoted 20 lines · David Ahern
Quoted 19 lines · Namhyung Kim

2014-04-28 (월), 12:01 +0200, Jiri Olsa:

Quoted 18 lines · Namhyung Kim
Quoted 6 lines

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 2 lines

Keeping the data file description open for the whole life of the dso object.

I suspect there might be an issue for reporting very large data file with this approach - like open file limit?

I've got as high as ~200 openned file descriptors for ~2GB data of system wide monitoring

but right that could be an issue.. I wonder we could workaround this somehow, because the speed up is quite noticable

how about we monitor number of openned dso file descriptor and once we cross this we close some portion of them

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

or something along those lines ;-)

Yeah, we'll need some way to control those eventually.

Handle EMFILE failures. Find an "old" one and close it to let the new one succeed.

But it would make other open(), if any, fail anyway.. So I'd rather limit the size of the dso cache to a reasonable size.

Thanks,
Namhyung

#17 lore raw

Re: [PATCH 2/3] perf tools: Cache dso data file descriptor

In reply to Jiri Olsa
Quoted 10 lines · Jiri Olsa
Quoted 6 lines · Namhyung Kim

2014-04-17 (목), 19:39 +0200, Jiri Olsa:

Quoted 2 lines

Keeping the data file description open for the whole life of the dso object.

I suspect there might be an issue for reporting very large data file with this approach - like open file limit?

I've got as high as ~200 openned file descriptors for ~2GB data of system wide monitoring

Note that 200 open file descriptors in themselves are not a scalability problem on Linux, as long as perf doesn't walk them linearly anywhere.

I think we are reasonably fast even with a million open files in a singe process, or so.

Thanks,

Ingo

author
#18 lore raw

PATCH 3/3 perf tools: Replace dso data cache with mapped data

In reply to Jiri Olsa

Removing dso data cache processing and mapping
whole dso object instead when requested.

Got about 13% speed up in dso__data_read_offset function for report command processing dwarf unwind stacks.

Output from report over 1.5 GB data with DWARF unwind stacks:
(TODO fix perf diff)

13.63% perf.old perf.old [.] dso__data_read_offset

0.32% perf perf [.] dso__data_read_offset

And overall speedup:

Performance counter stats for './perf.old report -i perf-test.data --stdio':

   113,076,591,004      cycles:u                  #    2.675 GHz
   163,353,590,494      instructions:u            #    1.44  insns per cycle
      42269.774797      task-clock (msec)         #    1.000 CPUs utilized
      42.267550053 seconds time elapsed

Performance counter stats for './perf report -i perf-test.data --stdio':

    92,953,167,072      cycles:u                  #    2.534 GHz
   132,967,448,023      instructions:u            #    1.43  insns per cycle
      36683.242639      task-clock (msec)         #    1.000 CPUs utilized
      36.682799394 seconds time elapsed
Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jean Pihet <[email protected]>
---
 tools/perf/tests/dso-data.c |   7 ++
 tools/perf/util/dso.c       | 185 +++++++++++---------------------------------
 tools/perf/util/dso.h       |  13 +---
 3 files changed, 54 insertions(+), 151 deletions(-)
tools/perf/tests/dso-data.c +7 -0
index 9cc81a3..024c15f 100644
@@ -40,6 +40,13 @@ static char *test_file(int size)
 	return templ;
 }
 
+/*
+ * The data access is now pure memory map of the file,
+ * so we dont need DSO__DATA_CACHE_SIZE anymore.
+ * Anyway keeping it for the sake of this test to
+ * ensure dso__data_read_offset interface works.
+ */
+#define DSO__DATA_CACHE_SIZE 4096
 #define TEST_FILE_SIZE (DSO__DATA_CACHE_SIZE * 20)
 
 struct test_data_offset {
tools/perf/util/dso.c +45 -140

232 lines not on this page · raw message

tools/perf/util/dso.h +2 -11
index 6e48cdc..fe4e4aa 100644
@@ -62,21 +62,10 @@ enum dso_swap_type {
 	____r;						\
 })
 
-#define DSO__DATA_CACHE_SIZE 4096
-#define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
-
-struct dso_cache {
-	struct rb_node	rb_node;
-	u64 offset;
-	u64 size;
-	char data[0];
-};
-
 struct dso {
 	struct list_head node;
 	struct rb_root	 symbols[MAP__NR_TYPES];
 	struct rb_root	 symbol_names[MAP__NR_TYPES];
-	struct rb_root	 cache;
 	void		 *a2l;
 	char		 *symsrc_filename;
 	unsigned int	 a2l_fails;
@@ -100,6 +89,8 @@ struct dso {
 	u16		 long_name_len;
 	u16		 short_name_len;
 	int		 data_fd;
+	size_t		 data_size;
+	char		 *data_mmap;
 	char		 name[0];
 };
 
Signature · 1 line
1.8.3.1
#19 lore raw
In reply to Jiri Olsa
Quoted 9 lines · Jiri Olsa

hi,
trying to speedup DWARF unwind report code by factoring related code:
- caching sample's registers access
- keep dso data file descriptor open for the life of the dso object
- replace dso cache code by mapping dso data file directly for the life of the dso object

Quoted 3 lines

The speedup is mainly for libunwind unwind. The libdw will benefit mainly from cached registers access, because it handles dso data accesses by itself.. and anyway it's still faster ;-).

Just curious: do you have any numbers about how much faster it got in practice?

Thanks,

Ingo

#20 lore raw
Quoted 12 lines · Ingo Molnar
Quoted 9 lines · Jiri Olsa

hi,
trying to speedup DWARF unwind report code by factoring related code:
- caching sample's registers access
- keep dso data file descriptor open for the life of the dso object
- replace dso cache code by mapping dso data file directly for the life of the dso object

Quoted 6 lines
Quoted 3 lines

The speedup is mainly for libunwind unwind. The libdw will benefit mainly from cached registers access, because it handles dso data accesses by itself.. and anyway it's still faster ;-).

Just curious: do you have any numbers about how much faster it got in practice?

Oh, the numbers are all in the changelogs, never mind!

So in your test workload it went from 54.6 seconds to 36.7, a 48% speedup :-)

Thanks,

Ingo

author
#21 lore raw
Quoted 19 lines · Ingo Molnar
Quoted 16 lines · Ingo Molnar
Quoted 12 lines · Jiri Olsa

hi,
trying to speedup DWARF unwind report code by factoring related code:
- caching sample's registers access
- keep dso data file descriptor open for the life of the dso object
- replace dso cache code by mapping dso data file directly for the life of the dso object

The speedup is mainly for libunwind unwind. The libdw will benefit mainly from cached registers access, because it handles dso data accesses by itself.. and anyway it's still faster ;-).

Quoted 8 lines
Quoted 2 lines

Just curious: do you have any numbers about how much faster it got in practice?

Oh, the numbers are all in the changelogs, never mind!

So in your test workload it went from 54.6 seconds to 36.7, a 48% speedup :-)

yep, I should have put it in here as well.. also the current libdw unwind time on this workload is 26 seconds.. 10 more seconds to go ;-)

jirka

author
#22 lore raw
In reply to Jiri Olsa
Quoted 9 lines · Jiri Olsa

hi,
trying to speedup DWARF unwind report code by factoring related code:
- caching sample's registers access
- keep dso data file descriptor open for the life of the dso object
- replace dso cache code by mapping dso data file directly for the life of the dso object

Quoted 7 lines

The speedup is mainly for libunwind unwind. The libdw will benefit mainly from cached registers access, because it handles dso data accesses by itself.. and anyway it's still faster ;-).

Also reachable in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_unwind_speedup

rebased to latest tip perf/core, review appreciated ;-)

jirka

author
#23 lore raw
In reply to Jiri Olsa

ping, any feedback?

thanks,
jirka

Quoted 44 lines · Jiri Olsa

hi,
trying to speedup DWARF unwind report code by factoring related code:
- caching sample's registers access
- keep dso data file descriptor open for the life of the dso object
- replace dso cache code by mapping dso data file directly for the life of the dso object

The speedup is mainly for libunwind unwind. The libdw will benefit mainly from cached registers access, because it handles dso data accesses by itself.. and anyway it's still faster ;-).

Also reachable in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_unwind_speedup

thanks,
jirka

Cc Corey Ashford <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jean Pihet <[email protected]>

---
Jiri Olsa (3):
perf tools: Cache register accesses for unwind processing perf tools: Cache dso data file descriptor
perf tools: Replace dso data cache with mapped data

 tools/perf/tests/dso-data.c        |   7 ++++
 tools/perf/util/dso.c              | 200 +++++++++++++++++++++++++++---------------------------------------------------------------------
 tools/perf/util/dso.h              |  14 ++-----
 tools/perf/util/event.h            |   5 +++
 tools/perf/util/perf_regs.c        |  10 ++++-
 tools/perf/util/perf_regs.h        |   4 +-
 tools/perf/util/unwind-libunwind.c |   2 -
 7 files changed, 83 insertions(+), 159 deletions(-)