Skip to contentSkip to conversation

fdinfo shared stats

PATCH Superseded Alex Deucher 7 participants 15 messages Dec 7, 2023 – Jan 9, 2024

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

cover fdinfo shared stats

We had a request to add shared buffer stats to fdinfo for amdgpu and while implementing that, Christian mentioned that just looking at the GEM handle count doesn't take into account buffers shared with other subsystems like V4L or RDMA. Those subsystems don't use GEM, so it doesn't really matter from a GPU top perspective, but it's more correct if you actually want to see shared buffers.

Alex Deucher (2):
drm: update drm_show_memory_stats() for dma-bufs drm/amdgpu: add shared fdinfo stats

 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
 drivers/gpu/drm/drm_file.c                 |  2 +-
 4 files changed, 22 insertions(+), 1 deletion(-)
Signature · 1 line
2.42.0
author maintainer
#1 lore raw

PATCH 1/2 drm: update drm_show_memory_stats() for dma-bufs

Show buffers as shared if they are shared via dma-buf as well (e.g., shared with v4l or some other subsystem).

---
 drivers/gpu/drm/drm_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_file.c +1 -1
index 5ddaffd32586..5d5f93b9c263 100644
@@ -973,7 +973,7 @@ void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
 					DRM_GEM_OBJECT_PURGEABLE;
 		}
 
-		if (obj->handle_count > 1) {
+		if ((obj->handle_count > 1) || obj->dma_buf) {
 			status.shared += obj->size;
 		} else {
 			status.private += obj->size;
Signature · 1 line
2.42.0
#2 lore raw

Re: [PATCH 1/2] drm: update drm_show_memory_stats() for dma-bufs

Quoted 2 lines · Alex Deucher

Show buffers as shared if they are shared via dma-buf as well (e.g., shared with v4l or some other subsystem).

You can add KFD to that list. With the in-progress CUDA11 VM changes and improved interop between KFD and render nodes, sharing DMABufs between KFD and render nodes will become much more common.

Regards,
  Felix

Quoted 20 lines
---
  drivers/gpu/drm/drm_file.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_file.c +1 -1
index 5ddaffd32586..5d5f93b9c263 100644
@@ -973,7 +973,7 @@ void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
  					DRM_GEM_OBJECT_PURGEABLE;
  		}
 
-		if (obj->handle_count > 1) {
+		if ((obj->handle_count > 1) || obj->dma_buf) {
  			status.shared += obj->size;
  		} else {
  			status.private += obj->size;
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] drm: update drm_show_memory_stats() for dma-bufs

In reply to Alex Deucher
Quoted 6 lines · Alex Deucher

Show buffers as shared if they are shared via dma-buf as well (e.g., shared with v4l or some other subsystem).

Quoted 20 lines
---
 drivers/gpu/drm/drm_file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/drm_file.c +1 -2
index 5ddaffd32586..5d5f93b9c263 100644
@@ -973,7 +973,7 @@ void drm_show_memory_stats(struct drm_printer *p, struct drm_file *file)
                                        DRM_GEM_OBJECT_PURGEABLE;
                }
 
-               if (obj->handle_count > 1) {
+               if ((obj->handle_count > 1) || obj->dma_buf) {
                        status.shared += obj->size;
                } else {
                        status.private += obj->size;
--

2.42.0

author maintainer
#4 lore raw

PATCH 2/2 drm/amdgpu: add shared fdinfo stats

In reply to Alex Deucher

Add shared stats. Useful for seeing shared memory.

v2: take dma-buf into account as well

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
 3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
 		   stats.requested_visible_vram/1024UL);
 	drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
 		   stats.requested_gtt/1024UL);
+	drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+	drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+	drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 		if (!usage[hw_ip])
 			continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +11 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
 			  struct amdgpu_mem_stats *stats)
 {
 	uint64_t size = amdgpu_bo_size(bo);
+	struct drm_gem_object *obj;
 	unsigned int domain;
+	bool shared;
 
 	/* Abort if the BO doesn't currently have a backing store */
 	if (!bo->tbo.resource)
 		return;
 
+	obj = &bo->tbo.base;
+	shared = (obj->handle_count > 1) || obj->dma_buf;
+
 	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
 	switch (domain) {
 	case AMDGPU_GEM_DOMAIN_VRAM:
 		stats->vram += size;
 		if (amdgpu_bo_in_cpu_visible_vram(bo))
 			stats->visible_vram += size;
+		if (shared)
+			stats->vram_shared += size;
 		break;
 	case AMDGPU_GEM_DOMAIN_GTT:
 		stats->gtt += size;
+		if (shared)
+			stats->gtt_shared += size;
 		break;
 	case AMDGPU_GEM_DOMAIN_CPU:
 	default:
 		stats->cpu += size;
+		if (shared)
+			stats->cpu_shared += size;
 		break;
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
 struct amdgpu_mem_stats {
 	/* current VRAM usage, includes visible VRAM */
 	uint64_t vram;
+	/* current shared VRAM usage, includes visible VRAM */
+	uint64_t vram_shared;
 	/* current visible VRAM usage */
 	uint64_t visible_vram;
 	/* current GTT usage */
 	uint64_t gtt;
+	/* current shared GTT usage */
+	uint64_t gtt_shared;
 	/* current system memory usage */
 	uint64_t cpu;
+	/* current shared system memory usage */
+	uint64_t cpu_shared;
 	/* sum of evicted buffers, includes visible VRAM */
 	uint64_t evicted_vram;
 	/* sum of evicted buffers due to CPU access */
Signature · 1 line
2.42.0
#5 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

Ping?

Alex

Quoted 95 lines · Alex Deucher

Add shared stats. Useful for seeing shared memory.

v2: take dma-buf into account as well

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
 3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
                   stats.requested_visible_vram/1024UL);
        drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
                   stats.requested_gtt/1024UL);
+       drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+       drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+       drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
        for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
                if (!usage[hw_ip])
                        continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +11 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                          struct amdgpu_mem_stats *stats)
 {
        uint64_t size = amdgpu_bo_size(bo);
+       struct drm_gem_object *obj;
        unsigned int domain;
+       bool shared;
 
        /* Abort if the BO doesn't currently have a backing store */
        if (!bo->tbo.resource)
                return;
 
+       obj = &bo->tbo.base;
+       shared = (obj->handle_count > 1) || obj->dma_buf;
+
        domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
        switch (domain) {
        case AMDGPU_GEM_DOMAIN_VRAM:
                stats->vram += size;
                if (amdgpu_bo_in_cpu_visible_vram(bo))
                        stats->visible_vram += size;
+               if (shared)
+                       stats->vram_shared += size;
                break;
        case AMDGPU_GEM_DOMAIN_GTT:
                stats->gtt += size;
+               if (shared)
+                       stats->gtt_shared += size;
                break;
        case AMDGPU_GEM_DOMAIN_CPU:
        default:
                stats->cpu += size;
+               if (shared)
+                       stats->cpu_shared += size;
                break;
        }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -1
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
 struct amdgpu_mem_stats {
        /* current VRAM usage, includes visible VRAM */
        uint64_t vram;
+       /* current shared VRAM usage, includes visible VRAM */
+       uint64_t vram_shared;
        /* current visible VRAM usage */
        uint64_t visible_vram;
        /* current GTT usage */
        uint64_t gtt;
+       /* current shared GTT usage */
+       uint64_t gtt_shared;
        /* current system memory usage */
        uint64_t cpu;
+       /* current shared system memory usage */
+       uint64_t cpu_shared;
        /* sum of evicted buffers, includes visible VRAM */
        uint64_t evicted_vram;
        /* sum of evicted buffers due to CPU access */
--

2.42.0

#6 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

In reply to Alex Deucher

Am 07.12.23 um 19:02 schrieb Alex Deucher:

Quoted 35 lines

Add shared stats. Useful for seeing shared memory.

v2: take dma-buf into account as well

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
  3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +4 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  		   stats.requested_visible_vram/1024UL);
  	drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
  		   stats.requested_gtt/1024UL);
