Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-23 Thread Christian König

Am 23.04.21 um 12:55 schrieb Roy Sun:

Tracking devices, process info and fence info using
/proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
  drivers/gpu/drm/amd/amdgpu/Makefile|  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu.h|  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 61 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 98 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h | 43 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 20 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 45 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +
  11 files changed, 282 insertions(+), 2 deletions(-)
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
amdgpu_fw_attestation.o amdgpu_securedisplay.o
  
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

+
  amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
  
  # add asic specific block

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 125b25a5ce5b..3365feae15e1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
  #include "amdgpu_gfxhub.h"
  #include "amdgpu_df.h"
  #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"
  
  #define MAX_GPU_INSTANCE		16
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c

index 0350205c4897..01fe60fedcbe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
idr_destroy(>ctx_handles);
mutex_destroy(>lock);
  }
+
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max)
+{
+   ktime_t now, t1;
+   uint32_t i;
+
+   now = ktime_get();
+   for (i = 0; i < amdgpu_sched_jobs; i++) {
+   struct dma_fence *fence;
+   struct drm_sched_fence *s_fence;
+
+   spin_lock(>ring_lock);
+   fence = dma_fence_get(centity->fences[i]);
+   spin_unlock(>ring_lock);
+   if (!fence)
+   continue;
+   s_fence = to_drm_sched_fence(fence);
+   if (!dma_fence_is_signaled(_fence->scheduled))
+   continue;
+   t1 = s_fence->scheduled.timestamp;
+   if (t1 >= now)
+   continue;
+   if (dma_fence_is_signaled(_fence->finished) &&
+   s_fence->finished.timestamp < now)
+   *total += ktime_sub(s_fence->finished.timestamp, t1);
+   else
+   *total += ktime_sub(now, t1);
+   t1 = ktime_sub(now, t1);
+   dma_fence_put(fence);
+   *max = max(t1, *max);
+   }
+}
+
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)
+{
+   struct idr *idp;
+   struct amdgpu_ctx *ctx;
+   uint32_t id;
+   struct amdgpu_ctx_entity *centity;
+   ktime_t total = 0, max = 0;
+
+   if (idx >= AMDGPU_MAX_ENTITY_NUM)
+   return 0;
+   idp = >ctx_handles;
+   mutex_lock(>lock);
+   idr_for_each_entry(idp, ctx, id) {
+   if (!ctx->entities[hwip][idx])
+   continue;
+
+   centity = ctx->entities[hwip][idx];
+   amdgpu_ctx_fence_time(ctx, centity, , );
+   }
+
+   mutex_unlock(>lock);
+   if (elapsed)
+   *elapsed = max;
+
+   return total;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index f54e10314661..10dcf59a5c6b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
@@ -87,5 +87,8 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
  void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);
  long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
-
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed);
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t 

Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-23 Thread Christian König




Am 23.04.21 um 11:19 schrieb Roy Sun:

Tracking devices, process info and fence info using
/proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
  drivers/gpu/drm/amd/amdgpu/Makefile|  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu.h|  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 61 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 96 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h | 43 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 20 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 45 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  2 +
  11 files changed, 280 insertions(+), 2 deletions(-)
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
amdgpu_fw_attestation.o amdgpu_securedisplay.o
  
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

+
  amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
  
  # add asic specific block

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 125b25a5ce5b..3365feae15e1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
  #include "amdgpu_gfxhub.h"
  #include "amdgpu_df.h"
  #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"
  
  #define MAX_GPU_INSTANCE		16
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c

index 0350205c4897..01fe60fedcbe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
idr_destroy(>ctx_handles);
mutex_destroy(>lock);
  }
+
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max)
+{
+   ktime_t now, t1;
+   uint32_t i;
+
+   now = ktime_get();
+   for (i = 0; i < amdgpu_sched_jobs; i++) {
+   struct dma_fence *fence;
+   struct drm_sched_fence *s_fence;
+
+   spin_lock(>ring_lock);
+   fence = dma_fence_get(centity->fences[i]);
+   spin_unlock(>ring_lock);
+   if (!fence)
+   continue;
+   s_fence = to_drm_sched_fence(fence);
+   if (!dma_fence_is_signaled(_fence->scheduled))
+   continue;
+   t1 = s_fence->scheduled.timestamp;
+   if (t1 >= now)
+   continue;
+   if (dma_fence_is_signaled(_fence->finished) &&
+   s_fence->finished.timestamp < now)
+   *total += ktime_sub(s_fence->finished.timestamp, t1);
+   else
+   *total += ktime_sub(now, t1);
+   t1 = ktime_sub(now, t1);
+   dma_fence_put(fence);
+   *max = max(t1, *max);
+   }
+}
+
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)
+{
+   struct idr *idp;
+   struct amdgpu_ctx *ctx;
+   uint32_t id;
+   struct amdgpu_ctx_entity *centity;
+   ktime_t total = 0, max = 0;
+
+   if (idx >= AMDGPU_MAX_ENTITY_NUM)
+   return 0;
+   idp = >ctx_handles;
+   mutex_lock(>lock);
+   idr_for_each_entry(idp, ctx, id) {
+   if (!ctx->entities[hwip][idx])
+   continue;
+
+   centity = ctx->entities[hwip][idx];
+   amdgpu_ctx_fence_time(ctx, centity, , );
+   }
+
+   mutex_unlock(>lock);
+   if (elapsed)
+   *elapsed = max;
+
+   return total;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index f54e10314661..10dcf59a5c6b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
@@ -87,5 +87,8 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
  void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);
  long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
-
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed);
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   

Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-20 Thread Nieto, David M
[AMD Official Use Only - Internal Distribution Only]

I think we should probably add the pci domain to the bdf to match the format in 
the kernel

+   seq_printf(m, "pdev:\t%02x:%02x.%d\npasid:\t%u\n", bus, dev, fn,
+   fpriv->vm.pasid);

you can get it with

pci_domain_nr(pdev->bus)

David


From: Roy Sun 
Sent: Tuesday, April 20, 2021 8:46 PM
To: amd-gfx@lists.freedesktop.org 
Cc: Sun, Roy ; Nieto, David M 
Subject: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

Tracking devices, process info and fence info using
/proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
 drivers/gpu/drm/amd/amdgpu/Makefile|  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu.h|  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 61 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 92 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h | 43 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c |  1 +
 8 files changed, 208 insertions(+), 2 deletions(-)
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
 amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
 amdgpu_fw_attestation.o amdgpu_securedisplay.o

+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
+
 amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o

 # add asic specific block
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 125b25a5ce5b..3365feae15e1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
 #include "amdgpu_gfxhub.h"
 #include "amdgpu_df.h"
 #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"

 #define MAX_GPU_INSTANCE16

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 0350205c4897..01fe60fedcbe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
 idr_destroy(>ctx_handles);
 mutex_destroy(>lock);
 }
+
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max)
+{
+   ktime_t now, t1;
+   uint32_t i;
+
+   now = ktime_get();
+   for (i = 0; i < amdgpu_sched_jobs; i++) {
+   struct dma_fence *fence;
+   struct drm_sched_fence *s_fence;
+
+   spin_lock(>ring_lock);
+   fence = dma_fence_get(centity->fences[i]);
+   spin_unlock(>ring_lock);
+   if (!fence)
+   continue;
+   s_fence = to_drm_sched_fence(fence);
+   if (!dma_fence_is_signaled(_fence->scheduled))
+   continue;
+   t1 = s_fence->scheduled.timestamp;
+   if (t1 >= now)
+   continue;
+   if (dma_fence_is_signaled(_fence->finished) &&
+   s_fence->finished.timestamp < now)
+   *total += ktime_sub(s_fence->finished.timestamp, t1);
+   else
+   *total += ktime_sub(now, t1);
+   t1 = ktime_sub(now, t1);
+   dma_fence_put(fence);
+   *max = max(t1, *max);
+   }
+}
+
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)
+{
+   struct idr *idp;
+   struct amdgpu_ctx *ctx;
+   uint32_t id;
+   struct amdgpu_ctx_entity *centity;
+   ktime_t total = 0, max = 0;
+
+   if (idx >= AMDGPU_MAX_ENTITY_NUM)
+   return 0;
+   idp = >ctx_handles;
+   mutex_lock(>lock);
+   idr_for_each_entry(idp, ctx, id) {
+   if (!ctx->entities[hwip][idx])
+   continue;
+
+   centity = ctx->entities[hwip][idx];
+   amdgpu_ctx_fence_time(ctx, centity, , );
+   }
+
+   mutex_unlock(>lock);
+   if (elapsed)
+   *elapsed = max;
+
+   return total;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index f54e10314661..10dcf59a5c6b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
@@ -87,5 +87,8 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
 void 

Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-20 Thread Christian König




Am 20.04.21 um 13:51 schrieb Roy Sun:

[SNIP]
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index 848e175e99ff..72727117c479 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -150,6 +150,7 @@ struct amdgpu_vm_bo_base {
/* protected by spinlock */
struct list_headvm_status;
  
+	struct list_head		bo_head;


Well I'm repeating myself. This is a no-go.

You already have the same information in the VM state machine, see 
function amdgpu_debugfs_vm_bo_info().


Christian.


/* protected by the BO being reserved */
boolmoved;
  };
@@ -274,6 +275,7 @@ struct amdgpu_vm {
struct list_headinvalidated;
spinlock_t  invalidated_lock;
  
+	struct list_head	bo_list;

/* BO mappings freed, but not yet updated in the PT */
struct list_headfreed;
  
@@ -458,6 +460,8 @@ void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,

struct amdgpu_vm *vm);
  void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo);
  
