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