+	drm_printf(p, "drm-shared-vram:\t%llu KiB\n", stats.vram_shared/1024UL);
+	drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", stats.gtt_shared/1024UL);
+	drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", stats.cpu_shared/1024UL);
+
  	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
  		if (!usage[hw_ip])
  			continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
  			  struct amdgpu_mem_stats *stats)
  {
  	uint64_t size = amdgpu_bo_size(bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +4 -0
+	struct drm_gem_object *obj;
  	unsigned int domain;
+	bool shared;
 
  	/* Abort if the BO doesn't currently have a backing store */
  	if (!bo->tbo.resource)
  		return;
 
+	obj = &bo->tbo.base;
+	shared = (obj->handle_count > 1) || obj->dma_buf;

I still think that looking at handle_count is the completely wrong approach, we should really only look at obj->dma_buf.

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
  	domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
  	switch (domain) {
  	case AMDGPU_GEM_DOMAIN_VRAM:
  		stats->vram += size;
  		if (amdgpu_bo_in_cpu_visible_vram(bo))
  			stats->visible_vram += size;
+		if (shared)
+			stats->vram_shared += size;
  		break;
  	case AMDGPU_GEM_DOMAIN_GTT:
  		stats->gtt += size;
+		if (shared)
+			stats->gtt_shared += size;
  		break;
  	case AMDGPU_GEM_DOMAIN_CPU:
  	default:
  		stats->cpu += size;
+		if (shared)
+			stats->cpu_shared += size;
  		break;
  	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
  struct amdgpu_mem_stats {
  	/* current VRAM usage, includes visible VRAM */
  	uint64_t vram;
+	/* current shared VRAM usage, includes visible VRAM */
+	uint64_t vram_shared;
  	/* current visible VRAM usage */
  	uint64_t visible_vram;
  	/* current GTT usage */
  	uint64_t gtt;
+	/* current shared GTT usage */
+	uint64_t gtt_shared;
  	/* current system memory usage */
  	uint64_t cpu;
+	/* current shared system memory usage */
+	uint64_t cpu_shared;
  	/* sum of evicted buffers, includes visible VRAM */
  	uint64_t evicted_vram;
  	/* sum of evicted buffers due to CPU access */
#7 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

Quoted 42 lines · Christian König

Am 07.12.23 um 19:02 schrieb Alex Deucher:

Quoted 41 lines

Add shared stats.  Useful for seeing shared memory.

v2: take dma-buf into account as well

---

  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
  3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +3 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p, 
 struct drm_file *file)
              stats.requested_visible_vram/1024UL);
       drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
              stats.requested_gtt/1024UL);
+    drm_printf(p, "drm-shared-vram:\t%llu KiB\n", 
 stats.vram_shared/1024UL);
+    drm_printf(p, "drm-shared-gtt:\t%llu KiB\n", 
 stats.gtt_shared/1024UL);
+    drm_printf(p, "drm-shared-cpu:\t%llu KiB\n", 

stats.cpu_shared/1024UL);
+
      for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) { if (!usage[hw_ip])
              continue;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                 struct amdgpu_mem_stats *stats)
   {
       uint64_t size = amdgpu_bo_size(bo);
+    struct drm_gem_object *obj;
       unsigned int domain;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -0
+    bool shared;
       /* Abort if the BO doesn't currently have a backing store */
       if (!bo->tbo.resource)
           return;
+    obj = &bo->tbo.base;
+    shared = (obj->handle_count > 1) || obj->dma_buf;

I still think that looking at handle_count is the completely wrong approach, we should really only look at obj->dma_buf.

Yeah it is all a bit tricky with the handle table walk. I don't think it is even possible to claim it is shared with obj->dma_buf could be the same process creating say via udmabuf and importing into drm. It is a wild scenario yes, but it could be private memory in that case. Not sure where it would leave us if we said this is just a limitation of a BO based tracking.

Would adding a new category "imported" help?

Hmm or we simply change drm-usage-stats.rst:

"""
- drm-shared-<region>: <uint> [KiB|MiB]

The total size of buffers that are shared with another file (ie. have more than than a single handle). """

Changing ie into eg coule be get our of jail free card to allow the "(obj->handle_count > 1) || obj->dma_buf;" condition?

Because of the shared with another _file_ wording would cover my wild udmabuf self-import case. Unless there are more such creative private import options.

Regards,

Tvrtko

Quoted 50 lines

Regards,
Christian.

Quoted 45 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
+
       domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
       switch (domain) {

      case AMDGPU_GEM_DOMAIN_VRAM:
          stats->vram += size;
          if (amdgpu_bo_in_cpu_visible_vram(bo))
              stats->visible_vram += size;
+        if (shared)
+            stats->vram_shared += size;
          break;
      case AMDGPU_GEM_DOMAIN_GTT:
          stats->gtt += size;
+        if (shared)
+            stats->gtt_shared += size;
          break;
      case AMDGPU_GEM_DOMAIN_CPU:
      default:
          stats->cpu += size;
+        if (shared)
+            stats->cpu_shared += size;
          break;
      }

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
   struct amdgpu_mem_stats {
       /* current VRAM usage, includes visible VRAM */
       uint64_t vram;
+    /* current shared VRAM usage, includes visible VRAM */
+    uint64_t vram_shared;
       /* current visible VRAM usage */
       uint64_t visible_vram;
       /* current GTT usage */
       uint64_t gtt;
+    /* current shared GTT usage */
+    uint64_t gtt_shared;
       /* current system memory usage */
       uint64_t cpu;
+    /* current shared system memory usage */
+    uint64_t cpu_shared;
       /* sum of evicted buffers, includes visible VRAM */
       uint64_t evicted_vram;
       /* sum of evicted buffers due to CPU access */
#8 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

Quoted 73 lines · Tvrtko Ursulin
Quoted 45 lines · Christian König

Am 07.12.23 um 19:02 schrieb Alex Deucher:

Quoted 44 lines

Add shared stats.  Useful for seeing shared memory.

v2: take dma-buf into account as well

---

  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
  3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +3 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
              stats.requested_visible_vram/1024UL);
       drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
              stats.requested_gtt/1024UL);
