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