Re: perf record: why we used type casting of (uint64_t *) instead of int

From: Stephane Eranian
Date: Fri May 25 2012 - 04:20:59 EST


On Fri, May 25, 2012 at 7:27 AM, Anshuman Khandual
<khandual@xxxxxxxxxxxxxxxxxx> wrote:
> This code is breaking in powerpc systems.
>
> (1) 'opt->value' gets updated inside the function parse_branch_stack via
> Â Âdereferencing a (uint64_t *) type casted pointer.
>
> (2) But the value is not accessible when we again use opt->value via
> Â Âdereferencing a (int *) type casted pointer.
>
> (3) As a result record.opts.branch_stack remains 0 and unchanged by parse_branch_stack
>
> This is caused by bit representation of 'uint64_t' and 'int' in powerpc systems. Bytes update
> for the data (when accessed trough (uint64_t *) casting) is no longer available to the
> data when accessed through (int *) type casting. Verified this from bit representation of
> the data (accessed through both type casting methods).
>
> However this problem does not seem to be present on an Intel box. Integer dereferencing of
> the opt->value still gives the value which was updated as (uint64_t).
>
> All this problem would not have been there if we had used (int *) instead of (uint64_t *) in
> the first place inside parse_branch_stack function.
>
The bug is that in struct record_opts, branch_stack is declared int
instead of u64.
I can post a patch to fix that. The value is eventually passed to struct
perf_event_attr.branch_sample_type which is defined as u64.

I can post a patch to fix that.

Thanks for catching this.

> On Thursday 24 May 2012 02:51 PM, Anshuman Khandual wrote:
>
>> Hey Stephane,
>>
>> Just wondering why we used the type casting of (uint64_t *) on a data
>> which is defined as "int" in the structure of "perf_record_opts".
>>
>> struct perf_record_opts {
>> Â Â Â Â struct perf_target target;
>>     bool     call_graph;
>>     bool     group;
>>     bool     inherit_stat;
>>     bool     no_delay;
>>     bool     no_inherit;
>>     bool     no_samples;
>>     bool     pipe_output;
>>     bool     raw_samples;
>>     bool     sample_address;
>>     bool     sample_time;
>>     bool     sample_id_all_missing;
>>     bool     exclude_guest_missing;
>>     bool     period;
>> Â Â Â Â unsigned int freq;
>> Â Â Â Â unsigned int mmap_pages;
>> Â Â Â Â unsigned int user_freq;
>>     int     Âbranch_stack;
>> Â Â Â Â u64 Â Â Â Â Âdefault_interval;
>> Â Â Â Â u64 Â Â Â Â Âuser_interval;
>> };
>>
>> static int
>> parse_branch_stack(const struct option *opt, const char *str, int unset)
>> {
>> #define ONLY_PLM \
>> Â Â Â Â (PERF_SAMPLE_BRANCH_USER Â Â Â Â|\
>> Â Â Â Â ÂPERF_SAMPLE_BRANCH_KERNEL Â Â Â|\
>> Â Â Â Â ÂPERF_SAMPLE_BRANCH_HV)
>>
>> Â Â Â Â uint64_t *mode = (uint64_t *)opt->value;
>> --
>> Regards
>> Anshuman Khandual
>>
>> --
>> 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/
>>
>
>
--
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/