]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2009-2016 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
29 | #include <mach/mach_types.h> | |
30 | #include <mach/machine.h> | |
31 | #include <mach/policy.h> | |
32 | #include <mach/sync_policy.h> | |
33 | #include <mach/thread_act.h> | |
34 | ||
35 | #include <machine/machine_routines.h> | |
36 | #include <machine/sched_param.h> | |
37 | #include <machine/machine_cpu.h> | |
38 | ||
39 | #include <kern/kern_types.h> | |
40 | #include <kern/clock.h> | |
41 | #include <kern/counters.h> | |
42 | #include <kern/cpu_number.h> | |
43 | #include <kern/cpu_data.h> | |
44 | #include <kern/debug.h> | |
45 | #include <kern/macro_help.h> | |
46 | #include <kern/machine.h> | |
47 | #include <kern/misc_protos.h> | |
48 | #include <kern/processor.h> | |
49 | #include <kern/queue.h> | |
50 | #include <kern/sched.h> | |
51 | #include <kern/sched_prim.h> | |
52 | #include <kern/syscall_subr.h> | |
53 | #include <kern/task.h> | |
54 | #include <kern/thread.h> | |
55 | ||
56 | #include <vm/pmap.h> | |
57 | #include <vm/vm_kern.h> | |
58 | #include <vm/vm_map.h> | |
59 | ||
60 | #include <mach/sdt.h> | |
61 | ||
62 | #include <sys/kdebug.h> | |
63 | ||
64 | #if defined(CONFIG_SCHED_GRRR_CORE) | |
65 | ||
66 | static void | |
67 | grrr_priority_mapping_init(void); | |
68 | ||
69 | static boolean_t | |
70 | grrr_enqueue( | |
71 | grrr_run_queue_t rq, | |
72 | thread_t thread); | |
73 | ||
74 | static thread_t | |
75 | grrr_select( | |
76 | grrr_run_queue_t rq); | |
77 | ||
78 | static void | |
79 | grrr_remove( | |
80 | grrr_run_queue_t rq, | |
81 | thread_t thread); | |
82 | ||
83 | ||
84 | static void | |
85 | grrr_sorted_list_insert_group(grrr_run_queue_t rq, | |
86 | grrr_group_t group); | |
87 | ||
88 | static void | |
89 | grrr_rescale_work(grrr_run_queue_t rq); | |
90 | ||
91 | static void | |
92 | grrr_runqueue_init(grrr_run_queue_t runq); | |
93 | ||
94 | /* Map Mach priorities to ones suitable for proportional sharing */ | |
95 | static grrr_proportional_priority_t grrr_priority_mapping[NRQS]; | |
96 | ||
97 | /* Map each proportional priority to its group */ | |
98 | static grrr_group_index_t grrr_group_mapping[NUM_GRRR_PROPORTIONAL_PRIORITIES]; | |
99 | ||
100 | uint32_t grrr_rescale_tick; | |
101 | ||
102 | #endif /* defined(CONFIG_SCHED_GRRR_CORE) */ | |
103 | ||
104 | #if defined(CONFIG_SCHED_GRRR) | |
105 | ||
106 | static void | |
107 | sched_grrr_init(void); | |
108 | ||
109 | static void | |
110 | sched_grrr_timebase_init(void); | |
111 | ||
112 | static void | |
113 | sched_grrr_processor_init(processor_t processor); | |
114 | ||
115 | static void | |
116 | sched_grrr_pset_init(processor_set_t pset); | |
117 | ||
118 | static void | |
119 | sched_grrr_maintenance_continuation(void); | |
120 | ||
121 | static thread_t | |
122 | sched_grrr_choose_thread(processor_t processor, | |
123 | int priority, | |
124 | ast_t reason); | |
125 | ||
126 | static thread_t | |
127 | sched_grrr_steal_thread(processor_set_t pset); | |
128 | ||
129 | static int | |
130 | sched_grrr_compute_priority(thread_t thread); | |
131 | ||
132 | static processor_t | |
133 | sched_grrr_choose_processor( processor_set_t pset, | |
134 | processor_t processor, | |
135 | thread_t thread); | |
136 | ||
137 | static boolean_t | |
138 | sched_grrr_processor_enqueue( | |
139 | processor_t processor, | |
140 | thread_t thread, | |
141 | integer_t options); | |
142 | ||
143 | static void | |
144 | sched_grrr_processor_queue_shutdown( | |
145 | processor_t processor); | |
146 | ||
147 | static boolean_t | |
148 | sched_grrr_processor_queue_remove( | |
149 | processor_t processor, | |
150 | thread_t thread); | |
151 | ||
152 | static boolean_t | |
153 | sched_grrr_processor_queue_empty(processor_t processor); | |
154 | ||
155 | static boolean_t | |
156 | sched_grrr_processor_queue_has_priority(processor_t processor, | |
157 | int priority, | |
158 | boolean_t gte); | |
159 | ||
160 | static boolean_t | |
161 | sched_grrr_priority_is_urgent(int priority); | |
162 | ||
163 | static ast_t | |
164 | sched_grrr_processor_csw_check(processor_t processor); | |
165 | ||
166 | static uint32_t | |
167 | sched_grrr_initial_quantum_size(thread_t thread); | |
168 | ||
169 | static sched_mode_t | |
170 | sched_grrr_initial_thread_sched_mode(task_t parent_task); | |
171 | ||
172 | static boolean_t | |
173 | sched_grrr_can_update_priority(thread_t thread); | |
174 | ||
175 | static void | |
176 | sched_grrr_update_priority(thread_t thread); | |
177 | ||
178 | static void | |
179 | sched_grrr_lightweight_update_priority(thread_t thread); | |
180 | ||
181 | static int | |
182 | sched_grrr_processor_runq_count(processor_t processor); | |
183 | ||
184 | static uint64_t | |
185 | sched_grrr_processor_runq_stats_count_sum(processor_t processor); | |
186 | ||
187 | static int | |
188 | sched_grrr_processor_bound_count(processor_t processor); | |
189 | ||
190 | static void | |
191 | sched_grrr_thread_update_scan(sched_update_scan_context_t scan_context); | |
192 | ||
193 | const struct sched_dispatch_table sched_grrr_dispatch = { | |
194 | .sched_name = "grrr", | |
195 | .init = sched_grrr_init, | |
196 | .timebase_init = sched_grrr_timebase_init, | |
197 | .processor_init = sched_grrr_processor_init, | |
198 | .pset_init = sched_grrr_pset_init, | |
199 | .maintenance_continuation = sched_grrr_maintenance_continuation, | |
200 | .choose_thread = sched_grrr_choose_thread, | |
201 | .steal_thread_enabled = FALSE, | |
202 | .steal_thread = sched_grrr_steal_thread, | |
203 | .compute_timeshare_priority = sched_grrr_compute_priority, | |
204 | .choose_processor = sched_grrr_choose_processor, | |
205 | .processor_enqueue = sched_grrr_processor_enqueue, | |
206 | .processor_queue_shutdown = sched_grrr_processor_queue_shutdown, | |
207 | .processor_queue_remove = sched_grrr_processor_queue_remove, | |
208 | .processor_queue_empty = sched_grrr_processor_queue_empty, | |
209 | .priority_is_urgent = sched_grrr_priority_is_urgent, | |
210 | .processor_csw_check = sched_grrr_processor_csw_check, | |
211 | .processor_queue_has_priority = sched_grrr_processor_queue_has_priority, | |
212 | .initial_quantum_size = sched_grrr_initial_quantum_size, | |
213 | .initial_thread_sched_mode = sched_grrr_initial_thread_sched_mode, | |
214 | .can_update_priority = sched_grrr_can_update_priority, | |
215 | .update_priority = sched_grrr_update_priority, | |
216 | .lightweight_update_priority = sched_grrr_lightweight_update_priority, | |
217 | .quantum_expire = sched_default_quantum_expire, | |
218 | .processor_runq_count = sched_grrr_processor_runq_count, | |
219 | .processor_runq_stats_count_sum = sched_grrr_processor_runq_stats_count_sum, | |
220 | .processor_bound_count = sched_grrr_processor_bound_count, | |
221 | .thread_update_scan = sched_grrr_thread_update_scan, | |
222 | .direct_dispatch_to_idle_processors = TRUE, | |
223 | .multiple_psets_enabled = TRUE, | |
224 | .sched_groups_enabled = FALSE, | |
225 | .avoid_processor_enabled = FALSE, | |
226 | .thread_avoid_processor = NULL, | |
227 | .processor_balance = sched_SMT_balance, | |
228 | ||
229 | .rt_runq = sched_rtglobal_runq, | |
230 | .rt_init = sched_rtglobal_init, | |
231 | .rt_queue_shutdown = sched_rtglobal_queue_shutdown, | |
232 | .rt_runq_scan = sched_rtglobal_runq_scan, | |
233 | .rt_runq_count_sum = sched_rtglobal_runq_count_sum, | |
234 | ||
235 | .qos_max_parallelism = sched_qos_max_parallelism, | |
236 | .check_spill = sched_check_spill, | |
237 | .ipi_policy = sched_ipi_policy, | |
238 | .thread_should_yield = sched_thread_should_yield, | |
239 | }; | |
240 | ||
241 | extern int max_unsafe_quanta; | |
242 | ||
243 | static uint32_t grrr_quantum_us; | |
244 | static uint32_t grrr_quantum; | |
245 | ||
246 | static uint64_t sched_grrr_tick_deadline; | |
247 | ||
248 | static void | |
249 | sched_grrr_init(void) | |
250 | { | |
251 | if (default_preemption_rate < 1) | |
252 | default_preemption_rate = 100; | |
253 | grrr_quantum_us = (1000 * 1000) / default_preemption_rate; | |
254 | ||
255 | printf("standard grrr timeslicing quantum is %d us\n", grrr_quantum_us); | |
256 | ||
257 | grrr_priority_mapping_init(); | |
258 | } | |
259 | ||
260 | static void | |
261 | sched_grrr_timebase_init(void) | |
262 | { | |
263 | uint64_t abstime; | |
264 | ||
265 | /* standard timeslicing quantum */ | |
266 | clock_interval_to_absolutetime_interval( | |
267 | grrr_quantum_us, NSEC_PER_USEC, &abstime); | |
268 | assert((abstime >> 32) == 0 && (uint32_t)abstime != 0); | |
269 | grrr_quantum = (uint32_t)abstime; | |
270 | ||
271 | thread_depress_time = 1 * grrr_quantum; | |
272 | default_timeshare_computation = grrr_quantum / 2; | |
273 | default_timeshare_constraint = grrr_quantum; | |
274 | ||
275 | max_unsafe_computation = max_unsafe_quanta * grrr_quantum; | |
276 | sched_safe_duration = 2 * max_unsafe_quanta * grrr_quantum; | |
277 | ||
278 | } | |
279 | ||
280 | static void | |
281 | sched_grrr_processor_init(processor_t processor) | |
282 | { | |
283 | grrr_runqueue_init(&processor->grrr_runq); | |
284 | } | |
285 | ||
286 | static void | |
287 | sched_grrr_pset_init(processor_set_t pset __unused) | |
288 | { | |
289 | } | |
290 | ||
291 | static void | |
292 | sched_grrr_maintenance_continuation(void) | |
293 | { | |
294 | uint64_t abstime = mach_absolute_time(); | |
295 | ||
296 | grrr_rescale_tick++; | |
297 | ||
298 | /* | |
299 | * Compute various averages. | |
300 | */ | |
301 | compute_averages(1); | |
302 | ||
303 | if (sched_grrr_tick_deadline == 0) | |
304 | sched_grrr_tick_deadline = abstime; | |
305 | ||
306 | clock_deadline_for_periodic_event(10*sched_one_second_interval, abstime, | |
307 | &sched_grrr_tick_deadline); | |
308 | ||
309 | assert_wait_deadline((event_t)sched_grrr_maintenance_continuation, THREAD_UNINT, sched_grrr_tick_deadline); | |
310 | thread_block((thread_continue_t)sched_grrr_maintenance_continuation); | |
311 | /*NOTREACHED*/ | |
312 | } | |
313 | ||
314 | static thread_t | |
315 | sched_grrr_choose_thread(processor_t processor, | |
316 | int priority __unused, | |
317 | ast_t reason __unused) | |
318 | { | |
319 | grrr_run_queue_t rq = &processor->grrr_runq; | |
320 | ||
321 | return grrr_select(rq); | |
322 | } | |
323 | ||
324 | static thread_t | |
325 | sched_grrr_steal_thread(processor_set_t pset) | |
326 | { | |
327 | pset_unlock(pset); | |
328 | ||
329 | return THREAD_NULL; | |
330 | } | |
331 | ||
332 | static int | |
333 | sched_grrr_compute_priority(thread_t thread) | |
334 | { | |
335 | return thread->base_pri; | |
336 | } | |
337 | ||
338 | static processor_t | |
339 | sched_grrr_choose_processor( processor_set_t pset, | |
340 | processor_t processor, | |
341 | thread_t thread) | |
342 | { | |
343 | return choose_processor(pset, processor, thread); | |
344 | } | |
345 | ||
346 | static boolean_t | |
347 | sched_grrr_processor_enqueue( | |
348 | processor_t processor, | |
349 | thread_t thread, | |
350 | integer_t options __unused) | |
351 | { | |
352 | grrr_run_queue_t rq = &processor->grrr_runq; | |
353 | boolean_t result; | |
354 | ||
355 | result = grrr_enqueue(rq, thread); | |
356 | ||
357 | thread->runq = processor; | |
358 | ||
359 | return result; | |
360 | } | |
361 | ||
362 | static void | |
363 | sched_grrr_processor_queue_shutdown( | |
364 | processor_t processor) | |
365 | { | |
366 | processor_set_t pset = processor->processor_set; | |
367 | thread_t thread; | |
368 | queue_head_t tqueue, bqueue; | |
369 | ||
370 | queue_init(&tqueue); | |
371 | queue_init(&bqueue); | |
372 | ||
373 | while ((thread = sched_grrr_choose_thread(processor, IDLEPRI, AST_NONE)) != THREAD_NULL) { | |
374 | if (thread->bound_processor == PROCESSOR_NULL) { | |
375 | enqueue_tail(&tqueue, (queue_entry_t)thread); | |
376 | } else { | |
377 | enqueue_tail(&bqueue, (queue_entry_t)thread); | |
378 | } | |
379 | } | |
380 | ||
381 | while ((thread = (thread_t)(void *)dequeue_head(&bqueue)) != THREAD_NULL) { | |
382 | sched_grrr_processor_enqueue(processor, thread, SCHED_TAILQ); | |
383 | } | |
384 | ||
385 | pset_unlock(pset); | |
386 | ||
387 | while ((thread = (thread_t)(void *)dequeue_head(&tqueue)) != THREAD_NULL) { | |
388 | thread_lock(thread); | |
389 | ||
390 | thread_setrun(thread, SCHED_TAILQ); | |
391 | ||
392 | thread_unlock(thread); | |
393 | } | |
394 | } | |
395 | ||
396 | static boolean_t | |
397 | sched_grrr_processor_queue_remove( | |
398 | processor_t processor, | |
399 | thread_t thread) | |
400 | { | |
401 | processor_set_t pset = processor->processor_set; | |
402 | ||
403 | pset_lock(pset); | |
404 | ||
405 | if (processor == thread->runq) { | |
406 | /* | |
407 | * Thread is on a run queue and we have a lock on | |
408 | * that run queue. | |
409 | */ | |
410 | grrr_run_queue_t rq = &processor->grrr_runq; | |
411 | ||
412 | grrr_remove(rq, thread); | |
413 | } else { | |
414 | /* | |
415 | * The thread left the run queue before we could | |
416 | * lock the run queue. | |
417 | */ | |
418 | assert(thread->runq == PROCESSOR_NULL); | |
419 | processor = PROCESSOR_NULL; | |
420 | } | |
421 | ||
422 | pset_unlock(pset); | |
423 | ||
424 | return (processor != PROCESSOR_NULL); | |
425 | } | |
426 | ||
427 | static boolean_t | |
428 | sched_grrr_processor_queue_empty(processor_t processor __unused) | |
429 | { | |
430 | boolean_t result; | |
431 | ||
432 | result = (processor->grrr_runq.count == 0); | |
433 | ||
434 | return result; | |
435 | } | |
436 | ||
437 | static boolean_t | |
438 | sched_grrr_processor_queue_has_priority(processor_t processor, | |
439 | int priority, | |
440 | boolean_t gte __unused) | |
441 | { | |
442 | grrr_run_queue_t rq = &processor->grrr_runq; | |
443 | unsigned int i; | |
444 | ||
445 | i = grrr_group_mapping[grrr_priority_mapping[priority]]; | |
446 | for ( ; i < NUM_GRRR_GROUPS; i++) { | |
447 | if (rq->groups[i].count > 0) | |
448 | return TRUE; | |
449 | } | |
450 | ||
451 | return FALSE; | |
452 | } | |
453 | ||
454 | /* Implement sched_preempt_pri in code */ | |
455 | static boolean_t | |
456 | sched_grrr_priority_is_urgent(int priority) | |
457 | { | |
458 | if (priority <= BASEPRI_FOREGROUND) | |
459 | return FALSE; | |
460 | ||
461 | if (priority < MINPRI_KERNEL) | |
462 | return TRUE; | |
463 | ||
464 | if (priority >= BASEPRI_PREEMPT) | |
465 | return TRUE; | |
466 | ||
467 | return FALSE; | |
468 | } | |
469 | ||
470 | static ast_t | |
471 | sched_grrr_processor_csw_check(processor_t processor) | |
472 | { | |
473 | int count; | |
474 | ||
475 | count = sched_grrr_processor_runq_count(processor); | |
476 | ||
477 | if (count > 0) | |
478 | return AST_PREEMPT; | |
479 | ||
480 | return AST_NONE; | |
481 | } | |
482 | ||
483 | static uint32_t | |
484 | sched_grrr_initial_quantum_size(thread_t thread __unused) | |
485 | { | |
486 | return grrr_quantum; | |
487 | } | |
488 | ||
489 | static sched_mode_t | |
490 | sched_grrr_initial_thread_sched_mode(task_t parent_task) | |
491 | { | |
492 | if (parent_task == kernel_task) | |
493 | return TH_MODE_FIXED; | |
494 | else | |
495 | return TH_MODE_TIMESHARE; | |
496 | } | |
497 | ||
498 | static boolean_t | |
499 | sched_grrr_can_update_priority(thread_t thread __unused) | |
500 | { | |
501 | return FALSE; | |
502 | } | |
503 | ||
504 | static void | |
505 | sched_grrr_update_priority(thread_t thread __unused) | |
506 | { | |
507 | return; | |
508 | } | |
509 | ||
510 | static void | |
511 | sched_grrr_lightweight_update_priority(thread_t thread __unused) | |
512 | { | |
513 | return; | |
514 | } | |
515 | ||
516 | static int | |
517 | sched_grrr_processor_runq_count(processor_t processor) | |
518 | { | |
519 | return processor->grrr_runq.count; | |
520 | } | |
521 | ||
522 | static uint64_t | |
523 | sched_grrr_processor_runq_stats_count_sum(processor_t processor) | |
524 | { | |
525 | return processor->grrr_runq.runq_stats.count_sum; | |
526 | } | |
527 | ||
528 | static int | |
529 | sched_grrr_processor_bound_count(__unused processor_t processor) | |
530 | { | |
531 | return 0; | |
532 | } | |
533 | ||
534 | static void | |
535 | sched_grrr_thread_update_scan(__unused sched_update_scan_context_t scan_context) | |
536 | { | |
537 | return; | |
538 | } | |
539 | ||
540 | #endif /* defined(CONFIG_SCHED_GRRR) */ | |
541 | ||
542 | #if defined(CONFIG_SCHED_GRRR_CORE) | |
543 | ||
544 | static void | |
545 | grrr_priority_mapping_init(void) | |
546 | { | |
547 | unsigned int i; | |
548 | ||
549 | /* Map 0->0 up to 10->20 */ | |
550 | for (i=0; i <= 10; i++) { | |
551 | grrr_priority_mapping[i] = 2*i; | |
552 | } | |
553 | ||
554 | /* Map user priorities 11->33 up to 51 -> 153 */ | |
555 | for (i=11; i <= 51; i++) { | |
556 | grrr_priority_mapping[i] = 3*i; | |
557 | } | |
558 | ||
559 | /* Map high priorities 52->180 up to 127->255 */ | |
560 | for (i=52; i <= 127; i++) { | |
561 | grrr_priority_mapping[i] = 128 + i; | |
562 | } | |
563 | ||
564 | for (i = 0; i < NUM_GRRR_PROPORTIONAL_PRIORITIES; i++) { | |
565 | ||
566 | #if 0 | |
567 | unsigned j, k; | |
568 | /* Calculate log(i); */ | |
569 | for (j=0, k=1; k <= i; j++, k *= 2); | |
570 | #endif | |
571 | ||
572 | /* Groups of 4 */ | |
573 | grrr_group_mapping[i] = i >> 2; | |
574 | } | |
575 | } | |
576 | ||
577 | static thread_t | |
578 | grrr_intragroup_schedule(grrr_group_t group) | |
579 | { | |
580 | thread_t thread; | |
581 | ||
582 | if (group->count == 0) { | |
583 | return THREAD_NULL; | |
584 | } | |
585 | ||
586 | thread = group->current_client; | |
587 | if (thread == THREAD_NULL) { | |
588 | thread = (thread_t)(void *)queue_first(&group->clients); | |
589 | } | |
590 | ||
591 | if (1 /* deficit */) { | |
592 | group->current_client = (thread_t)(void *)queue_next((queue_entry_t)thread); | |
593 | if (queue_end(&group->clients, (queue_entry_t)group->current_client)) { | |
594 | group->current_client = (thread_t)(void *)queue_first(&group->clients); | |
595 | } | |
596 | ||
597 | thread = group->current_client; | |
598 | } | |
599 | ||
600 | return thread; | |
601 | } | |
602 | ||
603 | static thread_t | |
604 | grrr_intergroup_schedule(grrr_run_queue_t rq) | |
605 | { | |
606 | thread_t thread; | |
607 | grrr_group_t group; | |
608 | ||
609 | if (rq->count == 0) { | |
610 | return THREAD_NULL; | |
611 | } | |
612 | ||
613 | group = rq->current_group; | |
614 | ||
615 | if (group == GRRR_GROUP_NULL) { | |
616 | group = (grrr_group_t)queue_first(&rq->sorted_group_list); | |
617 | } | |
618 | ||
619 | thread = grrr_intragroup_schedule(group); | |
620 | ||
621 | if ((group->work >= (UINT32_MAX-256)) || (rq->last_rescale_tick != grrr_rescale_tick)) { | |
622 | grrr_rescale_work(rq); | |
623 | } | |
624 | group->work++; | |
625 | ||
626 | if (queue_end(&rq->sorted_group_list, queue_next((queue_entry_t)group))) { | |
627 | /* last group, go back to beginning */ | |
628 | group = (grrr_group_t)queue_first(&rq->sorted_group_list); | |
629 | } else { | |
630 | grrr_group_t nextgroup = (grrr_group_t)queue_next((queue_entry_t)group); | |
631 | uint64_t orderleft, orderright; | |
632 | ||
633 | /* | |
634 | * The well-ordering condition for intergroup selection is: | |
635 | * | |
636 | * (group->work+1) / (nextgroup->work+1) > (group->weight) / (nextgroup->weight) | |
637 | * | |
638 | * Multiply both sides by their denominators to avoid division | |
639 | * | |
640 | */ | |
641 | orderleft = (group->work + 1) * ((uint64_t)nextgroup->weight); | |
642 | orderright = (nextgroup->work + 1) * ((uint64_t)group->weight); | |
643 | if (orderleft > orderright) { | |
644 | group = nextgroup; | |
645 | } else { | |
646 | group = (grrr_group_t)queue_first(&rq->sorted_group_list); | |
647 | } | |
648 | } | |
649 | ||
650 | rq->current_group = group; | |
651 | ||
652 | return thread; | |
653 | } | |
654 | ||
655 | static void | |
656 | grrr_runqueue_init(grrr_run_queue_t runq) | |
657 | { | |
658 | grrr_group_index_t index; | |
659 | ||
660 | runq->count = 0; | |
661 | ||
662 | for (index = 0; index < NUM_GRRR_GROUPS; index++) { | |
663 | unsigned int prisearch; | |
664 | ||
665 | for (prisearch = 0; | |
666 | prisearch < NUM_GRRR_PROPORTIONAL_PRIORITIES; | |
667 | prisearch++) { | |
668 | if (grrr_group_mapping[prisearch] == index) { | |
669 | runq->groups[index].minpriority = (grrr_proportional_priority_t)prisearch; | |
670 | break; | |
671 | } | |
672 | } | |
673 | ||
674 | runq->groups[index].index = index; | |
675 | ||
676 | queue_init(&runq->groups[index].clients); | |
677 | runq->groups[index].count = 0; | |
678 | runq->groups[index].weight = 0; | |
679 | runq->groups[index].work = 0; | |
680 | runq->groups[index].current_client = THREAD_NULL; | |
681 | } | |
682 | ||
683 | queue_init(&runq->sorted_group_list); | |
684 | runq->weight = 0; | |
685 | runq->current_group = GRRR_GROUP_NULL; | |
686 | } | |
687 | ||
688 | static void | |
689 | grrr_rescale_work(grrr_run_queue_t rq) | |
690 | { | |
691 | grrr_group_index_t index; | |
692 | ||
693 | /* avoid overflow by scaling by 1/8th */ | |
694 | for (index = 0; index < NUM_GRRR_GROUPS; index++) { | |
695 | rq->groups[index].work >>= 3; | |
696 | } | |
697 | ||
698 | rq->last_rescale_tick = grrr_rescale_tick; | |
699 | } | |
700 | ||
701 | static boolean_t | |
702 | grrr_enqueue( | |
703 | grrr_run_queue_t rq, | |
704 | thread_t thread) | |
705 | { | |
706 | grrr_proportional_priority_t gpriority; | |
707 | grrr_group_index_t gindex; | |
708 | grrr_group_t group; | |
709 | ||
710 | gpriority = grrr_priority_mapping[thread->sched_pri]; | |
711 | gindex = grrr_group_mapping[gpriority]; | |
712 | group = &rq->groups[gindex]; | |
713 | ||
714 | #if 0 | |
715 | thread->grrr_deficit = 0; | |
716 | #endif | |
717 | ||
718 | if (group->count == 0) { | |
719 | /* Empty group, this is the first client */ | |
720 | enqueue_tail(&group->clients, (queue_entry_t)thread); | |
721 | group->count = 1; | |
722 | group->weight = gpriority; | |
723 | group->current_client = thread; | |
724 | } else { | |
725 | /* Insert before the current client */ | |
726 | if (group->current_client == THREAD_NULL || | |
727 | queue_first(&group->clients) == (queue_entry_t)group->current_client) { | |
728 | enqueue_head(&group->clients, (queue_entry_t)thread); | |
729 | } else { | |
730 | insque((queue_entry_t)thread, queue_prev((queue_entry_t)group->current_client)); | |
731 | } | |
732 | SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count); | |
733 | group->count++; | |
734 | group->weight += gpriority; | |
735 | ||
736 | /* Since there was already a client, this is on the per-processor sorted list already */ | |
737 | remqueue((queue_entry_t)group); | |
738 | } | |
739 | ||
740 | grrr_sorted_list_insert_group(rq, group); | |
741 | ||
742 | rq->count++; | |
743 | rq->weight += gpriority; | |
744 | ||
745 | return FALSE; | |
746 | } | |
747 | ||
748 | static thread_t | |
749 | grrr_select(grrr_run_queue_t rq) | |
750 | { | |
751 | thread_t thread; | |
752 | ||
753 | thread = grrr_intergroup_schedule(rq); | |
754 | if (thread != THREAD_NULL) { | |
755 | grrr_proportional_priority_t gpriority; | |
756 | grrr_group_index_t gindex; | |
757 | grrr_group_t group; | |
758 | ||
759 | gpriority = grrr_priority_mapping[thread->sched_pri]; | |
760 | gindex = grrr_group_mapping[gpriority]; | |
761 | group = &rq->groups[gindex]; | |
762 | ||
763 | remqueue((queue_entry_t)thread); | |
764 | SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count); | |
765 | group->count--; | |
766 | group->weight -= gpriority; | |
767 | if (group->current_client == thread) { | |
768 | group->current_client = THREAD_NULL; | |
769 | } | |
770 | ||
771 | remqueue((queue_entry_t)group); | |
772 | if (group->count == 0) { | |
773 | if (rq->current_group == group) { | |
774 | rq->current_group = GRRR_GROUP_NULL; | |
775 | } | |
776 | } else { | |
777 | /* Need to re-insert in sorted location */ | |
778 | grrr_sorted_list_insert_group(rq, group); | |
779 | } | |
780 | ||
781 | rq->count--; | |
782 | rq->weight -= gpriority; | |
783 | ||
784 | thread->runq = PROCESSOR_NULL; | |
785 | } | |
786 | ||
787 | return thread; | |
788 | } | |
789 | ||
790 | static void | |
791 | grrr_remove( | |
792 | grrr_run_queue_t rq, | |
793 | thread_t thread) | |
794 | { | |
795 | grrr_proportional_priority_t gpriority; | |
796 | grrr_group_index_t gindex; | |
797 | grrr_group_t group; | |
798 | ||
799 | gpriority = grrr_priority_mapping[thread->sched_pri]; | |
800 | gindex = grrr_group_mapping[gpriority]; | |
801 | group = &rq->groups[gindex]; | |
802 | ||
803 | remqueue((queue_entry_t)thread); | |
804 | SCHED_STATS_RUNQ_CHANGE(&rq->runq_stats, rq->count); | |
805 | group->count--; | |
806 | group->weight -= gpriority; | |
807 | if (group->current_client == thread) { | |
808 | group->current_client = THREAD_NULL; | |
809 | } | |
810 | ||
811 | remqueue((queue_entry_t)group); | |
812 | if (group->count == 0) { | |
813 | if (rq->current_group == group) { | |
814 | rq->current_group = GRRR_GROUP_NULL; | |
815 | } | |
816 | } else { | |
817 | /* Need to re-insert in sorted location */ | |
818 | grrr_sorted_list_insert_group(rq, group); | |
819 | } | |
820 | ||
821 | rq->count--; | |
822 | rq->weight -= gpriority; | |
823 | ||
824 | thread->runq = PROCESSOR_NULL; | |
825 | } | |
826 | ||
827 | static void | |
828 | grrr_sorted_list_insert_group(grrr_run_queue_t rq, | |
829 | grrr_group_t group) | |
830 | { | |
831 | /* Simple insertion sort */ | |
832 | if (queue_empty(&rq->sorted_group_list)) { | |
833 | enqueue_tail(&rq->sorted_group_list, (queue_entry_t)group); | |
834 | } else { | |
835 | grrr_group_t search_group; | |
836 | ||
837 | /* Start searching from the head (heaviest weight) for the first | |
838 | * element less than us, so we can insert before it | |
839 | */ | |
840 | search_group = (grrr_group_t)queue_first(&rq->sorted_group_list); | |
841 | while (!queue_end(&rq->sorted_group_list, (queue_entry_t)search_group) ) { | |
842 | ||
843 | if (search_group->weight < group->weight) { | |
844 | /* we should be before this */ | |
845 | search_group = (grrr_group_t)queue_prev((queue_entry_t)search_group); | |
846 | break; | |
847 | } if (search_group->weight == group->weight) { | |
848 | /* Use group index as a tie breaker */ | |
849 | if (search_group->index < group->index) { | |
850 | search_group = (grrr_group_t)queue_prev((queue_entry_t)search_group); | |
851 | break; | |
852 | } | |
853 | } | |
854 | ||
855 | /* otherwise, our weight is too small, keep going */ | |
856 | search_group = (grrr_group_t)queue_next((queue_entry_t)search_group); | |
857 | } | |
858 | ||
859 | if (queue_end(&rq->sorted_group_list, (queue_entry_t)search_group)) { | |
860 | enqueue_tail(&rq->sorted_group_list, (queue_entry_t)group); | |
861 | } else { | |
862 | insque((queue_entry_t)group, (queue_entry_t)search_group); | |
863 | } | |
864 | } | |
865 | } | |
866 | ||
867 | #endif /* defined(CONFIG_SCHED_GRRR_CORE) */ |