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