Re: [PATCH v5 14/18] perf: fix endianness detection in perf.data

From: Stephane Eranian
Date: Mon Feb 06 2012 - 13:18:30 EST


On Mon, Feb 6, 2012 at 7:17 PM, Arnaldo Carvalho de Melo
<acme@xxxxxxxxxx> wrote:
> Em Thu, Feb 02, 2012 at 01:54:44PM +0100, Stephane Eranian escreveu:
>> 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.
>
> Looks ok, but IIRC David Ahern interacted with you on this specific
> patch in the past, having his Acked-by and/or Tested-by would be great,
> David?
>
I agree, I am still waiting for the results of his test on big-endian systems.
I don't have any unfortunately.

> - Arnaldo
>
>> Signed-off-by: Stephane Eranian <eranian@xxxxxxxxxx>
>> ---
>> Âtools/perf/util/header.c | Â 77 ++++++++++++++++++++++++++++++++++++++--------
>> Â1 files changed, 64 insertions(+), 13 deletions(-)
>>
>> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
>> index ecd7f4d..6f4187d 100644
>> --- a/tools/perf/util/header.c
>> +++ b/tools/perf/util/header.c
>> @@ -63,9 +63,20 @@ char *perf_header__find_event(u64 id)
>> Â Â Â return NULL;
>> Â}
>>
>> -static const char *__perf_magic = "PERFFILE";
>> +/*
>> + * magic2 = "PERFILE2"
>> + * must be a numerical value to let the endianness
>> + * determine the memory layout. That way we are able
>> + * to detect endianness when reading the perf.data file
>> + * back.
>> + *
>> + * we check for legacy (PERFFILE) format.
>> + */
>> +static const char *__perf_magic1 = "PERFFILE";
>> +static const u64 __perf_magic2 Â Â= 0x32454c4946524550ULL;
>> +static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
>>
>> -#define PERF_MAGIC Â (*(u64 *)__perf_magic)
>> +#define PERF_MAGIC Â __perf_magic2
>>
>> Âstruct perf_file_attr {
>> Â Â Â struct perf_event_attr Âattr;
>> @@ -1620,24 +1631,59 @@ int perf_header__process_sections(struct perf_header *header, int fd,
>> Â Â Â return err;
>> Â}
>>
>> +static int check_magic_endian(u64 *magic, struct perf_file_header *header,
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â struct perf_header *ph)
>> +{
>> + Â Â int ret;
>> +
>> + Â Â /* check for legacy format */
>> + Â Â ret = memcmp(magic, __perf_magic1, sizeof(*magic));
>> + Â Â if (ret == 0) {
>> + Â Â Â Â Â Â pr_debug("legacy perf.data format\n");
>> + Â Â Â Â Â Â if (!header)
>> + Â Â Â Â Â Â Â Â Â Â return -1;
>> +
>> + Â Â Â Â Â Â if (header->attr_size != sizeof(struct perf_file_attr)) {
>> + Â Â Â Â Â Â Â Â Â Â u64 attr_size = bswap_64(header->attr_size);
>> +
>> + Â Â Â Â Â Â Â Â Â Â if (attr_size != sizeof(struct perf_file_attr))
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â return -1;
>> +
>> + Â Â Â Â Â Â Â Â Â Â ph->needs_swap = true;
>> + Â Â Â Â Â Â }
>> + Â Â Â Â Â Â return 0;
>> + Â Â }
>> +
>> + Â Â /* check magic number with same endianness */
>> + Â Â if (*magic == __perf_magic2)
>> + Â Â Â Â Â Â return 0;
>> +
>> + Â Â /* check magic number but opposite endianness */
>> + Â Â if (*magic != __perf_magic2_sw)
>> + Â Â Â Â Â Â return -1;
>> +
>> + Â Â ph->needs_swap = true;
>> +
>> + Â Â return 0;
>> +}
>> +
>> Âint perf_file_header__read(struct perf_file_header *header,
>> Â Â Â Â Â Â Â Â Â Â Â Â Âstruct perf_header *ph, int fd)
>> Â{
>> + Â Â int ret;
>> +
>> Â Â Â lseek(fd, 0, SEEK_SET);
>>
>> - Â Â if (readn(fd, header, sizeof(*header)) <= 0 ||
>> - Â Â Â Â memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
>> + Â Â ret = readn(fd, header, sizeof(*header));
>> + Â Â if (ret <= 0)
>> Â Â Â Â Â Â Â return -1;
>>
>> - Â Â if (header->attr_size != sizeof(struct perf_file_attr)) {
>> - Â Â Â Â Â Â u64 attr_size = bswap_64(header->attr_size);
>> -
>> - Â Â Â Â Â Â if (attr_size != sizeof(struct perf_file_attr))
>> - Â Â Â Â Â Â Â Â Â Â return -1;
>> + Â Â if (check_magic_endian(&header->magic, header, ph) < 0)
>> + Â Â Â Â Â Â return -1;
>>
>> + Â Â if (ph->needs_swap) {
>> Â Â Â Â Â Â Â mem_bswap_64(header, offsetof(struct perf_file_header,
>> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â adds_features));
>> - Â Â Â Â Â Â ph->needs_swap = true;
>> + Â Â Â Â Â Â Â Â Â Â Â Â Âadds_features));
>> Â Â Â }
>>
>> Â Â Â if (header->size != sizeof(*header)) {
>> @@ -1873,8 +1919,13 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âstruct perf_header *ph, int fd,
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âbool repipe)
>> Â{
>> - Â Â if (readn(fd, header, sizeof(*header)) <= 0 ||
>> - Â Â Â Â memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
>> + Â Â int ret;
>> +
>> + Â Â ret = readn(fd, header, sizeof(*header));
>> + Â Â if (ret <= 0)
>> + Â Â Â Â Â Â return -1;
>> +
>> + Â Â Âif (check_magic_endian(&header->magic, NULL, ph) < 0)
>> Â Â Â Â Â Â Â return -1;
>>
>> Â Â Â if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
>> --
>> 1.7.4.1
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/