glsl/tests: Fix waiting for disk_cache_put() to finish.
authorEric Anholt <eric@anholt.net>
Tue, 10 Mar 2020 21:52:42 +0000 (14:52 -0700)
committerMarge Bot <eric+marge@anholt.net>
Thu, 12 Mar 2020 19:47:23 +0000 (19:47 +0000)
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 <tarceri@itsqueeze.com>
Tested-by: Marge Bot <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4140>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4140>

src/compiler/glsl/tests/cache_test.c
src/util/disk_cache.c
src/util/disk_cache.h

index cf654e5b01db29c20828702c79987d8e8b8f7465..a1db67a5845d8981f48534f5303fbc0adc97c9ab 100644 (file)
@@ -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;
index d1f147367252451f5ef387b1d5f6599f40c480ad..74fc51b63053e2549407170b8e53d608c33bb513 100644 (file)
@@ -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.
  *
index a77bb678b1adde305d4a549c641e335ff84d47af..09b316e6e8d0600e35c224dcea923d7b5d4bf1e3 100644 (file)
@@ -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.
  */