+    drm_printf(p, "drm-shared-vram:\t%llu KiB\n",
 stats.vram_shared/1024UL);
+    drm_printf(p, "drm-shared-gtt:\t%llu KiB\n",
 stats.gtt_shared/1024UL);
+    drm_printf(p, "drm-shared-cpu:\t%llu KiB\n",

stats.cpu_shared/1024UL);
+
      for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) { if (!usage[hw_ip])
              continue;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                 struct amdgpu_mem_stats *stats)
   {
       uint64_t size = amdgpu_bo_size(bo);
+    struct drm_gem_object *obj;
       unsigned int domain;
+    bool shared;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -0
       /* Abort if the BO doesn't currently have a backing store */
       if (!bo->tbo.resource)
           return;
+    obj = &bo->tbo.base;
+    shared = (obj->handle_count > 1) || obj->dma_buf;

I still think that looking at handle_count is the completely wrong approach, we should really only look at obj->dma_buf.

Yeah it is all a bit tricky with the handle table walk. I don't think it is even possible to claim it is shared with obj->dma_buf could be the same process creating say via udmabuf and importing into drm. It is a wild scenario yes, but it could be private memory in that case. Not sure where it would leave us if we said this is just a limitation of a BO based tracking.

Would adding a new category "imported" help?

Hmm or we simply change drm-usage-stats.rst:

"""
- drm-shared-<region>: <uint> [KiB|MiB]

The total size of buffers that are shared with another file (ie. have more than than a single handle).
"""

Quoted 6 lines

Changing ie into eg coule be get our of jail free card to allow the "(obj->handle_count > 1) || obj->dma_buf;" condition?

Because of the shared with another _file_ wording would cover my wild udmabuf self-import case. Unless there are more such creative private import options.

Yeah I think clarifying that we can only track sharing with other fd and have no idea whether this means sharing with another process or not is probably simplest. Maybe not exactly what users want, but still the roughly best-case approximation we can deliver somewhat cheaply.

Also maybe time for a drm_gem_buffer_object_is_shared() helper so we don't copypaste this all over and then end up in divergent conditions? I'm guessing that there's going to be a bunch of drivers which needs this little helper to add drm-shared-* stats to their fdinfo ...

Cheers, Sima

Quoted 56 lines

Regards,

Tvrtko

Quoted 51 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
+
       domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
       switch (domain) {

      case AMDGPU_GEM_DOMAIN_VRAM:
          stats->vram += size;
          if (amdgpu_bo_in_cpu_visible_vram(bo))
              stats->visible_vram += size;
+        if (shared)
+            stats->vram_shared += size;
          break;
      case AMDGPU_GEM_DOMAIN_GTT:
          stats->gtt += size;
+        if (shared)
+            stats->gtt_shared += size;
          break;
      case AMDGPU_GEM_DOMAIN_CPU:
      default:
          stats->cpu += size;
+        if (shared)
+            stats->cpu_shared += size;
          break;
      }

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
   struct amdgpu_mem_stats {
       /* current VRAM usage, includes visible VRAM */
       uint64_t vram;
+    /* current shared VRAM usage, includes visible VRAM */
+    uint64_t vram_shared;
       /* current visible VRAM usage */
       uint64_t visible_vram;
       /* current GTT usage */
       uint64_t gtt;
+    /* current shared GTT usage */
+    uint64_t gtt_shared;
       /* current system memory usage */
       uint64_t cpu;
+    /* current shared system memory usage */
+    uint64_t cpu_shared;
       /* sum of evicted buffers, includes visible VRAM */
       uint64_t evicted_vram;
       /* sum of evicted buffers due to CPU access */
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#9 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

Quoted 86 lines · Daniel Vetter
Quoted 73 lines · Tvrtko Ursulin
Quoted 45 lines · Christian König

Am 07.12.23 um 19:02 schrieb Alex Deucher:

Quoted 44 lines

Add shared stats.  Useful for seeing shared memory.

v2: take dma-buf into account as well

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
   3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +3 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
              stats.requested_visible_vram/1024UL);
       drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
              stats.requested_gtt/1024UL);
