Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [igt-dev] [PATCH i-g-t] lib/xe_query: add helper getting available VRAM
@ 2023-08-07  9:24 Lukasz Laguna
  2023-08-07 10:55 ` [igt-dev] ○ CI.xeBAT: info for lib/xe_query: add helper getting available VRAM (rev4) Patchwork
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Lukasz Laguna @ 2023-08-07  9:24 UTC (permalink / raw)
  To: igt-dev; +Cc: mauro.chehab

Helper allows to read available VRAM of a specified device and GT.

v2:
- Query for current memory usage instead of using cached one (Zbigniew)
v3:
- Reacquire mem_region pointer (Zbigniew)
v4:
- Update value of used memory only in xe_dev

Signed-off-by: Lukasz Laguna <lukasz.laguna@intel.com>
---
 lib/xe/xe_query.c | 35 +++++++++++++++++++++++++++++++++++
 lib/xe/xe_query.h |  1 +
 2 files changed, 36 insertions(+)

diff --git a/lib/xe/xe_query.c b/lib/xe/xe_query.c
index 8963c7b06..c356abe1e 100644
--- a/lib/xe/xe_query.c
+++ b/lib/xe/xe_query.c
@@ -630,6 +630,41 @@ uint64_t xe_visible_vram_size(int fd, int gt)
 
 	return visible_size;
 }
+/**
+ * xe_vram_available:
+ * @fd: xe device fd
+ * @gt: gt
+ *
+ * Returns available vram of xe device @fd and @gt.
+ */
+uint64_t xe_vram_available(int fd, int gt)
+{
+	struct xe_device *xe_dev;
+	int region_idx;
+	struct drm_xe_query_mem_region *mem_region;
+	struct drm_xe_query_mem_usage *mem_usage;
+
+	xe_dev = find_in_cache(fd);
+	igt_assert(xe_dev);
+
+	region_idx = ffs(native_region_for_gt(xe_dev->gts, gt)) - 1;
+	mem_region = &xe_dev->mem_usage->regions[region_idx];
+
+	if (XE_IS_CLASS_VRAM(mem_region)) {
+		uint64_t available_vram;
+
+		mem_usage = xe_query_mem_usage_new(fd);
+		pthread_mutex_lock(&cache.cache_mutex);
+		mem_region->used = mem_usage->regions[region_idx].used;
+		available_vram = mem_region->total_size - mem_region->used;
+		pthread_mutex_unlock(&cache.cache_mutex);
+		free(mem_usage);
+
+		return available_vram;
+	}
+
+	return 0;
+}
 
 /**
  * xe_get_default_alignment:
diff --git a/lib/xe/xe_query.h b/lib/xe/xe_query.h
index 1b74c58ab..20dbfa12c 100644
--- a/lib/xe/xe_query.h
+++ b/lib/xe/xe_query.h
@@ -97,6 +97,7 @@ unsigned int xe_number_hw_engines(int fd);
 bool xe_has_vram(int fd);
 uint64_t xe_vram_size(int fd, int gt);
 uint64_t xe_visible_vram_size(int fd, int gt);
+uint64_t xe_vram_available(int fd, int gt);
 uint32_t xe_get_default_alignment(int fd);
 uint32_t xe_va_bits(int fd);
 uint16_t xe_dev_id(int fd);
-- 
2.40.0

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-08-08 10:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-07  9:24 [igt-dev] [PATCH i-g-t] lib/xe_query: add helper getting available VRAM Lukasz Laguna
2023-08-07 10:55 ` [igt-dev] ○ CI.xeBAT: info for lib/xe_query: add helper getting available VRAM (rev4) Patchwork
2023-08-07 10:58 ` [igt-dev] ✓ Fi.CI.BAT: success " Patchwork
2023-08-07 11:58 ` [igt-dev] [PATCH i-g-t] lib/xe_query: add helper getting available VRAM Zbigniew Kempczyński
2023-08-07 14:39 ` [igt-dev] ✗ Fi.CI.IGT: failure for lib/xe_query: add helper getting available VRAM (rev4) Patchwork
2023-08-08 10:46   ` Laguna, Lukasz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox