Skip to content

Commit

Permalink
stress-ng.h: move mem_cache stressor fields to struct mem_cache
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Ian King <[email protected]>
  • Loading branch information
ColinIanKing committed Jul 28, 2023
1 parent 568e6d2 commit 7d2b8bd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 67 deletions.
42 changes: 21 additions & 21 deletions core-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1848,63 +1848,63 @@ 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;
}

max_cache_level = stress_cpu_cache_get_max_level(cpu_caches);
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));
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand Down
72 changes: 36 additions & 36 deletions stress-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); \
Expand All @@ -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; \
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand All @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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));
Expand All @@ -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));

Expand Down
11 changes: 6 additions & 5 deletions stress-ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions stress-ng.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) && \
Expand Down

0 comments on commit 7d2b8bd

Please sign in to comment.