[PATCH 03/10] perf: Add new read_format bit to read build id faults

From: Jiri Olsa
Date: Tue Jun 22 2021 - 11:39:42 EST


It's now possible to retrieve build id faults stats by read
syscall on events with perf_event_attr::build_id set.

Adding PERF_FORMAT_BUILD_ID_FAULTS read_format bit to get
the value of build_id_faults through the read syscall.

Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
include/uapi/linux/perf_event.h | 17 ++++++++++-------
kernel/events/core.c | 21 ++++++++++++++++++---
2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index f92880a15645..2424ba7f95fb 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -296,16 +296,18 @@ enum {
*
* struct read_format {
* { u64 value;
- * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED
- * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING
- * { u64 id; } && PERF_FORMAT_ID
+ * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED
+ * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING
+ * { u64 id; } && PERF_FORMAT_ID
+ * { u64 build_id_faults; } && PERF_FORMAT_BUILD_ID_FAULTS
* } && !PERF_FORMAT_GROUP
*
* { u64 nr;
- * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED
- * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING
+ * { u64 time_enabled; } && PERF_FORMAT_TOTAL_TIME_ENABLED
+ * { u64 time_running; } && PERF_FORMAT_TOTAL_TIME_RUNNING
* { u64 value;
- * { u64 id; } && PERF_FORMAT_ID
+ * { u64 id; } && PERF_FORMAT_ID
+ * { u64 build_id_faults; } && PERF_FORMAT_BUILD_ID_FAULTS
* } cntr[nr];
* } && PERF_FORMAT_GROUP
* };
@@ -315,8 +317,9 @@ enum perf_event_read_format {
PERF_FORMAT_TOTAL_TIME_RUNNING = 1U << 1,
PERF_FORMAT_ID = 1U << 2,
PERF_FORMAT_GROUP = 1U << 3,
+ PERF_FORMAT_BUILD_ID_FAULTS = 1U << 4,

- PERF_FORMAT_MAX = 1U << 4, /* non-ABI */
+ PERF_FORMAT_MAX = 1U << 5, /* non-ABI */
};

#define PERF_ATTR_SIZE_VER0 64 /* sizeof first published struct */
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 3f1630c06195..f3cd06012115 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1840,6 +1840,9 @@ static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
if (event->attr.read_format & PERF_FORMAT_ID)
entry += sizeof(u64);

+ if (event->attr.read_format & PERF_FORMAT_BUILD_ID_FAULTS)
+ entry += sizeof(u64);
+
if (event->attr.read_format & PERF_FORMAT_GROUP) {
nr += nr_siblings;
size += sizeof(u64);
@@ -5247,11 +5250,15 @@ static int __perf_read_group_add(struct perf_event *leader,
values[n++] += perf_event_count(leader);
if (read_format & PERF_FORMAT_ID)
values[n++] = primary_event_id(leader);
+ if (read_format & PERF_FORMAT_BUILD_ID_FAULTS)
+ values[n++] = local64_read(&leader->build_id_faults);

for_each_sibling_event(sub, leader) {
values[n++] += perf_event_count(sub);
if (read_format & PERF_FORMAT_ID)
values[n++] = primary_event_id(sub);
+ if (read_format & PERF_FORMAT_BUILD_ID_FAULTS)
+ values[n++] = local64_read(&sub->build_id_faults);
}

raw_spin_unlock_irqrestore(&ctx->lock, flags);
@@ -5308,7 +5315,7 @@ static int perf_read_one(struct perf_event *event,
u64 read_format, char __user *buf)
{
u64 enabled, running;
- u64 values[4];
+ u64 values[5];
int n = 0;

values[n++] = __perf_event_read_value(event, &enabled, &running);
@@ -5318,6 +5325,8 @@ static int perf_read_one(struct perf_event *event,
values[n++] = running;
if (read_format & PERF_FORMAT_ID)
values[n++] = primary_event_id(event);
+ if (read_format & PERF_FORMAT_BUILD_ID_FAULTS)
+ values[n++] = local64_read(&event->build_id_faults);

if (copy_to_user(buf, values, n * sizeof(u64)))
return -EFAULT;
@@ -6820,7 +6829,7 @@ static void perf_output_read_one(struct perf_output_handle *handle,
u64 enabled, u64 running)
{
u64 read_format = event->attr.read_format;
- u64 values[4];
+ u64 values[5];
int n = 0;

values[n++] = perf_event_count(event);
@@ -6834,6 +6843,8 @@ static void perf_output_read_one(struct perf_output_handle *handle,
}
if (read_format & PERF_FORMAT_ID)
values[n++] = primary_event_id(event);
+ if (read_format & PERF_FORMAT_BUILD_ID_FAULTS)
+ values[n++] = local64_read(&event->build_id_faults);

__output_copy(handle, values, n * sizeof(u64));
}
@@ -6844,7 +6855,7 @@ static void perf_output_read_group(struct perf_output_handle *handle,
{
struct perf_event *leader = event->group_leader, *sub;
u64 read_format = event->attr.read_format;
- u64 values[5];
+ u64 values[6];
int n = 0;

values[n++] = 1 + leader->nr_siblings;
@@ -6862,6 +6873,8 @@ static void perf_output_read_group(struct perf_output_handle *handle,
values[n++] = perf_event_count(leader);
if (read_format & PERF_FORMAT_ID)
values[n++] = primary_event_id(leader);
+ if (read_format & PERF_FORMAT_BUILD_ID_FAULTS)
+ values[n++] = local64_read(&leader->build_id_faults);

__output_copy(handle, values, n * sizeof(u64));

@@ -6875,6 +6888,8 @@ static void perf_output_read_group(struct perf_output_handle *handle,
values[n++] = perf_event_count(sub);
if (read_format & PERF_FORMAT_ID)
values[n++] = primary_event_id(sub);
+ if (read_format & PERF_FORMAT_BUILD_ID_FAULTS)
+ values[n++] = local64_read(&sub->build_id_faults);

__output_copy(handle, values, n * sizeof(u64));
}
--
2.31.1