Bug 1825055 [wpt PR 39247] - Add support for allowing elements with display:contents...
[gecko.git] / nsprpub / pr / tests / servr_kk.c
blob06f4fae117d61981a2539d3b3ef1c089aee7871a
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 /***********************************************************************
7 **
8 ** This server simulates a server running in loopback mode.
9 **
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 /***********************************************************************
26 ** Includes
27 ***********************************************************************/
28 /* Used to get the command line option */
29 #include "plgetopt.h"
31 #include "nspr.h"
32 #include "pprthred.h"
34 #include <string.h>
36 #define PORT 15004
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"
47 #define MAIN "Main"
49 #define SERVER_STATE_STARTUP 0
50 #define SERVER_STATE_READY 1
51 #define SERVER_STATE_DYING 2
52 #define SERVER_STATE_DEAD 4
53 int ServerState;
54 PRLock *ServerStateCVLock;
55 PRCondVar *ServerStateCV;
57 #ifdef DEBUGPRINTS
58 #define DPRINTF printf
59 #else
60 #define DPRINTF
61 #endif
63 PRIntn failed_already=0;
64 PRIntn debug_mode;
65 static void do_work(void);
67 /* --- Server state functions --------------------------------------------- */
68 void
69 SetServerState(char *waiter, PRInt32 state)
71 PR_Lock(ServerStateCVLock);
72 ServerState = state;
73 PR_NotifyCondVar(ServerStateCV);
75 if (debug_mode) {
76 DPRINTF("\t%s changed state to %d\n", waiter, state);
79 PR_Unlock(ServerStateCVLock);
82 int
83 WaitServerState(char *waiter, PRInt32 state)
85 PRInt32 rv;
87 PR_Lock(ServerStateCVLock);
89 if (debug_mode) {
90 DPRINTF("\t%s waiting for state %d\n", waiter, state);
93 while(!(ServerState & state)) {
94 PR_WaitCondVar(ServerStateCV, PR_INTERVAL_NO_TIMEOUT);
96 rv = ServerState;
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);
102 return rv;
105 /* --- Server Functions ------------------------------------------- */
107 PRLock *workerThreadsLock;
108 PRInt32 workerThreads;
109 PRInt32 workerThreadsBusy;
111 void
112 WorkerThreadFunc(void *_listenSock)
114 PRFileDesc *listenSock = (PRFileDesc *)_listenSock;
115 PRInt32 bytesRead;
116 PRInt32 bytesWritten;
117 char *dataBuf;
118 char *sendBuf;
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);
123 if (!dataBuf)
124 if (debug_mode) {
125 printf("\tServer could not malloc space!?\n");
127 sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char));
128 if (!sendBuf)
129 if (debug_mode) {
130 printf("\tServer could not malloc space!?\n");
133 if (debug_mode) {
134 DPRINTF("\tServer worker thread running\n");
137 while(1) {
138 PRInt32 bytesToRead = _client_data;
139 PRInt32 bytesToWrite = _server_data;
140 PRFileDesc *newSock;
141 PRNetAddr *rAddr;
142 PRInt32 loops = 0;
144 loops++;
146 if (debug_mode) {
147 DPRINTF("\tServer thread going into accept\n");
150 bytesRead = PR_AcceptRead(listenSock,
151 &newSock,
152 &rAddr,
153 dataBuf,
154 bytesToRead,
155 PR_INTERVAL_NO_TIMEOUT);
157 if (bytesRead < 0) {
158 if (debug_mode) {
159 printf("\tServer error in accept (%d)\n", bytesRead);
161 continue;
164 if (debug_mode) {
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(
175 PR_SYSTEM_THREAD,
176 WorkerThreadFunc,
177 listenSock,
178 PR_PRIORITY_NORMAL,
179 ServerScope,
180 PR_UNJOINABLE_THREAD,
181 THREAD_STACKSIZE);
183 if (!WorkerThread) {
184 if (debug_mode) {
185 printf("Error creating client thread %d\n", workerThreads);
187 } else {
188 PR_AtomicIncrement(&workerThreads);
189 if (debug_mode) {
190 DPRINTF("\tServer creates worker (%d)\n", workerThreads);
194 PR_Unlock(workerThreadsLock);
197 bytesToRead -= bytesRead;
198 while (bytesToRead) {
199 bytesRead = PR_Recv(newSock,
200 dataBuf,
201 bytesToRead,
203 PR_INTERVAL_NO_TIMEOUT);
204 if (bytesRead < 0) {
205 if (debug_mode) {
206 printf("\tServer error receiving data (%d)\n", bytesRead);
208 continue;
210 if (debug_mode) {
211 DPRINTF("\tServer received %d bytes\n", bytesRead);
215 bytesWritten = PR_Send(newSock,
216 sendBuf,
217 bytesToWrite,
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());
223 } else {
224 if (debug_mode) {
225 DPRINTF("\tServer sent %d bytes\n", bytesWritten);
229 PR_Close(newSock);
230 PR_AtomicDecrement(&workerThreadsBusy);
234 PRFileDesc *
235 ServerSetup(void)
237 PRFileDesc *listenSocket;
238 PRSocketOptionData sockOpt;
239 PRNetAddr serverAddr;
240 PRThread *WorkerThread;
242 if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
243 if (debug_mode) {
244 printf("\tServer error creating listen socket\n");
246 else {
247 failed_already=1;
249 return NULL;
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",
256 PR_GetOSError());
257 else {
258 failed_already=1;
260 PR_Close(listenSocket);
261 return NULL;
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",
271 PR_GetOSError());
272 else {
273 failed_already=1;
275 PR_Close(listenSocket);
276 return NULL;
279 if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
280 if (debug_mode) {
281 printf("\tServer error listening to server socket\n");
283 else {
284 failed_already=1;
286 PR_Close(listenSocket);
288 return NULL;
291 /* Create Clients */
292 workerThreads = 0;
293 workerThreadsBusy = 0;
295 workerThreadsLock = PR_NewLock();
297 WorkerThread = PR_CreateThread(
298 PR_SYSTEM_THREAD,
299 WorkerThreadFunc,
300 listenSocket,
301 PR_PRIORITY_NORMAL,
302 ServerScope,
303 PR_UNJOINABLE_THREAD,
304 THREAD_STACKSIZE);
306 if (!WorkerThread) {
307 if (debug_mode) {
308 printf("error creating working thread\n");
310 PR_Close(listenSocket);
311 return NULL;
313 PR_AtomicIncrement(&workerThreads);
314 if (debug_mode) {
315 DPRINTF("\tServer created primordial worker thread\n");
318 return listenSocket;
321 /* The main server loop */
322 void
323 ServerThreadFunc(void *unused)
325 PRFileDesc *listenSocket;
327 /* Do setup */
328 listenSocket = ServerSetup();
330 if (!listenSocket) {
331 SetServerState(SERVER, SERVER_STATE_DEAD);
332 } else {
334 if (debug_mode) {
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);
344 /* Cleanup */
345 SetServerState(SERVER, SERVER_STATE_DEAD);
349 /* --- Client Functions ------------------------------------------- */
351 PRInt32 numRequests;
352 PRInt32 numClients;
353 PRMonitor *clientMonitor;
355 void
356 ClientThreadFunc(void *unused)
358 PRNetAddr serverAddr;
359 PRFileDesc *clientSocket;
360 char *sendBuf;
361 char *recvBuf;
362 PRInt32 rv;
363 PRInt32 bytesNeeded;
365 sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char));
366 if (!sendBuf)
367 if (debug_mode) {
368 printf("\tClient could not malloc space!?\n");
370 recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char));
371 if (!recvBuf)
372 if (debug_mode) {
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 )
384 if (debug_mode) {
385 printf(".");
387 if (debug_mode) {
388 DPRINTF("\tClient starting request %d\n", numRequests);
391 clientSocket = PR_NewTCPSocket();
392 if (!clientSocket) {
393 if (debug_mode) printf("Client error creating socket: OS error %d\n",
394 PR_GetOSError());
395 continue;
398 if (debug_mode) {
399 DPRINTF("\tClient connecting\n");
402 rv = PR_Connect(clientSocket,
403 &serverAddr,
404 PR_INTERVAL_NO_TIMEOUT);
405 if (!clientSocket) {
406 if (debug_mode) {
407 printf("\tClient error connecting\n");
409 continue;
412 if (debug_mode) {
413 DPRINTF("\tClient connected\n");
416 rv = PR_Send(clientSocket,
417 sendBuf,
418 _client_data,
420 PR_INTERVAL_NO_TIMEOUT);
421 if (rv != _client_data) {
422 if (debug_mode) {
423 printf("Client error sending data (%d)\n", rv);
425 PR_Close(clientSocket);
426 continue;
429 if (debug_mode) {
430 DPRINTF("\tClient sent %d bytes\n", rv);
433 bytesNeeded = _server_data;
434 while(bytesNeeded) {
435 rv = PR_Recv(clientSocket,
436 recvBuf,
437 bytesNeeded,
439 PR_INTERVAL_NO_TIMEOUT);
440 if (rv <= 0) {
441 if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n",
442 rv, (_server_data - bytesNeeded), _server_data);
443 break;
445 if (debug_mode) {
446 DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv);
448 bytesNeeded -= rv;
451 PR_Close(clientSocket);
453 PR_AtomicDecrement(&numRequests);
456 PR_EnterMonitor(clientMonitor);
457 --numClients;
458 PR_Notify(clientMonitor);
459 PR_ExitMonitor(clientMonitor);
461 PR_DELETE(sendBuf);
462 PR_DELETE(recvBuf);
465 void
466 RunClients(void)
468 PRInt32 index;
470 numRequests = _iterations;
471 numClients = _clients;
472 clientMonitor = PR_NewMonitor();
474 for (index=0; index<_clients; index++) {
475 PRThread *clientThread;
478 clientThread = PR_CreateThread(
479 PR_USER_THREAD,
480 ClientThreadFunc,
481 NULL,
482 PR_PRIORITY_NORMAL,
483 ClientScope,
484 PR_UNJOINABLE_THREAD,
485 THREAD_STACKSIZE);
487 if (!clientThread) {
488 if (debug_mode) {
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);
498 while(numClients) {
499 PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT);
501 PR_ExitMonitor(clientMonitor);
504 /* --- Main Function ---------------------------------------------- */
506 static
507 void do_work()
509 PRThread *ServerThread;
510 PRInt32 state;
512 SetServerState(MAIN, SERVER_STATE_STARTUP);
513 ServerThread = PR_CreateThread(
514 PR_USER_THREAD,
515 ServerThreadFunc,
516 NULL,
517 PR_PRIORITY_NORMAL,
518 ServerScope,
519 PR_JOINABLE_THREAD,
520 THREAD_STACKSIZE);
521 if (!ServerThread) {
522 if (debug_mode) {
523 printf("error creating main server thread\n");
525 return;
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 */
533 RunClients();
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;
546 do_work();
549 static void do_workUK(void)
551 ServerScope = PR_LOCAL_THREAD;
552 ClientScope = PR_GLOBAL_THREAD;
553 do_work();
556 static void do_workKU(void)
558 ServerScope = PR_GLOBAL_THREAD;
559 ClientScope = PR_LOCAL_THREAD;
560 do_work();
563 static void do_workKK(void)
565 ServerScope = PR_GLOBAL_THREAD;
566 ClientScope = PR_GLOBAL_THREAD;
567 do_work();
571 static void Measure(void (*func)(void), const char *msg)
573 PRIntervalTime start, stop;
574 double d;
576 start = PR_IntervalNow();
577 (*func)();
578 stop = PR_IntervalNow();
580 d = (double)PR_IntervalToMicroseconds(stop - start);
582 if (debug_mode) {
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)
593 test.
594 Usage: test_name -d
596 PLOptStatus os;
597 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
598 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
600 if (PL_OPT_BAD == os) {
601 continue;
603 switch (opt->option)
605 case 'd': /* debug mode */
606 debug_mode = 1;
607 break;
608 default:
609 break;
612 PL_DestroyOptState(opt);
614 /* main test */
615 if (debug_mode) {
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);
625 else
627 _iterations = 7;
628 _clients = 7;
629 _client_data = 100;
630 _server_data = 100;
633 if (debug_mode) {
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);
640 PR_STDIO_INIT();
642 PR_SetThreadRecycleMode(64);
644 ServerStateCVLock = PR_NewLock();
645 ServerStateCV = PR_NewCondVar(ServerStateCVLock);
648 Measure(do_workKK, "server loop kernel/kernel");
650 PR_Cleanup();
652 if(failed_already) {
653 return 1;
655 else {
656 return 0;