From f78c99f357eee08926dcf9434c3944707837fe34 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alejandro=20Pi=C3=B1eiro?= Date: Wed, 17 Jun 2020 01:41:13 +0200 Subject: [PATCH] v3dv/bo: add a maximum size for the bo_cache and a envvar to configure it V3DV_MAX_BO_CACHE_SIZE can be used to configure it. So one way to disable the bo cache is setting V3DV_MAX_BO_CACHE_SIZE to zero. This would still run all the bo_cache size checks, but having another envvar just to ensure that anything related to the cache is used seemed like an overkill. Part-of: --- src/broadcom/vulkan/v3dv_bo.c | 35 ++++++++++++++++++++++++++++++++++- src/broadcom/vulkan/v3dv_private.h | 4 ++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/broadcom/vulkan/v3dv_bo.c b/src/broadcom/vulkan/v3dv_bo.c index d966cba..fbc9371 100644 --- a/src/broadcom/vulkan/v3dv_bo.c +++ b/src/broadcom/vulkan/v3dv_bo.c @@ -29,11 +29,22 @@ #include "drm-uapi/v3d_drm.h" #include "util/u_memory.h" +/* Default max size of the bo cache, in MB. + * + * FIXME: we got this value when testing some apps using the rpi4 with 4GB, + * but it should depend on the total amount of RAM. But for that we would need + * to test on real hw with different amount of RAM. Using this value for now. + */ +#define DEFAULT_MAX_BO_CACHE_SIZE 512 + static void bo_remove_from_cache(struct v3dv_bo_cache *cache, struct v3dv_bo *bo) { list_del(&bo->time_list); list_del(&bo->size_list); + + cache->cache_count--; + cache->cache_size -= bo->size; } static struct v3dv_bo * @@ -300,6 +311,16 @@ v3dv_bo_cache_init(struct v3dv_device *device) * reallocations */ device->bo_cache.size_list_size = 0; + + const char *max_cache_size_str = getenv("V3DV_MAX_BO_CACHE_SIZE"); + if (max_cache_size_str == NULL) + device->bo_cache.max_cache_size = DEFAULT_MAX_BO_CACHE_SIZE; + else + device->bo_cache.max_cache_size = atoll(max_cache_size_str); + + device->bo_cache.max_cache_size *= 1024 * 1024; + device->bo_cache.cache_count = 0; + device->bo_cache.cache_size = 0; } void @@ -339,8 +360,17 @@ v3dv_bo_free(struct v3dv_device *device, struct v3dv_bo_cache *cache = &device->bo_cache; uint32_t page_index = bo->size / 4096 - 1; - if (!bo->private) + if (bo->private && + bo->size > cache->max_cache_size - cache->cache_size) { + mtx_lock(&cache->lock); + free_stale_bos(device, time.tv_sec); + mtx_unlock(&cache->lock); + } + + if (!bo->private || + bo->size > cache->max_cache_size - cache->cache_size) { return bo_free(device, bo); + } clock_gettime(CLOCK_MONOTONIC, &time); mtx_lock(&cache->lock); @@ -361,6 +391,9 @@ v3dv_bo_free(struct v3dv_device *device, bo->free_time = time.tv_sec; list_addtail(&bo->size_list, &cache->size_list[page_index]); list_addtail(&bo->time_list, &cache->time_list); + + cache->cache_count++; + cache->cache_size += bo->size; bo->name = NULL; free_stale_bos(device, time.tv_sec); diff --git a/src/broadcom/vulkan/v3dv_private.h b/src/broadcom/vulkan/v3dv_private.h index f805a50..5cbdc0b 100644 --- a/src/broadcom/vulkan/v3dv_private.h +++ b/src/broadcom/vulkan/v3dv_private.h @@ -299,6 +299,10 @@ struct v3dv_device { uint32_t size_list_size; mtx_t lock; + + uint32_t cache_size; + uint32_t cache_count; + uint32_t max_cache_size; } bo_cache; uint32_t bo_size; -- 2.7.4