Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1618280rwl; Wed, 12 Apr 2023 16:10:26 -0700 (PDT) X-Google-Smtp-Source: AKy350ZVC4Mo1bOLgxXFJ2asNm3l+8G+zXxQ1F/hzeNqpY+a+TzhF+PIwrosNBd0ft/LzjAfz45C X-Received: by 2002:a17:907:1191:b0:94a:4875:262d with SMTP id uz17-20020a170907119100b0094a4875262dmr416102ejb.67.1681341025927; Wed, 12 Apr 2023 16:10:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681341025; cv=none; d=google.com; s=arc-20160816; b=CgvZnHtIPacTxSBD0UED8d8uM+inID4S4V1ucTraRdX5OeVIjUBtDu9ix4n6kXG9VH zWTmxVON/C1L92vMwGfsZT9/o3Lea8Ldd+f3B4mvNMHxSmj/IbvAdfSknDRoAMkPEnoe nAjTFcp/+2pi4JN+PjBpgTwwN55TAJppKQDqRgfSeSkQGXLrak0u48r7owOjiF5vf4NX 1KamF0iZjKUcEwh2KYWbrmtyHGBrndinP9iyxD9byMBYmHOti9HiyusfN6JVSe3ITka4 phRZYB9gq07UGc2YfHUEa2n2r1P9g/oRGdaYNUFasYMFvwLhBArXkWtrvkZ6HJSFmXR1 hwpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=geumVPtdfdeonNbJRvQRqI8DZnvMBPVyWVWG9NtZCuE=; b=Hw5U1Pog5TzZiy/kGCP6W/PIXajSgmPlnxms4xE6wXJIpLFY8M0rAvOQItdP9gZfvw yxqjFO0BenV5BVdY/5RKw1HIvSMz+fPhFjlokmHyj2acsa48RtK352UCb7GpQPvfhTab n0YJmoBBEKx1NldexPcj39jp3vk2mFpRDuOPsec7yQmX9/9XGIn6Bnx/xgAKbk1bIeGT aMT9jsM8IC+KZNGVa6KlMLf4pPQVsMnMuFbY5Dtyi0JfeFJYqTafhCgW5QJs5Eld3KpM A9iWBWM6oTTWeFKqM71P7pq5u8eN1V9YhbJ+XnpCJ+23c/I33irh4TDQTbKK+d9CW0Dt EiVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=ATps2cQz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id if8-20020a170906df4800b0094a8a702461si93270ejc.364.2023.04.12.16.10.02; Wed, 12 Apr 2023 16:10:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=ATps2cQz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229635AbjDLWoC (ORCPT + 99 others); Wed, 12 Apr 2023 18:44:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229897AbjDLWns (ORCPT ); Wed, 12 Apr 2023 18:43:48 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D76B7DB9; Wed, 12 Apr 2023 15:43:37 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-51870e745bfso782479a12.2; Wed, 12 Apr 2023 15:43:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681339417; 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=geumVPtdfdeonNbJRvQRqI8DZnvMBPVyWVWG9NtZCuE=; b=ATps2cQzsHjp2Awpl3uuwTjgh7vygigJUTrlBrNgwlzPytl1gYSdhpkH8e0PjnFWFU kAODEM4CPn0vunvQGMwPw7W57CANlRTN1eMEB4bs+TZCtsxhR3IPkrXpyzbPCWSW8tRl BdzHrm1X3RWXwDmNOOYE9+H/tAHvcxxdxVHEC8r0m5E/4zTcwnonBVaM4WMB9ry8c7Lv 6ENttFGk7zdFWXvyOm3iV4/XAKOvg8e+4NV3H6CViKepyJmEcq8Jghxkx56OoH+Qpiyr ZJhYOTLji+bvvBtrK+mNGoDMQiCoPlOdkOzceBZ6+s8tpxQBezB4/x93lIJCmhfpilrz lRtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681339417; 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=geumVPtdfdeonNbJRvQRqI8DZnvMBPVyWVWG9NtZCuE=; b=IOvZTU+JuYz0J/Slcv+1KfBGsmBVwKpFFV55N80epwu4O+dzNaP9D0Dkw1gm6jAY43 LdrASb92fo8pJ1d59BXYGIGlQi0Ckf0qYUPEAn2bWX82rX6LLSWrI9NbvXJP9UyKQwY8 J+t2x3qIxbK7PZ99EkQtyDr7/50pXrFegsMpdL6EmxTxXEd7roT6aj+UBVvZY/Y8gVL4 PEA0229lS92UWhkyV9R6PdVA9B1PQdzJXXeVzYkKqEh3ZmIF9Y0tsAfPM1vItwgd4c54 6VXFYV1LSP3cK281IcidtUTomTOAZKWwwtbyvH4fig2bYFEs4C4We6UeQ2PSvWLsKK/4 W00w== X-Gm-Message-State: AAQBX9d3U/NCzOTXmHycuF/LbGnKuaDNNOkbrN9EbdV7VgVA4lrwG8V3 +gi5ZFunpRrIEDJToTzqx+4= X-Received: by 2002:a05:6a00:244c:b0:63a:65a9:10db with SMTP id d12-20020a056a00244c00b0063a65a910dbmr634262pfj.7.1681339416832; Wed, 12 Apr 2023 15:43:36 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id t22-20020aa79396000000b00635d381b57csm7144133pfe.205.2023.04.12.15.43.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 15:43:36 -0700 (PDT) From: Rob Clark To: dri-devel@lists.freedesktop.org Cc: linux-arm-msm@vger.kernel.org, freedreno@lists.freedesktop.org, Boris Brezillon , Tvrtko Ursulin , Christopher Healy , Emil Velikov , =?UTF-8?q?Christian=20K=C3=B6nig?= , Daniel Vetter , Rob Clark , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Jonathan Corbet , linux-doc@vger.kernel.org (open list:DOCUMENTATION), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v4 5/6] drm: Add fdinfo memory stats Date: Wed, 12 Apr 2023 15:42:57 -0700 Message-Id: <20230412224311.23511-6-robdclark@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230412224311.23511-1-robdclark@gmail.com> References: <20230412224311.23511-1-robdclark@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Rob Clark Reviewed-by: Emil Velikov Reviewed-by: Daniel Vetter --- Documentation/gpu/drm-usage-stats.rst | 21 ++++++++ drivers/gpu/drm/drm_file.c | 76 +++++++++++++++++++++++++++ include/drm/drm_file.h | 1 + include/drm/drm_gem.h | 30 +++++++++++ 4 files changed, 128 insertions(+) diff --git a/Documentation/gpu/drm-usage-stats.rst b/Documentation/gpu/drm-usage-stats.rst index 2ab32c40e93c..80003e27e28e 100644 --- a/Documentation/gpu/drm-usage-stats.rst +++ b/Documentation/gpu/drm-usage-stats.rst @@ -105,6 +105,27 @@ 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-shared-memory: [KiB|MiB] + +The total size of buffers that are shared with another file (ie. have more +than a single handle). + +- drm-private-memory: [KiB|MiB] + +The total size of buffers that are not shared with another file. + +- drm-resident-memory: [KiB|MiB] + +The total size of buffers that are resident in system memory. + +- drm-purgeable-memory: [KiB|MiB] + +The total size of buffers that are purgeable. + +- drm-active-memory: [KiB|MiB] + +The total size of buffers that are active on one or more rings. + - 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 6d5bdd684ae2..04a7ed7eba8e 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,79 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) } EXPORT_SYMBOL(drm_send_event); +static void print_size(struct drm_printer *p, const char *stat, size_t sz) +{ + const char *units[] = {"", " KiB", " MiB"}; + unsigned u; + + for (u = 0; u < ARRAY_SIZE(units) - 1; u++) { + if (sz < SZ_1K) + break; + sz = div_u64(sz, SZ_1K); + } + + drm_printf(p, "%s:\t%zu%s\n", stat, sz, units[u]); +} + +static void print_memory_stats(struct drm_printer *p, struct drm_file *file) +{ + struct drm_gem_object *obj; + struct { + size_t shared; + size_t private; + size_t resident; + size_t purgeable; + size_t active; + } size = {0}; + bool has_status = false; + int id; + + spin_lock(&file->table_lock); + idr_for_each_entry (&file->object_idr, obj, id) { + enum drm_gem_object_status s = 0; + + if (obj->funcs && obj->funcs->status) { + s = obj->funcs->status(obj); + has_status = true; + } + + if (obj->handle_count > 1) { + size.shared += obj->size; + } else { + size.private += obj->size; + } + + if (s & DRM_GEM_OBJECT_RESIDENT) { + size.resident += obj->size; + } else { + /* If already purged or not yet backed by pages, don't + * count it as purgeable: + */ + s &= ~DRM_GEM_OBJECT_PURGEABLE; + } + + if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) { + size.active += obj->size; + + /* If still active, don't count as purgeable: */ + s &= ~DRM_GEM_OBJECT_PURGEABLE; + } + + if (s & DRM_GEM_OBJECT_PURGEABLE) + size.purgeable += obj->size; + } + spin_unlock(&file->table_lock); + + print_size(p, "drm-shared-memory", size.shared); + print_size(p, "drm-private-memory", size.private); + print_size(p, "drm-active-memory", size.active); + + if (has_status) { + print_size(p, "drm-resident-memory", size.resident); + print_size(p, "drm-purgeable-memory", size.purgeable); + } +} + /** * drm_show_fdinfo - helper for drm file fops * @seq_file: output stream @@ -900,6 +974,8 @@ void drm_show_fdinfo(struct seq_file *m, struct file *f) if (dev->driver->show_fdinfo) dev->driver->show_fdinfo(&p, file); + + print_memory_stats(&p, file); } EXPORT_SYMBOL(drm_show_fdinfo); diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index 6de6d0e9c634..919284bb4f1d 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -41,6 +41,7 @@ struct dma_fence; struct drm_file; struct drm_device; +struct drm_printer; struct device; struct file; diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h index 189fd618ca65..9ebd2820ad1f 100644 --- a/include/drm/drm_gem.h +++ b/include/drm/drm_gem.h @@ -42,6 +42,25 @@ struct iosys_map; struct drm_gem_object; +/** + * enum drm_gem_object_status - bitmask of object state for fdinfo reporting + * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned) + * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace + * + * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status + * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if + * it still active or not resident, in which case drm_show_fdinfo() will not + * account for it as purgeable. So drivers do not need to check if the buffer + * is idle and resident to return this bit. (Ie. userspace can mark a buffer + * as purgeable even while it is still busy on the GPU.. it does not _actually_ + * become puregeable until it becomes idle. The status gem object func does + * not need to consider this.) + */ +enum drm_gem_object_status { + DRM_GEM_OBJECT_RESIDENT = BIT(0), + DRM_GEM_OBJECT_PURGEABLE = BIT(1), +}; + /** * struct drm_gem_object_funcs - GEM object functions */ @@ -174,6 +193,17 @@ struct drm_gem_object_funcs { */ int (*evict)(struct drm_gem_object *obj); + /** + * @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. + */ + enum drm_gem_object_status (*status)(struct drm_gem_object *obj); + /** * @vm_ops: * -- 2.39.2