+void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem,

+   uint64_t *gtt_mem);
  #if defined(CONFIG_DEBUG_FS)
  void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m);
  #endif


___
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx


Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-20 Thread Christian König

Am 19.04.21 um 08:26 schrieb Roy Sun:

Tracking devices, process info and fence info using
/proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
  drivers/gpu/drm/amd/amdgpu/Makefile|  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu.h|  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 61 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 95 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h | 43 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c|  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 +
  10 files changed, 239 insertions(+), 2 deletions(-)
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
amdgpu_fw_attestation.o amdgpu_securedisplay.o
  
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

+
  amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
  
  # add asic specific block

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 125b25a5ce5b..3365feae15e1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
  #include "amdgpu_gfxhub.h"
  #include "amdgpu_df.h"
  #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"
  
  #define MAX_GPU_INSTANCE		16
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c

index 0350205c4897..01fe60fedcbe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
idr_destroy(>ctx_handles);
mutex_destroy(>lock);
  }
+
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max)
+{
+   ktime_t now, t1;
+   uint32_t i;
+
+   now = ktime_get();
+   for (i = 0; i < amdgpu_sched_jobs; i++) {
+   struct dma_fence *fence;
+   struct drm_sched_fence *s_fence;
+
+   spin_lock(>ring_lock);
+   fence = dma_fence_get(centity->fences[i]);
+   spin_unlock(>ring_lock);
+   if (!fence)
+   continue;
+   s_fence = to_drm_sched_fence(fence);
+   if (!dma_fence_is_signaled(_fence->scheduled))
+   continue;
+   t1 = s_fence->scheduled.timestamp;
+   if (t1 >= now)
+   continue;
+   if (dma_fence_is_signaled(_fence->finished) &&
+   s_fence->finished.timestamp < now)
+   *total += ktime_sub(s_fence->finished.timestamp, t1);
+   else
+   *total += ktime_sub(now, t1);
+   t1 = ktime_sub(now, t1);
+   dma_fence_put(fence);
+   *max = max(t1, *max);
+   }
+}
+
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)
+{
+   struct idr *idp;
+   struct amdgpu_ctx *ctx;
+   uint32_t id;
+   struct amdgpu_ctx_entity *centity;
+   ktime_t total = 0, max = 0;
+
+   if (idx >= AMDGPU_MAX_ENTITY_NUM)
+   return 0;
+   idp = >ctx_handles;
+   mutex_lock(>lock);
+   idr_for_each_entry(idp, ctx, id) {
+   if (!ctx->entities[hwip][idx])
+   continue;
+
+   centity = ctx->entities[hwip][idx];
+   amdgpu_ctx_fence_time(ctx, centity, , );
+   }
+
+   mutex_unlock(>lock);
+   if (elapsed)
+   *elapsed = max;
+
+   return total;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index f54e10314661..10dcf59a5c6b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
@@ -87,5 +87,8 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
  void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);
  long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
-
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed);
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max);
  #endif
diff --git 

RE: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-20 Thread Deng, Emily
Hi Christian,
 Could you help to review these patches again, thanks.

