Message ID | 20230412224311.23511-2-robdclark@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp643624vqo; Wed, 12 Apr 2023 15:46:57 -0700 (PDT) X-Google-Smtp-Source: AKy350bJ2HEfn9mbzZykLChfcItvHFf2G7d8n+9XWfLrnobjhqxD77m/4fv4nMRdbzBctybWmJxt X-Received: by 2002:a17:90b:10f:b0:244:af48:c4f3 with SMTP id p15-20020a17090b010f00b00244af48c4f3mr4151847pjz.7.1681339617345; Wed, 12 Apr 2023 15:46:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681339617; cv=none; d=google.com; s=arc-20160816; b=RM/03GfuX1iTzPooheXxB5ue43gdM7jpyZKCkClxHZ84VyUKQE1sUKDVTgJuNtK8aZ 1GgnOjWwQYbZJA3q9X6JJaH/9NzIrmxlKxkM/ZbPG5x7sgm/1bPYMQH9Umiw2NdXKxCi hohegUjQX3dvCYr2SM/RCxypD20tIvpGf2TXatHHhidA3MkQcf+fs8PDVGFtQ1V0WxZQ EPKfgUNTn3cjwM8a9VJm9xDav/e5KetsXIHoto6MYgLcdBITzNaz4x16aGJNop7YW1Y8 LHMLBpvOVmjnsa34TaOItEF5/Ip3H8w+be/j7Cbjiiu1Gy4LfYHhyzyMI5gTwVircKZM 7jJw== 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=/p/qDE2ikX+16GISXMOf5Sx657qFQJnEhjHYRNC2x2w=; b=SxXcl5RXltxT23nf4kYh8eC03MTuVRvMABeEPLQhk/sCBRUjlBTFaXoMHrQQ3yufP4 1ZVfR5Sci2heoAylZ36PmFIVct6cuuC9z0JGbAuVTN0WO6M5xayuXQHjdZ7ciBbSI6zn SSI87mjLLEK1cQF4IO81YrMUcNEvZ8SemdIFo9gM0AUm2YwnM/ua+TTa89ph2B151yri 360ZDyysk/G2au2++GaSqyxlsTWfRE4UcfWLTds1wka05A46VjmU1G7i5fFI+xUvj8Fk F+bDk8grGM9HMDdi615i2/wo26rWbKe0kdtcLaVGOEgNdFhVYzKrGjFk9d7nrk8nrA/+ O36Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=l4NSCNVB; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h3-20020a17090ac38300b00246f9af56e6si172368pjt.76.2023.04.12.15.46.44; Wed, 12 Apr 2023 15:46:57 -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=l4NSCNVB; 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 S229628AbjDLWnZ (ORCPT <rfc822;peter110.wang@gmail.com> + 99 others); Wed, 12 Apr 2023 18:43:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229553AbjDLWnY (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 12 Apr 2023 18:43:24 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09D6B65B3; Wed, 12 Apr 2023 15:43:23 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id pc4-20020a17090b3b8400b0024676052044so13296732pjb.1; Wed, 12 Apr 2023 15:43:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681339402; 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=/p/qDE2ikX+16GISXMOf5Sx657qFQJnEhjHYRNC2x2w=; b=l4NSCNVBJ9Ay1odXd9M9YvurdYkrA4QQ8XIRYugfEVRjK1SiCvU60Ff89JKg+FqyIn U0QLFE+z+lHrl14haFZmuYAVNGQIoxw/hrNA6dknnmbd9sZ4wWdAlqPtz3HAUEtouKF2 /LwdtqJL2YU11YrrMJI/+qOlp0+wJ/1r24weHBT9AR6aTIa4qO+7Nwq/NGsGTH0VD3vt kW+kqp2ZXUGf/Xb02D+cRtpFiUOwPa8CHEs2A2MJv1nvQutZvSxHGL46wUljAVdPrt02 mBm1p3Eync8pH3zkqFzDsCJ9TxHsgYHFrsPe/gnq2ygMTWTchdlIl4v8/wmsJ/3L5eWV WYDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681339402; 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=/p/qDE2ikX+16GISXMOf5Sx657qFQJnEhjHYRNC2x2w=; b=aQ56aYmmV06T2vsqh6J50rkFg0ZVA5AJm2OfNnolSNdA9HcW5s5ldCbN9EbEeGjnWD pLP58V39WBN7CjQxt11oTk5+or5QozrubqUZOs4kGZMJx7s1PjnEhKD7U8fvqgDuyPR9 mC2nsH1jCaN6SlRinF8Nmn0jx+wOIDGthcOcCj5N/7PEK78s3jDnUNxlV4qxUq6ces8i ivULMaalEy4Zc6VHUI5Zfy/aLcx4XUGYa8uC2DHgyLKpOZSvQfLNJA7+y4eLamOEJHCt m6vfpnY1GhFVu2o34f/guBijzOvS2/VSUqgtBiesRSuHk7M16/JQlXNVQVmb0T+bN5g1 bnoQ== X-Gm-Message-State: AAQBX9d9hYZEQZQ+K0wPZPtnd10JraEqZpTOtk3eqKzkSozus4ZnX9m1 VVv2IAATrLubX1KSyl3+qpE= X-Received: by 2002:a17:903:7c6:b0:1a6:52f9:d4c7 with SMTP id ko6-20020a17090307c600b001a652f9d4c7mr370020plb.60.1681339402470; Wed, 12 Apr 2023 15:43:22 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id m7-20020a1709026bc700b0019a5aa7eab0sm102772plt.54.2023.04.12.15.43.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 15:43:22 -0700 (PDT) From: Rob Clark <robdclark@gmail.com> To: dri-devel@lists.freedesktop.org Cc: linux-arm-msm@vger.kernel.org, freedreno@lists.freedesktop.org, Boris Brezillon <boris.brezillon@collabora.com>, Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>, Christopher Healy <healych@amazon.com>, Emil Velikov <emil.l.velikov@gmail.com>, =?utf-8?q?Christian_K=C3=B6nig?= <christian.koenig@amd.com>, Daniel Vetter <daniel@ffwll.ch>, Rob Clark <robdclark@chromium.org>, Daniel Vetter <daniel.vetter@ffwll.ch>, Maarten Lankhorst <maarten.lankhorst@linux.intel.com>, Maxime Ripard <mripard@kernel.org>, Thomas Zimmermann <tzimmermann@suse.de>, David Airlie <airlied@gmail.com>, Jonathan Corbet <corbet@lwn.net>, linux-doc@vger.kernel.org (open list:DOCUMENTATION), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v4 1/6] drm: Add common fdinfo helper Date: Wed, 12 Apr 2023 15:42:53 -0700 Message-Id: <20230412224311.23511-2-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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763012371045315512?= X-GMAIL-MSGID: =?utf-8?q?1763012371045315512?= |
Series | drm: fdinfo memory stats | |
Commit Message
Rob Clark
April 12, 2023, 10:42 p.m. UTC
From: Rob Clark <robdclark@chromium.org> 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 <daniel.vetter@ffwll.ch> Signed-off-by: Rob Clark <robdclark@chromium.org> --- 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(-)
Comments
Am 13.04.23 um 00:42 schrieb Rob Clark: > From: Rob Clark <robdclark@chromium.org> > > 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 <daniel.vetter@ffwll.ch> > Signed-off-by: Rob Clark <robdclark@chromium.org> > --- > 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-<str> 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. I'm really wondering if it wouldn't be less mid-layering if we let the drivers call the drm function to print the common values instead of the other way around? Apart from that question the patch looks good to me. Christian. > + > Driver specific implementations > -=============================== > +------------------------------- > > :ref:`i915-usage-stats` > diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c > index a51ff8cee049..6d5bdd684ae2 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_pid(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 5b86bb7603e7..5edf2a13733b 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 0d1f853092ab..6de6d0e9c634 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; > > @@ -437,6 +440,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); >
On Thu, Apr 13, 2023 at 10:07:11AM +0200, Christian König wrote: > Am 13.04.23 um 00:42 schrieb Rob Clark: > > From: Rob Clark <robdclark@chromium.org> > > > > 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 <daniel.vetter@ffwll.ch> > > Signed-off-by: Rob Clark <robdclark@chromium.org> > > --- > > 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-<str> 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. > > I'm really wondering if it wouldn't be less mid-layering if we let the > drivers call the drm function to print the common values instead of the > other way around? The idea is that we plug this into DRM_GEM_FOPS and then everyone gets it by default. So it's a bit a tradeoff between midlayering and having inconsistent uapi between drivers. And there's generic tools that parse this, so consistency across drivers is good. My gut feeling was that after a bit of experimenting with lots of different drivers for fdinfo stuff it's time to push for a bit more standardization and less fragmentation. We can of course later on course-correct and shuffle things around again, e.g. by pushing more things into the gem_bo_fops->status hook (ttm and other memory manager libs could implement a decent one by default), or moving more into the drm_driver->show_fdinfo callback again. If you look at kms we also shuffle things back&forth between core (for more consistency) and drivers (for more flexibility where needed). The important part here imo is that we start with some scaffolding to be able to do this. Like another thing that I think we want is some drm_fdinfo_print functions that make sure the formatting is guaranteed consistents and we don't trip up parsers (like some drivers use " \t" as separator instead of just "\t", I guess by accident). > Apart from thatquestion the patch looks good to me. Ack? Or want the above recorded in the commit message, I think it'd make sense to put it there. -Daniel > > Christian. > > > + > > Driver specific implementations > > -=============================== > > +------------------------------- > > :ref:`i915-usage-stats` > > diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c > > index a51ff8cee049..6d5bdd684ae2 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_pid(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 5b86bb7603e7..5edf2a13733b 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 0d1f853092ab..6de6d0e9c634 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; > > @@ -437,6 +440,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); >
Am 13.04.23 um 10:46 schrieb Daniel Vetter: > On Thu, Apr 13, 2023 at 10:07:11AM +0200, Christian König wrote: >> Am 13.04.23 um 00:42 schrieb Rob Clark: >>> From: Rob Clark <robdclark@chromium.org> >>> >>> 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 <daniel.vetter@ffwll.ch> >>> Signed-off-by: Rob Clark <robdclark@chromium.org> >>> --- >>> 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-<str> 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. >> I'm really wondering if it wouldn't be less mid-layering if we let the >> drivers call the drm function to print the common values instead of the >> other way around? > The idea is that we plug this into DRM_GEM_FOPS and then everyone gets it > by default. So it's a bit a tradeoff between midlayering and having > inconsistent uapi between drivers. And there's generic tools that parse > this, so consistency across drivers is good. > > My gut feeling was that after a bit of experimenting with lots of > different drivers for fdinfo stuff it's time to push for a bit more > standardization and less fragmentation. Yeah, that's indeed a trade of. > > We can of course later on course-correct and shuffle things around again, > e.g. by pushing more things into the gem_bo_fops->status hook (ttm and > other memory manager libs could implement a decent one by default), or > moving more into the drm_driver->show_fdinfo callback again. > > If you look at kms we also shuffle things back&forth between core (for > more consistency) and drivers (for more flexibility where needed). > > The important part here imo is that we start with some scaffolding to be > able to do this. Like another thing that I think we want is some > drm_fdinfo_print functions that make sure the formatting is guaranteed > consistents and we don't trip up parsers (like some drivers use " \t" as > separator instead of just "\t", I guess by accident). That's indeed a bit ugly and should probably be fixed on a higher level in the fs code. Something like fdinfo_print(seq, name, format, value); > >> Apart from thatquestion the patch looks good to me. > Ack? Or want the above recorded in the commit message, I think it'd make > sense to put it there. Well if Rob mentions this trade of in the commit message or even better code document feel free to add my rb to the patch. Christian. > -Daniel > >> Christian. >> >>> + >>> Driver specific implementations >>> -=============================== >>> +------------------------------- >>> :ref:`i915-usage-stats` >>> diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c >>> index a51ff8cee049..6d5bdd684ae2 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_pid(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 5b86bb7603e7..5edf2a13733b 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 0d1f853092ab..6de6d0e9c634 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; >>> @@ -437,6 +440,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);
On 13/04/2023 09:46, Daniel Vetter wrote: > On Thu, Apr 13, 2023 at 10:07:11AM +0200, Christian König wrote: >> Am 13.04.23 um 00:42 schrieb Rob Clark: >>> From: Rob Clark <robdclark@chromium.org> >>> >>> 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 <daniel.vetter@ffwll.ch> >>> Signed-off-by: Rob Clark <robdclark@chromium.org> >>> --- >>> 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-<str> 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. >> >> I'm really wondering if it wouldn't be less mid-layering if we let the >> drivers call the drm function to print the common values instead of the >> other way around? > > The idea is that we plug this into DRM_GEM_FOPS and then everyone gets it > by default. So it's a bit a tradeoff between midlayering and having > inconsistent uapi between drivers. And there's generic tools that parse > this, so consistency across drivers is good. > > My gut feeling was that after a bit of experimenting with lots of > different drivers for fdinfo stuff it's time to push for a bit more > standardization and less fragmentation. > > We can of course later on course-correct and shuffle things around again, > e.g. by pushing more things into the gem_bo_fops->status hook (ttm and > other memory manager libs could implement a decent one by default), or > moving more into the drm_driver->show_fdinfo callback again. > > If you look at kms we also shuffle things back&forth between core (for > more consistency) and drivers (for more flexibility where needed). > > The important part here imo is that we start with some scaffolding to be > able to do this. Like another thing that I think we want is some > drm_fdinfo_print functions that make sure the formatting is guaranteed > consistents and we don't trip up parsers (like some drivers use " \t" as > separator instead of just "\t", I guess by accident). On this particular part I'd say it's even better to keep userspace on their toes and not let them hardcode assumption about how much and what whitespace characters to expect. Otherwise I agree that mid-layer or not can be changed later, as long as my opens from elsewhere in the thread are closed before merging. Regards, Tvrtko >> Apart from thatquestion the patch looks good to me. > > Ack? Or want the above recorded in the commit message, I think it'd make > sense to put it there. > -Daniel > >> >> Christian. >> >>> + >>> Driver specific implementations >>> -=============================== >>> +------------------------------- >>> :ref:`i915-usage-stats` >>> diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c >>> index a51ff8cee049..6d5bdd684ae2 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_pid(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 5b86bb7603e7..5edf2a13733b 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 0d1f853092ab..6de6d0e9c634 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; >>> @@ -437,6 +440,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); >> >
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-<str> 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 a51ff8cee049..6d5bdd684ae2 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_pid(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 5b86bb7603e7..5edf2a13733b 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 0d1f853092ab..6de6d0e9c634 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; @@ -437,6 +440,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);