• 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 #include "test_common.h"
28 
29 #define NUM_THREADS 16
30 #define STATES_PER_THREAD 1024
31 #define NUM_RUNS 64
32 
33 static struct job {
34    pthread_t thread;
35    unsigned id;
36    struct anv_state_pool *pool;
37    uint32_t offsets[STATES_PER_THREAD];
38 } jobs[NUM_THREADS];
39 
40 static pthread_barrier_t barrier;
41 
alloc_states(void * _job)42 static void *alloc_states(void *_job)
43 {
44    struct job *job = _job;
45 
46    pthread_barrier_wait(&barrier);
47 
48    for (unsigned i = 0; i < STATES_PER_THREAD; i++) {
49       struct anv_state state = anv_state_pool_alloc(job->pool, 16, 16);
50       job->offsets[i] = state.offset;
51    }
52 
53    return NULL;
54 }
55 
run_test()56 static void run_test()
57 {
58    struct anv_physical_device physical_device = { };
59    struct anv_device device = {};
60    struct anv_state_pool state_pool;
61 
62    anv_device_set_physical(&device, &physical_device);
63    pthread_mutex_init(&device.mutex, NULL);
64    anv_bo_cache_init(&device.bo_cache, &device);
65    anv_state_pool_init(&state_pool, &device, "test", 4096, 0, 64);
66 
67    pthread_barrier_init(&barrier, NULL, NUM_THREADS);
68 
69    for (unsigned i = 0; i < NUM_THREADS; i++) {
70       jobs[i].pool = &state_pool;
71       jobs[i].id = i;
72       pthread_create(&jobs[i].thread, NULL, alloc_states, &jobs[i]);
73    }
74 
75    for (unsigned i = 0; i < NUM_THREADS; i++)
76       pthread_join(jobs[i].thread, NULL);
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 highest next element */
85       int thread_max = -1;
86       int max_thread_idx = -1;
87       for (unsigned i = 0; i < NUM_THREADS; i++) {
88          if (next[i] >= STATES_PER_THREAD)
89             continue;
90 
91          if (thread_max < jobs[i].offsets[next[i]]) {
92             thread_max = jobs[i].offsets[next[i]];
93             max_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_max == -1)
101          break;
102 
103       /* That next element had better be higher than the previous highest */
104       ASSERT(jobs[max_thread_idx].offsets[next[max_thread_idx]] > highest);
105 
106       highest = jobs[max_thread_idx].offsets[next[max_thread_idx]];
107       next[max_thread_idx]++;
108    }
109 
110    anv_state_pool_finish(&state_pool);
111    anv_bo_cache_finish(&device.bo_cache);
112    pthread_mutex_destroy(&device.mutex);
113 }
114 
115 void state_pool_no_free_test(void);
116 
state_pool_no_free_test(void)117 void state_pool_no_free_test(void)
118 {
119    for (unsigned i = 0; i < NUM_RUNS; i++)
120       run_test();
121 }
122