Best wishes
Emily Deng
>-Original Message-
>From: amd-gfx  On Behalf Of Sun, Roy
>Sent: Tuesday, April 20, 2021 4:54 PM
>To: Sun, Roy ; amd-gfx@lists.freedesktop.org
>Cc: Nieto, David M 
>Subject: RE: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface
>
>[AMD Official Use Only - Internal Distribution Only]
>
>Ping.
>Could you help review this patch again?
>
>BR
>Roy
>
>-Original Message-
>From: Roy Sun 
>Sent: Monday, April 19, 2021 2:26 PM
>To: amd-gfx@lists.freedesktop.org
>Cc: Sun, Roy ; Nieto, David M 
>Subject: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface
>
>Tracking devices, process info and fence info using /proc/pid/fdinfo
>
>Signed-off-by: David M Nieto 
>Signed-off-by: Roy Sun 
>---
> drivers/gpu/drm/amd/amdgpu/Makefile|  2 +
> drivers/gpu/drm/amd/amdgpu/amdgpu.h|  1 +
> drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 61 ++
> drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|  5 +-
> drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|  5 +-
> drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 95
>++  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h |
>43 ++
> drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c|  1 +
> drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 ++
> drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 +
> 10 files changed, 239 insertions(+), 2 deletions(-)  create mode 100644
>drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
> create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h
>
>diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile
>b/drivers/gpu/drm/amd/amdgpu/Makefile
>index ee85e8aba636..d216b7ecb5d1 100644
>--- a/drivers/gpu/drm/amd/amdgpu/Makefile
>+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
>@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
>   amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o
>amdgpu_rap.o \
>   amdgpu_fw_attestation.o amdgpu_securedisplay.o
>
>+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
>+
> amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
>
> # add asic specific block
>diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
>b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
>index 125b25a5ce5b..3365feae15e1 100644
>--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
>+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
>@@ -107,6 +107,7 @@
> #include "amdgpu_gfxhub.h"
> #include "amdgpu_df.h"
> #include "amdgpu_smuio.h"
>+#include "amdgpu_fdinfo.h"
>
> #define MAX_GPU_INSTANCE  16
>
>diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
>b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
>index 0350205c4897..01fe60fedcbe 100644
>--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
>+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
>@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr
>*mgr)
>   idr_destroy(>ctx_handles);
>   mutex_destroy(>lock);
> }
>+
>+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct
>amdgpu_ctx_entity *centity,
>+  ktime_t *total, ktime_t *max)
>+{
>+  ktime_t now, t1;
>+  uint32_t i;
>+
>+  now = ktime_get();
>+  for (i = 0; i < amdgpu_sched_jobs; i++) {
>+  struct dma_fence *fence;
>+  struct drm_sched_fence *s_fence;
>+
>+  spin_lock(>ring_lock);
>+  fence = dma_fence_get(centity->fences[i]);
>+  spin_unlock(>ring_lock);
>+  if (!fence)
>+  continue;
>+  s_fence = to_drm_sched_fence(fence);
>+  if (!dma_fence_is_signaled(_fence->scheduled))
>+  continue;
>+  t1 = s_fence->scheduled.timestamp;
>+  if (t1 >= now)
>+  continue;
>+  if (dma_fence_is_signaled(_fence->finished) &&
>+  s_fence->finished.timestamp < now)
>+  *total += ktime_sub(s_fence->finished.timestamp, t1);
>+  else
>+  *total += ktime_sub(now, t1);
>+  t1 = ktime_sub(now, t1);
>+  dma_fence_put(fence);
>+  *max = max(t1, *max);
>+  }
>+}
>+
>+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr,
>uint32_t hwip,
>+  uint32_t idx, uint64_t *elapsed)
>+{
>+  struct idr *idp;
>+  struct amdgpu_ctx *ctx;
>+  uint32_t id;
>+  struct amdgpu_ctx_entity *centity;
>+  ktime_t total = 0, max = 0;
>+
>+  if (idx >= AMDGPU_MAX_ENTITY_NUM)
>+  return 0;
>+  idp = >ctx_handles;
>+  mutex_lock(>

RE: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-20 Thread Sun, Roy
[AMD Official Use Only - Internal Distribution Only]

Ping.
Could you help review this patch again?

BR
Roy

-Original Message-
From: Roy Sun  
Sent: Monday, April 19, 2021 2:26 PM
To: amd-gfx@lists.freedesktop.org
Cc: Sun, Roy ; Nieto, David M 
Subject: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

Tracking devices, process info and fence info using /proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
 drivers/gpu/drm/amd/amdgpu/Makefile|  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu.h|  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 61 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 95 ++  
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h | 43 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c|  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 24 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  4 +
 10 files changed, 239 insertions(+), 2 deletions(-)  create mode 100644 
drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
amdgpu_fw_attestation.o amdgpu_securedisplay.o
 
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o
+
 amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
 
 # add asic specific block
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 125b25a5ce5b..3365feae15e1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
 #include "amdgpu_gfxhub.h"
 #include "amdgpu_df.h"
 #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"
 
 #define MAX_GPU_INSTANCE   16
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 0350205c4897..01fe60fedcbe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
idr_destroy(>ctx_handles);
mutex_destroy(>lock);
 }
+
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max)
+{
+   ktime_t now, t1;
+   uint32_t i;
+
+   now = ktime_get();
+   for (i = 0; i < amdgpu_sched_jobs; i++) {
+   struct dma_fence *fence;
+   struct drm_sched_fence *s_fence;
+
+   spin_lock(>ring_lock);
+   fence = dma_fence_get(centity->fences[i]);
+   spin_unlock(>ring_lock);
+   if (!fence)
+   continue;
+   s_fence = to_drm_sched_fence(fence);
+   if (!dma_fence_is_signaled(_fence->scheduled))
+   continue;
+   t1 = s_fence->scheduled.timestamp;
+   if (t1 >= now)
+   continue;
+   if (dma_fence_is_signaled(_fence->finished) &&
+   s_fence->finished.timestamp < now)
+   *total += ktime_sub(s_fence->finished.timestamp, t1);
+   else
+   *total += ktime_sub(now, t1);
+   t1 = ktime_sub(now, t1);
+   dma_fence_put(fence);
+   *max = max(t1, *max);
+   }
+}
+
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)
+{
+   struct idr *idp;
+   struct amdgpu_ctx *ctx;
+   uint32_t id;
+   struct amdgpu_ctx_entity *centity;
+   ktime_t total = 0, max = 0;
+
+   if (idx >= AMDGPU_MAX_ENTITY_NUM)
+   return 0;
+   idp = >ctx_handles;
+   mutex_lock(>lock);
+   idr_for_each_entry(idp, ctx, id) {
+   if (!ctx->entities[hwip][idx])
+   continue;
+
+   centity = ctx->entities[hwip][idx];
+   amdgpu_ctx_fence_time(ctx, centity, , );
+   }
+
+   mutex_unlock(>lock);
+   if (elapsed)
+   *elapsed = max;
+
+   return total;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index f54e10314661..10dcf59a5c6b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
@@ -87,5 +87,8 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);  void 
amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);  long 
amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);  void 
amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
-
+ktime_t 

Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-14 Thread Christian König

