VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstDarwinSched.cpp@ 102792

Last change on this file since 102792 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1/* $Id: tstDarwinSched.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT testcase - darwin scheduling.
4 */
5
6/*
7 * Copyright (C) 2009-2023 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#include <mach/thread_act.h>
42#include <mach/thread_policy.h>
43#include <mach/thread_info.h>
44#include <mach/host_info.h>
45#include <mach/mach_init.h>
46#include <mach/mach_host.h>
47#include <pthread.h>
48#include <sched.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53
54static void thread_print_policies(int fDefault)
55{
56 thread_extended_policy_data_t Extended = { 0 };
57 thread_time_constraint_policy_data_t TimeConstraint = { 0, 0, 0, 1 };
58 thread_precedence_policy_data_t Precedence = { 0 };
59#ifdef THREAD_AFFINITY_POLICY /* 10.5 */
60 thread_affinity_policy_data_t Affinity = { 0 };
61#endif
62 boolean_t GetDefault;
63 mach_msg_type_number_t Count;
64 kern_return_t krc;
65
66 GetDefault = fDefault;
67 Count = THREAD_EXTENDED_POLICY_COUNT;
68 krc = thread_policy_get(mach_thread_self(), THREAD_EXTENDED_POLICY, (thread_policy_t)&Extended, &Count, &GetDefault);
69 printf("THREAD_EXTENDED_POLICY: krc=%#x default=%d timeshare=%d (%#x)\n",
70 krc, GetDefault, Extended.timeshare, Extended.timeshare);
71
72 GetDefault = fDefault;
73 Count = THREAD_PRECEDENCE_POLICY_COUNT;
74 krc = thread_policy_get(mach_thread_self(), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&Precedence, &Count, &GetDefault);
75 printf("THREAD_PRECEDENCE_POLICY: krc=%#x default=%d importance=%d (%#x)\n",
76 krc, GetDefault, Precedence.importance, Precedence.importance);
77
78 GetDefault = fDefault;
79 Count = THREAD_TIME_CONSTRAINT_POLICY_COUNT;
80 krc = thread_policy_get(mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&TimeConstraint, &Count, &GetDefault);
81 printf("THREAD_TIME_CONSTRAINT_POLICY: krc=%#x default=%d period=%u (%#x) computation=%u (%#x) constraint=%u (%#x) preemptible=%d\n",
82 krc, GetDefault, TimeConstraint.period, TimeConstraint.period,
83 TimeConstraint.computation, TimeConstraint.computation,
84 TimeConstraint.constraint, TimeConstraint.constraint,
85 TimeConstraint.preemptible);
86
87#ifdef THREAD_AFFINITY_POLICY /* 10.5 */
88 GetDefault = fDefault;
89 Count = THREAD_AFFINITY_POLICY_COUNT;
90 krc = thread_policy_get(mach_thread_self(), THREAD_AFFINITY_POLICY, (thread_policy_t)&Affinity, &Count, &GetDefault);
91 printf("THREAD_AFFINITY_POLICY: krc=%#x default=%d affinity_tag=%d (%#x)\n",
92 krc, GetDefault, Affinity.affinity_tag, Affinity.affinity_tag);
93#endif
94
95 if (!fDefault)
96 {
97 struct sched_param Param;
98 int iPolicy = 0;
99 struct thread_basic_info BasicInfo = {{0,0},{0,0},0,0,0,0,0,0};
100 struct policy_timeshare_info TSInfo = {0,0,0,0,0};
101 int rc;
102
103 memset(&Param, 0, sizeof(Param));
104 rc = pthread_getschedparam(pthread_self(), &iPolicy, &Param);
105 printf("pthread_getschedparam: rc=%d iPolicy=%d (%#x) sched_priority=%d (%#x) opaque=%d (%#x)\n",
106 rc, iPolicy, iPolicy, Param.sched_priority, Param.sched_priority,
107#ifdef THREAD_AFFINITY_POLICY /* 10.5 */
108 *(int *)&Param.__opaque, *(int *)&Param.__opaque);
109#else
110 *(int *)&Param.opaque, *(int *)&Param.opaque);
111#endif
112
113 Count = THREAD_BASIC_INFO_COUNT;
114 krc = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&BasicInfo, &Count);
115 printf("THREAD_BASIC_INFO: krc=%#x user_time=%d.%06d system_time=%d.%06d cpu_usage=%d policy=%d\n"
116 " run_state=%d flags=%#x suspend_count=%d sleep_time=%d\n",
117 krc,
118 BasicInfo.user_time.seconds, BasicInfo.user_time.microseconds,
119 BasicInfo.system_time.seconds, BasicInfo.system_time.microseconds,
120 BasicInfo.cpu_usage,
121 BasicInfo.policy,
122 BasicInfo.run_state,
123 BasicInfo.flags,
124 BasicInfo.suspend_count,
125 BasicInfo.sleep_time);
126
127 Count = POLICY_TIMESHARE_INFO_COUNT;
128 krc = thread_info(mach_thread_self(), THREAD_SCHED_TIMESHARE_INFO, (thread_info_t)&TSInfo, &Count);
129 printf("THREAD_SCHED_TIMESHARE_INFO: krc=%#x max_priority=%d (%#x) base_priority=%d (%#x) cur_priority=%d (%#x)\n"
130 " depressed=%d depress_priority=%d (%#x)\n",
131 krc,
132 TSInfo.max_priority, TSInfo.max_priority,
133 TSInfo.base_priority, TSInfo.base_priority,
134 TSInfo.cur_priority, TSInfo.cur_priority,
135 TSInfo.depressed,
136 TSInfo.depress_priority, TSInfo.depress_priority);
137 }
138 else
139 {
140 host_priority_info_data_t PriorityInfo = {0,0,0,0,0,0,0,0};
141
142 Count = HOST_PRIORITY_INFO_COUNT;
143 krc = host_info(mach_host_self(), HOST_PRIORITY_INFO, (host_info_t)&PriorityInfo, &Count);
144 printf("HOST_PRIORITY_INFO: krc=%#x \n"
145 " kernel_priority=%2d (%#x)\n"
146 " system_priority=%2d (%#x)\n"
147 " server_priority=%2d (%#x)\n"
148 " user_priority=%2d (%#x)\n"
149 " depress_priority=%2d (%#x)\n"
150 " idle_priority=%2d (%#x)\n"
151 " minimum_priority=%2d (%#x)\n"
152 " maximum_priority=%2d (%#x)\n",
153 krc,
154 PriorityInfo.kernel_priority, PriorityInfo.kernel_priority,
155 PriorityInfo.system_priority, PriorityInfo.system_priority,
156 PriorityInfo.server_priority, PriorityInfo.server_priority,
157 PriorityInfo.user_priority, PriorityInfo.user_priority,
158 PriorityInfo.depress_priority, PriorityInfo.depress_priority,
159 PriorityInfo.idle_priority, PriorityInfo.idle_priority,
160 PriorityInfo.minimum_priority, PriorityInfo.minimum_priority,
161 PriorityInfo.maximum_priority, PriorityInfo.maximum_priority);
162 }
163}
164
165int main()
166{
167 struct sched_param Param;
168 int iPolicy;
169 int iPriority;
170 int rc;
171
172 printf("tstDarwinSched: Default policies:\n");
173 thread_print_policies(1);
174
175 printf("tstDarwinSched: Current policies:\n");
176 thread_print_policies(0);
177
178
179 printf("tstDarwinSched:\n");
180 printf("tstDarwinSched: Trying max priority using pthread API\n");
181 iPolicy = SCHED_OTHER;
182 memset(&Param, 0, sizeof(Param));
183 pthread_getschedparam(pthread_self(), &iPolicy, &Param);
184 Param.sched_priority = iPriority = sched_get_priority_max(iPolicy);
185 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
186 if (!rc)
187 {
188 do
189 {
190 Param.sched_priority = ++iPriority;
191 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
192 } while (!rc);
193 iPriority--;
194 rc = 0;
195 }
196 printf("tstDarwinSched: pthread_setschedparam(iPriority=%d [max=%d]) -> %d\n",
197 iPriority, sched_get_priority_max(iPolicy), rc);
198 thread_print_policies(0);
199
200
201 printf("tstDarwinSched:\n");
202 printf("tstDarwinSched: Trying min priority using pthread API\n");
203 iPolicy = SCHED_OTHER;
204 memset(&Param, 0, sizeof(Param));
205 pthread_getschedparam(pthread_self(), &iPolicy, &Param);
206 Param.sched_priority = iPriority = sched_get_priority_min(iPolicy);
207 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
208 if (!rc)
209 {
210 do
211 {
212 Param.sched_priority = --iPriority;
213 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
214 } while (!rc);
215 iPriority++;
216 rc = 0;
217 }
218 printf("tstDarwinSched: pthread_setschedparam(iPriority=%d [min=%d]) -> %d\n",
219 iPriority, sched_get_priority_min(iPolicy), rc);
220 thread_print_policies(0);
221
222
223 return 0;
224}
225
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette