From 66d3b4a8b256201c08f96e60021921e06e172d41 Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Thu, 27 Jul 2023 14:06:45 -0700 Subject: [PATCH] hasvk/tests: Refactor state_pool_test_helper to not use macros for parametrization Acked-by: Lionel Landwerlin Part-of: --- src/intel/vulkan_hasvk/tests/state_pool.c | 13 +++--- .../vulkan_hasvk/tests/state_pool_free_list_only.c | 12 +++-- .../vulkan_hasvk/tests/state_pool_test_helper.h | 53 +++++++++++++++------- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/intel/vulkan_hasvk/tests/state_pool.c b/src/intel/vulkan_hasvk/tests/state_pool.c index 57cfa73..88440ab 100644 --- a/src/intel/vulkan_hasvk/tests/state_pool.c +++ b/src/intel/vulkan_hasvk/tests/state_pool.c @@ -26,15 +26,13 @@ #include "anv_private.h" #include "test_common.h" -#define NUM_THREADS 8 -#define STATES_PER_THREAD_LOG2 10 -#define STATES_PER_THREAD (1 << STATES_PER_THREAD_LOG2) -#define NUM_RUNS 64 - #include "state_pool_test_helper.h" int main(void) { + const unsigned num_threads = 8; + const unsigned states_per_thread = 1 << 10; + struct anv_physical_device physical_device = { }; struct anv_device device = {}; struct anv_state_pool state_pool; @@ -43,13 +41,14 @@ int main(void) pthread_mutex_init(&device.mutex, NULL); anv_bo_cache_init(&device.bo_cache, &device); - for (unsigned i = 0; i < NUM_RUNS; i++) { + const unsigned num_runs = 64; + for (unsigned i = 0; i < num_runs; i++) { anv_state_pool_init(&state_pool, &device, "test", 4096, 0, 256); /* Grab one so a zero offset is impossible */ anv_state_pool_alloc(&state_pool, 16, 16); - run_state_pool_test(&state_pool); + run_state_pool_test(&state_pool, num_threads, states_per_thread); anv_state_pool_finish(&state_pool); } diff --git a/src/intel/vulkan_hasvk/tests/state_pool_free_list_only.c b/src/intel/vulkan_hasvk/tests/state_pool_free_list_only.c index 602346f..8c01502 100644 --- a/src/intel/vulkan_hasvk/tests/state_pool_free_list_only.c +++ b/src/intel/vulkan_hasvk/tests/state_pool_free_list_only.c @@ -26,7 +26,6 @@ #include "anv_private.h" #include "test_common.h" -#define NUM_THREADS 8 #define STATES_PER_THREAD_LOG2 12 #define STATES_PER_THREAD (1 << STATES_PER_THREAD_LOG2) @@ -34,6 +33,9 @@ int main(void) { + const unsigned num_threads = 8; + const unsigned states_per_thread = 1 << 12; + struct anv_physical_device physical_device = { }; struct anv_device device = {}; struct anv_state_pool state_pool; @@ -50,17 +52,17 @@ int main(void) * actually ever resize anything. */ { - struct anv_state states[NUM_THREADS * STATES_PER_THREAD]; - for (unsigned i = 0; i < NUM_THREADS * STATES_PER_THREAD; i++) { + struct anv_state states[num_threads * states_per_thread]; + for (unsigned i = 0; i < ARRAY_SIZE(states); i++) { states[i] = anv_state_pool_alloc(&state_pool, 16, 16); ASSERT(states[i].offset != 0); } - for (unsigned i = 0; i < NUM_THREADS * STATES_PER_THREAD; i++) + for (unsigned i = 0; i < ARRAY_SIZE(states); i++) anv_state_pool_free(&state_pool, states[i]); } - run_state_pool_test(&state_pool); + run_state_pool_test(&state_pool, num_threads, states_per_thread); anv_state_pool_finish(&state_pool); anv_bo_cache_finish(&device.bo_cache); diff --git a/src/intel/vulkan_hasvk/tests/state_pool_test_helper.h b/src/intel/vulkan_hasvk/tests/state_pool_test_helper.h index f22a28e..de6a363 100644 --- a/src/intel/vulkan_hasvk/tests/state_pool_test_helper.h +++ b/src/intel/vulkan_hasvk/tests/state_pool_test_helper.h @@ -23,49 +23,70 @@ #include +#include "util/u_math.h" + struct job { - struct anv_state_pool *pool; + struct state_pool_test_context *ctx; unsigned id; pthread_t thread; -} jobs[NUM_THREADS]; +}; + +struct state_pool_test_context { + struct anv_state_pool *pool; + unsigned states_per_thread; + pthread_barrier_t barrier; -pthread_barrier_t barrier; + struct job *jobs; +}; static void *alloc_states(void *void_job) { struct job *job = void_job; + struct state_pool_test_context *ctx = job->ctx; - const unsigned chunk_size = 1 << (job->id % STATES_PER_THREAD_LOG2); - const unsigned num_chunks = STATES_PER_THREAD / chunk_size; + const unsigned states_per_thread_log2 = util_logbase2(ctx->states_per_thread); + const unsigned chunk_size = 1 << (job->id % states_per_thread_log2); + const unsigned num_chunks = ctx->states_per_thread / chunk_size; struct anv_state states[chunk_size]; - pthread_barrier_wait(&barrier); + pthread_barrier_wait(&ctx->barrier); for (unsigned c = 0; c < num_chunks; c++) { for (unsigned i = 0; i < chunk_size; i++) { - states[i] = anv_state_pool_alloc(job->pool, 16, 16); + states[i] = anv_state_pool_alloc(ctx->pool, 16, 16); memset(states[i].map, 139, 16); ASSERT(states[i].offset != 0); } for (unsigned i = 0; i < chunk_size; i++) - anv_state_pool_free(job->pool, states[i]); + anv_state_pool_free(ctx->pool, states[i]); } return NULL; } -static void run_state_pool_test(struct anv_state_pool *state_pool) +static void run_state_pool_test(struct anv_state_pool *state_pool, unsigned num_threads, + unsigned states_per_thread) { - pthread_barrier_init(&barrier, NULL, NUM_THREADS); + struct state_pool_test_context ctx = { + .pool = state_pool, + .states_per_thread = states_per_thread, + .jobs = calloc(num_threads, sizeof(struct job)), + }; + pthread_barrier_init(&ctx.barrier, NULL, num_threads); + + for (unsigned i = 0; i < num_threads; i++) { + struct job *job = &ctx.jobs[i]; + job->ctx = &ctx; + job->id = i; + pthread_create(&job->thread, NULL, alloc_states, job); + } - for (unsigned i = 0; i < NUM_THREADS; i++) { - jobs[i].pool = state_pool; - jobs[i].id = i; - pthread_create(&jobs[i].thread, NULL, alloc_states, &jobs[i]); + for (unsigned i = 0; i < num_threads; i++) { + struct job *job = &ctx.jobs[i]; + pthread_join(job->thread, NULL); } - for (unsigned i = 0; i < NUM_THREADS; i++) - pthread_join(jobs[i].thread, NULL); + free(ctx.jobs); } -- 2.7.4