1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /***********************************************************************
8 ** This server simulates a server running in loopback mode.
10 ** The idea is that a single server is created. The server initially creates
11 ** a number of worker threads. Then, with the server running, a number of
12 ** clients are created which start requesting service from the server.
15 ** Modification History:
16 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
17 ** The debug mode will print all of the printfs associated with this test.
18 ** The regress mode will be the default mode. Since the regress tool limits
19 ** the output to a one line status:PASS or FAIL,all of the printf statements
20 ** have been handled with an if (debug_mode) statement.
21 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
22 ** recognize the return code from tha main program.
23 ***********************************************************************/
25 /***********************************************************************
27 ***********************************************************************/
28 /* Used to get the command line option */
37 #define THREAD_STACKSIZE 0
39 static int _iterations
= 1000;
40 static int _clients
= 1;
41 static int _client_data
= 250;
42 static int _server_data
= (8*1024);
44 static PRThreadScope ServerScope
, ClientScope
;
46 #define SERVER "Server"
49 #define SERVER_STATE_STARTUP 0
50 #define SERVER_STATE_READY 1
51 #define SERVER_STATE_DYING 2
52 #define SERVER_STATE_DEAD 4
54 PRLock
*ServerStateCVLock
;
55 PRCondVar
*ServerStateCV
;
58 #define DPRINTF printf
63 PRIntn failed_already
=0;
65 static void do_work(void);
67 /* --- Server state functions --------------------------------------------- */
69 SetServerState(char *waiter
, PRInt32 state
)
71 PR_Lock(ServerStateCVLock
);
73 PR_NotifyCondVar(ServerStateCV
);
76 DPRINTF("\t%s changed state to %d\n", waiter
, state
);
79 PR_Unlock(ServerStateCVLock
);
83 WaitServerState(char *waiter
, PRInt32 state
)
87 PR_Lock(ServerStateCVLock
);
90 DPRINTF("\t%s waiting for state %d\n", waiter
, state
);
93 while(!(ServerState
& state
)) {
94 PR_WaitCondVar(ServerStateCV
, PR_INTERVAL_NO_TIMEOUT
);
98 if (debug_mode
) DPRINTF("\t%s resuming from wait for state %d; state now %d\n",
99 waiter
, state
, ServerState
);
100 PR_Unlock(ServerStateCVLock
);
105 /* --- Server Functions ------------------------------------------- */
107 PRLock
*workerThreadsLock
;
108 PRInt32 workerThreads
;
109 PRInt32 workerThreadsBusy
;
112 WorkerThreadFunc(void *_listenSock
)
114 PRFileDesc
*listenSock
= (PRFileDesc
*)_listenSock
;
116 PRInt32 bytesWritten
;
120 if (debug_mode
) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n",
121 _client_data
+(2*sizeof(PRNetAddr
))+32, _client_data
, (2*sizeof(PRNetAddr
))+32);
122 dataBuf
= (char *)PR_MALLOC(_client_data
+ 2*sizeof(PRNetAddr
) + 32);
125 printf("\tServer could not malloc space!?\n");
127 sendBuf
= (char *)PR_MALLOC(_server_data
*sizeof(char));
130 printf("\tServer could not malloc space!?\n");
134 DPRINTF("\tServer worker thread running\n");
138 PRInt32 bytesToRead
= _client_data
;
139 PRInt32 bytesToWrite
= _server_data
;
147 DPRINTF("\tServer thread going into accept\n");
150 bytesRead
= PR_AcceptRead(listenSock
,
155 PR_INTERVAL_NO_TIMEOUT
);
159 printf("\tServer error in accept (%d)\n", bytesRead
);
165 DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead
);
168 PR_AtomicIncrement(&workerThreadsBusy
);
169 if (workerThreadsBusy
== workerThreads
) {
170 PR_Lock(workerThreadsLock
);
171 if (workerThreadsBusy
== workerThreads
) {
172 PRThread
*WorkerThread
;
174 WorkerThread
= PR_CreateThread(
180 PR_UNJOINABLE_THREAD
,
185 printf("Error creating client thread %d\n", workerThreads
);
188 PR_AtomicIncrement(&workerThreads
);
190 DPRINTF("\tServer creates worker (%d)\n", workerThreads
);
194 PR_Unlock(workerThreadsLock
);
197 bytesToRead
-= bytesRead
;
198 while (bytesToRead
) {
199 bytesRead
= PR_Recv(newSock
,
203 PR_INTERVAL_NO_TIMEOUT
);
206 printf("\tServer error receiving data (%d)\n", bytesRead
);
211 DPRINTF("\tServer received %d bytes\n", bytesRead
);
215 bytesWritten
= PR_Send(newSock
,
219 PR_INTERVAL_NO_TIMEOUT
);
220 if (bytesWritten
!= _server_data
) {
221 if (debug_mode
) printf("\tError sending data to client (%d, %d)\n",
222 bytesWritten
, PR_GetOSError());
225 DPRINTF("\tServer sent %d bytes\n", bytesWritten
);
230 PR_AtomicDecrement(&workerThreadsBusy
);
237 PRFileDesc
*listenSocket
;
238 PRSocketOptionData sockOpt
;
239 PRNetAddr serverAddr
;
240 PRThread
*WorkerThread
;
242 if ( (listenSocket
= PR_NewTCPSocket()) == NULL
) {
244 printf("\tServer error creating listen socket\n");
252 sockOpt
.option
= PR_SockOpt_Reuseaddr
;
253 sockOpt
.value
.reuse_addr
= PR_TRUE
;
254 if ( PR_SetSocketOption(listenSocket
, &sockOpt
) == PR_FAILURE
) {
255 if (debug_mode
) printf("\tServer error setting socket option: OS error %d\n",
260 PR_Close(listenSocket
);
264 memset(&serverAddr
, 0, sizeof(PRNetAddr
));
265 serverAddr
.inet
.family
= PR_AF_INET
;
266 serverAddr
.inet
.port
= PR_htons(PORT
);
267 serverAddr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
269 if ( PR_Bind(listenSocket
, &serverAddr
) == PR_FAILURE
) {
270 if (debug_mode
) printf("\tServer error binding to server address: OS error %d\n",
275 PR_Close(listenSocket
);
279 if ( PR_Listen(listenSocket
, 128) == PR_FAILURE
) {
281 printf("\tServer error listening to server socket\n");
286 PR_Close(listenSocket
);
293 workerThreadsBusy
= 0;
295 workerThreadsLock
= PR_NewLock();
297 WorkerThread
= PR_CreateThread(
303 PR_UNJOINABLE_THREAD
,
308 printf("error creating working thread\n");
310 PR_Close(listenSocket
);
313 PR_AtomicIncrement(&workerThreads
);
315 DPRINTF("\tServer created primordial worker thread\n");
321 /* The main server loop */
323 ServerThreadFunc(void *unused
)
325 PRFileDesc
*listenSocket
;
328 listenSocket
= ServerSetup();
331 SetServerState(SERVER
, SERVER_STATE_DEAD
);
335 DPRINTF("\tServer up\n");
338 /* Tell clients they can start now. */
339 SetServerState(SERVER
, SERVER_STATE_READY
);
341 /* Now wait for server death signal */
342 WaitServerState(SERVER
, SERVER_STATE_DYING
);
345 SetServerState(SERVER
, SERVER_STATE_DEAD
);
349 /* --- Client Functions ------------------------------------------- */
353 PRMonitor
*clientMonitor
;
356 ClientThreadFunc(void *unused
)
358 PRNetAddr serverAddr
;
359 PRFileDesc
*clientSocket
;
365 sendBuf
= (char *)PR_MALLOC(_client_data
* sizeof(char));
368 printf("\tClient could not malloc space!?\n");
370 recvBuf
= (char *)PR_MALLOC(_server_data
* sizeof(char));
373 printf("\tClient could not malloc space!?\n");
376 memset(&serverAddr
, 0, sizeof(PRNetAddr
));
377 serverAddr
.inet
.family
= PR_AF_INET
;
378 serverAddr
.inet
.port
= PR_htons(PORT
);
379 serverAddr
.inet
.ip
= PR_htonl(PR_INADDR_LOOPBACK
);
381 while(numRequests
> 0) {
383 if ( (numRequests
% 10) == 0 )
388 DPRINTF("\tClient starting request %d\n", numRequests
);
391 clientSocket
= PR_NewTCPSocket();
393 if (debug_mode
) printf("Client error creating socket: OS error %d\n",
399 DPRINTF("\tClient connecting\n");
402 rv
= PR_Connect(clientSocket
,
404 PR_INTERVAL_NO_TIMEOUT
);
407 printf("\tClient error connecting\n");
413 DPRINTF("\tClient connected\n");
416 rv
= PR_Send(clientSocket
,
420 PR_INTERVAL_NO_TIMEOUT
);
421 if (rv
!= _client_data
) {
423 printf("Client error sending data (%d)\n", rv
);
425 PR_Close(clientSocket
);
430 DPRINTF("\tClient sent %d bytes\n", rv
);
433 bytesNeeded
= _server_data
;
435 rv
= PR_Recv(clientSocket
,
439 PR_INTERVAL_NO_TIMEOUT
);
441 if (debug_mode
) printf("Client error receiving data (%d) (%d/%d)\n",
442 rv
, (_server_data
- bytesNeeded
), _server_data
);
446 DPRINTF("\tClient received %d bytes; need %d more\n", rv
, bytesNeeded
- rv
);
451 PR_Close(clientSocket
);
453 PR_AtomicDecrement(&numRequests
);
456 PR_EnterMonitor(clientMonitor
);
458 PR_Notify(clientMonitor
);
459 PR_ExitMonitor(clientMonitor
);
470 numRequests
= _iterations
;
471 numClients
= _clients
;
472 clientMonitor
= PR_NewMonitor();
474 for (index
=0; index
<_clients
; index
++) {
475 PRThread
*clientThread
;
478 clientThread
= PR_CreateThread(
484 PR_UNJOINABLE_THREAD
,
489 printf("\terror creating client thread %d\n", index
);
491 } else if (debug_mode
) {
492 DPRINTF("\tMain created client %d/%d\n", index
+1, _clients
);
497 PR_EnterMonitor(clientMonitor
);
499 PR_Wait(clientMonitor
, PR_INTERVAL_NO_TIMEOUT
);
501 PR_ExitMonitor(clientMonitor
);
504 /* --- Main Function ---------------------------------------------- */
509 PRThread
*ServerThread
;
512 SetServerState(MAIN
, SERVER_STATE_STARTUP
);
513 ServerThread
= PR_CreateThread(
523 printf("error creating main server thread\n");
528 /* Wait for server to be ready */
529 state
= WaitServerState(MAIN
, SERVER_STATE_READY
|SERVER_STATE_DEAD
);
531 if (!(state
& SERVER_STATE_DEAD
)) {
532 /* Run Test Clients */
535 /* Send death signal to server */
536 SetServerState(MAIN
, SERVER_STATE_DYING
);
539 PR_JoinThread(ServerThread
);
542 static void do_workUU(void)
544 ServerScope
= PR_LOCAL_THREAD
;
545 ClientScope
= PR_LOCAL_THREAD
;
549 static void do_workUK(void)
551 ServerScope
= PR_LOCAL_THREAD
;
552 ClientScope
= PR_GLOBAL_THREAD
;
556 static void do_workKU(void)
558 ServerScope
= PR_GLOBAL_THREAD
;
559 ClientScope
= PR_LOCAL_THREAD
;
563 static void do_workKK(void)
565 ServerScope
= PR_GLOBAL_THREAD
;
566 ClientScope
= PR_GLOBAL_THREAD
;
571 static void Measure(void (*func
)(void), const char *msg
)
573 PRIntervalTime start
, stop
;
576 start
= PR_IntervalNow();
578 stop
= PR_IntervalNow();
580 d
= (double)PR_IntervalToMicroseconds(stop
- start
);
583 printf("\n%40s: %6.2f usec\n", msg
, d
/ _iterations
);
588 int main(int argc
, char **argv
)
590 /* The command line argument: -d is used to determine if the test is being run
591 in debug mode. The regress tool requires only one line output:PASS or FAIL.
592 All of the printfs associated with this test has been handled with a if (debug_mode)
597 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
598 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
600 if (PL_OPT_BAD
== os
) {
605 case 'd': /* debug mode */
612 PL_DestroyOptState(opt
);
616 printf("Enter number of iterations: \n");
617 scanf("%d", &_iterations
);
618 printf("Enter number of clients : \n");
619 scanf("%d", &_clients
);
620 printf("Enter size of client data : \n");
621 scanf("%d", &_client_data
);
622 printf("Enter size of server data : \n");
623 scanf("%d", &_server_data
);
634 printf("\n\n%d iterations with %d client threads.\n",
635 _iterations
, _clients
);
636 printf("Sending %d bytes of client data and %d bytes of server data\n",
637 _client_data
, _server_data
);
639 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
642 PR_SetThreadRecycleMode(64);
644 ServerStateCVLock
= PR_NewLock();
645 ServerStateCV
= PR_NewCondVar(ServerStateCVLock
);
648 Measure(do_workKK
, "server loop kernel/kernel");