+    drm_printf(p, "drm-shared-vram:\t%llu KiB\n",
 stats.vram_shared/1024UL);
+    drm_printf(p, "drm-shared-gtt:\t%llu KiB\n",
 stats.gtt_shared/1024UL);
+    drm_printf(p, "drm-shared-cpu:\t%llu KiB\n",

stats.cpu_shared/1024UL);

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -0
+
       for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
           if (!usage[hw_ip])
               continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                 struct amdgpu_mem_stats *stats)
   {
       uint64_t size = amdgpu_bo_size(bo);
+    struct drm_gem_object *obj;
       unsigned int domain;
+    bool shared;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -0
       /* Abort if the BO doesn't currently have a backing store */
       if (!bo->tbo.resource)
           return;
+    obj = &bo->tbo.base;
+    shared = (obj->handle_count > 1) || obj->dma_buf;

I still think that looking at handle_count is the completely wrong approach, we should really only look at obj->dma_buf.

Yeah it is all a bit tricky with the handle table walk. I don't think it is even possible to claim it is shared with obj->dma_buf could be the same process creating say via udmabuf and importing into drm. It is a wild scenario yes, but it could be private memory in that case. Not sure where it would leave us if we said this is just a limitation of a BO based tracking.

Would adding a new category "imported" help?

Hmm or we simply change drm-usage-stats.rst:

"""
- drm-shared-<region>: <uint> [KiB|MiB]

The total size of buffers that are shared with another file (ie. have more than than a single handle).
"""

Quoted 6 lines

Changing ie into eg coule be get our of jail free card to allow the "(obj->handle_count > 1) || obj->dma_buf;" condition?

Because of the shared with another _file_ wording would cover my wild udmabuf self-import case. Unless there are more such creative private import options.

Yeah I think clarifying that we can only track sharing with other fd and have no idea whether this means sharing with another process or not is probably simplest. Maybe not exactly what users want, but still the roughly best-case approximation we can deliver somewhat cheaply.

Quoted 4 lines

Also maybe time for a drm_gem_buffer_object_is_shared() helper so we don't copypaste this all over and then end up in divergent conditions? I'm guessing that there's going to be a bunch of drivers which needs this little helper to add drm-shared-* stats to their fdinfo ...

Yeah I agree that works and i915 would need to use the helper too.

I would only suggest to name it so the meaning of shared is obviously only about the fdinfo memory stats and no one gets a more meaningful idea about its semantics.

We have drm_show_memory_stats and drm_print_memory_stats exported so perhaps something like drm_object_is_shared_for_memory_stats, drm_object_is_memory_stats_shared, drm_memory_stats_object_is_shared?

And s/ie/eg/ in the above quoted drm-usage-stats.rst.

Regards,

Tvrtko

Quoted 59 lines

Cheers, Sima

Quoted 56 lines

Regards,

Tvrtko

Quoted 51 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
       domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
       switch (domain) {
       case AMDGPU_GEM_DOMAIN_VRAM:
           stats->vram += size;
           if (amdgpu_bo_in_cpu_visible_vram(bo))
               stats->visible_vram += size;
+        if (shared)
+            stats->vram_shared += size;
           break;
       case AMDGPU_GEM_DOMAIN_GTT:
           stats->gtt += size;
+        if (shared)
+            stats->gtt_shared += size;
           break;
       case AMDGPU_GEM_DOMAIN_CPU:
       default:
           stats->cpu += size;
+        if (shared)
+            stats->cpu_shared += size;
           break;
       }
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
   struct amdgpu_mem_stats {
       /* current VRAM usage, includes visible VRAM */
       uint64_t vram;
+    /* current shared VRAM usage, includes visible VRAM */
+    uint64_t vram_shared;
       /* current visible VRAM usage */
       uint64_t visible_vram;
       /* current GTT usage */
       uint64_t gtt;
+    /* current shared GTT usage */
+    uint64_t gtt_shared;
       /* current system memory usage */
       uint64_t cpu;
+    /* current shared system memory usage */
+    uint64_t cpu_shared;
       /* sum of evicted buffers, includes visible VRAM */
       uint64_t evicted_vram;
       /* sum of evicted buffers due to CPU access */
#10 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

Quoted 100 lines · Tvrtko Ursulin
Quoted 86 lines · Daniel Vetter
Quoted 73 lines · Tvrtko Ursulin
Quoted 45 lines · Christian König

Am 07.12.23 um 19:02 schrieb Alex Deucher:

Quoted 44 lines

Add shared stats. Useful for seeing shared memory.

v2: take dma-buf into account as well

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
   3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +3 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
              stats.requested_visible_vram/1024UL);
       drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
              stats.requested_gtt/1024UL);
