VirtualBox

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

Last change on this file since 58063 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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