From patchwork Thu Apr 6 21:59:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 671595 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 A3F57C77B6E for ; Thu, 6 Apr 2023 22:00:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238433AbjDFWAY (ORCPT ); Thu, 6 Apr 2023 18:00:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239874AbjDFV7g (ORCPT ); Thu, 6 Apr 2023 17:59:36 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7C25B742; Thu, 6 Apr 2023 14:59:34 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id o2so38698502plg.4; Thu, 06 Apr 2023 14:59:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680818374; 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=c6L5ENPI75XNzxj2+ZIEkRc54Pl4hiK3xelpisJ84I0=; b=AnAkUdnHDzYKT97ng+g2R/yROgrNHBiakRRrw+nsrbwnxApR/yjxOJdHyK168NDqRI 3n8MQXqz+MHMZFXNLoFoAqwPcdY4Ha1DZLzLKP9WPLW2W6m3QqfYrvcDJfec/x2BNYOF nhTvTBfHwdPiM4zr9MXWPyn3DKhjQglGC22qJM2mx+1y2bj6gYP8tOYzPPSkkdUA3puM cAIGHzgz+SJpzEIJF2sG/wzLcgxhjUwwh315IIxqMWCHoP4tEabxOZd08q+Yujp23ww6 Cb4HlAEpaVmQxmOvnUvKvlqgzs+1zrtM7nB1fLyttJXocpQB522eg0ixh0mYVycxcDhc KuZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680818374; 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=c6L5ENPI75XNzxj2+ZIEkRc54Pl4hiK3xelpisJ84I0=; b=V2vv3HyCb5IOgYOd5OUgXJbD9iY45qNtOajEwEA4QVu1COjas9QrbaB3QjT9BhLLE2 m7WeaZ/Jm/g5pHBxri5h+jHrwVaoIxrLRPHJIetuNX+odxVClhiNaUHJCBu0Ss8KosAb bcttXMe8DbsK1iWjhmgMdAlxQRkEaRVP23D+VjncotNfrA2PHzF8tqK2pN68v/gEAjLO IkXv48Hpx4jU642upA7wGvVcVL/MDMRmkCWoHLO3i8k6hX22F8oFAfZYFBK9ME8fbf3Z VNn4n50hssjq0qpg9Z8SC7A/ndyE8iradMRy2hSvPzyag0H+j87NZU5LnRDo0yDwFxtD 4zDQ== X-Gm-Message-State: AAQBX9dnHDJFs3oZbKEvcoWaq1UiYY23lknVnbQh8xCSwApH8TL9o7DB LXlE8QM+pGKxvsD7lI0PQpQ= X-Google-Smtp-Source: AKy350a3Dxbhpp10Xwx9B8AMZVnumZvl4Zrhg3p3wx5sI9TNreng7qrPlNBDenob6BEbClj6yW/BGA== X-Received: by 2002:a17:902:f986:b0:1a0:549d:3996 with SMTP id ky6-20020a170902f98600b001a0549d3996mr5050194plb.25.1680818373990; Thu, 06 Apr 2023 14:59:33 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id v6-20020a1709029a0600b0019a6cce2060sm1813770plp.57.2023.04.06.14.59.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 14:59:33 -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 , Rob Clark , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Jonathan Corbet , linux-doc@vger.kernel.org (open list:DOCUMENTATION), linux-kernel@vger.kernel.org (open list) Subject: [RFC 1/2] drm: Add fdinfo memory stats Date: Thu, 6 Apr 2023 14:59:16 -0700 Message-Id: <20230406215917.1475704-2-robdclark@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230406215917.1475704-1-robdclark@gmail.com> References: <20230406215917.1475704-1-robdclark@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org 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. 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..56e3c95b6e0a 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 purgable. + +- 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..21911d6ff38d 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[] = {"B", "KiB", "MiB", "GiB"}; + unsigned u; + + for (u = 0; u < ARRAY_SIZE(units) - 1; u++) { + if (sz < SZ_1K) + break; + sz /= SZ_1K; + } + + drm_printf(p, "%s:\t%lu %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 destroy. + */ +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 Thu Apr 6 21:59:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 670818 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 23B8BC76196 for ; Thu, 6 Apr 2023 22:00:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237929AbjDFWAZ (ORCPT ); Thu, 6 Apr 2023 18:00:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239892AbjDFV7h (ORCPT ); Thu, 6 Apr 2023 17:59:37 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6363D7A84; Thu, 6 Apr 2023 14:59:36 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id z19so38701039plo.2; Thu, 06 Apr 2023 14:59:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680818376; 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=HMDj3adSFOes8c4RulM+RRmxsL0r8xM8YpSu25MRY/M=; b=Yblq2nHitw7vSa46fVeSrpQOC+mlt7hYVYKAGPAn2GTbTSQiKY8IKr3X9ouFCF1UFY OUf3cV9cnqyTMflhMlkMNrdtb7CsoeMq5ta2Fj7uXcBrviyfhAaQwdYCFdedqd8djI4I trOoXFizmsKbxPg0bHdszRKgXVTmFnuwe2tWAqUm22P5wKuMzLPb7mHZksKrkdbRCmon H4mARk40fEFRDleMlCPqRS89F2as1UviHP1ZzGOfCWDM1RiiF3/DZm4k2shK3MMwiNSS KIWP7HpbXbELJugRn82UV+Srv6QURGPS842GBePZRfoLS6q9mlcZS54AvuoEJxe1UGDY we1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680818376; 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=HMDj3adSFOes8c4RulM+RRmxsL0r8xM8YpSu25MRY/M=; b=QtlgDuJBL+1fdUfFvZJog3CmsoS+bZ2X5UVwISPq6Dz7CerfoN+QfJ5kkm8PQTVm5l PqrgmyB7oLQRETx1KdMk/CK+de8Bdm8TSkqKRU0sIXHSxto/Lp2zoJS9fVjzOVGC6fOD hRfhwn3zQ+WAvNg8lJtpD+f4gl3aSq66KAu6D3xNu5NKdHNrTRHxyN6NrLIF/LfzqCeP OL6yJlyfveLHulWegsfwTh/vhPEESZT3Ago54SMCFKdKmbhencZI7oa/qSTSZvYWxVIS M1NOekYRPZSVS3Eog6/N+jmrvyIRdxg2d6fd5REXbRtNZzPwzexkvaG4xPFHG428WTmj JWyQ== X-Gm-Message-State: AAQBX9e1xus9n8DFnu7XwZEUK9JnmJKzYuJI9ip5iWlMy5/pFFzoRIrZ FcOGGFfeh/f/CPmNOWiF8BNDOarrC+E= X-Google-Smtp-Source: AKy350bJFPhkA5oP8PFqWPtRwZB24Y6FtqcHMt/vUTEJBVqnYj1Y/hGGiH6gg3cmocGVhKR+qnDcGQ== X-Received: by 2002:a17:90b:1810:b0:237:752f:1af0 with SMTP id lw16-20020a17090b181000b00237752f1af0mr52603pjb.29.1680818375761; Thu, 06 Apr 2023 14:59:35 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id t15-20020a17090a024f00b00244b6899ce5sm19034pje.3.2023.04.06.14.59.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 14:59:35 -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 , Rob Clark , Rob Clark , Abhinav Kumar , Dmitry Baryshkov , Sean Paul , David Airlie , Daniel Vetter , linux-kernel@vger.kernel.org (open list) Subject: [RFC 2/2] drm/msm: Add memory stats to fdinfo Date: Thu, 6 Apr 2023 14:59:17 -0700 Message-Id: <20230406215917.1475704-3-robdclark@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230406215917.1475704-1-robdclark@gmail.com> References: <20230406215917.1475704-1-robdclark@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org From: Rob Clark Use the new helper to export stats about memory usage. Signed-off-by: Rob Clark Reviewed-by: Emil Velikov --- drivers/gpu/drm/msm/msm_drv.c | 26 +++++++++++++++++++++++++- drivers/gpu/drm/msm/msm_gpu.c | 2 -- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/msm/msm_drv.c b/drivers/gpu/drm/msm/msm_drv.c index 9b6f17b1261f..385776f6a531 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 = { @@ -1067,6 +1090,7 @@ static const struct drm_driver msm_driver = { DRIVER_RENDER | DRIVER_ATOMIC | DRIVER_MODESET | + DRIVER_SYNCOBJ_TIMELINE | DRIVER_SYNCOBJ, .open = msm_open, .postclose = msm_postclose, 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);