Per client engine busyness
Subsystems (2)
cover Per client engine busyness
From: Tvrtko Ursulin <[email protected]>
Another re-spin of the per-client engine busyness series. Highlights from this version:
* Checkpatch cleanup and bits of review feedback only.
Internally we track time spent on engines for each struct intel_context. This can serve as a building block for several features from the want list:
smarter scheduler decisions, getrusage(2)-like per-GEM-context functionality wanted by some customers, cgroups controller, dynamic SSEU tuning,...
Externally, in sysfs, we expose time spent on GPU per client and per engine class.
Sysfs interface enables us to implement a "top-like" tool for GPU tasks. Or with a "screenshot":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
intel-gpu-top - 906/ 955 MHz; 0% RC6; 5.30 Watts; 933 irqs/s
IMC reads: 4414 MiB/s
IMC writes: 3805 MiB/s
ENGINE BUSY MI_SEMA MI_WAIT
Render/3D/0 93.46% |████████████████████████████████▋ | 0% 0%
Blitter/0 0.00% | | 0% 0%
Video/0 0.00% | | 0% 0%
VideoEnhance/0 0.00% | | 0% 0%
PID NAME Render/3D Blitter Video VideoEnhance
2733 neverball |██████▌ || || || | 2047 Xorg |███▊ || || || | 2737 glxgears |█▍ || || || | 2128 xfwm4 | || || || | 2047 Xorg | || || || |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Implementation wise we add a a bunch of files in sysfs like:
# cd /sys/class/drm/card0/clients/ # tree . ├── 7 │ ├── busy │ │ ├── 0 │ │ ├── 1 │ │ ├── 2 │ │ └── 3 │ ├── name │ └── pid ├── 8 │ ├── busy │ │ ├── 0 │ │ ├── 1 │ │ ├── 2 │ │ └── 3 │ ├── name │ └── pid └── 9 ├── busy │ ├── 0 │ ├── 1 │ ├── 2 │ └── 3 ├── name └── pid
Files in 'busy' directories are numbered using the engine class ABI values and they contain accumulated nanoseconds each client spent on engines of a respective class.
It is stil a RFC since it misses dedicated test cases to ensure things really work as advertised.
Tvrtko Ursulin (9):
drm/i915: Update client name on context create drm/i915: Make GEM contexts track DRM clients drm/i915: Use explicit flag to mark unreachable intel_context drm/i915: Track runtime spent in unreachable intel_contexts drm/i915: Track runtime spent in closed GEM contexts drm/i915: Track all user contexts per client drm/i915: Expose per-engine client busyness drm/i915: Track context current active time drm/i915: Prefer software tracked context busyness
drivers/gpu/drm/i915/gem/i915_gem_context.c | 63 +++- .../gpu/drm/i915/gem/i915_gem_context_types.h | 21 +- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 2 +- drivers/gpu/drm/i915/gt/intel_context.c | 18 +- drivers/gpu/drm/i915/gt/intel_context.h | 6 +- drivers/gpu/drm/i915/gt/intel_context_types.h | 25 +- drivers/gpu/drm/i915/gt/intel_lrc.c | 55 +++- drivers/gpu/drm/i915/gt/selftest_lrc.c | 10 +- drivers/gpu/drm/i915/i915_debugfs.c | 29 +- drivers/gpu/drm/i915/i915_drm_client.c | 274 +++++++++++++++++- drivers/gpu/drm/i915/i915_drm_client.h | 33 ++- drivers/gpu/drm/i915/i915_gpu_error.c | 25 +- 12 files changed, 473 insertions(+), 88 deletions(-)
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 1/9 drm/i915: Update client name on context create
From: Tvrtko Ursulin <[email protected]>
Some clients have the DRM fd passed to them over a socket by the X server.
Grab the real client and pid when they create their first context and update the exposed data for more useful enumeration.
To enable lockless access to client name and pid data from the following patches, we also make these fields rcu protected. In this way asynchronous code paths where both contexts which remain after the client exit, and access to client name and pid as they are getting updated due context creation running in parallel with name/pid queries.
v2:
* Do not leak the pid reference and borrow context idr_lock. (Chris)
v3:
* More avoiding leaks. (Chris)
v4:
* Move update completely to drm client. (Chris)
* Do not lose previous client data on failure to re-register and simplify update to only touch what it needs.
v5:
* Reuse ext_data local. (Chris)
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 5 + drivers/gpu/drm/i915/i915_drm_client.c | 103 ++++++++++++++++++-- drivers/gpu/drm/i915/i915_drm_client.h | 10 +- 3 files changed, 106 insertions(+), 12 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +5 -0
@@ -74,6 +74,7 @@#include "gt/intel_engine_user.h" #include "gt/intel_ring.h" +#include "i915_drm_client.h" #include "i915_gem_context.h" #include "i915_globals.h" #include "i915_trace.h"
@@ -2357,6 +2358,10 @@ int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,return -EIO ; } + ret =i915_drm_client_update (ext_data.fpriv->client, current); +if (ret) +return ret; + ext_data.ctx =i915_gem_create_context (i915, args->flags);if (IS_ERR (ext_data.ctx))return PTR_ERR (ext_data.ctx);
drivers/gpu/drm/i915/i915_drm_client.c +93 -10
144 lines not on this page · raw message
drivers/gpu/drm/i915/i915_drm_client.h +8 -2
@@ -9,6 +9,7 @@#include <linux/device.h> #include <linux/kobject.h> #include <linux/kref.h> +#include <linux/mutex.h> #include <linux/pid.h> #include <linux/rcupdate.h> #include <linux/sched.h>
@@ -26,9 +27,11 @@ struct i915_drm_client {struct rcu_head rcu; +struct mutex update_lock;/* Serializes name and pid updates. */ +unsigned int id; -struct pid *pid; -char *name; +struct pid __rcu *pid; +char __rcu *name;bool closed;struct i915_drm_clients *clients;
@@ -61,4 +64,7 @@ void i915_drm_client_close(struct i915_drm_client *client);struct i915_drm_client *i915_drm_client_add (struct i915_drm_clients *clients,struct task_struct *task); +int i915_drm_client_update (struct i915_drm_client *client, +struct task_struct *task); +#endif /* !__I915_DRM_CLIENT_H__ */
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 2/9 drm/i915: Make GEM contexts track DRM clients
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
If we make GEM contexts keep a reference to i915_drm_client for the whole of their lifetime, we can consolidate the current task pid and name usage by getting it from the client.
v2:
* Don't bother supporting selftests contexts from debugfs. (Chris)
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 23 ++++++++++++--- .../gpu/drm/i915/gem/i915_gem_context_types.h | 13 ++------- drivers/gpu/drm/i915/i915_debugfs.c | 29 +++++++------------ drivers/gpu/drm/i915/i915_gpu_error.c | 21 ++++++++------ 4 files changed, 44 insertions(+), 42 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +19 -4
@@ -340,8 +340,13 @@ static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx)static void i915_gem_context_free (struct i915_gem_context *ctx) { +struct i915_drm_client *client = ctx->client; +GEM_BUG_ON (!i915_gem_context_is_closed (ctx)); +if (client) +i915_drm_client_put (client); +spin_lock (&ctx->i915->gem.contexts.lock);list_del (&ctx->link);spin_unlock (&ctx->i915->gem.contexts.lock);
@@ -351,7 +356,6 @@ static void i915_gem_context_free(struct i915_gem_context *ctx)if (ctx->timeline)intel_timeline_put (ctx->timeline); -put_pid (ctx->pid);mutex_destroy (&ctx->mutex);kfree_rcu (ctx, rcu);
@@ -911,6 +915,7 @@ static int gem_context_register(struct i915_gem_context *ctx,struct drm_i915_file_private *fpriv, u32 *id) { + struct i915_drm_client *client;struct i915_address_space *vm;int ret;
@@ -922,15 +927,25 @@ static int gem_context_register(struct i915_gem_context *ctx,WRITE_ONCE (vm->file, fpriv);/* XXX */ mutex_unlock (&ctx->mutex); - ctx->pid =get_task_pid (current,PIDTYPE_PID ); + client =i915_drm_client_get (fpriv->client); + +rcu_read_lock ();snprintf (ctx->name,sizeof (ctx->name),"%s[%d]" , - current->comm,pid_nr (ctx->pid)); +rcu_dereference (client->name), +pid_nr (rcu_dereference (client->pid))); +rcu_read_unlock ();/* And finally expose ourselves to userspace via the idr */ ret =xa_alloc (&fpriv->context_xa, id, ctx, xa_limit_32b,GFP_KERNEL );if (ret) -put_pid (fetch_and_zero (&ctx->pid)); + goto err; + + ctx->client = client; +return 0 ; + +err: +i915_drm_client_put (client);return ret; }
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +3 -10
@@ -96,20 +96,13 @@ struct i915_gem_context {*/ struct i915_address_space __rcu *vm; -/** -* @pid: process id of creator -* -* Note that who created the context may not be the principle user, -* as the context may be shared across a local socket. However, -* that should only affect the default context, all contexts created -* explicitly by the client are expected to be isolated. -*/ -struct pid *pid; -/** link: place with &drm_i915_private.context_list */ struct list_head link;struct llist_node free_link; +/** client: struct i915_drm_client */ +struct i915_drm_client *client; +/** * @ref: reference count *
drivers/gpu/drm/i915/i915_debugfs.c +10 -19
@@ -330,17 +330,15 @@ static void print_context_stats(struct seq_file *m, .vm = rcu_access_pointer(ctx->vm), };struct drm_file *file = ctx->file_priv->file; -struct task_struct *task;char name[80 ];rcu_read_lock (); +idr_for_each (&file->object_idr, per_file_stats, &stats); -rcu_read_unlock (); -rcu_read_lock (); - task =pid_task (ctx->pid ?: file->pid,PIDTYPE_PID );snprintf (name,sizeof (name),"%s" , - task ? task->comm :"<unknown>" ); +rcu_dereference (ctx->client->name)); +rcu_read_unlock ();print_file_stats (m, name, stats);
@@ -1059,20 +1057,13 @@ static int i915_context_status(struct seq_file *m, void *unused)spin_unlock (&i915->gem.contexts.lock);seq_puts (m,"HW context " ); -if (ctx->pid) { -struct task_struct *task; - - task =get_pid_task (ctx->pid,PIDTYPE_PID ); -if (task) { -seq_printf (m,"(%s [%d]) " , - task->comm, task->pid); -put_task_struct (task); - } - }else if (IS_ERR (ctx->file_priv)) { -seq_puts (m,"(deleted) " ); - }else { -seq_puts (m,"(kernel) " ); - } + +rcu_read_lock (); +seq_printf (m,"(%s [%d]) %s" , +rcu_dereference (ctx->client->name), +pid_nr (rcu_dereference (ctx->client->pid)), + ctx->client->closed ?"(closed) " :"" ); +rcu_read_unlock ();seq_putc (m, ctx->remap_slice ?'R' :'r' );seq_putc (m,'\n' );
drivers/gpu/drm/i915/i915_gpu_error.c +12 -9
@@ -1221,7 +1221,8 @@ static void record_request(const struct i915_request *request,rcu_read_lock (); ctx =rcu_dereference (request->context->gem_context);if (ctx) - erq->pid =pid_nr (ctx->pid); + erq->pid =I915_SELFTEST_ONLY (!ctx->client) ? +0 :pid_nr (rcu_dereference (ctx->client->pid));rcu_read_unlock (); }
@@ -1241,23 +1242,25 @@ static bool record_context(struct i915_gem_context_coredump *e, const struct i915_request *rq) {struct i915_gem_context *ctx; -struct task_struct *task;bool simulated;rcu_read_lock (); + ctx =rcu_dereference (rq->context->gem_context);if (ctx && !kref_get_unless_zero (&ctx->ref)) ctx =NULL ; -rcu_read_unlock (); -if (!ctx) +if (!ctx) { +rcu_read_unlock ();return true; + } -rcu_read_lock (); - task =pid_task (ctx->pid,PIDTYPE_PID ); -if (task) { -strcpy (e->comm, task->comm); - e->pid = task->pid; +if (I915_SELFTEST_ONLY (!ctx->client)) { +strcpy (e->comm,"[kernel]" ); + }else { +strcpy (e->comm,rcu_dereference (ctx->client->name)); + e->pid =pid_nr (rcu_dereference (ctx->client->pid)); } +rcu_read_unlock (); e->sched_attr = ctx->sched;
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 3/9 drm/i915: Use explicit flag to mark unreachable intel_context
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
I need to keep the GEM context around a bit longer so adding an explicit flag for syncing execbuf with closed/abandonded contexts.
v2:
* Use already available context flags. (Chris)
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 3 ++- drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c | 2 +- drivers/gpu/drm/i915/gt/intel_context_types.h | 1 + 3 files changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -1
@@ -579,7 +579,8 @@ static void engines_idle_release(struct i915_gem_context *ctx,int err =0 ;/* serialises with execbuf */ -RCU_INIT_POINTER (ce->gem_context,NULL ); +set_bit (INTEL_CONTEXT_CLOSED , &ce->flags); +if (!intel_context_pin_if_active (ce))continue ;
drivers/gpu/drm/i915/gem/i915_gem_execbuffer.c +1 -1
@@ -2316,7 +2316,7 @@ static void eb_request_add(struct i915_execbuffer *eb) prev =__i915_request_commit (rq);/* Check that the context wasn't destroyed before submission */ -if (likely (rcu_access_pointer (eb->context->gem_context))) { +if (likely (!test_bit (INTEL_CONTEXT_CLOSED , &eb->context->flags))) { attr = eb->gem_context->sched;/*
drivers/gpu/drm/i915/gt/intel_context_types.h +1 -0
@@ -66,6 +66,7 @@ struct intel_context {#define CONTEXT_BANNED 4#define CONTEXT_FORCE_SINGLE_SUBMISSION 5#define CONTEXT_NOPREEMPT 6 +#define INTEL_CONTEXT_CLOSED 7u32 *lrc_reg_state;u64 lrc_desc;
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 5/9 drm/i915: Track runtime spent in closed GEM contexts
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
As GEM contexts are closed we want to have the DRM client remember how much GPU time they used (per class) so later we can used it for smarter purposes.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 12 +++++++++++- drivers/gpu/drm/i915/i915_drm_client.h | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +11 -1
@@ -355,8 +355,18 @@ static void i915_gem_context_free(struct i915_gem_context *ctx)GEM_BUG_ON (!i915_gem_context_is_closed (ctx)); -if (client) +if (client) { +unsigned int i; + +/* Transfer accumulated runtime to the parent drm client. */ +BUILD_BUG_ON (ARRAY_SIZE (client->past_runtime) != +ARRAY_SIZE (ctx->past_runtime)); +for (i =0 ; i <ARRAY_SIZE (client->past_runtime); i++) +atomic64_add (atomic64_read (&ctx->past_runtime[i]), + &client->past_runtime[i]); +i915_drm_client_put (client); + }spin_lock (&ctx->i915->gem.contexts.lock);list_del (&ctx->link);
drivers/gpu/drm/i915/i915_drm_client.h +7 -0
@@ -15,6 +15,8 @@#include <linux/sched.h> #include <linux/xarray.h> +#include "gt/intel_engine_types.h" +struct i915_drm_clients {struct xarray xarray;u32 next_id;
@@ -41,6 +43,11 @@ struct i915_drm_client {struct device_attribute pid;struct device_attribute name; } attr; + +/** +* @past_runtime: Accumulation of pphwsp runtimes from closed contexts. +*/ +atomic64_t past_runtime[MAX_ENGINE_CLASS +1 ]; };void i915_drm_clients_init (struct i915_drm_clients *clients);
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 7/9 drm/i915: Expose per-engine client busyness
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Expose per-client and per-engine busyness under the previously added sysfs client root.
The new files are one per-engine instance and located under the 'busy' directory. Each contains a monotonically increasing nano-second resolution times each client's jobs were executing on the GPU.
This enables userspace to create a top-like tool for GPU utilization:
==========================================================================
intel-gpu-top - 935/ 935 MHz; 0% RC6; 14.73 Watts; 1097 irqs/s
IMC reads: 1401 MiB/s
IMC writes: 4 MiB/s
ENGINE BUSY MI_SEMA MI_WAIT
Render/3D/0 63.73% |███████████████████ | 3% 0%
Blitter/0 9.53% |██▊ | 6% 0%
Video/0 39.32% |███████████▊ | 16% 0%
Video/1 15.62% |████▋ | 0% 0%
VideoEnhance/0 0.00% | | 0% 0%
PID NAME RCS BCS VCS VECS
4084 gem_wsim |█████▌ ||█ || || | 4086 gem_wsim |█▌ || ||███ || |
==========================================================================
v2: Use intel_context_engine_get_busy_time.
v3: New directory structure.
v4: Rebase.
v5: sysfs_attr_init.
v6: Small tidy in i915_gem_add_client.
v7: Rebase to be engine class based.
v8:
* Always enable stats.
* Walk all client contexts.
v9:
* Skip unsupported engine classes. (Chris)
* Use scheduler caps. (Chris)
v10:
* Use pphwsp runtime only.
--- drivers/gpu/drm/i915/i915_drm_client.c | 110 ++++++++++++++++++++++++- drivers/gpu/drm/i915/i915_drm_client.h | 11 +++ 2 files changed, 120 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_drm_client.c +109 -1
@@ -9,8 +9,13 @@#include <drm/drm_print.h> +#include <uapi/drm/i915_drm.h> +#include "i915_drv.h" #include "i915_drm_client.h" +#include "gem/i915_gem_context.h" +#include "gt/intel_engine_user.h" +#include "i915_drv.h" #include "i915_gem.h" #include "i915_utils.h"
@@ -52,6 +57,104 @@ show_client_pid(struct device *kdev, struct device_attribute *attr, char *buf)return ret; } +static u64 +pphwsp_busy_add (struct i915_gem_context *ctx,unsigned int class) +{ +struct i915_gem_engines *engines =rcu_dereference (ctx->engines); +struct i915_gem_engines_iter it; +struct intel_context *ce; +u64 total =0 ; + +for_each_gem_engine (ce, engines, it) { +if (ce->engine->uabi_class == class) + total += ce->runtime.total; + } + +return total; +} + +static ssize_t +show_client_busy (struct device *kdev,struct device_attribute *attr,char *buf) +{ +struct i915_engine_busy_attribute *i915_attr = +container_of (attr,typeof (*i915_attr), attr); +unsigned int class = i915_attr->engine_class; +struct i915_drm_client *client = i915_attr->client; +u64 total =atomic64_read (&client->past_runtime[class]); +struct list_head *list = &client->ctx_list; +struct i915_gem_context *ctx; + +rcu_read_lock (); +list_for_each_entry_rcu (ctx, list, client_link) { + total +=atomic64_read (&ctx->past_runtime[class]); + total +=pphwsp_busy_add (ctx, class); + } +rcu_read_unlock (); + + total *=RUNTIME_INFO (i915_attr->i915)->cs_timestamp_period_ns; + +return snprintf (buf,PAGE_SIZE ,"%llu\n" , total); +} + +static const char *const uabi_class_names[] = { + [I915_ENGINE_CLASS_RENDER ] ="0" , + [I915_ENGINE_CLASS_COPY ] ="1" , + [I915_ENGINE_CLASS_VIDEO ] ="2" , + [I915_ENGINE_CLASS_VIDEO_ENHANCE ] ="3" , +}; + +static int +__client_register_sysfs_busy (struct i915_drm_client *client) +{ +struct i915_drm_clients *clients = client->clients; +struct drm_i915_private *i915 = +container_of (clients,typeof (*i915), clients); +unsigned int i; +int ret =0 ; + +if (!HAS_LOGICAL_RING_CONTEXTS (i915)) +return 0 ; + + client->busy_root =kobject_create_and_add ("busy" , client->root); +if (!client->busy_root) +return -ENOMEM ; + +for (i =0 ; i <ARRAY_SIZE (uabi_class_names); i++) { +struct i915_engine_busy_attribute *i915_attr = + &client->attr.busy[i]; +struct device_attribute *attr = &i915_attr->attr; + +if (!intel_engine_lookup_user (i915, i,0 )) +continue ; + + i915_attr->client = client; + i915_attr->i915 = i915; + i915_attr->engine_class = i; + +sysfs_attr_init (&attr->attr); + + attr->attr.name = uabi_class_names[i]; + attr->attr.mode =0444 ; + attr->show = show_client_busy; + + ret =sysfs_create_file (client->busy_root, + (struct attribute *)attr); +if (ret) + goto err; + } + +return 0 ; + +err: +kobject_put (client->busy_root); +return ret; +} + +static void __client_unregister_sysfs_busy (struct i915_drm_client *client) +{ +kobject_put (fetch_and_zero (&client->busy_root)); +} +static int __client_register_sysfs (struct i915_drm_client *client) {
@@ -88,9 +191,12 @@ __client_register_sysfs(struct i915_drm_client *client) ret =sysfs_create_file (client->root, (struct attribute *)attr);if (ret) -break ; + goto out; } + ret =__client_register_sysfs_busy (client); + +out:if (ret)kobject_put (client->root);
@@ -99,6 +205,8 @@ __client_register_sysfs(struct i915_drm_client *client)static void __client_unregister_sysfs (struct i915_drm_client *client) { +__client_unregister_sysfs_busy (client); +kobject_put (fetch_and_zero (&client->root)); }
drivers/gpu/drm/i915/i915_drm_client.h +11 -0
@@ -26,6 +26,15 @@ struct i915_drm_clients {struct kobject *root; }; +struct i915_drm_client ; + +struct i915_engine_busy_attribute { +struct device_attribute attr; +struct drm_i915_private *i915; +struct i915_drm_client *client; +unsigned int engine_class; +}; +struct i915_drm_client {struct kref kref;
@@ -44,9 +53,11 @@ struct i915_drm_client {struct i915_drm_clients *clients;struct kobject *root; +struct kobject *busy_root;struct {struct device_attribute pid;struct device_attribute name; +struct i915_engine_busy_attribute busy[MAX_ENGINE_CLASS +1 ]; } attr;/**
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 4/9 drm/i915: Track runtime spent in unreachable intel_contexts
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
As contexts are abandoned we want to remember how much GPU time they used (per class) so later we can used it for smarter purposes.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 13 ++++++++++++- drivers/gpu/drm/i915/gem/i915_gem_context_types.h | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +12 -1
@@ -257,7 +257,19 @@ static void free_engines_rcu(struct rcu_head *rcu) {struct i915_gem_engines *engines = container_of(rcu, struct i915_gem_engines, rcu); +struct i915_gem_context *ctx = engines->ctx; +struct i915_gem_engines_iter it; +struct intel_context *ce; + +/* Transfer accumulated runtime to the parent GEM context. */ +for_each_gem_engine (ce, engines, it) { +unsigned int class = ce->engine->uabi_class; +GEM_BUG_ON (class >=ARRAY_SIZE (ctx->past_runtime)); +atomic64_add (ce->runtime.total, &ctx->past_runtime[class]); + } + +i915_gem_context_put (ctx);i915_sw_fence_fini (&engines->fence);free_engines (engines); }
@@ -278,7 +290,6 @@ engines_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)list_del (&engines->link);spin_unlock_irqrestore (&ctx->stale.lock, flags); } -i915_gem_context_put (engines->ctx);break ;case FENCE_FREE :
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +5 -0
@@ -177,6 +177,11 @@ struct i915_gem_context {spinlock_t lock; struct list_head engines; } stale; + +/** +* @past_runtime: Accumulation of freed intel_context pphwsp runtimes. +*/ +atomic64_t past_runtime[MAX_ENGINE_CLASS +1 ]; };#endif /* __I915_GEM_CONTEXT_TYPES_H__ */
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 6/9 drm/i915: Track all user contexts per client
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
We soon want to start answering questions like how much GPU time is the context belonging to a client which exited still using.
To enable this we start tracking all context belonging to a client on a separate list.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 8 ++++++++ drivers/gpu/drm/i915/gem/i915_gem_context_types.h | 3 +++ drivers/gpu/drm/i915/i915_drm_client.c | 3 +++ drivers/gpu/drm/i915/i915_drm_client.h | 5 +++++ 4 files changed, 19 insertions(+)
drivers/gpu/drm/i915/gem/i915_gem_context.c +8 -0
@@ -358,6 +358,10 @@ static void i915_gem_context_free(struct i915_gem_context *ctx)if (client) {unsigned int i; +spin_lock (&client->ctx_lock); +list_del_rcu (&ctx->client_link); +spin_unlock (&client->ctx_lock); +/* Transfer accumulated runtime to the parent drm client. */ BUILD_BUG_ON (ARRAY_SIZE (client->past_runtime) !=ARRAY_SIZE (ctx->past_runtime));
@@ -964,6 +968,10 @@ static int gem_context_register(struct i915_gem_context *ctx, ctx->client = client; +spin_lock (&client->ctx_lock); +list_add_tail_rcu (&ctx->client_link, &client->ctx_list); +spin_unlock (&client->ctx_lock); +return 0 ; err:
drivers/gpu/drm/i915/gem/i915_gem_context_types.h +3 -0
@@ -103,6 +103,9 @@ struct i915_gem_context {/** client: struct i915_drm_client */ struct i915_drm_client *client; +/** link: &drm_client.context_list */ +struct list_head client_link; +/** * @ref: reference count *
drivers/gpu/drm/i915/i915_drm_client.c +3 -0
@@ -154,6 +154,9 @@ i915_drm_client_add(struct i915_drm_clients *clients, struct task_struct *task)kref_init (&client->kref);mutex_init (&client->update_lock); +spin_lock_init (&client->ctx_lock); +INIT_LIST_HEAD (&client->ctx_list); + client->clients = clients; ret = xa_alloc_cyclic(&clients->xarray, &client->id, client,
drivers/gpu/drm/i915/i915_drm_client.h +5 -0
@@ -9,10 +9,12 @@#include <linux/device.h> #include <linux/kobject.h> #include <linux/kref.h> +#include <linux/list.h> #include <linux/mutex.h> #include <linux/pid.h> #include <linux/rcupdate.h> #include <linux/sched.h> +#include <linux/spinlock.h> #include <linux/xarray.h> #include "gt/intel_engine_types.h"
@@ -36,6 +38,9 @@ struct i915_drm_client {char __rcu *name;bool closed; +spinlock_t ctx_lock;/* For add/remove from ctx_list. */ +struct list_head ctx_list;/* List of contexts belonging to client. */ +struct i915_drm_clients *clients;struct kobject *root;
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 8/9 drm/i915: Track context current active time
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
Track context active (on hardware) status together with the start timestamp.
This will be used to provide better granularity of context runtime reporting in conjunction with already tracked pphwsp accumulated runtime.
The latter is only updated on context save so does not give us visibility to any currently executing work.
As part of the patch the existing runtime tracking data is moved under the new ce->stats member and updated under the seqlock. This provides the ability to atomically read out accumulated plus active runtime.
--- drivers/gpu/drm/i915/gem/i915_gem_context.c | 3 +- drivers/gpu/drm/i915/gt/intel_context.c | 18 +++++- drivers/gpu/drm/i915/gt/intel_context.h | 6 +- drivers/gpu/drm/i915/gt/intel_context_types.h | 24 +++++--- drivers/gpu/drm/i915/gt/intel_lrc.c | 55 +++++++++++++++---- drivers/gpu/drm/i915/gt/selftest_lrc.c | 10 ++-- drivers/gpu/drm/i915/i915_drm_client.c | 2 +- drivers/gpu/drm/i915/i915_gpu_error.c | 4 +- 8 files changed, 90 insertions(+), 32 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +2 -1
@@ -266,7 +266,8 @@ static void free_engines_rcu(struct rcu_head *rcu)unsigned int class = ce->engine->uabi_class;GEM_BUG_ON (class >=ARRAY_SIZE (ctx->past_runtime)); -atomic64_add (ce->runtime.total, &ctx->past_runtime[class]); +atomic64_add (ce->stats.runtime.total, + &ctx->past_runtime[class]); }i915_gem_context_put (ctx);
drivers/gpu/drm/i915/gt/intel_context.c +17 -1
@@ -288,7 +288,7 @@ intel_context_init(struct intel_context *ce, ce->sseu = engine->sseu; ce->ring =__intel_context_ring_size (SZ_4K ); -ewma_runtime_init (&ce->runtime.avg); +ewma_runtime_init (&ce->stats.runtime.avg); ce->vm =i915_vm_get (engine->gt->vm);
@@ -296,6 +296,7 @@ intel_context_init(struct intel_context *ce,INIT_LIST_HEAD (&ce->signals);mutex_init (&ce->pin_mutex); +seqlock_init (&ce->stats.lock);i915_active_init (&ce->active, __intel_context_active, __intel_context_retire);
@@ -390,6 +391,21 @@ struct i915_request *intel_context_create_request(struct intel_context *ce)return rq; } +ktime_t intel_context_get_active_time (struct intel_context *ce) +{ +struct intel_context_stats *stats = &ce->stats; +unsigned int seq; +ktime_t total =0 ; + +do { + seq =read_seqbegin (&stats->lock); +if (stats->active) + total =ktime_sub (ktime_get (), stats->start); + }while (read_seqretry (&stats->lock, seq)); + +return total; +} +#if IS_ENABLED (CONFIG_DRM_I915_SELFTEST )#include "selftest_context.c" #endif
drivers/gpu/drm/i915/gt/intel_context.h +4 -2
@@ -233,7 +233,7 @@ static inline u64 intel_context_get_total_runtime_ns(struct intel_context *ce)const u32 period =RUNTIME_INFO (ce->engine->i915)->cs_timestamp_period_ns; -return READ_ONCE (ce->runtime.total) * period; +return READ_ONCE (ce->stats.runtime.total) * period; }static inline u64 intel_context_get_avg_runtime_ns (struct intel_context *ce)
@@ -241,7 +241,9 @@ static inline u64 intel_context_get_avg_runtime_ns(struct intel_context *ce)const u32 period =RUNTIME_INFO (ce->engine->i915)->cs_timestamp_period_ns; - return mul_u32_u32(ewma_runtime_read(&ce->runtime.avg), period); + return mul_u32_u32(ewma_runtime_read(&ce->stats.runtime.avg), period); } +ktime_t intel_context_get_active_time (struct intel_context *ce); +#endif /* __INTEL_CONTEXT_H__ */
drivers/gpu/drm/i915/gt/intel_context_types.h +16 -8
@@ -12,6 +12,7 @@#include <linux/list.h> #include <linux/mutex.h> #include <linux/types.h> +#include <linux/seqlock.h> #include "i915_active_types.h" #include "i915_utils.h"
@@ -72,14 +73,21 @@ struct intel_context {u64 lrc_desc;u32 tag;/* cookie passed to HW to track this context on submission */ -/* Time on GPU as tracked by the hw. */ -struct { -struct ewma_runtime avg; -u64 total; -u32 last; - I915_SELFTEST_DECLARE(u32 num_underflow); - I915_SELFTEST_DECLARE(u32 max_underflow); - } runtime; +/** stats: Context GPU engine busyness tracking. */ +struct intel_context_stats { +seqlock_t lock; +bool active; +ktime_t start; + +/* Time on GPU as tracked by the hw. */ +struct { +struct ewma_runtime avg; +u64 total; +u32 last; + I915_SELFTEST_DECLARE(u32 num_underflow); + I915_SELFTEST_DECLARE(u32 max_underflow); + } runtime; + } stats;unsigned int active_count;/* protected by timeline->mutex */
drivers/gpu/drm/i915/gt/intel_lrc.c +43 -12
99 lines not on this page · raw message
drivers/gpu/drm/i915/gt/selftest_lrc.c +5 -5
@@ -5479,8 +5479,8 @@ static int __live_pphwsp_runtime(struct intel_engine_cs *engine)if (IS_ERR (ce))return PTR_ERR (ce); - ce->runtime.num_underflow =0 ; - ce->runtime.max_underflow =0 ; + ce->stats.runtime.num_underflow =0 ; + ce->stats.runtime.max_underflow =0 ; do { unsigned int loop = 1024;
@@ -5518,11 +5518,11 @@ static int __live_pphwsp_runtime(struct intel_engine_cs *engine) intel_context_get_avg_runtime_ns(ce)); err =0 ; -if (ce->runtime.num_underflow) { +if (ce->stats.runtime.num_underflow) {pr_err ("%s: pphwsp underflow %u time(s), max %u cycles!\n" , engine->name, - ce->runtime.num_underflow, - ce->runtime.max_underflow); + ce->stats.runtime.num_underflow, + ce->stats.runtime.max_underflow);GEM_TRACE_DUMP (); err = -EOVERFLOW ; }
drivers/gpu/drm/i915/i915_drm_client.c +1 -1
@@ -67,7 +67,7 @@ pphwsp_busy_add(struct i915_gem_context *ctx, unsigned int class)for_each_gem_engine (ce, engines, it) {if (ce->engine->uabi_class == class) - total += ce->runtime.total; + total += ce->stats.runtime.total; }return total;
drivers/gpu/drm/i915/i915_gpu_error.c +2 -2
@@ -1267,8 +1267,8 @@ static bool record_context(struct i915_gem_context_coredump *e, e->guilty =atomic_read (&ctx->guilty_count); e->active =atomic_read (&ctx->active_count); - e->total_runtime = rq->context->runtime.total; - e->avg_runtime =ewma_runtime_read (&rq->context->runtime.avg); + e->total_runtime = rq->context->stats.runtime.total; + e->avg_runtime =ewma_runtime_read (&rq->context->stats.runtime.avg); simulated =i915_gem_context_no_error_capture (ctx);
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
PATCH 9/9 drm/i915: Prefer software tracked context busyness
In reply to Tvrtko UrsulinFrom: Tvrtko Ursulin <[email protected]>
When available prefer context tracked context busyness because it provides visibility into currently executing contexts as well.
--- drivers/gpu/drm/i915/i915_drm_client.c | 68 ++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 5 deletions(-)
drivers/gpu/drm/i915/i915_drm_client.c +63 -5
@@ -96,6 +96,61 @@ show_client_busy(struct device *kdev, struct device_attribute *attr, char *buf)return snprintf (buf,PAGE_SIZE ,"%llu\n" , total); } +static u64 +sw_busy_add (struct i915_gem_context *ctx,unsigned int class) +{ +struct i915_gem_engines *engines =rcu_dereference (ctx->engines); +u32 period_ns =RUNTIME_INFO (ctx->i915)->cs_timestamp_period_ns; +struct i915_gem_engines_iter it; +struct intel_context *ce; +u64 total =0 ; + +for_each_gem_engine (ce, engines, it) { +struct intel_context_stats *stats; +unsigned int seq; +u64 t; + +if (ce->engine->uabi_class != class) +continue ; + + stats = &ce->stats; + +do { + seq =read_seqbegin (&stats->lock); + t = ce->stats.runtime.total * period_ns; + t +=intel_context_get_active_time (ce); + }while (read_seqretry (&stats->lock, seq)); + + total += t; + } + +return total; +} + +static ssize_t +show_client_sw_busy (struct device *kdev, +struct device_attribute *attr, +char *buf) +{ +struct i915_engine_busy_attribute *i915_attr = +container_of (attr,typeof (*i915_attr), attr); +unsigned int class = i915_attr->engine_class; +struct i915_drm_client *client = i915_attr->client; +u32 period_ns =RUNTIME_INFO (i915_attr->i915)->cs_timestamp_period_ns; +u64 total =atomic64_read (&client->past_runtime[class]) * period_ns; +struct list_head *list = &client->ctx_list; +struct i915_gem_context *ctx; + +rcu_read_lock (); +list_for_each_entry_rcu (ctx, list, client_link) { + total +=atomic64_read (&ctx->past_runtime[class]) * period_ns + +sw_busy_add (ctx, class); + } +rcu_read_unlock (); + +return snprintf (buf,PAGE_SIZE ,"%llu\n" , total); +} +static const char *const uabi_class_names[] = { [I915_ENGINE_CLASS_RENDER ] ="0" , [I915_ENGINE_CLASS_COPY ] ="1" ,
@@ -109,6 +164,8 @@ __client_register_sysfs_busy(struct i915_drm_client *client)struct i915_drm_clients *clients = client->clients;struct drm_i915_private *i915 =container_of (clients,typeof (*i915), clients); +bool sw_stats = i915->caps.scheduler & +I915_SCHEDULER_CAP_ENGINE_BUSY_STATS ;unsigned int i;int ret =0 ;
@@ -135,18 +192,19 @@ __client_register_sysfs_busy(struct i915_drm_client *client) attr->attr.name = uabi_class_names[i]; attr->attr.mode =0444 ; - attr->show = show_client_busy; + attr->show = sw_stats ? + show_client_sw_busy : show_client_busy; ret =sysfs_create_file (client->busy_root, (struct attribute *)attr);if (ret) - goto err; + goto out; } -return 0 ; +out: +if (ret) +kobject_put (client->busy_root); -err: -kobject_put (client->busy_root);return ret; }
Signature · 6 lines
2.20.1 _______________________________________________ Intel-gfx mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Patchwork
CI[Intel-gfx] ✗ Fi.CI.BUILD: failure for Per client engine busyness (rev8)
In reply to Tvrtko Ursulin== Series Details ==
== Series Details ==
Series: Per client engine busyness (rev8)
URL : https://patchwork.freedesktop.org/series/70977/
State : failure
== Summary ==
Applying: drm/i915: Update client name on context create
error: sha1 information is lacking or useless (drivers/gpu/drm/i915/i915_drm_client.c).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch' to see the failed patch
Patch failed at 0001 drm/i915: Update client name on context create
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Quoted 5 lines · Tvrtko Ursulin
From: Tvrtko Ursulin <[email protected]>
Another re-spin of the per-client engine busyness series. Highlights from this version:
Broken version with one patch missing, apologies for the spam.
Regards,
Tvrtko
Quoted 80 lines
* Checkpatch cleanup and bits of review feedback only.
Internally we track time spent on engines for each struct
intel_context. This can serve as a building block for several features from the want list:
smarter scheduler decisions, getrusage(2)-like per-GEM-context functionality wanted by some customers, cgroups controller, dynamic SSEU tuning,...Externally, in sysfs, we expose time spent on GPU per client and per engine class.
Sysfs interface enables us to implement a "top-like" tool for GPU tasks. Or with a "screenshot":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
intel-gpu-top - 906/ 955 MHz; 0% RC6; 5.30 Watts; 933 irqs/sIMC reads: 4414 MiB/s
IMC writes: 3805 MiB/sENGINE BUSY
MI_SEMAMI_WAIT
Render/3D/0 93.46% |████████████████████████████████▋ | 0% 0%
Blitter/0 0.00% | | 0% 0%
Video/0 0.00% | | 0% 0%
VideoEnhance/0 0.00% | | 0% 0%PID NAME Render/3D Blitter Video VideoEnhance
2733 neverball |██████▌ || || || | 2047 Xorg |███▊ || || || | 2737 glxgears |█▍ || || || | 2128 xfwm4 | || || || | 2047 Xorg | || || || |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Implementation wise we add a a bunch of files in sysfs like:
# cd /sys/class/drm/card0/clients/ # tree . ├── 7 │ ├── busy │ │ ├── 0 │ │ ├── 1 │ │ ├── 2 │ │ └── 3 │ ├── name │ └── pid ├── 8 │ ├── busy │ │ ├── 0 │ │ ├── 1 │ │ ├── 2 │ │ └── 3 │ ├── name │ └── pid └── 9 ├── busy │ ├── 0 │ ├── 1 │ ├── 2 │ └── 3 ├── name └── pidFiles in 'busy' directories are numbered using the engine class ABI values and they contain accumulated nanoseconds each client spent on engines of a respective class.
It is stil a RFC since it misses dedicated test cases to ensure things really work as advertised.
Tvrtko Ursulin (9):
drm/i915: Update client name on context create drm/i915: Make GEM contexts track DRM clients drm/i915: Use explicit flag to mark unreachableintel_contextdrm/i915: Track runtime spent in unreachableintel_contextsdrm/i915: Track runtime spent in closed GEM contexts drm/i915: Track all user contexts per client drm/i915: Expose per-engine client busyness drm/i915: Track context current active time drm/i915: Prefer software tracked context busyness
Quoted 14 lines
drivers/gpu/drm/i915/gem/i915_gem_context.c | 63 +++- .../gpu/drm/i915/gem/i915_gem_context_types.h | 21 +- .../gpu/drm/i915/gem/i915_gem_execbuffer.c | 2 +- drivers/gpu/drm/i915/gt/intel_context.c | 18 +- drivers/gpu/drm/i915/gt/intel_context.h | 6 +- drivers/gpu/drm/i915/gt/intel_context_types.h | 25 +- drivers/gpu/drm/i915/gt/intel_lrc.c | 55 +++- drivers/gpu/drm/i915/gt/selftest_lrc.c | 10 +- drivers/gpu/drm/i915/i915_debugfs.c | 29 +- drivers/gpu/drm/i915/i915_drm_client.c | 274 +++++++++++++++++- drivers/gpu/drm/i915/i915_drm_client.h | 33 ++- drivers/gpu/drm/i915/i915_gpu_error.c | 25 +- 12 files changed, 473 insertions(+), 88 deletions(-)
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx