Re: [PATCH v6 16/18] perf: enable reading of perf.data files fromdifferent ABI rev
From: Stephane Eranian
Date: Thu Feb 09 2012 - 17:41:10 EST
On Thu, Feb 9, 2012 at 11:39 PM, David Ahern <dsahern@xxxxxxxxx> wrote:
> On 02/09/2012 03:21 PM, Stephane Eranian wrote:
>> This patch allows perf to process perf.data files generated
>> using an ABI that has a different perf_event_attr struct size, i.e.,
>> a different ABI version.
>>
>> The perf_event_attr can be extended, yet perf needs to cope with
>> older perf.data files. Similarly, perf must be able to cope with
>> a perf.data file which is using a newer version of the ABI than
>> what it knows about.
>>
>> This patch adds read_attr(), a routine that reads a perf_event_attr
>> struct from a file incrementally based on its advertised size. If
>> the on-file struct is smaller than what perf knows, then the extra
>> fields are zeroed. If the on-file struct is bigger, then perf only
>> uses what it knows about, the rest is skipped.
>>
>> Signed-off-by: Stephane Eranian <eranian@xxxxxxxxxx>
>> ---
>> Âtools/perf/util/header.c Â| Â 53 ++++++++++++++++++++++++++++++++++++++++----
>> Âtools/perf/util/session.c | Â Â4 +-
>> Â2 files changed, 50 insertions(+), 7 deletions(-)
>>
>> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
>> index 6f4187d..c2a3410 100644
>> --- a/tools/perf/util/header.c
>> +++ b/tools/perf/util/header.c
>> @@ -1959,6 +1959,51 @@ static int perf_header__read_pipe(struct perf_session *session, int fd)
>> Â Â Â return 0;
>> Â}
>>
>> +static int read_attr(int fd, struct perf_header *ph,
>> + Â Â Â Â Â Â Â Â Âstruct perf_file_attr *f_attr)
>> +{
>> + Â Â struct perf_event_attr *attr = &f_attr->attr;
>> + Â Â size_t sz, left;
>> + Â Â size_t our_sz = sizeof(f_attr->attr);
>> + Â Â int ret;
>> +
>> + Â Â memset(f_attr, 0, sizeof(*f_attr));
>> +
>> + Â Â /* read minimal guaranteed structure */
>> + Â Â ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
>> + Â Â if (ret <= 0) {
>> + Â Â Â Â Â Â pr_debug("cannot read %d bytes of header attr\n",
>> + Â Â Â Â Â Â Â Â Â Â ÂPERF_ATTR_SIZE_VER0);
>> + Â Â Â Â Â Â return -1;
>> + Â Â }
>> +
>> + Â Â /* on file perf_event_attr size */
>> + Â Â sz = attr->size;
>> + Â Â if (ph->needs_swap)
>> + Â Â Â Â Â Â sz = bswap_32(sz);
>> +
>> + Â Â if (sz == 0) {
>> + Â Â Â Â Â Â /* assume ABI0 */
>> + Â Â Â Â Â Â sz = ÂPERF_ATTR_SIZE_VER0;
>> + Â Â } else if (sz > our_sz) {
>> + Â Â Â Â Â Â pr_debug("file uses a more recent and unsupported ABI"
>> + Â Â Â Â Â Â Â Â Â Â Â" (%zu bytes extra)\n", sz - our_sz);
>> + Â Â Â Â Â Â return -1;
>> + Â Â }
>> + Â Â /* what we have not yet read and that we know about */
>> + Â Â left = sz - PERF_ATTR_SIZE_VER0;
>> + Â Â if (left) {
>> + Â Â Â Â Â Â void *ptr = attr;
>> + Â Â Â Â Â Â ptr += PERF_ATTR_SIZE_VER0;
>> +
>> + Â Â Â Â Â Â ret = readn(fd, ptr, left);
>> + Â Â }
>> + Â Â /* read perf_file_section, ids are read in caller */
>> + Â Â ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
>
> I thought the above 2 lines were to be removed.
>
No, They are needed to get the offsets of the ids[] table.
The field name are confusing. This is not reading the ids[],
but just the offset of where the table is. Without that you
get bogus offsets. I ran into that bug during testing...
> David
>
>
>> +
>> + Â Â return ret <= 0 ? -1 : 0;
>> +}
>> +
>> Âint perf_session__read_header(struct perf_session *session, int fd)
>> Â{
>> Â Â Â struct perf_header *header = &session->header;
>> @@ -1974,19 +2019,17 @@ int perf_session__read_header(struct perf_session *session, int fd)
>> Â Â Â if (session->fd_pipe)
>> Â Â Â Â Â Â Â return perf_header__read_pipe(session, fd);
>>
>> - Â Â if (perf_file_header__read(&f_header, header, fd) < 0) {
>> - Â Â Â Â Â Â pr_debug("incompatible file format\n");
>> + Â Â if (perf_file_header__read(&f_header, header, fd) < 0)
>> Â Â Â Â Â Â Â return -EINVAL;
>> - Â Â }
>>
>> - Â Â nr_attrs = f_header.attrs.size / sizeof(f_attr);
>> + Â Â nr_attrs = f_header.attrs.size / f_header.attr_size;
>> Â Â Â lseek(fd, f_header.attrs.offset, SEEK_SET);
>>
>> Â Â Â for (i = 0; i < nr_attrs; i++) {
>> Â Â Â Â Â Â Â struct perf_evsel *evsel;
>> Â Â Â Â Â Â Â off_t tmp;
>>
>> - Â Â Â Â Â Â if (readn(fd, &f_attr, sizeof(f_attr)) <= 0)
>> + Â Â Â Â Â Â if (read_attr(fd, header, &f_attr) < 0)
>> Â Â Â Â Â Â Â Â Â Â Â goto out_errno;
>>
>> Â Â Â Â Â Â Â if (header->needs_swap)
>> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
>> index 3f3afed..5476320 100644
>> --- a/tools/perf/util/session.c
>> +++ b/tools/perf/util/session.c
>> @@ -24,7 +24,7 @@ static int perf_session__open(struct perf_session *self, bool force)
>> Â Â Â Â Â Â Â self->fd = STDIN_FILENO;
>>
>> Â Â Â Â Â Â Â if (perf_session__read_header(self, self->fd) < 0)
>> - Â Â Â Â Â Â Â Â Â Â pr_err("incompatible file format");
>> + Â Â Â Â Â Â Â Â Â Â pr_err("incompatible file format (rerun with -v to learn more)");
>>
>> Â Â Â Â Â Â Â return 0;
>> Â Â Â }
>> @@ -56,7 +56,7 @@ static int perf_session__open(struct perf_session *self, bool force)
>> Â Â Â }
>>
>> Â Â Â if (perf_session__read_header(self, self->fd) < 0) {
>> - Â Â Â Â Â Â pr_err("incompatible file format");
>> + Â Â Â Â Â Â pr_err("incompatible file format (rerun with -v to learn more)");
>> Â Â Â Â Â Â Â goto out_close;
>> Â Â Â }
>>
--
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/