1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <pthread.h>
25
26 #include "util/u_math.h"
27
28 struct job {
29 struct state_pool_test_context *ctx;
30 unsigned id;
31 pthread_t thread;
32 };
33
34 struct state_pool_test_context {
35 struct anv_state_pool *pool;
36 unsigned states_per_thread;
37 pthread_barrier_t barrier;
38
39 struct job *jobs;
40 };
41
alloc_states(void * void_job)42 static void *alloc_states(void *void_job)
43 {
44 struct job *job = void_job;
45 struct state_pool_test_context *ctx = job->ctx;
46
47 const unsigned states_per_thread_log2 = util_logbase2(ctx->states_per_thread);
48 const unsigned chunk_size = 1 << (job->id % states_per_thread_log2);
49 const unsigned num_chunks = ctx->states_per_thread / chunk_size;
50
51 struct anv_state states[chunk_size];
52
53 pthread_barrier_wait(&ctx->barrier);
54
55 for (unsigned c = 0; c < num_chunks; c++) {
56 for (unsigned i = 0; i < chunk_size; i++) {
57 states[i] = anv_state_pool_alloc(ctx->pool, 16, 16);
58 memset(states[i].map, 139, 16);
59 ASSERT(states[i].offset != 0);
60 }
61
62 for (unsigned i = 0; i < chunk_size; i++)
63 anv_state_pool_free(ctx->pool, states[i]);
64 }
65
66 return NULL;
67 }
68
run_state_pool_test(struct anv_state_pool * state_pool,unsigned num_threads,unsigned states_per_thread)69 static void run_state_pool_test(struct anv_state_pool *state_pool, unsigned num_threads,
70 unsigned states_per_thread)
71 {
72 struct state_pool_test_context ctx = {
73 .pool = state_pool,
74 .states_per_thread = states_per_thread,
75 .jobs = calloc(num_threads, sizeof(struct job)),
76 };
77 pthread_barrier_init(&ctx.barrier, NULL, num_threads);
78
79 for (unsigned i = 0; i < num_threads; i++) {
80 struct job *job = &ctx.jobs[i];
81 job->ctx = &ctx;
82 job->id = i;
83 pthread_create(&job->thread, NULL, alloc_states, job);
84 }
85
86 for (unsigned i = 0; i < num_threads; i++) {
87 struct job *job = &ctx.jobs[i];
88 pthread_join(job->thread, NULL);
89 }
90
91 free(ctx.jobs);
92 }
93