From patchwork Mon Apr 10 21:06:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 13206767 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 115C7C77B71 for ; Mon, 10 Apr 2023 21:06:30 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 993DD10E25B; Mon, 10 Apr 2023 21:06:24 +0000 (UTC) Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2B29410E25B; Mon, 10 Apr 2023 21:06:23 +0000 (UTC) Received: by mail-pj1-x102b.google.com with SMTP id v9so11068862pjk.0; Mon, 10 Apr 2023 14:06:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1681160781; 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=PJFK/82i55S3LMJhngVaMIQtHLPKCOXYIQAVhENxSX4=; b=iQ1YcGademZken7tt39Vxwt10Q+8aCRwzy9R1BvI2zLub/OEjldjGNyD8wjrxIK14d C1Nipbmugr14pmM1yuk+yn7B5j7cWCilIr/gnf95raKNcnvyxasiDe6xKvcx9zho1H31 Dhwnxoo47lY3KdBWBTBMJjqdBeo0K1XA1dYLozfXnoheJPlfBlUy4rytViAgQ7gqBy8g g/EaDX8ZuCd3VDCnsaT3VJr3nfCtqHt8Fzxkaq57G234PjoOOQiIgZOPoU3blqZY50Cn 2m5fOkdqw3WQ1j3OTvSU6Xh1YhluZNhyLcuhKDEfl5UvhWtTmAHlmGwASBkLSD5TyorI SJHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681160781; 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=PJFK/82i55S3LMJhngVaMIQtHLPKCOXYIQAVhENxSX4=; b=jGwFjCYIqvYj+q+vCZNDOh/9vhVB7hLVNel0jUSWCnJwlNCpPTcI7mWZ6L32Z0sFy/ LyksR48BxJ/YVojc1OomogX7TzV2yCmD1qa/UXD4ymln6jJpIUDNzepx06MpXXHSZRm3 2fKs7nkvelJ2R778zYFv5/NXe4KYQJiwavRv8XlDfRmYsLAglk3L0Z+o/aFn4GHy7AD6 UPlLc+x91YecuwaqB8yUvfMcd8CNk3WmntHJCN2Rjb0fiQPCcfLn+GEMtyOwRBhBNwGH wgJU2NTlcjqOOs6QQdovBpceFfIl9O4X9rTcHXn02szAsqTajIk8C1bH7c6Mw3mk2Kki IEhQ== X-Gm-Message-State: AAQBX9ev+p6ZaUpRu1c6mGCY5vyS02yxp2mFsWvqComihghTPZMqDFZq smNBoMQOMqKZINkKv/HxIERzpW0dBcA= X-Google-Smtp-Source: AKy350amaaLhS95M7Dkn5QyPjiWDMhBzzAAuXASGjiYpVy3PCNTBkyN0n8dOQhViJMNf3n/OVhcRHA== X-Received: by 2002:a17:90b:1b4c:b0:237:24eb:99d8 with SMTP id nv12-20020a17090b1b4c00b0023724eb99d8mr14642142pjb.19.1681160781506; Mon, 10 Apr 2023 14:06:21 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id iy18-20020a170903131200b001a644662ee0sm2029320plb.18.2023.04.10.14.06.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Apr 2023 14:06:21 -0700 (PDT) From: Rob Clark To: dri-devel@lists.freedesktop.org Subject: [PATCH v2 1/2] drm: Add fdinfo memory stats Date: Mon, 10 Apr 2023 14:06:06 -0700 Message-Id: <20230410210608.1873968-2-robdclark@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230410210608.1873968-1-robdclark@gmail.com> References: <20230410210608.1873968-1-robdclark@gmail.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 , Thomas Zimmermann , Jonathan Corbet , linux-arm-msm@vger.kernel.org, "open list:DOCUMENTATION" , Emil Velikov , Christopher Healy , open list , Boris Brezillon , freedreno@lists.freedesktop.org Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Rob Clark Add a helper to dump memory stats to fdinfo. For the things the drm core isn't aware of, use a callback. v2: Fix typos, change size units to match docs, use div_u64 Signed-off-by: Rob Clark Reviewed-by: Emil Velikov --- Documentation/gpu/drm-usage-stats.rst | 21 +++++++ drivers/gpu/drm/drm_file.c | 79 +++++++++++++++++++++++++++ include/drm/drm_file.h | 10 ++++ 3 files changed, 110 insertions(+) diff --git a/Documentation/gpu/drm-usage-stats.rst b/Documentation/gpu/drm-usage-stats.rst index b46327356e80..b5e7802532ed 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 a51ff8cee049..085b01842a87 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" @@ -868,6 +869,84 @@ 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]); +} + +/** + * drm_print_memory_stats - Helper to print standard fdinfo memory stats + * @file: the DRM file + * @p: the printer to print output to + * @status: callback to get driver tracked object status + * + * Helper to iterate over GEM objects with a handle allocated in the specified + * file. 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. + */ +void drm_print_memory_stats(struct drm_file *file, struct drm_printer *p, + enum drm_gem_object_status (*status)(struct drm_gem_object *)) +{ + struct drm_gem_object *obj; + struct { + size_t shared; + size_t private; + size_t resident; + size_t purgeable; + size_t active; + } size = {0}; + int id; + + spin_lock(&file->table_lock); + idr_for_each_entry (&file->object_idr, obj, id) { + enum drm_gem_object_status s = 0; + + if (status) + s = status(obj); + + if (obj->handle_count > 1) { + size.shared += obj->size; + } else { + size.private += obj->size; + } + + if (s & DRM_GEM_OBJECT_RESIDENT) { + size.resident += obj->size; + s &= ~DRM_GEM_OBJECT_PURGEABLE; + } + + if (s & DRM_GEM_OBJECT_ACTIVE) { + size.active += obj->size; + 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); + + if (status) { + print_size(p, "drm-resident-memory", size.resident); + print_size(p, "drm-purgeable-memory", size.purgeable); + print_size(p, "drm-active-memory", size.active); + } +} +EXPORT_SYMBOL(drm_print_memory_stats); + /** * 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_file.h b/include/drm/drm_file.h index 0d1f853092ab..7bd8a1374f39 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; @@ -438,6 +439,15 @@ void drm_send_event_timestamp_locked(struct drm_device *dev, struct drm_pending_event *e, ktime_t timestamp); +enum drm_gem_object_status { + DRM_GEM_OBJECT_RESIDENT = BIT(0), + DRM_GEM_OBJECT_PURGEABLE = BIT(1), + DRM_GEM_OBJECT_ACTIVE = BIT(2), +}; + +void drm_print_memory_stats(struct drm_file *file, struct drm_printer *p, + enum drm_gem_object_status (*status)(struct drm_gem_object *)); + struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags); #endif /* _DRM_FILE_H_ */ From patchwork Mon Apr 10 21:06:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 13206768 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 4FC72C77B61 for ; Mon, 10 Apr 2023 21:06:33 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2109010E25E; Mon, 10 Apr 2023 21:06:27 +0000 (UTC) Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by gabe.freedesktop.org (Postfix) with ESMTPS id 37EA810E25B; Mon, 10 Apr 2023 21:06:24 +0000 (UTC) Received: by mail-pl1-x631.google.com with SMTP id p8so5822744plk.9; Mon, 10 Apr 2023 14:06:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1681160783; 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=WoMJpN6B4g/LBSey9MdJhme7GrVtOKGM7Nwy9VTZNzg=; b=BlcXAsRgjAj5Zu7jvrN45QfBFtRGjkaydvijkD/qrVhkC1Yt9q6sd8dm98PttiR8sm QflvYmOB7VE+sOqDGY0XMVxVuRgmNkAjdXdOFtTjpSniEvUyBp7pRYpcTHdUeHclmGK8 bNXPEUvkNQxR8M/uwkG1tf+nxg/pmPIZ+/yP04zMGApmPhHTIZOFOzLNuhyF/NL6GTnM ZWtZ8rUTbBiQo1fngvQ6WDKxEeYtxUVsUbH5Dc7hl9Hgpa2GwaSojtfxzXFIyXh5QFb5 cTP14VXyq3G/UfBUWdraL0wT9B9mH+90WD5JdKNL6FTJ529Nx+eGWES2juRXXSN5Sunh wAxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681160783; 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=WoMJpN6B4g/LBSey9MdJhme7GrVtOKGM7Nwy9VTZNzg=; b=hzLoJDlwZ8jikjii9k7JL+3EOq5LXqqqs1v+vFo8zMVVR13il8QD/02PaJLI/gNKii UdFRM+I2nxQcbj6358ow0FuJUWsR2mpZUtn/WKb6c4S18W8banSzrBeEC4r9UYv1jV6+ 6bsG4Ps4Fjf11wCn3nSVe7XFnBjZPW0zEE6stPsNgN6dkO00wVrO5lGy0O6WQhDNNUKB hi941B8BfL4NC6Xa/MTROoy7jdupZy5EgJ+Fe7iIMnu6gZhCjbSpXbXjdTc/dB8LyPTS 7Z3soBU8dhzCfHh10mIj8y0dKNs/Al9/26tyu9Wh2sy7BpdLk6bp/bvu2Nt2VG70KaBX 6vOg== X-Gm-Message-State: AAQBX9edYTYoqCFZBSCKKLEYJOjtFcMYhfIE/cnR7u7ex40cqRUVs+N6 VSk3JpYW36Yqh/urPEpv0v9/4EWEj40= X-Google-Smtp-Source: AKy350auT6h4uHmPGN7puvU25ZQAvvzrs8Ijoo87GihNfVZmS8pmOvwOPMZawn9IN4ZfWVzrBZBXuQ== X-Received: by 2002:a05:6a20:21ca:b0:cc:c5db:ea4a with SMTP id p10-20020a056a2021ca00b000ccc5dbea4amr8359014pzb.33.1681160783384; Mon, 10 Apr 2023 14:06:23 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id x13-20020a63f70d000000b005023496e339sm7242152pgh.63.2023.04.10.14.06.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Apr 2023 14:06:23 -0700 (PDT) From: Rob Clark To: dri-devel@lists.freedesktop.org Subject: [PATCH v2 2/2] drm/msm: Add memory stats to fdinfo Date: Mon, 10 Apr 2023 14:06:07 -0700 Message-Id: <20230410210608.1873968-3-robdclark@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230410210608.1873968-1-robdclark@gmail.com> References: <20230410210608.1873968-1-robdclark@gmail.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 , linux-arm-msm@vger.kernel.org, Emil Velikov , Christopher Healy , Abhinav Kumar , Sean Paul , Boris Brezillon , Dmitry Baryshkov , freedreno@lists.freedesktop.org, open list Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Rob Clark Use the new helper to export stats about memory usage. v2: Drop unintended hunk Signed-off-by: Rob Clark Reviewed-by: Emil Velikov --- drivers/gpu/drm/msm/msm_drv.c | 25 ++++++++++++++++++++++++- drivers/gpu/drm/msm/msm_gpu.c | 2 -- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/msm/msm_drv.c b/drivers/gpu/drm/msm/msm_drv.c index 9b6f17b1261f..9acc3ebfbc08 100644 --- a/drivers/gpu/drm/msm/msm_drv.c +++ b/drivers/gpu/drm/msm/msm_drv.c @@ -1043,17 +1043,40 @@ static const struct drm_ioctl_desc msm_ioctls[] = { DRM_IOCTL_DEF_DRV(MSM_SUBMITQUEUE_QUERY, msm_ioctl_submitqueue_query, DRM_RENDER_ALLOW), }; +enum drm_gem_object_status gem_status(struct drm_gem_object *obj) +{ + struct msm_gem_object *msm_obj = to_msm_bo(obj); + enum drm_gem_object_status status = 0; + + if (!dma_resv_test_signaled(obj->resv, dma_resv_usage_rw(true))) + status |= DRM_GEM_OBJECT_ACTIVE; + + if (msm_obj->pages) + status |= DRM_GEM_OBJECT_RESIDENT; + + if (msm_obj->madv == MSM_MADV_DONTNEED) + status |= DRM_GEM_OBJECT_PURGEABLE; + + return status; +} + static void msm_fop_show_fdinfo(struct seq_file *m, struct file *f) { struct drm_file *file = f->private_data; struct drm_device *dev = file->minor->dev; struct msm_drm_private *priv = dev->dev_private; + struct msm_file_private *ctx = file->driver_priv; struct drm_printer p = drm_seq_file_printer(m); if (!priv->gpu) return; - msm_gpu_show_fdinfo(priv->gpu, file->driver_priv, &p); + drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name); + drm_printf(&p, "drm-client-id:\t%u\n", ctx->seqno); + + msm_gpu_show_fdinfo(priv->gpu, ctx, &p); + + drm_print_memory_stats(file, &p, gem_status); } static const struct file_operations fops = { diff --git a/drivers/gpu/drm/msm/msm_gpu.c b/drivers/gpu/drm/msm/msm_gpu.c index 26ebda40be4f..c403912d13ab 100644 --- a/drivers/gpu/drm/msm/msm_gpu.c +++ b/drivers/gpu/drm/msm/msm_gpu.c @@ -151,8 +151,6 @@ int msm_gpu_pm_suspend(struct msm_gpu *gpu) void msm_gpu_show_fdinfo(struct msm_gpu *gpu, struct msm_file_private *ctx, struct drm_printer *p) { - drm_printf(p, "drm-driver:\t%s\n", gpu->dev->driver->name); - drm_printf(p, "drm-client-id:\t%u\n", ctx->seqno); drm_printf(p, "drm-engine-gpu:\t%llu ns\n", ctx->elapsed_ns); drm_printf(p, "drm-cycles-gpu:\t%llu\n", ctx->cycles); drm_printf(p, "drm-maxfreq-gpu:\t%u Hz\n", gpu->fast_rate);