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

Ajay Kaher posted 3 patches 1 year, 5 months ago
[PATCH RFC 1/3] perf/core: add logic to collect off-cpu sample
Posted by Ajay Kaher 1 year, 5 months ago
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@broadcom.com>

---
 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
Re: [PATCH RFC 1/3] perf/core: add logic to collect off-cpu sample
Posted by Peter Zijlstra 1 year, 5 months ago
On Thu, Jul 11, 2024 at 05:46:17PM +0530, Ajay Kaher wrote:
> 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
> 

I have absolutely no idea what you're trying to do :/ The above does not
constitute a comprehensible Changelog.
Re: [PATCH RFC 1/3] perf/core: add logic to collect off-cpu sample
Posted by Ajay Kaher 1 year, 5 months ago
On Fri, Jul 12, 2024 at 3:19 AM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Thu, Jul 11, 2024 at 05:46:17PM +0530, Ajay Kaher wrote:
> > 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
> >
>
> I have absolutely no idea what you're trying to do :/ The above does not
> constitute a comprehensible Changelog.

Sorry Peter, it’s sched-in/out (not swap-in/out).

'Perf record' captures on-cpu samples at frequency which is specified by
the user ( i.e. time period to collect sample is NSEC_PER_SEC / freq).

This patch is to collect the off_cpu sample and time period of off_cpu
sample is calculated based upon the time when target task was sched_out
to sched_in, as:

 off-cpu time period = sched_in time - sched-out time

-Ajay
Re: [PATCH RFC 1/3] perf/core: add logic to collect off-cpu sample
Posted by Peter Zijlstra 1 year, 5 months ago
On Sun, Jul 14, 2024 at 09:53:10PM +0530, Ajay Kaher wrote:
> On Fri, Jul 12, 2024 at 3:19 AM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > On Thu, Jul 11, 2024 at 05:46:17PM +0530, Ajay Kaher wrote:
> > > 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
> > >
> >
> > I have absolutely no idea what you're trying to do :/ The above does not
> > constitute a comprehensible Changelog.
> 
> Sorry Peter, it’s sched-in/out (not swap-in/out).
> 
> 'Perf record' captures on-cpu samples at frequency which is specified by
> the user ( i.e. time period to collect sample is NSEC_PER_SEC / freq).
> 
> This patch is to collect the off_cpu sample and time period of off_cpu
> sample is calculated based upon the time when target task was sched_out
> to sched_in, as:
> 
>  off-cpu time period = sched_in time - sched-out time

But but but... you don't need anything new for that. The sched_wakeup
tracepoint should generate an event in both the task/cpu that does the
wakeup *AND* the task being woken.

So sched_switch + sched_wakeup should get you all this already. What am
I missing?

https://lore.kernel.org/all/1342016098-213063-1-git-send-email-avagin@openvz.org/T/#u