aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
authorStephane Eranian <eranian@google.com>2012-02-02 07:54:44 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2012-02-09 09:28:10 -0500
commit73323f541fe5f55a3b8a5c3d565bfc1efd64abf6 (patch)
tree472cb89ec5cd5d63d64adbacb8b3d212218e8f47 /tools/perf
parentc98fdeaa92731308ed80386261fa2589addefa47 (diff)
perf tools: fix endianness detection in perf.data
The current version of perf detects whether or not the perf.data file is written in a different endianness using the attr_size field in the header of the file. This field represents sizeof(struct perf_event_attr) as known to perf record. If the sizes do not match, then perf tries the byte-swapped version. If they match, then the tool assumes a different endianness. The issue with the approach is that it assumes the size of perf_event_attr always has to match between perf record and perf report. However, the kernel perf_event ABI is extensible. New fields can be added to struct perf_event_attr. Consequently, it is not possible to use attr_size to detect endianness. This patch takes another approach by using the magic number written at the beginning of the perf.data file to detect endianness. The magic number is an eight-byte signature. It's primary purpose is to identify (signature) a perf.data file. But it could also be used to encode the endianness. The patch introduces a new value for this signature. The key difference is that the signature is written differently in the file depending on the endianness. Thus, by comparing the signature from the file with the tool's own signature it is possible to detect endianness. The new signature is "PERFILE2". Backward compatiblity with existing perf.data file is ensured. Tested-by: David Ahern <dsahern@gmail.com> Acked-by: David Ahern <dsahern@gmail.com> Cc: Andi Kleen <andi@firstfloor.org> Cc: Anshuman Khandual <khandual@linux.vnet.ibm.com> Cc: Arun Sharma <asharma@fb.com> Cc: David Ahern <dsahern@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Lin Ming <ming.m.lin@intel.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Roberto Agostino Vitillo <ravitillo@lbl.gov> Cc: Robert Richter <robert.richter@amd.com> Cc: Vince Weaver <vweaver1@eecs.utk.edu> Link: http://lkml.kernel.org/r/1328187288-24395-15-git-send-email-eranian@google.com Signed-off-by: Stephane Eranian <eranian@google.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/util/header.c77
1 files changed, 64 insertions, 13 deletions
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index ecd7f4dd7eea..6f4187de4b72 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -63,9 +63,20 @@ char *perf_header__find_event(u64 id)
63 return NULL; 63 return NULL;
64} 64}
65 65
66static const char *__perf_magic = "PERFFILE"; 66/*
67 * magic2 = "PERFILE2"
68 * must be a numerical value to let the endianness
69 * determine the memory layout. That way we are able
70 * to detect endianness when reading the perf.data file
71 * back.
72 *
73 * we check for legacy (PERFFILE) format.
74 */
75static const char *__perf_magic1 = "PERFFILE";
76static const u64 __perf_magic2 = 0x32454c4946524550ULL;
77static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
67 78
68#define PERF_MAGIC (*(u64 *)__perf_magic) 79#define PERF_MAGIC __perf_magic2
69 80
70struct perf_file_attr { 81struct perf_file_attr {
71 struct perf_event_attr attr; 82 struct perf_event_attr attr;
@@ -1620,24 +1631,59 @@ out_free:
1620 return err; 1631 return err;
1621} 1632}
1622 1633
1634static int check_magic_endian(u64 *magic, struct perf_file_header *header,
1635 struct perf_header *ph)
1636{
1637 int ret;
1638
1639 /* check for legacy format */
1640 ret = memcmp(magic, __perf_magic1, sizeof(*magic));
1641 if (ret == 0) {
1642 pr_debug("legacy perf.data format\n");
1643 if (!header)
1644 return -1;
1645
1646 if (header->attr_size != sizeof(struct perf_file_attr)) {
1647 u64 attr_size = bswap_64(header->attr_size);
1648
1649 if (attr_size != sizeof(struct perf_file_attr))
1650 return -1;
1651
1652 ph->needs_swap = true;
1653 }
1654 return 0;
1655 }
1656
1657 /* check magic number with same endianness */
1658 if (*magic == __perf_magic2)
1659 return 0;
1660
1661 /* check magic number but opposite endianness */
1662 if (*magic != __perf_magic2_sw)
1663 return -1;
1664
1665 ph->needs_swap = true;
1666
1667 return 0;
1668}
1669
1623int perf_file_header__read(struct perf_file_header *header, 1670int perf_file_header__read(struct perf_file_header *header,
1624 struct perf_header *ph, int fd) 1671 struct perf_header *ph, int fd)
1625{ 1672{
1673 int ret;
1674
1626 lseek(fd, 0, SEEK_SET); 1675 lseek(fd, 0, SEEK_SET);
1627 1676
1628 if (readn(fd, header, sizeof(*header)) <= 0 || 1677 ret = readn(fd, header, sizeof(*header));
1629 memcmp(&header->magic, __perf_magic, sizeof(header->magic))) 1678 if (ret <= 0)
1630 return -1; 1679 return -1;
1631 1680
1632 if (header->attr_size != sizeof(struct perf_file_attr)) { 1681 if (check_magic_endian(&header->magic, header, ph) < 0)
1633 u64 attr_size = bswap_64(header->attr_size); 1682 return -1;
1634
1635 if (attr_size != sizeof(struct perf_file_attr))
1636 return -1;
1637 1683
1684 if (ph->needs_swap) {
1638 mem_bswap_64(header, offsetof(struct perf_file_header, 1685 mem_bswap_64(header, offsetof(struct perf_file_header,
1639 adds_features)); 1686 adds_features));
1640 ph->needs_swap = true;
1641 } 1687 }
1642 1688
1643 if (header->size != sizeof(*header)) { 1689 if (header->size != sizeof(*header)) {
@@ -1873,8 +1919,13 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
1873 struct perf_header *ph, int fd, 1919 struct perf_header *ph, int fd,
1874 bool repipe) 1920 bool repipe)
1875{ 1921{
1876 if (readn(fd, header, sizeof(*header)) <= 0 || 1922 int ret;
1877 memcmp(&header->magic, __perf_magic, sizeof(header->magic))) 1923
1924 ret = readn(fd, header, sizeof(*header));
1925 if (ret <= 0)
1926 return -1;
1927
1928 if (check_magic_endian(&header->magic, NULL, ph) < 0)
1878 return -1; 1929 return -1;
1879 1930
1880 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) 1931 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)