From patchwork Mon Apr 17 15:56:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13214304 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A5264C77B7A for ; Mon, 17 Apr 2023 15:56:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4BE0610E417; Mon, 17 Apr 2023 15:56:41 +0000 (UTC) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4663910E1B1; Mon, 17 Apr 2023 15:56:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681746998; x=1713282998; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=atDvmZyUtCQfHIE+ErRNt7jlzXViu3axN8KeAcTKy6o=; b=AhNs0O6kfZig3bJBiD1sPuSx+CQnH62g/2o37r0xOzkaD17eHKLZHr3V JORfzY0mpchRWuXIAOZmb1IGh1qlvUbLPSKFmivFaopyCr2TqWy2P6mYv BDp4JpcMO9C4F1x2BjqqyPLmelFuq8P95pBcjQ+HbbbnYfT9BHMTAVFFn MGXMHv/eMK+sRLEw29gZLOWNdHgBdukFZKYNToSNMAChL0S5K2cpb6FB+ aRo3AgcDPhBp1CVqtBc7NyvoL3Sgz6xP6/4CapuMpBLnXjNuuGd77mvyv s6zhGR4CVLfkPyx31tUtFxGmYgrChsLkC4KQ7ZPDC4E7FVm+s7ecZyuuw w==; X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="372804617" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="372804617" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="690718268" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="690718268" Received: from gtohallo-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.232.210]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:25 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [RFC 1/6] drm: Add common fdinfo helper Date: Mon, 17 Apr 2023 16:56:08 +0100 Message-Id: <20230417155613.4143258-2-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> References: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Rob Clark , Daniel Vetter , Emil Velikov , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Rob Clark Handle a bit of the boiler-plate in a single case, and make it easier to add some core tracked stats. v2: Update drm-usage-stats.rst, 64b client-id, rename drm_show_fdinfo Reviewed-by: Daniel Vetter Signed-off-by: Rob Clark --- Documentation/gpu/drm-usage-stats.rst | 10 +++++++- drivers/gpu/drm/drm_file.c | 35 +++++++++++++++++++++++++++ include/drm/drm_drv.h | 7 ++++++ include/drm/drm_file.h | 4 +++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Documentation/gpu/drm-usage-stats.rst b/Documentation/gpu/drm-usage-stats.rst index b46327356e80..2ab32c40e93c 100644 --- a/Documentation/gpu/drm-usage-stats.rst +++ b/Documentation/gpu/drm-usage-stats.rst @@ -126,7 +126,15 @@ percentage utilization of the engine, whereas drm-engine- only reflects time active without considering what frequency the engine is operating as a percentage of it's maximum frequency. +Implementation Details +====================== + +Drivers should use drm_show_fdinfo() in their `struct file_operations`, and +implement &drm_driver.show_fdinfo if they wish to provide any stats which +are not provided by drm_show_fdinfo(). But even driver specific stats should +be documented above and where possible, aligned with other drivers. + Driver specific implementations -=============================== +------------------------------- :ref:`i915-usage-stats` diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c index c1018c470047..37b4f76a5191 100644 --- a/drivers/gpu/drm/drm_file.c +++ b/drivers/gpu/drm/drm_file.c @@ -148,6 +148,7 @@ bool drm_dev_needs_global_mutex(struct drm_device *dev) */ struct drm_file *drm_file_alloc(struct drm_minor *minor) { + static atomic64_t ident = ATOMIC_INIT(0); struct drm_device *dev = minor->dev; struct drm_file *file; int ret; @@ -156,6 +157,8 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor) if (!file) return ERR_PTR(-ENOMEM); + /* Get a unique identifier for fdinfo: */ + file->client_id = atomic64_inc_return(&ident); file->pid = get_pid(task_tgid(current)); file->minor = minor; @@ -868,6 +871,38 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) } EXPORT_SYMBOL(drm_send_event); +/** + * drm_show_fdinfo - helper for drm file fops + * @seq_file: output stream + * @f: the device file instance + * + * Helper to implement fdinfo, for userspace to query usage stats, etc, of a + * process using the GPU. See also &drm_driver.show_fdinfo. + * + * For text output format description please see Documentation/gpu/drm-usage-stats.rst + */ +void drm_show_fdinfo(struct seq_file *m, struct file *f) +{ + struct drm_file *file = f->private_data; + struct drm_device *dev = file->minor->dev; + struct drm_printer p = drm_seq_file_printer(m); + + drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name); + drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id); + + if (dev_is_pci(dev->dev)) { + struct pci_dev *pdev = to_pci_dev(dev->dev); + + drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n", + pci_domain_nr(pdev->bus), pdev->bus->number, + PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); + } + + if (dev->driver->show_fdinfo) + dev->driver->show_fdinfo(&p, file); +} +EXPORT_SYMBOL(drm_show_fdinfo); + /** * mock_drm_getfile - Create a new struct file for the drm device * @minor: drm minor to wrap (e.g. #drm_device.primary) diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index b419c59c4bef..89e2706cac56 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -401,6 +401,13 @@ struct drm_driver { struct drm_device *dev, uint32_t handle, uint64_t *offset); + /** + * @show_fdinfo: + * + * Print device specific fdinfo. See Documentation/gpu/drm-usage-stats.rst. + */ + void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f); + /** @major: driver major number */ int major; /** @minor: driver minor number */ diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index ecffe24e2b1b..7d9b3c65cbc1 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -258,6 +258,9 @@ struct drm_file { /** @pid: Process that opened this file. */ struct pid *pid; + /** @client_id: A unique id for fdinfo */ + u64 client_id; + /** @magic: Authentication magic, see @authenticated. */ drm_magic_t magic; @@ -438,6 +441,7 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e); void drm_send_event_timestamp_locked(struct drm_device *dev, struct drm_pending_event *e, ktime_t timestamp); +void drm_show_fdinfo(struct seq_file *m, struct file *f); struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags); From patchwork Mon Apr 17 15:56:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13214307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 52D00C77B76 for ; Mon, 17 Apr 2023 15:56:56 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0FB2010E40A; Mon, 17 Apr 2023 15:56:49 +0000 (UTC) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2344810E1B1; Mon, 17 Apr 2023 15:56:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681746999; x=1713282999; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZZ5sdfxvwan0NtZOVxjrO+xnxP2dQu3WGW/RRDuIchE=; b=ClrCDA+XUz8/0+QCswLkJ5apEEe5vwTEXtS1RElRZW10EGwW2lavdAOb B1wrF4jrRz4mFrTBtaVcpLeHByOOaxt7hogN9TYpIWa37SZxfZHzmWV+A RlMfB3bHCGEy9k9bwaHsJPzvacFl6+EQ4RI4srxwgN65Rvh28HdkNqL9t OR6wDC97XUL8A+6U+dy6LJW6DoPJQHfUqYpxj02Y11jUD65CWdoWmE0Qd 5im/K/GlWHBpP079m/KnAXW5OXvU6qcerpoGh1IRJDQFToG9+p4ciKrwl TxOtosG7JQEKSpSUDUGu7lWdHRTmi1IJZrTdlhVX1VuPfXQnftgry7q7y g==; X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="372804626" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="372804626" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="690718275" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="690718275" Received: from gtohallo-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.232.210]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:27 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [RFC 2/6] drm/i915: Use the fdinfo helper Date: Mon, 17 Apr 2023 16:56:09 +0100 Message-Id: <20230417155613.4143258-3-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> References: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Rob Clark , Tvrtko Ursulin , Daniel Vetter , Emil Velikov , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin Use the common fdinfo helper for printing the basics. Remove now unused client id allocation code. Signed-off-by: Tvrtko Ursulin Cc: Rob Clark --- drivers/gpu/drm/i915/i915_driver.c | 6 +-- drivers/gpu/drm/i915/i915_drm_client.c | 65 ++++---------------------- drivers/gpu/drm/i915/i915_drm_client.h | 22 ++------- drivers/gpu/drm/i915/i915_drv.h | 2 - drivers/gpu/drm/i915/i915_gem.c | 6 +-- 5 files changed, 18 insertions(+), 83 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c index a52db8a80900..6493548c69bf 100644 --- a/drivers/gpu/drm/i915/i915_driver.c +++ b/drivers/gpu/drm/i915/i915_driver.c @@ -244,8 +244,6 @@ static int i915_driver_early_probe(struct drm_i915_private *dev_priv) if (ret < 0) goto err_rootgt; - i915_drm_clients_init(&dev_priv->clients, dev_priv); - i915_gem_init_early(dev_priv); /* This must be called before any calls to HAS_PCH_* */ @@ -279,7 +277,6 @@ static void i915_driver_late_release(struct drm_i915_private *dev_priv) intel_power_domains_cleanup(dev_priv); i915_gem_cleanup_early(dev_priv); intel_gt_driver_late_release_all(dev_priv); - i915_drm_clients_fini(&dev_priv->clients); intel_region_ttm_device_fini(dev_priv); vlv_suspend_cleanup(dev_priv); i915_workqueues_cleanup(dev_priv); @@ -1700,7 +1697,7 @@ static const struct file_operations i915_driver_fops = { .compat_ioctl = i915_ioc32_compat_ioctl, .llseek = noop_llseek, #ifdef CONFIG_PROC_FS - .show_fdinfo = i915_drm_client_fdinfo, + .show_fdinfo = drm_show_fdinfo, #endif }; @@ -1800,6 +1797,7 @@ static const struct drm_driver i915_drm_driver = { .open = i915_driver_open, .lastclose = i915_driver_lastclose, .postclose = i915_driver_postclose, + .show_fdinfo = i915_drm_client_fdinfo, .prime_handle_to_fd = drm_gem_prime_handle_to_fd, .prime_fd_to_handle = drm_gem_prime_fd_to_handle, diff --git a/drivers/gpu/drm/i915/i915_drm_client.c b/drivers/gpu/drm/i915/i915_drm_client.c index e8fa172ebe5e..c654984189f7 100644 --- a/drivers/gpu/drm/i915/i915_drm_client.c +++ b/drivers/gpu/drm/i915/i915_drm_client.c @@ -17,64 +17,29 @@ #include "i915_gem.h" #include "i915_utils.h" -void i915_drm_clients_init(struct i915_drm_clients *clients, - struct drm_i915_private *i915) -{ - clients->i915 = i915; - clients->next_id = 0; - - xa_init_flags(&clients->xarray, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ); -} - -struct i915_drm_client *i915_drm_client_add(struct i915_drm_clients *clients) +struct i915_drm_client *i915_drm_client_alloc(void) { struct i915_drm_client *client; - struct xarray *xa = &clients->xarray; - int ret; client = kzalloc(sizeof(*client), GFP_KERNEL); if (!client) - return ERR_PTR(-ENOMEM); - - xa_lock_irq(xa); - ret = __xa_alloc_cyclic(xa, &client->id, client, xa_limit_32b, - &clients->next_id, GFP_KERNEL); - xa_unlock_irq(xa); - if (ret < 0) - goto err; + return NULL; kref_init(&client->kref); spin_lock_init(&client->ctx_lock); INIT_LIST_HEAD(&client->ctx_list); - client->clients = clients; return client; - -err: - kfree(client); - - return ERR_PTR(ret); } void __i915_drm_client_free(struct kref *kref) { struct i915_drm_client *client = container_of(kref, typeof(*client), kref); - struct xarray *xa = &client->clients->xarray; - unsigned long flags; - xa_lock_irqsave(xa, flags); - __xa_erase(xa, client->id); - xa_unlock_irqrestore(xa, flags); kfree(client); } -void i915_drm_clients_fini(struct i915_drm_clients *clients) -{ - GEM_BUG_ON(!xa_empty(&clients->xarray)); - xa_destroy(&clients->xarray); -} - #ifdef CONFIG_PROC_FS static const char * const uabi_class_names[] = { [I915_ENGINE_CLASS_RENDER] = "render", @@ -101,38 +66,34 @@ static u64 busy_add(struct i915_gem_context *ctx, unsigned int class) } static void -show_client_class(struct seq_file *m, +show_client_class(struct drm_printer *p, + struct drm_i915_private *i915, struct i915_drm_client *client, unsigned int class) { - const struct list_head *list = &client->ctx_list; + const unsigned int capacity = i915->engine_uabi_class_count[class]; u64 total = atomic64_read(&client->past_runtime[class]); - const unsigned int capacity = - client->clients->i915->engine_uabi_class_count[class]; struct i915_gem_context *ctx; rcu_read_lock(); - list_for_each_entry_rcu(ctx, list, client_link) + list_for_each_entry_rcu(ctx, &client->ctx_list, client_link) total += busy_add(ctx, class); rcu_read_unlock(); if (capacity) - seq_printf(m, "drm-engine-%s:\t%llu ns\n", + drm_printf(p, "drm-engine-%s:\t%llu ns\n", uabi_class_names[class], total); if (capacity > 1) - seq_printf(m, "drm-engine-capacity-%s:\t%u\n", + drm_printf(p, "drm-engine-capacity-%s:\t%u\n", uabi_class_names[class], capacity); } -void i915_drm_client_fdinfo(struct seq_file *m, struct file *f) +void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file) { - struct drm_file *file = f->private_data; struct drm_i915_file_private *file_priv = file->driver_priv; struct drm_i915_private *i915 = file_priv->i915; - struct i915_drm_client *client = file_priv->client; - struct pci_dev *pdev = to_pci_dev(i915->drm.dev); unsigned int i; /* @@ -141,12 +102,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f) * ****************************************************************** */ - seq_printf(m, "drm-driver:\t%s\n", i915->drm.driver->name); - seq_printf(m, "drm-pdev:\t%04x:%02x:%02x.%d\n", - pci_domain_nr(pdev->bus), pdev->bus->number, - PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); - seq_printf(m, "drm-client-id:\t%u\n", client->id); - /* * Temporarily skip showing client engine information with GuC submission till * fetching engine busyness is implemented in the GuC submission backend @@ -155,6 +110,6 @@ void i915_drm_client_fdinfo(struct seq_file *m, struct file *f) return; for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++) - show_client_class(m, client, i); + show_client_class(p, i915, file_priv->client, i); } #endif diff --git a/drivers/gpu/drm/i915/i915_drm_client.h b/drivers/gpu/drm/i915/i915_drm_client.h index 69496af996d9..4c18b99e10a4 100644 --- a/drivers/gpu/drm/i915/i915_drm_client.h +++ b/drivers/gpu/drm/i915/i915_drm_client.h @@ -9,20 +9,13 @@ #include #include #include -#include #include #define I915_LAST_UABI_ENGINE_CLASS I915_ENGINE_CLASS_COMPUTE -struct drm_i915_private; - -struct i915_drm_clients { - struct drm_i915_private *i915; - - struct xarray xarray; - u32 next_id; -}; +struct drm_file; +struct drm_printer; struct i915_drm_client { struct kref kref; @@ -32,17 +25,12 @@ struct i915_drm_client { 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; - /** * @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, - struct drm_i915_private *i915); - static inline struct i915_drm_client * i915_drm_client_get(struct i915_drm_client *client) { @@ -57,12 +45,10 @@ static inline void i915_drm_client_put(struct i915_drm_client *client) kref_put(&client->kref, __i915_drm_client_free); } -struct i915_drm_client *i915_drm_client_add(struct i915_drm_clients *clients); +struct i915_drm_client *i915_drm_client_alloc(void); #ifdef CONFIG_PROC_FS -void i915_drm_client_fdinfo(struct seq_file *m, struct file *f); +void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file); #endif -void i915_drm_clients_fini(struct i915_drm_clients *clients); - #endif /* !__I915_DRM_CLIENT_H__ */ diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h index fe7eeafe9cff..eb739fb9cdbb 100644 --- a/drivers/gpu/drm/i915/i915_drv.h +++ b/drivers/gpu/drm/i915/i915_drv.h @@ -347,8 +347,6 @@ struct drm_i915_private { struct i915_pmu pmu; - struct i915_drm_clients clients; - /* The TTM device structure. */ struct ttm_device bdev; diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c index 0a78bdbd36b1..db4c4af5f4b7 100644 --- a/drivers/gpu/drm/i915/i915_gem.c +++ b/drivers/gpu/drm/i915/i915_gem.c @@ -1306,11 +1306,9 @@ int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file) if (!file_priv) goto err_alloc; - client = i915_drm_client_add(&i915->clients); - if (IS_ERR(client)) { - ret = PTR_ERR(client); + client = i915_drm_client_alloc(); + if (!client) goto err_client; - } file->driver_priv = file_priv; file_priv->i915 = i915; From patchwork Mon Apr 17 15:56:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13214305 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3DED2C77B76 for ; Mon, 17 Apr 2023 15:56:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 835C510E41D; Mon, 17 Apr 2023 15:56:42 +0000 (UTC) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id B4BD410E40D; Mon, 17 Apr 2023 15:56:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681746999; x=1713282999; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GLWFkkWJByBHQ6XYRqSC3Pfg48gXQS8Zm0DWoBY3EJ0=; b=DRCxOsRoq1pM0NybXVASaW+Kmd9EA3tadnl3be/dsZ5cBhk7nfkHRwwr 4OasgwTo78VoF806cwoNyuOfKCTapxBOzwKhortUzSPaWP8H7BQbUm4RK 8kq4pdHR4hkClkxrmFtBT/TChncQVbB5Ophf7i2LPkRytbKn2aDvLIkjh VP50ZAK5HTd2ISUwZQLybW+KmqU2hETwXv76b7HYpNTzFXAzxNpsiENoX fw7945A/BFfpj63r8HS0deS9GdUAAvH5yM9A5zHO8OwmlGwcyhuETpK50 Qym7Bs70ecRg7yCyx9Ek8K4UAi7UbijbDFHfXiP7opIFoXGt8/4s1a6zv w==; X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="372804639" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="372804639" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="690718292" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="690718292" Received: from gtohallo-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.232.210]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:30 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [RFC 3/6] drm: Add fdinfo memory stats Date: Mon, 17 Apr 2023 16:56:10 +0100 Message-Id: <20230417155613.4143258-4-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> References: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tvrtko Ursulin , Daniel Vetter , Emil Velikov , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin Add support to dump GEM stats to fdinfo. Signed-off-by: Tvrtko Ursulin --- Documentation/gpu/drm-usage-stats.rst | 12 +++++++ drivers/gpu/drm/drm_file.c | 52 +++++++++++++++++++++++++++ include/drm/drm_drv.h | 7 ++++ include/drm/drm_file.h | 8 +++++ 4 files changed, 79 insertions(+) diff --git a/Documentation/gpu/drm-usage-stats.rst b/Documentation/gpu/drm-usage-stats.rst index 2ab32c40e93c..8273a41b2fb0 100644 --- a/Documentation/gpu/drm-usage-stats.rst +++ b/Documentation/gpu/drm-usage-stats.rst @@ -21,6 +21,7 @@ File format specification - File shall contain one key value pair per one line of text. - Colon character (`:`) must be used to delimit keys and values. +- Caret (`^`) is also a reserved character. - All keys shall be prefixed with `drm-`. - Whitespace between the delimiter and first non-whitespace character shall be ignored when parsing. @@ -105,6 +106,17 @@ object belong to this client, in the respective memory region. Default unit shall be bytes with optional unit specifiers of 'KiB' or 'MiB' indicating kibi- or mebi-bytes. +- drm-memory-^size: [KiB|MiB] +- drm-memory-^shared: [KiB|MiB] +- drm-memory-^resident: [KiB|MiB] +- drm-memory-^purgeable: [KiB|MiB] +- drm-memory-^active: [KiB|MiB] + +Resident category is identical to the drm-memory- key and two should be +mutually exclusive. + +TODO more description text... + - drm-cycles- Engine identifier string must be the same as the one specified in the diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c index 37b4f76a5191..e202f79e816d 100644 --- a/drivers/gpu/drm/drm_file.c +++ b/drivers/gpu/drm/drm_file.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include "drm_crtc_internal.h" @@ -871,6 +872,54 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) } EXPORT_SYMBOL(drm_send_event); +static void +print_stat(struct drm_printer *p, const char *stat, const char *region, u64 sz) +{ + const char *units[] = {"", " KiB", " MiB"}; + unsigned int u; + + if (sz == ~0ull) /* Not supported by the driver. */ + return; + + for (u = 0; u < ARRAY_SIZE(units) - 1; u++) { + if (sz < SZ_1K) + break; + sz = div_u64(sz, SZ_1K); + } + + drm_printf(p, "drm-memory-%s^%s:\t%llu%s\n", + region, stat, sz, units[u]); +} + +static void print_memory_stats(struct drm_printer *p, struct drm_file *file) +{ + struct drm_device *dev = file->minor->dev; + struct drm_fdinfo_memory_stat *stats; + unsigned int num, i; + char **regions; + + regions = dev->driver->query_fdinfo_memory_regions(dev, &num); + + stats = kcalloc(num, sizeof(*stats), GFP_KERNEL); + if (!stats) + return; + + dev->driver->query_fdinfo_memory_stats(file, stats); + + for (i = 0; i < num; i++) { + if (!regions[i]) /* Allow sparse name arrays. */ + continue; + + print_stat(p, "size", regions[i], stats[i].size); + print_stat(p, "shared", regions[i], stats[i].shared); + print_stat(p, "resident", regions[i], stats[i].resident); + print_stat(p, "purgeable", regions[i], stats[i].purgeable); + print_stat(p, "active", regions[i], stats[i].active); + } + + kfree(stats); +} + /** * drm_show_fdinfo - helper for drm file fops * @seq_file: output stream @@ -900,6 +949,9 @@ void drm_show_fdinfo(struct seq_file *m, struct file *f) if (dev->driver->show_fdinfo) dev->driver->show_fdinfo(&p, file); + + if (dev->driver->query_fdinfo_memory_regions) + print_memory_stats(&p, file); } EXPORT_SYMBOL(drm_show_fdinfo); diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index 89e2706cac56..ccc1cd98d2aa 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -35,6 +35,7 @@ #include struct drm_file; +struct drm_fdinfo_memory_stat; struct drm_gem_object; struct drm_master; struct drm_minor; @@ -408,6 +409,12 @@ struct drm_driver { */ void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f); + char ** (*query_fdinfo_memory_regions)(struct drm_device *dev, + unsigned int *num); + + void (*query_fdinfo_memory_stats)(struct drm_file *f, + struct drm_fdinfo_memory_stat *stat); + /** @major: driver major number */ int major; /** @minor: driver minor number */ diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index 7d9b3c65cbc1..00d48beeac5c 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -375,6 +375,14 @@ struct drm_file { #endif }; +struct drm_fdinfo_memory_stat { + u64 size; + u64 shared; + u64 resident; + u64 purgeable; + u64 active; +}; + /** * drm_is_primary_client - is this an open file of the primary node * @file_priv: DRM file From patchwork Mon Apr 17 15:56:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13214306 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A3519C77B7C for ; Mon, 17 Apr 2023 15:56:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F3A2D10E24D; Mon, 17 Apr 2023 15:56:42 +0000 (UTC) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 536BC10E24D; Mon, 17 Apr 2023 15:56:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681747000; x=1713283000; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=19F6hSCCQIYmXYRORvRZl/lUhx925hpUlhZPRAIaMVk=; b=akBK+o/39On6CXUwUtBIOny13k5g1RH/6Q12KRPglUqeOhis4lmGxix5 5ll/d2NsTF0j47svN3kbkb+RUUXPo30ftwR5wP84k8MxKYN1YCuTbmnen tJ/p+328MAlhBe0PxwzuwVZpDxT3U9ORijoBON9y8EUhJrd/h9SIeCTZ/ ox65csvOfjcldijOFH85rw7IoELucmtVt7g5Up5gNSd1VstQuB+llxrr1 S5wWFXbn6rbfyiJbT3QEJ7TKRZitatI8xNWHNSUnobxrLJFrVHtc5vQhW FUKBgo27B6+zLt6sw7ltc7LSKWt63WAlycbddduZd0jz8ekCk1xMOvyMY g==; X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="372804649" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="372804649" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="690718313" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="690718313" Received: from gtohallo-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.232.210]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:32 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [RFC 4/6] drm: Add simple fdinfo memory helpers Date: Mon, 17 Apr 2023 16:56:11 +0100 Message-Id: <20230417155613.4143258-5-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> References: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tvrtko Ursulin , Daniel Vetter , Emil Velikov , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin For drivers who only wish to show one memory region called 'system, and only account the GEM buffer object handles under it. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/drm_file.c | 45 ++++++++++++++++++++++++++++++++++++++ include/drm/drm_file.h | 6 +++++ 2 files changed, 51 insertions(+) diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c index e202f79e816d..1e70669dddf7 100644 --- a/drivers/gpu/drm/drm_file.c +++ b/drivers/gpu/drm/drm_file.c @@ -872,6 +872,51 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) } EXPORT_SYMBOL(drm_send_event); +static void +add_obj(struct drm_gem_object *obj, struct drm_fdinfo_memory_stat *stats) +{ + u64 sz = obj->size; + + stats[0].size += sz; + + if (obj->handle_count > 1) + stats[0].shared += sz; + + if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) + stats[0].active += sz; + + /* Not supported. */ + stats[0].resident = ~0ull; + stats[0].purgeable = ~0ull; +} + +char ** +drm_query_fdinfo_system_region(struct drm_device *dev, unsigned int *num) +{ + static char *region[] = { + "system", + }; + + *num = 1; + + return region; +} +EXPORT_SYMBOL(drm_query_fdinfo_system_region); + +void +drm_query_fdinfo_system_memory(struct drm_file *file, + struct drm_fdinfo_memory_stat *stats) +{ + struct drm_gem_object *obj; + int id; + + spin_lock(&file->table_lock); + idr_for_each_entry(&file->object_idr, obj, id) + add_obj(obj, stats); + spin_unlock(&file->table_lock); +} +EXPORT_SYMBOL(drm_query_fdinfo_system_memory); + static void print_stat(struct drm_printer *p, const char *stat, const char *region, u64 sz) { diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index 00d48beeac5c..dd7c6fb2c975 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -383,6 +383,12 @@ struct drm_fdinfo_memory_stat { u64 active; }; +char **drm_query_fdinfo_system_region(struct drm_device *dev, + unsigned int *num); +void drm_query_fdinfo_system_memory(struct drm_file *file, + struct drm_fdinfo_memory_stat *stats); + + /** * drm_is_primary_client - is this an open file of the primary node * @file_priv: DRM file From patchwork Mon Apr 17 15:56:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13214308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 25CB1C77B7A for ; Mon, 17 Apr 2023 15:56:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DEEBA10E443; Mon, 17 Apr 2023 15:56:49 +0000 (UTC) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0152510E24D; Mon, 17 Apr 2023 15:56:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681747001; x=1713283001; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=v0q6qBk1finMRPc4VpbFT2NMst6/U62L6g6UmWOKdWk=; b=LcctRhNSGBegt0aZ8Ghen6acg0H1DDq8tNsUKocc6OZL9VFX+JE8JjBv BHOceXCZnNWjVoqwSHyvMqfT9Os4EaUOCIECzcp10hD5NRXP/pV+9juqW zQuCWeyRObl84HJXbyC4lxCWQMkWceg1GPWopuSQKQVJcfGaTbHjHX+2o iX7H8yU7GiaFUR7I1AlsDXHSKwNmXiwyLXapXBEHXbF42c5Kr0ppZP/J1 5OYwVhrUFvkSI1veNv9jIpPLTC0wD5numVCFe+UQKqXdADyH7cF2vCED7 e+9QwPEfclR/4zn/ddYuKi9AFFsi2UNS4AhP++pa9j0d4IasaAykwPV5d g==; X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="372804657" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="372804657" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="690718347" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="690718347" Received: from gtohallo-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.232.210]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:34 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [RFC 5/6] drm/msm: Add basic memory stats Date: Mon, 17 Apr 2023 16:56:12 +0100 Message-Id: <20230417155613.4143258-6-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> References: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Rob Clark , Tvrtko Ursulin , Daniel Vetter , Emil Velikov , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin Use DRM helpers for implementing basic memory stats. Signed-off-by: Tvrtko Ursulin Cc: Rob Clark --- drivers/gpu/drm/msm/msm_drv.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/msm/msm_drv.c b/drivers/gpu/drm/msm/msm_drv.c index 060c7689a739..7662103b5999 100644 --- a/drivers/gpu/drm/msm/msm_drv.c +++ b/drivers/gpu/drm/msm/msm_drv.c @@ -1090,6 +1090,10 @@ static const struct drm_driver msm_driver = { .prime_fd_to_handle = drm_gem_prime_fd_to_handle, .gem_prime_import_sg_table = msm_gem_prime_import_sg_table, .gem_prime_mmap = msm_gem_prime_mmap, +#ifdef CONFIG_PROC_FS + .query_fdinfo_memory_regions = drm_query_fdinfo_system_region, + .query_fdinfo_memory_stats = drm_query_fdinfo_system_memory, +#endif #ifdef CONFIG_DEBUG_FS .debugfs_init = msm_debugfs_init, #endif From patchwork Mon Apr 17 15:56:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13214309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CB232C77B7C for ; Mon, 17 Apr 2023 15:56:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9BE2410E452; Mon, 17 Apr 2023 15:56:50 +0000 (UTC) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 55D3310E418; Mon, 17 Apr 2023 15:56:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681747002; x=1713283002; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=p5vaX8w0+kDbnKWICo5v+DxZRla8dbBN2J5LMKRINFQ=; b=Oz7tACLRuRUJ7hTRmyvGKP9Q8UIyBb6Urja4V+NRXxeIcZwwoE5Up09u flPxjWa8AOlD4PN7zA/g4rb1NsUJ1lmORXABUHj9Oi8xwdtsT8l7kSCau iNDCW1Q7tIAxC0gygr3IsrjJ4OsPePqo0IJUiAlaktRYv/hUQXF/E7YYe qJqH2YAVhWA0wkA4rQp4f5mJ3dXYnUS3C2ilVh9JP+T2ahcOopi1oV0fG NlCEVIiomN43kwfPb5ltpIH36BAOO3XArVsw8sJNgk1CMY/prS6QQwyar DsBnMmi5x69N2Fwjtlzxu6+xnYBXUDkfkibEXOav+knxTe3F6TpT/FYFJ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="372804669" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="372804669" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10683"; a="690718352" X-IronPort-AV: E=Sophos;i="5.99,204,1677571200"; d="scan'208";a="690718352" Received: from gtohallo-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.213.232.210]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Apr 2023 08:56:37 -0700 From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [RFC 6/6] drm/i915: Implement fdinfo memory stats printing Date: Mon, 17 Apr 2023 16:56:13 +0100 Message-Id: <20230417155613.4143258-7-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> References: <20230417155613.4143258-1-tvrtko.ursulin@linux.intel.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tvrtko Ursulin , Daniel Vetter , Emil Velikov , =?utf-8?q?Christian_K=C3=B6nig?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin Show how more driver specific set of memory stats could be shown, more specifically where object can reside in multiple regions, showing all the supported stats, and where there is more to show than just user visible objects. WIP... Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/i915/i915_driver.c | 5 ++ drivers/gpu/drm/i915/i915_drm_client.c | 102 +++++++++++++++++++++++++ drivers/gpu/drm/i915/i915_drm_client.h | 8 ++ drivers/gpu/drm/i915/i915_drv.h | 2 + 4 files changed, 117 insertions(+) diff --git a/drivers/gpu/drm/i915/i915_driver.c b/drivers/gpu/drm/i915/i915_driver.c index 6493548c69bf..4c70206cbc27 100644 --- a/drivers/gpu/drm/i915/i915_driver.c +++ b/drivers/gpu/drm/i915/i915_driver.c @@ -1806,6 +1806,11 @@ static const struct drm_driver i915_drm_driver = { .dumb_create = i915_gem_dumb_create, .dumb_map_offset = i915_gem_dumb_mmap_offset, +#ifdef CONFIG_PROC_FS + .query_fdinfo_memory_regions = i915_query_fdinfo_memory_regions, + .query_fdinfo_memory_stats = i915_query_fdinfo_memory_stats, +#endif + .ioctls = i915_ioctls, .num_ioctls = ARRAY_SIZE(i915_ioctls), .fops = &i915_driver_fops, diff --git a/drivers/gpu/drm/i915/i915_drm_client.c b/drivers/gpu/drm/i915/i915_drm_client.c index c654984189f7..65857c68bdb3 100644 --- a/drivers/gpu/drm/i915/i915_drm_client.c +++ b/drivers/gpu/drm/i915/i915_drm_client.c @@ -12,6 +12,7 @@ #include #include "gem/i915_gem_context.h" +#include "intel_memory_region.h" #include "i915_drm_client.h" #include "i915_file_private.h" #include "i915_gem.h" @@ -112,4 +113,105 @@ void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file) for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++) show_client_class(p, i915, file_priv->client, i); } + +char ** +i915_query_fdinfo_memory_regions(struct drm_device *dev, unsigned int *num) +{ + struct drm_i915_private *i915 = to_i915(dev); + struct intel_memory_region *mr; + enum intel_region_id id; + + /* FIXME move to init */ + for_each_memory_region(mr, i915, id) { + if (!i915->mm.region_names[id]) + i915->mm.region_names[id] = mr->name; + } + + *num = id; + + return i915->mm.region_names; +} + +static void +add_obj(struct drm_i915_gem_object *obj, struct drm_fdinfo_memory_stat *stats) +{ + struct intel_memory_region *mr; + u64 sz = obj->base.size; + enum intel_region_id id; + unsigned int i; + + if (!obj) + return; + + /* Attribute size and shared to all possible memory regions. */ + for (i = 0; i < obj->mm.n_placements; i++) { + mr = obj->mm.placements[i]; + id = mr->id; + + stats[id].size += sz; + if (obj->base.handle_count > 1) + stats[id].shared += sz; + } + + /* Attribute other categories to only the current region. */ + mr = obj->mm.region; + if (mr) + id = mr->id; + else + id = INTEL_REGION_SMEM; + + if (!i915_gem_object_has_pages(obj)) + return; + + stats[id].resident += sz; + + if (!dma_resv_test_signaled(obj->base.resv, dma_resv_usage_rw(true))) + stats[id].active += sz; + else if (i915_gem_object_is_shrinkable(obj) && + obj->mm.madv == I915_MADV_DONTNEED) + stats[id].purgeable += sz; +} + +void +i915_query_fdinfo_memory_stats(struct drm_file *file, + struct drm_fdinfo_memory_stat *stats) +{ + struct drm_i915_file_private *file_priv = file->driver_priv; + struct i915_drm_client *client = file_priv->client; + struct drm_gem_object *drm_obj; + struct i915_gem_context *ctx; + int id; + + /* + * FIXME - we can do this better and in fewer passes if we are to start + * exporting proper memory stats. + */ + + /* User created objects */ + spin_lock(&file->table_lock); + idr_for_each_entry(&file->object_idr, drm_obj, id) + add_obj(to_intel_bo(drm_obj), stats); + spin_unlock(&file->table_lock); + + /* Contexts, rings, timelines, page tables, ... */ + rcu_read_lock(); + list_for_each_entry_rcu(ctx, &client->ctx_list, client_link) { + struct i915_gem_engines_iter it; + struct intel_context *ce; + + for_each_gem_engine(ce, rcu_dereference(ctx->engines), it) { + /* FIXME races?! */ + if (ce->state) + add_obj(ce->state->obj, stats); + if (ce->timeline && ce->timeline->hwsp_ggtt) + add_obj(ce->timeline->hwsp_ggtt->obj, stats); + if (ce->ring && ce->ring->vma) + add_obj(ce->ring->vma->obj, stats); + } + + /* TODO vtx->vm page table backing objects */ + } + rcu_read_unlock(); +} + #endif diff --git a/drivers/gpu/drm/i915/i915_drm_client.h b/drivers/gpu/drm/i915/i915_drm_client.h index 4c18b99e10a4..622936c51903 100644 --- a/drivers/gpu/drm/i915/i915_drm_client.h +++ b/drivers/gpu/drm/i915/i915_drm_client.h @@ -14,7 +14,10 @@ #define I915_LAST_UABI_ENGINE_CLASS I915_ENGINE_CLASS_COMPUTE +struct drm_device; struct drm_file; +struct drm_fdinfo_memory_stat; +struct drm_gem_object; struct drm_printer; struct i915_drm_client { @@ -49,6 +52,11 @@ struct i915_drm_client *i915_drm_client_alloc(void); #ifdef CONFIG_PROC_FS void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file); + +char **i915_query_fdinfo_memory_regions(struct drm_device *dev, + unsigned int *num); +void i915_query_fdinfo_memory_stats(struct drm_file *file, + struct drm_fdinfo_memory_stat *stats); #endif #endif /* !__I915_DRM_CLIENT_H__ */ diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h index eb739fb9cdbb..b84d2f0ed2cb 100644 --- a/drivers/gpu/drm/i915/i915_drv.h +++ b/drivers/gpu/drm/i915/i915_drv.h @@ -161,6 +161,8 @@ struct i915_gem_mm { struct intel_memory_region *regions[INTEL_REGION_UNKNOWN]; + char *region_names[INTEL_REGION_UNKNOWN]; + struct notifier_block oom_notifier; struct notifier_block vmap_notifier; struct shrinker shrinker;