Corrections to cpu map event encoding
Ian Rogers
reviewercover Corrections to cpu map event encoding
A mask encoding of a cpu map is laid out as:
u16 nr
u16 long_size
unsigned long mask[];
However, the mask may be 8-byte aligned meaning there is a 4-byte pad after long_size. This means 32-bit and 64-bit builds see the mask as being at different offsets. On top of this the structure is in the byte data[] encoded as:
u16 type
char data[]
This means the mask's struct isn't the required 4 or 8 byte aligned, but is offset by 2. Consequently the long reads and writes are causing undefined behavior as the alignment is broken.
These changes do minor clean up with const, visibility of functions and using the constant time max function. It then adds 32 and 64-bit mask encoding variants, packed to match current alignment. Taking the address of a packed struct leads to unaligned data, so function arguments are altered to be passed the packed struct. To compact the mask encoding further and drop the padding, the 4-byte variant is preferred. Finally a new range encoding is added, that reduces the size of the common case of a range of CPUs to a single u64.
On a 72 CPU (hyperthread) machine the original encoding of all CPUs is:0x9a98 [0x28]: event: 74
.
. ... raw event: size 40 bytes
. 0000: 4a 00 00 00 00 00 28 00 01 00 02 00 08 00 00 00 J.....(......... . 0010: 00 00 ff ff ff ff ff ff ff ff ff 00 00 00 00 00 ................ . 0020: 00 00 00 00 00 00 00 00 ........
0 0 0x9a98 [0x28]: PERF_RECORD_CPU_MAP
Using the 4-byte encoding it is:
0x9a98@pipe [0x20]: event: 74
.
. ... raw event: size 32 bytes
. 0000: 4a 00 00 00 00 00 20 00 01 00 03 00 04 00 ff ff J..... ......... . 0010: ff ff ff ff ff ff ff 00 00 00 00 00 00 00 00 00 ................
0 0 0x9a98 [0x20]: PERF_RECORD_CPU_MAP
Finally, with the range encoding it is:
0x9ab8@pipe [0x10]: event: 74
.
. ... raw event: size 16 bytes
. 0000: 4a 00 00 00 00 00 10 00 02 00 00 00 00 00 47 00 J.............G.
0 0 0x9ab8 [0x10]: PERF_RECORD_CPU_MAP
Ian Rogers (6):
perf cpumap: Const map for max
perf cpumap: Synthetic events and const/static perf cpumap: Compute mask size in constant time perf cpumap: Fix alignment for masks in event encoding perf events: Prefer union over variable length array perf cpumap: Add range data encoding
tools/lib/perf/cpumap.c | 2 +- tools/lib/perf/include/perf/cpumap.h | 2 +- tools/lib/perf/include/perf/event.h | 61 ++++++++- tools/perf/tests/cpumap.c | 71 ++++++++--- tools/perf/tests/event_update.c | 14 +-- tools/perf/util/cpumap.c | 111 +++++++++++++--- tools/perf/util/cpumap.h | 4 +- tools/perf/util/event.h | 4 - tools/perf/util/header.c | 24 ++-- tools/perf/util/session.c | 35 +++--- tools/perf/util/synthetic-events.c | 182 +++++++++++++-------------- tools/perf/util/synthetic-events.h | 2 +- 12 files changed, 327 insertions(+), 185 deletions(-)
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Ian Rogers
reviewerPATCH 1/6 perf cpumap: Const map for max
Allows max to be used with const perf_cpu_maps.
--- tools/lib/perf/cpumap.c | 2 +- tools/lib/perf/include/perf/cpumap.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
tools/lib/perf/cpumap.c +1 -1
@@ -309,7 +309,7 @@ bool perf_cpu_map__has(const struct perf_cpu_map *cpus, struct perf_cpu cpu)return perf_cpu_map__idx (cpus, cpu) !=-1 ; } -struct perf_cpu perf_cpu_map__max (struct perf_cpu_map *map) +struct perf_cpu perf_cpu_map__max (const struct perf_cpu_map *map) {struct perf_cpu result = { .cpu =-1
tools/lib/perf/include/perf/cpumap.h +1 -1
@@ -23,7 +23,7 @@ LIBPERF_API void perf_cpu_map__put(struct perf_cpu_map *map); LIBPERF_API struct perf_cpu perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx); LIBPERF_API int perf_cpu_map__nr(const struct perf_cpu_map *cpus); LIBPERF_API bool perf_cpu_map__empty(const struct perf_cpu_map *map); -LIBPERF_API struct perf_cpu perf_cpu_map__max(struct perf_cpu_map *map); +LIBPERF_API struct perf_cpu perf_cpu_map__max(const struct perf_cpu_map *map); LIBPERF_API bool perf_cpu_map__has(const struct perf_cpu_map *map, struct perf_cpu cpu);#define perf_cpu_map__for_each_cpu (cpu, idx, cpus) \
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Ian Rogers
reviewerPATCH 2/6 perf cpumap: Synthetic events and const/static
In reply to Ian RogersMake the cpumap arguments const to make it clearer they are in rather than out arguments. Make two functions static and remove external declarations.
--- tools/perf/util/event.h | 4 ---- tools/perf/util/synthetic-events.c | 20 +++++++++++--------- tools/perf/util/synthetic-events.h | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-)
tools/perf/util/event.h +0 -4
@@ -461,10 +461,6 @@ size_t perf_event__fprintf(union perf_event *event, struct machine *machine, FILint kallsyms__get_function_start (const char *kallsyms_filename,const char *symbol_name,u64 *addr); -void *cpu_map_data__alloc (struct perf_cpu_map *map,size_t *size,u16 *type,int *max); -void cpu_map_data__synthesize (struct perf_record_cpu_map_data *data,struct perf_cpu_map *map, -u16 type,int max); -void event_attr_init (struct perf_event_attr *attr);int perf_event_paranoid (void );
tools/perf/util/synthetic-events.c +11 -9
@@ -1184,7 +1184,7 @@ int perf_event__synthesize_thread_map2(struct perf_tool *tool, }static void synthesize_cpus (struct cpu_map_entries *cpus, -struct perf_cpu_map *map) +const struct perf_cpu_map *map) {int i, map_nr =perf_cpu_map__nr (map);
@@ -1195,7 +1195,7 @@ static void synthesize_cpus(struct cpu_map_entries *cpus, }static void synthesize_mask (struct perf_record_record_cpu_map *mask, -struct perf_cpu_map *map,int max) +const struct perf_cpu_map *map,int max) {int i;
@@ -1206,12 +1206,12 @@ static void synthesize_mask(struct perf_record_record_cpu_map *mask,set_bit (perf_cpu_map__cpu (map, i).cpu, mask->mask); } -static size_t cpus_size (struct perf_cpu_map *map) +static size_t cpus_size (const struct perf_cpu_map *map) {return sizeof (struct cpu_map_entries ) +perf_cpu_map__nr (map) *sizeof (u16); } -static size_t mask_size (struct perf_cpu_map *map,int *max) +static size_t mask_size (const struct perf_cpu_map *map,int *max) {int i;
@@ -1228,7 +1228,8 @@ static size_t mask_size(struct perf_cpu_map *map, int *max)return sizeof (struct perf_record_record_cpu_map ) +BITS_TO_LONGS (*max) *sizeof (long ); } -void *cpu_map_data__alloc (struct perf_cpu_map *map,size_t *size,u16 *type,int *max) +static void *cpu_map_data__alloc (const struct perf_cpu_map *map,size_t *size, +u16 *type,int *max) {size_t size_cpus, size_mask;bool is_dummy =perf_cpu_map__empty (map);
@@ -1262,8 +1263,9 @@ void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, intreturn zalloc (*size); } -void cpu_map_data__synthesize (struct perf_record_cpu_map_data *data,struct perf_cpu_map *map, -u16 type,int max) +static void cpu_map_data__synthesize (struct perf_record_cpu_map_data *data, +const struct perf_cpu_map *map, +u16 type,int max) { data->type = type;
@@ -1278,7 +1280,7 @@ void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf } } -static struct perf_record_cpu_map *cpu_map_event__new (struct perf_cpu_map *map) +static struct perf_record_cpu_map *cpu_map_event__new (const struct perf_cpu_map *map) {size_t size =sizeof (struct perf_record_cpu_map );struct perf_record_cpu_map *event;
@@ -1298,7 +1300,7 @@ static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map) }int perf_event__synthesize_cpu_map (struct perf_tool *tool, -struct perf_cpu_map *map, +const struct perf_cpu_map *map,perf_event__handler_t process,struct machine *machine) {
tools/perf/util/synthetic-events.h +1 -1
@@ -46,7 +46,7 @@ typedef int (*perf_event__handler_t)(struct perf_tool *tool, union perf_event *eint perf_event__synthesize_attrs (struct perf_tool *tool,struct evlist *evlist,perf_event__handler_t process);int perf_event__synthesize_attr (struct perf_tool *tool,struct perf_event_attr *attr,u32 ids,u64 *id,perf_event__handler_t process);int perf_event__synthesize_build_id (struct perf_tool *tool,struct dso *pos,u16 misc,perf_event__handler_t process,struct machine *machine); -int perf_event__synthesize_cpu_map (struct perf_tool *tool,struct perf_cpu_map *cpus,perf_event__handler_t process,struct machine *machine); +int perf_event__synthesize_cpu_map (struct perf_tool *tool,const struct perf_cpu_map *cpus,perf_event__handler_t process,struct machine *machine);int perf_event__synthesize_event_update_cpus (struct perf_tool *tool,struct evsel *evsel,perf_event__handler_t process);int perf_event__synthesize_event_update_name (struct perf_tool *tool,struct evsel *evsel,perf_event__handler_t process);int perf_event__synthesize_event_update_scale (struct perf_tool *tool,struct evsel *evsel,perf_event__handler_t process);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Ian Rogers
reviewerPATCH 3/6 perf cpumap: Compute mask size in constant time
In reply to Ian Rogersperf_cpu_map__max computes the cpumap's maximum value, no need to iterate over all values.
--- tools/perf/util/synthetic-events.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
tools/perf/util/synthetic-events.c +1 -12
@@ -1213,18 +1213,7 @@ static size_t cpus_size(const struct perf_cpu_map *map)static size_t mask_size (const struct perf_cpu_map *map,int *max) { -int i; - - *max =0 ; - -for (i =0 ; i <perf_cpu_map__nr (map); i++) { -/* bit position of the cpu is + 1 */ -int bit =perf_cpu_map__cpu (map, i).cpu +1 ; - -if (bit > *max) - *max = bit; - } - + *max =perf_cpu_map__max (map).cpu;return sizeof (struct perf_record_record_cpu_map ) +BITS_TO_LONGS (*max) *sizeof (long ); }
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Ian Rogers
reviewerPATCH 4/6 perf cpumap: Fix alignment for masks in event encoding
In reply to Ian RogersA mask encoding of a cpu map is laid out as:
u16 nr
u16 long_size
unsigned long mask[];
However, the mask may be 8-byte aligned meaning there is a 4-byte pad after long_size. This means 32-bit and 64-bit builds see the mask as being at different offsets. On top of this the structure is in the byte data[] encoded as:
u16 type
char data[]
This means the mask's struct isn't the required 4 or 8 byte aligned, but is offset by 2. Consequently the long reads and writes are causing undefined behavior as the alignment is broken.
Fix the mask struct by creating explicit 32 and 64-bit variants, use a union to avoid data[] and casts; the struct must be packed so the layout matches the existing perf.data layout. Taking an address of a member of a packed struct breaks alignment so pass the packed perf_record_cpu_map_data to functions, so they can access variables with the right alignment.
As the 64-bit version has 4 bytes of padding, optimizing writing to only write the 32-bit version.
--- tools/lib/perf/include/perf/event.h | 36 +++++++++++-- tools/perf/tests/cpumap.c | 19 ++++--- tools/perf/util/cpumap.c | 80 +++++++++++++++++++++++------ tools/perf/util/cpumap.h | 4 +- tools/perf/util/session.c | 30 +++++------ tools/perf/util/synthetic-events.c | 34 +++++++----- 6 files changed, 143 insertions(+), 60 deletions(-)
tools/lib/perf/include/perf/event.h +32 -4
@@ -6,6 +6,7 @@#include <linux/types.h> #include <linux/limits.h> #include <linux/bpf.h> +#include <linux/compiler.h> #include <sys/types.h> /* pid_t */ #define event_contains (obj, mem) ((obj).header.size > offsetof(typeof(obj), mem))
@@ -153,20 +154,47 @@ enum { PERF_CPU_MAP__MASK = 1, }; +/* +* Array encoding of a perf_cpu_map where nr is the number of entries in cpu[] +* and each entry is a value for a CPU in the map. +*/ struct cpu_map_entries {__u16 nr;__u16 cpu[]; }; -struct perf_record_record_cpu_map { +/* Bitmap encoding of a perf_cpu_map where bitmap entries are 32-bit. */ +struct perf_record_mask_cpu_map32 { +/* Number of mask values. */ __u16 nr; +/* Constant 4. */ __u16 long_size; -unsigned long mask[]; +/* Bitmap data. */ +__u32 mask[]; }; -struct perf_record_cpu_map_data { +/* Bitmap encoding of a perf_cpu_map where bitmap entries are 64-bit. */ +struct perf_record_mask_cpu_map64 { +/* Number of mask values. */ +__u16 nr; +/* Constant 8. */ +__u16 long_size; +/* Legacy padding. */ +char __pad[4 ]; +/* Bitmap data. */ +__u64 mask[]; +}; + +struct __packed perf_record_cpu_map_data {__u16 type; -char data[]; +union { +/* Used when type == PERF_CPU_MAP__CPUS. */ +struct cpu_map_entries cpus_data; +/* Used when type == PERF_CPU_MAP__MASK and long_size == 4. */ +struct perf_record_mask_cpu_map32 mask32_data; +/* Used when type == PERF_CPU_MAP__MASK and long_size == 8. */ +struct perf_record_mask_cpu_map64 mask64_data; + }; };struct perf_record_cpu_map {
tools/perf/tests/cpumap.c +9 -10
@@ -17,21 +17,23 @@ static int process_event_mask(struct perf_tool *tool __maybe_unused, struct machine *machine __maybe_unused) {struct perf_record_cpu_map *map_event = &event->cpu_map; -struct perf_record_record_cpu_map *mask;struct perf_record_cpu_map_data *data;struct perf_cpu_map *map;int i; +unsigned int long_size; data = &map_event->data;TEST_ASSERT_VAL ("wrong type" , data->type ==PERF_CPU_MAP__MASK ); - mask = (struct perf_record_record_cpu_map *)data->data; + long_size = data->mask32_data.long_size; -TEST_ASSERT_VAL ("wrong nr" , mask->nr ==1 ); +TEST_ASSERT_VAL ("wrong long_size" , long_size ==4 || long_size ==8 ); + +TEST_ASSERT_VAL ("wrong nr" , data->mask32_data.nr ==1 );for (i =0 ; i <20 ; i++) { -TEST_ASSERT_VAL ("wrong cpu" ,test_bit (i, mask->mask)); +TEST_ASSERT_VAL ("wrong cpu" ,perf_record_cpu_map_data__test_bit (i, data)); } map =cpu_map__new_data (data);
@@ -51,7 +53,6 @@ static int process_event_cpus(struct perf_tool *tool __maybe_unused, struct machine *machine __maybe_unused) {struct perf_record_cpu_map *map_event = &event->cpu_map; -struct cpu_map_entries *cpus;struct perf_record_cpu_map_data *data;struct perf_cpu_map *map;
@@ -59,11 +60,9 @@ static int process_event_cpus(struct perf_tool *tool __maybe_unused,TEST_ASSERT_VAL ("wrong type" , data->type ==PERF_CPU_MAP__CPUS ); - cpus = (struct cpu_map_entries *)data->data; - -TEST_ASSERT_VAL ("wrong nr" , cpus->nr ==2 ); -TEST_ASSERT_VAL ("wrong cpu" , cpus->cpu[0 ] ==1 ); -TEST_ASSERT_VAL ("wrong cpu" , cpus->cpu[1 ] ==256 ); +TEST_ASSERT_VAL ("wrong nr" , data->cpus_data.nr ==2 ); +TEST_ASSERT_VAL ("wrong cpu" , data->cpus_data.cpu[0 ] ==1 ); +TEST_ASSERT_VAL ("wrong cpu" , data->cpus_data.cpu[1 ] ==256 ); map =cpu_map__new_data (data);TEST_ASSERT_VAL ("wrong nr" ,perf_cpu_map__nr (map) ==2 );
tools/perf/util/cpumap.c +64 -16
118 lines not on this page · raw message
tools/perf/util/cpumap.h +3 -1
@@ -37,9 +37,11 @@ struct cpu_aggr_map {struct perf_record_cpu_map_data ; +bool perf_record_cpu_map_data__test_bit (int i,const struct perf_record_cpu_map_data *data); +struct perf_cpu_map *perf_cpu_map__empty_new (int nr); -struct perf_cpu_map *cpu_map__new_data (struct perf_record_cpu_map_data *data); +struct perf_cpu_map *cpu_map__new_data (const struct perf_record_cpu_map_data *data);size_t cpu_map__snprint (struct perf_cpu_map *map,char *buf,size_t size);size_t cpu_map__snprint_mask (struct perf_cpu_map *map,char *buf,size_t size);size_t cpu_map__fprintf (struct perf_cpu_map *map,FILE *fp);
tools/perf/util/session.c +15 -15
@@ -914,30 +914,30 @@ static void perf_event__cpu_map_swap(union perf_event *event, bool sample_id_all __maybe_unused) {struct perf_record_cpu_map_data *data = &event->cpu_map.data; -struct cpu_map_entries *cpus; -struct perf_record_record_cpu_map *mask; -unsigned i; data->type =bswap_16 (data->type);switch (data->type) {case PERF_CPU_MAP__CPUS : - cpus = (struct cpu_map_entries *)data->data; - - cpus->nr =bswap_16 (cpus->nr); + data->cpus_data.nr =bswap_16 (data->cpus_data.nr); -for (i =0 ; i < cpus->nr; i++) - cpus->cpu[i] =bswap_16 (cpus->cpu[i]); +for (unsigned i =0 ; i < data->cpus_data.nr; i++) + data->cpus_data.cpu[i] =bswap_16 (data->cpus_data.cpu[i]);break ;case PERF_CPU_MAP__MASK : - mask = (struct perf_record_record_cpu_map *)data->data; - - mask->nr =bswap_16 (mask->nr); - mask->long_size =bswap_16 (mask->long_size); + data->mask32_data.long_size =bswap_16 (data->mask32_data.long_size); -switch (mask->long_size) { -case 4 :mem_bswap_32 (&mask->mask, mask->nr);break ; -case 8 :mem_bswap_64 (&mask->mask, mask->nr);break ; +switch (data->mask32_data.long_size) { +case 4 : + data->mask32_data.nr =bswap_16 (data->mask32_data.nr); +for (unsigned i =0 ; i < data->mask32_data.nr; i++) + data->mask32_data.mask[i] =bswap_32 (data->mask32_data.mask[i]); +break ; +case 8 : + data->mask64_data.nr =bswap_16 (data->mask64_data.nr); +for (unsigned i =0 ; i < data->mask64_data.nr; i++) + data->mask64_data.mask[i] =bswap_64 (data->mask64_data.mask[i]); +break ;default :pr_err ("cpu_map swap: unsupported long size\n" ); }
tools/perf/util/synthetic-events.c +20 -14
@@ -1183,27 +1183,33 @@ int perf_event__synthesize_thread_map2(struct perf_tool *tool,return err; } -static void synthesize_cpus (struct cpu_map_entries *cpus, +static void synthesize_cpus (struct perf_record_cpu_map_data *data,const struct perf_cpu_map *map) {int i, map_nr =perf_cpu_map__nr (map); - cpus->nr = map_nr; + data->cpus_data.nr = map_nr;for (i =0 ; i < map_nr; i++) - cpus->cpu[i] =perf_cpu_map__cpu (map, i).cpu; + data->cpus_data.cpu[i] =perf_cpu_map__cpu (map, i).cpu; } -static void synthesize_mask (struct perf_record_record_cpu_map *mask, +static void synthesize_mask (struct perf_record_cpu_map_data *data,const struct perf_cpu_map *map,int max) { -int i; +int idx; +struct perf_cpu cpu; + +/* Due to padding, the 4bytes per entry mask variant is always smaller. */ + data->mask32_data.nr =BITS_TO_U32 (max); + data->mask32_data.long_size =4 ; - mask->nr =BITS_TO_LONGS (max); - mask->long_size =sizeof (long ); +perf_cpu_map__for_each_cpu (cpu, idx, map) { +int bit_word = cpu.cpu /32 ; +__u32 bit_mask =1U << (cpu.cpu &31 ); -for (i =0 ; i <perf_cpu_map__nr (map); i++) -set_bit (perf_cpu_map__cpu (map, i).cpu, mask->mask); + data->mask32_data.mask[bit_word] |= bit_mask; + } }static size_t cpus_size (const struct perf_cpu_map *map)
@@ -1214,7 +1220,7 @@ static size_t cpus_size(const struct perf_cpu_map *map)static size_t mask_size (const struct perf_cpu_map *map,int *max) { *max =perf_cpu_map__max (map).cpu; -return sizeof (struct perf_record_record_cpu_map ) +BITS_TO_LONGS (*max) *sizeof (long ); +return sizeof (struct perf_record_mask_cpu_map32 ) +BITS_TO_U32 (*max) *sizeof (__u32); } static void *cpu_map_data__alloc(const struct perf_cpu_map *map, size_t *size,
@@ -1247,7 +1253,7 @@ static void *cpu_map_data__alloc(const struct perf_cpu_map *map, size_t *size, *type =PERF_CPU_MAP__MASK ; } - *size +=sizeof (struct perf_record_cpu_map_data ); + *size +=sizeof (__u16);/* For perf_record_cpu_map_data.type. */ *size =PERF_ALIGN (*size,sizeof (u64));return zalloc (*size); }
@@ -1260,10 +1266,10 @@ static void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data,switch (type) {case PERF_CPU_MAP__CPUS : -synthesize_cpus ((struct cpu_map_entries *) data->data, map); +synthesize_cpus (data, map);break ;case PERF_CPU_MAP__MASK : -synthesize_mask ((struct perf_record_record_cpu_map *)data->data, map, max); +synthesize_mask (data, map, max);default :break ; }
@@ -1271,7 +1277,7 @@ static void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data,static struct perf_record_cpu_map *cpu_map_event__new (const struct perf_cpu_map *map) { -size_t size =sizeof (struct perf_record_cpu_map ); +size_t size =sizeof (struct perf_event_header );struct perf_record_cpu_map *event;int max;u16 type;
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Ian Rogers
reviewerPATCH 5/6 perf events: Prefer union over variable length array
In reply to Ian RogersIt is possible for casts to introduce alignment issues, prefer a union for perf_record_event_update.
--- tools/lib/perf/include/perf/event.h | 11 ++++++++++- tools/perf/tests/event_update.c | 14 ++++---------- tools/perf/util/header.c | 24 ++++++++---------------- tools/perf/util/synthetic-events.c | 12 +++++------- 4 files changed, 27 insertions(+), 34 deletions(-)
tools/lib/perf/include/perf/event.h +10 -1
@@ -221,7 +221,16 @@ struct perf_record_event_update {struct perf_event_header header;__u64 type;__u64 id; -char data[]; +union { +/* Used when type == PERF_EVENT_UPDATE__SCALE. */ +struct perf_record_event_update_scale scale; +/* Used when type == PERF_EVENT_UPDATE__UNIT. */ +char unit[0 ]; +/* Used when type == PERF_EVENT_UPDATE__NAME. */ +char name[0 ]; +/* Used when type == PERF_EVENT_UPDATE__CPUS. */ +struct perf_record_event_update_cpus cpus; + }; };#define MAX_EVENT_NAME 64
tools/perf/tests/event_update.c +4 -10
@@ -21,7 +21,7 @@ static int process_event_unit(struct perf_tool *tool __maybe_unused,TEST_ASSERT_VAL ("wrong id" , ev->id ==123 );TEST_ASSERT_VAL ("wrong id" , ev->type ==PERF_EVENT_UPDATE__UNIT ); -TEST_ASSERT_VAL ("wrong unit" , !strcmp (ev->data,"KRAVA" )); +TEST_ASSERT_VAL ("wrong unit" , !strcmp (ev->unit,"KRAVA" ));return 0 ; }
@@ -31,13 +31,10 @@ static int process_event_scale(struct perf_tool *tool __maybe_unused, struct machine *machine __maybe_unused) {struct perf_record_event_update *ev = (struct perf_record_event_update *)event; -struct perf_record_event_update_scale *ev_data; - - ev_data = (struct perf_record_event_update_scale *)ev->data;TEST_ASSERT_VAL ("wrong id" , ev->id ==123 );TEST_ASSERT_VAL ("wrong id" , ev->type ==PERF_EVENT_UPDATE__SCALE ); -TEST_ASSERT_VAL ("wrong scale" , ev_data->scale ==0.123 ); +TEST_ASSERT_VAL ("wrong scale" , ev->scale.scale ==0.123 );return 0 ; }
@@ -56,7 +53,7 @@ static int process_event_name(struct perf_tool *tool,TEST_ASSERT_VAL ("wrong id" , ev->id ==123 );TEST_ASSERT_VAL ("wrong id" , ev->type ==PERF_EVENT_UPDATE__NAME ); -TEST_ASSERT_VAL ("wrong name" , !strcmp (ev->data, tmp->name)); +TEST_ASSERT_VAL ("wrong name" , !strcmp (ev->name, tmp->name));return 0 ; }
@@ -66,12 +63,9 @@ static int process_event_cpus(struct perf_tool *tool __maybe_unused, struct machine *machine __maybe_unused) {struct perf_record_event_update *ev = (struct perf_record_event_update *)event; -struct perf_record_event_update_cpus *ev_data;struct perf_cpu_map *map; - ev_data = (struct perf_record_event_update_cpus *) ev->data; - - map =cpu_map__new_data (&ev_data->cpus); + map =cpu_map__new_data (&ev->cpus.cpus);TEST_ASSERT_VAL ("wrong id" , ev->id ==123 );TEST_ASSERT_VAL ("wrong type" , ev->type ==PERF_EVENT_UPDATE__CPUS );
tools/perf/util/header.c +8 -16
@@ -4264,8 +4264,6 @@ int perf_event__process_feature(struct perf_session *session,size_t perf_event__fprintf_event_update (union perf_event *event,FILE *fp) {struct perf_record_event_update *ev = &event->event_update; -struct perf_record_event_update_scale *ev_scale; -struct perf_record_event_update_cpus *ev_cpus;struct perf_cpu_map *map;size_t ret;
@@ -4273,20 +4271,18 @@ size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)switch (ev->type) {case PERF_EVENT_UPDATE__SCALE : - ev_scale = (struct perf_record_event_update_scale *)ev->data; - ret +=fprintf (fp,"... scale: %f\n" , ev_scale->scale); + ret +=fprintf (fp,"... scale: %f\n" , ev->scale.scale);break ;case PERF_EVENT_UPDATE__UNIT : - ret +=fprintf (fp,"... unit: %s\n" , ev->data); + ret +=fprintf (fp,"... unit: %s\n" , ev->unit);break ;case PERF_EVENT_UPDATE__NAME : - ret +=fprintf (fp,"... name: %s\n" , ev->data); + ret +=fprintf (fp,"... name: %s\n" , ev->name);break ;case PERF_EVENT_UPDATE__CPUS : - ev_cpus = (struct perf_record_event_update_cpus *)ev->data; ret +=fprintf (fp,"... " ); - map =cpu_map__new_data (&ev_cpus->cpus); + map =cpu_map__new_data (&ev->cpus.cpus);if (map) ret +=cpu_map__fprintf (map, fp);else
@@ -4343,8 +4339,6 @@ int perf_event__process_event_update(struct perf_tool *tool __maybe_unused, struct evlist **pevlist) {struct perf_record_event_update *ev = &event->event_update; -struct perf_record_event_update_scale *ev_scale; -struct perf_record_event_update_cpus *ev_cpus;struct evlist *evlist;struct evsel *evsel;struct perf_cpu_map *map;
@@ -4361,19 +4355,17 @@ int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,switch (ev->type) {case PERF_EVENT_UPDATE__UNIT :free ((char *)evsel->unit); - evsel->unit =strdup (ev->data); + evsel->unit =strdup (ev->unit);break ;case PERF_EVENT_UPDATE__NAME :free (evsel->name); - evsel->name =strdup (ev->data); + evsel->name =strdup (ev->name);break ;case PERF_EVENT_UPDATE__SCALE : - ev_scale = (struct perf_record_event_update_scale *)ev->data; - evsel->scale = ev_scale->scale; + evsel->scale = ev->scale.scale;break ;case PERF_EVENT_UPDATE__CPUS : - ev_cpus = (struct perf_record_event_update_cpus *)ev->data; - map =cpu_map__new_data (&ev_cpus->cpus); + map =cpu_map__new_data (&ev->cpus.cpus);if (map) {perf_cpu_map__put (evsel->core.own_cpus); evsel->core.own_cpus = map;
tools/perf/util/synthetic-events.c +5 -7
@@ -1848,7 +1848,7 @@ int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evseif (ev ==NULL )return -ENOMEM ; -strlcpy (ev->data, evsel->unit, size +1 ); +strlcpy (ev->unit, evsel->unit, size +1 ); err =process (tool, (union perf_event *)ev,NULL ,NULL );free (ev);return err;
@@ -1865,8 +1865,7 @@ int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsif (ev ==NULL )return -ENOMEM ; - ev_data = (struct perf_record_event_update_scale *)ev->data; - ev_data->scale = evsel->scale; + ev->scale.scale = evsel->scale; err =process (tool, (union perf_event *)ev,NULL ,NULL );free (ev);return err;
@@ -1883,7 +1882,7 @@ int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evseif (ev ==NULL )return -ENOMEM ; -strlcpy (ev->data, evsel->name, len +1 ); +strlcpy (ev->name, evsel->name, len +1 ); err =process (tool, (union perf_event *)ev,NULL ,NULL );free (ev);return err;
@@ -1892,7 +1891,7 @@ int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evseint perf_event__synthesize_event_update_cpus (struct perf_tool *tool,struct evsel *evsel,perf_event__handler_t process) { -size_t size =sizeof (struct perf_record_event_update ); +size_t size =sizeof (struct perf_event_header ) +sizeof (u64) +sizeof (u64);struct perf_record_event_update *ev;int max, err;u16 type;
@@ -1909,8 +1908,7 @@ int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evse ev->type =PERF_EVENT_UPDATE__CPUS ; ev->id = evsel->core.id[0 ]; -cpu_map_data__synthesize ((struct perf_record_cpu_map_data *)ev->data, - evsel->core.own_cpus, type, max); +cpu_map_data__synthesize (&ev->cpus.cpus, evsel->core.own_cpus, type, max); err =process (tool, (union perf_event *)ev,NULL ,NULL );free (ev);
Signature · 1 line
2.36.1.476.g0c4daa206d-goog
Ian Rogers
reviewerPATCH 6/6 perf cpumap: Add range data encoding
In reply to Ian RogersOften cpumaps encode a range of all CPUs, add a compact encoding that doesn't require a bit mask or list of all CPUs.
--- tools/lib/perf/include/perf/event.h | 14 +++ tools/perf/tests/cpumap.c | 52 ++++++++-- tools/perf/util/cpumap.c | 31 +++++- tools/perf/util/session.c | 5 + tools/perf/util/synthetic-events.c | 151 ++++++++++++++-------------- 5 files changed, 166 insertions(+), 87 deletions(-)
tools/lib/perf/include/perf/event.h +14 -0
@@ -152,6 +152,7 @@ struct perf_record_header_attr {enum {PERF_CPU_MAP__CPUS =0 ,PERF_CPU_MAP__MASK =1 , +PERF_CPU_MAP__RANGE_CPUS =2 , };/*
@@ -185,6 +186,17 @@ struct perf_record_mask_cpu_map64 {__u64 mask[]; }; +/* +* An encoding of a CPU map for a range starting at start_cpu through to +* end_cpu. If any_cpu is 1, an any CPU (-1) value (aka dummy value) is present. +*/ +struct perf_record_range_cpu_map { +__u8 any_cpu; +__u8 __pad; +__u16 start_cpu; +__u16 end_cpu; +}; +struct __packed perf_record_cpu_map_data {__u16 type;union {
@@ -194,6 +206,8 @@ struct __packed perf_record_cpu_map_data {struct perf_record_mask_cpu_map32 mask32_data;/* Used when type == PERF_CPU_MAP__MASK and long_size == 8. */ struct perf_record_mask_cpu_map64 mask64_data; +/* Used when type == PERF_CPU_MAP__RANGE_CPUS. */ +struct perf_record_range_cpu_map range_cpu_data; }; };
tools/perf/tests/cpumap.c +43 -9
91 lines not on this page · raw message
tools/perf/util/cpumap.c +29 -2
@@ -112,12 +112,39 @@ static struct perf_cpu_map *cpu_map__from_mask(const struct perf_record_cpu_map_ } +static struct perf_cpu_map *cpu_map__from_range (const struct perf_record_cpu_map_data *data) +{ +struct perf_cpu_map *map; +unsigned int i =0 ; + + map =perf_cpu_map__empty_new (data->range_cpu_data.end_cpu - + data->range_cpu_data.start_cpu +1 + data->range_cpu_data.any_cpu); +if (!map) +return NULL ; + +if (data->range_cpu_data.any_cpu) + map->map[i++].cpu =-1 ; + +for (int cpu = data->range_cpu_data.start_cpu; cpu <= data->range_cpu_data.end_cpu; + i++, cpu++) + map->map[i].cpu = cpu; + +return map; +} +struct perf_cpu_map *cpu_map__new_data (const struct perf_record_cpu_map_data *data) { -if (data->type ==PERF_CPU_MAP__CPUS ) +switch (data->type) { +case PERF_CPU_MAP__CPUS :return cpu_map__from_entries (data); -else +case PERF_CPU_MAP__MASK :return cpu_map__from_mask (data); +case PERF_CPU_MAP__RANGE_CPUS : +return cpu_map__from_range (data); +default : +pr_err ("cpu_map__new_data unknown type %d\n" , data->type); +return NULL ; + } }size_t cpu_map__fprintf (struct perf_cpu_map *map,FILE *fp)
tools/perf/util/session.c +5 -0
@@ -941,6 +941,11 @@ static void perf_event__cpu_map_swap(union perf_event *event,default :pr_err ("cpu_map swap: unsupported long size\n" ); } +break ; +case PERF_CPU_MAP__RANGE_CPUS : + data->range_cpu_data.start_cpu =bswap_16 (data->range_cpu_data.start_cpu); + data->range_cpu_data.end_cpu =bswap_16 (data->range_cpu_data.end_cpu); +break ;default :break ; }
tools/perf/util/synthetic-events.c +75 -76
215 lines not on this page · raw message
Signature · 1 line
2.36.1.476.g0c4daa206d-goog