a9a11d88d33548741d7f3014276685e0a94d9f58
2 * Copyright © 2015 Intel Corporation
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:
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
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
28 #include "anv_private.h"
30 #define NUM_THREADS 16
31 #define BLOCKS_PER_THREAD 1024
37 struct anv_block_pool
*pool
;
38 int32_t blocks
[BLOCKS_PER_THREAD
];
39 int32_t back_blocks
[BLOCKS_PER_THREAD
];
43 static void *alloc_blocks(void *_job
)
45 struct job
*job
= _job
;
46 uint32_t job_id
= job
- jobs
;
47 uint32_t block_size
= 16 * ((job_id
% 4) + 1);
50 for (unsigned i
= 0; i
< BLOCKS_PER_THREAD
; i
++) {
51 block
= anv_block_pool_alloc(job
->pool
, block_size
, NULL
);
52 data
= anv_block_pool_map(job
->pool
, block
);
55 job
->blocks
[i
] = block
;
57 block
= anv_block_pool_alloc_back(job
->pool
, block_size
);
58 data
= anv_block_pool_map(job
->pool
, block
);
61 job
->back_blocks
[i
] = -block
;
64 for (unsigned i
= 0; i
< BLOCKS_PER_THREAD
; i
++) {
65 block
= job
->blocks
[i
];
66 data
= anv_block_pool_map(job
->pool
, block
);
67 assert(*data
== block
);
69 block
= -job
->back_blocks
[i
];
70 data
= anv_block_pool_map(job
->pool
, block
);
71 assert(*data
== block
);
77 static void validate_monotonic(int32_t **blocks
)
79 /* A list of indices, one per thread */
80 unsigned next
[NUM_THREADS
];
81 memset(next
, 0, sizeof(next
));
85 /* First, we find which thread has the lowest next element */
86 int32_t thread_min
= INT32_MAX
;
87 int min_thread_idx
= -1;
88 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
89 if (next
[i
] >= BLOCKS_PER_THREAD
)
92 if (thread_min
> blocks
[i
][next
[i
]]) {
93 thread_min
= blocks
[i
][next
[i
]];
98 /* The only way this can happen is if all of the next[] values are at
99 * BLOCKS_PER_THREAD, in which case, we're done.
101 if (thread_min
== INT32_MAX
)
104 /* That next element had better be higher than the previous highest */
105 assert(blocks
[min_thread_idx
][next
[min_thread_idx
]] > highest
);
107 highest
= blocks
[min_thread_idx
][next
[min_thread_idx
]];
108 next
[min_thread_idx
]++;
112 static void run_test()
114 struct anv_instance instance
= { };
115 struct anv_device device
= {
116 .instance
= &instance
,
118 struct anv_block_pool pool
;
120 pthread_mutex_init(&device
.mutex
, NULL
);
121 anv_bo_cache_init(&device
.bo_cache
);
122 anv_block_pool_init(&pool
, &device
, 4096, 4096);
124 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
125 jobs
[i
].pool
= &pool
;
127 pthread_create(&jobs
[i
].thread
, NULL
, alloc_blocks
, &jobs
[i
]);
130 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
131 pthread_join(jobs
[i
].thread
, NULL
);
133 /* Validate that the block allocations were monotonic */
134 int32_t *block_ptrs
[NUM_THREADS
];
135 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
136 block_ptrs
[i
] = jobs
[i
].blocks
;
137 validate_monotonic(block_ptrs
);
139 /* Validate that the back block allocations were monotonic */
140 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
141 block_ptrs
[i
] = jobs
[i
].back_blocks
;
142 validate_monotonic(block_ptrs
);
144 anv_block_pool_finish(&pool
);
145 pthread_mutex_destroy(&device
.mutex
);
148 int main(int argc
, char **argv
)
150 for (unsigned i
= 0; i
< NUM_RUNS
; i
++)