Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / nsprpub / pr / tests / servr_uk.c
blob727b7b472ad70f8fb5bc83b550760f98eea1a890
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
13 * License.
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.
22 * Contributor(s):
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 /***********************************************************************
58 ** Includes
59 ***********************************************************************/
60 /* Used to get the command line option */
61 #include "plgetopt.h"
63 #include "nspr.h"
64 #include "pprthred.h"
66 #include <string.h>
68 #define PORT 15004
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"
79 #define MAIN "Main"
81 #define SERVER_STATE_STARTUP 0
82 #define SERVER_STATE_READY 1
83 #define SERVER_STATE_DYING 2
84 #define SERVER_STATE_DEAD 4
85 int ServerState;
86 PRLock *ServerStateCVLock;
87 PRCondVar *ServerStateCV;
89 #ifdef DEBUGPRINTS
90 #define DPRINTF printf
91 #else
92 #define DPRINTF
93 #endif
95 PRIntn failed_already=0;
96 PRIntn debug_mode;
100 static void do_work(void);
102 /* --- Server state functions --------------------------------------------- */
103 void
104 SetServerState(char *waiter, PRInt32 state)
106 PR_Lock(ServerStateCVLock);
107 ServerState = state;
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)
118 PRInt32 rv;
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);
126 rv = ServerState;
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);
132 return rv;
135 /* --- Server Functions ------------------------------------------- */
137 PRLock *workerThreadsLock;
138 PRInt32 workerThreads;
139 PRInt32 workerThreadsBusy;
141 void
142 WorkerThreadFunc(void *_listenSock)
144 PRFileDesc *listenSock = (PRFileDesc *)_listenSock;
145 PRInt32 bytesRead;
146 PRInt32 bytesWritten;
147 char *dataBuf;
148 char *sendBuf;
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);
153 if (!dataBuf)
154 if (debug_mode) printf("\tServer could not malloc space!?\n");
155 sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char));
156 if (!sendBuf)
157 if (debug_mode) printf("\tServer could not malloc space!?\n");
159 if (debug_mode) DPRINTF("\tServer worker thread running\n");
161 while(1) {
162 PRInt32 bytesToRead = _client_data;
163 PRInt32 bytesToWrite = _server_data;
164 PRFileDesc *newSock;
165 PRNetAddr *rAddr;
166 PRInt32 loops = 0;
168 loops++;
170 if (debug_mode) DPRINTF("\tServer thread going into accept\n");
172 bytesRead = PR_AcceptRead(listenSock,
173 &newSock,
174 &rAddr,
175 dataBuf,
176 bytesToRead,
177 PR_INTERVAL_NO_TIMEOUT);
179 if (bytesRead < 0) {
180 if (debug_mode) printf("\tServer error in accept (%d)\n", bytesRead);
181 continue;
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(
194 PR_SYSTEM_THREAD,
195 WorkerThreadFunc,
196 listenSock,
197 PR_PRIORITY_NORMAL,
198 ServerScope,
199 PR_UNJOINABLE_THREAD,
200 THREAD_STACKSIZE);
202 if (!WorkerThread) {
203 if (debug_mode) printf("Error creating client thread %d\n", workerThreads);
204 } else {
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,
215 dataBuf,
216 bytesToRead,
218 PR_INTERVAL_NO_TIMEOUT);
219 if (bytesRead < 0) {
220 if (debug_mode) printf("\tServer error receiving data (%d)\n", bytesRead);
221 continue;
223 if (debug_mode) DPRINTF("\tServer received %d bytes\n", bytesRead);
226 bytesWritten = PR_Send(newSock,
227 sendBuf,
228 bytesToWrite,
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());
234 } else {
235 if (debug_mode) DPRINTF("\tServer sent %d bytes\n", bytesWritten);
238 PR_Close(newSock);
239 PR_AtomicDecrement(&workerThreadsBusy);
243 PRFileDesc *
244 ServerSetup(void)
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");
253 else
254 return NULL;
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",
261 PR_GetOSError());
262 else failed_already=1;
263 PR_Close(listenSocket);
264 return NULL;
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",
274 PR_GetOSError());
275 else failed_already=1;
276 PR_Close(listenSocket);
277 return NULL;
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);
285 return NULL;
288 /* Create Clients */
289 workerThreads = 0;
290 workerThreadsBusy = 0;
292 workerThreadsLock = PR_NewLock();
294 WorkerThread = PR_CreateThread(
295 PR_SYSTEM_THREAD,
296 WorkerThreadFunc,
297 listenSocket,
298 PR_PRIORITY_NORMAL,
299 ServerScope,
300 PR_UNJOINABLE_THREAD,
301 THREAD_STACKSIZE);
303 if (!WorkerThread) {
304 if (debug_mode) printf("error creating working thread\n");
305 PR_Close(listenSocket);
306 return NULL;
308 PR_AtomicIncrement(&workerThreads);
309 if (debug_mode) DPRINTF("\tServer created primordial worker thread\n");
311 return listenSocket;
314 /* The main server loop */
315 void
316 ServerThreadFunc(void *unused)
318 PRFileDesc *listenSocket;
320 /* Do setup */
321 listenSocket = ServerSetup();
323 if (!listenSocket) {
324 SetServerState(SERVER, SERVER_STATE_DEAD);
325 } else {
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);
335 /* Cleanup */
336 SetServerState(SERVER, SERVER_STATE_DEAD);
340 /* --- Client Functions ------------------------------------------- */
342 PRInt32 numRequests;
343 PRInt32 numClients;
344 PRMonitor *clientMonitor;
346 void
347 ClientThreadFunc(void *unused)
349 PRNetAddr serverAddr;
350 PRFileDesc *clientSocket;
351 char *sendBuf;
352 char *recvBuf;
353 PRInt32 rv;
354 PRInt32 bytesNeeded;
356 sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char));
357 if (!sendBuf)
358 if (debug_mode) printf("\tClient could not malloc space!?\n");
359 recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char));
360 if (!recvBuf)
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();
375 if (!clientSocket) {
376 if (debug_mode) printf("Client error creating socket: OS error %d\n",
377 PR_GetOSError());
378 continue;
381 if (debug_mode) DPRINTF("\tClient connecting\n");
383 rv = PR_Connect(clientSocket,
384 &serverAddr,
385 PR_INTERVAL_NO_TIMEOUT);
386 if (!clientSocket) {
387 if (debug_mode) printf("\tClient error connecting\n");
388 continue;
391 if (debug_mode) DPRINTF("\tClient connected\n");
393 rv = PR_Send(clientSocket,
394 sendBuf,
395 _client_data,
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);
401 continue;
404 if (debug_mode) DPRINTF("\tClient sent %d bytes\n", rv);
406 bytesNeeded = _server_data;
407 while(bytesNeeded) {
408 rv = PR_Recv(clientSocket,
409 recvBuf,
410 bytesNeeded,
412 PR_INTERVAL_NO_TIMEOUT);
413 if (rv <= 0) {
414 if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
415 rv, (_server_data - bytesNeeded), _server_data);
416 break;
418 if (debug_mode) DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
419 bytesNeeded -= rv;
422 PR_Close(clientSocket);
424 PR_AtomicDecrement(&numRequests);
427 PR_EnterMonitor(clientMonitor);
428 --numClients;
429 PR_Notify(clientMonitor);
430 PR_ExitMonitor(clientMonitor);
432 PR_DELETE(sendBuf);
433 PR_DELETE(recvBuf);
436 void
437 RunClients(void)
439 PRInt32 index;
441 numRequests = _iterations;
442 numClients = _clients;
443 clientMonitor = PR_NewMonitor();
445 for (index=0; index<_clients; index++) {
446 PRThread *clientThread;
449 clientThread = PR_CreateThread(
450 PR_USER_THREAD,
451 ClientThreadFunc,
452 NULL,
453 PR_PRIORITY_NORMAL,
454 ClientScope,
455 PR_UNJOINABLE_THREAD,
456 THREAD_STACKSIZE);
458 if (!clientThread) {
459 if (debug_mode) printf("\terror creating client thread %d\n", index);
460 } else
461 if (debug_mode) DPRINTF("\tMain created client %d/%d\n", index+1, _clients);
465 PR_EnterMonitor(clientMonitor);
466 while(numClients)
467 PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT);
468 PR_ExitMonitor(clientMonitor);
471 /* --- Main Function ---------------------------------------------- */
473 static
474 void do_work()
476 PRThread *ServerThread;
477 PRInt32 state;
479 SetServerState(MAIN, SERVER_STATE_STARTUP);
480 ServerThread = PR_CreateThread(
481 PR_USER_THREAD,
482 ServerThreadFunc,
483 NULL,
484 PR_PRIORITY_NORMAL,
485 ServerScope,
486 PR_JOINABLE_THREAD,
487 THREAD_STACKSIZE);
488 if (!ServerThread) {
489 if (debug_mode) printf("error creating main server thread\n");
490 return;
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 */
498 RunClients();
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;
512 do_work();
517 static void Measure(void (*func)(void), const char *msg)
519 PRIntervalTime start, stop;
520 double d;
522 start = PR_IntervalNow();
523 (*func)();
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)
537 test.
538 Usage: test_name -d
540 PLOptStatus os;
541 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
542 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
544 if (PL_OPT_BAD == os) continue;
545 switch (opt->option)
547 case 'd': /* debug mode */
548 debug_mode = 1;
549 break;
550 default:
551 break;
554 PL_DestroyOptState(opt);
556 /* main test */
557 if (debug_mode) {
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);
567 else {
568 _iterations = 7;
569 _clients = 7;
570 _client_data = 100;
571 _server_data = 100;
574 if (debug_mode) {
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);
581 PR_STDIO_INIT();
583 PR_SetThreadRecycleMode(64);
585 ServerStateCVLock = PR_NewLock();
586 ServerStateCV = PR_NewCondVar(ServerStateCVLock);
588 Measure(do_workUK, "server loop user/kernel");
590 PR_Cleanup();
592 if(failed_already)
593 return 1;
594 else
595 return 0;