1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
// $Id$
// ============================================================================
//
// = LIBRARY
// performance-tests/Misc
//
// = FILENAME
// preempt.cpp
//
// = DESCRIPTION
// This is a simple test to illustrate OS thread preemption. One
// ore more high priority threads periodically (every half
// second, by default) reads the clock. They use select () to
// block for that duration. Meanwhile, a low priority thread
// continually chews up the CPU. Without preemption, the high
// priority thread won't have a chance to read the clock in a
// timely manner.
//
// At the end of the test, the actual clock read intervals by the
// high priority task(s) are printed out. With proper
// preemption, the intervals should correspond to the requested
// clock read interval.
//
// There is a -y option for the low priority thread to periodically
// yield. It shouldn't be necessary to use that option, if preemption
// is supported. It's a handy option for testing.
//
// = AUTHOR
// David L. Levine
//
// ============================================================================
#include "ace/Task.h"
#include "ace/Sched_Params.h"
#include "ace/Get_Opt.h"
#if defined (ACE_HAS_THREADS)
static const char usage [] = "[-? |\n"
" [-c <iterations, default 10>]\n"
" [-n <high priority threads, default 1>\n"
" [-p <read period, default 500000 usec>\n"
" [-y to yield the low priority thread\n"
"[<iterations>]]";
// Configuration options.
u_int iterations = 10;
u_int high_priority_tasks = 1;
u_long read_period = 500000; /* usec */
u_int low_yield = 0;
// To permit calculation of relative times.
ACE_hrtime_t starttime;
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class High_Priority_Task
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
class High_Priority_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
High_Priority_Task (void);
~High_Priority_Task (void);
int open (void *);
int svc (void);
int done () const { return done_; }
void print_times () const;
private:
int priority_;
int done_;
u_long *time_;
};
High_Priority_Task::High_Priority_Task (void)
: ACE_Task<ACE_MT_SYNCH> (ACE_Thread_Manager::instance ()),
priority_ (ACE_Sched_Params::next_priority (
ACE_SCHED_FIFO,
ACE_Sched_Params::priority_min (ACE_SCHED_FIFO,
ACE_SCOPE_THREAD),
ACE_SCOPE_THREAD)),
done_ (0)
{
ACE_NEW (time_, u_long[iterations]);
}
High_Priority_Task::~High_Priority_Task (void)
{
delete [] time_;
time_ = 0;
}
int
High_Priority_Task::open (void *)
{
long flags = THR_BOUND | THR_SCHED_FIFO;
// Become an active object.
if (this->activate (flags, 1, 0, this->priority_) == -1)
{
ACE_DEBUG ((LM_ERROR, "(%t) task activation failed, exiting!\n%a", -1));
}
return 0;
}
int
High_Priority_Task::svc (void)
{
ACE_hthread_t thr_handle;
ACE_Thread::self (thr_handle);
int prio;
if (ACE_Thread::getprio (thr_handle, prio) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "getprio failed"), -1);
ACE_DEBUG ((LM_DEBUG, "(%t) High: prio = %d, priority_ = %d\n",
prio, this->priority_));
ACE_ASSERT (this->priority_ == prio);
ACE_Time_Value pause (0, read_period);
for (u_int i = 0; i < iterations; ++i)
{
time_ [i] = (u_long) ((ACE_OS::gethrtime () - starttime)/1000000ul);
ACE_OS::select (0, 0, 0, 0, &pause);
}
done_ = 1;
return 0;
}
void
High_Priority_Task::print_times () const
{
for (u_int i = 0; i < iterations; ++i)
{
ACE_OS::printf (" iteration %u, time %lu msec\n",
i, time_ [i]);
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class Low_Priority_Task
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
class Low_Priority_Task : public ACE_Task<ACE_MT_SYNCH>
{
public:
Low_Priority_Task (const High_Priority_Task &);
int open (void *);
int svc (void);
private:
int priority_;
const High_Priority_Task &high_priority_task_;
};
Low_Priority_Task::Low_Priority_Task (
const High_Priority_Task &high_priority_task)
: ACE_Task<ACE_MT_SYNCH> (ACE_Thread_Manager::instance ()),
priority_ (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO,
ACE_SCOPE_THREAD)),
high_priority_task_ (high_priority_task)
{
}
int
Low_Priority_Task::open (void *)
{
long flags = THR_BOUND | THR_SCHED_FIFO;
// Become an active object.
if (this->activate (flags, 1, 0, this->priority_) == -1)
{
ACE_DEBUG ((LM_ERROR, "(%t) task activation failed, exiting!\n%a", -1));
}
return 0;
}
int
Low_Priority_Task::svc (void)
{
ACE_hthread_t thr_handle;
ACE_Thread::self (thr_handle);
int prio;
if (ACE_Thread::getprio (thr_handle, prio) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "getprio failed"), -1);
ACE_DEBUG ((LM_DEBUG, "(%t) Low: prio = %d, priority_ = %d\n",
prio, this->priority_));
ACE_ASSERT (this->priority_ == prio);
const u_int interval = 1; /* seconds */
u_long iterations = 0;
// Chew up CPU for the entire interval.
for (;
! high_priority_task_.done () &&
iterations < interval * 1000000 / 40.2;
++iterations)
{
u_long n = 1279ul; /* takes about 40.2 usecs on a 168 MHz Ultra2 */
ACE::is_prime (n,
2ul /* min_factor */,
n/2 /* max_factor */);
if (low_yield) ACE_OS::thr_yield ();
}
ACE_OS::printf ("%lu iterations\n", iterations);
return 0;
}
#endif /* ACE_HAS_THREADS */
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// function get_options
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static
unsigned int
get_options (int argc, char *argv[])
{
ACE_Get_Opt get_opt (argc, argv, "c:n:p:y?");
int opt;
while ((opt = get_opt ()) != EOF) {
switch (opt) {
case 'c':
if (ACE_OS::atoi (get_opt.optarg) >= 2)
{
iterations = ACE_OS::atoi (get_opt.optarg);
}
else
{
ACE_DEBUG ((LM_ERROR, "%n: iterations must be >= 2\n"));
return 1;
}
break;
case 'n':
if (ACE_OS::atoi (get_opt.optarg) >= 1)
{
high_priority_tasks = ACE_OS::atoi (get_opt.optarg);
}
else
{
ACE_DEBUG ((LM_ERROR, "%n: number of high priority threads "
"must be >= 1\n"));
return 1;
}
break;
case 'p':
if (ACE_OS::atoi (get_opt.optarg) > 0)
{
read_period = ACE_OS::atoi (get_opt.optarg);
}
else
{
ACE_DEBUG ((LM_ERROR, "%n: read period > 0\n"));
return 1;
}
break;
case 'y':
low_yield = 1;
break;
case '?':
ACE_DEBUG ((LM_ERROR, "usage: %n %s\n%a", usage, 0));
break;
default:
ACE_DEBUG ((LM_ERROR, "%n: unknown arg, %c\n", opt));
ACE_DEBUG ((LM_ERROR, "usage: %n %s\n", usage));
return 1;
}
}
switch (argc - get_opt.optind) {
case 0:
// use default number of iterations
break;
default:
ACE_DEBUG ((LM_ERROR, "%n: too many arguments\n"));
ACE_DEBUG ((LM_ERROR, "usage: %n %s\n", usage));
return 1;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// function main
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int
main (int argc, char *argv[])
{
#if defined (ACE_HAS_THREADS)
u_int i;
if (get_options (argc, argv)) ACE_OS::exit (-1);
// Enable FIFO scheduling, e.g., RT scheduling class on Solaris.
if (ACE_OS::sched_params (
ACE_Sched_Params (
ACE_SCHED_FIFO,
ACE_Sched_Params::priority_min (ACE_SCHED_FIFO),
ACE_SCOPE_PROCESS)) != 0)
{
if (ACE_OS::last_error () == EPERM)
{
ACE_DEBUG ((LM_MAX, "preempt: user is not superuser, "
"so remain in time-sharing class\n"));
}
else
{
ACE_OS::perror ("context_switch_time");
ACE_OS::exit (-1);
}
}
High_Priority_Task *high_priority_task;
ACE_NEW_RETURN (high_priority_task, High_Priority_Task [high_priority_tasks],
1);
Low_Priority_Task low_priority_task (high_priority_task[0]);
// Save the start time, so that deltas can be displayed later.
starttime = ACE_OS::gethrtime ();
// Spawn the threads . . .
for (i = 0; i < high_priority_tasks; ++i)
{
high_priority_task[i].open (0);
}
low_priority_task.open (0);
ACE_DEBUG ((LM_DEBUG, "(%t) main (), wait for threads to exit . . .\n"));
// Wait for all threads to exit.
ACE_Thread_Manager::instance ()->wait ();
// Display the time deltas. They should be about a half second apart.
for (i = 0; i < high_priority_tasks; ++i)
{
ACE_OS::printf ("High priority task %u:\n", i + 1);
high_priority_task[i].print_times ();
}
delete [] high_priority_task;
#else
ACE_ERROR ((LM_ERROR, "threads not supported on this platform\n"));
#endif /* ACE_HAS_THREADS */
return 0;
}
|