gallium/util: start with a random L3 cache index for AMD Zen
authorMarek Olšák <marek.olsak@amd.com>
Fri, 7 Sep 2018 19:37:45 +0000 (15:37 -0400)
committerMarek Olšák <marek.olsak@amd.com>
Sat, 15 Sep 2018 01:05:37 +0000 (21:05 -0400)
src/gallium/auxiliary/util/u_helpers.c

index 8095242efd958ed06339800708c964a3513c61db..14367e5a1189d0602ad4257e7bcea0642fc8dc6d 100644 (file)
@@ -30,6 +30,7 @@
 #include "util/u_inlines.h"
 #include "util/u_upload_mgr.h"
 #include "util/u_thread.h"
+#include "util/os_time.h"
 #include <inttypes.h>
 
 /**
@@ -120,6 +121,17 @@ util_upload_index_buffer(struct pipe_context *pipe,
    return *out_buffer != NULL;
 }
 
+static unsigned L3_cache_number;
+static once_flag init_cache_number_flag = ONCE_FLAG_INIT;
+
+static void
+util_init_cache_number(void)
+{
+   /* Get a semi-random number. */
+   int64_t t = os_time_get_nano();
+   L3_cache_number = (t ^ (t >> 8) ^ (t >> 16));
+}
+
 /**
  * Called by MakeCurrent. Used to notify the driver that the application
  * thread may have been changed.
@@ -141,12 +153,12 @@ util_context_thread_changed(struct pipe_context *ctx, thrd_t *upper_thread)
 
    /* If the main thread is not pinned, choose the L3 cache. */
    if (cache == -1) {
-      unsigned num_caches = util_cpu_caps.nr_cpus /
-                            util_cpu_caps.cores_per_L3;
-      static unsigned last_cache;
+      unsigned num_L3_caches = util_cpu_caps.nr_cpus /
+                               util_cpu_caps.cores_per_L3;
 
       /* Choose a different L3 cache for each subsequent MakeCurrent. */
-      cache = p_atomic_inc_return(&last_cache) % num_caches;
+      call_once(&init_cache_number_flag, util_init_cache_number);
+      cache = p_atomic_inc_return(&L3_cache_number) % num_L3_caches;
       util_pin_thread_to_L3(current, cache, util_cpu_caps.cores_per_L3);
    }