a9a11d88d33548741d7f3014276685e0a94d9f58
[mesa.git] / src / intel / vulkan / tests / block_pool_no_free.c
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 #undef NDEBUG
25
26 #include <pthread.h>
27
28 #include "anv_private.h"
29
30 #define NUM_THREADS 16
31 #define BLOCKS_PER_THREAD 1024
32 #define NUM_RUNS 64
33
34 struct job {
35 pthread_t thread;
36 unsigned id;
37 struct anv_block_pool *pool;
38 int32_t blocks[BLOCKS_PER_THREAD];
39 int32_t back_blocks[BLOCKS_PER_THREAD];
40 } jobs[NUM_THREADS];
41
42
43 static void *alloc_blocks(void *_job)
44 {
45 struct job *job = _job;
46 uint32_t job_id = job - jobs;
47 uint32_t block_size = 16 * ((job_id % 4) + 1);
48 int32_t block, *data;
49
50 for (unsigned i = 0; i < BLOCKS_PER_THREAD; i++) {
51 block = anv_block_pool_alloc(job->pool, block_size, NULL);
52 data = anv_block_pool_map(job->pool, block);
53 *data = block;
54 assert(block >= 0);
55 job->blocks[i] = block;
56
57 block = anv_block_pool_alloc_back(job->pool, block_size);
58 data = anv_block_pool_map(job->pool, block);
59 *data = block;
60 assert(block < 0);
61 job->back_blocks[i] = -block;
62 }
63
64 for (unsigned i = 0; i < BLOCKS_PER_THREAD; i++) {
65 block = job->blocks[i];
66 data = anv_block_pool_map(job->pool, block);
67 assert(*data == block);
68
69 block = -job->back_blocks[i];
70 data = anv_block_pool_map(job->pool, block);
71 assert(*data == block);
72 }
73
74 return NULL;
75 }
76
77 static void validate_monotonic(int32_t **blocks)
78 {
79 /* A list of indices, one per thread */
80 unsigned next[NUM_THREADS];
81 memset(next, 0, sizeof(next));
82
83 int highest = -1;
84 while (true) {
85 /* First, we find which thread has the lowest next element */
86 int32_t thread_min = INT32_MAX;
87 int min_thread_idx = -1;
88 for (unsigned i = 0; i < NUM_THREADS; i++) {
89 if (next[i] >= BLOCKS_PER_THREAD)
90 continue;
91
92 if (thread_min > blocks[i][next[i]]) {
93 thread_min = blocks[i][next[i]];
94 min_thread_idx = i;
95 }
96 }
97
98 /* The only way this can happen is if all of the next[] values are at
99 * BLOCKS_PER_THREAD, in which case, we're done.
100 */
101 if (thread_min == INT32_MAX)
102 break;
103
104 /* That next element had better be higher than the previous highest */
105 assert(blocks[min_thread_idx][next[min_thread_idx]] > highest);
106
107 highest = blocks[min_thread_idx][next[min_thread_idx]];
108 next[min_thread_idx]++;
109 }
110 }
111
112 static void run_test()
113 {
114 struct anv_instance instance = { };
115 struct anv_device device = {
116 .instance = &instance,
117 };
118 struct anv_block_pool pool;
119
120 pthread_mutex_init(&device.mutex, NULL);
121 anv_bo_cache_init(&device.bo_cache);
122 anv_block_pool_init(&pool, &device, 4096, 4096);
123
124 for (unsigned i = 0; i < NUM_THREADS; i++) {
125 jobs[i].pool = &pool;
126 jobs[i].id = i;
127 pthread_create(&jobs[i].thread, NULL, alloc_blocks, &jobs[i]);
128 }
129
130 for (unsigned i = 0; i < NUM_THREADS; i++)
131 pthread_join(jobs[i].thread, NULL);
132
133 /* Validate that the block allocations were monotonic */
134 int32_t *block_ptrs[NUM_THREADS];
135 for (unsigned i = 0; i < NUM_THREADS; i++)
136 block_ptrs[i] = jobs[i].blocks;
137 validate_monotonic(block_ptrs);
138
139 /* Validate that the back block allocations were monotonic */
140 for (unsigned i = 0; i < NUM_THREADS; i++)
141 block_ptrs[i] = jobs[i].back_blocks;
142 validate_monotonic(block_ptrs);
143
144 anv_block_pool_finish(&pool);
145 pthread_mutex_destroy(&device.mutex);
146 }
147
148 int main(int argc, char **argv)
149 {
150 for (unsigned i = 0; i < NUM_RUNS; i++)
151 run_test();
152 }