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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gpu/command_buffer/service/scheduler.h"
#include <algorithm>
#include "base/bind.h"
#include "base/task/post_task.h"
#include "base/test/task_environment.h"
#include "base/test/test_simple_task_runner.h"
#include "base/time/time.h"
#include "gpu/command_buffer/service/sync_point_manager.h"
#include "gpu/config/gpu_preferences.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
template <typename T>
void RunFunctor(T functor) {
functor();
}
template <typename T>
base::OnceClosure GetClosure(T functor) {
return base::BindOnce(&RunFunctor<T>, functor);
}
class SchedulerTest : public testing::Test {
public:
SchedulerTest()
: sync_point_manager_(new SyncPointManager),
scheduler_(new Scheduler(sync_point_manager_.get(), GpuPreferences())) {
}
protected:
SyncPointManager* sync_point_manager() const {
return sync_point_manager_.get();
}
Scheduler* scheduler() const { return scheduler_.get(); }
void RunAllPendingTasks() {
SequenceId sequence_id =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
scheduler()->ScheduleTask(Scheduler::Task(
sequence_id, run_loop_.QuitClosure(), std::vector<SyncToken>()));
run_loop_.Run();
scheduler()->DestroySequence(sequence_id);
}
base::test::SingleThreadTaskEnvironment task_environment_;
base::RunLoop run_loop_;
private:
std::unique_ptr<SyncPointManager> sync_point_manager_;
std::unique_ptr<Scheduler> scheduler_;
};
TEST_F(SchedulerTest, ScheduledTasksRunInOrder) {
SequenceId sequence_id =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
static int count = 0;
int ran1 = 0;
scheduler()->ScheduleTask(Scheduler::Task(sequence_id,
GetClosure([&] { ran1 = ++count; }),
std::vector<SyncToken>()));
int ran2 = 0;
scheduler()->ScheduleTask(Scheduler::Task(sequence_id,
GetClosure([&] { ran2 = ++count; }),
std::vector<SyncToken>()));
scheduler()->ScheduleTask(Scheduler::Task(
sequence_id, run_loop_.QuitClosure(), std::vector<SyncToken>()));
run_loop_.Run();
EXPECT_EQ(ran1, 1);
EXPECT_EQ(ran2, 2);
scheduler()->DestroySequence(sequence_id);
}
TEST_F(SchedulerTest, ScheduledTasksRunAfterReporting) {
SequenceId sequence_id =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
bool ran = false;
bool reported = false;
scheduler()->ScheduleTask(
Scheduler::Task(sequence_id, GetClosure([&] {
EXPECT_TRUE(reported);
ran = true;
}),
std::vector<SyncToken>(),
base::BindOnce(
[&](bool& ran, bool& reported, base::TimeTicks t) {
EXPECT_FALSE(ran);
reported = true;
},
std::ref(ran), std::ref(reported))));
scheduler()->ScheduleTask(Scheduler::Task(
sequence_id, run_loop_.QuitClosure(), std::vector<SyncToken>()));
run_loop_.Run();
EXPECT_TRUE(ran);
scheduler()->DestroySequence(sequence_id);
}
TEST_F(SchedulerTest, ContinuedTasksRunFirst) {
SequenceId sequence_id =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
static int count = 0;
int ran1 = 0;
int continued1 = 0;
scheduler()->ScheduleTask(Scheduler::Task(
sequence_id, GetClosure([&] {
scheduler()->ContinueTask(sequence_id,
GetClosure([&] { continued1 = ++count; }));
ran1 = ++count;
}),
std::vector<SyncToken>()));
int ran2 = 0;
scheduler()->ScheduleTask(Scheduler::Task(sequence_id,
GetClosure([&] { ran2 = ++count; }),
std::vector<SyncToken>()));
scheduler()->ScheduleTask(Scheduler::Task(
sequence_id, run_loop_.QuitClosure(), std::vector<SyncToken>()));
run_loop_.Run();
EXPECT_EQ(ran1, 1);
EXPECT_EQ(continued1, 2);
EXPECT_EQ(ran2, 3);
scheduler()->DestroySequence(sequence_id);
}
class SchedulerTaskRunOrderTest : public SchedulerTest {
public:
SchedulerTaskRunOrderTest() = default;
~SchedulerTaskRunOrderTest() override {
while (!sequence_info_.empty()) {
DestroySequence(sequence_info_.begin()->first);
}
}
protected:
void CreateSequence(int sequence_key, SchedulingPriority priority) {
SequenceId sequence_id = scheduler()->CreateSequenceForTesting(priority);
CommandBufferId command_buffer_id =
CommandBufferId::FromUnsafeValue(sequence_key);
scoped_refptr<SyncPointClientState> release_state =
sync_point_manager()->CreateSyncPointClientState(
kNamespaceId, command_buffer_id, sequence_id);
sequence_info_.emplace(std::make_pair(
sequence_key,
SequenceInfo(sequence_id, command_buffer_id, release_state)));
}
void CreateExternalSequence(int sequence_key) {
auto order_data = sync_point_manager()->CreateSyncPointOrderData();
auto command_buffer_id = CommandBufferId::FromUnsafeValue(sequence_key);
auto release_state = sync_point_manager()->CreateSyncPointClientState(
kNamespaceId, command_buffer_id, order_data->sequence_id());
sequence_info_.emplace(std::make_pair(
sequence_key,
SequenceInfo(std::move(order_data), command_buffer_id, release_state)));
}
void DestroySequence(int sequence_key) {
auto info_it = sequence_info_.find(sequence_key);
ASSERT_TRUE(info_it != sequence_info_.end());
info_it->second.release_state->Destroy();
if (info_it->second.order_data)
info_it->second.order_data->Destroy();
else
scheduler()->DestroySequence(info_it->second.sequence_id);
sequence_info_.erase(info_it);
}
void CreateSyncToken(int sequence_key, int release_sync) {
auto info_it = sequence_info_.find(sequence_key);
ASSERT_TRUE(info_it != sequence_info_.end());
uint64_t release = release_sync + 1;
sync_tokens_.emplace(std::make_pair(
release_sync,
SyncToken(kNamespaceId, info_it->second.command_buffer_id, release)));
}
static void RunExternalTask(base::OnceClosure task,
scoped_refptr<SyncPointOrderData> order_data,
uint32_t order_num) {
order_data->BeginProcessingOrderNumber(order_num);
std::move(task).Run();
order_data->FinishProcessingOrderNumber(order_num);
}
base::OnceClosure GetTaskClosure(int sequence_key, int release_sync) {
const int task_id = num_tasks_scheduled_++;
uint64_t release = 0;
if (release_sync >= 0) {
CreateSyncToken(sequence_key, release_sync);
release = release_sync + 1;
}
auto info_it = sequence_info_.find(sequence_key);
DCHECK(info_it != sequence_info_.end());
auto closure = GetClosure([this, task_id, sequence_key, release] {
if (release) {
auto info_it = sequence_info_.find(sequence_key);
ASSERT_TRUE(info_it != sequence_info_.end());
info_it->second.release_state->ReleaseFenceSync(release);
}
this->tasks_executed_.push_back(task_id);
});
// Simulate external sequence, when tasks are run outside of this
// gpu::Scheduler
if (info_it->second.external()) {
auto order_data = info_it->second.order_data;
uint32_t order_num = order_data->GenerateUnprocessedOrderNumber();
return base::BindOnce(RunExternalTask, std::move(closure), order_data,
order_num);
} else {
return closure;
}
}
void ScheduleTask(int sequence_key, int wait_sync, int release_sync) {
auto closure = GetTaskClosure(sequence_key, release_sync);
auto info_it = sequence_info_.find(sequence_key);
ASSERT_TRUE(info_it != sequence_info_.end());
DCHECK(!info_it->second.external());
std::vector<SyncToken> wait;
if (wait_sync >= 0) {
wait.push_back(sync_tokens_[wait_sync]);
}
scheduler()->ScheduleTask(
Scheduler::Task(info_it->second.sequence_id, std::move(closure), wait));
}
const std::vector<int>& tasks_executed() { return tasks_executed_; }
base::SingleThreadTaskRunner* GetTaskRunnerFromSequence(int sequence_key) {
auto info_it = sequence_info_.find(sequence_key);
if (info_it == sequence_info_.end())
return nullptr;
return scheduler()->GetTaskRunnerForTesting(info_it->second.sequence_id);
}
private:
const CommandBufferNamespace kNamespaceId = CommandBufferNamespace::GPU_IO;
int num_tasks_scheduled_ = 0;
struct SequenceInfo {
SequenceInfo(SequenceId sequence_id,
CommandBufferId command_buffer_id,
scoped_refptr<SyncPointClientState> release_state)
: sequence_id(sequence_id),
command_buffer_id(command_buffer_id),
release_state(release_state) {}
SequenceInfo(scoped_refptr<SyncPointOrderData> order_data,
CommandBufferId command_buffer_id,
scoped_refptr<SyncPointClientState> release_state)
: sequence_id(order_data->sequence_id()),
command_buffer_id(command_buffer_id),
order_data(order_data),
release_state(release_state) {}
bool external() const { return !!order_data; }
SequenceId sequence_id;
CommandBufferId command_buffer_id;
// |order_data| is only set for external sequences.
scoped_refptr<SyncPointOrderData> order_data;
scoped_refptr<SyncPointClientState> release_state;
};
std::map<int, const SequenceInfo> sequence_info_;
std::map<int, const SyncToken> sync_tokens_;
std::vector<int> tasks_executed_;
};
TEST_F(SchedulerTaskRunOrderTest, SequencesRunInPriorityOrder) {
CreateSequence(0, SchedulingPriority::kLow);
CreateSequence(1, SchedulingPriority::kNormal);
CreateSequence(2, SchedulingPriority::kHigh);
ScheduleTask(0, -1, -1); // task 0: seq 0, no wait, no release
ScheduleTask(1, -1, -1); // task 1: seq 1, no wait, no release
ScheduleTask(2, -1, -1); // task 2: seq 2, no wait, no release
RunAllPendingTasks();
const int expected_task_order[] = {2, 1, 0};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, SequencesOfSamePriorityRunInOrder) {
CreateSequence(0, SchedulingPriority::kNormal);
CreateSequence(1, SchedulingPriority::kNormal);
CreateSequence(2, SchedulingPriority::kNormal);
CreateSequence(3, SchedulingPriority::kNormal);
ScheduleTask(0, -1, -1); // task 0: seq 0, no wait, no release
ScheduleTask(1, -1, -1); // task 1: seq 1, no wait, no release
ScheduleTask(2, -1, -1); // task 2: seq 2, no wait, no release
ScheduleTask(3, -1, -1); // task 3: seq 2, no wait, no release
RunAllPendingTasks();
const int expected_task_order[] = {0, 1, 2, 3};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, SequenceWaitsForFence) {
CreateSequence(0, SchedulingPriority::kHigh);
CreateSequence(1, SchedulingPriority::kNormal);
ScheduleTask(1, -1, 0); // task 0: seq 1, no wait, release 0
ScheduleTask(0, 0, -1); // task 1: seq 0, wait 0, no release
RunAllPendingTasks();
const int expected_task_order[] = {0, 1};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, SequenceWaitsForFenceExternal) {
CreateSequence(0, SchedulingPriority::kHigh);
CreateExternalSequence(1);
// Create task 0 on seq 1 that will release 0, but don't post it.
auto external_task = GetTaskClosure(1, 0);
ScheduleTask(0, 0, -1); // task 1: seq 0, wait 0, no release
// task runner for all the sequences created here from same thread is same.
// only sequences created on different threads have different task runner.
GetTaskRunnerFromSequence(0)->PostTask(FROM_HERE, std::move(external_task));
RunAllPendingTasks();
const int expected_task_order[] = {0, 1};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, SequenceDoesNotWaitForInvalidFence) {
CreateSequence(0, SchedulingPriority::kNormal);
CreateSequence(1, SchedulingPriority::kNormal);
CreateSyncToken(1, 0); // declare sync_token 0 on seq 1
ScheduleTask(0, 0, -1); // task 0: seq 0, wait 0, no release
ScheduleTask(1, -1, 0); // task 1: seq 1, no wait, release 0
RunAllPendingTasks();
// Task 0 does not wait on unrelease sync token 0.
const int expected_task_order[] = {0, 1};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, ReleaseSequenceIsPrioritized) {
CreateSequence(0, SchedulingPriority::kNormal);
CreateSequence(1, SchedulingPriority::kLow);
CreateSequence(2, SchedulingPriority::kHigh);
ScheduleTask(0, -1, -1); // task 0: seq 0, no wait, no release
ScheduleTask(1, -1, 0); // task 1: seq 1, no wait, release 0
ScheduleTask(2, 0, -1); // task 2: seq 2, wait 0, no release
RunAllPendingTasks();
const int expected_task_order[] = {1, 2, 0};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, ReleaseSequenceHasPriorityOfWaiter) {
CreateSequence(0, SchedulingPriority::kLow);
CreateSequence(1, SchedulingPriority::kNormal);
CreateSequence(2, SchedulingPriority::kHigh);
ScheduleTask(0, -1, 0); // task 0: seq 0, no wait, release 0
ScheduleTask(1, 0, -1); // task 1: seq 1, wait 0, no release
ScheduleTask(2, -1, -1); // task 2: seq 2, no wait, no release
RunAllPendingTasks();
const int expected_task_order[] = {2, 0, 1};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, ReleaseSequenceRevertsToDefaultPriority) {
CreateSequence(0, SchedulingPriority::kNormal);
CreateSequence(1, SchedulingPriority::kLow);
CreateSequence(2, SchedulingPriority::kHigh);
ScheduleTask(0, -1, -1); // task 0: seq 0, no wait, no release
ScheduleTask(1, -1, 0); // task 1: seq 1, no wait, release 0
ScheduleTask(2, 0, -1); // task 2: seq 2, wait 0, no release
DestroySequence(2);
RunAllPendingTasks();
const int expected_task_order[] = {0, 1};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, ReleaseSequenceCircularRelease) {
CreateSequence(0, SchedulingPriority::kLow);
CreateSequence(1, SchedulingPriority::kNormal);
CreateSequence(2, SchedulingPriority::kHigh);
ScheduleTask(0, -1, -1); // task 0: seq 0, no wait, no release
ScheduleTask(1, -1, -1); // task 1: seq 1, no wait, no release
ScheduleTask(2, -1, -1); // task 2: seq 2, no wait, no release
ScheduleTask(0, -1, 0); // task 3: seq 0, no wait, release 0
ScheduleTask(0, -1, -1); // task 4: seq 0, no wait, no release
ScheduleTask(1, 0, 1); // task 5: seq 1, wait 0, release 1
ScheduleTask(1, -1, -1); // task 6: seq 1, no wait, no release
ScheduleTask(2, 1, 2); // task 7: seq 2, wait 1, release 2
ScheduleTask(2, -1, -1); // task 8: seq 2, no wait, no release
ScheduleTask(0, 2, 3); // task 9: seq 0, wait 2, releases 3
ScheduleTask(1, 3, 4); // task 10: seq 1, wait 3, releases 4
ScheduleTask(2, 4, -1); // task 11: seq 2, wait 4, no release
ScheduleTask(0, -1, -1); // task 12: seq 0, no wait, no release
ScheduleTask(1, -1, -1); // task 13: seq 1, no wait, no release
ScheduleTask(2, -1, -1); // task 14: seq 2, no wait, no release
RunAllPendingTasks();
const int expected_task_order[] = {0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 14, 13, 12};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTaskRunOrderTest, WaitOnSelfShouldNotBlockSequence) {
CreateSequence(0, SchedulingPriority::kHigh);
CreateSyncToken(0, 0); // declare sync_token 0 on seq 1
// Dummy order number to avoid the wait_order_num <= processed_order_num + 1
// check in SyncPointOrderData::ValidateReleaseOrderNum.
sync_point_manager()->GenerateOrderNumber();
ScheduleTask(0, 0, -1); // task 0: seq 0, wait 0, no release
RunAllPendingTasks();
const int expected_task_order[] = {0};
EXPECT_THAT(tasks_executed(), testing::ElementsAreArray(expected_task_order));
}
TEST_F(SchedulerTest, ReleaseSequenceShouldYield) {
SequenceId sequence_id1 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
CommandBufferNamespace namespace_id = CommandBufferNamespace::GPU_IO;
CommandBufferId command_buffer_id = CommandBufferId::FromUnsafeValue(1);
scoped_refptr<SyncPointClientState> release_state =
sync_point_manager()->CreateSyncPointClientState(
namespace_id, command_buffer_id, sequence_id1);
uint64_t release = 1;
static int count = 0;
int ran1 = 0;
scheduler()->ScheduleTask(
Scheduler::Task(sequence_id1, GetClosure([&] {
EXPECT_FALSE(scheduler()->ShouldYield(sequence_id1));
release_state->ReleaseFenceSync(release);
EXPECT_TRUE(scheduler()->ShouldYield(sequence_id1));
ran1 = ++count;
}),
std::vector<SyncToken>()));
int ran2 = 0;
SyncToken sync_token(namespace_id, command_buffer_id, release);
SequenceId sequence_id2 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kHigh);
scheduler()->ScheduleTask(Scheduler::Task(
sequence_id2, GetClosure([&] { ran2 = ++count; }), {sync_token}));
RunAllPendingTasks();
EXPECT_EQ(ran1, 1);
EXPECT_EQ(ran2, 2);
EXPECT_TRUE(sync_point_manager()->IsSyncTokenReleased(sync_token));
release_state->Destroy();
scheduler()->DestroySequence(sequence_id1);
scheduler()->DestroySequence(sequence_id2);
}
TEST_F(SchedulerTest, ReentrantEnableSequenceShouldNotDeadlock) {
SequenceId sequence_id1 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kHigh);
CommandBufferNamespace namespace_id = CommandBufferNamespace::GPU_IO;
CommandBufferId command_buffer_id1 = CommandBufferId::FromUnsafeValue(1);
scoped_refptr<SyncPointClientState> release_state1 =
sync_point_manager()->CreateSyncPointClientState(
namespace_id, command_buffer_id1, sequence_id1);
SequenceId sequence_id2 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
CommandBufferId command_buffer_id2 = CommandBufferId::FromUnsafeValue(2);
scoped_refptr<SyncPointClientState> release_state2 =
sync_point_manager()->CreateSyncPointClientState(
namespace_id, command_buffer_id2, sequence_id2);
uint64_t release = 1;
SyncToken sync_token(namespace_id, command_buffer_id2, release);
static int count = 0;
int ran1, ran2 = 0;
// Schedule task on sequence 2 first so that the sync token wait isn't a nop.
// BeginProcessingOrderNumber for this task will run the EnableSequence
// callback. This should not deadlock.
scheduler()->ScheduleTask(Scheduler::Task(sequence_id2,
GetClosure([&] { ran2 = ++count; }),
std::vector<SyncToken>()));
// This will run first because of the higher priority and no scheduling sync
// token dependencies.
scheduler()->ScheduleTask(Scheduler::Task(
sequence_id1, GetClosure([&] {
ran1 = ++count;
release_state1->Wait(
sync_token,
base::BindOnce(&Scheduler::EnableSequence,
base::Unretained(scheduler()), sequence_id1));
scheduler()->DisableSequence(sequence_id1);
}),
std::vector<SyncToken>()));
RunAllPendingTasks();
EXPECT_EQ(ran1, 1);
EXPECT_EQ(ran2, 2);
EXPECT_FALSE(sync_point_manager()->IsSyncTokenReleased(sync_token));
release_state1->Destroy();
release_state2->Destroy();
scheduler()->DestroySequence(sequence_id1);
scheduler()->DestroySequence(sequence_id2);
}
TEST_F(SchedulerTest, ClientWaitIsPrioritized) {
SequenceId sequence_id1 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
SequenceId sequence_id2 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
SequenceId sequence_id3 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kHigh);
CommandBufferId command_buffer_id = CommandBufferId::FromUnsafeValue(1);
static int count = 0;
int ran1 = 0, ran2 = 0, ran3 = 0;
scheduler()->ScheduleTask(Scheduler::Task(sequence_id1,
GetClosure([&] { ran1 = ++count; }),
std::vector<SyncToken>()));
scheduler()->ScheduleTask(Scheduler::Task(sequence_id2,
GetClosure([&] { ran2 = ++count; }),
std::vector<SyncToken>()));
scheduler()->ScheduleTask(Scheduler::Task(sequence_id3,
GetClosure([&] { ran3 = ++count; }),
std::vector<SyncToken>()));
scheduler()->RaisePriorityForClientWait(sequence_id2, command_buffer_id);
RunAllPendingTasks();
EXPECT_EQ(ran2, 1);
EXPECT_EQ(ran3, 2);
EXPECT_EQ(ran1, 3);
ran1 = ran2 = ran3 = 0;
scheduler()->ScheduleTask(Scheduler::Task(sequence_id1,
GetClosure([&] { ran1 = ++count; }),
std::vector<SyncToken>()));
scheduler()->ScheduleTask(Scheduler::Task(sequence_id2,
GetClosure([&] { ran2 = ++count; }),
std::vector<SyncToken>()));
scheduler()->ScheduleTask(Scheduler::Task(sequence_id3,
GetClosure([&] { ran3 = ++count; }),
std::vector<SyncToken>()));
scheduler()->ResetPriorityForClientWait(sequence_id2, command_buffer_id);
// Note that we are not using RunAllPendingTasks() here because more than one
// Run() is not allowed on the same Runloop. Hence creating a new runloop to
// schedule the task.
base::RunLoop run_loop_temp;
SequenceId sequence_id_run_loop =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
scheduler()->ScheduleTask(Scheduler::Task(sequence_id_run_loop,
run_loop_temp.QuitClosure(),
std::vector<SyncToken>()));
run_loop_temp.Run();
EXPECT_EQ(ran3, 4);
EXPECT_EQ(ran1, 5);
EXPECT_EQ(ran2, 6);
scheduler()->DestroySequence(sequence_id1);
scheduler()->DestroySequence(sequence_id2);
scheduler()->DestroySequence(sequence_id3);
scheduler()->DestroySequence(sequence_id_run_loop);
}
TEST_F(SchedulerTest, StreamPriorities) {
SequenceId seq_id1 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
SequenceId seq_id2 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
SequenceId seq_id3 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kHigh);
CommandBufferNamespace namespace_id = CommandBufferNamespace::GPU_IO;
CommandBufferId command_buffer_id1 = CommandBufferId::FromUnsafeValue(1);
CommandBufferId command_buffer_id2 = CommandBufferId::FromUnsafeValue(2);
base::AutoLock auto_lock(scheduler()->lock_);
Scheduler::Sequence* seq1 = scheduler()->GetSequence(seq_id1);
Scheduler::Sequence* seq2 = scheduler()->GetSequence(seq_id2);
Scheduler::Sequence* seq3 = scheduler()->GetSequence(seq_id3);
// Initial default priorities.
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
SyncToken sync_token1(namespace_id, command_buffer_id1, 1);
SyncToken sync_token2(namespace_id, command_buffer_id2, 1);
// Wait priorities propagate.
seq2->AddWaitFence(sync_token1, 1, seq_id1);
EXPECT_EQ(SchedulingPriority::kNormal, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
seq2->AddWaitFence(sync_token1, 1, seq_id1);
EXPECT_EQ(SchedulingPriority::kNormal, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
seq3->AddWaitFence(sync_token2, 2, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
// Release priority propagate.
seq2->RemoveWaitFence(sync_token1, 1, seq_id1);
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
seq2->RemoveWaitFence(sync_token1, 1, seq_id1);
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
seq3->RemoveWaitFence(sync_token2, 2, seq_id2);
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
{
base::AutoUnlock auto_unlock(scheduler()->lock_);
scheduler()->DestroySequence(seq_id1);
scheduler()->DestroySequence(seq_id2);
scheduler()->DestroySequence(seq_id3);
}
}
TEST_F(SchedulerTest, StreamDestroyRemovesPriorities) {
SequenceId seq_id1 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
SequenceId seq_id2 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
SequenceId seq_id3 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kHigh);
CommandBufferNamespace namespace_id = CommandBufferNamespace::GPU_IO;
CommandBufferId command_buffer_id1 = CommandBufferId::FromUnsafeValue(1);
CommandBufferId command_buffer_id2 = CommandBufferId::FromUnsafeValue(2);
base::AutoLock auto_lock(scheduler()->lock_);
Scheduler::Sequence* seq1 = scheduler()->GetSequence(seq_id1);
Scheduler::Sequence* seq2 = scheduler()->GetSequence(seq_id2);
Scheduler::Sequence* seq3 = scheduler()->GetSequence(seq_id3);
SyncToken sync_token1(namespace_id, command_buffer_id1, 1);
SyncToken sync_token2(namespace_id, command_buffer_id2, 1);
// Wait priorities propagate.
seq2->AddWaitFence(sync_token1, 1, seq_id1);
seq2->AddWaitFence(sync_token1, 1, seq_id1);
seq3->AddWaitFence(sync_token2, 2, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
// Deleting waiting sequences removes priorities.
{
base::AutoUnlock auto_unlock(scheduler()->lock_);
scheduler()->DestroySequence(seq_id3);
}
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
{
base::AutoUnlock auto_unlock(scheduler()->lock_);
scheduler()->DestroySequence(seq_id2);
}
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
{
base::AutoUnlock auto_unlock(scheduler()->lock_);
scheduler()->DestroySequence(seq_id1);
}
}
// crbug.com/781585#5: Test RemoveWait/AddWait/RemoveWait sequence.
TEST_F(SchedulerTest, StreamPriorityChangeWhileReleasing) {
SequenceId seq_id1 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
SequenceId seq_id2 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
SequenceId seq_id3 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kHigh);
CommandBufferNamespace namespace_id = CommandBufferNamespace::GPU_IO;
CommandBufferId command_buffer_id1 = CommandBufferId::FromUnsafeValue(1);
CommandBufferId command_buffer_id2 = CommandBufferId::FromUnsafeValue(2);
base::AutoLock auto_lock(scheduler()->lock_);
Scheduler::Sequence* seq1 = scheduler()->GetSequence(seq_id1);
Scheduler::Sequence* seq2 = scheduler()->GetSequence(seq_id2);
Scheduler::Sequence* seq3 = scheduler()->GetSequence(seq_id3);
SyncToken sync_token1(namespace_id, command_buffer_id1, 1);
SyncToken sync_token2(namespace_id, command_buffer_id2, 2);
// Wait on same fence multiple times.
seq2->AddWaitFence(sync_token1, 1, seq_id1);
seq2->AddWaitFence(sync_token1, 1, seq_id1);
EXPECT_EQ(SchedulingPriority::kNormal, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
// All matching wait fences are removed together.
seq2->RemoveWaitFence(sync_token1, 1, seq_id1);
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
// Add wait fence with higher priority. This replicates a possible race.
seq3->AddWaitFence(sync_token2, 2, seq_id2);
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
// This should be a No-op.
seq2->RemoveWaitFence(sync_token1, 1, seq_id1);
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
seq3->RemoveWaitFence(sync_token2, 2, seq_id2);
EXPECT_EQ(SchedulingPriority::kLow, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq3->current_priority());
{
base::AutoUnlock auto_unlock(scheduler()->lock_);
scheduler()->DestroySequence(seq_id1);
scheduler()->DestroySequence(seq_id2);
scheduler()->DestroySequence(seq_id3);
}
}
TEST_F(SchedulerTest, CircularPriorities) {
SequenceId seq_id1 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kHigh);
SequenceId seq_id2 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kLow);
SequenceId seq_id3 =
scheduler()->CreateSequenceForTesting(SchedulingPriority::kNormal);
CommandBufferNamespace namespace_id = CommandBufferNamespace::GPU_IO;
CommandBufferId command_buffer_id2 = CommandBufferId::FromUnsafeValue(2);
CommandBufferId command_buffer_id3 = CommandBufferId::FromUnsafeValue(3);
base::AutoLock auto_lock(scheduler()->lock_);
Scheduler::Sequence* seq1 = scheduler()->GetSequence(seq_id1);
Scheduler::Sequence* seq2 = scheduler()->GetSequence(seq_id2);
Scheduler::Sequence* seq3 = scheduler()->GetSequence(seq_id3);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kLow, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
SyncToken sync_token_seq2_1(namespace_id, command_buffer_id2, 1);
SyncToken sync_token_seq2_2(namespace_id, command_buffer_id2, 2);
SyncToken sync_token_seq2_3(namespace_id, command_buffer_id2, 3);
SyncToken sync_token_seq3_1(namespace_id, command_buffer_id3, 1);
seq3->AddWaitFence(sync_token_seq2_1, 1, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
seq1->AddWaitFence(sync_token_seq2_2, 2, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
seq3->AddWaitFence(sync_token_seq2_3, 3, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
seq2->AddWaitFence(sync_token_seq3_1, 4, seq_id3);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
seq3->RemoveWaitFence(sync_token_seq2_1, 1, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kHigh, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
seq1->RemoveWaitFence(sync_token_seq2_2, 2, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
seq3->RemoveWaitFence(sync_token_seq2_3, 3, seq_id2);
EXPECT_EQ(SchedulingPriority::kHigh, seq1->current_priority());
EXPECT_EQ(SchedulingPriority::kLow, seq2->current_priority());
EXPECT_EQ(SchedulingPriority::kNormal, seq3->current_priority());
{
base::AutoUnlock auto_unlock(scheduler()->lock_);
scheduler()->DestroySequence(seq_id1);
scheduler()->DestroySequence(seq_id2);
scheduler()->DestroySequence(seq_id3);
}
}
} // namespace gpu
|