VirtualBox

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

最後變更 在這個檔案從23295是 15821,由 vboxsync 提交於 16 年 前

tstDarwinSched: experiment with darwin scheduling.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 7.9 KB
 
1
2/*******************************************************************************
3* Header Files *
4*******************************************************************************/
5#include <mach/thread_act.h>
6#include <mach/thread_policy.h>
7#include <mach/thread_info.h>
8#include <mach/host_info.h>
9#include <mach/mach_init.h>
10#include <mach/mach_host.h>
11#include <pthread.h>
12#include <sched.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17
18static void thread_print_policies(int fDefault)
19{
20 thread_extended_policy_data_t Extended = { 0 };
21 thread_time_constraint_policy_data_t TimeConstraint = { 0, 0, 0, 1 };
22 thread_precedence_policy_data_t Precedence = { 0 };
23#ifdef THREAD_AFFINITY_POLICY /* 10.5 */
24 thread_affinity_policy_data_t Affinity = { 0 };
25#endif
26 boolean_t GetDefault;
27 mach_msg_type_number_t Count;
28 kern_return_t krc;
29
30 GetDefault = fDefault;
31 Count = THREAD_EXTENDED_POLICY_COUNT;
32 krc = thread_policy_get(mach_thread_self(), THREAD_EXTENDED_POLICY, (thread_policy_t)&Extended, &Count, &GetDefault);
33 printf("THREAD_EXTENDED_POLICY: krc=%#x default=%d timeshare=%d (%#x)\n",
34 krc, GetDefault, Extended.timeshare, Extended.timeshare);
35
36 GetDefault = fDefault;
37 Count = THREAD_PRECEDENCE_POLICY_COUNT;
38 krc = thread_policy_get(mach_thread_self(), THREAD_PRECEDENCE_POLICY, (thread_policy_t)&Precedence, &Count, &GetDefault);
39 printf("THREAD_PRECEDENCE_POLICY: krc=%#x default=%d importance=%d (%#x)\n",
40 krc, GetDefault, Precedence.importance, Precedence.importance);
41
42 GetDefault = fDefault;
43 Count = THREAD_TIME_CONSTRAINT_POLICY_COUNT;
44 krc = thread_policy_get(mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY, (thread_policy_t)&TimeConstraint, &Count, &GetDefault);
45 printf("THREAD_TIME_CONSTRAINT_POLICY: krc=%#x default=%d period=%u (%#x) computation=%u (%#x) constraint=%u (%#x) preemptible=%d\n",
46 krc, GetDefault, TimeConstraint.period, TimeConstraint.period,
47 TimeConstraint.computation, TimeConstraint.computation,
48 TimeConstraint.constraint, TimeConstraint.constraint,
49 TimeConstraint.preemptible);
50
51#ifdef THREAD_AFFINITY_POLICY /* 10.5 */
52 GetDefault = fDefault;
53 Count = THREAD_AFFINITY_POLICY_COUNT;
54 krc = thread_policy_get(mach_thread_self(), THREAD_AFFINITY_POLICY, (thread_policy_t)&Affinity, &Count, &GetDefault);
55 printf("THREAD_AFFINITY_POLICY: krc=%#x default=%d affinity_tag=%d (%#x)\n",
56 krc, GetDefault, Affinity.affinity_tag, Affinity.affinity_tag);
57#endif
58
59 if (!fDefault)
60 {
61 struct sched_param Param;
62 int iPolicy = 0;
63 struct thread_basic_info BasicInfo = {{0,0},{0,0},0,0,0,0,0,0};
64 struct policy_timeshare_info TSInfo = {0,0,0,0,0};
65 int rc;
66 int i;
67
68 memset(&Param, 0, sizeof(Param));
69 rc = pthread_getschedparam(pthread_self(), &iPolicy, &Param);
70 printf("pthread_getschedparam: rc=%d iPolicy=%d (%#x) sched_priority=%d (%#x) opaque=%d (%#x)\n",
71 rc, iPolicy, iPolicy, Param.sched_priority, Param.sched_priority,
72#ifdef THREAD_AFFINITY_POLICY /* 10.5 */
73 *(int *)&Param.__opaque, *(int *)&Param.__opaque);
74#else
75 *(int *)&Param.opaque, *(int *)&Param.opaque);
76#endif
77
78 Count = THREAD_BASIC_INFO_COUNT;
79 krc = thread_info(mach_thread_self(), THREAD_BASIC_INFO, (thread_info_t)&BasicInfo, &Count);
80 printf("THREAD_BASIC_INFO: krc=%#x user_time=%d.%06d system_time=%d.%06d cpu_usage=%d policy=%d\n"
81 " run_state=%d flags=%#x suspend_count=%d sleep_time=%d\n",
82 krc,
83 BasicInfo.user_time.seconds, BasicInfo.user_time.microseconds,
84 BasicInfo.system_time.seconds, BasicInfo.system_time.microseconds,
85 BasicInfo.cpu_usage,
86 BasicInfo.policy,
87 BasicInfo.run_state,
88 BasicInfo.flags,
89 BasicInfo.suspend_count,
90 BasicInfo.sleep_time);
91
92 Count = POLICY_TIMESHARE_INFO_COUNT;
93 krc = thread_info(mach_thread_self(), THREAD_SCHED_TIMESHARE_INFO, (thread_info_t)&TSInfo, &Count);
94 printf("THREAD_SCHED_TIMESHARE_INFO: krc=%#x max_priority=%d (%#x) base_priority=%d (%#x) cur_priority=%d (%#x)\n"
95 " depressed=%d depress_priority=%d (%#x)\n",
96 krc,
97 TSInfo.max_priority, TSInfo.max_priority,
98 TSInfo.base_priority, TSInfo.base_priority,
99 TSInfo.cur_priority, TSInfo.cur_priority,
100 TSInfo.depressed,
101 TSInfo.depress_priority, TSInfo.depress_priority);
102 }
103 else
104 {
105 host_priority_info_data_t PriorityInfo = {0,0,0,0,0,0,0,0};
106
107 Count = HOST_PRIORITY_INFO_COUNT;
108 krc = host_info(mach_host_self(), HOST_PRIORITY_INFO, (host_info_t)&PriorityInfo, &Count);
109 printf("HOST_PRIORITY_INFO: krc=%#x \n"
110 " kernel_priority=%2d (%#x)\n"
111 " system_priority=%2d (%#x)\n"
112 " server_priority=%2d (%#x)\n"
113 " user_priority=%2d (%#x)\n"
114 " depress_priority=%2d (%#x)\n"
115 " idle_priority=%2d (%#x)\n"
116 " minimum_priority=%2d (%#x)\n"
117 " maximum_priority=%2d (%#x)\n",
118 krc,
119 PriorityInfo.kernel_priority, PriorityInfo.kernel_priority,
120 PriorityInfo.system_priority, PriorityInfo.system_priority,
121 PriorityInfo.server_priority, PriorityInfo.server_priority,
122 PriorityInfo.user_priority, PriorityInfo.user_priority,
123 PriorityInfo.depress_priority, PriorityInfo.depress_priority,
124 PriorityInfo.idle_priority, PriorityInfo.idle_priority,
125 PriorityInfo.minimum_priority, PriorityInfo.minimum_priority,
126 PriorityInfo.maximum_priority, PriorityInfo.maximum_priority);
127 }
128}
129
130int main()
131{
132 struct sched_param Param;
133 int iPolicy;
134 int iPriority;
135 int rc;
136
137 printf("tstDarwinSched: Default policies:\n");
138 thread_print_policies(1);
139
140 printf("tstDarwinSched: Current policies:\n");
141 thread_print_policies(0);
142
143
144 printf("tstDarwinSched:\n");
145 printf("tstDarwinSched: Trying max priority using pthread API\n");
146 iPolicy = SCHED_OTHER;
147 memset(&Param, 0, sizeof(Param));
148 pthread_getschedparam(pthread_self(), &iPolicy, &Param);
149 Param.sched_priority = iPriority = sched_get_priority_max(iPolicy);
150 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
151 if (!rc)
152 {
153 do
154 {
155 Param.sched_priority = ++iPriority;
156 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
157 } while (!rc);
158 iPriority--;
159 rc = 0;
160 }
161 printf("tstDarwinSched: pthread_setschedparam(iPriority=%d [max=%d]) -> %d\n",
162 iPriority, sched_get_priority_max(iPolicy), rc);
163 thread_print_policies(0);
164
165
166 printf("tstDarwinSched:\n");
167 printf("tstDarwinSched: Trying min priority using pthread API\n");
168 iPolicy = SCHED_OTHER;
169 memset(&Param, 0, sizeof(Param));
170 pthread_getschedparam(pthread_self(), &iPolicy, &Param);
171 Param.sched_priority = iPriority = sched_get_priority_min(iPolicy);
172 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
173 if (!rc)
174 {
175 do
176 {
177 Param.sched_priority = --iPriority;
178 rc = pthread_setschedparam(pthread_self(), iPolicy, &Param);
179 } while (!rc);
180 iPriority++;
181 rc = 0;
182 }
183 printf("tstDarwinSched: pthread_setschedparam(iPriority=%d [min=%d]) -> %d\n",
184 iPriority, sched_get_priority_min(iPolicy), rc);
185 thread_print_policies(0);
186
187
188 return 0;
189}
190
191
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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