perf tools: Speedup DWARF unwind
Jiri Olsa
reviewerv2 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 ;-).
v2 changes:
- adding limit for open dso objects with sort of LRU mechanism to pick up and close dso objects if we are over the limit
- file mmaping changes are omitted, because I couldn't prove the improvement exactly, will resubmit later
- added dso close logic in case of no memory
- added tests
On 10GB perf data file with dwarf unwind data I've got around 30% speed up.
Also reachable in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_unwind_speedup
thanks,
jirka
---
Jiri Olsa (14):
perf tools: Cache register accesses for unwind processing perf tools: Separate dso data related variables
perf tools: Add data_fd into dso object
perf tools: Add global list of opened dso objects perf tools: Add global count of opened dso objects perf tools: Cache dso data file descriptor
perf tools: Add file size check and factor dso__data_read_offset perf tools: Allow to close dso fd in case of open failure perf tools: Add dso__data_* interface descriptons perf tests: Spawn child for each test
perf tests: Allow reuse of test_file function
perf tests: Add test interface for dso data fd limit perf tests: Add test for caching dso file descriptors perf tests: Add test for closing dso objects on EMFILE error
tools/perf/tests/builtin-test.c | 42 ++++++++++++++- tools/perf/tests/dso-data.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- tools/perf/tests/tests.h | 2 + tools/perf/util/dso.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- tools/perf/util/dso.h | 56 +++++++++++++++++++- 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 | 4 +- 9 files changed, 617 insertions(+), 36 deletions(-)
Jiri Olsa
reviewerPATCH v2 1/14 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:
5.84% perf perf [.] perf_reg_value
change:
1.94% perf perf [.] perf_reg_value
And little bit of overall speed up:
(perf stat -r 5 -e '{cycles,instructions}:u' ...)
current code:
310,298,611,754 cycles ( +- 0.33% ) 439,669,689,341 instructions ( +- 0.03% )
188.656753166 seconds time elapsed ( +- 0.82% )
change:
291,315,329,878 cycles ( +- 0.22% ) 391,763,485,304 instructions ( +- 0.03% )
180.742249687 seconds time elapsed ( +- 0.64% )
--- 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
@@ -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
@@ -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
@@ -2,7 +2,8 @@#define __PERF_REGS_H#include <linux/types.h> -#include "event.h" + +struct regs_dump ;#ifdef HAVE_PERF_REGS_SUPPORT #include <perf_regs.h>
@@ -11,6 +12,7 @@ int perf_reg_value(u64 *valp, struct regs_dump *regs, int id);#else #define PERF_REGS_MASK 0 +#define PERF_REGS_MAX 0static inline const char *perf_reg_name (int id__maybe_unused ) {
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 2/14 perf tools: Separate dso data related variables
In reply to Jiri OlsaAdd separated structure/namespace for data related variables. We are going to add mode of them, so this way they will be clearly separated.
--- tools/perf/util/dso.c | 8 ++++---- tools/perf/util/dso.h | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-)
tools/perf/util/dso.c +4 -4
@@ -292,7 +292,7 @@ dso_cache__read(struct dso *dso, struct machine *machine, cache->offset = cache_offset; cache->size = ret; -dso_cache__insert (&dso->cache, cache); +dso_cache__insert (&dso->data.cache, cache); ret =dso_cache__memcpy (cache, offset, data, size);
@@ -310,7 +310,7 @@ static ssize_t dso_cache_read(struct dso *dso, struct machine *machine, {struct dso_cache *cache; - cache =dso_cache__find (&dso->cache, offset); + cache =dso_cache__find (&dso->data.cache, offset);if (cache)return dso_cache__memcpy (cache, offset, data, size);else
@@ -473,7 +473,7 @@ struct dso *dso__new(const char *name)dso__set_short_name (dso, dso->name, false);for (i =0 ; i <MAP__NR_TYPES ; ++i) dso->symbols[i] = dso->symbol_names[i] =RB_ROOT ; - dso->cache =RB_ROOT ; + dso->data.cache =RB_ROOT ; dso->symtab_type =DSO_BINARY_TYPE__NOT_FOUND ; dso->binary_type =DSO_BINARY_TYPE__NOT_FOUND ; dso->loaded =0 ;
@@ -506,7 +506,7 @@ void dso__delete(struct dso *dso) dso->long_name_allocated = false; } -dso_cache__free (&dso->cache); +dso_cache__free (&dso->data.cache);dso__free_a2l (dso);zfree (&dso->symsrc_filename);free (dso);
tools/perf/util/dso.h +6 -1
@@ -76,7 +76,6 @@ 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;
@@ -99,6 +98,12 @@ struct dso {const char *long_name;u16 long_name_len;u16 short_name_len; + +/* dso data file */ +struct { +struct rb_root cache; + } data; +char name[0 ]; };
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 3/14 perf tools: Add data_fd into dso object
In reply to Jiri OlsaAdding data_fd into dso object so we could handle caching of opened dso file data descriptors coming int next patches.
Adding dso__data_close interface to keep the data_fd updated when the descriptor is closed.
--- tools/perf/util/dso.c | 23 +++++++++++++++++++---- tools/perf/util/dso.h | 3 +++ tools/perf/util/unwind-libunwind.c | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-)
tools/perf/util/dso.c +19 -4
@@ -159,6 +159,14 @@ static int open_dso(struct dso *dso, struct machine *machine)return fd; } +void dso__data_close (struct dso *dso) +{ +if (dso->data.fd >=0 ) { +close (dso->data.fd); + dso->data.fd =-1 ; + } +} +int dso__data_fd (struct dso *dso,struct machine *machine) {enum dso_binary_type binary_type_data[] = {
@@ -168,8 +176,13 @@ 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; + +if (dso->binary_type !=DSO_BINARY_TYPE__NOT_FOUND ) { + dso->data.fd =open_dso (dso, machine); +return dso->data.fd; + } do { int fd;
@@ -178,7 +191,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 +314,7 @@ dso_cache__read(struct dso *dso, struct machine *machine,if (ret <=0 )free (cache); -close (fd); +dso__data_close (dso);return ret; }
@@ -474,6 +487,7 @@ struct dso *dso__new(const char *name)for (i =0 ; i <MAP__NR_TYPES ; ++i) dso->symbols[i] = dso->symbol_names[i] =RB_ROOT ; dso->data.cache =RB_ROOT ; + dso->data.fd =-1 ; dso->symtab_type =DSO_BINARY_TYPE__NOT_FOUND ; dso->binary_type =DSO_BINARY_TYPE__NOT_FOUND ; dso->loaded =0 ;
@@ -506,6 +520,7 @@ void dso__delete(struct dso *dso) dso->long_name_allocated = false; } +dso__data_close (dso);dso_cache__free (&dso->data.cache);dso__free_a2l (dso);zfree (&dso->symsrc_filename);
tools/perf/util/dso.h +3 -0
@@ -102,6 +102,7 @@ struct dso {/* dso data file */ struct {struct rb_root cache; +int fd; } data;char name[0 ];
@@ -147,6 +148,8 @@ int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type t char *root_dir, char *filename, size_t size);int dso__data_fd (struct dso *dso,struct machine *machine); +void dso__data_close (struct dso *dso); +ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine,u64 offset,u8 *data,ssize_t size); ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
tools/perf/util/unwind-libunwind.c +2 -2
@@ -250,7 +250,7 @@ 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); +dso__data_close (dso);if (offset) ret = unwind_spec_ehframe(dso, machine, offset,
@@ -271,7 +271,7 @@ 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); +dso__data_close (dso);if (*offset)return 0 ;
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 4/14 perf tools: Add global list of opened dso objects
In reply to Jiri OlsaAdding global list of opened dso objects, so we can track them and use the list for caching dso data file descriptors.
--- tools/perf/util/dso.c | 41 +++++++++++++++++++++++++++++++++++++++-- tools/perf/util/dso.h | 1 + 2 files changed, 40 insertions(+), 2 deletions(-)
tools/perf/util/dso.c +39 -2
@@ -136,7 +136,22 @@ int dso__read_binary_type_filename(const struct dso *dso,return ret; } -static int open_dso (struct dso *dso,struct machine *machine) +/* +* Global list of open DSOs. +*/ +static LIST_HEAD (dso__data_open); + +static void dso__list_add (struct dso *dso) +{ +list_add_tail (&dso->data.open_entry, &dso__data_open); +} + +static void dso__list_del (struct dso *dso) +{ +list_del (&dso->data.open_entry); +} + +static int __open_dso (struct dso *dso,struct machine *machine) {int fd;char *root_dir = (char *)"" ;
@@ -159,14 +174,35 @@ static int open_dso(struct dso *dso, struct machine *machine)return fd; } -void dso__data_close (struct dso *dso) +static int open_dso (struct dso *dso,struct machine *machine) +{ +int fd =__open_dso (dso, machine); + +if (fd >0 ) +dso__list_add (dso); + +return fd; +} + +static void close_data_fd (struct dso *dso) {if (dso->data.fd >=0 ) {close (dso->data.fd); dso->data.fd =-1 ; +dso__list_del (dso); } } +static void close_dso (struct dso *dso) +{ +close_data_fd (dso); +} + +void dso__data_close (struct dso *dso) +{ +close_dso (dso); +} +int dso__data_fd (struct dso *dso,struct machine *machine) {enum dso_binary_type binary_type_data[] = {
@@ -499,6 +535,7 @@ struct dso *dso__new(const char *name) dso->kernel =DSO_TYPE_USER ; dso->needs_swap =DSO_SWAP__UNSET ;INIT_LIST_HEAD (&dso->node); +INIT_LIST_HEAD (&dso->data.open_entry); }return dso;
tools/perf/util/dso.h +1 -0
@@ -103,6 +103,7 @@ struct dso {struct {struct rb_root cache;int fd; +struct list_head open_entry; } data;char name[0 ];
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 5/14 perf tools: Add global count of opened dso objects
In reply to Jiri OlsaAdding global count of opened dso objects so we could properly limit the number of opened dso data file descriptors.
--- tools/perf/util/dso.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +7 -1
@@ -1,3 +1,4 @@ +#include <asm/bug.h> #include "symbol.h" #include "dso.h" #include "machine.h"
@@ -137,18 +138,23 @@ int dso__read_binary_type_filename(const struct dso *dso, }/* -* Global list of open DSOs. +* Global list of open DSOs and the counter. */ static LIST_HEAD (dso__data_open); +static long dso__data_open_cnt;static void dso__list_add (struct dso *dso) {list_add_tail (&dso->data.open_entry, &dso__data_open); + dso__data_open_cnt++; }static void dso__list_del (struct dso *dso) {list_del (&dso->data.open_entry); +WARN_ONCE (dso__data_open_cnt <=0 , +"DSO data fd counter out of bounds." ); + dso__data_open_cnt--; }static int __open_dso (struct dso *dso,struct machine *machine)
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 6/14 perf tools: Cache dso data file descriptor
In reply to Jiri OlsaChanging the dso__data_close to keep the dso data file descriptor open if possible.
We keep dsos data file descriptors open until their count reaches the half of the current fd open limit (RLIMIT_NOFILE). In this case we close file descriptor of the first opened dso object.
We've got speed up in dso__data_fd function.
Output from report over 10 GB data with DWARF unwind stacks:
(TODO fix perf diff)
current code:
0.16% perf perf [.] dso__data_fd
change:
no record of dso__data_fd
And we got some noticable overall speed up:
(perf perf stat -e '{cycles,instructions}:u' ...)
current code:
291,315,329,878 cycles ( +- 0.22% ) 391,763,485,304 instructions ( +- 0.03% )
180.742249687 seconds time elapsed ( +- 0.64% )
change:
228,948,088,958 cycles ( +- 0.20% ) 324,936,504,336 instructions ( +- 0.02% )
133.789677792 seconds time elapsed ( +- 0.76% )
--- tools/perf/util/dso.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-)
tools/perf/util/dso.c +59 -3
@@ -1,4 +1,6 @@#include <asm/bug.h> +#include <sys/time.h> +#include <sys/resource.h> #include "symbol.h" #include "dso.h" #include "machine.h"
@@ -204,11 +206,60 @@ static void close_dso(struct dso *dso)close_data_fd (dso); } -void dso__data_close (struct dso *dso) +static void close_first_dso (void ) { +struct dso *dso; + + dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);close_dso (dso); } +static rlim_t get_fd_limit (void ) +{ +struct rlimit l; +rlim_t limit =0 ; + +/* Allow half of the current open fd limit. */ +if (getrlimit (RLIMIT_NOFILE , &l) ==0 ) { +if (l.rlim_cur ==RLIM_INFINITY ) + limit = l.rlim_cur; +else + limit = l.rlim_cur /2 ; + }else { +pr_err ("failed to get fd limit\n" ); + limit =1 ; + } + +return limit; +} + +static bool may_cache_fd (void ) +{ +static rlim_t limit; + +if (!limit) + limit =get_fd_limit (); + +if (limit ==RLIM_INFINITY ) +return true; + +return limit > (rlim_t ) dso__data_open_cnt; +} + +static void data_close (void ) +{ +bool cache_fd =may_cache_fd (); + +if (!cache_fd) +close_first_dso (); +} + +void dso__data_close (struct dso *dso) +{ +if (dso->data.fd >=0 ) +data_close (); +} +int dso__data_fd (struct dso *dso,struct machine *machine) {enum dso_binary_type binary_type_data[] = {
@@ -318,6 +369,7 @@ static ssize_t dso_cache__read(struct dso *dso, struct machine *machine, u64 offset, u8 *data, ssize_t size) { + bool new_fd = dso->data.fd == -1;struct dso_cache *cache;ssize_t ret;int fd;
@@ -356,7 +408,10 @@ dso_cache__read(struct dso *dso, struct machine *machine,if (ret <=0 )free (cache); -dso__data_close (dso); +/* Check the cache only if we just added new fd. */ +if (new_fd) +data_close (); +return ret; }
@@ -563,7 +618,8 @@ void dso__delete(struct dso *dso) dso->long_name_allocated = false; } -dso__data_close (dso); +if (dso->data.fd !=-1 ) +close_dso (dso);dso_cache__free (&dso->data.cache);dso__free_a2l (dso);zfree (&dso->symsrc_filename);
Signature · 1 line
1.8.3.1
Re: [PATCH 06/14] perf tools: Cache dso data file descriptor
Hi Jiri,
On Thu, 15 May 2014 19:23:27 +0200, Jiri Olsa wrote:
[SNIP]
tools/perf/util/dso.c static void close_dso(struct dso *dso) · line 255
Quoted 7 lines
tools/perf/util/dso.c +7
-0+static void data_close (void ) +{ +bool cache_fd =may_cache_fd (); + +if (!cache_fd) +close_first_dso (); +}
Why do you do this at close()? As long as there's no attempt to open a new file, we can keep existing fd, no?
tools/perf/util/dso.c static void close_dso(struct dso *dso) · line 261
Quoted 6 lines
tools/perf/util/dso.c +6
-0+ +void dso__data_close (struct dso *dso) +{ +if (dso->data.fd >=0 ) +data_close (); +}
Hmm.. it's confusing dso__data_close(dso) closes an other dso rather than the given dso. And this dso__data_close() is not paired with any _open() also these close calls make me confusing which one to use. ;-p
Thanks
Namhyung
Re: [PATCH 06/14] perf tools: Cache dso data file descriptor
Quoted 5 lines · Namhyung Kim
Hi Jiri,
On Thu, 15 May 2014 19:23:27 +0200, Jiri Olsa wrote:
[SNIP]
Quoted 10 lines
tools/perf/util/dso.c static void close_dso(struct dso *dso) · line 255
Quoted 7 lines
tools/perf/util/dso.c +7
-0+static void data_close (void ) +{ +bool cache_fd =may_cache_fd (); + +if (!cache_fd) +close_first_dso (); +}Why do you do this at
close()? As long as there's no attempt to open a new file, we can keep existing fd, no?
so the way it works now is:
- we keep up to the 'RLIMIT_NOFILE / 2' of open dso objects
- if we try to open dso and it fails, because we are out of file descriptors, we close dso objects and try to reopen (check do_open function)
- when we close the dso object we check if number of opened dso objects is below 'RLIMIT_NOFILE / 2'.. if it is, we keep the dso opened, if not we close first dso in the list
util/dso.h tries to describe that
Quoted 11 lines
tools/perf/util/dso.c static void close_dso(struct dso *dso) · line 261
Quoted 7 lines
tools/perf/util/dso.c +6
-0+ +void dso__data_close (struct dso *dso) +{ +if (dso->data.fd >=0 ) +data_close (); +}Hmm.. it's confusing
dso__data_close(dso)closes an other dso rather than the given dso. And thisdso__data_close()is not paired with any_open()also these close calls make me confusing which one to use. ;-p
thats due to the caching.. as explained above
About the pairing.. originally the interface was only dso__data_fd that opened and returned fd, which the caller needed to close.
I added dso__data_close so we could keep track of file descriptors.
I could add dso__data_open I guess, but it is dso__data_fd which is needed for elf interface anyway.
thanks,
jirka
Namhyung Kim
maintainerRe: [PATCH 06/14] perf tools: Cache dso data file descriptor
Quoted 27 lines · Jiri Olsa
Quoted 5 lines · Namhyung Kim
Hi Jiri,
On Thu, 15 May 2014 19:23:27 +0200, Jiri Olsa wrote:
[SNIP]
Quoted 10 lines
tools/perf/util/dso.c static void close_dso(struct dso *dso) · line 255
Quoted 7 lines
tools/perf/util/dso.c +7
-0+static void data_close (void ) +{ +bool cache_fd =may_cache_fd (); + +if (!cache_fd) +close_first_dso (); +}Why do you do this at
close()? As long as there's no attempt to open a new file, we can keep existing fd, no?so the way it works now is:
- we keep up to the '
RLIMIT_NOFILE/ 2' of open dso objects
- if we try to open dso and it fails, because we are out of file descriptors, we close dso objects and try to reopen (checkdo_openfunction)
- when we close the dso object we check if number of opened dso objects is below 'RLIMIT_NOFILE/ 2'.. if it is, we keep the dso opened, if not we close first dso in the list
Quoted 1 line
util/dso.htries to describe that
Yes, I know. But my question is why do this at close()? Isn't it sufficient to check the file limit at open() and close previous one if necessary?
Quoted 15 lines
Quoted 12 lines
tools/perf/util/dso.c static void close_dso(struct dso *dso) · line 261
Quoted 8 lines
tools/perf/util/dso.c +6
-0+ +void dso__data_close (struct dso *dso) +{ +if (dso->data.fd >=0 ) +data_close (); +}Hmm.. it's confusing
dso__data_close(dso)closes an other dso rather than the given dso. And thisdso__data_close()is not paired with any_open()also these close calls make me confusing which one to use. ;-pthats due to the caching.. as explained above
Quoted 7 lines
About the pairing.. originally the interface was only
dso__data_fdthat opened and returned fd, which the caller needed to close.I added
dso__data_closeso we could keep track of file descriptors.I could add
dso__data_openI guess, but it isdso__data_fdwhich is needed for elf interface anyway.
I'd rather suggest dropping the open/close idiom for this case since it's confusing. What about get/put or get_fd/put_fd?
Thanks,
Namhyung
Re: [PATCH 06/14] perf tools: Cache dso data file descriptor
Quoted 28 lines · Namhyung Kim
Quoted 27 lines · Jiri Olsa
Quoted 5 lines · Namhyung Kim
Hi Jiri,
On Thu, 15 May 2014 19:23:27 +0200, Jiri Olsa wrote:
[SNIP]
Quoted 10 lines
tools/perf/util/dso.c static void close_dso(struct dso *dso) · line 255
Quoted 7 lines
tools/perf/util/dso.c +7
-0+static void data_close (void ) +{ +bool cache_fd =may_cache_fd (); + +if (!cache_fd) +close_first_dso (); +}Why do you do this at
close()? As long as there's no attempt to open a new file, we can keep existing fd, no?so the way it works now is:
- we keep up to the '
RLIMIT_NOFILE/ 2' of open dso objects
- if we try to open dso and it fails, because we are out of file descriptors, we close dso objects and try to reopen (checkdo_openfunction)
- when we close the dso object we check if number of opened dso objects is below 'RLIMIT_NOFILE/ 2'.. if it is, we keep the dso opened, if not we close first dso in the list
Quoted 5 lines
Quoted 1 line
util/dso.htries to describe thatYes, I know. But my question is why do this at
close()? Isn't it sufficient to check the file limit atopen()and close previous one if necessary?
hm, it's still the same operation to be done either in open or in close.. but we would not need dso__data_close then.. ok, I'll make the change ;-)
thanks,
jirka
Jiri Olsa
reviewerPATCH v2 7/14 perf tools: Add file size check and factor dso__data_read_offset
In reply to Jiri OlsaAdding file size check, because the lseek will succeed for any offset behind file size. Factoring the code the check the offset earli in the flow.
--- tools/perf/util/dso.c | 78 ++++++++++++++++++++++++++++++++++++++------------- tools/perf/util/dso.h | 1 + 2 files changed, 59 insertions(+), 20 deletions(-)
tools/perf/util/dso.c +58 -20
@@ -197,6 +197,7 @@ static void close_data_fd(struct dso *dso)if (dso->data.fd >=0 ) {close (dso->data.fd); dso->data.fd =-1 ; + dso->data.file_size =0 ;dso__list_del (dso); } }
@@ -366,17 +367,10 @@ dso_cache__memcpy(struct dso_cache *cache, u64 offset, }static ssize_t -dso_cache__read (struct dso *dso,struct machine *machine, -u64 offset,u8 *data,ssize_t size) +dso_cache__read (struct dso *dso,u64 offset,u8 *data,ssize_t size) { -bool new_fd = dso->data.fd ==-1 ;struct dso_cache *cache;ssize_t ret; -int fd; - - fd =dso__data_fd (dso, machine); -if (fd <0 ) -return -1 ; do { u64 cache_offset;
@@ -390,10 +384,10 @@ dso_cache__read(struct dso *dso, struct machine *machine, cache_offset = offset &DSO__DATA_CACHE_MASK ; ret = -EINVAL ; -if (-1 ==lseek (fd, cache_offset,SEEK_SET )) +if (-1 ==lseek (dso->data.fd, cache_offset,SEEK_SET ))break ; - ret =read (fd, cache->data,DSO__DATA_CACHE_SIZE ); + ret =read (dso->data.fd, cache->data,DSO__DATA_CACHE_SIZE );if (ret <=0 )break ;
@@ -408,15 +402,11 @@ dso_cache__read(struct dso *dso, struct machine *machine,if (ret <=0 )free (cache); -/* Check the cache only if we just added new fd. */ -if (new_fd) -data_close (); -return ret; } -static ssize_t dso_cache_read (struct dso *dso,struct machine *machine, -u64 offset,u8 *data,ssize_t size) +static ssize_t dso_cache_read (struct dso *dso,u64 offset, +u8 *data,ssize_t size) {struct dso_cache *cache;
@@ -424,11 +414,10 @@ static ssize_t dso_cache_read(struct dso *dso, struct machine *machine,if (cache)return dso_cache__memcpy (cache, offset, data, size);else -return dso_cache__read (dso, machine, offset, data, size); +return dso_cache__read (dso, offset, data, size); } -ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine, -u64 offset,u8 *data,ssize_t size) +static ssize_t cached_read (struct dso *dso,u64 offset,u8 *data,ssize_t size) {ssize_t r =0 ;u8 *p = data;
@@ -436,7 +425,7 @@ ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, do { ssize_t ret; - ret = dso_cache_read(dso, machine, offset, p, size); + ret = dso_cache_read(dso, offset, p, size); if (ret < 0) return ret;
@@ -456,6 +445,55 @@ ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,return r; } +static int data_file_size (struct dso *dso) +{ +struct stat st; + +if (!dso->data.file_size) { +if (fstat (dso->data.fd, &st)) { +pr_err ("dso mmap failed, fstat: %s\n" ,strerror (errno)); +return -1 ; + } + dso->data.file_size = st.st_size; + } + +return 0 ; +} + +static ssize_t data_read_offset (struct dso *dso,u64 offset, +u8 *data,ssize_t size) +{ +if (data_file_size (dso)) +return -1 ; + +/* Check the offset sanity. */ +if (offset > dso->data.file_size) +return -1 ; + +if (offset + size < offset) +return -1 ; + +return cached_read (dso, offset, data, size); +} + +ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine, +u64 offset,u8 *data,ssize_t size) +{ +bool new_fd = dso->data.fd ==-1 ; +ssize_t ret; + +if (dso__data_fd (dso, machine) <0 ) +return -1 ; + + ret =data_read_offset (dso, offset, data, size); + +/* Check the cache only if we just added new fd. */ +if (new_fd) +data_close (); + +return ret; +} +ssize_t dso__data_read_addr (struct dso *dso,struct map *map,struct machine *machine,u64 addr,u8 *data,ssize_t size)
tools/perf/util/dso.h +1 -0
@@ -103,6 +103,7 @@ struct dso {struct {struct rb_root cache;int fd; +size_t file_size;struct list_head open_entry; } data;
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 8/14 perf tools: Allow to close dso fd in case of open failure
In reply to Jiri OlsaAdding do_open function that tries to close opened dso objects in case we fail to open the dso due to to crossing the allowed RLIMIT_NOFILE limit.
--- tools/perf/util/dso.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +22 -1
@@ -159,6 +159,27 @@ static void dso__list_del(struct dso *dso) dso__data_open_cnt--; } +static void close_first_dso (void ); + +static int do_open (char *name) +{ +int fd; + +do { + fd =open (name,O_RDONLY ); +if (fd >=0 ) +return fd; + +pr_debug ("dso open failed, mmap: %s\n" ,strerror (errno)); +if (!dso__data_open_cnt || errno !=EMFILE ) +break ; + +close_first_dso (); + }while (1 ); + +return -1 ; +} +static int __open_dso (struct dso *dso,struct machine *machine) {int fd;
@@ -177,7 +198,7 @@ static int __open_dso(struct dso *dso, struct machine *machine)return -EINVAL ; } - fd =open (name,O_RDONLY ); + fd =do_open (name);free (name);return fd; }
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 9/14 perf tools: Add dso__data_* interface descriptons
In reply to Jiri OlsaAdding descriptions/explanations for dso__data_* interface functions.
--- tools/perf/util/dso.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/util/dso.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+)
tools/perf/util/dso.c +46 -0
@@ -268,6 +268,11 @@ static bool may_cache_fd(void)return limit > (rlim_t ) dso__data_open_cnt; } +/* +* Check and close LRU dso if we crossed allowed limit +* for opened dso file descriptors. The limit is half +* of the RLIMIT_NOFILE files opened. +*/ static void data_close (void ) {bool cache_fd =may_cache_fd ();
@@ -276,12 +281,27 @@ static void data_close(void)close_first_dso (); } +/** +* dso__data_close - close data file +* @dso: dso object +* +* Calls data_close (in case current dso is open) which takes care +* about caching dso objects file descriptors. +*/ void dso__data_close (struct dso *dso) {if (dso->data.fd >=0 )data_close (); } +/** +* dso__data_fd - Get dso's data file descriptor +* @dso: dso object +* @machine: machine object +* +* Find dso's file, open it and returns file descriptor, +* which needs to be closed later by dso__data_close. +*/ int dso__data_fd (struct dso *dso,struct machine *machine) {enum dso_binary_type binary_type_data[] = {
@@ -438,6 +458,11 @@ static ssize_t dso_cache_read(struct dso *dso, u64 offset,return dso_cache__read (dso, offset, data, size); } +/* +* Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks +* in the rb_tree. Any read to already cached data is served +* by cached data. +*/ static ssize_t cached_read (struct dso *dso,u64 offset,u8 *data,ssize_t size) {ssize_t r =0 ;
@@ -497,6 +522,17 @@ static ssize_t data_read_offset(struct dso *dso, u64 offset,return cached_read (dso, offset, data, size); } +/** +* dso__data_read_offset - Read data from dso file offset +* @dso: dso object +* @machine: machine object +* @offset: file offset +* @data: buffer to store data +* @size: size of the @data buffer +* +* Read data from dso file offset. Opens dso data file +* and use cached_read to get the data. +*/ ssize_t dso__data_read_offset (struct dso *dso,struct machine *machine,u64 offset,u8 *data,ssize_t size) {
@@ -515,6 +551,16 @@ ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,return ret; } +/** +* dso__data_read_addr - Read data from dso address +* @dso: dso object +* @machine: machine object +* @offset: file offset +* @data: buffer to store data +* @size: size of the @data buffer +* +* Read data from dso address. +*/ ssize_t dso__data_read_addr (struct dso *dso,struct map *map,struct machine *machine,u64 addr,u8 *data,ssize_t size)
tools/perf/util/dso.h +41 -0
47 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [PATCH 09/14] perf tools: Add dso__data_* interface descriptons
On Thu, 15 May 2014 19:23:30 +0200, Jiri Olsa wrote:
[SNIP]
Quoted 5 lines
+/**
+ *dso__data_read_addr- Read data from dso address
+ * @dso: dso object
+ * @machine: machine object
+ * @offset: file offset
s/offset/addr/
Thanks
Namhyung
Quoted 8 lines
+ * @data: buffer to store data
+ * @size: size of the @data buffer
+ *
+ * Read data from dso address.
+ */
ssize_tdso__data_read_addr(struct dso *dso, struct map *map,tools/perf/util/dso.c +0
-0struct machine *machine, u64 addr, u8 *data, ssize_t size)
Re: [PATCH 09/14] perf tools: Add dso__data_* interface descriptons
Quoted 10 lines · Namhyung Kim
On Thu, 15 May 2014 19:23:30 +0200, Jiri Olsa wrote:
[SNIP]
Quoted 5 lines
+/**
+ *dso__data_read_addr- Read data from dso address
+ * @dso: dso object
+ * @machine: machine object
+ * @offset: file offsets/offset/addr/
oops.. copy/paste stuff ;-\
thanks,
jirka
Jiri Olsa
reviewerPATCH v2 10/14 perf tests: Spawn child for each test
In reply to Jiri OlsaIn upcoming tests we will setup process limits, which might affect other tests. Spawning child for each test to prevent this.
--- tools/perf/tests/builtin-test.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-)
tools/perf/tests/builtin-test.c +31 -1
@@ -3,6 +3,8 @@* * Builtin regression testing command: ever growing number of sanity tests */ +#include <unistd.h> +#include <string.h> #include "builtin.h" #include "intlist.h" #include "tests.h"
@@ -164,6 +166,34 @@ static bool perf_test__matches(int curr, int argc, const char *argv[])return false; } +static int run_test (struct test *test) +{ +int status, err =-1 , child =fork (); + +if (child <0 ) { +pr_err ("failed to fork test: %s\n" ,strerror (errno)); +return -1 ; + } + +if (!child) { +pr_debug ("test child forked, pid %d\n" ,getpid ()); + err = test->func (); +exit (err); + } + +wait (&status); + +if (WIFEXITED (status)) { + err =WEXITSTATUS (status); +pr_debug ("test child finished with %d\n" , err); + }else if (WIFSIGNALED (status)) { + err =-1 ; +pr_debug ("test child interrupted\n" ); + } + +return err; +} +static int __cmd_test (int argc,const char *argv[],struct intlist *skiplist) {int i =0 ;
@@ -192,7 +222,7 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) }pr_debug ("\n--- start ---\n" ); - err = tests[curr].func (); + err =run_test (&tests[curr]);pr_debug ("---- end ----\n%s:" , tests[curr].desc);switch (err) {
Signature · 1 line
1.8.3.1
Re: [PATCH 10/14] perf tests: Spawn child for each test
Quoted 3 lines · Jiri Olsa
In upcoming tests we will setup process limits, which might affect other tests. Spawning child for each test to prevent this.
But you can restore original limits after the test using soft limits? But I think it's better to run the tests in a child anyway. :)
Thanks,
Namhyung
Re: [PATCH 10/14] perf tests: Spawn child for each test
Quoted 7 lines · Namhyung Kim
Quoted 3 lines · Jiri Olsa
In upcoming tests we will setup process limits, which might affect other tests. Spawning child for each test to prevent this.
But you can restore original limits after the test using soft limits? But I think it's better to run the tests in a child anyway. :)
right.. I also recalled that Ingo recommended this before, I'll queue this one separately..
thanks,
jirka
Quoted 3 lines
Thanks,
Namhyung
Jiri Olsa
reviewerPATCH v2 11/14 perf tests: Allow reuse of test_file function
In reply to Jiri OlsaMaking the test_file function to be reusable for new tests coming in following patches.
--- tools/perf/tests/dso-data.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/perf/tests/dso-data.c +5 -1
@@ -12,11 +12,15 @@static char *test_file (int size) { -static char buf_templ[] ="/tmp/test-XXXXXX" ; +#define TEMPL "/tmp/test-XXXXXX" +static char buf_templ[sizeof (TEMPL )];char *templ = buf_templ;int fd, i;unsigned char *buf; +strcpy (buf_templ,TEMPL ); +#undef TEMPL + fd =mkstemp (templ);if (fd <0 ) {perror ("mkstemp failed" );
Signature · 1 line
1.8.3.1
Jiri Olsa
reviewerPATCH v2 12/14 perf tests: Add test interface for dso data fd limit
In reply to Jiri OlsaAdding a way to setup test dso limit by global variable test_dso_data__fd_limit. It'll be used in the dso data cache tests.
--- tools/perf/util/dso.c | 5 +++++ tools/perf/util/dso.h | 3 +++ 2 files changed, 8 insertions(+)
tools/perf/util/dso.c +5 -0
@@ -7,6 +7,8 @@#include "util.h" #include "debug.h" +rlim_t test_dso_data__fd_limit; +char dso__symtab_origin (const struct dso *dso) {static const char origin[] = {
@@ -262,6 +264,9 @@ static bool may_cache_fd(void)if (!limit) limit =get_fd_limit (); +if (unlikely (test_dso_data__fd_limit)) + limit = test_dso_data__fd_limit; +if (limit ==RLIM_INFINITY )return true;
tools/perf/util/dso.h +3 -0
@@ -1,6 +1,7 @@#ifndef __PERF_DSO#define __PERF_DSO +#include <sys/resource.h> #include <linux/types.h> #include <linux/rbtree.h> #include <stdbool.h>
@@ -8,6 +9,8 @@#include "map.h" #include "build-id.h" +extern rlim_t test_dso_data__fd_limit; +enum dso_binary_type {DSO_BINARY_TYPE__KALLSYMS =0 ,DSO_BINARY_TYPE__GUEST_KALLSYMS ,
Signature · 1 line
1.8.3.1
Re: [PATCH 12/14] perf tests: Add test interface for dso data fd limit
Quoted 3 lines · Jiri Olsa
Adding a way to setup test dso limit by global variable
test_dso_data__fd_limit. It'll be used in the dso data cache tests.
Why is this needed? Why not justing setting RLIMIT_NOFILE in the test cases?
Thanks,
Namhyung
Quoted 60 lines
Cc Arnaldo Carvalho de Melo <[email protected]>Cc Corey Ashford <[email protected]>Cc David Ahern <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jean Pihet <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- tools/perf/util/dso.c | 5 +++++ tools/perf/util/dso.h | 3 +++ 2 files changed, 8 insertions(+)tools/perf/util/dso.c +5
-0@@ -7,6 +7,8 @@#include "util.h" #include "debug.h" +rlim_t test_dso_data__fd_limit; +char dso__symtab_origin (const struct dso *dso) {static const char origin[] = {@@ -262,6 +264,9 @@ static bool may_cache_fd(void)if (!limit) limit =get_fd_limit (); +if (unlikely (test_dso_data__fd_limit)) + limit = test_dso_data__fd_limit; +if (limit ==RLIM_INFINITY )return true;tools/perf/util/dso.h +3
-0@@ -1,6 +1,7 @@#ifndef __PERF_DSO#define __PERF_DSO +#include <sys/resource.h> #include <linux/types.h> #include <linux/rbtree.h> #include <stdbool.h> @@ -8,6 +9,8 @@#include "map.h" #include "build-id.h" +extern rlim_t test_dso_data__fd_limit; +enum dso_binary_type {DSO_BINARY_TYPE__KALLSYMS =0 ,DSO_BINARY_TYPE__GUEST_KALLSYMS ,
Re: [PATCH 12/14] perf tests: Add test interface for dso data fd limit
Quoted 7 lines · Namhyung Kim
Quoted 3 lines · Jiri Olsa
Adding a way to setup test dso limit by global variable
test_dso_data__fd_limit. It'll be used in the dso data cache tests.Why is this needed? Why not justing setting
RLIMIT_NOFILEin the test cases?
well, I wanted to avoid to customizing the test to the current open files count state.. the way with artifical limit is easier and more clean IMO ;-)
I'll see if I can make the setup without this one
thanks,
jirka
Jiri Olsa
reviewerPATCH v2 13/14 perf tests: Add test for caching dso file descriptors
In reply to Jiri OlsaAdding test that setup test_dso_data__fd_limit and test dso data file descriptors are cached appropriately.
--- tools/perf/tests/builtin-test.c | 6 +- tools/perf/tests/dso-data.c | 180 +++++++++++++++++++++++++++++++++++++++- tools/perf/tests/tests.h | 1 + 3 files changed, 183 insertions(+), 4 deletions(-)
tools/perf/tests/builtin-test.c +5 -1
@@ -52,10 +52,14 @@ static struct test { .func = test__pmu, }, { - .desc = "Test dso data interface", + .desc = "Test dso data read", .func = test__dso_data, }, { + .desc = "Test dso data cache", + .func = test__dso_data_cache, + }, + { .desc = "roundtrip evsel->name check", .func = test__perf_evsel__roundtrip_name_test, },
tools/perf/tests/dso-data.c +177 -3
191 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -28,6 +28,7 @@ int test__syscall_open_tp_fields(void);int test__pmu (void );int test__attr (void );int test__dso_data (void ); +int test__dso_data_cache (void );int test__parse_events (void );int test__hists_link (void );int test__python_use (void );
Signature · 1 line
1.8.3.1
Re: [PATCH 13/14] perf tests: Add test for caching dso file descriptors
Quoted 2 lines · Jiri Olsa
Adding test that setup
test_dso_data__fd_limitand test dso data file descriptors are cached appropriately.
[SNIP]
tools/perf/tests/dso-data.c int test__dso_data(void) · line 168
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+static long open_files_cnt (void ) +{ +char path[PATH_MAX ]; +struct dirent *dent; +DIR *dir; +long nr =0 ; +int n; + + n =scnprintf (path,PATH_MAX ,"%s/self/fd" ,procfs__mountpoint ()); +TEST_ASSERT_VAL ("couldn't get fd path" , n <PATH_MAX );
Looks like an unnecessary check since the scnprintf() cannot return more than (or equal to) PATH_MAX.
Quoted 17 lines
tools/perf/tests/dso-data.c +17
-0+ +pr_debug ("fd path: %s\n" , path); + + dir =opendir (path); +TEST_ASSERT_VAL ("failed to open fd directory" , dir); + +while ((dent =readdir (dir)) !=NULL ) { +if (!strcmp (dent->d_name,"." ) || + !strcmp (dent->d_name,".." )) +continue ; + + nr++; + } + +closedir (dir); +return nr -1 ; +}
[SNIP]
Quoted 17 lines
tools/perf/tests/dso-data.c +17
-0+static int set_fd_limit (int n) +{ +struct rlimit rlim; + +if (getrlimit (RLIMIT_NOFILE , &rlim)) +return -1 ; + +pr_debug ("file limit %ld, new %d\n" , (long ) rlim.rlim_cur, n); + + rlim.rlim_cur = n; +return setrlimit (RLIMIT_NOFILE , &rlim); +} + +int test__dso_data_cache (void ) +{ +struct machine machine; +long nr =open_files_cnt ();
tools/perf/tests/dso-data.c int test__dso_data(void) · line 246
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+#define BUFSIZE 10 +u8 buf[BUFSIZE ]; +ssize_t n; +int fd; + +memset (&machine,0 ,sizeof (machine)); + +/* Make sure we are able to open 3 fds anyway */ +TEST_ASSERT_VAL ("failed to set file limit" , + !set_fd_limit ((nr +6 )));
3 or 6?
tools/perf/tests/dso-data.c int test__dso_data(void) · line 256
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+ +/* +* Test scenario: +* - create 3 dso objects +* - set the limit of opened data file descriptors to 2 +* - open/close dsos data fds and check for proper +* handling of the dso data cache +*/ + + test_dso_data__fd_limit =3 ;
2 or 3?
tools/perf/tests/dso-data.c int test__dso_data(void) · line 268
Quoted 12 lines
tools/perf/tests/dso-data.c +12
-0+ +TEST_ASSERT_VAL ("failed to create dsos\n" , !dsos__create (TEST_FILE_SIZE )); + +/* open dso_0 */ + fd =dso__data_fd (dso_0, &machine); +TEST_ASSERT_VAL ("failed to get fd" , fd >0 ); + + n =dso__data_read_offset (dso_0, &machine,0 , buf,BUFSIZE ); +TEST_ASSERT_VAL ("failed to read dso" , n ==BUFSIZE ); + +/* +* Close dso_0 data with cache = true,
What does it mean by 'cache = true'?
Quoted 13 lines
tools/perf/tests/dso-data.c +13
-0+* dso_0 should remain open. +*/ +dso__data_close (dso_0); +TEST_ASSERT_VAL ("failed to not close dso" , dso_0->data.fd !=-1 ); + +/* open dso_1 */ + n =dso__data_read_offset (dso_1, &machine,0 , buf,BUFSIZE ); +TEST_ASSERT_VAL ("failed to read dso" , n ==BUFSIZE ); + +/* +* Close dso_1 data with cache = true, +* dso_0 and dso_1 should remain open. +*/
tools/perf/tests/dso-data.c int test__dso_data(void) · line 291
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+dso__data_close (dso_1); +TEST_ASSERT_VAL ("failed to not close dso" , dso_0->data.fd !=-1 ); +TEST_ASSERT_VAL ("failed to not close dso" , dso_1->data.fd !=-1 ); + +/* open dso_2 */ + fd =dso__data_fd (dso_2, &machine); +TEST_ASSERT_VAL ("failed to get fd" , fd >0 ); + +/* +* Close dso_1 data with cache = true,
You meant dso_2 right? :)
Thanks,
Namhyung
Quoted 51 lines
tools/perf/tests/dso-data.c +39
-0+* dso_0 should get closed now +*/ +dso__data_close (dso_2); +TEST_ASSERT_VAL ("failed to close dso_0" , dso_0->data.fd ==-1 ); + +/* reopen dso_0 */ + fd =dso__data_fd (dso_0, &machine); +TEST_ASSERT_VAL ("failed to get fd" , fd >0 ); + +/* +* Close dso_0 data with cache = true, +* dso_1 should get closed now. +*/ +dso__data_close (dso_0); +TEST_ASSERT_VAL ("failed to close dso_1" , dso_1->data.fd ==-1 ); + +/* reopen dso_1 */ + n =dso__data_read_offset (dso_1, &machine,0 , buf,BUFSIZE ); +TEST_ASSERT_VAL ("failed to read dso" , n ==BUFSIZE ); + +/* +* Close dso_1 data with cache = true, +* dso_2 should get closed now. +*/ +dso__data_close (dso_1); +TEST_ASSERT_VAL ("failed to close dso_2" , dso_2->data.fd ==-1 ); + +/* dso_0 remains open */ +TEST_ASSERT_VAL ("failed to keep open dso_0" , dso_0->data.fd >=0 ); + +/* cleanup everything */ +dsos__delete (); + +pr_debug ("nr start %ld, nr stop %ld\n" , nr,open_files_cnt ()); + +/* Make sure we did not leak any file descriptor. */ +TEST_ASSERT_VAL ("failed leadking files" , nr ==open_files_cnt ()); +return 0 ; +}tools/perf/tests/tests.h +1
-0@@ -28,6 +28,7 @@ int test__syscall_open_tp_fields(void);int test__pmu (void );int test__attr (void );int test__dso_data (void ); +int test__dso_data_cache (void );int test__parse_events (void );int test__hists_link (void );int test__python_use (void );
Re: [PATCH 13/14] perf tests: Add test for caching dso file descriptors
Quoted 5 lines · Namhyung Kim
Quoted 2 lines · Jiri Olsa
Adding test that setup
test_dso_data__fd_limitand test dso data file descriptors are cached appropriately.[SNIP]
Quoted 13 lines
tools/perf/tests/dso-data.c int test__dso_data(void) · line 168
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+static long open_files_cnt (void ) +{ +char path[PATH_MAX ]; +struct dirent *dent; +DIR *dir; +long nr =0 ; +int n; + + n =scnprintf (path,PATH_MAX ,"%s/self/fd" ,procfs__mountpoint ()); +TEST_ASSERT_VAL ("couldn't get fd path" , n <PATH_MAX );Looks like an unnecessary check since the
scnprintf()cannot return more than (or equal to)PATH_MAX.
once it's equal it's bad.. as the man says:
"return value of size or more means that the output was truncated"
I'll fix the rest you found
thanks,
jirka
Namhyung Kim
maintainerRe: [PATCH 13/14] perf tests: Add test for caching dso file descriptors
Quoted 20 lines · Jiri Olsa
Quoted 5 lines · Namhyung Kim
Quoted 2 lines · Jiri Olsa
Adding test that setup
test_dso_data__fd_limitand test dso data file descriptors are cached appropriately.[SNIP]
Quoted 13 lines
tools/perf/tests/dso-data.c int test__dso_data(void) · line 168
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+static long open_files_cnt (void ) +{ +char path[PATH_MAX ]; +struct dirent *dent; +DIR *dir; +long nr =0 ; +int n; + + n =scnprintf (path,PATH_MAX ,"%s/self/fd" ,procfs__mountpoint ()); +TEST_ASSERT_VAL ("couldn't get fd path" , n <PATH_MAX );Looks like an unnecessary check since the
scnprintf()cannot return more than (or equal to)PATH_MAX.
Quoted 2 lines
once it's equal it's bad.. as the man says:
"return value of size or more means that the output was truncated"
Did you see "sn"printf?
Thanks,
Namhyung
Re: [PATCH 13/14] perf tests: Add test for caching dso file descriptors
Quoted 21 lines · Namhyung Kim
Quoted 20 lines · Jiri Olsa
Quoted 18 lines · Namhyung Kim
Quoted 2 lines · Jiri Olsa
Adding test that setup
test_dso_data__fd_limitand test dso data file descriptors are cached appropriately.[SNIP]
tools/perf/tests/dso-data.c int test__dso_data(void) · line 168
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+static long open_files_cnt (void ) +{ +char path[PATH_MAX ]; +struct dirent *dent; +DIR *dir; +long nr =0 ; +int n; + + n =scnprintf (path,PATH_MAX ,"%s/self/fd" ,procfs__mountpoint ()); +TEST_ASSERT_VAL ("couldn't get fd path" , n <PATH_MAX );Looks like an unnecessary check since the
scnprintf()cannot return more than (or equal to)PATH_MAX.
Quoted 4 lines
Quoted 2 lines
once it's equal it's bad.. as the man says:
"return value of size or more means that the output was truncated"Did you see "sn"printf?
yes, I just double checked.. jirka
Namhyung Kim
maintainerRe: [PATCH 13/14] perf tests: Add test for caching dso file descriptors
Hi Jiri,
Quoted 22 lines · Jiri Olsa
Quoted 21 lines · Namhyung Kim
Quoted 20 lines · Jiri Olsa
Quoted 18 lines · Namhyung Kim
Quoted 2 lines · Jiri Olsa
Adding test that setup
test_dso_data__fd_limitand test dso data file descriptors are cached appropriately.[SNIP]
tools/perf/tests/dso-data.c int test__dso_data(void) · line 168
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+static long open_files_cnt (void ) +{ +char path[PATH_MAX ]; +struct dirent *dent; +DIR *dir; +long nr =0 ; +int n; + + n =scnprintf (path,PATH_MAX ,"%s/self/fd" ,procfs__mountpoint ()); +TEST_ASSERT_VAL ("couldn't get fd path" , n <PATH_MAX );Looks like an unnecessary check since the
scnprintf()cannot return more than (or equal to)PATH_MAX.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
once it's equal it's bad.. as the man says:
"return value of size or more means that the output was truncated"Did you see "sn"printf?
yes, I just double checked.. jirka
Please see "scn"printf then. :) It does something like below..
i = snprintf(buf, size, ...); return (i >= size) ? : size - 1 : i;
Thanks,
Namhyung
Re: [PATCH 13/14] perf tests: Add test for caching dso file descriptors
Quoted 28 lines · Namhyung Kim
Hi Jiri,
Quoted 25 lines · Jiri Olsa
Quoted 24 lines · Namhyung Kim
Quoted 22 lines · Jiri Olsa
Quoted 5 lines · Namhyung Kim
Quoted 2 lines · Jiri Olsa
Adding test that setup
test_dso_data__fd_limitand test dso data file descriptors are cached appropriately.[SNIP]
Quoted 13 lines
tools/perf/tests/dso-data.c int test__dso_data(void) · line 168
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+static long open_files_cnt (void ) +{ +char path[PATH_MAX ]; +struct dirent *dent; +DIR *dir; +long nr =0 ; +int n; + + n =scnprintf (path,PATH_MAX ,"%s/self/fd" ,procfs__mountpoint ()); +TEST_ASSERT_VAL ("couldn't get fd path" , n <PATH_MAX );Looks like an unnecessary check since the
scnprintf()cannot return more than (or equal to)PATH_MAX.once it's equal it's bad.. as the man says:
"return value of size or more means that the output was truncated"
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Did you see "sn"printf?
yes, I just double checked.. jirka
Please see "scn"printf then. :) It does something like below..
i = snprintf(buf, size, ...); return (i >= size) ? : size - 1 : i;
aaargh... ok ;-)
thanks,
jirka
Jiri Olsa
reviewerPATCH v2 14/14 perf tests: Add test for closing dso objects on EMFILE error
In reply to Jiri OlsaTesting that perf properly closes opened dso objects and tries to reopen in case we run out of allowed file descriptors for dso data.
--- tools/perf/tests/builtin-test.c | 4 +++ tools/perf/tests/dso-data.c | 70 +++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 3 files changed, 75 insertions(+)
tools/perf/tests/builtin-test.c +4 -0
@@ -60,6 +60,10 @@ static struct test { .func = test__dso_data_cache, }, { + .desc = "Test dso data reopen", + .func = test__dso_data_reopen, + }, + { .desc = "roundtrip evsel->name check", .func = test__perf_evsel__roundtrip_name_test, },
tools/perf/tests/dso-data.c +70 -0
73 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -29,6 +29,7 @@ int test__pmu(void);int test__attr (void );int test__dso_data (void );int test__dso_data_cache (void ); +int test__dso_data_reopen (void );int test__parse_events (void );int test__hists_link (void );int test__python_use (void );
Signature · 1 line
1.8.3.1
Re: [PATCH 14/14] perf tests: Add test for closing dso objects on EMFILE error
Quoted 40 lines · Jiri Olsa
Testing that perf properly closes opened dso objects and tries to reopen in case we run out of allowed file descriptors for dso data.
Cc Arnaldo Carvalho de Melo <[email protected]>Cc Corey Ashford <[email protected]>Cc David Ahern <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jean Pihet <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- tools/perf/tests/builtin-test.c | 4 +++ tools/perf/tests/dso-data.c | 70 +++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 3 files changed, 75 insertions(+)tools/perf/tests/builtin-test.c +4
-0@@ -60,6 +60,10 @@ static struct test { .func = test__dso_data_cache, }, { + .desc = "Test dso data reopen", + .func = test__dso_data_reopen, + }, + { .desc = "roundtrip evsel->name check", .func = test__perf_evsel__roundtrip_name_test, },tools/perf/tests/dso-data.c +0
-0
tools/perf/tests/dso-data.c int test__dso_data_cache(void) · line 336
Quoted 9 lines
tools/perf/tests/dso-data.c +6
-0@@ -328,3 +328,73 @@ int test__dso_data_cache(void)TEST_ASSERT_VAL ("failed leadking files" , nr ==open_files_cnt ());return 0 ; } + +int test__dso_data_reopen (void ) +{ +struct machine machine; +long nr =open_files_cnt (); +#define BUFSIZE 10
Looks like a copy-n-paste error.. :)
Quoted 43 lines
tools/perf/tests/dso-data.c +43
-0+int fd, fd_extra; + +memset (&machine,0 ,sizeof (machine)); + +/* +* Test scenario: +* - create 3 dso objects +* - set process file descriptor limit to current +* files count + 3 +* - test that the first dso gets closed when we +* reach the files count limit +*/ + +/* Make sure we are able to open 3 fds anyway */ +TEST_ASSERT_VAL ("failed to set file limit" , + !set_fd_limit ((nr +3 ))); + +TEST_ASSERT_VAL ("failed to create dsos\n" , !dsos__create (TEST_FILE_SIZE )); + +/* open dso_0 */ + fd =dso__data_fd (dso_0, &machine); +TEST_ASSERT_VAL ("failed to get fd" , fd >0 ); + +/* open dso_1 */ + fd =dso__data_fd (dso_1, &machine); +TEST_ASSERT_VAL ("failed to get fd" , fd >0 ); + +/* +* open extra file descriptor and we just +* reached the files count limit +*/ + fd_extra =open ("/dev/null" ,O_RDONLY ); +TEST_ASSERT_VAL ("failed to open extra fd" , fd_extra >0 ); + +/* open dso_2 */ + fd =dso__data_fd (dso_2, &machine); +TEST_ASSERT_VAL ("failed to get fd" , fd >0 ); + +/* +* dso_0 should get closed, because we reached +* the file descriptor limit +*/ +TEST_ASSERT_VAL ("failed to close dso_0" , dso_0->data.fd ==-1 );
tools/perf/tests/dso-data.c int test__dso_data_cache(void) · line 389
Quoted 10 lines
tools/perf/tests/dso-data.c +10
-0+ +/* open dso_0 */ + fd =dso__data_fd (dso_0, &machine); +TEST_ASSERT_VAL ("failed to get fd" , fd >0 ); + +/* +* dso_1 should get closed, because we reached +* the file descriptor limit +*/ +TEST_ASSERT_VAL ("failed to close dso_0" , dso_1->data.fd ==-1 );
s/dso_0/dso_1/
Btw, I don't see a big difference between this and previous testcase. Any chance to merge them into one?
Thanks,
Namhyung
Quoted 23 lines
tools/perf/tests/dso-data.c +11
-0+ +/* cleanup everything */ +close (fd_extra); +dsos__delete (); + +pr_debug ("nr start %ld, nr stop %ld\n" , nr,open_files_cnt ()); + +/* Make sure we did not leak any file descriptor. */ +TEST_ASSERT_VAL ("failed leadking files" , nr ==open_files_cnt ()); +return 0 ; +}tools/perf/tests/tests.h +1
-0@@ -29,6 +29,7 @@ int test__pmu(void);int test__attr (void );int test__dso_data (void );int test__dso_data_cache (void ); +int test__dso_data_reopen (void );int test__parse_events (void );int test__hists_link (void );int test__python_use (void );
Re: [PATCH 14/14] perf tests: Add test for closing dso objects on EMFILE error
Quoted 44 lines · Namhyung Kim
Quoted 43 lines · Jiri Olsa
Testing that perf properly closes opened dso objects and tries to reopen in case we run out of allowed file descriptors for dso data.
Cc Arnaldo Carvalho de Melo <[email protected]>Cc Corey Ashford <[email protected]>Cc David Ahern <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jean Pihet <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- tools/perf/tests/builtin-test.c | 4 +++ tools/perf/tests/dso-data.c | 70 +++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 3 files changed, 75 insertions(+)tools/perf/tests/builtin-test.c +4
-0@@ -60,6 +60,10 @@ static struct test { .func = test__dso_data_cache, }, { + .desc = "Test dso data reopen", + .func = test__dso_data_reopen, + }, + { .desc = "roundtrip evsel->name check", .func = test__perf_evsel__roundtrip_name_test, },tools/perf/tests/dso-data.c +0
-0@@ -328,3 +328,73 @@ int test__dso_data_cache(void)TEST_ASSERT_VAL ("failed leadking files" , nr ==open_files_cnt ());return 0 ; }
Quoted 8 lines
tools/perf/tests/dso-data.c int test__dso_data_cache(void) · line 336
Quoted 6 lines
tools/perf/tests/dso-data.c +6
-0+ +int test__dso_data_reopen (void ) +{ +struct machine machine; +long nr =open_files_cnt (); +#define BUFSIZE 10Looks like a copy-n-paste error.. :)
yea.. ;) I wonder why gcc did not scream about that
SNIP
Quoted 11 lines
tools/perf/tests/dso-data.c int test__dso_data_cache(void) · line 389
Quoted 6 lines
tools/perf/tests/dso-data.c +6
-0+ +/* +* dso_1 should get closed, because we reached +* the file descriptor limit +*/ +TEST_ASSERT_VAL ("failed to close dso_0" , dso_1->data.fd ==-1 );s/dso_0/dso_1/
Btw, I don't see a big difference between this and previous testcase. Any chance to merge them into one?
- the first one tests the caching or closing of file descriptors after dso__data_close is called
- the second one tests that we actually try to close dso objects in case we cannot open new dso
thanks,
jirka
ping ;-)
thanks,
jirka
Quoted 69 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 objectThe 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 ;-).
v2 changes:
- adding limit for open dso objects with sort of LRU mechanism to pick up and close dso objects if we are over the limit
- file mmaping changes are omitted, because I couldn't prove the improvement exactly, will resubmit later
- added dso close logic in case of no memory
- added testsOn 10GB perf data file with dwarf unwind data I've got around 30% speed up.
Also reachable in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_unwind_speedupthanks,
jirkaCc Arnaldo Carvalho de Melo <[email protected]>Cc Corey Ashford <[email protected]>Cc David Ahern <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jean Pihet <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
Jiri Olsa (14):
perf tools: Cache register accesses for unwind processing perf tools: Separate dso data related variables
perf tools: Adddata_fdinto dso object
perf tools: Add global list of opened dso objects perf tools: Add global count of opened dso objects perf tools: Cache dso data file descriptor
perf tools: Add file size check and factordso__data_read_offsetperf tools: Allow to close dso fd in case of open failure perf tools: Add dso__data_* interface descriptons perf tests: Spawn child for each test
perf tests: Allow reuse oftest_filefunction
perf tests: Add test interface for dso data fd limit perf tests: Add test for caching dso file descriptors perf tests: Add test for closing dso objects on EMFILE errortools/perf/tests/builtin-test.c | 42 ++++++++++++++- tools/perf/tests/dso-data.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- tools/perf/tests/tests.h | 2 + tools/perf/util/dso.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- tools/perf/util/dso.h | 56 +++++++++++++++++++- 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 | 4 +- 9 files changed, 617 insertions(+), 36 deletions(-)
Jean Pihet
Acked-byJiri,
Quoted 1 line · Jiri Olsa
ping ;-)
This looks a ping to yourself ;-)
Here are the results on ARMv7:
- libunwind: between -29% in execution time for light load (i.e. using not-so-deep backtraces from the stress app.) and -49% for deep backtrace (the 'stress_bt' app.),
- libdw: no significant improvement (0-2% improvement).
Cf. https://wiki.linaro.org/LEG/Engineering/TOOLS/perf-callstack-unwinding#Speed_improvement
for more details.
FWIW:
Regards,
Jean
Quoted 74 lines
thanks,
jirkaQuoted 69 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 objectThe 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 ;-).
v2 changes:
- adding limit for open dso objects with sort of LRU mechanism to pick up and close dso objects if we are over the limit
- file mmaping changes are omitted, because I couldn't prove the improvement exactly, will resubmit later
- added dso close logic in case of no memory
- added testsOn 10GB perf data file with dwarf unwind data I've got around 30% speed up.
Also reachable in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/core_unwind_speedupthanks,
jirkaCc Arnaldo Carvalho de Melo <[email protected]>Cc Corey Ashford <[email protected]>Cc David Ahern <[email protected]>Cc Frederic Weisbecker <[email protected]>Cc Ingo Molnar <[email protected]>Cc Jean Pihet <[email protected]>Cc Namhyung Kim <[email protected]>Cc Paul Mackerras <[email protected]>Cc Peter Zijlstra <[email protected]>Signed-off-by Jiri Olsa <[email protected]>---
Jiri Olsa (14):
perf tools: Cache register accesses for unwind processing perf tools: Separate dso data related variables
perf tools: Adddata_fdinto dso object
perf tools: Add global list of opened dso objects perf tools: Add global count of opened dso objects perf tools: Cache dso data file descriptor
perf tools: Add file size check and factordso__data_read_offsetperf tools: Allow to close dso fd in case of open failure perf tools: Add dso__data_* interface descriptons perf tests: Spawn child for each test
perf tests: Allow reuse oftest_filefunction
perf tests: Add test interface for dso data fd limit perf tests: Add test for caching dso file descriptors perf tests: Add test for closing dso objects on EMFILE errortools/perf/tests/builtin-test.c | 42 ++++++++++++++- tools/perf/tests/dso-data.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- tools/perf/tests/tests.h | 2 + tools/perf/util/dso.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- tools/perf/util/dso.h | 56 +++++++++++++++++++- 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 | 4 +- 9 files changed, 617 insertions(+), 36 deletions(-)
Quoted 6 lines · Jean Pihet
Jiri,
Quoted 1 line · Jiri Olsa
ping ;-)
This looks a ping to yourself ;-)
:-)
Quoted 6 lines
Here are the results on ARMv7:
- libunwind: between -29% in execution time for light load (i.e. using not-so-deep backtraces from the stress app.) and -49% for deep backtrace (the 'stress_bt' app.),
- libdw: no significant improvement (0-2% improvement).
nice, thanks for testing!
Quoted 6 lines
Cf. https://wiki.linaro.org/LEG/Engineering/TOOLS/perf-callstack-unwinding#Speed_improvement
for more details.FWIW:
Acked-by Jean Pihet <[email protected]>
thanks,
jirka