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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
// $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/OS_main.h"
#include "ace/ACE.h"
#include "ace/Task.h"
#include "ace/Sched_Params.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_sys_select.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_unistd.h"
ACE_RCSID(Misc, preempt, "$Id$")
#if defined (ACE_HAS_THREADS) || ! defined (ACE_LACKS_FORK)
#if defined (ACE_HAS_STHREADS)
# include <sys/lwp.h> /* for _lwp_self () */
#endif /* ACE_HAS_STHREADS */
static const char usage [] = "[-? |\n"
#if defined (ACE_HAS_THREADS)
" [-f use fork instead of spawn]\n"
#endif /* ACE_HAS_THREADS */
" [-h <high pri iterations, default 10>]\n"
" [-l <low pri iterations, default 25000>]\n"
" [-n <high priority threads, default 1>]\n"
" [-p <read period, default 500000 usec>]\n"
" [-y to yield the low priority thread]]\n";
// Configuration options.
#if defined (ACE_HAS_THREADS)
u_int use_fork = 0;
#else /* ! ACE_HAS_THREADS */
u_int use_fork = 1;
#endif /* ! ACE_HAS_THREADS */
u_int high_iterations = 10;
u_int high_priority_tasks = 1;
u_int low_iterations = 25000; /* Big enough to keep the low priority task
cranking for a while */
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_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_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[high_iterations]);
}
High_Priority_Task::~High_Priority_Task (void)
{
delete [] time_;
time_ = 0;
}
int
High_Priority_Task::open (void *)
{
if (use_fork == 1)
{
ACE_hthread_t thr_handle;
ACE_Thread::self (thr_handle);
if (ACE_Thread::setprio (thr_handle, priority_) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "setprio failed"), -1);
return svc ();
}
else
{
long flags = THR_BOUND | THR_NEW_LWP | THR_SCHED_FIFO;
// Become an active object.
if (this->activate (flags, 1, 0, this->priority_) == -1)
{
ACE_DEBUG ((LM_ERROR, "(%P|%t) task activation failed, exiting!\n"));
ACE_OS::exit (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);
#if defined (ACE_HAS_STHREADS)
ACE_DEBUG ((LM_DEBUG, "(%P|%u|%t) High: prio = %d, priority_ = %d\n",
_lwp_self (), prio, this->priority_));
#else /* ! ACE_HAS_STHREADS */
ACE_DEBUG ((LM_DEBUG, "(%P|%t) High: prio = %d, priority_ = %d\n",
prio, this->priority_));
#endif /* ! ACE_HAS_STHREADS */
ACE_ASSERT (this->priority_ == prio);
ACE_Time_Value pause (0, read_period);
for (u_int i = 0; i < high_iterations; ++i)
{
time_ [i] = (u_long) ((ACE_OS::gethrtime () - starttime)/
(ACE_UINT32) 1000000u);
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 < high_iterations; ++i)
{
ACE_DEBUG ((LM_INFO, " iteration %u, time %u msec\n",
i, time_ [i]));
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class Low_Priority_Task
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
class Low_Priority_Task : public ACE_Task<ACE_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_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 *)
{
if (use_fork == 1)
{
ACE_hthread_t thr_handle;
ACE_Thread::self (thr_handle);
if (ACE_Thread::setprio (thr_handle, priority_) == -1)
ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "setprio failed"), -1);
return svc ();
}
else
{
long flags = THR_BOUND | THR_NEW_LWP | THR_SCHED_FIFO;
// Become an active object.
if (this->activate (flags, 1, 0, this->priority_) == -1)
ACE_ERROR ((LM_ERROR,
"(%P|%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);
#if defined (ACE_HAS_STHREADS)
ACE_DEBUG ((LM_DEBUG, "(%P|%u|%t) Low: prio = %d, priority_ = %d\n",
_lwp_self (), prio, this->priority_));
#else /* ! ACE_HAS_STHREADS */
ACE_DEBUG ((LM_DEBUG, "(%P|%t) Low: prio = %d, priority_ = %d\n",
prio, this->priority_));
#endif /* ! ACE_HAS_STHREADS */
ACE_ASSERT (this->priority_ == prio);
u_long iterations = 0;
// Chew up CPU for the entire interval.
for (;
! high_priority_task_.done () && iterations < low_iterations;
++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_DEBUG ((LM_INFO, "Low completed %u iterations\n", iterations));
return 0;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// function get_options
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
static int
get_options (int argc, ACE_TCHAR *argv[])
{
ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("fh:l:n:p:y?"));
int opt;
while ((opt = get_opt ()) != EOF) {
switch (opt) {
case 'f':
use_fork = 1;
break;
case 'h':
if (ACE_OS::atoi (get_opt.opt_arg ()) >= 2)
high_iterations = ACE_OS::atoi (get_opt.opt_arg ());
else
ACE_ERROR_RETURN ((LM_ERROR, "%n: high iterations must be >= 2\n"),
-1);
break;
case 'l':
if (ACE_OS::atoi (get_opt.opt_arg ()) >= 2)
low_iterations = ACE_OS::atoi (get_opt.opt_arg ());
else
ACE_ERROR_RETURN ((LM_ERROR, "%n: low iterations must be >= 2\n"), -1);
break;
case 'n':
if (ACE_OS::atoi (get_opt.opt_arg ()) >= 1)
high_priority_tasks = ACE_OS::atoi (get_opt.opt_arg ());
else
ACE_ERROR_RETURN ((LM_ERROR, "%n: number of high priority threads "
"must be >= 1\n"), -1);
break;
case 'p':
if (ACE_OS::atoi (get_opt.opt_arg ()) > 0)
read_period = ACE_OS::atoi (get_opt.opt_arg ());
else
ACE_ERROR_RETURN ((LM_ERROR, "%n: read period > 0\n"), -1);
break;
case 'y':
low_yield = 1;
break;
case '?':
ACE_DEBUG ((LM_ERROR, "usage: %n %s\n%a", usage, 0));
break;
default:
ACE_ERROR_RETURN ((LM_ERROR,
"%n: unknown arg, %c\nusage: %n %s\n", opt, usage),
-1);
}
}
switch (argc - get_opt.opt_ind ()) {
case 0:
// OK, no non-flag arguments.
break;
default:
ACE_ERROR_RETURN ((LM_ERROR,
"%n: too many arguments\nusage: %n %s\n", usage),
-1);
}
return 0;
}
#endif /* ACE_HAS_THREADS || ! ACE_LACKS_FORK */
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// function main
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
ACE_LOG_MSG->open (argv[0] ? argv[0] : ACE_TEXT("preempt"));
#if defined (ACE_HAS_THREADS) || !defined (ACE_LACKS_FORK)
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_ERROR_RETURN ((LM_ERROR, "%n: ACE_OS::sched_params failed\n%a"),
-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]);
if (! use_fork)
{
ACE_DEBUG ((LM_DEBUG,
"(%P|%t) main (), wait for threads to exit . . .\n"));
}
// Save the start time, so that deltas can be displayed later.
starttime = ACE_OS::gethrtime ();
// Spawn the threads/processes . . .
pid_t child = 0;
if (use_fork == 1)
{
switch ((child = ACE_OS::fork (ACE_TEXT("preempt-low_priority_process"))))
{
case -1:
ACE_ERROR ((LM_ERROR, "%p\n%a", "fork failed"));
return -1;
case 0: // In child
{
low_priority_task.open (0);
break;
}
default: // In parent
for (i = 0; i < high_priority_tasks; ++i)
{
high_priority_task[i].open (0);
}
break;
}
}
else
{
for (i = 0; i < high_priority_tasks; ++i)
{
high_priority_task[i].open (0);
}
low_priority_task.open (0);
#if defined (ACE_HAS_THREADS)
// Wait for all threads to exit.
ACE_Thread_Manager::instance ()->wait ();
#endif /* ACE_HAS_THREADS */
}
// Display the time deltas. They should be about a half second apart.
if (child || ! use_fork)
{
for (i = 0; i < high_priority_tasks; ++i)
{
ACE_DEBUG ((LM_DEBUG, "High priority task %u:\n", i + 1));
high_priority_task[i].print_times ();
}
}
delete [] high_priority_task;
#else /* ! ACE_HAS_THREADS && ACE_LACKS_FORK */
ACE_UNUSED_ARG (argc);
ACE_UNUSED_ARG (argv);
ACE_ERROR ((LM_ERROR, "threads and fork not supported on this platform\n"));
#endif /* ! ACE_HAS_THREADS && ACE_LACKS_FORK */
return 0;
}
|