2012-10-05 10:36:15

by Stephane Eranian

[permalink] [raw]
Subject: [RFC] perf: perf_event_attr anon unions and static initializer issue

Hi,

I am writing a little test program for the perf_event API which is using
hardcoded events. Some of those events (SNBEP uncore events) require
a value for config1. I was naively assuming, one could simply do:

struct perf_event_attr attr = { .config = 0x1234, .config1 = 0x456 };

However this does not work with gcc version < 4.6 apparently. Don't
know how this even works with other compilers. I looked online and
I could not really find a good way of solving this that would be portable
across compiler versions.

If we name those unions, then this will break lots of existing programs.
We'd have to create a "named" version of perf_event_attr and then
memcpy() at runtime. Or simply initialize the .config1 and such fields at
runtime from a separate table.

Does anyone have a better solution to propose?


2012-10-05 11:01:36

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [RFC] perf: perf_event_attr anon unions and static initializer issue

On Fri, 2012-10-05 at 12:36 +0200, Stephane Eranian wrote:

> struct perf_event_attr attr = { .config = 0x1234, .config1 = 0x456 };

> Does anyone have a better solution to propose?


struct perf_event_attr attr = {
.config = 0x1234,
{ .config1 = 0x5678 },
};

sometimes works, but apparently not in this case.. its a bit unfortunate
indeed. EDG based compilers and the latest C std use your version --
hence also 4.6 supporting it.

Yeah, I'm afraid we're stuck with this until a future where modern C
isn't modern anymore.

2012-10-05 11:49:46

by Stephane Eranian

[permalink] [raw]
Subject: Re: [RFC] perf: perf_event_attr anon unions and static initializer issue

On Fri, Oct 5, 2012 at 1:01 PM, Peter Zijlstra <[email protected]> wrote:
> On Fri, 2012-10-05 at 12:36 +0200, Stephane Eranian wrote:
>
>> struct perf_event_attr attr = { .config = 0x1234, .config1 = 0x456 };
>
>> Does anyone have a better solution to propose?
>
>
> struct perf_event_attr attr = {
> .config = 0x1234,
> { .config1 = 0x5678 },
> };
>
> sometimes works, but apparently not in this case.. its a bit unfortunate
> indeed. EDG based compilers and the latest C std use your version --
> hence also 4.6 supporting it.
>
> Yeah, I'm afraid we're stuck with this until a future where modern C
> isn't modern anymore.

Yeah, unfortunately.
I ended up creating a separate table for config1 and initializing attr.config1
at runtime. That way it works regardless of the compiler....

2012-10-05 12:43:20

by Borislav Petkov

[permalink] [raw]
Subject: Re: [RFC] perf: perf_event_attr anon unions and static initializer issue

On Fri, Oct 05, 2012 at 01:49:44PM +0200, Stephane Eranian wrote:
> Yeah, unfortunately. I ended up creating a separate table for config1
> and initializing attr.config1 at runtime. That way it works regardless
> of the compiler....

Did the same thing in the persistent events patches recently:

http://marc.info/?l=linux-kernel&m=134513917126309

--
Regards/Gruss,
Boris.