1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /***********************************************************************
40 ** This server simulates a server running in loopback mode.
42 ** The idea is that a single server is created. The server initially creates
43 ** a number of worker threads. Then, with the server running, a number of
44 ** clients are created which start requesting service from the server.
47 ** Modification History:
48 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
49 ** The debug mode will print all of the printfs associated with this test.
50 ** The regress mode will be the default mode. Since the regress tool limits
51 ** the output to a one line status:PASS or FAIL,all of the printf statements
52 ** have been handled with an if (debug_mode) statement.
53 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
54 ** recognize the return code from tha main program.
55 ***********************************************************************/
57 /***********************************************************************
59 ***********************************************************************/
60 /* Used to get the command line option */
69 #define THREAD_STACKSIZE 0
71 static int _iterations
= 1000;
72 static int _clients
= 1;
73 static int _client_data
= 250;
74 static int _server_data
= (8*1024);
76 static PRThreadScope ServerScope
, ClientScope
;
78 #define SERVER "Server"
81 #define SERVER_STATE_STARTUP 0
82 #define SERVER_STATE_READY 1
83 #define SERVER_STATE_DYING 2
84 #define SERVER_STATE_DEAD 4
86 PRLock
*ServerStateCVLock
;
87 PRCondVar
*ServerStateCV
;
90 #define DPRINTF printf
95 PRIntn failed_already
=0;
100 static void do_work(void);
102 /* --- Server state functions --------------------------------------------- */
104 SetServerState(char *waiter
, PRInt32 state
)
106 PR_Lock(ServerStateCVLock
);
108 PR_NotifyCondVar(ServerStateCV
);
110 if (debug_mode
) DPRINTF("\t%s changed state to %d\n", waiter
, state
);
112 PR_Unlock(ServerStateCVLock
);
116 WaitServerState(char *waiter
, PRInt32 state
)
120 PR_Lock(ServerStateCVLock
);
122 if (debug_mode
) DPRINTF("\t%s waiting for state %d\n", waiter
, state
);
124 while(!(ServerState
& state
))
125 PR_WaitCondVar(ServerStateCV
, PR_INTERVAL_NO_TIMEOUT
);
128 if (debug_mode
) DPRINTF("\t%s resuming from wait for state %d; state now %d\n",
129 waiter
, state
, ServerState
);
130 PR_Unlock(ServerStateCVLock
);
135 /* --- Server Functions ------------------------------------------- */
137 PRLock
*workerThreadsLock
;
138 PRInt32 workerThreads
;
139 PRInt32 workerThreadsBusy
;
142 WorkerThreadFunc(void *_listenSock
)
144 PRFileDesc
*listenSock
= (PRFileDesc
*)_listenSock
;
146 PRInt32 bytesWritten
;
150 if (debug_mode
) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n",
151 _client_data
+(2*sizeof(PRNetAddr
))+32, _client_data
, (2*sizeof(PRNetAddr
))+32);
152 dataBuf
= (char *)PR_MALLOC(_client_data
+ 2*sizeof(PRNetAddr
) + 32);
154 if (debug_mode
) printf("\tServer could not malloc space!?\n");
155 sendBuf
= (char *)PR_MALLOC(_server_data
*sizeof(char));
157 if (debug_mode
) printf("\tServer could not malloc space!?\n");
159 if (debug_mode
) DPRINTF("\tServer worker thread running\n");
162 PRInt32 bytesToRead
= _client_data
;
163 PRInt32 bytesToWrite
= _server_data
;
170 if (debug_mode
) DPRINTF("\tServer thread going into accept\n");
172 bytesRead
= PR_AcceptRead(listenSock
,
177 PR_INTERVAL_NO_TIMEOUT
);
180 if (debug_mode
) printf("\tServer error in accept (%d)\n", bytesRead
);
184 if (debug_mode
) DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead
);
186 PR_AtomicIncrement(&workerThreadsBusy
);
187 if (workerThreadsBusy
== workerThreads
) {
189 PR_Lock(workerThreadsLock
);
190 if (workerThreadsBusy
== workerThreads
) {
191 PRThread
*WorkerThread
;
193 WorkerThread
= PR_CreateThread(
199 PR_UNJOINABLE_THREAD
,
203 if (debug_mode
) printf("Error creating client thread %d\n", workerThreads
);
205 PR_AtomicIncrement(&workerThreads
);
206 if (debug_mode
) DPRINTF("\tServer creates worker (%d)\n", workerThreads
);
209 PR_Unlock(workerThreadsLock
);
212 bytesToRead
-= bytesRead
;
213 while (bytesToRead
) {
214 bytesRead
= PR_Recv(newSock
,
218 PR_INTERVAL_NO_TIMEOUT
);
220 if (debug_mode
) printf("\tServer error receiving data (%d)\n", bytesRead
);
223 if (debug_mode
) DPRINTF("\tServer received %d bytes\n", bytesRead
);
226 bytesWritten
= PR_Send(newSock
,
230 PR_INTERVAL_NO_TIMEOUT
);
231 if (bytesWritten
!= _server_data
) {
232 if (debug_mode
) printf("\tError sending data to client (%d, %d)\n",
233 bytesWritten
, PR_GetOSError());
235 if (debug_mode
) DPRINTF("\tServer sent %d bytes\n", bytesWritten
);
239 PR_AtomicDecrement(&workerThreadsBusy
);
246 PRFileDesc
*listenSocket
;
247 PRSocketOptionData sockOpt
;
248 PRNetAddr serverAddr
;
249 PRThread
*WorkerThread
;
251 if ( (listenSocket
= PR_NewTCPSocket()) == NULL
) {
252 if (debug_mode
) printf("\tServer error creating listen socket\n");
257 sockOpt
.option
= PR_SockOpt_Reuseaddr
;
258 sockOpt
.value
.reuse_addr
= PR_TRUE
;
259 if ( PR_SetSocketOption(listenSocket
, &sockOpt
) == PR_FAILURE
) {
260 if (debug_mode
) printf("\tServer error setting socket option: OS error %d\n",
262 else failed_already
=1;
263 PR_Close(listenSocket
);
267 memset(&serverAddr
, 0, sizeof(PRNetAddr
));
268 serverAddr
.inet
.family
= PR_AF_INET
;
269 serverAddr
.inet
.port
= PR_htons(PORT
);
270 serverAddr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
272 if ( PR_Bind(listenSocket
, &serverAddr
) == PR_FAILURE
) {
273 if (debug_mode
) printf("\tServer error binding to server address: OS error %d\n",
275 else failed_already
=1;
276 PR_Close(listenSocket
);
280 if ( PR_Listen(listenSocket
, 128) == PR_FAILURE
) {
281 if (debug_mode
) printf("\tServer error listening to server socket\n");
282 else failed_already
=1;
283 PR_Close(listenSocket
);
290 workerThreadsBusy
= 0;
292 workerThreadsLock
= PR_NewLock();
294 WorkerThread
= PR_CreateThread(
300 PR_UNJOINABLE_THREAD
,
304 if (debug_mode
) printf("error creating working thread\n");
305 PR_Close(listenSocket
);
308 PR_AtomicIncrement(&workerThreads
);
309 if (debug_mode
) DPRINTF("\tServer created primordial worker thread\n");
314 /* The main server loop */
316 ServerThreadFunc(void *unused
)
318 PRFileDesc
*listenSocket
;
321 listenSocket
= ServerSetup();
324 SetServerState(SERVER
, SERVER_STATE_DEAD
);
327 if (debug_mode
) DPRINTF("\tServer up\n");
329 /* Tell clients they can start now. */
330 SetServerState(SERVER
, SERVER_STATE_READY
);
332 /* Now wait for server death signal */
333 WaitServerState(SERVER
, SERVER_STATE_DYING
);
336 SetServerState(SERVER
, SERVER_STATE_DEAD
);
340 /* --- Client Functions ------------------------------------------- */
344 PRMonitor
*clientMonitor
;
347 ClientThreadFunc(void *unused
)
349 PRNetAddr serverAddr
;
350 PRFileDesc
*clientSocket
;
356 sendBuf
= (char *)PR_MALLOC(_client_data
* sizeof(char));
358 if (debug_mode
) printf("\tClient could not malloc space!?\n");
359 recvBuf
= (char *)PR_MALLOC(_server_data
* sizeof(char));
361 if (debug_mode
) printf("\tClient could not malloc space!?\n");
363 memset(&serverAddr
, 0, sizeof(PRNetAddr
));
364 serverAddr
.inet
.family
= PR_AF_INET
;
365 serverAddr
.inet
.port
= PR_htons(PORT
);
366 serverAddr
.inet
.ip
= PR_htonl(PR_INADDR_LOOPBACK
);
368 while(numRequests
> 0) {
370 if ( (numRequests
% 10) == 0 )
371 if (debug_mode
) printf(".");
372 if (debug_mode
) DPRINTF("\tClient starting request %d\n", numRequests
);
374 clientSocket
= PR_NewTCPSocket();
376 if (debug_mode
) printf("Client error creating socket: OS error %d\n",
381 if (debug_mode
) DPRINTF("\tClient connecting\n");
383 rv
= PR_Connect(clientSocket
,
385 PR_INTERVAL_NO_TIMEOUT
);
387 if (debug_mode
) printf("\tClient error connecting\n");
391 if (debug_mode
) DPRINTF("\tClient connected\n");
393 rv
= PR_Send(clientSocket
,
397 PR_INTERVAL_NO_TIMEOUT
);
398 if (rv
!= _client_data
) {
399 if (debug_mode
) printf("Client error sending data (%d)\n", rv
);
400 PR_Close(clientSocket
);
404 if (debug_mode
) DPRINTF("\tClient sent %d bytes\n", rv
);
406 bytesNeeded
= _server_data
;
408 rv
= PR_Recv(clientSocket
,
412 PR_INTERVAL_NO_TIMEOUT
);
414 if (debug_mode
) printf("Client error receiving data (%d) (%d/%d)\n",
415 rv
, (_server_data
- bytesNeeded
), _server_data
);
418 if (debug_mode
) DPRINTF("\tClient received %d bytes; need %d more\n", rv
, bytesNeeded
- rv
);
422 PR_Close(clientSocket
);
424 PR_AtomicDecrement(&numRequests
);
427 PR_EnterMonitor(clientMonitor
);
429 PR_Notify(clientMonitor
);
430 PR_ExitMonitor(clientMonitor
);
441 numRequests
= _iterations
;
442 numClients
= _clients
;
443 clientMonitor
= PR_NewMonitor();
445 for (index
=0; index
<_clients
; index
++) {
446 PRThread
*clientThread
;
449 clientThread
= PR_CreateThread(
455 PR_UNJOINABLE_THREAD
,
459 if (debug_mode
) printf("\terror creating client thread %d\n", index
);
461 if (debug_mode
) DPRINTF("\tMain created client %d/%d\n", index
+1, _clients
);
465 PR_EnterMonitor(clientMonitor
);
467 PR_Wait(clientMonitor
, PR_INTERVAL_NO_TIMEOUT
);
468 PR_ExitMonitor(clientMonitor
);
471 /* --- Main Function ---------------------------------------------- */
476 PRThread
*ServerThread
;
479 SetServerState(MAIN
, SERVER_STATE_STARTUP
);
480 ServerThread
= PR_CreateThread(
489 if (debug_mode
) printf("error creating main server thread\n");
493 /* Wait for server to be ready */
494 state
= WaitServerState(MAIN
, SERVER_STATE_READY
|SERVER_STATE_DEAD
);
496 if (!(state
& SERVER_STATE_DEAD
)) {
497 /* Run Test Clients */
500 /* Send death signal to server */
501 SetServerState(MAIN
, SERVER_STATE_DYING
);
504 PR_JoinThread(ServerThread
);
508 static void do_workUK(void)
510 ServerScope
= PR_LOCAL_THREAD
;
511 ClientScope
= PR_GLOBAL_THREAD
;
517 static void Measure(void (*func
)(void), const char *msg
)
519 PRIntervalTime start
, stop
;
522 start
= PR_IntervalNow();
524 stop
= PR_IntervalNow();
526 d
= (double)PR_IntervalToMicroseconds(stop
- start
);
528 if (debug_mode
) printf("\n%40s: %6.2f usec\n", msg
, d
/ _iterations
);
532 main(int argc
, char **argv
)
534 /* The command line argument: -d is used to determine if the test is being run
535 in debug mode. The regress tool requires only one line output:PASS or FAIL.
536 All of the printfs associated with this test has been handled with a if (debug_mode)
541 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
542 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
544 if (PL_OPT_BAD
== os
) continue;
547 case 'd': /* debug mode */
554 PL_DestroyOptState(opt
);
558 printf("Enter number of iterations: \n");
559 scanf("%d", &_iterations
);
560 printf("Enter number of clients : \n");
561 scanf("%d", &_clients
);
562 printf("Enter size of client data : \n");
563 scanf("%d", &_client_data
);
564 printf("Enter size of server data : \n");
565 scanf("%d", &_server_data
);
575 printf("\n\n%d iterations with %d client threads.\n",
576 _iterations
, _clients
);
577 printf("Sending %d bytes of client data and %d bytes of server data\n",
578 _client_data
, _server_data
);
580 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
583 PR_SetThreadRecycleMode(64);
585 ServerStateCVLock
= PR_NewLock();
586 ServerStateCV
= PR_NewCondVar(ServerStateCVLock
);
588 Measure(do_workUK
, "server loop user/kernel");