Expose gpu counters via perf pmu driver
Subsystems (4)
cover Expose gpu counters via perf pmu driver
Although I haven't seen any precedent for drivers using perf pmus to expose device metrics, I've been experimenting with exposing some of the performance counters of Intel Gen graphics hardware recently and looking to see if it makes sense to build on the perf infrastructure for our use cases.
I've got a basic pmu driver working to expose our Observation Architecture counters, and it seems like a fairly good fit. The main caveat is that to allow the permission model we would like I needed to make some changes in events/core which I'd really appreciate some feedback on...
In this case we're using the driver to support some performance monitoring extensions in Mesa (AMD_performance_monitor + INTEL_performance_query) and we don't want to require OpenGL clients to run as root to be able to monitor a gpu context they own.
Our desired permission model seems consistent with perf's current model whereby you would need privileges if you want to profile across all gpu contexts but not need special permissions to profile your own context.
The awkward part is that it doesn't make sense for us to have userspace open a perf event with a specific pid as the way to avoid needing root permissions because a side effect of doing this is that the events will be dynamically added/deleted so as to only monitor while that process is scheduled and that's not really meaningful when we're monitoring the gpu.
Conceptually I suppose we want to be able to open an event that's not associated with any cpu or process, but to keep things simple and fit with perf's current design, the pmu I have a.t.m expects an event to be opened for a specific cpu and unspecified process.
To then subvert the cpu centric permission checks, I added a PERF_PMU_CAP_IS_DEVICE capability that a pmu driver can use to tell events/core that a pmu doesn't collect any cpu metrics and it can therefore skip its usual checks and assume the driver will implement its own checks as appropriate.
In addition I also explicitly black list numerous attributes and PERF_SAMPLE_ flags that I don't think make sense for a device pmu. This could be handled in the pmu driver but it seemed better to do in events/core, avoiding duplication in case we later have multiple device pmus.
I'd be interested to hear whether is sounds reasonable to others for us to expose gpu device metrics via a perf pmu and whether adding the PERF_PMU_CAP_IS_DEVICE flag as in my following patch could be acceptable.
Patches:
[RFC PATCH 1/3] perf: export perf_event_overflow
[RFC PATCH 2/3] perf: Add PERF_PMU_CAP_IS_DEVICE flag
The main change to core/events I'd really appreciate feedback on.
[RFC PATCH 3/3] i915: Expose PMU for Observation Architecture
My current pmu driver, provided for context (work in progress). Early, high-level, feedback would be appreciated, though I think it could be good to focus on the core/events change first. I also plan to send this to the intel-gfx list for review.
Essentially, this pmu allows us to configure the gpu to periodically write snapshots of performance counters (up to 64 32bit counters per snapshot) into a circular buffer. It then uses a 200Hz hrtimer to forward those snapshots to userspace as perf samples, with the counter snapshots written by the gpu attached as 'raw' data.
If anyone is interested in more details about Haswell's gpu performance counters, the PRM can be found here:
https://01.org/linuxgraphics/sites/default/files/documentation/
observability_performance_counters_haswell.pdf
To see how I'm currently using this from userspace, I have a couple of intel-gpu-tools utilities; intel_oacounter_top_pmu + intel_gpu_trace_pmu:
https://github.com/rib/intel-gpu-tools/commits/wip/rib/intel-i915-oa-pmu
And the current code I have to use this in Mesa is here:
https://github.com/rib/mesa/commits/wip/rib/i915_oa_perf
Regards,
- Robert
drivers/gpu/drm/i915/Makefile | 1 + drivers/gpu/drm/i915/i915_dma.c | 2 + drivers/gpu/drm/i915/i915_drv.h | 33 ++ drivers/gpu/drm/i915/i915_oa_perf.c | 675 ++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/i915/i915_reg.h | 87 +++++ include/linux/perf_event.h | 1 + include/uapi/drm/i915_drm.h | 21 ++ kernel/events/core.c | 40 ++- 8 files changed, 854 insertions(+), 6 deletions(-) create mode 100644 drivers/gpu/drm/i915/i915_oa_perf.c
Signature · 1 line
2.1.2
RFC PATCH 1/3 perf: export perf_event_overflow
To support pmu drivers in loadable modules, such as the i915 driver
--- kernel/events/core.c | 1 + 1 file changed, 1 insertion(+)
kernel/events/core.c +1 -0
@@ -5478,6 +5478,7 @@ int perf_event_overflow(struct perf_event *event, {return __perf_event_overflow (event,1 , data, regs); } +EXPORT_SYMBOL_GPL (perf_event_overflow);/* * Generic software event infrastructure
Signature · 1 line
2.1.2
RFC PATCH 2/3 perf: Add PERF_PMU_CAP_IS_DEVICE flag
In reply to Robert BraggThe PERF_PMU_CAP_IS_DEVICE flag provides pmu drivers a way to declare that they only monitor device specific metrics and since they don't monitor any cpu metrics then perf should bypass any cpu centric security checks, as well as disallow cpu centric attributes.
--- include/linux/perf_event.h | 1 + kernel/events/core.c | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-)
include/linux/perf_event.h +1 -0
@@ -170,6 +170,7 @@ struct perf_event;* pmu::capabilities flags */ #define PERF_PMU_CAP_NO_INTERRUPT 0x01 +#define PERF_PMU_CAP_IS_DEVICE 0x02/** * struct pmu - generic performance monitoring unit
kernel/events/core.c +33 -6
@@ -3131,7 +3131,8 @@ find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)if (!task) {/* Must be root to operate on a CPU event: */ -if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN )) +if (!(pmu->capabilities &PERF_PMU_CAP_IS_DEVICE ) && +perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN ))return ERR_PTR (-EACCES );/*
@@ -7091,11 +7092,6 @@ SYSCALL_DEFINE5(perf_event_open,if (err)return err; -if (!attr.exclude_kernel) { -if (perf_paranoid_kernel () && !capable (CAP_SYS_ADMIN )) -return -EACCES ; - } -if (attr.freq) {if (attr.sample_freq > sysctl_perf_event_sample_rate)return -EINVAL ;
@@ -7154,6 +7150,37 @@ SYSCALL_DEFINE5(perf_event_open, goto err_cpus; } +if (event->pmu->capabilities &PERF_PMU_CAP_IS_DEVICE ) { + +/* Don't allow cpu centric attributes... */ +if (event->attr.exclude_user || + event->attr.exclude_callchain_user || + event->attr.exclude_kernel || + event->attr.exclude_callchain_kernel || + event->attr.exclude_hv || + event->attr.exclude_idle || + event->attr.exclude_host || + event->attr.exclude_guest || + event->attr.mmap || + event->attr.comm || + event->attr.task) +return -EINVAL ; + +if (attr.sample_type & + (PERF_SAMPLE_IP | +PERF_SAMPLE_TID | +PERF_SAMPLE_ADDR | +PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_CPU | +PERF_SAMPLE_BRANCH_STACK | +PERF_SAMPLE_REGS_USER | +PERF_SAMPLE_STACK_USER )) +return -EINVAL ; + }else if (!attr.exclude_kernel) { +if (perf_paranoid_kernel () && !capable (CAP_SYS_ADMIN )) +return -EACCES ; + } +if (flags &PERF_FLAG_PID_CGROUP ) { err =perf_cgroup_connect (pid, event, &attr, group_leader);if (err) {
Signature · 1 line
2.1.2
RFC PATCH 3/3 i915: Expose PMU for Observation Architecture
In reply to Robert BraggGen graphics hardware can be set up to periodically write snapshots of performance counters into a circular buffer and this patch exposes that capability to userspace via the perf interface.
Only Haswell is supported currently.
--- drivers/gpu/drm/i915/Makefile | 1 + drivers/gpu/drm/i915/i915_dma.c | 2 + drivers/gpu/drm/i915/i915_drv.h | 33 ++ drivers/gpu/drm/i915/i915_oa_perf.c | 675 ++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/i915/i915_reg.h | 87 +++++ include/uapi/drm/i915_drm.h | 21 ++ 6 files changed, 819 insertions(+) create mode 100644 drivers/gpu/drm/i915/i915_oa_perf.c
drivers/gpu/drm/i915/Makefile +1 -0
@@ -14,6 +14,7 @@ i915-y := i915_drv.o \ intel_pm.o i915-$(CONFIG_COMPAT) += i915_ioc32.o i915-$(CONFIG_DEBUG_FS) += i915_debugfs.o +i915-$(CONFIG_PERF_EVENTS) += i915_oa_perf.o # GEM code i915-y += i915_cmd_parser.o \
drivers/gpu/drm/i915/i915_dma.c +2 -0
@@ -1792,6 +1792,7 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags)intel_gpu_ips_init (dev_priv);intel_init_runtime_pm (dev_priv); +i915_oa_pmu_register (dev);return 0 ;
@@ -1839,6 +1840,7 @@ int i915_driver_unload(struct drm_device *dev)return ret; } +i915_oa_pmu_unregister (dev);intel_fini_runtime_pm (dev_priv);intel_gpu_ips_teardown ();
drivers/gpu/drm/i915/i915_drv.h +33 -0
@@ -45,6 +45,7 @@#include <linux/hashtable.h> #include <linux/intel-iommu.h> #include <linux/kref.h> +#include <linux/perf_event.h> #include <linux/pm_qos.h> /* General customization:
@@ -1636,6 +1637,29 @@ struct drm_i915_private {*/ struct workqueue_struct *dp_wq; +#ifdef CONFIG_PERF_EVENTS +struct { +struct pmu pmu; +spinlock_t lock; +struct hrtimer timer; +struct pt_regs dummy_regs; + +struct perf_event *exclusive_event; +struct intel_context *specific_ctx; + +struct { +struct kref refcount; +struct drm_i915_gem_object *obj; +u32 gtt_offset; +u8 *addr; +u32 head; +u32 tail; +int format; +int format_size; + } oa_buffer; + } oa_pmu; +#endif +/* Old dri1 support infrastructure, beware the dragons ya fools entering * here! */ struct i915_dri1_state dri1;
@@ -2688,6 +2712,15 @@ int i915_parse_cmds(struct intel_engine_cs *ring,u32 batch_start_offset, bool is_master); +/* i915_oa_perf.c */ +#ifdef CONFIG_PERF_EVENTS +extern void i915_oa_pmu_register (struct drm_device *dev); +extern void i915_oa_pmu_unregister (struct drm_device *dev); +#else +static inline void i915_oa_pmu_register (struct drm_device *dev) {} +static inline void i915_oa_pmu_unregister (struct drm_device *dev) {} +#endif +/* i915_suspend.c */ extern int i915_save_state (struct drm_device *dev);extern int i915_restore_state (struct drm_device *dev);
drivers/gpu/drm/i915/i915_oa_perf.c new file +675 -0
675 lines not on this page · raw message
drivers/gpu/drm/i915/i915_reg.h +87 -0
99 lines not on this page · raw message
include/uapi/drm/i915_drm.h +21 -0
@@ -58,6 +58,27 @@#define I915_ERROR_UEVENT "ERROR"#define I915_RESET_UEVENT "RESET" +/** +* DOC: perf events configuration exposed by i915 through /sys/bus/event_sources/drivers/i915_oa +* +*/ +#define I915_PERF_OA_CTX_ID_MASK 0xffffffff +#define I915_PERF_OA_SINGLE_CONTEXT_ENABLE (1ULL << 32) + +#define I915_PERF_OA_FORMAT_SHIFT 33 +#define I915_PERF_OA_FORMAT_MASK (0x7ULL << 33) +#define I915_PERF_OA_FORMAT_A13_HSW (0ULL << 33) +#define I915_PERF_OA_FORMAT_A29_HSW (1ULL << 33) +#define I915_PERF_OA_FORMAT_A13_B8_C8_HSW (2ULL << 33) +#define I915_PERF_OA_FORMAT_A29_B8_C8_HSW (3ULL << 33) +#define I915_PERF_OA_FORMAT_B4_C8_HSW (4ULL << 33) +#define I915_PERF_OA_FORMAT_A45_B8_C8_HSW (5ULL << 33) +#define I915_PERF_OA_FORMAT_B4_C8_A16_HSW (6ULL << 33) +#define I915_PERF_OA_FORMAT_C4_B8_HSW (7ULL << 33) + +#define I915_PERF_OA_TIMER_EXPONENT_SHIFT 36 +#define I915_PERF_OA_TIMER_EXPONENT_MASK (0x3fULL << 36) +/* Each region is a minimum of 16k, and there are at most 255 of them. */ #define I915_NR_TEX_REGIONS 255/* table size 2k - maximum due to use
Signature · 1 line
2.1.2
Re: [RFC PATCH 3/3] i915: Expose PMU for Observation Architecture
Quoted 6 lines · Robert Bragg
drivers/gpu/drm/i915/i915_oa_perf.c new file +6
-0+/* XXX: Not sure that this is really acceptable... +* +* i915_gem_context.c currently owns pinning/unpinning legacy +* context buffers and although that code has a +* get_context_alignment() func to handle a different +* constraint for gen6 we are assuming it's fixed for gen7
drivers/gpu/drm/i915/i915_oa_perf.c line 467
Quoted 10 lines
drivers/gpu/drm/i915/i915_oa_perf.c new file +10
-0+* here. Another option besides pinning here would be to +* instead hook into context switching and update the +* OACONTROL configuration on the fly. +*/ +if (dev_priv->oa_pmu.specific_ctx) { +struct intel_context *ctx = dev_priv->oa_pmu.specific_ctx; +int ret; + + ret =i915_gem_obj_ggtt_pin (ctx->legacy_hw_ctx.rcs_state, +4096 ,0 );
Right if you pin it here with a different alignment, when we try to pin it with the required hw ctx alignment it will fail. Easiest way is to record the ctx->legacy_hw_ctx.alignment and reuse that here.
drivers/gpu/drm/i915/i915_oa_perf.c line 470
Quoted 3 lines
drivers/gpu/drm/i915/i915_oa_perf.c new file +3
-0+if (ret) { +DRM_DEBUG_DRIVER ("Couldn't pin %d\n" , ret); + ret = -EBUSY ;
As an exercise, think of all the possible error values from pin() and tell me why overriding that here is a bad, bad idea. -Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Re: [RFC PATCH 3/3] i915: Expose PMU for Observation Architecture
Quoted 18 lines · Chris Wilson
Quoted 6 lines · Robert Bragg
drivers/gpu/drm/i915/i915_oa_perf.c new file +6
-0+/* XXX: Not sure that this is really acceptable... +* +* i915_gem_context.c currently owns pinning/unpinning legacy +* context buffers and although that code has a +* get_context_alignment() func to handle a different +* constraint for gen6 we are assuming it's fixed for gen7 drivers/gpu/drm/i915/i915_oa_perf.c line 467
Quoted 10 lines
drivers/gpu/drm/i915/i915_oa_perf.c new file +10
-0+* here. Another option besides pinning here would be to +* instead hook into context switching and update the +* OACONTROL configuration on the fly. +*/ +if (dev_priv->oa_pmu.specific_ctx) { +struct intel_context *ctx = dev_priv->oa_pmu.specific_ctx; +int ret; + + ret =i915_gem_obj_ggtt_pin (ctx->legacy_hw_ctx.rcs_state, +4096 ,0 );
Quoted 3 lines
Right if you pin it here with a different alignment, when we try to pin it with the required hw ctx alignment it will fail. Easiest way is to record the
ctx->legacy_hw_ctx.alignmentand reuse that here.
Ok I can look into that a bit more. I'm not currently sure I can assume the ctx will have been pinned before, to be able to record the alignment. Skimming i915_gem_context.c, it looks like we only pin the default context on creation and a user could open a perf even before we first switch to that context.
I wonder if it would be ok to expose an i915_get_context_alignment() api to deal with this?
Quoted 7 lines
drivers/gpu/drm/i915/i915_oa_perf.c line 470
Quoted 4 lines
drivers/gpu/drm/i915/i915_oa_perf.c new file +3
-0+if (ret) { +DRM_DEBUG_DRIVER ("Couldn't pin %d\n" , ret); + ret = -EBUSY ;As an exercise, think of all the possible error values from
pin()and tell me why overriding that here is a bad, bad idea.
Hmm, I'm not quite sure why I decided to squash the error code there, it looks pretty arbitrary. My take on your comment a.t.m is essentially that some of the pin() errors don't really represent a busy state where it would make sense for userspace to try again later; such as -ENODEV. Sorry if you saw a very specific case that offended you :-) I have removed the override locally.
Thanks for taking a look.
- Robert
Quoted 4 lines
-Chris
--
Chris Wilson, Intel Open Source Technology Centre
Re: [RFC PATCH 3/3] i915: Expose PMU for Observation Architecture
Quoted 23 lines · Robert Bragg
Quoted 18 lines · Chris Wilson
Quoted 6 lines · Robert Bragg
drivers/gpu/drm/i915/i915_oa_perf.c new file +6
-0+/* XXX: Not sure that this is really acceptable... +* +* i915_gem_context.c currently owns pinning/unpinning legacy +* context buffers and although that code has a +* get_context_alignment() func to handle a different +* constraint for gen6 we are assuming it's fixed for gen7 drivers/gpu/drm/i915/i915_oa_perf.c line 467
Quoted 10 lines
drivers/gpu/drm/i915/i915_oa_perf.c new file +10
-0+* here. Another option besides pinning here would be to +* instead hook into context switching and update the +* OACONTROL configuration on the fly. +*/ +if (dev_priv->oa_pmu.specific_ctx) { +struct intel_context *ctx = dev_priv->oa_pmu.specific_ctx; +int ret; + + ret =i915_gem_obj_ggtt_pin (ctx->legacy_hw_ctx.rcs_state, +4096 ,0 );Quoted 3 lines
Right if you pin it here with a different alignment, when we try to pin it with the required hw ctx alignment it will fail. Easiest way is to record the
ctx->legacy_hw_ctx.alignmentand reuse that here.
Quoted 8 lines
Ok I can look into that a bit more. I'm not currently sure I can assume the ctx will have been pinned before, to be able to record the alignment. Skimming
i915_gem_context.c, it looks like we only pin the default context on creation and a user could open a perf even before we first switch to that context.I wonder if it would be ok to expose an
i915_get_context_alignment()api to deal with this?
I would either add intel_context_pin_state()/unpin_state() or expose ctx->...state_alignment. Leaning towards the former so that we don't have too many places mucking around inside ctx.
Quoted 8 lines
Quoted 7 lines
drivers/gpu/drm/i915/i915_oa_perf.c line 470
Quoted 4 lines
drivers/gpu/drm/i915/i915_oa_perf.c new file +3
-0+if (ret) { +DRM_DEBUG_DRIVER ("Couldn't pin %d\n" , ret); + ret = -EBUSY ;As an exercise, think of all the possible error values from
pin()and tell me why overriding that here is a bad, bad idea.
Quoted 6 lines
Hmm, I'm not quite sure why I decided to squash the error code there, it looks pretty arbitrary. My take on your comment a.t.m is essentially that some of the
pin()errors don't really represent a busy state where it would make sense for userspace to try again later; such as-ENODEV. Sorry if you saw a very specific case that offended you :-) I have removed the override locally.
Or EINTR/EAGAIN and try again immediately. ;)
-Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Quoted 6 lines · Robert Bragg
[...]
I'd be interested to hear whether is sounds reasonable to others for us to expose gpu device metrics via a perf pmu and whether adding the
PERF_PMU_CAP_IS_DEVICEflag as in my following patch could be acceptable.
I think it's perfectly reasonable, it's one of the interesting kernel features I hoped for years would be implemented for perf.
Quoted 7 lines
[...]
In addition I also explicitly black list numerous attributes and
PERF_SAMPLE_flags that I don't think make sense for a device pmu. This could be handled in the pmu driver but it seemed better to do in events/core, avoiding duplication in case we later have multiple device pmus.
Btw., if the GPU is able to dump (part of) its current execution status, you could in theory even do instruction profiling and in-GPU profiling (symbol resolution, maybe even annotation, etc.) with close to standard perf tooling - which I think is currently mostly the domain of proprietary tools.
Thanks,
Ingo
Quoted 5 lines · Ingo Molnar
Quoted 2 lines · Robert Bragg
[...]
Quoted 7 lines
Quoted 4 lines
I'd be interested to hear whether is sounds reasonable to others for us to expose gpu device metrics via a perf pmu and whether adding the
PERF_PMU_CAP_IS_DEVICEflag as in my following patch could be acceptable.I think it's perfectly reasonable, it's one of the interesting kernel features I hoped for years would be implemented for perf.
Ok, that's good to hear, thanks.
Quoted 9 lines
Quoted 8 lines
[...]
In addition I also explicitly black list numerous attributes and
PERF_SAMPLE_flags that I don't think make sense for a device pmu. This could be handled in the pmu driver but it seemed better to do in events/core, avoiding duplication in case we later have multiple device pmus.
Quoted 5 lines
Btw., if the GPU is able to dump (part of) its current execution status, you could in theory even do instruction profiling and in-GPU profiling (symbol resolution, maybe even annotation, etc.) with close to standard perf tooling - which I think is currently mostly the domain of proprietary tools.
I'm not entirely sure; but there are certainly quite a few hw debug features I haven't really explored yet that might lend themselves to supporting something like this. At least there's a breakpoint mechanism that looks like it might help for something like this, including providing a way to focus on specific threads of interest which would be pretty important here to reduce how much state would have to be periodically captured. I could be interested to experiment with this later for sure.
With respect to the OA counters I'm looking to expose first, I would note that the counter snapshots being written periodically are written by a fixed function unit where we only have a limited set of layouts that we can choose.
Beyond the OA counters I can certainly see us wanting further pmus for other metrics though. For reference Chris Wilson experimented with some related ideas last year, here:
http://cgit.freedesktop.org/~ickle/linux-2.6/commit/?
h=perf&id=f32c19f4fb3a3bda92ab7bd0b9f95da14e81ca0a
Regards,
- Robert
Quoted 4 lines
Thanks,
Ingo
Peter Zijlstra
maintainerQuoted 10 lines · Robert Bragg
Our desired permission model seems consistent with perf's current model whereby you would need privileges if you want to profile across all gpu contexts but not need special permissions to profile your own context.
The awkward part is that it doesn't make sense for us to have userspace open a perf event with a specific pid as the way to avoid needing root permissions because a side effect of doing this is that the events will be dynamically added/deleted so as to only monitor while that process is scheduled and that's not really meaningful when we're monitoring the gpu.
There is precedent in PERF_FLAG_PID_CGROUP to replace the pid argument with a fd to your object.
And do I take it right that if you're able/allowed/etc.. to open/have the fd to the GPU/DRM/DRI whatever context you have the right credentials to also observe these counters?
Quoted 4 lines
Conceptually I suppose we want to be able to open an event that's not associated with any cpu or process, but to keep things simple and fit with perf's current design, the pmu I have a.t.m expects an event to be opened for a specific cpu and unspecified process.
There are no actual scheduling ramifications right? Let me ponder his for a little while more..
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Robert Bragg
Our desired permission model seems consistent with perf's current model whereby you would need privileges if you want to profile across all gpu contexts but not need special permissions to profile your own context.
The awkward part is that it doesn't make sense for us to have userspace open a perf event with a specific pid as the way to avoid needing root permissions because a side effect of doing this is that the events will be dynamically added/deleted so as to only monitor while that process is scheduled and that's not really meaningful when we're monitoring the gpu.
Quoted 2 lines
There is precedent in
PERF_FLAG_PID_CGROUPto replace the pid argument with a fd to your object.
Ah ok, interesting.
Quoted 4 lines
And do I take it right that if you're able/allowed/etc.. to open/have the fd to the GPU/DRM/DRI whatever context you have the right credentials to also observe these counters?
Right and in particular since we want to allow OpenGL clients to be able the profile their own gpu context with out any special privileges my current pmu driver accepts a device file descriptor via config1 + a context id via attr->config, both for checking credentials and uniquely identifying which context should be profiled. (A single client can open multiple contexts via one drm fd)
That said though; when running as root it is not currently a requirement to pass any fd when configuring an event to profile across all gpu contexts. I'm just mentioning this because although I think it should be ok for us to use an fd to determine credentials and help specify a gpu context, an fd might not be necessary for system wide profiling cases.
Quoted 8 lines
Quoted 5 lines
Conceptually I suppose we want to be able to open an event that's not associated with any cpu or process, but to keep things simple and fit with perf's current design, the pmu I have a.t.m expects an event to be opened for a specific cpu and unspecified process.
There are no actual scheduling ramifications right? Let me ponder his for a little while more..
Ok, I can't say I'm familiar enough with the core perf infrastructure to entirely sure about this.
I recall looking at how some of the uncore perf drivers were working and it looked like they had a similar issue where conceptually the pmu doesn't belong to a specific cpu and so the id would internally get mapped to some package state, shared by multiple cpus.
My understanding had been that being associated with a specific cpu did have the side effect that most of the pmu methods for that event would then be invoked on that cpu through inter-process interrupts. At one point that had seemed slightly problematic because there weren't many places within my pmu driver where I could assume I was in process context and could sleep. This was a problem with an earlier version because the way I read registers had a slim chance of needing to sleep waiting for the gpu to come out of RC6, but isn't a problem any more.
One thing that does come to mind here though is that I am overloading pmu->read() as a mechanism for userspace to trigger a flush of all counter snapshots currently in the gpu circular buffer to userspace as perf events. Perhaps it would be best if that work (which might be relatively costly at times) were done in the context of the process issuing the flush(), instead of under an IPI (assuming that has some effect on scheduler accounting).
Regards,
- Robert
Peter Zijlstra
maintainerQuoted 10 lines · Robert Bragg
Quoted 3 lines
And do I take it right that if you're able/allowed/etc.. to open/have the fd to the GPU/DRM/DRI whatever context you have the right credentials to also observe these counters?
Right and in particular since we want to allow OpenGL clients to be able the profile their own gpu context with out any special privileges my current pmu driver accepts a device file descriptor via config1 + a context id via
attr->config, both for checking credentials and uniquely identifying which context should be profiled. (A single client can open multiple contexts via one drm fd)
Ah interesting. So we've got fd+context_id+event_id to identify any one number provided by the GPU.
Quoted 6 lines
That said though; when running as root it is not currently a requirement to pass any fd when configuring an event to profile across all gpu contexts. I'm just mentioning this because although I think it should be ok for us to use an fd to determine credentials and help specify a gpu context, an fd might not be necessary for system wide profiling cases.
Hmm, how does root know what context_id to provide? Are those exposed somewhere? Is there also a root context, one that encompasses all others?
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
Conceptually I suppose we want to be able to open an event that's not associated with any cpu or process, but to keep things simple and fit with perf's current design, the pmu I have a.t.m expects an event to be opened for a specific cpu and unspecified process.
There are no actual scheduling ramifications right? Let me ponder his for a little while more..
Ok, I can't say I'm familiar enough with the core perf infrastructure to entirely sure about this.
Yeah, so I don't think so. Its on the device, nothing the CPU/scheduler does affects what the device does.
Quoted 4 lines
I recall looking at how some of the uncore perf drivers were working and it looked like they had a similar issue where conceptually the pmu doesn't belong to a specific cpu and so the id would internally get mapped to some package state, shared by multiple cpus.
Yeah, we could try and map these devices to a cpu on their node -- PCI devices are node local. But I'm not sure we need to start out by doing that.
Quoted 8 lines
My understanding had been that being associated with a specific cpu did have the side effect that most of the pmu methods for that event would then be invoked on that cpu through inter-process interrupts. At one point that had seemed slightly problematic because there weren't many places within my pmu driver where I could assume I was in process context and could sleep. This was a problem with an earlier version because the way I read registers had a slim chance of needing to sleep waiting for the gpu to come out of RC6, but isn't a problem any more.
Right, so I suppose we could make a new global context for these device like things and avoid some that song and dance. But we can do that later.
Quoted 7 lines
One thing that does come to mind here though is that I am overloading
pmu->read()as a mechanism for userspace to trigger a flush of all counter snapshots currently in the gpu circular buffer to userspace as perf events. Perhaps it would be best if that work (which might be relatively costly at times) were done in the context of the process issuing theflush(), instead of under an IPI (assuming that has some effect on scheduler accounting).
Right, so given you tell the GPU to periodically dump these stats (per context I presume), you can at a similar interval schedule whatever to flush this and update the relevant event->count values and have an NO-OP pmu::read() method.
If the GPU provides interrupts to notify you of new data or whatnot, you can make that drive the thing.
Quoted 13 lines · Peter Zijlstra
Quoted 10 lines · Robert Bragg
Quoted 3 lines
And do I take it right that if you're able/allowed/etc.. to open/have the fd to the GPU/DRM/DRI whatever context you have the right credentials to also observe these counters?
Right and in particular since we want to allow OpenGL clients to be able the profile their own gpu context with out any special privileges my current pmu driver accepts a device file descriptor via config1 + a context id via
attr->config, both for checking credentials and uniquely identifying which context should be profiled. (A single client can open multiple contexts via one drm fd)
Quoted 2 lines
Ah interesting. So we've got fd+context_id+event_id to identify any one number provided by the GPU.
Roughly.
The fd represents the device we're interested in.
Since a single application can manage multiple unique gpu contexts for submitting work we have the context_id to identify which one in particular we want to collect metrics for.
The event_id here though really represents a set of counters that are written out together in a hardware specific report layout.
On Haswell there are 8 different report layouts that basically trade off how many counters to include from 13 to 61 32bit counters plus 1 64bit timestamp. I exposed this format choice in the event configuration. It's notable that all of the counter values written in one report are captured atomically with respect to the gpu clock.
Within the reports most of the counters are hard-wired and they are referred to as Aggregating counters, including things like:
* number of cycles the render engine was busy for
* number of cycles the gpu was active
* number of cycles the gpu was stalled
(i'll just gloss over what distinguishes each of these states)
* number of active cycles spent running a vertex shader
* number of stalled cycles spent running a vertex shader
* number of vertex shader threads spawned
* number of active cycles spent running a pixel shader
* number of stalled cycles spent running a pixel shader"
* number of pixel shader threads spawned
...
The values are aggregated across all of the gpu's execution units (e.g. up to 40 units on Haswell)
Besides these aggregating counters the reports also include a gpu clock counter which allows us to normalize these values into something more intuitive for profiling.
There is a further small set of counters referred to as B counters in the public prms that are also included in these reports and the hardware has some configurability for these counters but given the constrains on configuring them, the expectation would be to just allow userspace to specify a enum for certain pre-defined configurations. (E.g. a configuration that exposes a well defined set of B counters useful for OpenGL profiling vs GPGPU profiling)
I had considered uniquely identifying each of the A counters with separate perf event ids, but I think the main reasons I decided against that in the end are:
Since they are written atomically the counters in a snapshot are all related and the analysis to derive useful values for benchmarking typically needs to refer to multiple counters in a single snapshot at a time. E.g. to report the "Average cycles per vertex shader thread" would need to measure the number of cycles spent running a vertex shader / the number of vertex shader threads spawned. If we split the counters up we'd then need to do work to correlate them again in userspace.
My other concern was actually with memory bandwidth, considering that it's possible to request the gpu to write out periodic snapshots at a very high frequency (we can program a period as low as 160 nanoseconds) and pushing this to the limit (running as root + overriding perf_event_max_sample_rate) can start to expose some interesting details about how the gpu is working - though notable observer effects too. I was expecting memory bandwidth to be the limiting factor for what resolution we can achieve this way and splitting the counters up looked like it would have quite a big impact, due to the extra sample headers and that the gpu timestamp would need to be repeated with each counter. e.g. in the most extreme case, instead of 8byte header + 61 counters * 4 bytes + 8byte timestamp every 160ns ~= 1.6GB/s, each counter would need to be paired with a gpu timestamp + header so we could have 61 * (8 + 4 + 8)bytes ~= 7.6GB/s. To be fair though it's likely that if the counters were split up we probably wouldn't often need a full set of 61 counters.
One last thing to mention here is that this first pmu driver that I have written only relates to one very specific observation unit within the gpu that happens to expose counters via reports/snapshots. There are other interesting gpu counters I could imagine exposing through separate pmu drivers too where the counters might simply be accessed via mmio and for those cases I would imagine having a 1:1 mapping between event-ids and counters.
Quoted 11 lines
Quoted 7 lines
That said though; when running as root it is not currently a requirement to pass any fd when configuring an event to profile across all gpu contexts. I'm just mentioning this because although I think it should be ok for us to use an fd to determine credentials and help specify a gpu context, an fd might not be necessary for system wide profiling cases.
Hmm, how does root know what
context_idto provide? Are those exposed somewhere? Is there also a root context, one that encompasses all others?
No, it's just that the observation unit has two modes of operation; either we can ask the unit to only aggregate counters for a specific context_id or tell it to aggregate across all contexts.
Quoted 33 lines
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
Conceptually I suppose we want to be able to open an event that's not associated with any cpu or process, but to keep things simple and fit with perf's current design, the pmu I have a.t.m expects an event to be opened for a specific cpu and unspecified process.
There are no actual scheduling ramifications right? Let me ponder his for a little while more..
Ok, I can't say I'm familiar enough with the core perf infrastructure to entirely sure about this.
Yeah, so I don't think so. Its on the device, nothing the CPU/scheduler does affects what the device does.
Quoted 4 lines
I recall looking at how some of the uncore perf drivers were working and it looked like they had a similar issue where conceptually the pmu doesn't belong to a specific cpu and so the id would internally get mapped to some package state, shared by multiple cpus.
Yeah, we could try and map these devices to a cpu on their node -- PCI devices are node local. But I'm not sure we need to start out by doing that.
Quoted 8 lines
My understanding had been that being associated with a specific cpu did have the side effect that most of the pmu methods for that event would then be invoked on that cpu through inter-process interrupts. At one point that had seemed slightly problematic because there weren't many places within my pmu driver where I could assume I was in process context and could sleep. This was a problem with an earlier version because the way I read registers had a slim chance of needing to sleep waiting for the gpu to come out of RC6, but isn't a problem any more.
Quoted 3 lines
Right, so I suppose we could make a new global context for these device like things and avoid some that song and dance. But we can do that later.
sure, at least for now it seems workable.
Quoted 9 lines
Quoted 8 lines
One thing that does come to mind here though is that I am overloading
pmu->read()as a mechanism for userspace to trigger a flush of all counter snapshots currently in the gpu circular buffer to userspace as perf events. Perhaps it would be best if that work (which might be relatively costly at times) were done in the context of the process issuing theflush(), instead of under an IPI (assuming that has some effect on scheduler accounting).
Quoted 8 lines
Right, so given you tell the GPU to periodically dump these stats (per context I presume), you can at a similar interval schedule whatever to flush this and update the relevant
event->countvalues and have an NO-OPpmu::read()method.If the GPU provides interrupts to notify you of new data or whatnot, you can make that drive the thing.
Right, I'm already ensuring the events will be forwarded within a finite time using a hrtimer, currently at 200Hz but there are also times where userspace wants to pull at the driver too.
The use case here is supporting the INTEL_performance_query OpenGL extension, where an application which can submit work to render on the gpu and can also start and stop performance queries around specific work and then ask for the results. Given how the queries are delimited Mesa can determine when the work being queried has completed and at that point the application can request the results of the query.
In this model Mesa will have configured a perf event to deliver periodic counter snapshots, but it only really cares about snapshots that fall between the start and end of a query. For this use case the periodic snapshots are just to detect counters wrapping and so the period will be relatively low at ~50milliseconds. At the end of a query Mesa won't know whether there are any periodic snapshots that fell between the start-end so it wants to explicitly flush at a point where it knows any snapshots will be ready if there are any.
Alternatively I think I could arrange it so that Mesa relies on knowing the driver will forward snapshots @ 200Hz and we could delay informing the application that results are ready until we are certain they must have been forwarded. I think the api could allow us to do that (except for one awkward case where the application can demand a synchronous response where we'd potentially have to sleep) My concern here is having to rely on a fixed and relatively high frequency for forwarding events which seems like it should be left as an implementation detail that userspace shouldn't need to know.
I'm guessing it could also be good at some point for the hrtimer frequency to relate to the buffer size + report sizes + timer frequency instead of being fixed, but this could be difficult to change if userspace needs to make assumptions about it, it could also increase the time userspace would have to wait before it could be sure outstanding snapshots have been received.
Hopefully that explains why I'm overloading read() like this currently.
Regards
- Robert
Quoted 37 lines · Robert Bragg
Quoted 13 lines · Peter Zijlstra
Quoted 10 lines · Robert Bragg
Quoted 3 lines
And do I take it right that if you're able/allowed/etc.. to open/have the fd to the GPU/DRM/DRI whatever context you have the right credentials to also observe these counters?
Right and in particular since we want to allow OpenGL clients to be able the profile their own gpu context with out any special privileges my current pmu driver accepts a device file descriptor via config1 + a context id via
attr->config, both for checking credentials and uniquely identifying which context should be profiled. (A single client can open multiple contexts via one drm fd)Quoted 2 lines
Ah interesting. So we've got fd+context_id+event_id to identify any one number provided by the GPU.
Roughly.
The fd represents the device we're interested in.
Since a single application can manage multiple unique gpu contexts for submitting work we have the
context_idto identify which one in particular we want to collect metrics for.The
event_idhere though really represents a set of counters that are written out together in a hardware specific report layout.On Haswell there are 8 different report layouts that basically trade off how many counters to include from 13 to 61 32bit counters plus 1 64bit timestamp. I exposed this format choice in the event configuration. It's notable that all of the counter values written in one report are captured atomically with respect to the gpu clock.
Within the reports most of the counters are hard-wired and they are referred to as Aggregating counters, including things like:
Quoted 11 lines
* number of cycles the render engine was busy for
* number of cycles the gpu was active
* number of cycles the gpu was stalled
(i'll just gloss over what distinguishes each of these states)
* number of active cycles spent running a vertex shader
* number of stalled cycles spent running a vertex shader
* number of vertex shader threads spawned
* number of active cycles spent running a pixel shader
* number of stalled cycles spent running a pixel shader"
* number of pixel shader threads spawned
...
Just curious:
Beyond aggregated counts, do the GPU reports also allow sampling the PC of the vertex shader and pixel shader execution?
That would allow effective annotated disassembly of them and bottleneck analysis - much like 'perf annotate' and how you can drill into annotated assembly code in 'perf report' and 'perf top'.
Secondly, do you also have cache hit/miss counters (with sampling ability) for the various caches the GPU utilizes: such as the LLC it shares with the CPU, or GPU-specific caches (if any) such as the vertex cache? Most GPU shader performance problems relate to memory access patterns and the above aggregate counts only tell us the global picture.
Thirdly, if taken branch instructions block/stall non-taken threads within an execution unit (like it happens on other vector CPUs) then being able to measure/sample current effective thread concurrency within an execution unit is generally useful as well, to be able to analyze this major class of GPU/GPGPU performance problems.
Quoted 6 lines
The values are aggregated across all of the gpu's execution units (e.g. up to 40 units on Haswell)
Besides these aggregating counters the reports also include a gpu clock counter which allows us to normalize these values into something more intuitive for profiling.
Modern GPUs can also change their clock frequency depending on load - is the GPU clock normalized by the hardware to a known fixed frequency, or does it change as the GPU's clock changes?
Quoted 25 lines
[...]
I had considered uniquely identifying each of the A counters with separate perf event ids, but I think the main reasons I decided against that in the end are:
Since they are written atomically the counters in a snapshot are all related and the analysis to derive useful values for benchmarking typically needs to refer to multiple counters in a single snapshot at a time. E.g. to report the "Average cycles per vertex shader thread" would need to measure the number of cycles spent running a vertex shader / the number of vertex shader threads spawned. If we split the counters up we'd then need to do work to correlate them again in userspace.
My other concern was actually with memory bandwidth, considering that it's possible to request the gpu to write out periodic snapshots at a very high frequency (we can program a period as low as 160 nanoseconds) and pushing this to the limit (running as root + overriding
perf_event_max_sample_rate) can start to expose some interesting details about how the gpu is working - though notable observer effects too. I was expecting memory bandwidth to be the limiting factor for what resolution we can achieve this way and splitting the counters up looked like it would have quite a big impact, due to the extra sample
Quoted 8 lines
headers and that the gpu timestamp would need to be repeated with each counter. e.g. in the most extreme case, instead of 8byte header + 61 counters * 4 bytes + 8byte timestamp every 160ns ~= 1.6GB/s, each counter would need to be paired with a gpu timestamp + header so we could have 61 * (8 + 4 + 8)bytes ~= 7.6GB/s. To be fair though it's likely that if the counters were split up we probably wouldn't often need a full set of 61 counters.
If you really want to collect such high frequency data then you are probably right in trying to compress the report format as much as possible.
Quoted 8 lines
One last thing to mention here is that this first pmu driver that I have written only relates to one very specific observation unit within the gpu that happens to expose counters via reports/snapshots. There are other interesting gpu counters I could imagine exposing through separate pmu drivers too where the counters might simply be accessed via mmio and for those cases I would imagine having a 1:1 mapping between event-ids and counters.
I'd strong suggest thinking about sampling as well, if the hardware exposes sample information: at least for profiling CPU loads the difference is like day and night, compared to aggregated counts and self-profiling.
Quoted 27 lines
Quoted 4 lines
[...]
If the GPU provides interrupts to notify you of new data or whatnot, you can make that drive the thing.
Right, I'm already ensuring the events will be forwarded within a finite time using a hrtimer, currently at 200Hz but there are also times where userspace wants to pull at the driver too.
The use case here is supporting the
INTEL_performance_queryOpenGL extension, where an application which can submit work to render on the gpu and can also start and stop performance queries around specific work and then ask for the results. Given how the queries are delimited Mesa can determine when the work being queried has completed and at that point the application can request the results of the query.In this model Mesa will have configured a perf event to deliver periodic counter snapshots, but it only really cares about snapshots that fall between the start and end of a query. For this use case the periodic snapshots are just to detect counters wrapping and so the period will be relatively low at ~50milliseconds. At the end of a query Mesa won't know whether there are any periodic snapshots that fell between the start-end so it wants to explicitly flush at a point where it knows any snapshots will be ready if there are any.
Quoted 10 lines
Alternatively I think I could arrange it so that Mesa relies on knowing the driver will forward snapshots @ 200Hz and we could delay informing the application that results are ready until we are certain they must have been forwarded. I think the api could allow us to do that (except for one awkward case where the application can demand a synchronous response where we'd potentially have to sleep) My concern here is having to rely on a fixed and relatively high frequency for forwarding events which seems like it should be left as an implementation detail that userspace shouldn't need to know.
It's a very good idea to not expose such limitations to user-space - the GPU driver doing the necessary hrtimer polling to construct a proper count is a much higher quality solution.
The last thing you want to ask yourself when seeing some weird profiling result is 'did user-space properly poll the PMU or did we overflow??'. Instrumentation needs to be rock solid dependable and fast, in that order.
Thanks,
Ingo
Quoted 3 lines · Ingo Molnar
* Robert Bragg <[email protected]> wrote:
<snip>
Quoted 23 lines
Quoted 9 lines
On Haswell there are 8 different report layouts that basically trade off how many counters to include from 13 to 61 32bit counters plus 1 64bit timestamp. I exposed this format choice in the event configuration. It's notable that all of the counter values written in one report are captured atomically with respect to the gpu clock.
Within the reports most of the counters are hard-wired and they are referred to as Aggregating counters, including things like:
Quoted 11 lines
* number of cycles the render engine was busy for
* number of cycles the gpu was active
* number of cycles the gpu was stalled
(i'll just gloss over what distinguishes each of these states)
* number of active cycles spent running a vertex shader
* number of stalled cycles spent running a vertex shader
* number of vertex shader threads spawned
* number of active cycles spent running a pixel shader
* number of stalled cycles spent running a pixel shader"
* number of pixel shader threads spawned
...Just curious:
Quoted 7 lines
Beyond aggregated counts, do the GPU reports also allow sampling the PC of the vertex shader and pixel shader execution?
That would allow effective annotated disassembly of them and bottleneck analysis - much like 'perf annotate' and how you can drill into annotated assembly code in 'perf report' and 'perf top'.
No, I'm afraid these particular counter reports from the OA unit can't give us access to EU instruction pointers or other EU registers, even considering the set of configurable counters that can be exposed besides the aggregate counters. These OA counters are more-or-less just boolean event counters.
Because your train of thought got me wondering though if it would be possible to sample instruction pointers of EU threads periodically; I spent a bit of time investigating how it could potentially be implemented, out of curiosity. I found at least one possible approach, but one thing that became apparent is that it wouldn't really be possible to handle neatly from the kernel and would need tightly coupled support from Mesa in userspace too...
Gen EUs have some support for exception handling where an exception could be triggered periodically (not internally by the gpu, but rather by the cpu) and the EUs made to run a given 'system routine' which would be able to sample the instruction pointer of the interrupted threads. One of the difficulties is that it wouldn't be possible for the kernel to directly setup a system routine for profiling like this, since the pointer for the routine is set via a STATE_SIP command that requires a pointer relative to the 'instruction base pointer' which is state that's really owned and setup by userspace drivers.
Incidentally our current driver stack doesn't currently utilise system routines for anything and so at least something like this wouldn't conflict with an existing feature. Some experiments were done with system routines by Ben Widawsky some years ago now, with the aim of using them for debugging as opposed to profiling, and that means he has some code knocking around (intel-gpu-tools/debugger) that could make it possible to put together an experiment for this.
For now I'd like to continue with enabling access to the OA counters via perf if possible, since that's much lower hanging fruit but should still allow a decent range of profiling tools. If I get a chance though, I'm tempted to see if I can use Ben's code as a basis to experiment with this idea.
Quoted 7 lines
Secondly, do you also have cache hit/miss counters (with sampling ability) for the various caches the GPU utilizes: such as the LLC it shares with the CPU, or GPU-specific caches (if any) such as the vertex cache? Most GPU shader performance problems relate to memory access patterns and the above aggregate counts only tell us the global picture.
Right, we can expose some of these via OA counter reports, through the configurable counters. E.g. we can get a counter for the number of L3 cache read/write transactions via the LLC which can be converted into a throughput. There are also other interesting counters relating to the texture samplers for example, that are a common bottleneck.
My initial i915_oa driver doesn't look at exposing those yet since we still need to work through an approval process for some of the details. My first interest was to start with creating a driver to expose the features and counters we already have published public docs for, which in turn let me send out this RFC sooner rather than later.
Quoted 7 lines
Thirdly, if taken branch instructions block/stall non-taken threads within an execution unit (like it happens on other vector CPUs) then being able to measure/sample current effective thread concurrency within an execution unit is generally useful as well, to be able to analyze this major class of GPU/GPGPU performance problems.
Right, Gen EUs try to co-issue instructions from multiple threads at the same time, so long as they aren't contending for the same units.
I'm not currently sure of a way to get insight into this for Haswell, but for Broadwell we gain some more aggregate EU counters (actually some of them become customisable) and then it's possible to count the issuing of instructions for some of the sub-units that allow co-issuing.
Quoted 11 lines
Quoted 7 lines
The values are aggregated across all of the gpu's execution units (e.g. up to 40 units on Haswell)
Besides these aggregating counters the reports also include a gpu clock counter which allows us to normalize these values into something more intuitive for profiling.
Modern GPUs can also change their clock frequency depending on load - is the GPU clock normalized by the hardware to a known fixed frequency, or does it change as the GPU's clock changes?
Sadly on Haswell, while these OA counters are enabled we need to disable RC6 and also render trunk clock gating, so this obviously has an impact on profiling that needs to be take into account.
On Broadwell I think we should be able to enable both though and in that case the gpu will automatically write additional counter snapshots when transitioning in and out of RC6 as well as when the clock frequency changes.
<snip>
Quoted 10 lines
Quoted 9 lines
One last thing to mention here is that this first pmu driver that I have written only relates to one very specific observation unit within the gpu that happens to expose counters via reports/snapshots. There are other interesting gpu counters I could imagine exposing through separate pmu drivers too where the counters might simply be accessed via mmio and for those cases I would imagine having a 1:1 mapping between event-ids and counters.
Quoted 4 lines
I'd strong suggest thinking about sampling as well, if the hardware exposes sample information: at least for profiling CPU loads the difference is like day and night, compared to aggregated counts and self-profiling.
Here I was thinking of counters or data that can be sampled via mmio using a hrtimer. E.g. the current gpu frequency or the energy usage. I'm not currently aware of any capability for the gpu to say trigger an interrupt after a threshold number of events occurs (like clock cycles) so I think we may generally be limited to a wall clock time domain for sampling.
As above, I'll also keep in mind, experimenting with being able to sample EU IPs at some point too.
Quoted 39 lines
Quoted 28 lines
Quoted 5 lines
[...]
If the GPU provides interrupts to notify you of new data or whatnot, you can make that drive the thing.
Right, I'm already ensuring the events will be forwarded within a finite time using a hrtimer, currently at 200Hz but there are also times where userspace wants to pull at the driver too.
The use case here is supporting the
INTEL_performance_queryOpenGL extension, where an application which can submit work to render on the gpu and can also start and stop performance queries around specific work and then ask for the results. Given how the queries are delimited Mesa can determine when the work being queried has completed and at that point the application can request the results of the query.In this model Mesa will have configured a perf event to deliver periodic counter snapshots, but it only really cares about snapshots that fall between the start and end of a query. For this use case the periodic snapshots are just to detect counters wrapping and so the period will be relatively low at ~50milliseconds. At the end of a query Mesa won't know whether there are any periodic snapshots that fell between the start-end so it wants to explicitly flush at a point where it knows any snapshots will be ready if there are any.
Quoted 10 lines
Alternatively I think I could arrange it so that Mesa relies on knowing the driver will forward snapshots @ 200Hz and we could delay informing the application that results are ready until we are certain they must have been forwarded. I think the api could allow us to do that (except for one awkward case where the application can demand a synchronous response where we'd potentially have to sleep) My concern here is having to rely on a fixed and relatively high frequency for forwarding events which seems like it should be left as an implementation detail that userspace shouldn't need to know.
Quoted 3 lines
It's a very good idea to not expose such limitations to user-space - the GPU driver doing the necessary hrtimer polling to construct a proper count is a much higher quality solution.
That sounds preferable.
I'm open to suggestions for finding another way for userspace to initiate a flush besides through read() in case there's a concern that might be set a bad precedent. For the i915_oa driver it seems ok at the moment since we don't currently report a useful counter through read() and for the main use case where we want the flushing we expect that most of the time there won't be any significant cost involved in flushing since we'll be using a very low timer period. Maybe this will bite us later though.
Quoted 5 lines
The last thing you want to ask yourself when seeing some weird profiling result is 'did user-space properly poll the PMU or did we overflow??'. Instrumentation needs to be rock solid dependable and fast, in that order.
That sounds like good advice.
Thanks,
- Robert
Quoted 11 lines · Robert Bragg
Quoted 4 lines
I'd strong[ly] suggest thinking about sampling as well, if the hardware exposes sample information: at least for profiling CPU loads the difference is like day and night, compared to aggregated counts and self-profiling.
Here I was thinking of counters or data that can be sampled via mmio using a hrtimer. E.g. the current gpu frequency or the energy usage. I'm not currently aware of any capability for the gpu to say trigger an interrupt after a threshold number of events occurs (like clock cycles) so I think we may generally be limited to a wall clock time domain for sampling.
In general hrtimer-driven polling gives pretty good profiling information as well - key is to be able to get a sample of EU thread execution state.
(Trigger thresholds and so can be useful as well, but are a second order concern in terms of profiling quality.)
Quoted 7 lines
Quoted 4 lines
It's a very good idea to not expose such limitations to user-space - the GPU driver doing the necessary hrtimer polling to construct a proper count is a much higher quality solution.
That sounds preferable.
Quoted 9 lines
I'm open to suggestions for finding another way for userspace to initiate a flush besides through
read()in case there's a concern that might be set a bad precedent. For thei915_oadriver it seems ok at the moment since we don't currently report a useful counter throughread()and for the main use case where we want the flushing we expect that most of the time there won't be any significant cost involved in flushing since we'll be using a very low timer period. Maybe this will bite us later though.
You could add an ioctl() as well - we are not religious about them, there's always things that are special enough to not warrant a generic syscall.
Anyway, aggregate counts alone are obviously very useful to analyzing GPU performance, so your initial approach looks perfectly acceptable to me already.
Thanks,
Ingo