[PATCH RFC 1/3] perf/core: add logic to collect off-cpu sample

From: Ajay Kaher
Date: Thu Jul 11 2024 - 08:17:24 EST


following logics has been added to collect the off-cpu sample:

- 'task_pt_regs(current)' has been used to capture registers
status off-cpu sample.

- off-cpu time represent the time period for which the target
process not occupying the cpu cycles. And calculate as:

off-cpu time = swap-in time - swap-out time

Signed-off-by: Ajay Kaher <ajay.kaher@xxxxxxxxxxxx>

---
include/linux/perf_event.h | 16 ++++++++++++++++
include/uapi/linux/perf_event.h | 3 ++-
kernel/events/core.c | 27 ++++++++++++++++++++++-----
tools/include/uapi/linux/perf_event.h | 3 ++-
4 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index a5304ae8c654..09dc3f695974 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -599,6 +599,11 @@ enum perf_event_state {
PERF_EVENT_STATE_ACTIVE = 1,
};

+enum perf_sample_cpu {
+ PERF_SAMPLE_ON_CPU,
+ PERF_SAMPLE_OFF_CPU,
+};
+
struct file;
struct perf_sample_data;

@@ -828,6 +833,7 @@ struct perf_event {
void *security;
#endif
struct list_head sb_list;
+ u64 stop_time;

/*
* Certain events gets forwarded to another pmu internally by over-
@@ -1301,6 +1307,16 @@ static inline u32 perf_sample_data_size(struct perf_sample_data *data,
return size;
}

+static inline void perf_sample_set_off_cpu(struct perf_sample_data *data)
+{
+ data->cpu_entry.reserved = PERF_SAMPLE_OFF_CPU;
+}
+
+static inline void perf_sample_set_on_cpu(struct perf_sample_data *data)
+{
+ data->cpu_entry.reserved = PERF_SAMPLE_ON_CPU;
+}
+
/*
* Clear all bitfields in the perf_branch_entry.
* The to and from fields are not cleared because they are
diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index 3a64499b0f5d..9d0a23ab2549 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -460,7 +460,8 @@ struct perf_event_attr {
inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */
remove_on_exec : 1, /* event is removed from task on exec */
sigtrap : 1, /* send synchronous SIGTRAP on event */
- __reserved_1 : 26;
+ off_cpu : 1, /* include off_cpu sample */
+ __reserved_1 : 25;

union {
__u32 wakeup_events; /* wakeup every n events */
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 8f908f077935..e71eb7936134 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -7168,10 +7168,8 @@ static void __perf_event_header__init_id(struct perf_sample_data *data,
if (sample_type & PERF_SAMPLE_STREAM_ID)
data->stream_id = event->id;

- if (sample_type & PERF_SAMPLE_CPU) {
- data->cpu_entry.cpu = raw_smp_processor_id();
- data->cpu_entry.reserved = 0;
- }
+ if (sample_type & PERF_SAMPLE_CPU)
+ data->cpu_entry.cpu = raw_smp_processor_id();
}

void perf_event_header__init_id(struct perf_event_header *header,
@@ -11083,8 +11081,8 @@ static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
return HRTIMER_NORESTART;

event->pmu->read(event);
-
perf_sample_data_init(&data, 0, event->hw.last_period);
+ perf_sample_set_on_cpu(&data);
regs = get_irq_regs();

if (regs && !perf_exclude_event(event, regs)) {
@@ -11099,6 +11097,18 @@ static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
return ret;
}

+static void perf_swevent_offCPUSample(struct perf_event *event, u64 period)
+{
+ struct perf_sample_data data;
+ struct pt_regs *regs;
+
+ event->pmu->read(event);
+ perf_sample_data_init(&data, 0, period);
+ perf_sample_set_off_cpu(&data);
+ regs = task_pt_regs(current);
+ __perf_event_overflow(event, 1, &data, regs);
+}
+
static void perf_swevent_start_hrtimer(struct perf_event *event)
{
struct hw_perf_event *hwc = &event->hw;
@@ -11107,6 +11117,11 @@ static void perf_swevent_start_hrtimer(struct perf_event *event)
if (!is_sampling_event(event))
return;

+ if (event->attr.off_cpu && event->stop_time && hwc->sample_period) {
+ perf_swevent_offCPUSample(event, perf_clock() - event->stop_time);
+ event->stop_time = 0;
+ }
+
period = local64_read(&hwc->period_left);
if (period) {
if (period < 0)
@@ -11128,6 +11143,7 @@ static void perf_swevent_cancel_hrtimer(struct perf_event *event)
ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
local64_set(&hwc->period_left, ktime_to_ns(remaining));

+ event->stop_time = perf_clock();
hrtimer_cancel(&hwc->hrtimer);
}
}
@@ -11139,6 +11155,7 @@ static void perf_swevent_init_hrtimer(struct perf_event *event)
if (!is_sampling_event(event))
return;

+ event->stop_time = 0;
hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
hwc->hrtimer.function = perf_swevent_hrtimer;

diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/linux/perf_event.h
index 3a64499b0f5d..9d0a23ab2549 100644
--- a/tools/include/uapi/linux/perf_event.h
+++ b/tools/include/uapi/linux/perf_event.h
@@ -460,7 +460,8 @@ struct perf_event_attr {
inherit_thread : 1, /* children only inherit if cloned with CLONE_THREAD */
remove_on_exec : 1, /* event is removed from task on exec */
sigtrap : 1, /* send synchronous SIGTRAP on event */
- __reserved_1 : 26;
+ off_cpu : 1, /* include off_cpu sample */
+ __reserved_1 : 25;

union {
__u32 wakeup_events; /* wakeup every n events */
--
2.39.0