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