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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2015 by Delphix. All rights reserved.
*/
/*
* The System Duty Cycle (SDC) scheduling class
* --------------------------------------------
*
* Background
*
* Kernel threads in Solaris have traditionally not been large consumers
* of CPU time. They typically wake up, perform a small amount of
* work, then go back to sleep waiting for either a timeout or another
* signal. On the assumption that the small amount of work that they do
* is important for the behavior of the whole system, these threads are
* treated kindly by the dispatcher and the SYS scheduling class: they run
* without preemption from anything other than real-time and interrupt
* threads; when preempted, they are put at the front of the queue, so they
* generally do not migrate between CPUs; and they are allowed to stay
* running until they voluntarily give up the CPU.
*
* As Solaris has evolved, new workloads have emerged which require the
* kernel to perform significant amounts of CPU-intensive work. One
* example of such a workload is ZFS's transaction group sync processing.
* Each sync operation generates a large batch of I/Os, and each I/O
* may need to be compressed and/or checksummed before it is written to
* storage. The taskq threads which perform the compression and checksums
* will run nonstop as long as they have work to do; a large sync operation
* on a compression-heavy dataset can keep them busy for seconds on end.
* This causes human-time-scale dispatch latency bubbles for any other
* threads which have the misfortune to share a CPU with the taskq threads.
*
* The SDC scheduling class is a solution to this problem.
*
*
* Overview
*
* SDC is centered around the concept of a thread's duty cycle (DC):
*
* ONPROC time
* Duty Cycle = ----------------------
* ONPROC + Runnable time
*
* This is the ratio of the time that the thread spent running on a CPU
* divided by the time it spent running or trying to run. It is unaffected
* by any time the thread spent sleeping, stopped, etc.
*
* A thread joining the SDC class specifies a "target" DC that it wants
* to run at. To implement this policy, the routine sysdc_update() scans
* the list of active SDC threads every few ticks and uses each thread's
* microstate data to compute the actual duty cycle that that thread
* has experienced recently. If the thread is under its target DC, its
* priority is increased to the maximum available (sysdc_maxpri, which is
* 99 by default). If the thread is over its target DC, its priority is
* reduced to the minimum available (sysdc_minpri, 0 by default). This
* is a fairly primitive approach, in that it doesn't use any of the
* intermediate priorities, but it's not completely inappropriate. Even
* though threads in the SDC class might take a while to do their job, they
* are by some definition important if they're running inside the kernel,
* so it is reasonable that they should get to run at priority 99.
*
* If a thread is running when sysdc_update() calculates its actual duty
* cycle, and there are other threads of equal or greater priority on its
* CPU's dispatch queue, sysdc_update() preempts that thread. The thread
* acknowledges the preemption by calling sysdc_preempt(), which calls
* setbackdq(), which gives other threads with the same priority a chance
* to run. This creates a de facto time quantum for threads in the SDC
* scheduling class.
*
* An SDC thread which is assigned priority 0 can continue to run if
* nothing else needs to use the CPU that it's running on. Similarly, an
* SDC thread at priority 99 might not get to run as much as it wants to
* if there are other priority-99 or higher threads on its CPU. These
* situations would cause the thread to get ahead of or behind its target
* DC; the longer the situations lasted, the further ahead or behind the
* thread would get. Rather than condemning a thread to a lifetime of
* paying for its youthful indiscretions, SDC keeps "base" values for
* ONPROC and Runnable times in each thread's sysdc data, and updates these
* values periodically. The duty cycle is then computed using the elapsed
* amount of ONPROC and Runnable times since those base times.
*
* Since sysdc_update() scans SDC threads fairly frequently, it tries to
* keep the list of "active" threads small by pruning out threads which
* have been asleep for a brief time. They are not pruned immediately upon
* going to sleep, since some threads may bounce back and forth between
* sleeping and being runnable.
*
*
* Interfaces
*
* void sysdc_thread_enter(t, dc, flags)
*
* Moves a kernel thread from the SYS scheduling class to the
* SDC class. t must have an associated LWP (created by calling
* lwp_kernel_create()). The thread will have a target DC of dc.
* Flags should be either 0 or SYSDC_THREAD_BATCH. If
* SYSDC_THREAD_BATCH is specified, the thread is expected to be
* doing large amounts of processing.
*
*
* Complications
*
* - Run queue balancing
*
* The Solaris dispatcher is biased towards letting a thread run
* on the same CPU which it last ran on, if no more than 3 ticks
* (i.e. rechoose_interval) have passed since the thread last ran.
* This helps to preserve cache warmth. On the other hand, it also
* tries to keep the per-CPU run queues fairly balanced; if the CPU
* chosen for a runnable thread has a run queue which is three or
* more threads longer than a neighboring CPU's queue, the runnable
* thread is dispatched onto the neighboring CPU instead.
*
* These policies work well for some workloads, but not for many SDC
* threads. The taskq client of SDC, for example, has many discrete
* units of work to do. The work units are largely independent, so
* cache warmth is not an important consideration. It is important
* that the threads fan out quickly to different CPUs, since the
* amount of work these threads have to do (a few seconds worth at a
* time) doesn't leave much time to correct thread placement errors
* (i.e. two SDC threads being dispatched to the same CPU).
*
* To fix this, SDC uses the TS_RUNQMATCH flag introduced for FSS.
* This tells the dispatcher to keep neighboring run queues' lengths
* more evenly matched, which allows SDC threads to migrate more
* easily.
*
* - LWPs and system processes
*
* SDC can only be used for kernel threads. Since SDC uses microstate
* accounting data to compute each thread's actual duty cycle, all
* threads entering the SDC class must have associated LWPs (which
* store the microstate data). This means that the threads have to
* be associated with an SSYS process, i.e. one created by newproc().
* If the microstate accounting information is ever moved into the
* kthread_t, this restriction could be lifted.
*
* - Dealing with oversubscription
*
* Since SDC duty cycles are per-thread, it is possible that the
* aggregate requested duty cycle of all SDC threads in a processor
* set could be greater than the total CPU time available in that set.
* The FSS scheduling class has an analogous situation, which it deals
* with by reducing each thread's allotted CPU time proportionally.
* Since SDC doesn't need to be as precise as FSS, it uses a simpler
* solution to the oversubscription problem.
*
* sysdc_update() accumulates the amount of time that max-priority SDC
* threads have spent on-CPU in each processor set, and uses that sum
* to create an implied duty cycle for that processor set:
*
* accumulated CPU time
* pset DC = -----------------------------------
* (# CPUs) * time since last update
*
* If this implied duty cycle is above a maximum pset duty cycle (90%
* by default), sysdc_update() sets the priority of all SDC threads
* in that processor set to sysdc_minpri for a "break" period. After
* the break period, it waits for a "nobreak" period before trying to
* enforce the pset duty cycle limit again.
*
* - Processor sets
*
* As the above implies, SDC is processor set aware, but it does not
* currently allow threads to change processor sets while in the SDC
* class. Instead, those threads must join the desired processor set
* before entering SDC. [1]
*
* - Batch threads
*
* A thread joining the SDC class can specify the SDC_THREAD_BATCH
* flag. This flag currently has no effect, but marks threads which
* do bulk processing.
*
* - Why not FSS?
*
* It might seem that the existing FSS scheduling class could solve
* the problems that SDC is attempting to solve. FSS's more precise
* solution to the oversubscription problem would hardly cause
* trouble, as long as it performed well. SDC is implemented as
* a separate scheduling class for two main reasons: the initial
* consumer of SDC does not map well onto the "project" abstraction
* that is central to FSS, and FSS does not expect to run at kernel
* priorities.
*
*
* Tunables
*
* - sysdc_update_interval_msec: Number of milliseconds between
* consecutive thread priority updates.
*
* - sysdc_reset_interval_msec: Number of milliseconds between
* consecutive resets of a thread's base ONPROC and Runnable
* times.
*
* - sysdc_prune_interval_msec: Number of milliseconds of sleeping
* before a thread is pruned from the active list.
*
* - sysdc_max_pset_DC: Allowable percentage of a processor set's
* CPU time which SDC can give to its high-priority threads.
*
* - sysdc_break_msec: Number of milliseconds of "break" taken when
* sysdc_max_pset_DC is exceeded.
*
*
* Future work (in SDC and related subsystems)
*
* - Per-thread rechoose interval (0 for SDC)
*
* Allow each thread to specify its own rechoose interval. SDC
* threads would specify an interval of zero, which would rechoose
* the CPU with the lowest priority once per update.
*
* - Allow threads to change processor sets after joining the SDC class
*
* - Thread groups and per-group DC
*
* It might be nice to be able to specify a duty cycle which applies
* to a group of threads in aggregate.
*
* - Per-group DC callback to allow dynamic DC tuning
*
* Currently, DCs are assigned when the thread joins SDC. Some
* workloads could benefit from being able to tune their DC using
* subsystem-specific knowledge about the workload.
*
* - Finer-grained priority updates
*
* - More nuanced management of oversubscription
*
* - Moving other CPU-intensive threads into SDC
*
* - Move msacct data into kthread_t
*
* This would allow kernel threads without LWPs to join SDC.
*
*
* Footnotes
*
* [1] The details of doing so are left as an exercise for the reader.
*/
#include <sys/types.h>
#include <sys/sysdc.h>
#include <sys/sysdc_impl.h>
#include <sys/class.h>
#include <sys/cmn_err.h>
#include <sys/cpuvar.h>
#include <sys/cpupart.h>
#include <sys/debug.h>
#include <sys/disp.h>
#include <sys/errno.h>
#include <sys/inline.h>
#include <sys/kmem.h>
#include <sys/modctl.h>
#include <sys/schedctl.h>
#include <sys/sdt.h>
#include <sys/sunddi.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/var.h>
/*
* Tunables - loaded into the internal state at module load time
*/
uint_t sysdc_update_interval_msec = 20;
uint_t sysdc_reset_interval_msec = 400;
uint_t sysdc_prune_interval_msec = 100;
uint_t sysdc_max_pset_DC = 90;
uint_t sysdc_break_msec = 80;
/*
* Internal state - constants set up by sysdc_initparam()
*/
static clock_t sysdc_update_ticks; /* ticks between updates */
static uint_t sysdc_prune_updates; /* updates asleep before pruning */
static uint_t sysdc_reset_updates; /* # of updates before reset */
static uint_t sysdc_break_updates; /* updates to break */
static uint_t sysdc_nobreak_updates; /* updates to not check */
static uint_t sysdc_minDC; /* minimum allowed DC */
static uint_t sysdc_maxDC; /* maximum allowed DC */
static pri_t sysdc_minpri; /* minimum allowed priority */
static pri_t sysdc_maxpri; /* maximum allowed priority */
/*
* Internal state
*/
static kmutex_t sysdc_pset_lock; /* lock protecting pset data */
static list_t sysdc_psets; /* list of psets with SDC threads */
static uint_t sysdc_param_init; /* sysdc_initparam() has been called */
static uint_t sysdc_update_timeout_started; /* update timeout is active */
static hrtime_t sysdc_last_update; /* time of last sysdc_update() */
static sysdc_t sysdc_dummy; /* used to terminate active lists */
/*
* Internal state - active hash table
*/
#define SYSDC_NLISTS 8
#define SYSDC_HASH(sdc) (((uintptr_t)(sdc) >> 6) & (SYSDC_NLISTS - 1))
static sysdc_list_t sysdc_active[SYSDC_NLISTS];
#define SYSDC_LIST(sdc) (&sysdc_active[SYSDC_HASH(sdc)])
#ifdef DEBUG
static struct {
uint64_t sysdc_update_times_asleep;
uint64_t sysdc_update_times_base_ran_backwards;
uint64_t sysdc_update_times_already_done;
uint64_t sysdc_update_times_cur_ran_backwards;
uint64_t sysdc_compute_pri_breaking;
uint64_t sysdc_activate_enter;
uint64_t sysdc_update_enter;
uint64_t sysdc_update_exited;
uint64_t sysdc_update_not_sdc;
uint64_t sysdc_update_idle;
uint64_t sysdc_update_take_break;
uint64_t sysdc_update_no_psets;
uint64_t sysdc_tick_not_sdc;
uint64_t sysdc_tick_quantum_expired;
uint64_t sysdc_thread_enter_enter;
} sysdc_stats;
#define SYSDC_INC_STAT(x) (sysdc_stats.x++)
#else
#define SYSDC_INC_STAT(x) ((void)0)
#endif
/* macros are UPPER CASE */
#define HOWMANY(a, b) howmany((a), (b))
#define MSECTOTICKS(a) HOWMANY((a) * 1000, usec_per_tick)
static void
sysdc_initparam(void)
{
uint_t sysdc_break_ticks;
/* update / prune intervals */
sysdc_update_ticks = MSECTOTICKS(sysdc_update_interval_msec);
sysdc_prune_updates = HOWMANY(sysdc_prune_interval_msec,
sysdc_update_interval_msec);
sysdc_reset_updates = HOWMANY(sysdc_reset_interval_msec,
sysdc_update_interval_msec);
/* We must get at least a little time on CPU. */
sysdc_minDC = 1;
sysdc_maxDC = SYSDC_DC_MAX;
sysdc_minpri = 0;
sysdc_maxpri = maxclsyspri - 1;
/* break parameters */
if (sysdc_max_pset_DC > SYSDC_DC_MAX) {
sysdc_max_pset_DC = SYSDC_DC_MAX;
}
sysdc_break_ticks = MSECTOTICKS(sysdc_break_msec);
sysdc_break_updates = HOWMANY(sysdc_break_ticks, sysdc_update_ticks);
/*
* We want:
*
* sysdc_max_pset_DC = (nobreak / (break + nobreak))
*
* ==> nobreak = sysdc_max_pset_DC * (break + nobreak)
*
* sysdc_max_pset_DC * break
* ==> nobreak = -------------------------
* 1 - sysdc_max_pset_DC
*/
sysdc_nobreak_updates =
HOWMANY((uint64_t)sysdc_break_updates * sysdc_max_pset_DC,
(SYSDC_DC_MAX - sysdc_max_pset_DC));
sysdc_param_init = 1;
}
#undef HOWMANY
#undef MSECTOTICKS
#define SDC_UPDATE_INITIAL 0x1 /* for the initial update */
#define SDC_UPDATE_TIMEOUT 0x2 /* from sysdc_update() */
#define SDC_UPDATE_TICK 0x4 /* from sysdc_tick(), on expiry */
/*
* Updates the recorded times in the sdc, and returns the elapsed ONPROC
* and Runnable times since the last reset.
*
* newO is the thread's actual ONPROC time; it's used during sysdc_update()
* to track processor set usage.
*/
static void
sysdc_update_times(sysdc_t *sdc, uint_t flags,
hrtime_t *O, hrtime_t *R, hrtime_t *newO)
{
kthread_t *const t = sdc->sdc_thread;
const uint_t initial = (flags & SDC_UPDATE_INITIAL);
const uint_t update = (flags & SDC_UPDATE_TIMEOUT);
const clock_t now = ddi_get_lbolt();
uint_t do_reset;
ASSERT(THREAD_LOCK_HELD(t));
*O = *R = 0;
/* If we've been sleeping, we know we haven't had any ONPROC time. */
if (sdc->sdc_sleep_updates != 0 &&
sdc->sdc_sleep_updates != sdc->sdc_nupdates) {
*newO = sdc->sdc_last_base_O;
SYSDC_INC_STAT(sysdc_update_times_asleep);
return;
}
/*
* If this is our first update, or we've hit the reset point,
* we need to reset our base_{O,R}. Once we've updated them, we
* report O and R for the entire prior interval.
*/
do_reset = initial;
if (update) {
++sdc->sdc_nupdates;
if ((sdc->sdc_nupdates % sysdc_reset_updates) == 0)
do_reset = 1;
}
if (do_reset) {
hrtime_t baseO, baseR;
if (initial) {
/*
* Start off our cycle count somewhere in the middle,
* to keep the resets from all happening at once.
*
* 4999 is a handy prime much larger than
* sysdc_reset_updates, so that we don't run into
* trouble if the resolution is a multiple of
* sysdc_reset_updates.
*/
sdc->sdc_nupdates = (uint_t)((gethrtime() % 4999) %
sysdc_reset_updates);
baseO = baseR = 0;
} else {
baseO = sdc->sdc_base_O;
baseR = sdc->sdc_base_R;
}
mstate_systhread_times(t, &sdc->sdc_base_O, &sdc->sdc_base_R);
*newO = sdc->sdc_base_O;
sdc->sdc_reset = now;
sdc->sdc_pri_check = -1; /* force mismatch below */
/*
* See below for rationale.
*/
if (baseO > sdc->sdc_base_O || baseR > sdc->sdc_base_R) {
SYSDC_INC_STAT(sysdc_update_times_base_ran_backwards);
baseO = sdc->sdc_base_O;
baseR = sdc->sdc_base_R;
}
/* compute based on the entire interval */
*O = (sdc->sdc_base_O - baseO);
*R = (sdc->sdc_base_R - baseR);
return;
}
/*
* If we're called from sysdc_update(), we *must* return a value
* for newO, so we always call mstate_systhread_times().
*
* Otherwise, if we've already done a pri check this tick,
* we can skip it.
*/
if (!update && sdc->sdc_pri_check == now) {
SYSDC_INC_STAT(sysdc_update_times_already_done);
return;
}
/* Get the current times from the thread */
sdc->sdc_pri_check = now;
mstate_systhread_times(t, &sdc->sdc_cur_O, &sdc->sdc_cur_R);
*newO = sdc->sdc_cur_O;
/*
* The updating of microstate accounting is not done under a
* consistent set of locks, particularly the t_waitrq field. This
* can lead to narrow windows in which we account for time in the
* wrong bucket, which on the next read will be accounted for
* correctly.
*
* If our sdc_base_* fields were affected by one of these blips, we
* throw away the old data, and pretend this tick didn't happen.
*/
if (sdc->sdc_cur_O < sdc->sdc_base_O ||
sdc->sdc_cur_R < sdc->sdc_base_R) {
sdc->sdc_base_O = sdc->sdc_cur_O;
sdc->sdc_base_R = sdc->sdc_cur_R;
SYSDC_INC_STAT(sysdc_update_times_cur_ran_backwards);
return;
}
*O = sdc->sdc_cur_O - sdc->sdc_base_O;
*R = sdc->sdc_cur_R - sdc->sdc_base_R;
}
/*
* sysdc_compute_pri()
*
* Recomputes the priority of the thread, leaving the result in
* sdc->sdc_epri. Returns 1 if a priority update should occur
* (which will also trigger a cpu_surrender()), otherwise
* returns 0.
*/
static uint_t
sysdc_compute_pri(sysdc_t *sdc, uint_t flags)
{
kthread_t *const t = sdc->sdc_thread;
const uint_t update = (flags & SDC_UPDATE_TIMEOUT);
const uint_t tick = (flags & SDC_UPDATE_TICK);
hrtime_t O, R;
hrtime_t newO = -1;
ASSERT(THREAD_LOCK_HELD(t));
sysdc_update_times(sdc, flags, &O, &R, &newO);
ASSERT(!update || newO != -1);
/* If we have new data, recompute our priority. */
if ((O + R) != 0) {
sdc->sdc_cur_DC = (O * SYSDC_DC_MAX) / (O + R);
/* Adjust our priority to move our DC closer to the target. */
if (sdc->sdc_cur_DC < sdc->sdc_target_DC)
sdc->sdc_pri = sdc->sdc_maxpri;
else
sdc->sdc_pri = sdc->sdc_minpri;
}
/*
* If our per-pset duty cycle goes over the max, we will take a break.
* This forces all sysdc threads in the pset to minimum priority, in
* order to let everyone else have a chance at the CPU.
*/
if (sdc->sdc_pset->sdp_need_break) {
SYSDC_INC_STAT(sysdc_compute_pri_breaking);
sdc->sdc_epri = sdc->sdc_minpri;
} else {
sdc->sdc_epri = sdc->sdc_pri;
}
DTRACE_PROBE4(sysdc__compute__pri,
kthread_t *, t, pri_t, sdc->sdc_epri, uint_t, sdc->sdc_cur_DC,
uint_t, sdc->sdc_target_DC);
/*
* For sysdc_update(), we compute the ONPROC time for high-priority
* threads, which is used to calculate the per-pset duty cycle. We
* will always tell our callers to update the thread's priority,
* since we want to force a cpu_surrender().
*
* We reset sdc_update_ticks so that sysdc_tick() will only update
* the thread's priority if our timeout is delayed by a tick or
* more.
*/
if (update) {
/* SDC threads are not allowed to change cpupart bindings. */
ASSERT(t->t_cpupart == sdc->sdc_pset->sdp_cpupart);
/* If we were at MAXPRI, account for our onproc time. */
if (t->t_pri == sdc->sdc_maxpri &&
sdc->sdc_last_base_O != 0 &&
sdc->sdc_last_base_O < newO) {
sdc->sdc_last_O = newO - sdc->sdc_last_base_O;
sdc->sdc_pset->sdp_onproc_time +=
(uint64_t)sdc->sdc_last_O;
sdc->sdc_pset->sdp_onproc_threads++;
} else {
sdc->sdc_last_O = 0;
}
sdc->sdc_last_base_O = newO;
sdc->sdc_update_ticks = sdc->sdc_ticks + sysdc_update_ticks + 1;
return (1);
}
/*
* Like sysdc_update(), sysdc_tick() always wants to update the
* thread's priority, so that the CPU is surrendered if necessary.
* We reset sdc_update_ticks so that if the timeout continues to be
* delayed, we'll update at the regular interval.
*/
if (tick) {
ASSERT(sdc->sdc_ticks == sdc->sdc_update_ticks);
sdc->sdc_update_ticks = sdc->sdc_ticks + sysdc_update_ticks;
return (1);
}
/*
* Otherwise, only tell our callers to update the priority if it has
* changed.
*/
return (sdc->sdc_epri != t->t_pri);
}
static void
sysdc_update_pri(sysdc_t *sdc, uint_t flags)
{
kthread_t *t = sdc->sdc_thread;
ASSERT(THREAD_LOCK_HELD(t));
if (sysdc_compute_pri(sdc, flags)) {
if (!thread_change_pri(t, sdc->sdc_epri, 0)) {
cpu_surrender(t);
}
}
}
/*
* Add a thread onto the active list. It will only be removed by
* sysdc_update().
*/
static void
sysdc_activate(sysdc_t *sdc)
{
sysdc_t *volatile *headp = &SYSDC_LIST(sdc)->sdl_list;
sysdc_t *head;
kthread_t *t = sdc->sdc_thread;
SYSDC_INC_STAT(sysdc_activate_enter);
ASSERT(sdc->sdc_next == NULL);
ASSERT(THREAD_LOCK_HELD(t));
do {
head = *headp;
sdc->sdc_next = head;
} while (atomic_cas_ptr(headp, head, sdc) != head);
}
/*
* sysdc_update() has two jobs:
*
* 1. It updates the priorities of all active SDC threads on the system.
* 2. It measures pset CPU usage and enforces sysdc_max_pset_DC.
*/
static void
sysdc_update(void *arg)
{
int idx;
sysdc_t *freelist = NULL;
sysdc_pset_t *cur;
hrtime_t now, diff;
uint_t redeploy = 1;
SYSDC_INC_STAT(sysdc_update_enter);
ASSERT(sysdc_update_timeout_started);
/*
* If this is our first time through, diff will be gigantic, and
* no breaks will be necessary.
*/
now = gethrtime();
diff = now - sysdc_last_update;
sysdc_last_update = now;
mutex_enter(&sysdc_pset_lock);
for (cur = list_head(&sysdc_psets); cur != NULL;
cur = list_next(&sysdc_psets, cur)) {
boolean_t breaking = (cur->sdp_should_break != 0);
if (cur->sdp_need_break != breaking) {
DTRACE_PROBE2(sdc__pset__break, sysdc_pset_t *, cur,
boolean_t, breaking);
}
cur->sdp_onproc_time = 0;
cur->sdp_onproc_threads = 0;
cur->sdp_need_break = breaking;
}
mutex_exit(&sysdc_pset_lock);
for (idx = 0; idx < SYSDC_NLISTS; idx++) {
sysdc_list_t *sdl = &sysdc_active[idx];
sysdc_t *volatile *headp = &sdl->sdl_list;
sysdc_t *head, *tail;
sysdc_t **prevptr;
if (*headp == &sysdc_dummy)
continue;
/* Prevent any threads from exiting while we're poking them. */
mutex_enter(&sdl->sdl_lock);
/*
* Each sdl_list contains a singly-linked list of active
* threads. Threads which become active while we are
* processing the list will be added to sdl_list. Since we
* don't want that to interfere with our own processing, we
* swap in an empty list. Any newly active threads will
* go on to this empty list. When finished, we'll put any
* such threads at the end of the processed list.
*/
head = atomic_swap_ptr(headp, &sysdc_dummy);
prevptr = &head;
while (*prevptr != &sysdc_dummy) {
sysdc_t *const sdc = *prevptr;
kthread_t *const t = sdc->sdc_thread;
/*
* If the thread has exited, move its sysdc_t onto
* freelist, to be freed later.
*/
if (t == NULL) {
*prevptr = sdc->sdc_next;
SYSDC_INC_STAT(sysdc_update_exited);
sdc->sdc_next = freelist;
freelist = sdc;
continue;
}
thread_lock(t);
if (t->t_cid != sysdccid) {
thread_unlock(t);
prevptr = &sdc->sdc_next;
SYSDC_INC_STAT(sysdc_update_not_sdc);
continue;
}
ASSERT(t->t_cldata == sdc);
/*
* If the thread has been sleeping for longer
* than sysdc_prune_interval, make it inactive by
* removing it from the list.
*/
if (!(t->t_state & (TS_RUN | TS_ONPROC)) &&
sdc->sdc_sleep_updates != 0 &&
(sdc->sdc_sleep_updates - sdc->sdc_nupdates) >
sysdc_prune_updates) {
*prevptr = sdc->sdc_next;
SYSDC_INC_STAT(sysdc_update_idle);
sdc->sdc_next = NULL;
thread_unlock(t);
continue;
}
sysdc_update_pri(sdc, SDC_UPDATE_TIMEOUT);
thread_unlock(t);
prevptr = &sdc->sdc_next;
}
/*
* Add our list to the bucket, putting any new entries
* added while we were working at the tail of the list.
*/
do {
tail = *headp;
*prevptr = tail;
} while (atomic_cas_ptr(headp, tail, head) != tail);
mutex_exit(&sdl->sdl_lock);
}
mutex_enter(&sysdc_pset_lock);
for (cur = list_head(&sysdc_psets); cur != NULL;
cur = list_next(&sysdc_psets, cur)) {
cur->sdp_vtime_last_interval =
diff * cur->sdp_cpupart->cp_ncpus;
cur->sdp_DC_last_interval =
(cur->sdp_onproc_time * SYSDC_DC_MAX) /
cur->sdp_vtime_last_interval;
if (cur->sdp_should_break > 0) {
cur->sdp_should_break--; /* breaking */
continue;
}
if (cur->sdp_dont_break > 0) {
cur->sdp_dont_break--; /* waiting before checking */
continue;
}
if (cur->sdp_DC_last_interval > sysdc_max_pset_DC) {
cur->sdp_should_break = sysdc_break_updates;
cur->sdp_dont_break = sysdc_nobreak_updates;
SYSDC_INC_STAT(sysdc_update_take_break);
}
}
/*
* If there are no sysdc_psets, there can be no threads, so
* we can stop doing our timeout. Since we're holding the
* sysdc_pset_lock, no new sysdc_psets can come in, which will
* prevent anyone from racing with this and dropping our timeout
* on the floor.
*/
if (list_is_empty(&sysdc_psets)) {
SYSDC_INC_STAT(sysdc_update_no_psets);
ASSERT(sysdc_update_timeout_started);
sysdc_update_timeout_started = 0;
redeploy = 0;
}
mutex_exit(&sysdc_pset_lock);
while (freelist != NULL) {
sysdc_t *cur = freelist;
freelist = cur->sdc_next;
kmem_free(cur, sizeof (*cur));
}
if (redeploy) {
(void) timeout(sysdc_update, arg, sysdc_update_ticks);
}
}
static void
sysdc_preempt(kthread_t *t)
{
ASSERT(t == curthread);
ASSERT(THREAD_LOCK_HELD(t));
setbackdq(t); /* give others a chance to run */
}
static void
sysdc_tick(kthread_t *t)
{
sysdc_t *sdc;
thread_lock(t);
if (t->t_cid != sysdccid) {
SYSDC_INC_STAT(sysdc_tick_not_sdc);
thread_unlock(t);
return;
}
sdc = t->t_cldata;
if (t->t_state == TS_ONPROC &&
t->t_pri < t->t_disp_queue->disp_maxrunpri) {
cpu_surrender(t);
}
if (t->t_state == TS_ONPROC || t->t_state == TS_RUN) {
ASSERT(sdc->sdc_sleep_updates == 0);
}
ASSERT(sdc->sdc_ticks != sdc->sdc_update_ticks);
sdc->sdc_ticks++;
if (sdc->sdc_ticks == sdc->sdc_update_ticks) {
SYSDC_INC_STAT(sysdc_tick_quantum_expired);
sysdc_update_pri(sdc, SDC_UPDATE_TICK);
ASSERT(sdc->sdc_ticks != sdc->sdc_update_ticks);
}
thread_unlock(t);
}
static void
sysdc_setrun(kthread_t *t)
{
sysdc_t *sdc = t->t_cldata;
ASSERT(THREAD_LOCK_HELD(t)); /* t should be in transition */
sdc->sdc_sleep_updates = 0;
if (sdc->sdc_next == NULL) {
/*
* Since we're in transition, we don't want to use the
* full thread_update_pri().
*/
if (sysdc_compute_pri(sdc, 0)) {
THREAD_CHANGE_PRI(t, sdc->sdc_epri);
}
sysdc_activate(sdc);
ASSERT(sdc->sdc_next != NULL);
}
setbackdq(t);
}
static void
sysdc_wakeup(kthread_t *t)
{
sysdc_setrun(t);
}
static void
sysdc_sleep(kthread_t *t)
{
sysdc_t *sdc = t->t_cldata;
ASSERT(THREAD_LOCK_HELD(t)); /* t should be in transition */
sdc->sdc_sleep_updates = sdc->sdc_nupdates;
}
/*ARGSUSED*/
static int
sysdc_enterclass(kthread_t *t, id_t cid, void *parmsp, cred_t *reqpcredp,
void *bufp)
{
cpupart_t *const cpupart = t->t_cpupart;
sysdc_t *sdc = bufp;
sysdc_params_t *sdpp = parmsp;
sysdc_pset_t *newpset = sdc->sdc_pset;
sysdc_pset_t *pset;
int start_timeout;
if (t->t_cid != syscid)
return (EPERM);
ASSERT(ttolwp(t) != NULL);
ASSERT(sdpp != NULL);
ASSERT(newpset != NULL);
ASSERT(sysdc_param_init);
ASSERT(sdpp->sdp_minpri >= sysdc_minpri);
ASSERT(sdpp->sdp_maxpri <= sysdc_maxpri);
ASSERT(sdpp->sdp_DC >= sysdc_minDC);
ASSERT(sdpp->sdp_DC <= sysdc_maxDC);
sdc->sdc_thread = t;
sdc->sdc_pri = sdpp->sdp_maxpri; /* start off maximally */
sdc->sdc_minpri = sdpp->sdp_minpri;
sdc->sdc_maxpri = sdpp->sdp_maxpri;
sdc->sdc_target_DC = sdpp->sdp_DC;
sdc->sdc_ticks = 0;
sdc->sdc_update_ticks = sysdc_update_ticks + 1;
/* Assign ourselves to the appropriate pset. */
sdc->sdc_pset = NULL;
mutex_enter(&sysdc_pset_lock);
for (pset = list_head(&sysdc_psets); pset != NULL;
pset = list_next(&sysdc_psets, pset)) {
if (pset->sdp_cpupart == cpupart) {
break;
}
}
if (pset == NULL) {
pset = newpset;
newpset = NULL;
pset->sdp_cpupart = cpupart;
list_insert_tail(&sysdc_psets, pset);
}
pset->sdp_nthreads++;
ASSERT(pset->sdp_nthreads > 0);
sdc->sdc_pset = pset;
start_timeout = (sysdc_update_timeout_started == 0);
sysdc_update_timeout_started = 1;
mutex_exit(&sysdc_pset_lock);
if (newpset != NULL)
kmem_free(newpset, sizeof (*newpset));
/* Update t's scheduling class and priority. */
thread_lock(t);
t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
t->t_cid = cid;
t->t_cldata = sdc;
t->t_schedflag |= TS_RUNQMATCH;
sysdc_update_pri(sdc, SDC_UPDATE_INITIAL);
thread_unlock(t);
/* Kick off the thread timeout if we're the first one in. */
if (start_timeout) {
(void) timeout(sysdc_update, NULL, sysdc_update_ticks);
}
return (0);
}
static void
sysdc_leave(sysdc_t *sdc)
{
sysdc_pset_t *sdp = sdc->sdc_pset;
sysdc_list_t *sdl = SYSDC_LIST(sdc);
uint_t freedc;
mutex_enter(&sdl->sdl_lock); /* block sysdc_update() */
sdc->sdc_thread = NULL;
freedc = (sdc->sdc_next == NULL);
mutex_exit(&sdl->sdl_lock);
mutex_enter(&sysdc_pset_lock);
ASSERT(sdp != NULL);
ASSERT(sdp->sdp_nthreads > 0);
--sdp->sdp_nthreads;
if (sdp->sdp_nthreads == 0) {
list_remove(&sysdc_psets, sdp);
} else {
sdp = NULL;
}
mutex_exit(&sysdc_pset_lock);
if (freedc)
kmem_free(sdc, sizeof (*sdc));
if (sdp != NULL)
kmem_free(sdp, sizeof (*sdp));
}
static void
sysdc_exitclass(void *buf)
{
sysdc_leave((sysdc_t *)buf);
}
/*ARGSUSED*/
static int
sysdc_canexit(kthread_t *t, cred_t *reqpcredp)
{
/* Threads cannot exit SDC once joined, except in a body bag. */
return (EPERM);
}
static void
sysdc_exit(kthread_t *t)
{
sysdc_t *sdc;
/* We're exiting, so we just rejoin the SYS class. */
thread_lock(t);
ASSERT(t->t_cid == sysdccid);
sdc = t->t_cldata;
t->t_cid = syscid;
t->t_cldata = NULL;
t->t_clfuncs = &(sclass[syscid].cl_funcs->thread);
(void) thread_change_pri(t, maxclsyspri, 0);
t->t_schedflag &= ~TS_RUNQMATCH;
thread_unlock_nopreempt(t);
/* Unlink the sdc from everything. */
sysdc_leave(sdc);
}
/*ARGSUSED*/
static int
sysdc_fork(kthread_t *t, kthread_t *ct, void *bufp)
{
/*
* Threads cannot be created with SDC as their class; they must
* be created as SYS and then added with sysdc_thread_enter().
* Because of this restriction, sysdc_fork() should never be called.
*/
panic("sysdc cannot be forked");
return (ENOSYS);
}
/*ARGSUSED*/
static void
sysdc_forkret(kthread_t *t, kthread_t *ct)
{
/* SDC threads are part of system processes, which never fork. */
panic("sysdc cannot be forked");
}
static pri_t
sysdc_globpri(kthread_t *t)
{
return (t->t_epri);
}
/*ARGSUSED*/
static pri_t
sysdc_no_swap(kthread_t *t, int flags)
{
/* SDC threads cannot be swapped. */
return (-1);
}
/*
* Get maximum and minimum priorities enjoyed by SDC threads.
*/
static int
sysdc_getclpri(pcpri_t *pcprip)
{
pcprip->pc_clpmax = sysdc_maxpri;
pcprip->pc_clpmin = sysdc_minpri;
return (0);
}
/*ARGSUSED*/
static int
sysdc_getclinfo(void *arg)
{
return (0); /* no class-specific info */
}
/*ARGSUSED*/
static int
sysdc_alloc(void **p, int flag)
{
sysdc_t *new;
*p = NULL;
if ((new = kmem_zalloc(sizeof (*new), flag)) == NULL) {
return (ENOMEM);
}
if ((new->sdc_pset = kmem_zalloc(sizeof (*new->sdc_pset), flag)) ==
NULL) {
kmem_free(new, sizeof (*new));
return (ENOMEM);
}
*p = new;
return (0);
}
static void
sysdc_free(void *p)
{
sysdc_t *sdc = p;
if (sdc != NULL) {
/*
* We must have failed CL_ENTERCLASS(), so our pset should be
* there and unused.
*/
ASSERT(sdc->sdc_pset != NULL);
ASSERT(sdc->sdc_pset->sdp_cpupart == NULL);
kmem_free(sdc->sdc_pset, sizeof (*sdc->sdc_pset));
kmem_free(sdc, sizeof (*sdc));
}
}
static int sysdc_enosys(); /* Boy, ANSI-C's K&R compatibility is weird. */
static int sysdc_einval();
static void sysdc_nullsys();
static struct classfuncs sysdc_classfuncs = {
/* messages to class manager */
{
sysdc_enosys, /* admin */
sysdc_getclinfo,
sysdc_enosys, /* parmsin */
sysdc_enosys, /* parmsout */
sysdc_enosys, /* vaparmsin */
sysdc_enosys, /* vaparmsout */
sysdc_getclpri,
sysdc_alloc,
sysdc_free,
},
/* operations on threads */
{
sysdc_enterclass,
sysdc_exitclass,
sysdc_canexit,
sysdc_fork,
sysdc_forkret,
sysdc_nullsys, /* parmsget */
sysdc_enosys, /* parmsset */
sysdc_nullsys, /* stop */
sysdc_exit,
sysdc_nullsys, /* active */
sysdc_nullsys, /* inactive */
sysdc_no_swap, /* swapin */
sysdc_no_swap, /* swapout */
sysdc_nullsys, /* trapret */
sysdc_preempt,
sysdc_setrun,
sysdc_sleep,
sysdc_tick,
sysdc_wakeup,
sysdc_einval, /* donice */
sysdc_globpri,
sysdc_nullsys, /* set_process_group */
sysdc_nullsys, /* yield */
sysdc_einval, /* doprio */
}
};
static int
sysdc_enosys()
{
return (ENOSYS);
}
static int
sysdc_einval()
{
return (EINVAL);
}
static void
sysdc_nullsys()
{
}
/*ARGSUSED*/
static pri_t
sysdc_init(id_t cid, int clparmsz, classfuncs_t **clfuncspp)
{
int idx;
list_create(&sysdc_psets, sizeof (sysdc_pset_t),
offsetof(sysdc_pset_t, sdp_node));
for (idx = 0; idx < SYSDC_NLISTS; idx++) {
sysdc_active[idx].sdl_list = &sysdc_dummy;
}
sysdc_initparam();
sysdccid = cid;
*clfuncspp = &sysdc_classfuncs;
return ((pri_t)v.v_maxsyspri);
}
static struct sclass csw = {
"SDC",
sysdc_init,
0
};
static struct modlsched modlsched = {
&mod_schedops, "system duty cycle scheduling class", &csw
};
static struct modlinkage modlinkage = {
MODREV_1, (void *)&modlsched, NULL
};
int
_init()
{
return (mod_install(&modlinkage));
}
int
_fini()
{
return (EBUSY); /* can't unload for now */
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
/* --- consolidation-private interfaces --- */
void
sysdc_thread_enter(kthread_t *t, uint_t dc, uint_t flags)
{
void *buf = NULL;
sysdc_params_t sdp;
SYSDC_INC_STAT(sysdc_thread_enter_enter);
ASSERT(sysdc_param_init);
ASSERT(sysdccid >= 0);
ASSERT((flags & ~SYSDC_THREAD_BATCH) == 0);
sdp.sdp_minpri = sysdc_minpri;
sdp.sdp_maxpri = sysdc_maxpri;
sdp.sdp_DC = MAX(MIN(dc, sysdc_maxDC), sysdc_minDC);
VERIFY0(CL_ALLOC(&buf, sysdccid, KM_SLEEP));
ASSERT(t->t_lwp != NULL);
ASSERT(t->t_cid == syscid);
ASSERT(t->t_cldata == NULL);
VERIFY0(CL_CANEXIT(t, NULL));
VERIFY0(CL_ENTERCLASS(t, sysdccid, &sdp, kcred, buf));
CL_EXITCLASS(syscid, NULL);
}
|