+    drm_printf(p, "drm-shared-vram:\t%llu KiB\n",
 stats.vram_shared/1024UL);
+    drm_printf(p, "drm-shared-gtt:\t%llu KiB\n",
 stats.gtt_shared/1024UL);
+    drm_printf(p, "drm-shared-cpu:\t%llu KiB\n",

stats.cpu_shared/1024UL);

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -0
+
       for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
           if (!usage[hw_ip])
               continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                 struct amdgpu_mem_stats *stats)
   {
       uint64_t size = amdgpu_bo_size(bo);
+    struct drm_gem_object *obj;
       unsigned int domain;
+    bool shared;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -0
       /* Abort if the BO doesn't currently have a backing store */
       if (!bo->tbo.resource)
           return;
+    obj = &bo->tbo.base;
+    shared = (obj->handle_count > 1) || obj->dma_buf;

I still think that looking at handle_count is the completely wrong approach, we should really only look at obj->dma_buf.

Yeah it is all a bit tricky with the handle table walk. I don't think it is even possible to claim it is shared with obj->dma_buf could be the same process creating say via udmabuf and importing into drm. It is a wild scenario yes, but it could be private memory in that case. Not sure where it would leave us if we said this is just a limitation of a BO based tracking.

Would adding a new category "imported" help?

Hmm or we simply change drm-usage-stats.rst:

"""
- drm-shared-<region>: <uint> [KiB|MiB]

The total size of buffers that are shared with another file (ie. have more than than a single handle).
"""

Quoted 6 lines

Changing ie into eg coule be get our of jail free card to allow the "(obj->handle_count > 1) || obj->dma_buf;" condition?

Because of the shared with another _file_ wording would cover my wild udmabuf self-import case. Unless there are more such creative private import options.

Yeah I think clarifying that we can only track sharing with other fd and have no idea whether this means sharing with another process or not is probably simplest. Maybe not exactly what users want, but still the roughly best-case approximation we can deliver somewhat cheaply.

Quoted 4 lines

Also maybe time for a drm_gem_buffer_object_is_shared() helper so we don't copypaste this all over and then end up in divergent conditions? I'm guessing that there's going to be a bunch of drivers which needs this little helper to add drm-shared-* stats to their fdinfo ...

Yeah I agree that works and i915 would need to use the helper too.

I would only suggest to name it so the meaning of shared is obviously only about the fdinfo memory stats and no one gets a more meaningful idea about its semantics.

Quoted 5 lines

We have drm_show_memory_stats and drm_print_memory_stats exported so perhaps something like drm_object_is_shared_for_memory_stats, drm_object_is_memory_stats_shared, drm_memory_stats_object_is_shared?

And s/ie/eg/ in the above quoted drm-usage-stats.rst.

Ack on making it clear this helper would be for fdinfo memory stats only. Sounds like a good idea to stop people from finding really creative uses ...
-Sima

Quoted 64 lines

Regards,

Tvrtko

Quoted 59 lines

Cheers, Sima

Quoted 56 lines

Regards,

Tvrtko

Quoted 51 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
       domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
       switch (domain) {
       case AMDGPU_GEM_DOMAIN_VRAM:
           stats->vram += size;
           if (amdgpu_bo_in_cpu_visible_vram(bo))
               stats->visible_vram += size;
+        if (shared)
+            stats->vram_shared += size;
           break;
       case AMDGPU_GEM_DOMAIN_GTT:
           stats->gtt += size;
+        if (shared)
+            stats->gtt_shared += size;
           break;
       case AMDGPU_GEM_DOMAIN_CPU:
       default:
           stats->cpu += size;
+        if (shared)
+            stats->cpu_shared += size;
           break;
       }
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
   struct amdgpu_mem_stats {
       /* current VRAM usage, includes visible VRAM */
       uint64_t vram;
+    /* current shared VRAM usage, includes visible VRAM */
+    uint64_t vram_shared;
       /* current visible VRAM usage */
       uint64_t visible_vram;
       /* current GTT usage */
       uint64_t gtt;
+    /* current shared GTT usage */
+    uint64_t gtt_shared;
       /* current system memory usage */
       uint64_t cpu;
+    /* current shared system memory usage */
+    uint64_t cpu_shared;
       /* sum of evicted buffers, includes visible VRAM */
       uint64_t evicted_vram;
       /* sum of evicted buffers due to CPU access */
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#11 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

Am 09.01.24 um 15:26 schrieb Daniel Vetter:

Quoted 92 lines
Quoted 90 lines · Tvrtko Ursulin
Quoted 82 lines · Daniel Vetter
Quoted 70 lines · Tvrtko Ursulin
Quoted 44 lines · Christian König

Am 07.12.23 um 19:02 schrieb Alex Deucher:

Quoted 43 lines

Add shared stats. Useful for seeing shared memory.

v2: take dma-buf into account as well

---
    drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
    3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +3 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
               stats.requested_visible_vram/1024UL);
        drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
               stats.requested_gtt/1024UL);
