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
26 #include "anv_private.h"
28 #define NUM_THREADS 16
29 #define BLOCKS_PER_THREAD 1024
35 struct anv_block_pool
*pool
;
36 int32_t blocks
[BLOCKS_PER_THREAD
];
37 int32_t back_blocks
[BLOCKS_PER_THREAD
];
41 static void *alloc_blocks(void *_job
)
43 struct job
*job
= _job
;
44 uint32_t job_id
= job
- jobs
;
45 uint32_t block_size
= 16 * ((job_id
% 4) + 1);
48 for (unsigned i
= 0; i
< BLOCKS_PER_THREAD
; i
++) {
49 block
= anv_block_pool_alloc(job
->pool
, block_size
, NULL
);
50 data
= anv_block_pool_map(job
->pool
, block
);
53 job
->blocks
[i
] = block
;
55 block
= anv_block_pool_alloc_back(job
->pool
, block_size
);
56 data
= anv_block_pool_map(job
->pool
, block
);
59 job
->back_blocks
[i
] = -block
;
62 for (unsigned i
= 0; i
< BLOCKS_PER_THREAD
; i
++) {
63 block
= job
->blocks
[i
];
64 data
= anv_block_pool_map(job
->pool
, block
);
65 assert(*data
== block
);
67 block
= -job
->back_blocks
[i
];
68 data
= anv_block_pool_map(job
->pool
, block
);
69 assert(*data
== block
);
75 static void validate_monotonic(int32_t **blocks
)
77 /* A list of indices, one per thread */
78 unsigned next
[NUM_THREADS
];
79 memset(next
, 0, sizeof(next
));
83 /* First, we find which thread has the lowest next element */
84 int32_t thread_min
= INT32_MAX
;
85 int min_thread_idx
= -1;
86 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
87 if (next
[i
] >= BLOCKS_PER_THREAD
)
90 if (thread_min
> blocks
[i
][next
[i
]]) {
91 thread_min
= blocks
[i
][next
[i
]];
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.
99 if (thread_min
== INT32_MAX
)
102 /* That next element had better be higher than the previous highest */
103 assert(blocks
[min_thread_idx
][next
[min_thread_idx
]] > highest
);
105 highest
= blocks
[min_thread_idx
][next
[min_thread_idx
]];
106 next
[min_thread_idx
]++;
110 static void run_test()
112 struct anv_instance instance
;
113 struct anv_device device
= {
114 .instance
= &instance
,
116 struct anv_block_pool pool
;
118 pthread_mutex_init(&device
.mutex
, NULL
);
119 anv_block_pool_init(&pool
, &device
, 4096, 4096, 0);
121 for (unsigned i
= 0; i
< NUM_THREADS
; i
++) {
122 jobs
[i
].pool
= &pool
;
124 pthread_create(&jobs
[i
].thread
, NULL
, alloc_blocks
, &jobs
[i
]);
127 for (unsigned i
= 0; i
< NUM_THREADS
; i
++)
128 pthread_join(jobs
[i
].thread
, NULL
);
130 /* Validate that the block allocations were monotonic */
131 int32_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
);
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
);
141 anv_block_pool_finish(&pool
);
142 pthread_mutex_destroy(&device
.mutex
);
145 int main(int argc
, char **argv
)
147 for (unsigned i
= 0; i
< NUM_RUNS
; i
++)