[PATCH umr] top vram add visible vram usage info

StDenis, Tom Tom.StDenis at amd.com
Tue Jul 11 02:10:39 UTC 2017


Hi Qiang,

Sorry I must have missed that.  I'll try it out first thing in the morning (Markham time).  If it's all good I'll add a Rb and push it out.

Cheers,
Tom
________________________________________
From: Yu, Qiang
Sent: Monday, July 10, 2017 22:07
To: amd-gfx at lists.freedesktop.org; StDenis, Tom
Subject: Re: [PATCH umr] top vram add visible vram usage info

Hi Tom,

Any feedback on this patch?

Regards,
Qiang
________________________________________
From: Qiang Yu <Qiang.Yu at amd.com>
Sent: Thursday, July 6, 2017 2:32:20 PM
To: amd-gfx at lists.freedesktop.org
Cc: Yu, Qiang
Subject: [PATCH umr] top vram add visible vram usage info

Signed-off-by: Qiang Yu <Qiang.Yu at amd.com>
---
 src/app/top.c       | 56 ++++++++++++++++++++++++++++++++++++++++-------------
 src/lib/query_drm.c |  4 ++--
 src/umr.h           |  2 +-
 3 files changed, 46 insertions(+), 16 deletions(-)

diff --git a/src/app/top.c b/src/app/top.c
index 4a12f1b..359d241 100644
--- a/src/app/top.c
+++ b/src/app/top.c
@@ -287,6 +287,8 @@ static struct umr_bitfield stat_drm_bits[] = {

 static FILE *logfile = NULL;

+static uint64_t visible_vram_size = 0;
+
 static volatile int sensor_thread_quit = 0;
 static volatile uint32_t gpu_power_data[32];
 static volatile struct umr_bitfield *sensor_bits = NULL;
@@ -634,7 +636,7 @@ static void parse_drm(struct umr_asic *asic, uint32_t addr, struct umr_bitfield
                else if (bits[j].start == AMDGPU_INFO_WAVES)
                        counts[j] = vi_count_waves(asic);
                else
-                       umr_query_drm(asic, bits[j].start, &counts[j]);
+                       umr_query_drm(asic, bits[j].start, &counts[j], sizeof(counts[j]));
        }
 }

@@ -643,6 +645,7 @@ static void grab_vram(struct umr_asic *asic)
        char name[256];
        FILE *f;
        unsigned long total, free, used;
+       unsigned long man_size, ram_usage, vis_usage;

        snprintf(name, sizeof(name)-1, "/sys/kernel/debug/dri/%d/amdgpu_vram_mm", asic->instance);
        f = fopen(name, "rb");
@@ -650,47 +653,57 @@ static void grab_vram(struct umr_asic *asic)
                fseek(f, -128, SEEK_END); // skip to end of file
                memset(name, 0, sizeof name);
                while (fgets(name, sizeof(name)-1, f)) {
-                       if (memcmp(name, "total:", 6) == 0) {
-                               if (sscanf(name, "total: %lu, used %lu free %lu", &total, &used, &free) == 3)
-                                       printw("\nVRAM: %lu/%lu (MiB)\n", (used * 4096) / 1048576, (total * 4096) / 1048576);
-                               break;
-                       }
+                       if (!memcmp(name, "total:", 6))
+                               sscanf(name, "total: %lu, used %lu free %lu", &total, &used, &free);
+                       else if (!memcmp(name, "man size:", 9))
+                               sscanf(name, "man size:%lu pages, ram usage:%luMB, vis usage:%luMB",
+                                      &man_size, &ram_usage, &vis_usage);
                }
                fclose(f);
