From d0a52432b19f1d3e61b20a5dd4ba1a1e5546bd7d Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Tue, 10 Mar 2020 14:52:42 -0700 Subject: [PATCH] glsl/tests: Fix waiting for disk_cache_put() to finish. We were wasting 4s on waiting for expected-not-to-appear files to show up on every test. Using timeouts in test code is error-prone anyway, as our shared runners may be busy on other jobs. Fixes: 50989f87e62e ("util/disk_cache: use a thread queue to write to shader cache") Link: https://gitlab.freedesktop.org/mesa/mesa/issues/2505 Reviewed-by: Timothy Arceri Tested-by: Marge Bot Part-of: --- src/compiler/glsl/tests/cache_test.c | 53 ++++++---------------------- src/util/disk_cache.c | 6 ++++ src/util/disk_cache.h | 6 ++++ 3 files changed, 23 insertions(+), 42 deletions(-) diff --git a/src/compiler/glsl/tests/cache_test.c b/src/compiler/glsl/tests/cache_test.c index cf654e5b01d..a1db67a5845 100644 --- a/src/compiler/glsl/tests/cache_test.c +++ b/src/compiler/glsl/tests/cache_test.c @@ -162,26 +162,6 @@ does_cache_contain(struct disk_cache *cache, const cache_key key) return false; } -static void -wait_until_file_written(struct disk_cache *cache, const cache_key key) -{ - struct timespec req; - struct timespec rem; - - /* Set 100ms delay */ - req.tv_sec = 0; - req.tv_nsec = 100000000; - - unsigned retries = 0; - while (retries++ < 20) { - if (does_cache_contain(cache, key)) { - break; - } - - nanosleep(&req, &rem); - } -} - static void * cache_exists(struct disk_cache *cache) { @@ -192,7 +172,7 @@ cache_exists(struct disk_cache *cache) return NULL; disk_cache_put(cache, dummy_key, data, sizeof(data), NULL); - wait_until_file_written(cache, dummy_key); + disk_cache_wait_for_idle(cache); return disk_cache_get(cache, dummy_key, NULL); } @@ -298,10 +278,8 @@ test_put_and_get(void) /* Simple test of put and get. */ disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL); - /* disk_cache_put() hands things off to a thread give it some time to - * finish. - */ - wait_until_file_written(cache, blob_key); + /* disk_cache_put() hands things off to a thread so wait for it. */ + disk_cache_wait_for_idle(cache); result = disk_cache_get(cache, blob_key, &size); expect_equal_str(blob, result, "disk_cache_get of existing item (pointer)"); @@ -313,10 +291,8 @@ test_put_and_get(void) disk_cache_compute_key(cache, string, sizeof(string), string_key); disk_cache_put(cache, string_key, string, sizeof(string), NULL); - /* disk_cache_put() hands things off to a thread give it some time to - * finish. - */ - wait_until_file_written(cache, string_key); + /* disk_cache_put() hands things off to a thread so wait for it. */ + disk_cache_wait_for_idle(cache); result = disk_cache_get(cache, string_key, &size); expect_equal_str(result, string, "2nd disk_cache_get of existing item (pointer)"); @@ -356,10 +332,8 @@ test_put_and_get(void) free(one_KB); - /* disk_cache_put() hands things off to a thread give it some time to - * finish. - */ - wait_until_file_written(cache, one_KB_key); + /* disk_cache_put() hands things off to a thread so wait for it. */ + disk_cache_wait_for_idle(cache); result = disk_cache_get(cache, one_KB_key, &size); expect_non_null(result, "3rd disk_cache_get of existing item (pointer)"); @@ -398,11 +372,8 @@ test_put_and_get(void) disk_cache_put(cache, blob_key, blob, sizeof(blob), NULL); disk_cache_put(cache, string_key, string, sizeof(string), NULL); - /* disk_cache_put() hands things off to a thread give it some time to - * finish. - */ - wait_until_file_written(cache, blob_key); - wait_until_file_written(cache, string_key); + /* disk_cache_put() hands things off to a thread so wait for it. */ + disk_cache_wait_for_idle(cache); count = 0; if (does_cache_contain(cache, blob_key)) @@ -426,10 +397,8 @@ test_put_and_get(void) free(one_MB); - /* disk_cache_put() hands things off to a thread give it some time to - * finish. - */ - wait_until_file_written(cache, one_MB_key); + /* disk_cache_put() hands things off to a thread so wait for it. */ + disk_cache_wait_for_idle(cache); bool contains_1MB_file = false; count = 0; diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c index d1f14736725..74fc51b6305 100644 --- a/src/util/disk_cache.c +++ b/src/util/disk_cache.c @@ -455,6 +455,12 @@ disk_cache_destroy(struct disk_cache *cache) ralloc_free(cache); } +void +disk_cache_wait_for_idle(struct disk_cache *cache) +{ + util_queue_finish(&cache->cache_queue); +} + /* Return a filename within the cache's directory corresponding to 'key'. The * returned filename is ralloced with 'cache' as the parent context. * diff --git a/src/util/disk_cache.h b/src/util/disk_cache.h index a77bb678b1a..09b316e6e8d 100644 --- a/src/util/disk_cache.h +++ b/src/util/disk_cache.h @@ -174,6 +174,12 @@ disk_cache_create(const char *gpu_name, const char *timestamp, void disk_cache_destroy(struct disk_cache *cache); +/* Wait for all previous disk_cache_put() calls to be processed (used for unit + * testing). + */ +void +disk_cache_wait_for_idle(struct disk_cache *cache); + /** * Remove the item in the cache under the name \key. */ -- 2.30.2