perf, tool: Fix endian issues
v5 cover perf, tool: Fix endian issues
hi,
sending fixies to properly handle perf.data endians.
This patchset contains 3 updated patches with comments from David.
I can see still some discrepancies when reporting data on target and origin system. It's not related to endianity but to kallsyms symbols processing and deserves another patchset.
There's another David's fix that needs to be taken as well:
http://marc.info/?l=linux-kernel&m=133770186330032&w=2
v5 changes:
- patch 1/3 s/DSO__READ/DSO__SWAP/
- updated all changelogs with perf report diff example
v4 changes:
- fixed patch 6/7 which was broken for -fstrict-aliasing related optimization
- added patch 3/7 and 7/7
v3 changes:
- added patch 5 to fix addons bitmask handling
v2 changes:
- added patches 3 and 4 to handle sample_id_all header endianity
Attached patches:
1/3 perf, tool: Handle different endians properly during symbol load 2/3 perf, tool: Handle endianity swap on sample_id_all header data 3/3 perf, tool: Fix 32 bit values endianity swap for sample_id_all header
Tested by running following usecases:
test 1)
- origin system:
# perf record -a -- sleep 10 (any perf record will do)
# perf report > report.origin
# perf archive perf.data - copy the perf.data, report.origin and perf.data.tar.bz2 to a target system and run:
# tar xjvf perf.data.tar.bz2 -C ~/.debug
# perf report > report.target
# diff -u report.origin report.target
- the diff should produce no output
(besides some white space stuff and possibly different
date/TZ output)
test 2)
- origin system:
# perf record -ag -fo /tmp/perf.data -- sleep 1
- mount origin system root to the target system on /mnt/origin
- target system:
# perf script --symfs /mnt/origin -I -i /mnt/origin/tmp/perf.data \
--kallsyms /mnt/origin/proc/kallsyms
- complete perf.data header is displayed test 3)
- tested with old perf.data version, worked proplerly
Tested by above usecase cross following architectures:
i386, x86_64, s390x, ppc64, ppc32
Big thank to Caspar Zhang who verified this within RH QE testsuites.
thanks,
jirka
--- tools/perf/util/evsel.c | 29 +++++++++++++++----- tools/perf/util/session.c | 67 +++++++++++++++++++++++++++++++++++++-------- tools/perf/util/symbol.c | 33 +++++++++++++++++++++- tools/perf/util/symbol.h | 30 ++++++++++++++++++++ 4 files changed, 139 insertions(+), 20 deletions(-)
PATCH v5 1/3 perf, tool: Handle different endians properly during symbol load
Currently we dont care about the file object's endianness. It's possible we read buildid file object from different architecture than we are currentlly running on. So we need to care about properly reading such object's data - handle different endianness properly.
Adding:
needs_swap DSO field
dso__swap_init function to initialize DSO's needs_swap DSO__SWAP to read the data with proper swaps
Together with other endianity patches, this change fixies perf report discrepancies on origin and target systems as described in test 1 below, e.g. following perf report diff:
...
0.12% ps [kernel.kallsyms] [k] clear_page
- 0.12% awk bash [.] alloc_word_desc
+ 0.12% awk bash [.] yyparse
0.11% beah-rhts-task libpython2.6.so.1.0 [.] 0x5560e
0.10% perf libc-2.12.so [.] __ctype_toupper_loc
- 0.09% rhts-test-runne bash [.] maybe_make_export_env
+ 0.09% rhts-test-runne bash [.] 0x385a0
0.09% ps [kernel.kallsyms] [k] page_fault...
Note, running following to test perf endianity handling:
test 1)
- origin system:
# perf record -a -- sleep 10 (any perf record will do)
# perf report > report.origin
# perf archive perf.data - copy the perf.data, report.origin and perf.data.tar.bz2 to a target system and run:
# tar xjvf perf.data.tar.bz2 -C ~/.debug
# perf report > report.target
# diff -u report.origin report.target
- the diff should produce no output
(besides some white space stuff and possibly different
date/TZ output)
test 1)
- origin system:
# perf record -ag -fo /tmp/perf.data -- sleep 1
- mount origin system root to the target system on /mnt/origin
- target system:
# perf script --symfs /mnt/origin -I -i /mnt/origin/tmp/perf.data \
--kallsyms /mnt/origin/proc/kallsyms
- complete perf.data header is displayed--- tools/perf/util/symbol.c | 33 ++++++++++++++++++++++++++++++++- tools/perf/util/symbol.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletions(-)
tools/perf/util/symbol.c +32 -1
@@ -323,6 +323,7 @@ struct dso *dso__new(const char *name) dso->sorted_by_name =0 ; dso->has_build_id =0 ; dso->kernel =DSO_TYPE_USER ; + dso->needs_swap =DSO_SWAP__UNSET ;INIT_LIST_HEAD (&dso->node); }
@@ -1156,6 +1157,33 @@ static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)return -1 ; } +static int dso__swap_init (struct dso *dso,unsigned char eidata) +{ +static unsigned int const endian =1 ; + + dso->needs_swap =DSO_SWAP__NO ; + +switch (eidata) { +case ELFDATA2LSB : +/* We are big endian, DSO is little endian. */ +if (*(unsigned char const *)&endian !=1 ) + dso->needs_swap =DSO_SWAP__YES ; +break ; + +case ELFDATA2MSB : +/* We are little endian, DSO is big endian. */ +if (*(unsigned char const *)&endian !=0 ) + dso->needs_swap =DSO_SWAP__YES ; +break ; + +default : +pr_err ("unrecognized DSO data encoding %d\n" , eidata); +return -EINVAL ; + } + +return 0 ; +} +static int dso__load_sym (struct dso *dso,struct map *map,const char *name,int fd,symbol_filter_t filter,int kmodule,int want_symtab)
@@ -1187,6 +1215,9 @@ static int dso__load_sym(struct dso *dso, struct map *map, const char *name, goto out_elf_end; } +if (dso__swap_init (dso, ehdr.e_ident[EI_DATA ])) + goto out_elf_end; +/* Always reject images with a mismatched build-id: */ if (dso->has_build_id) {u8 build_id[BUILD_ID_SIZE ];
@@ -1272,7 +1303,7 @@ static int dso__load_sym(struct dso *dso, struct map *map, const char *name,if (opdsec && sym.st_shndx == opdidx) {u32 offset = sym.st_value - opdshdr.sh_addr;u64 *opd = opddata->d_buf + offset; - sym.st_value = *opd; + sym.st_value =DSO__SWAP (dso, u64, *opd); sym.st_shndx =elf_addr_to_index (elf, sym.st_value); }
tools/perf/util/symbol.h +30 -0
@@ -9,6 +9,7 @@#include <linux/list.h> #include <linux/rbtree.h> #include <stdio.h> +#include <byteswap.h> #ifdef HAVE_CPLUS_DEMANGLE extern char *cplus_demangle (const char *,int );
@@ -160,11 +161,18 @@ enum dso_kernel_type {DSO_TYPE_GUEST_KERNEL }; +enum dso_swap_type { +DSO_SWAP__UNSET , +DSO_SWAP__NO , +DSO_SWAP__YES , +}; +struct dso {struct list_head node;struct rb_root symbols[MAP__NR_TYPES ];struct rb_root symbol_names[MAP__NR_TYPES ];enum dso_kernel_type kernel; +enum dso_swap_type needs_swap;u8 adjust_symbols:1 ;u8 has_build_id:1 ;u8 hit:1 ;
@@ -182,6 +190,28 @@ struct dso {char name[0 ]; }; +#define DSO__SWAP (dso, type, val) \ +({ \ + type ____r = val; \ + BUG_ON(dso->needs_swap == DSO_SWAP__UNSET); \ + if (dso->needs_swap == DSO_SWAP__YES) { \ + switch (sizeof(____r)) { \ + case 2: \ + ____r = bswap_16(val); \ + break; \ + case 4: \ + ____r = bswap_32(val); \ + break; \ + case 8: \ + ____r = bswap_64(val); \ + break; \ + default: \ + BUG_ON(1); \ + } \ + } \ + ____r; \ +}) +struct dso *dso__new (const char *name);void dso__delete (struct dso *dso);
Signature · 1 line
1.7.7.6
[tip:perf/urgent] perf symbols: Handle different endians properly during symbol load
Commit-ID: 8db4841fc72acc58254029f050226ea5f8103854 Gitweb: http://git.kernel.org/tip/8db4841fc72acc58254029f050226ea5f8103854 Author: Jiri Olsa <[email protected]> AuthorDate: Wed, 30 May 2012 14:23:42 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 31 May 2012 11:55:36 -0300
perf symbols: Handle different endians properly during symbol load
Currently we dont care about the file object's endianness. It's possible we read buildid file object from different architecture than we are currentlly running on. So we need to care about properly reading such object's data - handle different endianness properly.
Adding:
needs_swap DSO field
dso__swap_init function to initialize DSO's needs_swap DSO__SWAP to read the data with proper swaps
Together with other endianity patches, this change fixies perf report discrepancies on origin and target systems as described in test 1 below, e.g. following perf report diff:
...
0.12% ps [kernel.kallsyms] [k] clear_page
- 0.12% awk bash [.] alloc_word_desc
+ 0.12% awk bash [.] yyparse
0.11% beah-rhts-task libpython2.6.so.1.0 [.] 0x5560e
0.10% perf libc-2.12.so [.] __ctype_toupper_loc
- 0.09% rhts-test-runne bash [.] maybe_make_export_env
+ 0.09% rhts-test-runne bash [.] 0x385a0
0.09% ps [kernel.kallsyms] [k] page_fault...
Note, running following to test perf endianity handling:
test 1)
- origin system:
# perf record -a -- sleep 10 (any perf record will do)
# perf report > report.origin
# perf archive perf.data - copy the perf.data, report.origin and perf.data.tar.bz2 to a target system and run:
# tar xjvf perf.data.tar.bz2 -C ~/.debug
# perf report > report.target
# diff -u report.origin report.target
- the diff should produce no output
(besides some white space stuff and possibly different
date/TZ output)
test 1)
- origin system:
# perf record -ag -fo /tmp/perf.data -- sleep 1
- mount origin system root to the target system on /mnt/origin
- target system:
# perf script --symfs /mnt/origin -I -i /mnt/origin/tmp/perf.data \
--kallsyms /mnt/origin/proc/kallsyms
- complete perf.data header is displayed--- tools/perf/util/symbol.c | 33 ++++++++++++++++++++++++++++++++- tools/perf/util/symbol.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletions(-)
tools/perf/util/symbol.c +32 -1
@@ -323,6 +323,7 @@ struct dso *dso__new(const char *name) dso->sorted_by_name =0 ; dso->has_build_id =0 ; dso->kernel =DSO_TYPE_USER ; + dso->needs_swap =DSO_SWAP__UNSET ;INIT_LIST_HEAD (&dso->node); }
@@ -1156,6 +1157,33 @@ static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)return -1 ; } +static int dso__swap_init (struct dso *dso,unsigned char eidata) +{ +static unsigned int const endian =1 ; + + dso->needs_swap =DSO_SWAP__NO ; + +switch (eidata) { +case ELFDATA2LSB : +/* We are big endian, DSO is little endian. */ +if (*(unsigned char const *)&endian !=1 ) + dso->needs_swap =DSO_SWAP__YES ; +break ; + +case ELFDATA2MSB : +/* We are little endian, DSO is big endian. */ +if (*(unsigned char const *)&endian !=0 ) + dso->needs_swap =DSO_SWAP__YES ; +break ; + +default : +pr_err ("unrecognized DSO data encoding %d\n" , eidata); +return -EINVAL ; + } + +return 0 ; +} +static int dso__load_sym (struct dso *dso,struct map *map,const char *name,int fd,symbol_filter_t filter,int kmodule,int want_symtab)
@@ -1187,6 +1215,9 @@ static int dso__load_sym(struct dso *dso, struct map *map, const char *name, goto out_elf_end; } +if (dso__swap_init (dso, ehdr.e_ident[EI_DATA ])) + goto out_elf_end; +/* Always reject images with a mismatched build-id: */ if (dso->has_build_id) {u8 build_id[BUILD_ID_SIZE ];
@@ -1272,7 +1303,7 @@ static int dso__load_sym(struct dso *dso, struct map *map, const char *name,if (opdsec && sym.st_shndx == opdidx) {u32 offset = sym.st_value - opdshdr.sh_addr;u64 *opd = opddata->d_buf + offset; - sym.st_value = *opd; + sym.st_value =DSO__SWAP (dso, u64, *opd); sym.st_shndx =elf_addr_to_index (elf, sym.st_value); }
tools/perf/util/symbol.h +30 -0
@@ -9,6 +9,7 @@#include <linux/list.h> #include <linux/rbtree.h> #include <stdio.h> +#include <byteswap.h> #ifdef HAVE_CPLUS_DEMANGLE extern char *cplus_demangle (const char *,int );
@@ -160,11 +161,18 @@ enum dso_kernel_type {DSO_TYPE_GUEST_KERNEL }; +enum dso_swap_type { +DSO_SWAP__UNSET , +DSO_SWAP__NO , +DSO_SWAP__YES , +}; +struct dso {struct list_head node;struct rb_root symbols[MAP__NR_TYPES ];struct rb_root symbol_names[MAP__NR_TYPES ];enum dso_kernel_type kernel; +enum dso_swap_type needs_swap;u8 adjust_symbols:1 ;u8 has_build_id:1 ;u8 hit:1 ;
@@ -182,6 +190,28 @@ struct dso {char name[0 ]; }; +#define DSO__SWAP (dso, type, val) \ +({ \ + type ____r = val; \ + BUG_ON(dso->needs_swap == DSO_SWAP__UNSET); \ + if (dso->needs_swap == DSO_SWAP__YES) { \ + switch (sizeof(____r)) { \ + case 2: \ + ____r = bswap_16(val); \ + break; \ + case 4: \ + ____r = bswap_32(val); \ + break; \ + case 8: \ + ____r = bswap_64(val); \ + break; \ + default: \ + BUG_ON(1); \ + } \ + } \ + ____r; \ +}) +struct dso *dso__new (const char *name);void dso__delete (struct dso *dso);
PATCH v5 2/3 perf, tool: Handle endianity swap on sample_id_all header data
In reply to Jiri OlsaAdding endianity swapping for event header attached via sample_id_all.
Currently we dont do that and it's causing wrong data to be read when running report on architecture with different endianity than the record.
The perf is currently able to process 32-bit PPC samples on 32-bit and 64-bit x86.
Together with other endianity patches, this change fixies perf report discrepancies on origin and target systems as described in test 1 below, e.g. following perf report diff:
...
0.12% ps [kernel.kallsyms] [k] clear_page
- 0.12% awk bash [.] alloc_word_desc
+ 0.12% awk bash [.] yyparse
0.11% beah-rhts-task libpython2.6.so.1.0 [.] 0x5560e
0.10% perf libc-2.12.so [.] __ctype_toupper_loc
- 0.09% rhts-test-runne bash [.] maybe_make_export_env
+ 0.09% rhts-test-runne bash [.] 0x385a0
0.09% ps [kernel.kallsyms] [k] page_fault...
Note, running following to test perf endianity handling:
test 1)
- origin system:
# perf record -a -- sleep 10 (any perf record will do)
# perf report > report.origin
# perf archive perf.data - copy the perf.data, report.origin and perf.data.tar.bz2 to a target system and run:
# tar xjvf perf.data.tar.bz2 -C ~/.debug
# perf report > report.target
# diff -u report.origin report.target
- the diff should produce no output
(besides some white space stuff and possibly different
date/TZ output)
test 2)
- origin system:
# perf record -ag -fo /tmp/perf.data -- sleep 1
- mount origin system root to the target system on /mnt/origin
- target system:
# perf script --symfs /mnt/origin -I -i /mnt/origin/tmp/perf.data \
--kallsyms /mnt/origin/proc/kallsyms
- complete perf.data header is displayed--- tools/perf/util/session.c | 67 +++++++++++++++++++++++++++++++++++++-------- 1 files changed, 55 insertions(+), 12 deletions(-)
tools/perf/util/session.c +55 -12
@@ -441,37 +441,65 @@ void mem_bswap_64(void *src, int byte_size) } } -static void perf_event__all64_swap (union perf_event *event) +static void swap_sample_id_all (union perf_event *event,void *data) +{ +void *end = (void *) event + event->header.size; +int size = end - data; + +BUG_ON (size %sizeof (u64)); +mem_bswap_64 (data, size); +} + +static void perf_event__all64_swap (union perf_event *event, +bool sample_id_all__used ) {struct perf_event_header *hdr = &event->header;mem_bswap_64 (hdr +1 , event->header.size -sizeof (*hdr)); } -static void perf_event__comm_swap (union perf_event *event) +static void perf_event__comm_swap (union perf_event *event,bool sample_id_all) { event->comm.pid =bswap_32 (event->comm.pid); event->comm.tid =bswap_32 (event->comm.tid); + +if (sample_id_all) { +void *data = &event->comm.comm; + + data +=ALIGN (strlen (data) +1 ,sizeof (u64)); +swap_sample_id_all (event, data); + } } -static void perf_event__mmap_swap (union perf_event *event) +static void perf_event__mmap_swap (union perf_event *event, +bool sample_id_all) { event->mmap.pid =bswap_32 (event->mmap.pid); event->mmap.tid =bswap_32 (event->mmap.tid); event->mmap.start =bswap_64 (event->mmap.start); event->mmap.len =bswap_64 (event->mmap.len); event->mmap.pgoff =bswap_64 (event->mmap.pgoff); + +if (sample_id_all) { +void *data = &event->mmap.filename; + + data +=ALIGN (strlen (data) +1 ,sizeof (u64)); +swap_sample_id_all (event, data); + } } -static void perf_event__task_swap (union perf_event *event) +static void perf_event__task_swap (union perf_event *event,bool sample_id_all) { event->fork.pid =bswap_32 (event->fork.pid); event->fork.tid =bswap_32 (event->fork.tid); event->fork.ppid =bswap_32 (event->fork.ppid); event->fork.ptid =bswap_32 (event->fork.ptid); event->fork.time =bswap_64 (event->fork.time); + +if (sample_id_all) +swap_sample_id_all (event, &event->fork +1 ); } -static void perf_event__read_swap (union perf_event *event) +static void perf_event__read_swap (union perf_event *event,bool sample_id_all) { event->read.pid =bswap_32 (event->read.pid); event->read.tid =bswap_32 (event->read.tid);
@@ -479,6 +507,9 @@ static void perf_event__read_swap(union perf_event *event) event->read.time_enabled =bswap_64 (event->read.time_enabled); event->read.time_running =bswap_64 (event->read.time_running); event->read.id =bswap_64 (event->read.id); + +if (sample_id_all) +swap_sample_id_all (event, &event->read +1 ); }static u8 revbyte (u8 b)
@@ -530,7 +561,8 @@ void perf_event__attr_swap(struct perf_event_attr *attr)swap_bitfield ((u8 *) (&attr->read_format +1 ),sizeof (u64)); } -static void perf_event__hdr_attr_swap (union perf_event *event) +static void perf_event__hdr_attr_swap (union perf_event *event, +bool sample_id_all__used ) {size_t size;
@@ -541,18 +573,21 @@ static void perf_event__hdr_attr_swap(union perf_event *event)mem_bswap_64 (event->attr.id, size); } -static void perf_event__event_type_swap (union perf_event *event) +static void perf_event__event_type_swap (union perf_event *event, +bool sample_id_all__used ) { event->event_type.event_type.event_id =bswap_64 (event->event_type.event_type.event_id); } -static void perf_event__tracing_data_swap (union perf_event *event) +static void perf_event__tracing_data_swap (union perf_event *event, +bool sample_id_all__used ) { event->tracing_data.size =bswap_32 (event->tracing_data.size); } -typedef void (*perf_event__swap_op )(union perf_event *event); +typedef void (*perf_event__swap_op )(union perf_event *event, +bool sample_id_all);static perf_event__swap_op perf_event__swap_ops[] = { [PERF_RECORD_MMAP ] = perf_event__mmap_swap,
@@ -986,6 +1021,15 @@ static int perf_session__process_user_event(struct perf_session *session, union } } +static void event_swap (union perf_event *event,bool sample_id_all) +{ +perf_event__swap_op swap; + + swap = perf_event__swap_ops[event->header.type]; +if (swap) +swap (event, sample_id_all); +} + static int perf_session__process_event(struct perf_session *session, union perf_event *event, struct perf_tool *tool,
@@ -994,9 +1038,8 @@ static int perf_session__process_event(struct perf_session *session,struct perf_sample sample;int ret; -if (session->header.needs_swap && - perf_event__swap_ops[event->header.type]) - perf_event__swap_ops[event->header.type](event); +if (session->header.needs_swap) +event_swap (event, session->sample_id_all);if (event->header.type >=PERF_RECORD_HEADER_MAX )return -EINVAL ;
Signature · 1 line
1.7.7.6
[tip:perf/urgent] perf session: Handle endianity swap on sample_id_all header data
Commit-ID: 268fb20f832e1eb4afd5113ee31fef9332986b13 Gitweb: http://git.kernel.org/tip/268fb20f832e1eb4afd5113ee31fef9332986b13 Author: Jiri Olsa <[email protected]> AuthorDate: Wed, 30 May 2012 14:23:43 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 31 May 2012 11:58:14 -0300
perf session: Handle endianity swap on sample_id_all header data
Adding endianity swapping for event header attached via sample_id_all.
Currently we dont do that and it's causing wrong data to be read when running report on architecture with different endianity than the record.
The perf is currently able to process 32-bit PPC samples on 32-bit and 64-bit x86.
Together with other endianity patches, this change fixies perf report discrepancies on origin and target systems as described in test 1 below, e.g. following perf report diff:
...
0.12% ps [kernel.kallsyms] [k] clear_page
- 0.12% awk bash [.] alloc_word_desc
+ 0.12% awk bash [.] yyparse
0.11% beah-rhts-task libpython2.6.so.1.0 [.] 0x5560e
0.10% perf libc-2.12.so [.] __ctype_toupper_loc
- 0.09% rhts-test-runne bash [.] maybe_make_export_env
+ 0.09% rhts-test-runne bash [.] 0x385a0
0.09% ps [kernel.kallsyms] [k] page_fault...
Note, running following to test perf endianity handling:
test 1)
- origin system:
# perf record -a -- sleep 10 (any perf record will do)
# perf report > report.origin
# perf archive perf.data - copy the perf.data, report.origin and perf.data.tar.bz2 to a target system and run:
# tar xjvf perf.data.tar.bz2 -C ~/.debug
# perf report > report.target
# diff -u report.origin report.target
- the diff should produce no output
(besides some white space stuff and possibly different
date/TZ output)
test 2)
- origin system:
# perf record -ag -fo /tmp/perf.data -- sleep 1
- mount origin system root to the target system on /mnt/origin
- target system:
# perf script --symfs /mnt/origin -I -i /mnt/origin/tmp/perf.data \
--kallsyms /mnt/origin/proc/kallsyms
- complete perf.data header is displayed--- tools/perf/util/session.c | 67 +++++++++++++++++++++++++++++++++++++-------- 1 files changed, 55 insertions(+), 12 deletions(-)
tools/perf/util/session.c +55 -12
@@ -454,37 +454,65 @@ void mem_bswap_64(void *src, int byte_size) } } -static void perf_event__all64_swap (union perf_event *event) +static void swap_sample_id_all (union perf_event *event,void *data) +{ +void *end = (void *) event + event->header.size; +int size = end - data; + +BUG_ON (size %sizeof (u64)); +mem_bswap_64 (data, size); +} + +static void perf_event__all64_swap (union perf_event *event, +bool sample_id_all__used ) {struct perf_event_header *hdr = &event->header;mem_bswap_64 (hdr +1 , event->header.size -sizeof (*hdr)); } -static void perf_event__comm_swap (union perf_event *event) +static void perf_event__comm_swap (union perf_event *event,bool sample_id_all) { event->comm.pid =bswap_32 (event->comm.pid); event->comm.tid =bswap_32 (event->comm.tid); + +if (sample_id_all) { +void *data = &event->comm.comm; + + data +=ALIGN (strlen (data) +1 ,sizeof (u64)); +swap_sample_id_all (event, data); + } } -static void perf_event__mmap_swap (union perf_event *event) +static void perf_event__mmap_swap (union perf_event *event, +bool sample_id_all) { event->mmap.pid =bswap_32 (event->mmap.pid); event->mmap.tid =bswap_32 (event->mmap.tid); event->mmap.start =bswap_64 (event->mmap.start); event->mmap.len =bswap_64 (event->mmap.len); event->mmap.pgoff =bswap_64 (event->mmap.pgoff); + +if (sample_id_all) { +void *data = &event->mmap.filename; + + data +=ALIGN (strlen (data) +1 ,sizeof (u64)); +swap_sample_id_all (event, data); + } } -static void perf_event__task_swap (union perf_event *event) +static void perf_event__task_swap (union perf_event *event,bool sample_id_all) { event->fork.pid =bswap_32 (event->fork.pid); event->fork.tid =bswap_32 (event->fork.tid); event->fork.ppid =bswap_32 (event->fork.ppid); event->fork.ptid =bswap_32 (event->fork.ptid); event->fork.time =bswap_64 (event->fork.time); + +if (sample_id_all) +swap_sample_id_all (event, &event->fork +1 ); } -static void perf_event__read_swap (union perf_event *event) +static void perf_event__read_swap (union perf_event *event,bool sample_id_all) { event->read.pid =bswap_32 (event->read.pid); event->read.tid =bswap_32 (event->read.tid);
@@ -492,6 +520,9 @@ static void perf_event__read_swap(union perf_event *event) event->read.time_enabled =bswap_64 (event->read.time_enabled); event->read.time_running =bswap_64 (event->read.time_running); event->read.id =bswap_64 (event->read.id); + +if (sample_id_all) +swap_sample_id_all (event, &event->read +1 ); }static u8 revbyte (u8 b)
@@ -543,7 +574,8 @@ void perf_event__attr_swap(struct perf_event_attr *attr)swap_bitfield ((u8 *) (&attr->read_format +1 ),sizeof (u64)); } -static void perf_event__hdr_attr_swap (union perf_event *event) +static void perf_event__hdr_attr_swap (union perf_event *event, +bool sample_id_all__used ) {size_t size;
@@ -554,18 +586,21 @@ static void perf_event__hdr_attr_swap(union perf_event *event)mem_bswap_64 (event->attr.id, size); } -static void perf_event__event_type_swap (union perf_event *event) +static void perf_event__event_type_swap (union perf_event *event, +bool sample_id_all__used ) { event->event_type.event_type.event_id =bswap_64 (event->event_type.event_type.event_id); } -static void perf_event__tracing_data_swap (union perf_event *event) +static void perf_event__tracing_data_swap (union perf_event *event, +bool sample_id_all__used ) { event->tracing_data.size =bswap_32 (event->tracing_data.size); } -typedef void (*perf_event__swap_op )(union perf_event *event); +typedef void (*perf_event__swap_op )(union perf_event *event, +bool sample_id_all);static perf_event__swap_op perf_event__swap_ops[] = { [PERF_RECORD_MMAP ] = perf_event__mmap_swap,
@@ -999,6 +1034,15 @@ static int perf_session__process_user_event(struct perf_session *session, union } } +static void event_swap (union perf_event *event,bool sample_id_all) +{ +perf_event__swap_op swap; + + swap = perf_event__swap_ops[event->header.type]; +if (swap) +swap (event, sample_id_all); +} + static int perf_session__process_event(struct perf_session *session, union perf_event *event, struct perf_tool *tool,
@@ -1007,9 +1051,8 @@ static int perf_session__process_event(struct perf_session *session,struct perf_sample sample;int ret; -if (session->header.needs_swap && - perf_event__swap_ops[event->header.type]) - perf_event__swap_ops[event->header.type](event); +if (session->header.needs_swap) +event_swap (event, session->sample_id_all);if (event->header.type >=PERF_RECORD_HEADER_MAX )return -EINVAL ;
PATCH v5 3/3 perf, tool: Fix 32 bit values endianity swap for sample_id_all header
In reply to Jiri OlsaWe swap the sample_id_all header by u64 pointers. Some members of the header happen to be 32 bit values. We need to handle them separatelly.
Together with other endianity patches, this change fixies perf report discrepancies on origin and target systems as described in test 1 below, e.g. following perf report diff:
...
0.12% ps [kernel.kallsyms] [k] clear_page
- 0.12% awk bash [.] alloc_word_desc
+ 0.12% awk bash [.] yyparse
0.11% beah-rhts-task libpython2.6.so.1.0 [.] 0x5560e
0.10% perf libc-2.12.so [.] __ctype_toupper_loc
- 0.09% rhts-test-runne bash [.] maybe_make_export_env
+ 0.09% rhts-test-runne bash [.] 0x385a0
0.09% ps [kernel.kallsyms] [k] page_fault...
Note, running following to test perf endianity handling:
test 1)
- origin system:
# perf record -a -- sleep 10 (any perf record will do)
# perf report > report.origin
# perf archive perf.data - copy the perf.data, report.origin and perf.data.tar.bz2 to a target system and run:
# tar xjvf perf.data.tar.bz2 -C ~/.debug
# perf report > report.target
# diff -u report.origin report.target
- the diff should produce no output
(besides some white space stuff and possibly different
date/TZ output)
test 2)
- origin system:
# perf record -ag -fo /tmp/perf.data -- sleep 1
- mount origin system root to the target system on /mnt/origin
- target system:
# perf script --symfs /mnt/origin -I -i /mnt/origin/tmp/perf.data \
--kallsyms /mnt/origin/proc/kallsyms
- complete perf.data header is displayed--- tools/perf/util/evsel.c | 29 ++++++++++++++++++++++------- 1 files changed, 22 insertions(+), 7 deletions(-)
tools/perf/util/evsel.c +22 -7
@@ -404,16 +404,24 @@ int perf_evsel__open_per_thread(struct perf_evsel *evsel, }static int perf_event__parse_id_sample (const union perf_event *event,u64 type, -struct perf_sample *sample) +struct perf_sample *sample, +bool swapped) {const u64 *array = event->sample.array; +union u64_swap u; array += ((event->header.size -sizeof (event->header)) /sizeof (u64)) -1 ;if (type &PERF_SAMPLE_CPU ) { -u32 *p = (u32 *)array; - sample->cpu = *p; + u.val64 = *array; +if (swapped) { +/* undo swap of u64, then swap on individual u32s */ + u.val64 =bswap_64 (u.val64); + u.val32[0 ] =bswap_32 (u.val32[0 ]); + } + + sample->cpu = u.val32[0 ]; array--; }
@@ -433,9 +441,16 @@ static int perf_event__parse_id_sample(const union perf_event *event, u64 type, }if (type &PERF_SAMPLE_TID ) { -u32 *p = (u32 *)array; - sample->pid = p[0 ]; - sample->tid = p[1 ]; + u.val64 = *array; +if (swapped) { +/* undo swap of u64, then swap on individual u32s */ + u.val64 =bswap_64 (u.val64); + u.val32[0 ] =bswap_32 (u.val32[0 ]); + u.val32[1 ] =bswap_32 (u.val32[1 ]); + } + + sample->pid = u.val32[0 ]; + sample->tid = u.val32[1 ]; }return 0 ;
@@ -472,7 +487,7 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,if (event->header.type !=PERF_RECORD_SAMPLE ) {if (!sample_id_all)return 0 ; -return perf_event__parse_id_sample (event, type, data); +return perf_event__parse_id_sample (event, type, data, swapped); } array = event->sample.array;
Signature · 1 line
1.7.7.6
[tip:perf/urgent] perf evsel: Fix 32 bit values endianity swap for sample_id_all header
Commit-ID: 37073f9e449bc430e6c40b9cffc2558002a0256a Gitweb: http://git.kernel.org/tip/37073f9e449bc430e6c40b9cffc2558002a0256a Author: Jiri Olsa <[email protected]> AuthorDate: Wed, 30 May 2012 14:23:44 +0200 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 31 May 2012 11:59:01 -0300
perf evsel: Fix 32 bit values endianity swap for sample_id_all header
We swap the sample_id_all header by u64 pointers. Some members of the header happen to be 32 bit values. We need to handle them separatelly.
Together with other endianity patches, this change fixies perf report discrepancies on origin and target systems as described in test 1 below, e.g. following perf report diff:
...
0.12% ps [kernel.kallsyms] [k] clear_page
- 0.12% awk bash [.] alloc_word_desc
+ 0.12% awk bash [.] yyparse
0.11% beah-rhts-task libpython2.6.so.1.0 [.] 0x5560e
0.10% perf libc-2.12.so [.] __ctype_toupper_loc
- 0.09% rhts-test-runne bash [.] maybe_make_export_env
+ 0.09% rhts-test-runne bash [.] 0x385a0
0.09% ps [kernel.kallsyms] [k] page_fault...
Note, running following to test perf endianity handling:
test 1)
- origin system:
# perf record -a -- sleep 10 (any perf record will do)
# perf report > report.origin
# perf archive perf.data - copy the perf.data, report.origin and perf.data.tar.bz2 to a target system and run:
# tar xjvf perf.data.tar.bz2 -C ~/.debug
# perf report > report.target
# diff -u report.origin report.target
- the diff should produce no output
(besides some white space stuff and possibly different
date/TZ output)
test 2)
- origin system:
# perf record -ag -fo /tmp/perf.data -- sleep 1
- mount origin system root to the target system on /mnt/origin
- target system:
# perf script --symfs /mnt/origin -I -i /mnt/origin/tmp/perf.data \
--kallsyms /mnt/origin/proc/kallsyms
- complete perf.data header is displayed--- tools/perf/util/evsel.c | 29 ++++++++++++++++++++++------- 1 files changed, 22 insertions(+), 7 deletions(-)
tools/perf/util/evsel.c +22 -7
@@ -494,16 +494,24 @@ int perf_evsel__open_per_thread(struct perf_evsel *evsel, }static int perf_event__parse_id_sample (const union perf_event *event,u64 type, -struct perf_sample *sample) +struct perf_sample *sample, +bool swapped) {const u64 *array = event->sample.array; +union u64_swap u; array += ((event->header.size -sizeof (event->header)) /sizeof (u64)) -1 ;if (type &PERF_SAMPLE_CPU ) { -u32 *p = (u32 *)array; - sample->cpu = *p; + u.val64 = *array; +if (swapped) { +/* undo swap of u64, then swap on individual u32s */ + u.val64 =bswap_64 (u.val64); + u.val32[0 ] =bswap_32 (u.val32[0 ]); + } + + sample->cpu = u.val32[0 ]; array--; }
@@ -523,9 +531,16 @@ static int perf_event__parse_id_sample(const union perf_event *event, u64 type, }if (type &PERF_SAMPLE_TID ) { -u32 *p = (u32 *)array; - sample->pid = p[0 ]; - sample->tid = p[1 ]; + u.val64 = *array; +if (swapped) { +/* undo swap of u64, then swap on individual u32s */ + u.val64 =bswap_64 (u.val64); + u.val32[0 ] =bswap_32 (u.val32[0 ]); + u.val32[1 ] =bswap_32 (u.val32[1 ]); + } + + sample->pid = u.val32[0 ]; + sample->tid = u.val32[1 ]; }return 0 ;
@@ -562,7 +577,7 @@ int perf_event__parse_sample(const union perf_event *event, u64 type,if (event->header.type !=PERF_RECORD_SAMPLE ) {if (!sample_id_all)return 0 ; -return perf_event__parse_id_sample (event, type, data); +return perf_event__parse_id_sample (event, type, data, swapped); } array = event->sample.array;