1 | /* $Id: sched-darwin.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Scheduling, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
32 | #include <mach/thread_act.h>
|
---|
33 | #include <mach/thread_policy.h>
|
---|
34 | #include <mach/thread_info.h>
|
---|
35 | #include <mach/host_info.h>
|
---|
36 | #include <mach/mach_init.h>
|
---|
37 | #include <mach/mach_host.h>
|
---|
38 | #include <sched.h>
|
---|
39 | #include <pthread.h>
|
---|
40 | #include <limits.h>
|
---|
41 | #include <errno.h>
|
---|
42 |
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include <iprt/log.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/err.h>
|
---|
47 | #include <iprt/asm.h>
|
---|
48 | #include <iprt/assert.h>
|
---|
49 | #include "internal/sched.h"
|
---|
50 | #include "internal/thread.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Structures and Typedefs *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | /**
|
---|
57 | * Configuration of one priority.
|
---|
58 | */
|
---|
59 | typedef struct
|
---|
60 | {
|
---|
61 | /** The priority. */
|
---|
62 | RTPROCPRIORITY enmPriority;
|
---|
63 | /** The name of this priority. */
|
---|
64 | const char *pszName;
|
---|
65 | /** Array scheduler attributes corresponding to each of the thread types. */
|
---|
66 | struct
|
---|
67 | {
|
---|
68 | /** For sanity include the array index. */
|
---|
69 | RTTHREADTYPE enmType;
|
---|
70 | /** The desired mach base_priority value. */
|
---|
71 | int iBasePriority;
|
---|
72 | /** The suggested priority value. (Same as iBasePriority seems to do the
|
---|
73 | * trick.) */
|
---|
74 | int iPriority;
|
---|
75 | } aTypes[RTTHREADTYPE_END];
|
---|
76 | } PROCPRIORITY;
|
---|
77 |
|
---|
78 |
|
---|
79 | /*********************************************************************************************************************************
|
---|
80 | * Global Variables *
|
---|
81 | *********************************************************************************************************************************/
|
---|
82 | /**
|
---|
83 | * Array of static priority configurations.
|
---|
84 | *
|
---|
85 | * ASSUMES that pthread_setschedparam takes a sched_priority argument in the
|
---|
86 | * range 0..127, which is translated into mach base_priority 0..63 and mach
|
---|
87 | * importance -31..32 (among other things). We also ASSUMES SCHED_OTHER.
|
---|
88 | *
|
---|
89 | * The base_priority range can be checked with tstDarwinSched, we're assuming it's
|
---|
90 | * 0..63 for user processes.
|
---|
91 | *
|
---|
92 | * Further we observe that fseventsd and mds both run at (mach) priority 50,
|
---|
93 | * while Finder runs at 47. At priority 63 we find the dynamic pager, the login
|
---|
94 | * window, UserEventAgent, SystemUIServer and coreaudiod. We do not wish to upset the
|
---|
95 | * dynamic pager, UI or audio, but we wish for I/O to not be bothered by spotlight
|
---|
96 | * (mds/fseventsd).
|
---|
97 | */
|
---|
98 | static const PROCPRIORITY g_aPriorities[] =
|
---|
99 | {
|
---|
100 | {
|
---|
101 | RTPROCPRIORITY_LOW, "Low",
|
---|
102 | {
|
---|
103 | { RTTHREADTYPE_INVALID, INT_MIN, INT_MIN },
|
---|
104 | { RTTHREADTYPE_INFREQUENT_POLLER, 20, 20 },
|
---|
105 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, 22, 22 },
|
---|
106 | { RTTHREADTYPE_EMULATION, 24, 24 },
|
---|
107 | { RTTHREADTYPE_DEFAULT, 28, 28 },
|
---|
108 | { RTTHREADTYPE_GUI, 29, 29 },
|
---|
109 | { RTTHREADTYPE_MAIN_WORKER, 30, 30 },
|
---|
110 | { RTTHREADTYPE_VRDP_IO, 31, 31 },
|
---|
111 | { RTTHREADTYPE_DEBUGGER, 31, 31 },
|
---|
112 | { RTTHREADTYPE_MSG_PUMP, 31, 31 },
|
---|
113 | { RTTHREADTYPE_IO, 31, 31 },
|
---|
114 | { RTTHREADTYPE_TIMER, 31, 31 }
|
---|
115 | }
|
---|
116 | },
|
---|
117 | {
|
---|
118 | RTPROCPRIORITY_NORMAL, "Normal",
|
---|
119 | {
|
---|
120 | { RTTHREADTYPE_INVALID, INT_MIN, INT_MIN },
|
---|
121 | { RTTHREADTYPE_INFREQUENT_POLLER, 29, 29 },
|
---|
122 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, 30, 30 },
|
---|
123 | { RTTHREADTYPE_EMULATION, 31, 31 }, /* the default priority */
|
---|
124 | { RTTHREADTYPE_DEFAULT, 32, 32 },
|
---|
125 | { RTTHREADTYPE_GUI, 32, 32 },
|
---|
126 | { RTTHREADTYPE_MAIN_WORKER, 32, 32 },
|
---|
127 | { RTTHREADTYPE_VRDP_IO, 39, 39 },
|
---|
128 | { RTTHREADTYPE_DEBUGGER, 42, 42 },
|
---|
129 | { RTTHREADTYPE_MSG_PUMP, 47, 47 },
|
---|
130 | { RTTHREADTYPE_IO, 52, 52 },
|
---|
131 | { RTTHREADTYPE_TIMER, 55, 55 }
|
---|
132 | }
|
---|
133 | },
|
---|
134 | {
|
---|
135 | RTPROCPRIORITY_HIGH, "High",
|
---|
136 | {
|
---|
137 | { RTTHREADTYPE_INVALID, INT_MIN, INT_MIN },
|
---|
138 | { RTTHREADTYPE_INFREQUENT_POLLER, 30, 30 },
|
---|
139 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, 31, 31 },
|
---|
140 | { RTTHREADTYPE_EMULATION, 32, 32 },
|
---|
141 | { RTTHREADTYPE_DEFAULT, 40, 40 },
|
---|
142 | { RTTHREADTYPE_GUI, 41, 41 },
|
---|
143 | { RTTHREADTYPE_MAIN_WORKER, 43, 43 },
|
---|
144 | { RTTHREADTYPE_VRDP_IO, 45, 45 },
|
---|
145 | { RTTHREADTYPE_DEBUGGER, 47, 47 },
|
---|
146 | { RTTHREADTYPE_MSG_PUMP, 49, 49 },
|
---|
147 | { RTTHREADTYPE_IO, 57, 57 },
|
---|
148 | { RTTHREADTYPE_TIMER, 61, 61 }
|
---|
149 | }
|
---|
150 | },
|
---|
151 | /* last */
|
---|
152 | {
|
---|
153 | RTPROCPRIORITY_FLAT, "Flat",
|
---|
154 | {
|
---|
155 | { RTTHREADTYPE_INVALID, INT_MIN, INT_MIN },
|
---|
156 | { RTTHREADTYPE_INFREQUENT_POLLER, 31, 31 },
|
---|
157 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, 31, 31 },
|
---|
158 | { RTTHREADTYPE_EMULATION, 31, 31 },
|
---|
159 | { RTTHREADTYPE_DEFAULT, 31, 31 },
|
---|
160 | { RTTHREADTYPE_GUI, 31, 31 },
|
---|
161 | { RTTHREADTYPE_MAIN_WORKER, 31, 31 },
|
---|
162 | { RTTHREADTYPE_VRDP_IO, 31, 31 },
|
---|
163 | { RTTHREADTYPE_DEBUGGER, 31, 31 },
|
---|
164 | { RTTHREADTYPE_MSG_PUMP, 31, 31 },
|
---|
165 | { RTTHREADTYPE_IO, 31, 31 },
|
---|
166 | { RTTHREADTYPE_TIMER, 31, 31 }
|
---|
167 | }
|
---|
168 | },
|
---|
169 | };
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * The dynamic default priority configuration.
|
---|
174 | *
|
---|
175 | * This can be recalulated at runtime depending on what the
|
---|
176 | * system allow us to do. Presently we don't do this as it seems
|
---|
177 | * Darwin generally lets us do whatever we want.
|
---|
178 | *
|
---|
179 | * @remarks this is the same as "Normal" above.
|
---|
180 | */
|
---|
181 | static PROCPRIORITY g_aDefaultPriority =
|
---|
182 | {
|
---|
183 | RTPROCPRIORITY_DEFAULT, "Default",
|
---|
184 | {
|
---|
185 | { RTTHREADTYPE_INVALID, INT_MIN, INT_MIN },
|
---|
186 | { RTTHREADTYPE_INFREQUENT_POLLER, 29, 29 },
|
---|
187 | { RTTHREADTYPE_MAIN_HEAVY_WORKER, 30, 30 },
|
---|
188 | { RTTHREADTYPE_EMULATION, 31, 31 }, /* the default priority */
|
---|
189 | { RTTHREADTYPE_DEFAULT, 32, 32 },
|
---|
190 | { RTTHREADTYPE_GUI, 32, 32 },
|
---|
191 | { RTTHREADTYPE_MAIN_WORKER, 32, 32 },
|
---|
192 | { RTTHREADTYPE_VRDP_IO, 39, 39 },
|
---|
193 | { RTTHREADTYPE_DEBUGGER, 42, 42 },
|
---|
194 | { RTTHREADTYPE_MSG_PUMP, 47, 47 },
|
---|
195 | { RTTHREADTYPE_IO, 52, 52 },
|
---|
196 | { RTTHREADTYPE_TIMER, 55, 55 }
|
---|
197 | }
|
---|
198 | };
|
---|
199 |
|
---|
200 |
|
---|
201 | /** Pointer to the current priority configuration. */
|
---|
202 | static const PROCPRIORITY *g_pProcessPriority = &g_aDefaultPriority;
|
---|
203 |
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Get's the priority information for the current thread.
|
---|
207 | *
|
---|
208 | * @returns The base priority
|
---|
209 | */
|
---|
210 | static int rtSchedDarwinGetBasePriority(void)
|
---|
211 | {
|
---|
212 | /* the base_priority. */
|
---|
213 | mach_msg_type_number_t Count = POLICY_TIMESHARE_INFO_COUNT;
|
---|
214 | struct policy_timeshare_info TSInfo = {0,0,0,0,0};
|
---|
215 | kern_return_t krc;
|
---|
216 | krc = thread_info(mach_thread_self(), THREAD_SCHED_TIMESHARE_INFO, (thread_info_t)&TSInfo, &Count);
|
---|
217 | Assert(krc == KERN_SUCCESS);
|
---|
218 |
|
---|
219 | return TSInfo.base_priority;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | DECLHIDDEN(int) rtSchedNativeCalcDefaultPriority(RTTHREADTYPE enmType)
|
---|
224 | {
|
---|
225 | Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Get the current priority.
|
---|
229 | */
|
---|
230 | int iBasePriority = rtSchedDarwinGetBasePriority();
|
---|
231 | Assert(iBasePriority >= 0 && iBasePriority <= 63);
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * If it doesn't match the default, select the closest one from the table.
|
---|
235 | */
|
---|
236 | int offBest = RT_ABS(g_pProcessPriority->aTypes[enmType].iBasePriority - iBasePriority);
|
---|
237 | if (offBest)
|
---|
238 | {
|
---|
239 | for (unsigned i = 0; i < RT_ELEMENTS(g_aPriorities); i++)
|
---|
240 | {
|
---|
241 | int off = RT_ABS(g_aPriorities[i].aTypes[enmType].iBasePriority - iBasePriority);
|
---|
242 | if (off < offBest)
|
---|
243 | {
|
---|
244 | g_pProcessPriority = &g_aPriorities[i];
|
---|
245 | if (!off)
|
---|
246 | break;
|
---|
247 | offBest = off;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | return VINF_SUCCESS;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | DECLHIDDEN(int) rtProcNativeSetPriority(RTPROCPRIORITY enmPriority)
|
---|
257 | {
|
---|
258 | Assert(enmPriority > RTPROCPRIORITY_INVALID && enmPriority < RTPROCPRIORITY_LAST);
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * No checks necessary, we assume we can set any priority in the user process range.
|
---|
262 | */
|
---|
263 | const PROCPRIORITY *pProcessPriority = &g_aDefaultPriority;
|
---|
264 | for (unsigned i = 0; i < RT_ELEMENTS(g_aPriorities); i++)
|
---|
265 | if (g_aPriorities[i].enmPriority == enmPriority)
|
---|
266 | {
|
---|
267 | pProcessPriority = &g_aPriorities[i];
|
---|
268 | break;
|
---|
269 | }
|
---|
270 | Assert(pProcessPriority != &g_aDefaultPriority);
|
---|
271 | ASMAtomicUoWritePtr(&g_pProcessPriority, pProcessPriority);
|
---|
272 |
|
---|
273 | return VINF_SUCCESS;
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
278 | {
|
---|
279 | Assert(pThread->Core.Key == pthread_self());
|
---|
280 | Assert(enmType > RTTHREADTYPE_INVALID && enmType < RTTHREADTYPE_END);
|
---|
281 | AssertMsg(g_pProcessPriority && g_pProcessPriority->aTypes[enmType].enmType == enmType,
|
---|
282 | ("enmType=%d entry=%d\n", enmType, g_pProcessPriority->aTypes[enmType].enmType));
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Get the current policy and params first since there are
|
---|
286 | * opaque members in the param structure and we don't wish to
|
---|
287 | * change the policy.
|
---|
288 | */
|
---|
289 | int iSchedPolicy = SCHED_OTHER;
|
---|
290 | struct sched_param SchedParam = {0, {0,0,0,0} };
|
---|
291 | int err = pthread_getschedparam((pthread_t)pThread->Core.Key, &iSchedPolicy, &SchedParam);
|
---|
292 | if (!err)
|
---|
293 | {
|
---|
294 | int const iDesiredBasePriority = g_pProcessPriority->aTypes[enmType].iBasePriority;
|
---|
295 | int iPriority = g_pProcessPriority->aTypes[enmType].iPriority;
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * First try with the given pthread priority number.
|
---|
299 | * Then make adjustments in case we missed the desired base priority (interface
|
---|
300 | * changed or whatever - its using an obsolete mach api).
|
---|
301 | */
|
---|
302 | SchedParam.sched_priority = iPriority;
|
---|
303 | err = pthread_setschedparam((pthread_t)pThread->Core.Key, iSchedPolicy, &SchedParam);
|
---|
304 | if (!err)
|
---|
305 | {
|
---|
306 | int i = 0;
|
---|
307 | int iBasePriority = rtSchedDarwinGetBasePriority();
|
---|
308 |
|
---|
309 | while (!err && iBasePriority < iDesiredBasePriority && i++ < 256)
|
---|
310 | {
|
---|
311 | SchedParam.sched_priority = ++iPriority;
|
---|
312 | err = pthread_setschedparam((pthread_t)pThread->Core.Key, iSchedPolicy, &SchedParam);
|
---|
313 | iBasePriority = rtSchedDarwinGetBasePriority();
|
---|
314 | }
|
---|
315 |
|
---|
316 | while (!err && iBasePriority > iDesiredBasePriority && i++ < 256)
|
---|
317 | {
|
---|
318 | SchedParam.sched_priority = --iPriority;
|
---|
319 | err = pthread_setschedparam((pthread_t)pThread->Core.Key, iSchedPolicy, &SchedParam);
|
---|
320 | iBasePriority = rtSchedDarwinGetBasePriority();
|
---|
321 | }
|
---|
322 | }
|
---|
323 | }
|
---|
324 | int rc = RTErrConvertFromErrno(err);
|
---|
325 | AssertMsgRC(rc, ("rc=%Rrc err=%d iSchedPolicy=%d sched_priority=%d\n",
|
---|
326 | rc, err, iSchedPolicy, SchedParam.sched_priority));
|
---|
327 | return rc;
|
---|
328 | }
|
---|
329 |
|
---|