-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
local_priority_queue_scheduler.hpp
1185 lines (999 loc) · 44.2 KB
/
local_priority_queue_scheduler.hpp
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2007-2017 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#if !defined(HPX_THREADMANAGER_SCHEDULING_LOCAL_PRIORITY_QUEUE_MAR_15_2011_0926AM)
#define HPX_THREADMANAGER_SCHEDULING_LOCAL_PRIORITY_QUEUE_MAR_15_2011_0926AM
#include <hpx/config.hpp>
#include <hpx/compat/mutex.hpp>
#include <hpx/runtime/threads/policies/lockfree_queue_backends.hpp>
#include <hpx/runtime/threads/policies/scheduler_base.hpp>
#include <hpx/runtime/threads/policies/thread_queue.hpp>
#include <hpx/runtime/threads/thread_data.hpp>
#include <hpx/runtime/threads/topology.hpp>
#include <hpx/runtime/threads_fwd.hpp>
#include <hpx/throw_exception.hpp>
#include <hpx/util/assert.hpp>
#include <hpx/util/logging.hpp>
#include <hpx/util_fwd.hpp>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <memory>
#include <string>
#include <type_traits>
#include <vector>
#include <hpx/config/warnings_prefix.hpp>
// TODO: add branch prediction and function heat
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace threads { namespace policies
{
#ifdef HPX_HAVE_THREAD_MINIMAL_DEADLOCK_DETECTION
///////////////////////////////////////////////////////////////////////////
// We globally control whether to do minimal deadlock detection using this
// global bool variable. It will be set once by the runtime configuration
// startup code
extern bool minimal_deadlock_detection;
#endif
///////////////////////////////////////////////////////////////////////////
/// The local_priority_queue_scheduler maintains exactly one queue of work
/// items (threads) per OS thread, where this OS thread pulls its next work
/// from. Additionally it maintains separate queues: several for high
/// priority threads and one for low priority threads.
/// High priority threads are executed by the first N OS threads before any
/// other work is executed. Low priority threads are executed by the last
/// OS thread whenever no other work is available.
template <typename Mutex = compat::mutex,
typename PendingQueuing = lockfree_fifo,
typename StagedQueuing = lockfree_fifo,
typename TerminatedQueuing = lockfree_lifo>
class HPX_EXPORT local_priority_queue_scheduler : public scheduler_base
{
protected:
// The maximum number of active threads this thread manager should
// create. This number will be a constraint only as long as the work
// items queue is not empty. Otherwise the number of active threads
// will be incremented in steps equal to the \a min_add_new_count
// specified above.
// FIXME: this is specified both here, and in thread_queue.
enum { max_thread_count = 1000 };
public:
typedef std::false_type has_periodic_maintenance;
typedef thread_queue<
Mutex, PendingQueuing, StagedQueuing, TerminatedQueuing
> thread_queue_type;
// the scheduler type takes two initialization parameters:
// the number of queues
// the number of high priority queues
// the maxcount per queue
struct init_parameter
{
init_parameter()
: num_queues_(1),
num_high_priority_queues_(1),
max_queue_thread_count_(max_thread_count),
numa_sensitive_(0),
description_("local_priority_queue_scheduler")
{}
init_parameter(std::size_t num_queues,
std::size_t num_high_priority_queues = std::size_t(-1),
std::size_t max_queue_thread_count = max_thread_count,
std::size_t numa_sensitive = 0,
char const* description = "local_priority_queue_scheduler")
: num_queues_(num_queues),
num_high_priority_queues_(
num_high_priority_queues == std::size_t(-1) ?
num_queues : num_high_priority_queues),
max_queue_thread_count_(max_queue_thread_count),
numa_sensitive_(numa_sensitive),
description_(description)
{}
init_parameter(std::size_t num_queues, char const* description)
: num_queues_(num_queues),
num_high_priority_queues_(num_queues),
max_queue_thread_count_(max_thread_count),
numa_sensitive_(false),
description_(description)
{}
std::size_t num_queues_;
std::size_t num_high_priority_queues_;
std::size_t max_queue_thread_count_;
std::size_t numa_sensitive_;
char const* description_;
};
typedef init_parameter init_parameter_type;
local_priority_queue_scheduler(init_parameter_type const& init,
bool deferred_initialization = true)
: scheduler_base(init.num_queues_, init.description_),
max_queue_thread_count_(init.max_queue_thread_count_),
queues_(init.num_queues_),
high_priority_queues_(init.num_high_priority_queues_),
low_priority_queue_(init.max_queue_thread_count_),
curr_queue_(0),
numa_sensitive_(init.numa_sensitive_),
rp_(resource::get_partitioner())
{
victim_threads_.clear();
victim_threads_.resize(init.num_queues_);
if (!deferred_initialization)
{
#if defined(HPX_MSVC)
#pragma warning(push)
#pragma warning(disable: 4316) // object allocated on the heap may not be aligned 16
#endif
HPX_ASSERT(init.num_queues_ != 0);
for (std::size_t i = 0; i < init.num_queues_; ++i)
queues_[i] = new thread_queue_type(init.max_queue_thread_count_);
HPX_ASSERT(init.num_high_priority_queues_ != 0);
HPX_ASSERT(init.num_high_priority_queues_ <= init.num_queues_);
for (std::size_t i = 0; i < init.num_high_priority_queues_; ++i) {
high_priority_queues_[i] =
new thread_queue_type(init.max_queue_thread_count_);
}
#if defined(HPX_MSVC)
#pragma warning(pop)
#endif
}
}
virtual ~local_priority_queue_scheduler()
{
for (std::size_t i = 0; i != queues_.size(); ++i)
delete queues_[i];
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
delete high_priority_queues_[i];
}
bool numa_sensitive() const { return numa_sensitive_ != 0; }
static std::string get_scheduler_name()
{
return "local_priority_queue_scheduler";
}
#ifdef HPX_HAVE_THREAD_CREATION_AND_CLEANUP_RATES
std::uint64_t get_creation_time(bool reset)
{
std::uint64_t time = 0;
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
time += high_priority_queues_[i]->get_creation_time(reset);
time += low_priority_queue_.get_creation_time(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
time += queues_[i]->get_creation_time(reset);
return time;
}
std::uint64_t get_cleanup_time(bool reset)
{
std::uint64_t time = 0;
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
time += high_priority_queues_[i]->
get_cleanup_time(reset);
time += low_priority_queue_.get_cleanup_time(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
time += queues_[i]->get_cleanup_time(reset);
return time;
}
#endif
#ifdef HPX_HAVE_THREAD_STEALING_COUNTS
std::int64_t get_num_pending_misses(std::size_t num_thread, bool reset)
{
std::int64_t num_pending_misses = 0;
if (num_thread == std::size_t(-1))
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
num_pending_misses += high_priority_queues_[i]->
get_num_pending_misses(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
num_pending_misses += queues_[i]->
get_num_pending_misses(reset);
num_pending_misses += low_priority_queue_.
get_num_pending_misses(reset);
return num_pending_misses;
}
num_pending_misses += queues_[num_thread]->
get_num_pending_misses(reset);
if (num_thread < high_priority_queues_.size())
{
num_pending_misses += high_priority_queues_[num_thread]->
get_num_pending_misses(reset);
}
if (num_thread == 0)
{
num_pending_misses += low_priority_queue_.
get_num_pending_misses(reset);
}
return num_pending_misses;
}
std::int64_t get_num_pending_accesses(std::size_t num_thread, bool reset)
{
std::int64_t num_pending_accesses = 0;
if (num_thread == std::size_t(-1))
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
num_pending_accesses += high_priority_queues_[i]->
get_num_pending_accesses(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
num_pending_accesses += queues_[i]->
get_num_pending_accesses(reset);
num_pending_accesses += low_priority_queue_.
get_num_pending_accesses(reset);
return num_pending_accesses;
}
num_pending_accesses += queues_[num_thread]->
get_num_pending_accesses(reset);
if (num_thread < high_priority_queues_.size())
{
num_pending_accesses += high_priority_queues_[num_thread]->
get_num_pending_accesses(reset);
}
if (num_thread == 0)
{
num_pending_accesses += low_priority_queue_.
get_num_pending_accesses(reset);
}
return num_pending_accesses;
}
std::int64_t get_num_stolen_from_pending(std::size_t num_thread, bool reset)
{
std::int64_t num_stolen_threads = 0;
if (num_thread == std::size_t(-1))
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
num_stolen_threads += high_priority_queues_[i]->
get_num_stolen_from_pending(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
num_stolen_threads += queues_[i]->
get_num_stolen_from_pending(reset);
num_stolen_threads += low_priority_queue_.
get_num_stolen_from_pending(reset);
return num_stolen_threads;
}
num_stolen_threads += queues_[num_thread]->
get_num_stolen_from_pending(reset);
if (num_thread < high_priority_queues_.size())
{
num_stolen_threads += high_priority_queues_[num_thread]->
get_num_stolen_from_pending(reset);
}
if (num_thread == 0)
{
num_stolen_threads += low_priority_queue_.
get_num_stolen_from_pending(reset);
}
return num_stolen_threads;
}
std::int64_t get_num_stolen_to_pending(std::size_t num_thread, bool reset)
{
std::int64_t num_stolen_threads = 0;
if (num_thread == std::size_t(-1))
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
num_stolen_threads += high_priority_queues_[i]->
get_num_stolen_to_pending(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
num_stolen_threads += queues_[i]->
get_num_stolen_to_pending(reset);
num_stolen_threads += low_priority_queue_.
get_num_stolen_to_pending(reset);
return num_stolen_threads;
}
num_stolen_threads += queues_[num_thread]->
get_num_stolen_to_pending(reset);
if (num_thread < high_priority_queues_.size())
{
num_stolen_threads += high_priority_queues_[num_thread]->
get_num_stolen_to_pending(reset);
}
if (num_thread == 0)
{
num_stolen_threads += low_priority_queue_.
get_num_stolen_to_pending(reset);
}
return num_stolen_threads;
}
std::int64_t get_num_stolen_from_staged(std::size_t num_thread, bool reset)
{
std::int64_t num_stolen_threads = 0;
if (num_thread == std::size_t(-1))
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
num_stolen_threads += high_priority_queues_[i]->
get_num_stolen_from_staged(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
num_stolen_threads += queues_[i]->
get_num_stolen_from_staged(reset);
num_stolen_threads += low_priority_queue_.
get_num_stolen_from_staged(reset);
return num_stolen_threads;
}
num_stolen_threads += queues_[num_thread]->
get_num_stolen_from_staged(reset);
if (num_thread < high_priority_queues_.size())
{
num_stolen_threads += high_priority_queues_[num_thread]->
get_num_stolen_from_staged(reset);
}
if (num_thread == 0)
{
num_stolen_threads += low_priority_queue_.
get_num_stolen_from_staged(reset);
}
return num_stolen_threads;
}
std::int64_t get_num_stolen_to_staged(std::size_t num_thread, bool reset)
{
std::int64_t num_stolen_threads = 0;
if (num_thread == std::size_t(-1))
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
num_stolen_threads += high_priority_queues_[i]->
get_num_stolen_to_staged(reset);
for (std::size_t i = 0; i != queues_.size(); ++i)
num_stolen_threads += queues_[i]->
get_num_stolen_to_staged(reset);
num_stolen_threads += low_priority_queue_.
get_num_stolen_to_staged(reset);
return num_stolen_threads;
}
num_stolen_threads += queues_[num_thread]->
get_num_stolen_to_staged(reset);
if (num_thread < high_priority_queues_.size())
{
num_stolen_threads += high_priority_queues_[num_thread]->
get_num_stolen_to_staged(reset);
}
if (num_thread == 0)
{
num_stolen_threads += low_priority_queue_.
get_num_stolen_to_staged(reset);
}
return num_stolen_threads;
}
#endif
///////////////////////////////////////////////////////////////////////
void abort_all_suspended_threads()
{
for (std::size_t i = 0; i != queues_.size(); ++i)
queues_[i]->abort_all_suspended_threads();
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
high_priority_queues_[i]->abort_all_suspended_threads();
low_priority_queue_.abort_all_suspended_threads();
}
///////////////////////////////////////////////////////////////////////
bool cleanup_terminated(bool delete_all = false)
{
bool empty = true;
for (std::size_t i = 0; i != queues_.size(); ++i)
empty = queues_[i]->cleanup_terminated(delete_all) && empty;
if (!delete_all)
return empty;
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
empty = high_priority_queues_[i]->
cleanup_terminated(delete_all) && empty;
empty = low_priority_queue_.cleanup_terminated(delete_all) && empty;
return empty;
}
///////////////////////////////////////////////////////////////////////
// create a new thread and schedule it if the initial state is equal to
// pending
void create_thread(thread_init_data& data, thread_id_type* id,
thread_state_enum initial_state, bool run_now, error_code& ec,
std::size_t num_thread)
{
#ifdef HPX_HAVE_THREAD_TARGET_ADDRESS
// // try to figure out the NUMA node where the data lives
// if (numa_sensitive_ && std::size_t(-1) == num_thread) {
// mask_cref_type mask =
// topology_.get_thread_affinity_mask_from_lva(data.lva);
// if (any(mask)) {
// num_thread = find_first(mask);
// }
// }
#endif
std::size_t queue_size = queues_.size();
if (std::size_t(-1) == num_thread)
num_thread = curr_queue_++ % queue_size;
if (num_thread >= queue_size)
num_thread %= queue_size;
// Select a OS thread which hasn't been disabled
auto mask = rp_.get_pu_mask(
num_thread + parent_pool_->get_thread_offset());
if(!threads::any(mask))
threads::set(mask, num_thread + parent_pool_->get_thread_offset());
while (true)
{
if (bit_and(mask, parent_pool_->get_used_processing_units()))
break;
num_thread = (num_thread + 1) % queue_size;
}
// now create the thread
if (data.priority == thread_priority_high_recursive ||
data.priority == thread_priority_high ||
data.priority == thread_priority_boost)
{
if (data.priority == thread_priority_boost)
{
data.priority = thread_priority_normal;
}
std::size_t num = num_thread % high_priority_queues_.size();
high_priority_queues_[num]->create_thread(data, id,
initial_state, run_now, ec);
return;
}
if (data.priority == thread_priority_low)
{
low_priority_queue_.create_thread(data, id, initial_state,
run_now, ec);
return;
}
HPX_ASSERT(num_thread < queue_size);
queues_[num_thread]->create_thread(data, id, initial_state,
run_now, ec);
}
/// Return the next thread to be executed, return false if none is
/// available
virtual bool get_next_thread(std::size_t num_thread, bool running,
std::int64_t& idle_loop_count, threads::thread_data*& thrd)
{
std::size_t queues_size = queues_.size();
std::size_t high_priority_queues = high_priority_queues_.size();
HPX_ASSERT(num_thread < queues_size);
thread_queue_type* this_high_priority_queue = nullptr;
thread_queue_type* this_queue = queues_[num_thread];
if (num_thread < high_priority_queues)
{
this_high_priority_queue = high_priority_queues_[num_thread];
bool result =
this_high_priority_queue->get_next_thread(thrd);
this_high_priority_queue->increment_num_pending_accesses();
if (result)
return true;
this_high_priority_queue->increment_num_pending_misses();
}
{
bool result = this_queue->get_next_thread(thrd);
this_queue->increment_num_pending_accesses();
if (result)
return true;
this_queue->increment_num_pending_misses();
bool have_staged = this_queue->
get_staged_queue_length(std::memory_order_relaxed) != 0;
// Give up, we should have work to convert.
if (have_staged)
return false;
}
for (std::size_t idx: victim_threads_[num_thread])
{
HPX_ASSERT(idx != num_thread);
if (idx < high_priority_queues &&
num_thread < high_priority_queues)
{
thread_queue_type* q = high_priority_queues_[idx];
if (q->get_next_thread(thrd, running))
{
q->increment_num_stolen_from_pending();
this_high_priority_queue->
increment_num_stolen_to_pending();
return true;
}
}
if (queues_[idx]->get_next_thread(thrd, running))
{
queues_[idx]->increment_num_stolen_from_pending();
this_queue->increment_num_stolen_to_pending();
return true;
}
}
return low_priority_queue_.get_next_thread(thrd);
}
/// Schedule the passed thread
void schedule_thread(threads::thread_data* thrd,
std::size_t num_thread,
thread_priority priority = thread_priority_normal)
{
std::size_t queue_size = queues_.size();
if (std::size_t(-1) == num_thread)
num_thread = curr_queue_++ % queue_size;
if (priority == thread_priority_high_recursive ||
priority == thread_priority_high ||
priority == thread_priority_boost)
{
std::size_t num = num_thread % high_priority_queues_.size();
high_priority_queues_[num]->schedule_thread(thrd);
}
else if (priority == thread_priority_low)
{
low_priority_queue_.schedule_thread(thrd);
}
else
{
HPX_ASSERT(num_thread < queues_.size());
queues_[num_thread]->schedule_thread(thrd);
}
}
void schedule_thread_last(threads::thread_data* thrd,
std::size_t num_thread,
thread_priority priority = thread_priority_normal)
{
std::size_t queue_size = queues_.size();
if (std::size_t(-1) == num_thread)
num_thread = curr_queue_++ % queue_size;
if (priority == thread_priority_high_recursive ||
priority == thread_priority_high ||
priority == thread_priority_boost)
{
std::size_t num = num_thread % high_priority_queues_.size();
high_priority_queues_[num]->schedule_thread(thrd, true);
}
else if (priority == thread_priority_low)
{
low_priority_queue_.schedule_thread(thrd, true);
}
else
{
HPX_ASSERT(num_thread < queues_.size());
queues_[num_thread]->schedule_thread(thrd, true);
}
}
/// Destroy the passed thread as it has been terminated
bool destroy_thread(threads::thread_data* thrd, std::int64_t& busy_count)
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
{
if (high_priority_queues_[i]->destroy_thread(thrd, busy_count))
return true;
}
for (std::size_t i = 0; i != queues_.size(); ++i)
{
if (queues_[i]->destroy_thread(thrd, busy_count))
return true;
}
if (low_priority_queue_.destroy_thread(thrd, busy_count))
return true;
// the thread has to belong to one of the queues, always
HPX_ASSERT(false);
return false;
}
///////////////////////////////////////////////////////////////////////
// This returns the current length of the queues (work items and new items)
std::int64_t get_queue_length(std::size_t num_thread = std::size_t(-1)) const
{
// Return queue length of one specific queue.
std::int64_t count = 0;
if (std::size_t(-1) != num_thread) {
HPX_ASSERT(num_thread < queues_.size());
if (num_thread < high_priority_queues_.size())
count = high_priority_queues_[num_thread]->get_queue_length();
if (num_thread == queues_.size()-1)
count += low_priority_queue_.get_queue_length();
return count + queues_[num_thread]->get_queue_length();
}
// Cumulative queue lengths of all queues.
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
count += high_priority_queues_[i]->get_queue_length();
count += low_priority_queue_.get_queue_length();
for (std::size_t i = 0; i != queues_.size(); ++i)
count += queues_[i]->get_queue_length();
return count;
}
///////////////////////////////////////////////////////////////////////
// Queries the current thread count of the queues.
std::int64_t get_thread_count(thread_state_enum state = unknown,
thread_priority priority = thread_priority_default,
std::size_t num_thread = std::size_t(-1), bool reset = false) const
{
// Return thread count of one specific queue.
std::int64_t count = 0;
if (std::size_t(-1) != num_thread)
{
HPX_ASSERT(num_thread < queues_.size());
switch (priority) {
case thread_priority_default:
{
if (num_thread < high_priority_queues_.size())
count = high_priority_queues_[num_thread]->
get_thread_count(state);
if (queues_.size()-1 == num_thread)
count += low_priority_queue_.get_thread_count(state);
return count + queues_[num_thread]->get_thread_count(state);
}
case thread_priority_low:
{
if (queues_.size()-1 == num_thread)
return low_priority_queue_.get_thread_count(state);
break;
}
case thread_priority_normal:
return queues_[num_thread]->get_thread_count(state);
case thread_priority_boost:
case thread_priority_high:
case thread_priority_high_recursive:
{
if (num_thread < high_priority_queues_.size())
return high_priority_queues_[num_thread]->
get_thread_count(state);
break;
}
default:
case thread_priority_unknown:
{
HPX_THROW_EXCEPTION(bad_parameter,
"local_priority_queue_scheduler::get_thread_count",
"unknown thread priority value (thread_priority_unknown)");
return 0;
}
}
return 0;
}
// Return the cumulative count for all queues.
switch (priority) {
case thread_priority_default:
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
count += high_priority_queues_[i]->get_thread_count(state);
count += low_priority_queue_.get_thread_count(state);
for (std::size_t i = 0; i != queues_.size(); ++i)
count += queues_[i]->get_thread_count(state);
break;
}
case thread_priority_low:
return low_priority_queue_.get_thread_count(state);
case thread_priority_normal:
{
for (std::size_t i = 0; i != queues_.size(); ++i)
count += queues_[i]->get_thread_count(state);
break;
}
case thread_priority_boost:
case thread_priority_high:
case thread_priority_high_recursive:
{
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
count += high_priority_queues_[i]->get_thread_count(state);
break;
}
default:
case thread_priority_unknown:
{
HPX_THROW_EXCEPTION(bad_parameter,
"local_priority_queue_scheduler::get_thread_count",
"unknown thread priority value (thread_priority_unknown)");
return 0;
}
}
return count;
}
///////////////////////////////////////////////////////////////////////
// Enumerate matching threads from all queues
bool enumerate_threads(
util::function_nonser<bool(thread_id_type)> const& f,
thread_state_enum state = unknown) const
{
bool result = true;
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
{
result = result &&
high_priority_queues_[i]->enumerate_threads(f, state);
}
result = result && low_priority_queue_.enumerate_threads(f, state);
for (std::size_t i = 0; i != queues_.size(); ++i)
{
result = result && queues_[i]->enumerate_threads(f, state);
}
return result;
}
#ifdef HPX_HAVE_THREAD_QUEUE_WAITTIME
///////////////////////////////////////////////////////////////////////
// Queries the current average thread wait time of the queues.
std::int64_t get_average_thread_wait_time(
std::size_t num_thread = std::size_t(-1)) const
{
// Return average thread wait time of one specific queue.
std::uint64_t wait_time = 0;
std::uint64_t count = 0;
if (std::size_t(-1) != num_thread)
{
HPX_ASSERT(num_thread < queues_.size());
if (num_thread < high_priority_queues_.size())
{
wait_time = high_priority_queues_[num_thread]->
get_average_thread_wait_time();
++count;
}
if (queues_.size()-1 == num_thread)
{
wait_time += low_priority_queue_.
get_average_thread_wait_time();
++count;
}
wait_time += queues_[num_thread]->get_average_thread_wait_time();
return wait_time / (count + 1);
}
// Return the cumulative average thread wait time for all queues.
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
{
wait_time += high_priority_queues_[i]->get_average_thread_wait_time();
++count;
}
wait_time += low_priority_queue_.get_average_thread_wait_time();
for (std::size_t i = 0; i != queues_.size(); ++i)
{
wait_time += queues_[i]->get_average_thread_wait_time();
++count;
}
return wait_time / (count + 1);
}
///////////////////////////////////////////////////////////////////////
// Queries the current average task wait time of the queues.
std::int64_t get_average_task_wait_time(
std::size_t num_thread = std::size_t(-1)) const
{
// Return average task wait time of one specific queue.
std::uint64_t wait_time = 0;
std::uint64_t count = 0;
if (std::size_t(-1) != num_thread)
{
HPX_ASSERT(num_thread < queues_.size());
if (num_thread < high_priority_queues_.size())
{
wait_time = high_priority_queues_[num_thread]->
get_average_task_wait_time();
++count;
}
if (queues_.size()-1 == num_thread)
{
wait_time += low_priority_queue_.
get_average_task_wait_time();
++count;
}
wait_time += queues_[num_thread]->get_average_task_wait_time();
return wait_time / (count + 1);
}
// Return the cumulative average task wait time for all queues.
for (std::size_t i = 0; i != high_priority_queues_.size(); ++i)
{
wait_time += high_priority_queues_[i]->
get_average_task_wait_time();
++count;
}
wait_time += low_priority_queue_.get_average_task_wait_time();
for (std::size_t i = 0; i != queues_.size(); ++i)
{
wait_time += queues_[i]->get_average_task_wait_time();
++count;
}
return wait_time / (count + 1);
}
#endif
/// This is a function which gets called periodically by the thread
/// manager to allow for maintenance tasks to be executed in the
/// scheduler. Returns true if the OS thread calling this function
/// has to be terminated (i.e. no more work has to be done).
virtual bool wait_or_add_new(std::size_t num_thread, bool running,
std::int64_t& idle_loop_count)
{
std::size_t added = 0;
bool result = true;
std::size_t high_priority_queues = high_priority_queues_.size();
thread_queue_type* this_high_priority_queue = nullptr;
thread_queue_type* this_queue = queues_[num_thread];
if (num_thread < high_priority_queues)
{
this_high_priority_queue = high_priority_queues_[num_thread];
result = this_high_priority_queue->wait_or_add_new(running,
idle_loop_count, added)
&& result;
if (0 != added) return result;
}
result = this_queue->wait_or_add_new(
running, idle_loop_count, added) && result;
if (0 != added) return result;
// Check if we have been disabled
{
auto mask = rp_.get_pu_mask(
num_thread + parent_pool_->get_thread_offset());
if (!bit_and(mask, parent_pool_->get_used_processing_units()))
{
return added == 0 && !running;
}
}
for (std::size_t idx: victim_threads_[num_thread])
{
HPX_ASSERT(idx != num_thread);
if (idx < high_priority_queues &&
num_thread < high_priority_queues)
{
thread_queue_type* q = high_priority_queues_[idx];
result = this_high_priority_queue->
wait_or_add_new(running, idle_loop_count,
added, q)
&& result;
if (0 != added)
{
q->increment_num_stolen_from_staged(added);
this_high_priority_queue->
increment_num_stolen_to_staged(added);
return result;
}
}
result = this_queue->wait_or_add_new(running,
idle_loop_count, added, queues_[idx]) && result;
if (0 != added)
{
queues_[idx]->increment_num_stolen_from_staged(added);
this_queue->increment_num_stolen_to_staged(added);
return result;
}
}
#ifdef HPX_HAVE_THREAD_MINIMAL_DEADLOCK_DETECTION
// no new work is available, are we deadlocked?
if (HPX_UNLIKELY(minimal_deadlock_detection && LHPX_ENABLED(error)))
{
bool suspended_only = true;
for (std::size_t i = 0; suspended_only && i != queues_.size(); ++i) {
suspended_only = queues_[i]->dump_suspended_threads(
i, idle_loop_count, running);
}
if (HPX_UNLIKELY(suspended_only)) {
if (running) {
LTM_(error) //-V128
<< "queue(" << num_thread << "): "
<< "no new work available, are we deadlocked?";
}
else {
LHPX_CONSOLE_(hpx::util::logging::level::error) //-V128
<< " [TM] " //-V128