radeonsi: Limit the size of the in-memory shader cache

The in-memory shader cache can get significantly
huge in some rare cases.
Limit its size to 64MB on 32 bits, and 1GB else.

Signed-off-by: Axel Davy <davyaxel0@gmail.com>
Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/9578>
This commit is contained in:
Axel Davy
2021-03-13 11:48:45 +01:00
committed by Marge Bot
parent c23c3b7a77
commit 8283ed65cf
2 changed files with 19 additions and 4 deletions

View File

@@ -645,6 +645,9 @@ struct si_screen {
*/
simple_mtx_t shader_cache_mutex;
struct hash_table *shader_cache;
/* Maximum and current size */
uint32_t shader_cache_size;
uint32_t shader_cache_max_size;
/* Shader cache of live shaders. */
struct util_live_shader_cache live_shader_cache;

View File

@@ -227,6 +227,10 @@ void si_shader_cache_insert_shader(struct si_screen *sscreen, unsigned char ir_s
void *hw_binary;
struct hash_entry *entry;
uint8_t key[CACHE_KEY_SIZE];
bool memory_cache_full = sscreen->shader_cache_size >= sscreen->shader_cache_max_size;
if (!insert_into_disk_cache && memory_cache_full)
return;
entry = _mesa_hash_table_search(sscreen->shader_cache, ir_sha1_cache_key);
if (entry)
@@ -236,10 +240,15 @@ void si_shader_cache_insert_shader(struct si_screen *sscreen, unsigned char ir_s
if (!hw_binary)
return;
if (_mesa_hash_table_insert(sscreen->shader_cache, mem_dup(ir_sha1_cache_key, 20), hw_binary) ==
NULL) {
FREE(hw_binary);
return;
if (!memory_cache_full) {
if (_mesa_hash_table_insert(sscreen->shader_cache,
mem_dup(ir_sha1_cache_key, 20),
hw_binary) == NULL) {
FREE(hw_binary);
return;
}
/* The size is stored at the start of the binary */
sscreen->shader_cache_size += *(uint32_t*)hw_binary;
}
if (sscreen->disk_shader_cache && insert_into_disk_cache) {
@@ -314,6 +323,9 @@ bool si_init_shader_cache(struct si_screen *sscreen)
(void)simple_mtx_init(&sscreen->shader_cache_mutex, mtx_plain);
sscreen->shader_cache =
_mesa_hash_table_create(NULL, si_shader_cache_key_hash, si_shader_cache_key_equals);
sscreen->shader_cache_size = 0;
/* Maximum size: 64MB on 32 bits, 1GB else */
sscreen->shader_cache_max_size = ((sizeof(void *) == 4) ? 64 : 1024) * 1024 * 1024;
return sscreen->shader_cache != NULL;
}