From 7d2b8bdb94342d7abc4055c09118b1b05b697c86 Mon Sep 17 00:00:00 2001 From: Colin Ian King Date: Fri, 28 Jul 2023 13:19:31 +0100 Subject: [PATCH] stress-ng.h: move mem_cache stressor fields to struct mem_cache Signed-off-by: Colin Ian King --- core-helper.c | 42 ++++++++++++++--------------- stress-cache.c | 72 +++++++++++++++++++++++++------------------------- stress-ng.c | 11 ++++---- stress-ng.h | 12 +++++---- 4 files changed, 70 insertions(+), 67 deletions(-) diff --git a/core-helper.c b/core-helper.c index b34582694..85ee0b0af 100644 --- a/core-helper.c +++ b/core-helper.c @@ -1848,13 +1848,13 @@ int stress_cache_alloc(const char *name) cpu_caches = stress_cpu_cache_get_all_details(); - if (g_shared->mem_cache_size > 0) + if (g_shared->mem_cache.size > 0) goto init_done; if (!cpu_caches) { if (stress_warn_once()) pr_dbg("%s: using defaults, cannot determine cache details\n", name); - g_shared->mem_cache_size = MEM_CACHE_SIZE; + g_shared->mem_cache.size = MEM_CACHE_SIZE; goto init_done; } @@ -1862,49 +1862,49 @@ int stress_cache_alloc(const char *name) if (max_cache_level == 0) { if (stress_warn_once()) pr_dbg("%s: using defaults, cannot determine cache level details\n", name); - g_shared->mem_cache_size = MEM_CACHE_SIZE; + g_shared->mem_cache.size = MEM_CACHE_SIZE; goto init_done; } - if (g_shared->mem_cache_level > max_cache_level) { + if (g_shared->mem_cache.level > max_cache_level) { if (stress_warn_once()) pr_dbg("%s: using cache maximum level L%d\n", name, max_cache_level); - g_shared->mem_cache_level = max_cache_level; + g_shared->mem_cache.level = max_cache_level; } - cache = stress_cpu_cache_get(cpu_caches, g_shared->mem_cache_level); + cache = stress_cpu_cache_get(cpu_caches, g_shared->mem_cache.level); if (!cache) { if (stress_warn_once()) pr_dbg("%s: using built-in defaults as no suitable " "cache found\n", name); - g_shared->mem_cache_size = MEM_CACHE_SIZE; + g_shared->mem_cache.size = MEM_CACHE_SIZE; goto init_done; } - if (g_shared->mem_cache_ways > 0) { + if (g_shared->mem_cache.ways > 0) { uint64_t way_size; - if (g_shared->mem_cache_ways > cache->ways) { + if (g_shared->mem_cache.ways > cache->ways) { if (stress_warn_once()) pr_inf("%s: cache way value too high - " "defaulting to %d (the maximum)\n", name, cache->ways); - g_shared->mem_cache_ways = cache->ways; + g_shared->mem_cache.ways = cache->ways; } way_size = cache->size / cache->ways; /* only fill the specified number of cache ways */ - g_shared->mem_cache_size = way_size * g_shared->mem_cache_ways; + g_shared->mem_cache.size = way_size * g_shared->mem_cache.ways; } else { /* fill the entire cache */ - g_shared->mem_cache_size = cache->size; + g_shared->mem_cache.size = cache->size; } - if (!g_shared->mem_cache_size) { + if (!g_shared->mem_cache.size) { if (stress_warn_once()) pr_dbg("%s: using built-in defaults as " "unable to determine cache size\n", name); - g_shared->mem_cache_size = MEM_CACHE_SIZE; + g_shared->mem_cache.size = MEM_CACHE_SIZE; } (void)memset(cache_info, 0, sizeof(cache_info)); @@ -1924,12 +1924,12 @@ int stress_cache_alloc(const char *name) init_done: stress_free_cpu_caches(cpu_caches); - g_shared->mem_cache = - (uint8_t *)mmap(NULL, g_shared->mem_cache_size, + g_shared->mem_cache.buffer = + (uint8_t *)mmap(NULL, g_shared->mem_cache.size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); - if (g_shared->mem_cache == MAP_FAILED) { - g_shared->mem_cache = NULL; + if (g_shared->mem_cache.buffer == MAP_FAILED) { + g_shared->mem_cache.buffer = NULL; pr_err("%s: failed to mmap shared cache buffer, errno=%d (%s)\n", name, errno, strerror(errno)); return -1; @@ -1948,7 +1948,7 @@ int stress_cache_alloc(const char *name) } if (stress_warn_once()) pr_dbg("%s: shared cache buffer size: %" PRIu64 "K\n", - name, g_shared->mem_cache_size / 1024); + name, g_shared->mem_cache.size / 1024); return 0; } @@ -1959,8 +1959,8 @@ int stress_cache_alloc(const char *name) */ void stress_cache_free(void) { - if (g_shared->mem_cache) - (void)munmap((void *)g_shared->mem_cache, g_shared->mem_cache_size); + if (g_shared->mem_cache.buffer) + (void)munmap((void *)g_shared->mem_cache.buffer, g_shared->mem_cache.size); if (g_shared->cacheline.buffer) (void)munmap((void *)g_shared->cacheline.buffer, g_shared->cacheline.size); } diff --git a/stress-cache.c b/stress-cache.c index c382057e4..db3ac65e7 100644 --- a/stress-cache.c +++ b/stress-cache.c @@ -222,27 +222,27 @@ static const stress_opt_set_func_t opt_set_funcs[] = { * The compiler optimises out the unused cache flush and mfence calls */ #define CACHE_WRITE_MOD(flags) \ - for (j = 0; LIKELY(j < mem_cache_size); j++) { \ + for (j = 0; LIKELY(j < buffer_size); j++) { \ i += inc; \ - i = (i >= mem_cache_size) ? i - mem_cache_size : i; \ + i = (i >= buffer_size) ? i - buffer_size : i; \ k += 33; \ - k = (k >= mem_cache_size) ? k - mem_cache_size : k; \ + k = (k >= buffer_size) ? k - buffer_size : k; \ \ if ((flags) & CACHE_FLAGS_PREFETCH) { \ - shim_builtin_prefetch(&mem_cache[i + 1], 1, 1); \ + shim_builtin_prefetch(&buffer[i + 1], 1, 1); \ } \ if ((flags) & CACHE_FLAGS_CLDEMOTE) { \ - SHIM_CLDEMOTE(&mem_cache[i]); \ + SHIM_CLDEMOTE(&buffer[i]); \ } \ if ((flags) & CACHE_FLAGS_CLFLUSHOPT) { \ - SHIM_CLFLUSHOPT(&mem_cache[i]); \ + SHIM_CLFLUSHOPT(&buffer[i]); \ } \ - mem_cache[i] += mem_cache[k] + r; \ + buffer[i] += buffer[k] + r; \ if ((flags) & CACHE_FLAGS_CLWB) { \ - SHIM_CLWB(&mem_cache[i]); \ + SHIM_CLWB(&buffer[i]); \ } \ if ((flags) & CACHE_FLAGS_CLFLUSH) { \ - SHIM_CLFLUSH(&mem_cache[i]); \ + SHIM_CLFLUSH(&buffer[i]); \ } \ if ((flags) & CACHE_FLAGS_FENCE) { \ shim_mfence(); \ @@ -264,14 +264,14 @@ static void OPTIMIZE3 stress_cache_write_mod_ ## x( \ stress_metrics_t *metrics) \ { \ register uint64_t i = *pi, j, k = *pk; \ - uint8_t *const mem_cache = g_shared->mem_cache; \ - const uint64_t mem_cache_size = g_shared->mem_cache_size; \ + uint8_t *const buffer = g_shared->mem_cache.buffer; \ + const uint64_t buffer_size = g_shared->mem_cache.size; \ double t; \ \ t = stress_time_now(); \ CACHE_WRITE_MOD(x); \ metrics->duration += stress_time_now() - t; \ - metrics->count += (double)mem_cache_size; \ + metrics->count += (double)buffer_size; \ stress_bogo_add(args, j >> 10); \ \ *pi = i; \ @@ -614,8 +614,8 @@ static void stress_cache_flush(void *addr, void *bad_addr, int size) static void stress_cache_read( const stress_args_t *args, - const uint8_t *mem_cache, - const uint64_t mem_cache_size, + const uint8_t *buffer, + const uint64_t buffer_size, const uint64_t inc, uint64_t *i_ptr, uint64_t *k_ptr, @@ -628,12 +628,12 @@ static void stress_cache_read( double t; t = stress_time_now(); - for (j = 0; j < mem_cache_size; j++) { + for (j = 0; j < buffer_size; j++) { i += inc; - i = (i >= mem_cache_size) ? i - mem_cache_size : i; + i = (i >= buffer_size) ? i - buffer_size : i; k += 33; - k = (k >= mem_cache_size) ? k - mem_cache_size : k; - total += mem_cache[i] + mem_cache[k]; + k = (k >= buffer_size) ? k - buffer_size : k; + total += buffer[i] + buffer[k]; if (!stress_continue_flag()) break; } @@ -649,8 +649,8 @@ static void stress_cache_read( static void stress_cache_write( const stress_args_t *args, - uint8_t *mem_cache, - const uint64_t mem_cache_size, + uint8_t *buffer, + const uint64_t buffer_size, const uint64_t inc, uint64_t *i_ptr, uint64_t *k_ptr, @@ -663,14 +663,14 @@ static void stress_cache_write( double t; t = stress_time_now(); - for (j = 0; j < mem_cache_size; j++) { + for (j = 0; j < buffer_size; j++) { register uint8_t v = j & 0xff; i += inc; - i = (i >= mem_cache_size) ? i - mem_cache_size : i; + i = (i >= buffer_size) ? i - buffer_size : i; k += 33; - k = (k >= mem_cache_size) ? k - mem_cache_size : k; - mem_cache[i] = v; - mem_cache[k] = v; + k = (k >= buffer_size) ? k - buffer_size : k; + buffer[i] = v; + buffer[k] = v; if (!stress_continue_flag()) break; } @@ -718,12 +718,12 @@ static int stress_cache(const stress_args_t *args) NOCLOBBER uint32_t cache_flags_mask = CACHE_FLAGS_MASK; NOCLOBBER uint32_t total = 0; int ret = EXIT_SUCCESS; - uint8_t *const mem_cache = g_shared->mem_cache; - const uint64_t mem_cache_size = g_shared->mem_cache_size; - uint64_t i = stress_mwc64modn(mem_cache_size); - uint64_t k = i + (mem_cache_size >> 1); + uint8_t *const buffer = g_shared->mem_cache.buffer; + const uint64_t buffer_size = g_shared->mem_cache.size; + uint64_t i = stress_mwc64modn(buffer_size); + uint64_t k = i + (buffer_size >> 1); NOCLOBBER uint64_t r = 0; - uint64_t inc = (mem_cache_size >> 2) + 1; + uint64_t inc = (buffer_size >> 2) + 1; void *bad_addr; size_t j; stress_metrics_t metrics[STRESS_CACHE_MAX]; @@ -759,7 +759,7 @@ static int stress_cache(const stress_args_t *args) (void)stress_get_setting("cache-flags", &cache_flags); if (args->instance == 0) pr_dbg("%s: using cache buffer size of %" PRIu64 "K\n", - args->name, mem_cache_size / 1024); + args->name, buffer_size / 1024); #if defined(HAVE_SCHED_GETAFFINITY) && \ defined(HAVE_SCHED_GETCPU) @@ -902,10 +902,10 @@ static int stress_cache(const stress_args_t *args) cache_mixed_ops_funcs[flags](args, inc, r, &i, &k, &metrics[STRESS_CACHE_MIXED_OPS]); break; case STRESS_CACHE_READ: - stress_cache_read(args, mem_cache, mem_cache_size, inc, &i, &k, &metrics[STRESS_CACHE_READ]); + stress_cache_read(args, buffer, buffer_size, inc, &i, &k, &metrics[STRESS_CACHE_READ]); break; case STRESS_CACHE_WRITE: - stress_cache_write(args, mem_cache, mem_cache_size, inc, &i, &k, &metrics[STRESS_CACHE_WRITE]); + stress_cache_write(args, buffer, buffer_size, inc, &i, &k, &metrics[STRESS_CACHE_WRITE]); break; } r++; @@ -947,7 +947,7 @@ static int stress_cache(const stress_args_t *args) UNEXPECTED #endif (void)shim_cacheflush((char *)stress_cache, 8192, SHIM_ICACHE); - (void)shim_cacheflush((char *)mem_cache, (int)mem_cache_size, SHIM_DCACHE); + (void)shim_cacheflush((char *)buffer, (int)buffer_size, SHIM_DCACHE); #if defined(HAVE_BUILTIN___CLEAR_CACHE) __builtin___clear_cache((void *)stress_cache, (void *)((char *)stress_cache + 64)); @@ -965,12 +965,12 @@ static int stress_cache(const stress_args_t *args) break; if (!jmpret) - stress_cache_flush(mem_cache, bad_addr, (int)args->page_size); + stress_cache_flush(buffer, bad_addr, (int)args->page_size); } next: /* Move forward a bit */ i += inc; - i = (i >= mem_cache_size) ? i - mem_cache_size : i; + i = (i >= buffer_size) ? i - buffer_size : i; } while (stress_continue(args)); diff --git a/stress-ng.c b/stress-ng.c index 95147891a..0604d598e 100644 --- a/stress-ng.c +++ b/stress-ng.c @@ -4573,11 +4573,12 @@ int main(int argc, char **argv, char **envp) /* * Allocate shared cache memory */ - g_shared->mem_cache_level = DEFAULT_CACHE_LEVEL; - (void)stress_get_setting("cache-size", &g_shared->mem_cache_size); - (void)stress_get_setting("cache-level", &g_shared->mem_cache_level); - g_shared->mem_cache_ways = 0; - (void)stress_get_setting("cache-ways", &g_shared->mem_cache_ways); + g_shared->mem_cache.size = 0; + (void)stress_get_setting("cache-size", &g_shared->mem_cache.size); + g_shared->mem_cache.level = DEFAULT_CACHE_LEVEL; + (void)stress_get_setting("cache-level", &g_shared->mem_cache.level); + g_shared->mem_cache.ways = 0; + (void)stress_get_setting("cache-ways", &g_shared->mem_cache.ways); if (stress_cache_alloc("cache allocate") < 0) { ret = EXIT_FAILURE; goto exit_shared_unmap; diff --git a/stress-ng.h b/stress-ng.h index e921c2408..8999e683f 100644 --- a/stress-ng.h +++ b/stress-ng.h @@ -947,11 +947,13 @@ typedef struct { uint32_t stressors_failed; /* Number of stressors failed */ uint32_t stressors_alarmed; /* Number of stressors got SIGALRM */ double time_started; /* Time when stressing started */ - uint8_t *mem_cache; /* Shared memory cache */ - uint64_t mem_cache_size; /* Bytes */ - uint16_t mem_cache_level; /* 1=L1, 2=L2, 3=L3 */ - uint16_t padding1; /* alignment padding */ - uint32_t mem_cache_ways; /* cache ways size */ + struct { + uint8_t *buffer; /* Shared memory cache buffer */ + uint64_t size; /* buffer size in bytes */ + uint16_t level; /* 1=L1, 2=L2, 3=L3 */ + uint16_t padding1; /* alignment padding */ + uint32_t ways; /* cache ways size */ + } mem_cache; const uint64_t zero; /* zero'd 64 bit data */ void *nullptr; /* Null pointer */ #if defined(HAVE_ATOMIC_COMPARE_EXCHANGE) && \