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) {
190 PR_Lock(workerThreadsLock);
191 if (workerThreadsBusy == workerThreads) {
192 PRThread *WorkerThread;
194 WorkerThread = PR_CreateThread(
200 PR_UNJOINABLE_THREAD,
204 if (debug_mode) printf("Error creating client thread %d\n", workerThreads);
206 PR_AtomicIncrement(&workerThreads);
207 if (debug_mode) DPRINTF("\tServer creates worker (%d)\n", workerThreads);
210 PR_Unlock(workerThreadsLock);
213 bytesToRead -= bytesRead;
214 while (bytesToRead) {
215 bytesRead = PR_Recv(newSock,
219 PR_INTERVAL_NO_TIMEOUT);
221 if (debug_mode) printf("\tServer error receiving data (%d)\n", bytesRead);
224 if (debug_mode) DPRINTF("\tServer received %d bytes\n", bytesRead);
227 bytesWritten = PR_Send(newSock,
231 PR_INTERVAL_NO_TIMEOUT);
232 if (bytesWritten != _server_data) {
233 if (debug_mode) printf("\tError sending data to client (%d, %d)\n",
234 bytesWritten, PR_GetOSError());
236 if (debug_mode) DPRINTF("\tServer sent %d bytes\n", bytesWritten);
240 PR_AtomicDecrement(&workerThreadsBusy);
247 PRFileDesc *listenSocket;
248 PRSocketOptionData sockOpt;
249 PRNetAddr serverAddr;
250 PRThread *WorkerThread;
252 if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
253 if (debug_mode) printf("\tServer error creating listen socket\n");
254 else failed_already=1;
258 sockOpt.option = PR_SockOpt_Reuseaddr;
259 sockOpt.value.reuse_addr = PR_TRUE;
260 if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) {
261 if (debug_mode) printf("\tServer error setting socket option: OS error %d\n",
263 else failed_already=1;
264 PR_Close(listenSocket);
268 memset(&serverAddr, 0, sizeof(PRNetAddr));
269 serverAddr.inet.family = PR_AF_INET;
270 serverAddr.inet.port = PR_htons(PORT);
271 serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
273 if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
274 if (debug_mode) printf("\tServer error binding to server address: OS error %d\n",
276 else failed_already=1;
277 PR_Close(listenSocket);
281 if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
282 if (debug_mode) printf("\tServer error listening to server socket\n");
283 else failed_already=1;
284 PR_Close(listenSocket);
291 workerThreadsBusy = 0;
293 workerThreadsLock = PR_NewLock();
295 WorkerThread = PR_CreateThread(
301 PR_UNJOINABLE_THREAD,
305 if (debug_mode) printf("error creating working thread\n");
306 PR_Close(listenSocket);
309 PR_AtomicIncrement(&workerThreads);
310 if (debug_mode) DPRINTF("\tServer created primordial worker thread\n");
315 /* The main server loop */
317 ServerThreadFunc(void *unused)
319 PRFileDesc *listenSocket;
322 listenSocket = ServerSetup();
325 SetServerState(SERVER, SERVER_STATE_DEAD);
328 if (debug_mode) DPRINTF("\tServer up\n");
330 /* Tell clients they can start now. */
331 SetServerState(SERVER, SERVER_STATE_READY);
333 /* Now wait for server death signal */
334 WaitServerState(SERVER, SERVER_STATE_DYING);
337 SetServerState(SERVER, SERVER_STATE_DEAD);
341 /* --- Client Functions ------------------------------------------- */
345 PRMonitor *clientMonitor;
348 ClientThreadFunc(void *unused)
350 PRNetAddr serverAddr;
351 PRFileDesc *clientSocket;
357 sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char));
359 if (debug_mode) printf("\tClient could not malloc space!?\n");
360 recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char));
362 if (debug_mode) printf("\tClient could not malloc space!?\n");
364 memset(&serverAddr, 0, sizeof(PRNetAddr));
365 serverAddr.inet.family = PR_AF_INET;
366 serverAddr.inet.port = PR_htons(PORT);
367 serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
369 while(numRequests > 0) {
371 if ( (numRequests % 10) == 0 )
372 if (debug_mode) printf(".");
373 if (debug_mode) DPRINTF("\tClient starting request %d\n", numRequests);
375 clientSocket = PR_NewTCPSocket();
377 if (debug_mode) printf("Client error creating socket: OS error %d\n",
382 if (debug_mode) DPRINTF("\tClient connecting\n");
384 rv = PR_Connect(clientSocket,
386 PR_INTERVAL_NO_TIMEOUT);
388 if (debug_mode) printf("\tClient error connecting\n");
392 if (debug_mode) DPRINTF("\tClient connected\n");
394 rv = PR_Send(clientSocket,
398 PR_INTERVAL_NO_TIMEOUT);
399 if (rv != _client_data) {
400 if (debug_mode) printf("Client error sending data (%d)\n", rv);
401 PR_Close(clientSocket);
405 if (debug_mode) DPRINTF("\tClient sent %d bytes\n", rv);
407 bytesNeeded = _server_data;
409 rv = PR_Recv(clientSocket,
413 PR_INTERVAL_NO_TIMEOUT);
415 if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
416 rv, (_server_data - bytesNeeded), _server_data);
419 if (debug_mode) DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
423 PR_Close(clientSocket);
425 PR_AtomicDecrement(&numRequests);
428 PR_EnterMonitor(clientMonitor);
430 PR_Notify(clientMonitor);
431 PR_ExitMonitor(clientMonitor);
442 numRequests = _iterations;
443 numClients = _clients;
444 clientMonitor = PR_NewMonitor();
446 for (index=0; index<_clients; index++) {
447 PRThread *clientThread;
450 clientThread = PR_CreateThread(
456 PR_UNJOINABLE_THREAD,
460 if (debug_mode) printf("\terror creating client thread %d\n", index);
462 if (debug_mode) DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
466 PR_EnterMonitor(clientMonitor);
468 PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT);
469 PR_ExitMonitor(clientMonitor);
472 /* --- Main Function ---------------------------------------------- */
477 PRThread *ServerThread;
480 SetServerState(MAIN, SERVER_STATE_STARTUP);
481 ServerThread = PR_CreateThread(
490 if (debug_mode) printf("error creating main server thread\n");
494 /* Wait for server to be ready */
495 state = WaitServerState(MAIN, SERVER_STATE_READY|SERVER_STATE_DEAD);
497 if (!(state & SERVER_STATE_DEAD)) {
498 /* Run Test Clients */
501 /* Send death signal to server */
502 SetServerState(MAIN, SERVER_STATE_DYING);
505 PR_JoinThread(ServerThread);
509 static void do_workKU(void)
511 ServerScope = PR_GLOBAL_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_workKU, "server loop kernel/user");