Am 14.04.21 um 15:59 schrieb Roy Sun:

Tracking devices, process info and fence info using
/proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
  drivers/gpu/drm/amd/amdgpu/Makefile|  2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu.h|  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c| 61 +++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|  5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 91 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h | 43 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c|  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 17 
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |  3 +
  10 files changed, 227 insertions(+), 2 deletions(-)
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
amdgpu_fw_attestation.o amdgpu_securedisplay.o
  
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

+
  amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
  
  # add asic specific block

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 616c85a01299..c2338a0dd1f0 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
  #include "amdgpu_gfxhub.h"
  #include "amdgpu_df.h"
  #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"
  
  #define MAX_GPU_INSTANCE		16
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c

index 0350205c4897..01fe60fedcbe 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
idr_destroy(>ctx_handles);
mutex_destroy(>lock);
  }
+
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max)
+{
+   ktime_t now, t1;
+   uint32_t i;
+
+   now = ktime_get();
+   for (i = 0; i < amdgpu_sched_jobs; i++) {
+   struct dma_fence *fence;
+   struct drm_sched_fence *s_fence;
+
+   spin_lock(>ring_lock);
+   fence = dma_fence_get(centity->fences[i]);
+   spin_unlock(>ring_lock);
+   if (!fence)
+   continue;
+   s_fence = to_drm_sched_fence(fence);
+   if (!dma_fence_is_signaled(_fence->scheduled))
+   continue;
+   t1 = s_fence->scheduled.timestamp;
+   if (t1 >= now)
+   continue;
+   if (dma_fence_is_signaled(_fence->finished) &&
+   s_fence->finished.timestamp < now)
+   *total += ktime_sub(s_fence->finished.timestamp, t1);
+   else
+   *total += ktime_sub(now, t1);
+   t1 = ktime_sub(now, t1);
+   dma_fence_put(fence);
+   *max = max(t1, *max);
+   }
+}
+
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)
+{
+   struct idr *idp;
+   struct amdgpu_ctx *ctx;
+   uint32_t id;
+   struct amdgpu_ctx_entity *centity;
+   ktime_t total = 0, max = 0;
+
+   if (idx >= AMDGPU_MAX_ENTITY_NUM)
+   return 0;
+   idp = >ctx_handles;
+   mutex_lock(>lock);
+   idr_for_each_entry(idp, ctx, id) {
+   if (!ctx->entities[hwip][idx])
+   continue;
+
+   centity = ctx->entities[hwip][idx];
+   amdgpu_ctx_fence_time(ctx, centity, , );
+   }
+
+   mutex_unlock(>lock);
+   if (elapsed)
+   *elapsed = max;
+
+   return total;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index f54e10314661..10dcf59a5c6b 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
@@ -87,5 +87,8 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);
  void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);
  long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);