+
+               printw("\nVRAM: %lu/%lu vis %lu/%llu (MiB)\n",
+                      (used * 4096) / 1048576, (total * 4096) / 1048576,
+                      vis_usage, visible_vram_size >> 20);
        }
 }

 static void analyze_drm_info(struct umr_asic *asic)
 {
        char region[256], name[256], line[256];
-       unsigned long old_pid, pid, id, size, tot_vram, tot_gtt;
+       unsigned long old_pid, pid, id, size, tot_vram, tot_gtt, tot_vis_vram;
        FILE *f;
+       unsigned long long vram_addr;

        snprintf(name, sizeof(name)-1, "/sys/kernel/debug/dri/%d/amdgpu_gem_info", asic->instance);
        f = fopen(name, "rb");
        if (f) {
                name[0] = 0;
-               old_pid = pid = tot_vram = tot_gtt = 0;
+               old_pid = pid = tot_vram = tot_gtt = tot_vis_vram = 0;
                while (fgets(line, sizeof(line)-1, f)) {
                        if (sscanf(line, "pid %lu command %s:", &pid, region) == 2) {
                                if (name[0]) {
                                        snprintf(line, sizeof(line)-1, "%s(%5lu)", name, old_pid);
-                                       printw("   %-30s: %10lu KiB VRAM, %10lu KiB GTT\n", line, tot_vram>>10, tot_gtt>>10);
+                                       printw("   %-30s: %10lu KiB VRAM, %10lu KiB vis VRAM, %10lu KiB GTT\n",
+                                              line, tot_vram>>10, tot_vis_vram>>10, tot_gtt>>10);
                                }
-                               tot_vram = tot_gtt = 0;
+                               tot_vram = tot_gtt = tot_vis_vram = 0;
                                old_pid = pid;
                                strcpy(name, region);
                        } else {
-                               sscanf(line, "\t0x%08lx: %lu byte %s @", &id, &size, region);
-                               if (!strcmp(region, "VRAM"))
+                               sscanf(line, "\t0x%08lx: %lu byte %s @ %llx", &id, &size, region, &vram_addr);
+                               if (!strcmp(region, "VRAM")) {
                                        tot_vram += size;
+                                       if (vram_addr < visible_vram_size>>12)
+                                               tot_vis_vram += size;
+                               }
                                else
                                        tot_gtt += size;
                        }
                }
                if (name[0]) {
                        snprintf(line, sizeof(line)-1, "%s(%5lu)", name, old_pid);
-                       printw("   %-30s: %10lu KiB VRAM, %10lu KiB GTT\n", line, tot_vram>>10, tot_gtt>>10);
+                       printw("   %-30s: %10lu KiB VRAM, %10lu KiB vis VRAM, %10lu KiB GTT\n",
+                              line, tot_vram>>10, tot_vis_vram>>10, tot_gtt>>10);
                }
                fclose(f);
        }
@@ -923,6 +936,21 @@ static void toggle_logger(void)
        }
 }

+#define AMDGPU_INFO_VRAM_GTT                   0x14
+static uint64_t get_visible_vram_size(struct umr_asic *asic)
+{
+       struct drm_amdgpu_info_vram_gtt {
+               uint64_t vram_size;
+               uint64_t vram_cpu_accessible_size;
+               uint64_t gtt_size;
+       } info;
+
+       if (umr_query_drm(asic, AMDGPU_INFO_VRAM_GTT, &info, sizeof(info)))
+           return 0;
+
+       return info.vram_cpu_accessible_size;
+}
+
 void umr_top(struct umr_asic *asic)
 {
        int i, j, k;
@@ -967,6 +995,8 @@ void umr_top(struct umr_asic *asic)
                return;
        }

+       visible_vram_size = get_visible_vram_size(asic);
+
        initscr();
        start_color();
        cbreak();
diff --git a/src/lib/query_drm.c b/src/lib/query_drm.c
index 755c65f..c92794b 100644
--- a/src/lib/query_drm.c
+++ b/src/lib/query_drm.c
@@ -33,13 +33,13 @@
 #define DRM_IOCTL_BASE                  'd'
 #define DRM_COMMAND_BASE                0x40

-int umr_query_drm(struct umr_asic *asic, int field, uint64_t *ret)
+int umr_query_drm(struct umr_asic *asic, int field, void *ret, int size)
 {
        struct drm_amdgpu_info inf;

        memset(&inf, 0, sizeof inf);
        inf.return_pointer = (uintptr_t)ret;
-       inf.return_size = sizeof(*ret);
+       inf.return_size = size;
        inf.query = field;
        return ioctl(asic->fd.drm, DRM_IOC(DRM_IOC_WRITE, DRM_IOCTL_BASE, DRM_COMMAND_BASE + DRM_AMDGPU_INFO, sizeof(inf)), &inf);

diff --git a/src/umr.h b/src/umr.h
index 4d63708..0a15c50 100644
--- a/src/umr.h
+++ b/src/umr.h
@@ -456,7 +456,7 @@ struct umr_asic *umr_discover_asic_by_did(struct umr_options *options, long did)
 struct umr_asic *umr_discover_asic_by_name(struct umr_options *options, char *name);
 void umr_free_asic(struct umr_asic *asic);
 void umr_close_asic(struct umr_asic *asic); // call this to close a fully open asic
-int umr_query_drm(struct umr_asic *asic, int field, uint64_t *ret);
+int umr_query_drm(struct umr_asic *asic, int field, void *ret, int size);
 void umr_enumerate_devices(void);
 int umr_update(struct umr_asic *asic, char *script);

--
1.9.1



More information about the amd-gfx mailing list