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