From nobody Thu Dec 18 23:02:44 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EBA7EC7EE23 for ; Wed, 24 May 2023 16:01:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237275AbjEXQBv (ORCPT ); Wed, 24 May 2023 12:01:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238221AbjEXQBP (ORCPT ); Wed, 24 May 2023 12:01:15 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46D0C173F; Wed, 24 May 2023 09:00:37 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1ae851f2a7dso3547295ad.0; Wed, 24 May 2023 09:00:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684944035; x=1687536035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mSsQ3aGvmnPAex9q1f35gfIysAp2jDmPNa2ojXepp/s=; b=daZaE0EeAHwkdRv2C6j9IV3QCFFXw9Wh5HyGxmD4X6nO6OMMHj+iZ7Dip1JNUN5nI1 O0K9zqNhvrWWur2nKL1lF4VbYU2yQTpb9Ajy5uJvXEewQjcOpCWoB38CLE178W2pL1Cn aaQUiGcFKz4+W8A+ut9TO5H9ySXlv+pzSnefk1RoPC47mZrGb2wclTKU7eGzyVSXgGKk EfIF/22EABy6uvgsTmi4m0RipfYboNaG7efpvxhZywiET9SqnjFzYsCHEp9fQiFL9m5l ciH7Uh2jUzzz++TrlgZLai0ZyXGOWLAwvCbRlh/w0WvhZ42rbcrGMOUUDFCmsINKwzT2 1Few== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684944035; x=1687536035; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mSsQ3aGvmnPAex9q1f35gfIysAp2jDmPNa2ojXepp/s=; b=b58vAR6XlX+z/0je69QYaJW8ukDK6dOTiZhVzLEDqC1ywMCOyRK5sDSKeyTRLmLYNF SNAAQEa3PpQxRstjqJnz9BC6oHQB1GEVkxSTor2AjexYjHFnmDa54ElGRandM4ZoWmq0 MqveQvxE24N0wjb31b5a9OLEiWMIgxTQHFukZNN5z9AynKFzVfP4u0EgfG18bI8JpOgZ Y3o6AFKhuYHeytpIID7mGe9Vy19h8PAsCoNcpRnh/bRiIAydO43jpVfN0UL1EejCKXjd RRrhX5CAaSGKYhaMIVsAESfwpvxB44dJUenUcrL4Jn9RFTGb/DVV1D3hDDi0neajWmGf aorA== X-Gm-Message-State: AC+VfDzoHOtPI6GfUJKXNQdehH7KPQRgjpEoY3EGsQNg5Wc3vLfwzOf6 /+X/yTYUgPHRean9Z514LJ4= X-Google-Smtp-Source: ACHHUZ7geA+6WGmVWjO9aZlyK6YbXch4LoyqXgsv1ysgMkQF2FWLqTSSwww3g7YRaHTpyefs2NRP6w== X-Received: by 2002:a17:903:8c4:b0:1ac:b03c:a58e with SMTP id lk4-20020a17090308c400b001acb03ca58emr22930072plb.25.1684944033892; Wed, 24 May 2023 09:00:33 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id g7-20020a1709029f8700b001ac4d3d3f72sm8841588plq.296.2023.05.24.09.00.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 09:00:33 -0700 (PDT) From: Rob Clark To: dri-devel@lists.freedesktop.org Cc: freedreno@lists.freedesktop.org, Daniel Vetter , Tvrtko Ursulin , Boris Brezillon , Christopher Healy , Emil Velikov , =?UTF-8?q?Christian=20K=C3=B6nig?= , Rob Clark , Daniel Vetter , Tvrtko Ursulin , Dave Airlie , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Jonathan Corbet , linux-doc@vger.kernel.org (open list:DOCUMENTATION), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v5 5/7] drm: Add fdinfo memory stats Date: Wed, 24 May 2023 08:59:35 -0700 Message-Id: <20230524155956.382440-6-robdclark@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230524155956.382440-1-robdclark@gmail.com> References: <20230524155956.382440-1-robdclark@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Rob Clark Add support to dump GEM stats to fdinfo. v2: Fix typos, change size units to match docs, use div_u64 v3: Do it in core v4: more kerneldoc v5: doc fixes v6: Actually use u64, bit more comment docs Signed-off-by: Rob Clark Reviewed-by: Emil Velikov Reviewed-by: Daniel Vetter Acked-by: Tvrtko Ursulin Acked-by: Dave Airlie --- Documentation/gpu/drm-usage-stats.rst | 54 +++++++++++---- drivers/gpu/drm/drm_file.c | 99 ++++++++++++++++++++++++++- include/drm/drm_file.h | 28 ++++++++ include/drm/drm_gem.h | 32 +++++++++ 4 files changed, 200 insertions(+), 13 deletions(-) diff --git a/Documentation/gpu/drm-usage-stats.rst b/Documentation/gpu/drm-= usage-stats.rst index 552195fb1ea3..d012eb56885e 100644 --- a/Documentation/gpu/drm-usage-stats.rst +++ b/Documentation/gpu/drm-usage-stats.rst @@ -45,37 +45,43 @@ Mandatory fully standardised keys --------------------------------- =20 - drm-driver: =20 String shall contain the name this driver registered as via the respective `struct drm_driver` data structure. =20 Optional fully standardised keys -------------------------------- =20 +Identification +^^^^^^^^^^^^^^ + - drm-pdev: =20 For PCI devices this should contain the PCI slot address of the device in question. =20 - drm-client-id: =20 Unique value relating to the open DRM file descriptor used to distinguish duplicated and shared file descriptors. Conceptually the value should map = 1:1 to the in kernel representation of `struct drm_file` instances. =20 Uniqueness of the value shall be either globally unique, or unique within = the scope of each device, in which case `drm-pdev` shall be present as well. =20 Userspace should make sure to not double account any usage statistics by u= sing the above described criteria in order to associate data to individual clie= nts. =20 +Utilization +^^^^^^^^^^^ + - drm-engine-: ns =20 GPUs usually contain multiple execution engines. Each shall be given a sta= ble and unique name (str), with possible values documented in the driver speci= fic documentation. =20 Value shall be in specified time units which the respective GPU engine spe= nt busy executing workloads belonging to this client. =20 Values are not required to be constantly monotonic if it makes the driver @@ -86,32 +92,20 @@ value until a monotonic update is seen. =20 - drm-engine-capacity-: =20 Engine identifier string must be the same as the one specified in the drm-engine- tag and shall contain a greater than zero number in case = the exported engine corresponds to a group of identical hardware engines. =20 In the absence of this tag parser shall assume capacity of one. Zero capac= ity is not allowed. =20 -- drm-memory-: [KiB|MiB] - -Each possible memory type which can be used to store buffer objects by the -GPU in question shall be given a stable and unique name to be returned as = the -string here. - -Value shall reflect the amount of storage currently consumed by the buffer -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-cycles-: =20 Engine identifier string must be the same as the one specified in the drm-engine- tag and shall contain the number of busy cycles for the g= iven engine. =20 Values are not required to be constantly monotonic if it makes the driver implementation easier, but are required to catch up with the previously re= ported larger value within a reasonable period. Upon observing a value lower than= what was previously read, userspace is expected to stay with that larger previo= us @@ -119,20 +113,56 @@ value until a monotonic update is seen. =20 - drm-maxfreq-: [Hz|MHz|KHz] =20 Engine identifier string must be the same as the one specified in the drm-engine- tag and shall contain the maximum frequency for the given engine. Taken together with drm-cycles-, this can be used to calcula= te percentage utilization of the engine, whereas drm-engine- only reflec= ts time active without considering what frequency the engine is operating as a percentage of it's maximum frequency. =20 +Memory +^^^^^^ + +- drm-memory-: [KiB|MiB] + +Each possible memory type which can be used to store buffer objects by the +GPU in question shall be given a stable and unique name to be returned as = the +string here. The name "memory" is reserved to refer to normal system memo= ry. + +Value shall reflect the amount of storage currently consumed by the buffer +objects 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-shared-: [KiB|MiB] + +The total size of buffers that are shared with another file (ie. have more +than a single handle). + +- drm-total-: [KiB|MiB] + +The total size of buffers that including shared and private memory. + +- drm-resident-: [KiB|MiB] + +The total size of buffers that are resident in the specified region. + +- drm-purgeable-: [KiB|MiB] + +The total size of buffers that are purgeable. + +- drm-active-: [KiB|MiB] + +The total size of buffers that are active on one or more engines. + Implementation Details =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 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 sho= uld be documented above and where possible, aligned with other drivers. =20 Driver specific implementations ------------------------------- diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c index 37b4f76a5191..883d83bc0e3d 100644 --- a/drivers/gpu/drm/drm_file.c +++ b/drivers/gpu/drm/drm_file.c @@ -35,20 +35,21 @@ #include #include #include #include #include #include =20 #include #include #include +#include #include =20 #include "drm_crtc_internal.h" #include "drm_internal.h" #include "drm_legacy.h" =20 /* from BKL pushdown */ DEFINE_MUTEX(drm_global_mutex); =20 bool drm_dev_needs_global_mutex(struct drm_device *dev) @@ -864,23 +865,119 @@ EXPORT_SYMBOL(drm_send_event_locked); void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) { unsigned long irqflags; =20 spin_lock_irqsave(&dev->event_lock, irqflags); drm_send_event_helper(dev, e, 0); spin_unlock_irqrestore(&dev->event_lock, irqflags); } EXPORT_SYMBOL(drm_send_event); =20 +static void print_size(struct drm_printer *p, const char *stat, + const char *region, u64 sz) +{ + const char *units[] =3D {"", " KiB", " MiB"}; + unsigned u; + + for (u =3D 0; u < ARRAY_SIZE(units) - 1; u++) { + if (sz < SZ_1K) + break; + sz =3D div_u64(sz, SZ_1K); + } + + drm_printf(p, "drm-%s-%s:\t%llu%s\n", stat, region, sz, units[u]); +} + +/** + * drm_print_memory_stats - A helper to print memory stats + * @p: The printer to print output to + * @stats: The collected memory stats + * @supported_status: Bitmask of optional stats which are available + * @region: The memory region + * + */ +void drm_print_memory_stats(struct drm_printer *p, + const struct drm_memory_stats *stats, + enum drm_gem_object_status supported_status, + const char *region) +{ + print_size(p, "total", region, stats->private + stats->shared); + print_size(p, "shared", region, stats->shared); + print_size(p, "active", region, stats->active); + + if (supported_status & DRM_GEM_OBJECT_RESIDENT) + print_size(p, "resident", region, stats->resident); + + if (supported_status & DRM_GEM_OBJECT_PURGEABLE) + print_size(p, "purgeable", region, stats->purgeable); +} +EXPORT_SYMBOL(drm_print_memory_stats); + +/** + * drm_show_memory_stats - Helper to collect and show standard fdinfo memo= ry stats + * @p: the printer to print output to + * @file: the DRM file + * + * Helper to iterate over GEM objects with a handle allocated in the speci= fied + * file. + */ +void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file) +{ + struct drm_gem_object *obj; + struct drm_memory_stats status =3D {}; + enum drm_gem_object_status supported_status; + int id; + + spin_lock(&file->table_lock); + idr_for_each_entry (&file->object_idr, obj, id) { + enum drm_gem_object_status s =3D 0; + + if (obj->funcs && obj->funcs->status) { + s =3D obj->funcs->status(obj); + supported_status =3D DRM_GEM_OBJECT_RESIDENT | + DRM_GEM_OBJECT_PURGEABLE; + } + + if (obj->handle_count > 1) { + status.shared +=3D obj->size; + } else { + status.private +=3D obj->size; + } + + if (s & DRM_GEM_OBJECT_RESIDENT) { + status.resident +=3D obj->size; + } else { + /* If already purged or not yet backed by pages, don't + * count it as purgeable: + */ + s &=3D ~DRM_GEM_OBJECT_PURGEABLE; + } + + if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) { + status.active +=3D obj->size; + + /* If still active, don't count as purgeable: */ + s &=3D ~DRM_GEM_OBJECT_PURGEABLE; + } + + if (s & DRM_GEM_OBJECT_PURGEABLE) + status.purgeable +=3D obj->size; + } + spin_unlock(&file->table_lock); + + drm_print_memory_stats(p, &status, supported_status, "memory"); +} +EXPORT_SYMBOL(drm_show_memory_stats); + /** * drm_show_fdinfo - helper for drm file fops - * @seq_file: output stream + * @m: 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-usa= ge-stats.rst */ void drm_show_fdinfo(struct seq_file *m, struct file *f) { struct drm_file *file =3D f->private_data; diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index 7d9b3c65cbc1..966912053cb0 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -34,20 +34,21 @@ #include #include =20 #include =20 #include =20 struct dma_fence; struct drm_file; struct drm_device; +struct drm_printer; struct device; struct file; =20 /* * FIXME: Not sure we want to have drm_minor here in the end, but to avoid * header include loops we need it here for now. */ =20 /* Note that the order of this enum is ABI (it determines * /dev/dri/renderD* numbers). @@ -434,15 +435,42 @@ int drm_event_reserve_init(struct drm_device *dev, struct drm_file *file_priv, struct drm_pending_event *p, struct drm_event *e); void drm_event_cancel_free(struct drm_device *dev, struct drm_pending_event *p); void drm_send_event_locked(struct drm_device *dev, struct drm_pending_even= t *e); 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); + +/** + * struct drm_memory_stats - GEM object stats associated + * @shared: Total size of GEM objects shared between processes + * @private: Total size of GEM objects + * @resident: Total size of GEM objects backing pages + * @purgeable: Total size of GEM objects that can be purged (resident and = not active) + * @active: Total size of GEM objects active on one or more engines + * + * Used by drm_print_memory_stats() + */ +struct drm_memory_stats { + u64 shared; + u64 private; + u64 resident; + u64 purgeable; + u64 active; +}; + +enum drm_gem_object_status; + +void drm_print_memory_stats(struct drm_printer *p, + const struct drm_memory_stats *stats, + enum drm_gem_object_status supported_status, + const char *region); + +void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file); void drm_show_fdinfo(struct seq_file *m, struct file *f); =20 struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags); =20 #endif /* _DRM_FILE_H_ */ diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index b8efd836edef..bbc721870c13 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -35,20 +35,39 @@ */ =20 #include #include =20 #include =20 struct iosys_map; struct drm_gem_object; =20 +/** + * enum drm_gem_object_status - bitmask of object state for fdinfo reporti= ng + * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinne= d) + * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace + * + * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_fun= cs.status + * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABL= E if + * it still active or not resident, in which case drm_show_fdinfo() will n= ot + * account for it as purgeable. So drivers do not need to check if the bu= ffer + * is idle and resident to return this bit. (Ie. userspace can mark a buf= fer + * as purgeable even while it is still busy on the GPU.. it does not _actu= ally_ + * become puregeable until it becomes idle. The status gem object func do= es + * not need to consider this.) + */ +enum drm_gem_object_status { + DRM_GEM_OBJECT_RESIDENT =3D BIT(0), + DRM_GEM_OBJECT_PURGEABLE =3D BIT(1), +}; + /** * struct drm_gem_object_funcs - GEM object functions */ struct drm_gem_object_funcs { /** * @free: * * Deconstructor for drm_gem_objects. * * This callback is mandatory. @@ -167,20 +186,33 @@ struct drm_gem_object_funcs { /** * @evict: * * Evicts gem object out from memory. Used by the drm_gem_object_evict() * helper. Returns 0 on success, -errno otherwise. * * This callback is optional. */ int (*evict)(struct drm_gem_object *obj); =20 + /** + * @status: + * + * The optional status callback can return additional object state + * which determines which stats the object is counted against. The + * callback is called under table_lock. Racing against object status + * change is "harmless", and the callback can expect to not race + * against object destruction. + * + * Called by drm_show_memory_stats(). + */ + enum drm_gem_object_status (*status)(struct drm_gem_object *obj); + /** * @vm_ops: * * Virtual memory operations used with mmap. * * This is optional but necessary for mmap support. */ const struct vm_operations_struct *vm_ops; }; =20 --=20 2.40.1