-
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed);
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity 
*centity,
+   ktime_t *total, ktime_t *max);
  #endif
diff --git 

Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-13 Thread Christian König

Am 13.04.21 um 14:14 schrieb Roy Sun:

Tracking devices, process info and fence info using
/proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
  drivers/gpu/drm/amd/amdgpu/Makefile|   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu.h|   1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c|  59 
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h|   3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|   5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 149 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h |  43 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c|  21 +++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h|   3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c|   1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c |  35 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |   2 +
  12 files changed, 321 insertions(+), 3 deletions(-)
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
amdgpu_fw_attestation.o amdgpu_securedisplay.o
  
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

+
  amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
  
  # add asic specific block

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 616c85a01299..c2338a0dd1f0 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
  #include "amdgpu_gfxhub.h"
  #include "amdgpu_df.h"
  #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"
  
  #define MAX_GPU_INSTANCE		16
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c

index 0350205c4897..8d33571754d9 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -651,3 +651,62 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
idr_destroy(>ctx_handles);
mutex_destroy(>lock);
  }
+
+ktime_t amdgpu_ctx_fence_usage(struct amdgpu_fpriv *fpriv, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)


That starts to look better, but please change this function so that it 
gets a amdgpu_ctx_mgr structure as first parameter and rename it to 
amdgpu_ctx_mgr_fence_usage.



+{
+   struct amdgpu_ctx_entity *centity;
+   struct idr *idp;
+   struct amdgpu_ctx *ctx;
+   uint32_t id, i;
+   ktime_t now, t1;
+   ktime_t total = 0, max = 0;
+
+
+   if (idx >= AMDGPU_MAX_ENTITY_NUM)
+   return 0;
+
+   idp = >ctx_mgr.ctx_handles;
+
+   mutex_lock(>ctx_mgr.lock);
+   idr_for_each_entry(idp, ctx, id) {


Maybe move everything from here till the end of the loop into a separate 
function.



+   if (!ctx->entities[hwip][idx])
+   continue;
+
+   centity = ctx->entities[hwip][idx];
+   now = ktime_get();
+   for (i = 0; i < amdgpu_sched_jobs; i++) {
+   struct dma_fence *fence;
+   struct drm_sched_fence *s_fence;
+
+   spin_lock(>ring_lock);
+   fence = dma_fence_get(centity->fences[i]);
+   spin_unlock(>ring_lock);
+   if (!fence)
+   continue;
+   s_fence = to_drm_sched_fence(fence);
+   if (!dma_fence_is_signaled(_fence->scheduled))
+   continue;
+   t1 = s_fence->scheduled.timestamp;
+   if (t1 >= now)
+   continue;
+   if (dma_fence_is_signaled(_fence->finished) &&
+   s_fence->finished.timestamp < 
now)


That is indented to far to the right, the "s_fence..." should be below 
the "dma_fence..." in the line above.



+   total += ktime_sub(s_fence->finished.timestamp, 
t1);
+   else
+   total += ktime_sub(now, t1);
+   t1 = ktime_sub(now, t1);
+   dma_fence_put(fence);
+   if (t1 > max)
+   max = t1;


Please write this as "max = max(max, t1);".


+   }
+
+   }
+
+   mutex_unlock(>ctx_mgr.lock);
+
+   if (elapsed)
+   *elapsed = max;
+
+   return total;
+}
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
index f54e10314661..8ee42f12e5f8 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h
+++ 

