perf/core: Add an ioctl to get a number of lost samples
Namhyung Kim
maintainerRFC 1/1 perf/core: Add an ioctl to get a number of lost samples
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty PERF_RECORD_LOST is generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.
Add event->lost_samples field and PERF_EVENT_IOC_LOST_SAMPLES to retrieve it from userspace.
--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)
include/linux/perf_event.h +2 -0
@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;
include/uapi/linux/perf_event.h +1 -0
@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)enum perf_event_ioc_flags {PERF_IOC_FLAG_GROUP =1U <<0 ,
kernel/events/core.c +9 -0
@@ -5664,6 +5664,15 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lonreturn perf_event_modify_attr (event, &new_attr); } + +case PERF_EVENT_IOC_LOST_SAMPLES : { +u64 lost =atomic_read (&event->lost_samples); + +if (copy_to_user ((void __user *)arg, &lost,sizeof (lost))) +return -EFAULT ; +return 0 ; + } +default :return -ENOTTY ; }
kernel/events/ring_buffer.c +4 -1
@@ -172,8 +172,10 @@ __perf_output_begin(struct perf_output_handle *handle, goto out;if (unlikely (rb->paused)) { -if (rb->nr_pages) +if (rb->nr_pages) {local_inc (&rb->lost); +atomic_inc (&event->lost_samples); + } goto out; }
@@ -254,6 +256,7 @@ __perf_output_begin(struct perf_output_handle *handle, fail:local_inc (&rb->lost); +atomic_inc (&event->lost_samples);perf_output_put_handle (handle); out:rcu_read_unlock ();
Signature · 1 line
2.32.0.605.g8dce9f2422-goog
include/linux/perf_event.h struct perf_event { · line 759
Quoted 10 lines
include/linux/perf_event.h +1
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples;
Would rather use atomic64_t. atomic_t might wrap too quickly.
But it might be better to put it somewhere where you already have a lock on the event, then you wouldn't need an atomic.
-Andi
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0
include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)
would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/
that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
jirka
Quoted 50 lines
include/uapi/linux/perf_event.h +0
-0enum perf_event_ioc_flags {PERF_IOC_FLAG_GROUP =1U <<0 ,kernel/events/core.c +9
-0@@ -5664,6 +5664,15 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lonreturn perf_event_modify_attr (event, &new_attr); } + +case PERF_EVENT_IOC_LOST_SAMPLES : { +u64 lost =atomic_read (&event->lost_samples); + +if (copy_to_user ((void __user *)arg, &lost,sizeof (lost))) +return -EFAULT ; +return 0 ; + } +default :return -ENOTTY ; }kernel/events/ring_buffer.c +4
-1@@ -172,8 +172,10 @@ __perf_output_begin(struct perf_output_handle *handle, goto out;if (unlikely (rb->paused)) { -if (rb->nr_pages) +if (rb->nr_pages) {local_inc (&rb->lost); +atomic_inc (&event->lost_samples); + } goto out; }@@ -254,6 +256,7 @@ __perf_output_begin(struct perf_output_handle *handle, fail:local_inc (&rb->lost); +atomic_inc (&event->lost_samples);perf_output_put_handle (handle); out:rcu_read_unlock ();Signature · 1 line
2.32.0.605.g8dce9f2422-goog
Quoted 44 lines · Jiri Olsa
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/
Quoted 4 lines
that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
Does the read format approach succeed even when the event is in error state?
Quoted 53 lines
jirka
Quoted 50 lines
include/uapi/linux/perf_event.h +0
-0enum perf_event_ioc_flags {PERF_IOC_FLAG_GROUP =1U <<0 ,kernel/events/core.c +9
-0@@ -5664,6 +5664,15 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lonreturn perf_event_modify_attr (event, &new_attr); } + +case PERF_EVENT_IOC_LOST_SAMPLES : { +u64 lost =atomic_read (&event->lost_samples); + +if (copy_to_user ((void __user *)arg, &lost,sizeof (lost))) +return -EFAULT ; +return 0 ; + } +default :return -ENOTTY ; }kernel/events/ring_buffer.c +4
-2@@ -172,8 +172,10 @@ __perf_output_begin(struct perf_output_handle *handle, goto out;if (unlikely (rb->paused)) { -if (rb->nr_pages) +if (rb->nr_pages) {local_inc (&rb->lost); +atomic_inc (&event->lost_samples); + } goto out; }@@ -254,6 +256,7 @@ __perf_output_begin(struct perf_output_handle *handle, fail:local_inc (&rb->lost); +atomic_inc (&event->lost_samples);perf_output_put_handle (handle); out:rcu_read_unlock (); --2.32.0.605.g8dce9f2422-goog
Quoted 45 lines · Stephane Eranian
Quoted 44 lines · Jiri Olsa
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/
Quoted 5 lines
Quoted 4 lines
that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
Does the read format approach succeed even when the event is in error state?
nope..
/*
* Return end-of-file for a read on an event that is in
* error state (i.e. because it was pinned but it couldn't be
* scheduled on to the CPU at some point).
*/
if (event->state == PERF_EVENT_STATE_ERROR)
return 0;jirka
Namhyung Kim
maintainerQuoted 55 lines · Jiri Olsa
Quoted 45 lines · Stephane Eranian
Quoted 44 lines · Jiri Olsa
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/Quoted 5 lines
Quoted 4 lines
that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
Does the read format approach succeed even when the event is in error state?
nope..
Quoted 8 lines
/*
* Return end-of-file for a read on an event that is in
* error state (i.e. because it was pinned but it couldn't be
* scheduled on to the CPU at some point).
*/
if (event->state == PERF_EVENT_STATE_ERROR)
return 0;By the way, it'd be nice if the kernel would provide a way for better error reporting. There are many cases return -EINVAL and it's hard to know what's the problem exactly.
Thanks,
Namhyung
Quoted 66 lines · Namhyung Kim
Quoted 55 lines · Jiri Olsa
Quoted 45 lines · Stephane Eranian
Quoted 44 lines · Jiri Olsa
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/Quoted 5 lines
Quoted 4 lines
that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
Does the read format approach succeed even when the event is in error state?
nope..
Quoted 8 lines
/* * Return end-of-file for a read on an event that is in * error state (i.e. because it was pinned but it couldn't be * scheduled on to the CPU at some point). */ if (event->state == PERF_EVENT_STATE_ERROR) return 0;
Quoted 4 lines
By the way, it'd be nice if the kernel would provide a way for better error reporting. There are many cases return
-EINVALand it's hard to know what's the problem exactly.
+1. If the kernel has more precise error reporting, then the tool could be more helpful in its error reporting and not try to guess with heuristics.
Namhyung Kim
maintainerHi Jiri,
Quoted 44 lines · Jiri Olsa
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/
Quoted 3 lines
that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
Oh, right... I forgot about that, sorry.
But I think the lost count is not collected accurately.
Peter, what do you think about the interface (read vs ioctl)?
Thanks,
Namhyung
Peter Zijlstra
maintainerQuoted 47 lines · Namhyung Kim
Hi Jiri,
Quoted 44 lines · Jiri Olsa
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/
Quoted 8 lines
Quoted 3 lines
that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
Oh, right... I forgot about that, sorry.
But I think the lost count is not collected accurately.Peter, what do you think about the interface (read vs ioctl)?
I think I'm the one that suggested PERF_FORMAT_LOST at the time :-)
Namhyung Kim
maintainerHi Peter,
Quoted 53 lines · Peter Zijlstra
Quoted 51 lines · Namhyung Kim
Hi Jiri,
Quoted 47 lines · Jiri Olsa
Quoted 34 lines · Namhyung Kim
Sometimes we want to know an accurate number of samples even if it's lost. Currenlty
PERF_RECORD_LOSTis generated for a ring-buffer which might be shared with other events. So it's hard to know per-event lost count.Add
event->lost_samplesfield andPERF_EVENT_IOC_LOST_SAMPLESto retrieve it from userspace.Signed-off-by Namhyung Kim <[email protected]>--- include/linux/perf_event.h | 2 ++ include/uapi/linux/perf_event.h | 1 + kernel/events/core.c | 9 +++++++++ kernel/events/ring_buffer.c | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-)include/linux/perf_event.h +2
-0@@ -756,6 +756,8 @@ struct perf_event {struct pid_namespace *ns;u64 id; +atomic_t lost_samples; +u64 (*clock)(void);perf_overflow_handler_t overflow_handler;void *overflow_handler_context;include/uapi/linux/perf_event.h +0
-0include/uapi/linux/perf_event.h struct perf_event_query_bpf { · line 508
Quoted 4 lines
include/uapi/linux/perf_event.h +1
-0@@ -505,6 +505,7 @@ struct perf_event_query_bpf {#define PERF_EVENT_IOC_PAUSE_OUTPUT _IOW('$', 9, __u32)#define PERF_EVENT_IOC_QUERY_BPF _IOWR('$', 10, struct perf_event_query_bpf *)#define PERF_EVENT_IOC_MODIFY_ATTRIBUTES _IOW('$', 11, struct perf_event_attr *) +#define PERF_EVENT_IOC_LOST_SAMPLES _IOR('$', 12, __u64 *)would it be better to use the read syscall for that?
https://lore.kernel.org/lkml/[email protected]/that patchset ended up on me not having a way to reproduce the issue you guys wanted the fix for ;-) the lost count is there as well
Quoted 6 lines
Quoted 4 lines
Oh, right... I forgot about that, sorry.
But I think the lost count is not collected accurately.Peter, what do you think about the interface (read vs ioctl)?
I think I'm the one that suggested
PERF_FORMAT_LOSTat the time :-)
Ah, ok. I'm fine with the read format then.
Thanks,
Namhyung