drm/i915: Track runtime spent in closed and unreachable GEM contexts
authorTvrtko Ursulin <tvrtko.ursulin@intel.com>
Fri, 1 Apr 2022 14:22:00 +0000 (15:22 +0100)
committerTvrtko Ursulin <tvrtko.ursulin@intel.com>
Tue, 5 Apr 2022 07:39:03 +0000 (08:39 +0100)
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.

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.

v2:
 * Size past runtimes array by uabi class, not internal.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Reviewed-by: Aravind Iddamsetty <aravind.iddamsetty@intel.com> # v1
Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk> # v1
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Reviewed-by: Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20220401142205.3123159-4-tvrtko.ursulin@linux.intel.com
drivers/gpu/drm/i915/gem/i915_gem_context.c
drivers/gpu/drm/i915/i915_drm_client.h

index 643951d..2ac6dca 100644 (file)
@@ -1030,23 +1030,44 @@ static void free_engines_rcu(struct rcu_head *rcu)
        free_engines(engines);
 }
 
+static void accumulate_runtime(struct i915_drm_client *client,
+                              struct i915_gem_engines *engines)
+{
+       struct i915_gem_engines_iter it;
+       struct intel_context *ce;
+
+       if (!client)
+               return;
+
+       /* 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(client->past_runtime));
+               atomic64_add(intel_context_get_total_runtime_ns(ce),
+                            &client->past_runtime[class]);
+       }
+}
+
 static int
 engines_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
 {
        struct i915_gem_engines *engines =
                container_of(fence, typeof(*engines), fence);
+       struct i915_gem_context *ctx = engines->ctx;
 
        switch (state) {
        case FENCE_COMPLETE:
                if (!list_empty(&engines->link)) {
-                       struct i915_gem_context *ctx = engines->ctx;
                        unsigned long flags;
 
                        spin_lock_irqsave(&ctx->stale.lock, flags);
                        list_del(&engines->link);
                        spin_unlock_irqrestore(&ctx->stale.lock, flags);
                }
-               i915_gem_context_put(engines->ctx);
+               accumulate_runtime(ctx->client, engines);
+               i915_gem_context_put(ctx);
+
                break;
 
        case FENCE_FREE:
index e8986ad..cbc3161 100644 (file)
@@ -9,6 +9,10 @@
 #include <linux/kref.h>
 #include <linux/xarray.h>
 
+#include "gt/intel_engine_types.h"
+
+#define I915_LAST_UABI_ENGINE_CLASS I915_ENGINE_CLASS_VIDEO_ENHANCE
+
 struct drm_i915_private;
 
 struct i915_drm_clients {
@@ -24,6 +28,11 @@ struct i915_drm_client {
        unsigned int id;
 
        struct i915_drm_clients *clients;
+
+       /**
+        * @past_runtime: Accumulation of pphwsp runtimes from closed contexts.
+        */
+       atomic64_t past_runtime[I915_LAST_UABI_ENGINE_CLASS + 1];
 };
 
 void i915_drm_clients_init(struct i915_drm_clients *clients,