31caeb97a7881302498266457ec8731b105562e1
[apple/xnu.git] / osfmk / kern / priority.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: clock_prim.c
54 * Author: Avadis Tevanian, Jr.
55 * Date: 1986
56 *
57 * Clock primitives.
58 */
59
60 #include <cpus.h>
61
62 #include <mach/boolean.h>
63 #include <mach/kern_return.h>
64 #include <mach/machine.h>
65 #include <kern/host.h>
66 #include <kern/mach_param.h>
67 #include <kern/sched.h>
68 #include <kern/spl.h>
69 #include <kern/thread.h>
70 #include <kern/processor.h>
71 #include <machine/machparam.h>
72
73 /*
74 * thread_quantum_expire:
75 *
76 * Recalculate the quantum and priority for a thread.
77 */
78
79 void
80 thread_quantum_expire(
81 timer_call_param_t p0,
82 timer_call_param_t p1)
83 {
84 register processor_t myprocessor = p0;
85 register thread_t thread = p1;
86 register processor_set_t pset;
87 spl_t s;
88
89 pset = myprocessor->processor_set;
90
91 /*
92 * Update set_quanta for timesharing.
93 */
94 pset->set_quanta = pset->machine_quanta[
95 (pset->runq.count > pset->processor_count) ?
96 pset->processor_count : pset->runq.count];
97
98 s = splsched();
99 thread_lock(thread);
100
101 /*
102 * Check for failsafe trip.
103 */
104 if (!(thread->sched_mode & TH_MODE_TIMESHARE)) {
105 extern uint64_t max_unsafe_computation;
106 uint64_t new_computation;
107
108 new_computation = myprocessor->quantum_end;
109 new_computation -= thread->computation_epoch;
110 if (new_computation + thread->metered_computation >
111 max_unsafe_computation) {
112 extern uint32_t sched_safe_duration;
113
114 if (thread->sched_mode & TH_MODE_REALTIME) {
115 if (thread->depress_priority < 0)
116 thread->priority = MINPRI;
117 else
118 thread->depress_priority = MINPRI;
119
120 thread->safe_mode |= TH_MODE_REALTIME;
121 thread->sched_mode &= ~TH_MODE_REALTIME;
122 }
123
124 thread->safe_release = sched_tick + sched_safe_duration;
125 thread->sched_mode |= (TH_MODE_FAILSAFE|TH_MODE_TIMESHARE);
126 }
127 }
128
129 /*
130 * Now recompute the priority of the thread if appropriate.
131 */
132 if (thread->sched_stamp != sched_tick)
133 update_priority(thread);
134 else
135 if ( (thread->sched_mode & TH_MODE_TIMESHARE) &&
136 thread->depress_priority < 0 ) {
137 thread_timer_delta(thread);
138 thread->sched_usage += thread->sched_delta;
139 thread->sched_delta = 0;
140 compute_my_priority(thread);
141 }
142
143 /*
144 * This quantum is up, give this thread another.
145 */
146 if (first_quantum(myprocessor))
147 myprocessor->slice_quanta--;
148
149 thread->current_quantum = (thread->sched_mode & TH_MODE_REALTIME)?
150 thread->realtime.computation: std_quantum;
151 myprocessor->quantum_end += thread->current_quantum;
152 timer_call_enter1(&myprocessor->quantum_timer,
153 thread, myprocessor->quantum_end);
154
155 thread_unlock(thread);
156 splx(s);
157
158 /*
159 * Check for and schedule ast if needed.
160 */
161 ast_check();
162 }