+    drm_printf(p, "drm-shared-vram:\t%llu KiB\n",
 stats.vram_shared/1024UL);
+    drm_printf(p, "drm-shared-gtt:\t%llu KiB\n",
 stats.gtt_shared/1024UL);
+    drm_printf(p, "drm-shared-cpu:\t%llu KiB\n",

stats.cpu_shared/1024UL);

drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +1 -0
+
        for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
            if (!usage[hw_ip])
                continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct amdgpu_bo *bo,
                  struct amdgpu_mem_stats *stats)
    {
        uint64_t size = amdgpu_bo_size(bo);
+    struct drm_gem_object *obj;
        unsigned int domain;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct amdgpu_bo *bo, · line 1299

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -0
+    bool shared;
        /* Abort if the BO doesn't currently have a backing store */
        if (!bo->tbo.resource)
            return;
+    obj = &bo->tbo.base;
+    shared = (obj->handle_count > 1) || obj->dma_buf;

I still think that looking at handle_count is the completely wrong approach, we should really only look at obj->dma_buf.

Yeah it is all a bit tricky with the handle table walk. I don't think it is even possible to claim it is shared with obj->dma_buf could be the same process creating say via udmabuf and importing into drm. It is a wild scenario yes, but it could be private memory in that case. Not sure where it would leave us if we said this is just a limitation of a BO based tracking.

Would adding a new category "imported" help?

Hmm or we simply change drm-usage-stats.rst:

"""
- drm-shared-<region>: <uint> [KiB|MiB]

The total size of buffers that are shared with another file (ie. have more than than a single handle).
"""

Quoted 6 lines

Changing ie into eg coule be get our of jail free card to allow the "(obj->handle_count > 1) || obj->dma_buf;" condition?

Because of the shared with another _file_ wording would cover my wild udmabuf self-import case. Unless there are more such creative private import options.

Yeah I think clarifying that we can only track sharing with other fd and have no idea whether this means sharing with another process or not is probably simplest. Maybe not exactly what users want, but still the roughly best-case approximation we can deliver somewhat cheaply.

Quoted 4 lines

Also maybe time for a drm_gem_buffer_object_is_shared() helper so we don't copypaste this all over and then end up in divergent conditions? I'm guessing that there's going to be a bunch of drivers which needs this little helper to add drm-shared-* stats to their fdinfo ...

Yeah I agree that works and i915 would need to use the helper too.

Quoted 7 lines
Quoted 7 lines

I would only suggest to name it so the meaning of shared is obviously only about the fdinfo memory stats and no one gets a more meaningful idea about its semantics.

We have drm_show_memory_stats and drm_print_memory_stats exported so perhaps something like drm_object_is_shared_for_memory_stats, drm_object_is_memory_stats_shared, drm_memory_stats_object_is_shared?

+ for drm_object_is_shared_for_memory_stats().

Quoted 5 lines
Quoted 2 lines

And s/ie/eg/ in the above quoted drm-usage-stats.rst.

Ack on making it clear this helper would be for fdinfo memory stats only. Sounds like a good idea to stop people from finding really creative uses ...

It's astonishing how defensive the development process has become :)

Cheers,
Christian.

Quoted 62 lines

-Sima

Quoted 58 lines

Regards,

Tvrtko

Quoted 54 lines

Cheers, Sima

Quoted 53 lines

Regards,

Tvrtko

Quoted 49 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +7 -0
+
        domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
        switch (domain) {
        case AMDGPU_GEM_DOMAIN_VRAM:
            stats->vram += size;
            if (amdgpu_bo_in_cpu_visible_vram(bo))
                stats->visible_vram += size;
+        if (shared)
+            stats->vram_shared += size;
            break;
        case AMDGPU_GEM_DOMAIN_GTT:
            stats->gtt += size;
+        if (shared)
+            stats->gtt_shared += size;
            break;
        case AMDGPU_GEM_DOMAIN_CPU:
        default:
            stats->cpu += size;
+        if (shared)
+            stats->cpu_shared += size;
            break;
        }
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
    struct amdgpu_mem_stats {
        /* current VRAM usage, includes visible VRAM */
        uint64_t vram;
+    /* current shared VRAM usage, includes visible VRAM */
+    uint64_t vram_shared;
        /* current visible VRAM usage */
        uint64_t visible_vram;
        /* current GTT usage */
        uint64_t gtt;
+    /* current shared GTT usage */
+    uint64_t gtt_shared;
        /* current system memory usage */
        uint64_t cpu;
+    /* current shared system memory usage */
+    uint64_t cpu_shared;
        /* sum of evicted buffers, includes visible VRAM */
        uint64_t evicted_vram;
        /* sum of evicted buffers due to CPU access */
#12 lore raw

Re: [PATCH 2/2] drm/amdgpu: add shared fdinfo stats

Quoted 98 lines · Christian König

Am 09.01.24 um 15:26 schrieb Daniel Vetter:

Quoted 97 lines
Quoted 95 lines · Tvrtko Ursulin
Quoted 83 lines · Daniel Vetter
Quoted 71 lines · Tvrtko Ursulin
Quoted 45 lines · Christian König

Am 07.12.23 um 19:02 schrieb Alex Deucher:

Quoted 44 lines

Add shared stats.  Useful for seeing shared memory.

v2: take dma-buf into account as well

---

    drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
    3 files changed, 21 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c +3 -0
index 5706b282a0c7..c7df7fa3459f 100644
@@ -97,6 +97,10 @@ void amdgpu_show_fdinfo(struct drm_printer *p,
 struct drm_file *file)
                stats.requested_visible_vram/1024UL);
         drm_printf(p, "amd-requested-gtt:\t%llu KiB\n",
                stats.requested_gtt/1024UL);
+    drm_printf(p, "drm-shared-vram:\t%llu KiB\n",
 stats.vram_shared/1024UL);
+    drm_printf(p, "drm-shared-gtt:\t%llu KiB\n",
 stats.gtt_shared/1024UL);
+    drm_printf(p, "drm-shared-cpu:\t%llu KiB\n",

stats.cpu_shared/1024UL);
+
        for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) { if (!usage[hw_ip])
                continue;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index d79b4ca1ecfc..1b37d95475b8 100644
@@ -1287,25 +1287,36 @@ void amdgpu_bo_get_memory(struct 
 amdgpu_bo *bo,
                   struct amdgpu_mem_stats *stats)
     {
         uint64_t size = amdgpu_bo_size(bo);
+    struct drm_gem_object *obj;
         unsigned int domain;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_get_memory(struct · line 1298

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -0
+    bool shared;
         /* Abort if the BO doesn't currently have a backing store */
         if (!bo->tbo.resource)
             return;
+    obj = &bo->tbo.base;
+    shared = (obj->handle_count > 1) || obj->dma_buf;

I still think that looking at handle_count is the completely wrong approach, we should really only look at obj->dma_buf.

Yeah it is all a bit tricky with the handle table walk. I don't think it is even possible to claim it is shared with obj->dma_buf could be the same process creating say via udmabuf and importing into drm. It is a wild scenario yes, but it could be private memory in that case. Not sure where it would leave us if we said this is just a limitation of a BO based tracking.

Would adding a new category "imported" help?

Hmm or we simply change drm-usage-stats.rst:

"""
- drm-shared-<region>: <uint> [KiB|MiB]

The total size of buffers that are shared with another file (ie. have more than than a single handle).
"""

Quoted 6 lines

Changing ie into eg coule be get our of jail free card to allow the "(obj->handle_count > 1) || obj->dma_buf;" condition?

Because of the shared with another _file_ wording would cover my wild udmabuf self-import case. Unless there are more such creative private import options.

Yeah I think clarifying that we can only track sharing with other fd and have no idea whether this means sharing with another process or not is probably simplest. Maybe not exactly what users want, but still the roughly best-case approximation we can deliver somewhat cheaply.

Quoted 4 lines

Also maybe time for a drm_gem_buffer_object_is_shared() helper so we don't copypaste this all over and then end up in divergent conditions? I'm guessing that there's going to be a bunch of drivers which needs this little helper to add drm-shared-* stats to their fdinfo ...

Yeah I agree that works and i915 would need to use the helper too.

I would only suggest to name it so the meaning of shared is obviously only about the fdinfo memory stats and no one gets a more meaningful idea about its semantics.

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

We have drm_show_memory_stats and drm_print_memory_stats exported so perhaps something like drm_object_is_shared_for_memory_stats, drm_object_is_memory_stats_shared, drm_memory_stats_object_is_shared?

+ for drm_object_is_shared_for_memory_stats().

Hmmm although I probably meant to write drm_gem_object_is_shared_for_memory_stats. Since drm_mode_object seems to have claimed the drm_object_ function name space.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

And s/ie/eg/ in the above quoted drm-usage-stats.rst.

Ack on making it clear this helper would be for fdinfo memory stats only. Sounds like a good idea to stop people from finding really creative uses ...

It's astonishing how defensive the development process has become :)

A bit. :) But probably justified in this case.

Regards,

Tvrtko

Quoted 67 lines

Cheers,
Christian.

Quoted 62 lines

-Sima

Quoted 58 lines

Regards,

Tvrtko

Quoted 54 lines

Cheers, Sima

Quoted 53 lines

Regards,

Tvrtko

Quoted 49 lines

Regards,
Christian.

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
+
         domain = amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type);
         switch (domain) {

        case AMDGPU_GEM_DOMAIN_VRAM:
            stats->vram += size;
            if (amdgpu_bo_in_cpu_visible_vram(bo)) stats->visible_vram += size; +        if (shared)
+            stats->vram_shared += size; break;
        case AMDGPU_GEM_DOMAIN_GTT:
            stats->gtt += size;
+        if (shared)
+            stats->gtt_shared += size; break;
        case AMDGPU_GEM_DOMAIN_CPU:
        default:
            stats->cpu += size;
+        if (shared)
+            stats->cpu_shared += size; break;
        }

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +6 -0
index d28e21baef16..0503af75dc26 100644
@@ -138,12 +138,18 @@ struct amdgpu_bo_vm {
     struct amdgpu_mem_stats {
         /* current VRAM usage, includes visible VRAM */
         uint64_t vram;
+    /* current shared VRAM usage, includes visible VRAM */
+    uint64_t vram_shared;
         /* current visible VRAM usage */
         uint64_t visible_vram;
         /* current GTT usage */
         uint64_t gtt;
+    /* current shared GTT usage */
+    uint64_t gtt_shared;
         /* current system memory usage */
         uint64_t cpu;
+    /* current shared system memory usage */
+    uint64_t cpu_shared;
         /* sum of evicted buffers, includes visible VRAM */
         uint64_t evicted_vram;
         /* sum of evicted buffers due to CPU access */
#13 lore raw
In reply to Alex Deucher
Quoted 7 lines · Alex Deucher

We had a request to add shared buffer stats to fdinfo for amdgpu and while implementing that, Christian mentioned that just looking at the GEM handle count doesn't take into account buffers shared with other subsystems like V4L or RDMA. Those subsystems don't use GEM, so it doesn't really matter from a GPU top perspective, but it's more correct if you actually want to see shared buffers.

Any thoughts on this? Seem reasonable?

Alex

Quoted 14 lines

Alex Deucher (2):
drm: update drm_show_memory_stats() for dma-bufs drm/amdgpu: add shared fdinfo stats

 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
 drivers/gpu/drm/drm_file.c                 |  2 +-
 4 files changed, 22 insertions(+), 1 deletion(-)

--
2.42.0

#14 lore raw

Ping on this series again?

Alex

Quoted 28 lines · Alex Deucher
Quoted 7 lines · Alex Deucher

We had a request to add shared buffer stats to fdinfo for amdgpu and while implementing that, Christian mentioned that just looking at the GEM handle count doesn't take into account buffers shared with other subsystems like V4L or RDMA. Those subsystems don't use GEM, so it doesn't really matter from a GPU top perspective, but it's more correct if you actually want to see shared buffers.

Any thoughts on this? Seem reasonable?

Alex

Quoted 14 lines

Alex Deucher (2):
drm: update drm_show_memory_stats() for dma-bufs drm/amdgpu: add shared fdinfo stats

 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c |  4 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 11 +++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  6 ++++++
 drivers/gpu/drm/drm_file.c                 |  2 +-
 4 files changed, 22 insertions(+), 1 deletion(-)

--
2.42.0