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
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Thread_Pool_Reactor_Test.cpp
//
// = DESCRIPTION
// This program is a torture test of thread poll reactors. It
// starts by spawning several server threads waiting to handle
// events. Several other client threads are spawned right after
// to initiate connections to server threads. Each connection
// adds a new Svc_Handler into the TP_Reactor and sends out
// several "requests" to the server thread. After the connection
// is closed, the Svc_Handler is removed from the TP_Reactor.
// Each message is treated as a separate request by the server so
// two consecutive requests might be serviced by two different
// threads.
//
// Usage: Thread_Pool_Reactor_Test [-r <hostname:port#>]
// [-s <server thr#>] [-c <client thr#>] [-d <delay>]
// [-i <client conn attempt#>] [-n <client request# per conn>]
//
// Default value:
// <hostname:port#>: ACE_DEFAULT_RENDEZVOUS
// <server thr#>: ACE_MAX_THREADS
// <client thr#>: ACE_MAX_ITERATIONS
// <client conn attempt#>: ACE_MAX_ITERATIONS
// <client req# per conn>: ACE_MAX_THREADS
// <delay>: 50 usec
//
// = AUTHOR
// Irfan Pyarali <irfan@cs.wustl.edu> and
// Nanbor Wang <nanbor@cs.wustl.edu>
//
// ============================================================================
#include "tests/test_config.h"
#include "ace/Get_Opt.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/Acceptor.h"
#include "ace/Thread_Manager.h"
#include "ace/TP_Reactor.h"
ACE_RCSID(tests, Atomic_Op_Test, "$Id$")
#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */
#if defined (ACE_HAS_THREADS)
#include "tests/Thread_Pool_Reactor_Test.h"
typedef ACE_Strategy_Acceptor <Request_Handler, ACE_SOCK_ACCEPTOR> ACCEPTOR;
// Accepting end point. This is actually "localhost:10010", but some
// platform couldn't resolve the name so we use the IP address
// directly here.
static const ASYS_TCHAR *rendezvous = ASYS_TEXT ("127.0.0.1:10010");
// Total number of server threads.
static size_t svr_thrno = ACE_MAX_THREADS;
#if defined (CHORUS) // Add platforms that can't handle too many
// connection simultaneously here.
#define ACE_LOAD_FACTOR /2
#else
#define ACE_LOAD_FACTOR
#endif
// Total number of client threads.
static size_t cli_thrno = ACE_MAX_THREADS ACE_LOAD_FACTOR;
// Total connection attemps of a client thread.
static size_t cli_conn_no = ACE_MAX_ITERATIONS ACE_LOAD_FACTOR;
// Total requests a client thread sends.
static size_t cli_req_no = ACE_MAX_THREADS ACE_LOAD_FACTOR;
// Delay before a thread sending the next request (in msec.)
static int req_delay = 50;
static void
parse_arg (int argc, ASYS_TCHAR *argv[])
{
ACE_Get_Opt getopt (argc, argv, ASYS_TEXT ("r:s:c:d:i:n:"));
int c;
while ((c = getopt ()) != -1)
{
switch (c)
{
case 'r': // hostname:port
rendezvous = getopt.optarg;
break;
case 's':
svr_thrno = ACE_OS::atoi (getopt.optarg);
break;
case 'c':
cli_thrno = ACE_OS::atoi (getopt.optarg);
break;
case 'd':
req_delay = ACE_OS::atoi (getopt.optarg);
break;
case 'i':
cli_conn_no = ACE_OS::atoi (getopt.optarg);
break;
case 'n':
cli_req_no = ACE_OS::atoi (getopt.optarg);
break;
default:
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("Usage: Thread_Pool_Reactor_Test [-r <hostname:port#>]")
ASYS_TEXT ("\t[-s <server thr#>] [-c <client thr#>] [-d <delay>]")
ASYS_TEXT ("\t[-i <client conn attempt#>]")
ASYS_TEXT ("[-n <client request# per conn>]\n")));
break;
}
}
}
Request_Handler::Request_Handler (ACE_Thread_Manager *thr_mgr)
: ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_MT_SYNCH> (thr_mgr),
nr_msgs_rcvd_(0)
{
// Make sure we use TP_Reactor with this class (that's the whole
// point, right?)
this->reactor (ACE_Reactor::instance ());
}
int
Request_Handler::handle_input (ACE_HANDLE fd)
{
ASYS_TCHAR buffer[BUFSIZ];
ASYS_TCHAR len = 0;
ssize_t result = this->peer ().recv (&len,
sizeof (ASYS_TCHAR));
if (result > 0
&& this->peer ().recv_n (buffer, len) == len)
{
++this->nr_msgs_rcvd_;
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%t) svr input; fd: 0x%x; input: %s\n"),
fd,
buffer));
if (ACE_OS::strcmp (buffer,
ASYS_TEXT ("shutdown")) == 0)
ACE_Reactor::end_event_loop ();
return 0;
}
else
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%t) Request_Handler: end handle input (0x%x)\n"),
fd));
return -1;
}
int
Request_Handler::handle_close (ACE_HANDLE fd, ACE_Reactor_Mask)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%t) svr close; fd: 0x%x, rcvd %d msgs\n"),
fd,
this->nr_msgs_rcvd_));
if (this->nr_msgs_rcvd_ != cli_req_no)
ACE_ERROR((LM_ERROR,
ASYS_TEXT ("(%t) Expected %d messages; got %d\n"),
cli_req_no,
this->nr_msgs_rcvd_));
this->destroy ();
return 0;
}
static void *
svr_worker (void *)
{
// Server thread function.
while (!ACE_Reactor::event_loop_done ())
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%t) handling events ....\n")));
if (ACE_Reactor::instance ()->handle_events () == -1)
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("(%t) %p\n"),
ASYS_TEXT ("Error handling events")));
}
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%t) I am done handling events. Bye, bye\n")));
return 0;
}
static void *
cli_worker (void *arg)
{
// Client thread function.
ACE_INET_Addr addr (rendezvous);
ACE_SOCK_Stream stream;
ACE_SOCK_Connector connect;
ACE_Time_Value delay (0, req_delay);
size_t len = * ACE_reinterpret_cast (ASYS_TCHAR *, arg);
for (size_t i = 0 ; i < cli_conn_no; i++)
{
if (connect.connect (stream, addr) < 0)
{
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("(%t) %p\n"),
ASYS_TEXT ("connect")));
continue;
}
for (size_t j = 0; j < cli_req_no; j++)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%t) conn_worker handle 0x%x, req %d\n"),
stream.get_handle (),
j+1));
if (stream.send_n (arg,
len + sizeof (ASYS_TCHAR)) == -1)
{
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("(%t) %p\n"),
ASYS_TEXT ("send_n")));
continue;
}
ACE_OS::sleep (delay);
}
stream.close ();
}
return 0;
}
static void *
worker (void *)
{
ACE_OS::sleep (3);
const ASYS_TCHAR *msg = ASYS_TEXT ("Message from Connection worker");
ASYS_TCHAR buf [BUFSIZ];
buf[0] = (ACE_OS::strlen (msg) + 1) * sizeof (ASYS_TCHAR);
ACE_OS::strcpy (&buf[1], msg);
ACE_INET_Addr addr (rendezvous);
ACE_DEBUG((LM_DEBUG,
ASYS_TEXT ("(%t) Spawning %d client threads...\n"),
cli_thrno));
int grp = ACE_Thread_Manager::instance ()->spawn_n (cli_thrno,
&cli_worker,
buf);
ACE_ASSERT (grp != -1);
ACE_Thread_Manager::instance ()->wait_grp (grp);
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("(%t) Client threads done; shutting down...\n")));
ACE_SOCK_Stream stream;
ACE_SOCK_Connector connect;
if (connect.connect (stream, addr) == -1)
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("(%t) %p Error while connecting\n"),
ASYS_TEXT ("connect")));
const char *sbuf = "\011shutdown";
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("shutdown stream handle = %x\n"),
stream.get_handle ()));
if (stream.send_n (sbuf, ACE_OS::strlen (sbuf) + 1) == -1)
ACE_ERROR ((LM_ERROR,
ASYS_TEXT ("(%t) %p\n"),
ASYS_TEXT ("send_n")));
stream.close ();
return 0;
}
int
main (int argc, ASYS_TCHAR *argv[])
{
ACE_START_TEST (ASYS_TEXT ("Thread_Pool_Reactor_Test"));
parse_arg (argc, argv);
// Changed the default
ACE_TP_Reactor sr;
ACE_Reactor new_reactor (&sr);
ACE_Reactor::instance (&new_reactor);
ACCEPTOR acceptor;
ACE_INET_Addr accept_addr (rendezvous);
if (acceptor.open (accept_addr) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ASYS_TEXT ("%p\n"),
ASYS_TEXT ("open")),
1);
ACE_DEBUG((LM_DEBUG,
ASYS_TEXT ("(%t) Spawning %d server threads...\n"),
svr_thrno));
ACE_Thread_Manager::instance ()->spawn_n (svr_thrno,
svr_worker);
ACE_Thread_Manager::instance ()->spawn (worker);
ACE_Thread_Manager::instance ()->wait ();
ACE_END_TEST;
return 0;
}
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Accept_Strategy<Request_Handler, ACE_SOCK_ACCEPTOR>;
template class ACE_Concurrency_Strategy<Request_Handler>;
template class ACE_Creation_Strategy<Request_Handler>;
template class ACE_Scheduling_Strategy<Request_Handler>;
template class ACE_Acceptor<Request_Handler, ACE_SOCK_ACCEPTOR>;
template class ACE_Strategy_Acceptor<Request_Handler, ACE_SOCK_ACCEPTOR>;
template class ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_MT_SYNCH>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Accept_Strategy<Request_Handler, ACE_SOCK_ACCEPTOR>
#pragma instantiate ACE_Concurrency_Strategy<Request_Handler>
#pragma instantiate ACE_Creation_Strategy<Request_Handler>
#pragma instantiate ACE_Scheduling_Strategy<Request_Handler>
#pragma instantiate ACE_Acceptor<Request_Handler, ACE_SOCK_ACCEPTOR>
#pragma instantiate ACE_Strategy_Acceptor<Request_Handler, ACE_SOCK_ACCEPTOR>
#pragma instantiate ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_MT_SYNCH>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
#else
int
main (int, ASYS_TCHAR *[])
{
ACE_START_TEST (ASYS_TEXT ("Thread_Pool_Reactor_Test"));
ACE_ERROR ((LM_INFO,
ASYS_TEXT ("threads not supported on this platform\n")));
ACE_END_TEST;
return 0;
}
#endif /* ACE_HAS_THREADS */
|