1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
gpu / command_buffer / client / query_tracker_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
// Tests for the QueryTracker.
#include "gpu/command_buffer/client/query_tracker.h"
#include <GLES2/gl2ext.h>
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <vector>
#include "gpu/command_buffer/client/client_test_helper.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/mapped_memory.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::AnyNumber;
namespace gpu {
namespace gles2 {
class QuerySyncManagerTest : public testing::Test {
protected:
static const int32_t kNumCommandEntries = 400;
static const int32_t kCommandBufferSizeBytes =
kNumCommandEntries * sizeof(CommandBufferEntry);
void SetUp() override {
command_buffer_ = std::make_unique<MockClientCommandBuffer>();
helper_ = std::make_unique<GLES2CmdHelper>(command_buffer_.get());
helper_->Initialize(kCommandBufferSizeBytes);
mapped_memory_ = std::make_unique<MappedMemoryManager>(
helper_.get(), MappedMemoryManager::kNoLimit);
sync_manager_ = std::make_unique<QuerySyncManager>(mapped_memory_.get());
}
void TearDown() override {
EXPECT_CALL(*command_buffer_, DestroyTransferBuffer(_)).Times(AnyNumber());
sync_manager_.reset();
mapped_memory_.reset();
helper_.reset();
command_buffer_.reset();
}
std::unique_ptr<MockClientCommandBuffer> command_buffer_;
std::unique_ptr<GLES2CmdHelper> helper_;
std::unique_ptr<MappedMemoryManager> mapped_memory_;
std::unique_ptr<QuerySyncManager> sync_manager_;
};
TEST_F(QuerySyncManagerTest, Basic) {
QuerySyncManager::QueryInfo infos[4];
memset(&infos, 0xBD, sizeof(infos));
for (size_t ii = 0; ii < std::size(infos); ++ii) {
EXPECT_TRUE(sync_manager_->Alloc(&infos[ii]));
ASSERT_TRUE(infos[ii].sync != nullptr);
EXPECT_EQ(0, base::subtle::Atomic32{infos[ii].sync->process_count});
EXPECT_EQ(0u, uint64_t{infos[ii].sync->result});
EXPECT_EQ(0, infos[ii].submit_count);
}
for (size_t ii = 0; ii < std::size(infos); ++ii) {
sync_manager_->Free(infos[ii]);
}
}
TEST_F(QuerySyncManagerTest, DontFree) {
QuerySyncManager::QueryInfo infos[4];
memset(&infos, 0xBD, sizeof(infos));
for (size_t ii = 0; ii < std::size(infos); ++ii) {
EXPECT_TRUE(sync_manager_->Alloc(&infos[ii]));
}
}
TEST_F(QuerySyncManagerTest, FreePendingSyncs) {
QuerySyncManager::QueryInfo info;
EXPECT_TRUE(sync_manager_->Alloc(&info));
QuerySyncManager::Bucket* bucket = info.bucket;
// Mark the query as in-use.
++info.submit_count;
// Freeing the QueryInfo should keep the QuerySync busy as it's still in-use,
// but should be tracked in pending_syncs.
sync_manager_->Free(info);
EXPECT_FALSE(bucket->pending_syncs.empty());
EXPECT_TRUE(bucket->in_use_query_syncs.any());
// FreePendingSyncs should not free in-use QuerySync.
bucket->FreePendingSyncs();
EXPECT_FALSE(bucket->pending_syncs.empty());
EXPECT_TRUE(bucket->in_use_query_syncs.any());
// Mark the query as completed.
info.sync->process_count = info.submit_count;
// FreePendingSyncs should free the QuerySync.
bucket->FreePendingSyncs();
EXPECT_TRUE(bucket->pending_syncs.empty());
EXPECT_FALSE(bucket->in_use_query_syncs.any());
// Allocate a new Query, mark it in-use
EXPECT_TRUE(sync_manager_->Alloc(&info));
bucket = info.bucket;
++info.submit_count;
// Mark the query as completed
info.sync->process_count = info.submit_count;
// FreePendingSyncs should not free the QuerySync. Even though the query is
// completed, is has not been deleted yet.
bucket->FreePendingSyncs();
EXPECT_TRUE(bucket->in_use_query_syncs.any());
// Free the QueryInfo, it should be immediately freed.
sync_manager_->Free(info);
EXPECT_TRUE(bucket->pending_syncs.empty());
EXPECT_FALSE(bucket->in_use_query_syncs.any());
}
TEST_F(QuerySyncManagerTest, Shrink) {
QuerySyncManager::QueryInfo info;
EXPECT_TRUE(sync_manager_->Alloc(&info));
QuerySyncManager::Bucket* bucket = info.bucket;
QuerySync* syncs = bucket->syncs;
FencedAllocator::State state =
mapped_memory_->GetPointerStatusForTest(syncs, nullptr);
EXPECT_EQ(FencedAllocator::IN_USE, state);
// Shrink while a query is allocated - should not release anything.
sync_manager_->Shrink(helper_.get());
state = mapped_memory_->GetPointerStatusForTest(syncs, nullptr);
EXPECT_EQ(FencedAllocator::IN_USE, state);
// Free query that was never submitted.
sync_manager_->Free(info);
EXPECT_TRUE(bucket->pending_syncs.empty());
EXPECT_FALSE(bucket->in_use_query_syncs.any());
// Shrink should release the memory immediately.
sync_manager_->Shrink(helper_.get());
EXPECT_TRUE(sync_manager_->buckets_.empty());
state = mapped_memory_->GetPointerStatusForTest(syncs, nullptr);
EXPECT_EQ(FencedAllocator::FREE, state);
EXPECT_TRUE(sync_manager_->Alloc(&info));
bucket = info.bucket;
syncs = bucket->syncs;
state = mapped_memory_->GetPointerStatusForTest(syncs, nullptr);
EXPECT_EQ(FencedAllocator::IN_USE, state);
// Free a query that was submitted, but not completed.
++info.submit_count;
sync_manager_->Free(info);
EXPECT_FALSE(bucket->pending_syncs.empty());
EXPECT_TRUE(bucket->in_use_query_syncs.any());
int32_t last_token = helper_->InsertToken();
// Shrink should release the memory, pending a new token.
sync_manager_->Shrink(helper_.get());
EXPECT_TRUE(sync_manager_->buckets_.empty());
int32_t token = 0;
state = mapped_memory_->GetPointerStatusForTest(syncs, &token);
EXPECT_EQ(FencedAllocator::FREE_PENDING_TOKEN, state);
EXPECT_EQ(last_token + 1, token);
EXPECT_TRUE(sync_manager_->Alloc(&info));
bucket = info.bucket;
syncs = bucket->syncs;
state = mapped_memory_->GetPointerStatusForTest(syncs, nullptr);
EXPECT_EQ(FencedAllocator::IN_USE, state);
// Free a query that was submitted, but not completed yet.
++info.submit_count;
int32_t submit_count = info.submit_count;
QuerySync* sync = info.sync;
sync_manager_->Free(info);
EXPECT_FALSE(bucket->pending_syncs.empty());
EXPECT_TRUE(bucket->in_use_query_syncs.any());
// Complete the query after Free.
sync->process_count = submit_count;
// Shrink should free the memory immediately since the query is completed.
sync_manager_->Shrink(helper_.get());
EXPECT_TRUE(sync_manager_->buckets_.empty());
state = mapped_memory_->GetPointerStatusForTest(syncs, nullptr);
EXPECT_EQ(FencedAllocator::FREE, state);
}
class QueryTrackerTest : public testing::Test {
protected:
static const int32_t kNumCommandEntries = 400;
static const int32_t kCommandBufferSizeBytes =
kNumCommandEntries * sizeof(CommandBufferEntry);
void SetUp() override {
command_buffer_ = std::make_unique<MockClientCommandBuffer>();
helper_ = std::make_unique<GLES2CmdHelper>(command_buffer_.get());
helper_->Initialize(kCommandBufferSizeBytes);
mapped_memory_ = std::make_unique<MappedMemoryManager>(
helper_.get(), MappedMemoryManager::kNoLimit);
query_tracker_ = std::make_unique<QueryTracker>(mapped_memory_.get());
}
void TearDown() override {
helper_->CommandBufferHelper::Flush();
EXPECT_CALL(*command_buffer_, DestroyTransferBuffer(_)).Times(AnyNumber());
query_tracker_.reset();
mapped_memory_.reset();
helper_.reset();
command_buffer_.reset();
}
QuerySync* GetSync(QueryTracker::Query* query) {
return query->info_.sync;
}
QuerySyncManager::Bucket* GetBucket(QueryTracker::Query* query) {
return query->info_.bucket;
}
uint32_t GetBucketUsedCount(QuerySyncManager::Bucket* bucket) {
return bucket->in_use_query_syncs.count();
}
uint32_t GetFlushGeneration() { return helper_->flush_generation(); }
std::unique_ptr<MockClientCommandBuffer> command_buffer_;
std::unique_ptr<GLES2CmdHelper> helper_;
std::unique_ptr<MappedMemoryManager> mapped_memory_;
std::unique_ptr<QueryTracker> query_tracker_;
};
TEST_F(QueryTrackerTest, Basic) {
const GLuint kId1 = 123;
const GLuint kId2 = 124;
// Check we can create a Query.
QueryTracker::Query* query = query_tracker_->CreateQuery(
kId1, GL_ANY_SAMPLES_PASSED_EXT);
ASSERT_TRUE(query != nullptr);
// Check we can get the same Query.
EXPECT_EQ(query, query_tracker_->GetQuery(kId1));
// Check we get nothing for a non-existent query.
EXPECT_TRUE(query_tracker_->GetQuery(kId2) == nullptr);
// Check we can delete the query.
query_tracker_->RemoveQuery(kId1);
// Check we get nothing for a non-existent query.
EXPECT_TRUE(query_tracker_->GetQuery(kId1) == nullptr);
}
TEST_F(QueryTrackerTest, Query) {
const GLuint kId1 = 123;
const int32_t kToken = 46;
const uint32_t kResult = 456;
// Create a Query.
QueryTracker::Query* query = query_tracker_->CreateQuery(
kId1, GL_ANY_SAMPLES_PASSED_EXT);
ASSERT_TRUE(query != nullptr);
EXPECT_TRUE(query->NeverUsed());
EXPECT_FALSE(query->Pending());
EXPECT_EQ(0, query->token());
EXPECT_EQ(0, query->submit_count());
// Check MarkAsActive.
query->MarkAsActive();
EXPECT_FALSE(query->NeverUsed());
EXPECT_FALSE(query->Pending());
EXPECT_EQ(0, query->token());
EXPECT_EQ(0, query->submit_count());
EXPECT_EQ(1, query->NextSubmitCount());
// Check MarkAsPending.
query->MarkAsPending(kToken, query->NextSubmitCount());
EXPECT_FALSE(query->NeverUsed());
EXPECT_TRUE(query->Pending());
EXPECT_EQ(kToken, query->token());
EXPECT_EQ(1, query->submit_count());
// Flush only once if no more flushes happened between a call to
// EndQuery command and CheckResultsAvailable
// Advance put_ so flush calls in CheckResultsAvailable go through
// and updates flush_generation count
helper_->Noop(1);
// Store FlushGeneration count after EndQuery is called
uint32_t gen1 = GetFlushGeneration();
bool flush_if_pending = false;
EXPECT_FALSE(query->CheckResultsAvailable(helper_.get(), flush_if_pending));
EXPECT_FALSE(query->NeverUsed());
EXPECT_TRUE(query->Pending());
// No flush should happen if |flush_if_pending| is false.
uint32_t gen2 = GetFlushGeneration();
EXPECT_EQ(gen1, gen2);
flush_if_pending = true;
// Check CheckResultsAvailable.
EXPECT_FALSE(query->CheckResultsAvailable(helper_.get(), flush_if_pending));
EXPECT_FALSE(query->NeverUsed());
EXPECT_TRUE(query->Pending());
gen2 = GetFlushGeneration();
EXPECT_NE(gen1, gen2);
// Repeated calls to CheckResultsAvailable should not flush unnecessarily
EXPECT_FALSE(query->CheckResultsAvailable(helper_.get(), flush_if_pending));
gen1 = GetFlushGeneration();
EXPECT_EQ(gen1, gen2);
EXPECT_FALSE(query->CheckResultsAvailable(helper_.get(), flush_if_pending));
gen1 = GetFlushGeneration();
EXPECT_EQ(gen1, gen2);
// Simulate GPU process marking it as available.
QuerySync* sync = GetSync(query);
sync->process_count = query->submit_count();
sync->result = kResult;
// Check CheckResultsAvailable.
EXPECT_TRUE(query->CheckResultsAvailable(helper_.get(), flush_if_pending));
EXPECT_EQ(kResult, query->GetResult());
EXPECT_FALSE(query->NeverUsed());
EXPECT_FALSE(query->Pending());
}
TEST_F(QueryTrackerTest, Remove) {
const GLuint kId1 = 123;
const int32_t kToken = 46;
const uint32_t kResult = 456;
// Create a Query.
QueryTracker::Query* query = query_tracker_->CreateQuery(
kId1, GL_ANY_SAMPLES_PASSED_EXT);
ASSERT_TRUE(query != nullptr);
QuerySyncManager::Bucket* bucket = GetBucket(query);
EXPECT_EQ(1u, GetBucketUsedCount(bucket));
query->MarkAsActive();
int32_t submit_count = query->NextSubmitCount();
query->MarkAsPending(kToken, submit_count);
QuerySync* sync = GetSync(query);
query_tracker_->RemoveQuery(kId1);
// Check we get nothing for a non-existent query.
EXPECT_TRUE(query_tracker_->GetQuery(kId1) == nullptr);
// Check that memory was not freed.
EXPECT_EQ(1u, GetBucketUsedCount(bucket));
EXPECT_EQ(1u, bucket->pending_syncs.size());
// Simulate GPU process marking it as available.
sync->result = kResult;
sync->process_count = submit_count;
// Check FreePendingSyncs.
bucket->FreePendingSyncs();
EXPECT_EQ(0u, GetBucketUsedCount(bucket));
}
TEST_F(QueryTrackerTest, RemoveActive) {
const GLuint kId1 = 123;
// Create a Query.
QueryTracker::Query* query =
query_tracker_->CreateQuery(kId1, GL_ANY_SAMPLES_PASSED_EXT);
ASSERT_TRUE(query != nullptr);
QuerySyncManager::Bucket* bucket = GetBucket(query);
EXPECT_EQ(1u, GetBucketUsedCount(bucket));
query->MarkAsActive();
query_tracker_->RemoveQuery(kId1);
// Check we get nothing for a non-existent query.
EXPECT_TRUE(query_tracker_->GetQuery(kId1) == nullptr);
// Check that memory was freed.
EXPECT_EQ(0u, GetBucketUsedCount(bucket));
EXPECT_EQ(0u, bucket->pending_syncs.size());
}
TEST_F(QueryTrackerTest, ManyQueries) {
const GLuint kId1 = 123;
const int32_t kToken = 46;
const uint32_t kResult = 456;
const uint32_t kTestSize = 4000;
static_assert(kTestSize > QuerySyncManager::kSyncsPerBucket,
"We want to use more than one bucket");
// Create lots of queries.
std::vector<QueryTracker::Query*> queries;
for (size_t i = 0; i < kTestSize; i++) {
QueryTracker::Query* query =
query_tracker_->CreateQuery(kId1 + i, GL_ANY_SAMPLES_PASSED_EXT);
ASSERT_TRUE(query != nullptr);
queries.push_back(query);
QuerySyncManager::Bucket* bucket = GetBucket(query);
EXPECT_LE(1u, GetBucketUsedCount(bucket));
}
QuerySyncManager::Bucket* query_0_bucket = GetBucket(queries[0]);
uint32_t expected_use_count = QuerySyncManager::kSyncsPerBucket;
EXPECT_EQ(expected_use_count, GetBucketUsedCount(query_0_bucket));
while (!queries.empty()) {
QueryTracker::Query* query = queries.back();
queries.pop_back();
GLuint query_id = kId1 + queries.size();
EXPECT_EQ(query_id, query->id());
query->MarkAsActive();
int32_t submit_count = query->NextSubmitCount();
query->MarkAsPending(kToken, submit_count);
QuerySync* sync = GetSync(query);
QuerySyncManager::Bucket* bucket = GetBucket(query);
uint32_t use_count_before_remove = GetBucketUsedCount(bucket);
bucket->FreePendingSyncs();
EXPECT_EQ(use_count_before_remove, GetBucketUsedCount(bucket));
query_tracker_->RemoveQuery(query_id);
// Check we get nothing for a non-existent query.
EXPECT_TRUE(query_tracker_->GetQuery(query_id) == nullptr);
// Check that memory was not freed since it was not completed.
EXPECT_EQ(use_count_before_remove, GetBucketUsedCount(bucket));
// Simulate GPU process marking it as available.
sync->process_count = submit_count;
sync->result = kResult;
// Check FreeCompletedQueries.
bucket->FreePendingSyncs();
EXPECT_EQ(use_count_before_remove - 1, GetBucketUsedCount(bucket));
}
}
} // namespace gles2
} // namespace gpu