hasvk/tests: Refactor state_pool_test_helper to not use macros for parametrization

Acked-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24355>
This commit is contained in:
Caio Oliveira
2023-07-27 14:06:45 -07:00
parent 54b0745b5e
commit 66d3b4a8b2
3 changed files with 50 additions and 28 deletions

View File

@@ -26,15 +26,13 @@
#include "anv_private.h" #include "anv_private.h"
#include "test_common.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" #include "state_pool_test_helper.h"
int main(void) int main(void)
{ {
const unsigned num_threads = 8;
const unsigned states_per_thread = 1 << 10;
struct anv_physical_device physical_device = { }; struct anv_physical_device physical_device = { };
struct anv_device device = {}; struct anv_device device = {};
struct anv_state_pool state_pool; struct anv_state_pool state_pool;
@@ -43,13 +41,14 @@ int main(void)
pthread_mutex_init(&device.mutex, NULL); pthread_mutex_init(&device.mutex, NULL);
anv_bo_cache_init(&device.bo_cache, &device); 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); anv_state_pool_init(&state_pool, &device, "test", 4096, 0, 256);
/* Grab one so a zero offset is impossible */ /* Grab one so a zero offset is impossible */
anv_state_pool_alloc(&state_pool, 16, 16); 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); anv_state_pool_finish(&state_pool);
} }

View File

@@ -26,7 +26,6 @@
#include "anv_private.h" #include "anv_private.h"
#include "test_common.h" #include "test_common.h"
#define NUM_THREADS 8
#define STATES_PER_THREAD_LOG2 12 #define STATES_PER_THREAD_LOG2 12
#define STATES_PER_THREAD (1 << STATES_PER_THREAD_LOG2) #define STATES_PER_THREAD (1 << STATES_PER_THREAD_LOG2)
@@ -34,6 +33,9 @@
int main(void) int main(void)
{ {
const unsigned num_threads = 8;
const unsigned states_per_thread = 1 << 12;
struct anv_physical_device physical_device = { }; struct anv_physical_device physical_device = { };
struct anv_device device = {}; struct anv_device device = {};
struct anv_state_pool state_pool; struct anv_state_pool state_pool;
@@ -50,17 +52,17 @@ int main(void)
* actually ever resize anything. * actually ever resize anything.
*/ */
{ {
struct anv_state states[NUM_THREADS * STATES_PER_THREAD]; struct anv_state states[num_threads * states_per_thread];
for (unsigned i = 0; i < NUM_THREADS * STATES_PER_THREAD; i++) { for (unsigned i = 0; i < ARRAY_SIZE(states); i++) {
states[i] = anv_state_pool_alloc(&state_pool, 16, 16); states[i] = anv_state_pool_alloc(&state_pool, 16, 16);
ASSERT(states[i].offset != 0); 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]); 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_state_pool_finish(&state_pool);
anv_bo_cache_finish(&device.bo_cache); anv_bo_cache_finish(&device.bo_cache);

View File

@@ -23,49 +23,70 @@
#include <pthread.h> #include <pthread.h>
#include "util/u_math.h"
struct job { struct job {
struct anv_state_pool *pool; struct state_pool_test_context *ctx;
unsigned id; unsigned id;
pthread_t thread; pthread_t thread;
} jobs[NUM_THREADS]; };
pthread_barrier_t barrier; struct state_pool_test_context {
struct anv_state_pool *pool;
unsigned states_per_thread;
pthread_barrier_t barrier;
struct job *jobs;
};
static void *alloc_states(void *void_job) static void *alloc_states(void *void_job)
{ {
struct job *job = 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 states_per_thread_log2 = util_logbase2(ctx->states_per_thread);
const unsigned num_chunks = STATES_PER_THREAD / chunk_size; 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]; 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 c = 0; c < num_chunks; c++) {
for (unsigned i = 0; i < chunk_size; i++) { 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); memset(states[i].map, 139, 16);
ASSERT(states[i].offset != 0); ASSERT(states[i].offset != 0);
} }
for (unsigned i = 0; i < chunk_size; i++) 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; 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++) { for (unsigned i = 0; i < num_threads; i++) {
jobs[i].pool = state_pool; struct job *job = &ctx.jobs[i];
jobs[i].id = i; job->ctx = &ctx;
pthread_create(&jobs[i].thread, NULL, alloc_states, &jobs[i]); job->id = i;
pthread_create(&job->thread, NULL, alloc_states, job);
} }
for (unsigned i = 0; i < NUM_THREADS; i++) for (unsigned i = 0; i < num_threads; i++) {
pthread_join(jobs[i].thread, NULL); struct job *job = &ctx.jobs[i];
pthread_join(job->thread, NULL);
}
free(ctx.jobs);
} }