Re: [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface

2021-04-12 Thread Christian König

Am 12.04.21 um 14:57 schrieb Roy Sun:

Tracking devices, process info and fence info using
/proc/pid/fdinfo

Signed-off-by: David M Nieto 
Signed-off-by: Roy Sun 
---
  drivers/gpu/drm/amd/amdgpu/Makefile|   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu.h|   1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c|   5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 207 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h |  50 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c|  21 +++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h|   3 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c|   1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c |  35 
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h |   2 +
  10 files changed, 325 insertions(+), 2 deletions(-)
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
  create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h

diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile 
b/drivers/gpu/drm/amd/amdgpu/Makefile
index ee85e8aba636..d216b7ecb5d1 100644
--- a/drivers/gpu/drm/amd/amdgpu/Makefile
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \
amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \
amdgpu_fw_attestation.o amdgpu_securedisplay.o
  
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o

+
  amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o
  
  # add asic specific block

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h 
b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
index 616c85a01299..c2338a0dd1f0 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h
@@ -107,6 +107,7 @@
  #include "amdgpu_gfxhub.h"
  #include "amdgpu_df.h"
  #include "amdgpu_smuio.h"
+#include "amdgpu_fdinfo.h"
  
  #define MAX_GPU_INSTANCE		16
  
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c

index 4bcc03c4c6c5..07aed377dec8 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c
@@ -42,7 +42,7 @@
  #include "amdgpu_irq.h"
  #include "amdgpu_dma_buf.h"
  #include "amdgpu_sched.h"
-
+#include "amdgpu_fdinfo.h"
  #include "amdgpu_amdkfd.h"
  
  #include "amdgpu_ras.h"

@@ -1691,6 +1691,9 @@ static const struct file_operations 
amdgpu_driver_kms_fops = {
  #ifdef CONFIG_COMPAT
.compat_ioctl = amdgpu_kms_compat_ioctl,
  #endif
+#ifdef CONFIG_PROC_FS
+   .show_fdinfo = amdgpu_show_fdinfo
+#endif
  };
  
  int amdgpu_file_to_fpriv(struct file *filp, struct amdgpu_fpriv **fpriv)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
new file mode 100644
index ..b6523fb141c2
--- /dev/null
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: MIT
+/* Copyright 2021 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: David Nieto
+ *  Roy Sun
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include "amdgpu.h"
+#include "amdgpu_vm.h"
+#include "amdgpu_gem.h"
+#include "amdgpu_fdinfo.h"
+
+
+static const char *amdgpu_ip_name[AMDGPU_HW_IP_NUM] = {
+   [AMDGPU_HW_IP_GFX]  =   "gfx",
+   [AMDGPU_HW_IP_COMPUTE]  =   "compute",
+   [AMDGPU_HW_IP_DMA]  =   "dma",
+   [AMDGPU_HW_IP_UVD]  =   "dec",
+   [AMDGPU_HW_IP_VCE]  =   "enc",
+   [AMDGPU_HW_IP_UVD_ENC]  =   "enc_1",
+   [AMDGPU_HW_IP_VCN_DEC]  =   "dec",
+   [AMDGPU_HW_IP_VCN_ENC]  =   "enc",
+   [AMDGPU_HW_IP_VCN_JPEG] =   "jpeg",
+};
+
+uint64_t amdgpu_get_fence_usage(struct amdgpu_fpriv *fpriv, uint32_t hwip,
+   uint32_t idx, uint64_t *elapsed)
+{


Well first of all you should move the code into the amdgpu_ctx.c file.


+   struct amdgpu_ctx_entity *centity;
+   struct idr *idp;
+