From fbdd9bec929ac0f6a170c2f615e66fa863c1632f Mon Sep 17 00:00:00 2001 From: Karmjit Mahil Date: Mon, 27 Apr 2026 14:50:41 +0100 Subject: [PATCH] util: Add heap_memory_percent driconf option Also adds helper function to be used by drivers. Signed-off-by: Karmjit Mahil Reviewed-by: Iago Toral Quiroga --- src/util/driconf.h | 4 ++++ src/util/os_misc.c | 1 + src/util/os_misc.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/src/util/driconf.h b/src/util/driconf.h index fc669e82e91..a595594f65b 100644 --- a/src/util/driconf.h +++ b/src/util/driconf.h @@ -541,6 +541,10 @@ DRI_CONF_OPT_B(no_fp16, def, \ "Disable 16-bit float support") +#define DRI_CONF_HEAP_MEMORY_PERCENT(def) \ + DRI_CONF_OPT_F(heap_memory_percent, def, 0.0, 1.0, \ + "Percentage of total system memory to report as gpu heap memory (0 = driver default)") + #define DRI_CONF_VK_ZERO_VRAM(def) \ DRI_CONF_OPT_B(vk_zero_vram, def, \ "Initialize to zero all VRAM allocations") diff --git a/src/util/os_misc.c b/src/util/os_misc.c index d379d31de1a..e1b87d8c568 100644 --- a/src/util/os_misc.c +++ b/src/util/os_misc.c @@ -33,6 +33,7 @@ #include "ralloc.h" #include "simple_mtx.h" #include "u_debug.h" +#include "u_math.h" #include diff --git a/src/util/os_misc.h b/src/util/os_misc.h index 9b411ac7126..b27ff720739 100644 --- a/src/util/os_misc.h +++ b/src/util/os_misc.h @@ -39,6 +39,7 @@ #include #include "util/detect.h" +#include "util/u_math.h" #if DETECT_OS_POSIX @@ -158,6 +159,53 @@ os_unset_option(const char *name) bool os_get_total_physical_memory(uint64_t *size); +#define OS_GPU_HEAP_SIZE_HEURISTIC (0.0f) + +/* + * Calculate the gpu heap size based on a percentage of @memory. + * If @percent is OS_GPU_HEAP_SIZE_HEURISTIC: + * Use a heuristic. + */ +static inline uint64_t +os_gpu_heap_size_calculate(uint64_t memory, float percent, float *percent_out) +{ + if (percent == OS_GPU_HEAP_SIZE_HEURISTIC) { + /* We don't want to burn too much ram with the GPU on devices with a small + * amount of memory. + */ + if (memory <= 1ull * 1024ull * 1024ull * 1024ull) + percent = 0.25f; + else if (memory <= 4ull * 1024ull * 1024ull * 1024ull) + percent = 0.5f; + else + percent = 0.75f; + } + + if (percent_out) + *percent_out = percent; + + return ROUND_DOWN_TO((uint64_t)(memory * percent), 1 << 20); +} + +/* + * Calculate the gpu heap size based on a percentage of the system memory. + * If @percent is OS_GPU_HEAP_SIZE_HEURISTIC: + * Use a heuristic. + * + * @percent_out is preserved on failure. + */ +static inline uint64_t +os_get_gpu_heap_size(float percent, float *percent_out) +{ + uint64_t memory; + + const bool success = os_get_total_physical_memory(&memory); + if (!success) + return 0; + + return os_gpu_heap_size_calculate(memory, percent, percent_out); +} + /* * Amount of physical memory available to a process */