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;
98 static void do_work(void);
100 /* --- Server state functions --------------------------------------------- */
102 SetServerState(char *waiter, PRInt32 state)
104 PR_Lock(ServerStateCVLock);
106 PR_NotifyCondVar(ServerStateCV);
108 if (debug_mode) DPRINTF("\t%s changed state to %d\n", waiter, state);
110 PR_Unlock(ServerStateCVLock);
114 WaitServerState(char *waiter, PRInt32 state)
118 PR_Lock(ServerStateCVLock);
120 if (debug_mode) DPRINTF("\t%s waiting for state %d\n", waiter, state);
122 while(!(ServerState & state))
123 PR_WaitCondVar(ServerStateCV, PR_INTERVAL_NO_TIMEOUT);
126 if (debug_mode) DPRINTF("\t%s resuming from wait for state %d; state now %d\n",
127 waiter, state, ServerState);
128 PR_Unlock(ServerStateCVLock);
133 /* --- Server Functions ------------------------------------------- */
135 PRLock *workerThreadsLock;
136 PRInt32 workerThreads;
137 PRInt32 workerThreadsBusy;
140 WorkerThreadFunc(void *_listenSock)
142 PRFileDesc *listenSock = (PRFileDesc *)_listenSock;
144 PRInt32 bytesWritten;
148 if (debug_mode) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n",
149 _client_data+(2*sizeof(PRNetAddr))+32, _client_data, (2*sizeof(PRNetAddr))+32);
150 dataBuf = (char *)PR_MALLOC(_client_data + 2*sizeof(PRNetAddr) + 32);
152 if (debug_mode) printf("\tServer could not malloc space!?\n");
153 sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char));
155 if (debug_mode) printf("\tServer could not malloc space!?\n");
157 if (debug_mode) DPRINTF("\tServer worker thread running\n");
160 PRInt32 bytesToRead = _client_data;
161 PRInt32 bytesToWrite = _server_data;
168 if (debug_mode) DPRINTF("\tServer thread going into accept\n");
170 bytesRead = PR_AcceptRead(listenSock,
175 PR_INTERVAL_NO_TIMEOUT);
178 if (debug_mode) printf("\tServer error in accept (%d)\n", bytesRead);
182 if (debug_mode) DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead);
184 PR_AtomicIncrement(&workerThreadsBusy);
186 if (workerThreadsBusy == workerThreads && workerThreads<1) {
188 if (workerThreadsBusy == workerThreads) {
191 PR_Lock(workerThreadsLock);
192 if (workerThreadsBusy == workerThreads) {
193 PRThread *WorkerThread;
195 WorkerThread = PR_CreateThread(
201 PR_UNJOINABLE_THREAD,
205 if (debug_mode) printf("Error creating client thread %d\n", workerThreads);
207 PR_AtomicIncrement(&workerThreads);
208 if (debug_mode) DPRINTF("\tServer creates worker (%d)\n", workerThreads);
211 PR_Unlock(workerThreadsLock);
214 bytesToRead -= bytesRead;
215 while (bytesToRead) {
216 bytesRead = PR_Recv(newSock,
220 PR_INTERVAL_NO_TIMEOUT);
222 if (debug_mode) printf("\tServer error receiving data (%d)\n", bytesRead);
225 if (debug_mode) DPRINTF("\tServer received %d bytes\n", bytesRead);
228 bytesWritten = PR_Send(newSock,
232 PR_INTERVAL_NO_TIMEOUT);
233 if (bytesWritten != _server_data) {
234 if (debug_mode) printf("\tError sending data to client (%d, %d)\n",
235 bytesWritten, PR_GetOSError());
237 if (debug_mode) DPRINTF("\tServer sent %d bytes\n", bytesWritten);
241 PR_AtomicDecrement(&workerThreadsBusy);
248 PRFileDesc *listenSocket;
249 PRSocketOptionData sockOpt;
250 PRNetAddr serverAddr;
251 PRThread *WorkerThread;
253 if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
254 if (debug_mode) printf("\tServer error creating listen socket\n");
255 else failed_already=1;
259 sockOpt.option = PR_SockOpt_Reuseaddr;
260 sockOpt.value.reuse_addr = PR_TRUE;
261 if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) {
262 if (debug_mode) printf("\tServer error setting socket option: OS error %d\n",
264 else failed_already=1;
265 PR_Close(listenSocket);
269 memset(&serverAddr, 0, sizeof(PRNetAddr));
270 serverAddr.inet.family = PR_AF_INET;
271 serverAddr.inet.port = PR_htons(PORT);
272 serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
274 if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
275 if (debug_mode) printf("\tServer error binding to server address: OS error %d\n",
277 else failed_already=1;
278 PR_Close(listenSocket);
282 if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
283 if (debug_mode) printf("\tServer error listening to server socket\n");
284 else failed_already=1;
285 PR_Close(listenSocket);
292 workerThreadsBusy = 0;
294 workerThreadsLock = PR_NewLock();
296 WorkerThread = PR_CreateThread(
302 PR_UNJOINABLE_THREAD,
306 if (debug_mode) printf("error creating working thread\n");
307 PR_Close(listenSocket);
310 PR_AtomicIncrement(&workerThreads);
311 if (debug_mode) DPRINTF("\tServer created primordial worker thread\n");
316 /* The main server loop */
318 ServerThreadFunc(void *unused)
320 PRFileDesc *listenSocket;
323 listenSocket = ServerSetup();
326 SetServerState(SERVER, SERVER_STATE_DEAD);
329 if (debug_mode) DPRINTF("\tServer up\n");
331 /* Tell clients they can start now. */
332 SetServerState(SERVER, SERVER_STATE_READY);
334 /* Now wait for server death signal */
335 WaitServerState(SERVER, SERVER_STATE_DYING);
338 SetServerState(SERVER, SERVER_STATE_DEAD);
342 /* --- Client Functions ------------------------------------------- */
346 PRMonitor *clientMonitor;
349 ClientThreadFunc(void *unused)
351 PRNetAddr serverAddr;
352 PRFileDesc *clientSocket;
358 sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char));
360 if (debug_mode) printf("\tClient could not malloc space!?\n");
361 recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char));
363 if (debug_mode) printf("\tClient could not malloc space!?\n");
365 memset(&serverAddr, 0, sizeof(PRNetAddr));
366 serverAddr.inet.family = PR_AF_INET;
367 serverAddr.inet.port = PR_htons(PORT);
368 serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
370 while(numRequests > 0) {
372 if ( (numRequests % 10) == 0 )
373 if (debug_mode) printf(".");
374 if (debug_mode) DPRINTF("\tClient starting request %d\n", numRequests);
376 clientSocket = PR_NewTCPSocket();
378 if (debug_mode) printf("Client error creating socket: OS error %d\n",
383 if (debug_mode) DPRINTF("\tClient connecting\n");
385 rv = PR_Connect(clientSocket,
387 PR_INTERVAL_NO_TIMEOUT);
389 if (debug_mode) printf("\tClient error connecting\n");
393 if (debug_mode) DPRINTF("\tClient connected\n");
395 rv = PR_Send(clientSocket,
399 PR_INTERVAL_NO_TIMEOUT);
400 if (rv != _client_data) {
401 if (debug_mode) printf("Client error sending data (%d)\n", rv);
402 PR_Close(clientSocket);
406 if (debug_mode) DPRINTF("\tClient sent %d bytes\n", rv);
408 bytesNeeded = _server_data;
410 rv = PR_Recv(clientSocket,
414 PR_INTERVAL_NO_TIMEOUT);
416 if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
417 rv, (_server_data - bytesNeeded), _server_data);
420 if (debug_mode) DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
424 PR_Close(clientSocket);
426 PR_AtomicDecrement(&numRequests);
429 PR_EnterMonitor(clientMonitor);
431 PR_Notify(clientMonitor);
432 PR_ExitMonitor(clientMonitor);
443 numRequests = _iterations;
444 numClients = _clients;
445 clientMonitor = PR_NewMonitor();
447 for (index=0; index<_clients; index++) {
448 PRThread *clientThread;
451 clientThread = PR_CreateThread(
457 PR_UNJOINABLE_THREAD,
461 if (debug_mode) printf("\terror creating client thread %d\n", index);
463 if (debug_mode) DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
467 PR_EnterMonitor(clientMonitor);
469 PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT);
470 PR_ExitMonitor(clientMonitor);
473 /* --- Main Function ---------------------------------------------- */
478 PRThread *ServerThread;
481 SetServerState(MAIN, SERVER_STATE_STARTUP);
482 ServerThread = PR_CreateThread(
491 if (debug_mode) printf("error creating main server thread\n");
495 /* Wait for server to be ready */
496 state = WaitServerState(MAIN, SERVER_STATE_READY|SERVER_STATE_DEAD);
498 if (!(state & SERVER_STATE_DEAD)) {
499 /* Run Test Clients */
502 /* Send death signal to server */
503 SetServerState(MAIN, SERVER_STATE_DYING);
506 PR_JoinThread(ServerThread);
509 static void do_workUU(void)
511 ServerScope = PR_LOCAL_THREAD;
512 ClientScope = PR_LOCAL_THREAD;
518 static void Measure(void (*func)(void), const char *msg)
520 PRIntervalTime start, stop;
523 start = PR_IntervalNow();
525 stop = PR_IntervalNow();
527 d = (double)PR_IntervalToMicroseconds(stop - start);
529 if (debug_mode) printf("\n%40s: %6.2f usec\n", msg, d / _iterations);
533 int main(int argc, char **argv)
535 /* The command line argument: -d is used to determine if the test is being run
536 in debug mode. The regress tool requires only one line output:PASS or FAIL.
537 All of the printfs associated with this test has been handled with a if (debug_mode)
542 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
543 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
545 if (PL_OPT_BAD == os) continue;
548 case 'd': /* debug mode */
555 PL_DestroyOptState(opt);
560 printf("Enter number of iterations: \n");
561 scanf("%d", &_iterations);
562 printf("Enter number of clients : \n");
563 scanf("%d", &_clients);
564 printf("Enter size of client data : \n");
565 scanf("%d", &_client_data);
566 printf("Enter size of server data : \n");
567 scanf("%d", &_server_data);
579 printf("\n\n%d iterations with %d client threads.\n",
580 _iterations, _clients);
581 printf("Sending %d bytes of client data and %d bytes of server data\n",
582 _client_data, _server_data);
584 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
587 PR_SetThreadRecycleMode(64);
589 ServerStateCVLock = PR_NewLock();
590 ServerStateCV = PR_NewCondVar(ServerStateCVLock);
592 Measure(do_workUU, "server loop user/user");