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