1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/servr_uu.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,569 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/*********************************************************************** 1.10 +** 1.11 +** This server simulates a server running in loopback mode. 1.12 +** 1.13 +** The idea is that a single server is created. The server initially creates 1.14 +** a number of worker threads. Then, with the server running, a number of 1.15 +** clients are created which start requesting service from the server. 1.16 +** 1.17 +** 1.18 +** Modification History: 1.19 +** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.20 +** The debug mode will print all of the printfs associated with this test. 1.21 +** The regress mode will be the default mode. Since the regress tool limits 1.22 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.23 +** have been handled with an if (debug_mode) statement. 1.24 +** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to 1.25 +** recognize the return code from tha main program. 1.26 +***********************************************************************/ 1.27 + 1.28 +/*********************************************************************** 1.29 +** Includes 1.30 +***********************************************************************/ 1.31 +/* Used to get the command line option */ 1.32 +#include "plgetopt.h" 1.33 + 1.34 +#include "nspr.h" 1.35 +#include "pprthred.h" 1.36 + 1.37 +#include <string.h> 1.38 + 1.39 +#define PORT 15004 1.40 +#define THREAD_STACKSIZE 0 1.41 + 1.42 +static int _iterations = 1000; 1.43 +static int _clients = 1; 1.44 +static int _client_data = 250; 1.45 +static int _server_data = (8*1024); 1.46 + 1.47 +static PRThreadScope ServerScope, ClientScope; 1.48 + 1.49 +#define SERVER "Server" 1.50 +#define MAIN "Main" 1.51 + 1.52 +#define SERVER_STATE_STARTUP 0 1.53 +#define SERVER_STATE_READY 1 1.54 +#define SERVER_STATE_DYING 2 1.55 +#define SERVER_STATE_DEAD 4 1.56 +int ServerState; 1.57 +PRLock *ServerStateCVLock; 1.58 +PRCondVar *ServerStateCV; 1.59 + 1.60 +#ifdef DEBUGPRINTS 1.61 +#define DPRINTF printf 1.62 +#else 1.63 +#define DPRINTF 1.64 +#endif 1.65 + 1.66 +PRIntn failed_already=0; 1.67 +PRIntn debug_mode; 1.68 + 1.69 +static void do_work(void); 1.70 + 1.71 +/* --- Server state functions --------------------------------------------- */ 1.72 +void 1.73 +SetServerState(char *waiter, PRInt32 state) 1.74 +{ 1.75 + PR_Lock(ServerStateCVLock); 1.76 + ServerState = state; 1.77 + PR_NotifyCondVar(ServerStateCV); 1.78 + 1.79 + if (debug_mode) DPRINTF("\t%s changed state to %d\n", waiter, state); 1.80 + 1.81 + PR_Unlock(ServerStateCVLock); 1.82 +} 1.83 + 1.84 +int 1.85 +WaitServerState(char *waiter, PRInt32 state) 1.86 +{ 1.87 + PRInt32 rv; 1.88 + 1.89 + PR_Lock(ServerStateCVLock); 1.90 + 1.91 + if (debug_mode) DPRINTF("\t%s waiting for state %d\n", waiter, state); 1.92 + 1.93 + while(!(ServerState & state)) 1.94 + PR_WaitCondVar(ServerStateCV, PR_INTERVAL_NO_TIMEOUT); 1.95 + rv = ServerState; 1.96 + 1.97 + if (debug_mode) DPRINTF("\t%s resuming from wait for state %d; state now %d\n", 1.98 + waiter, state, ServerState); 1.99 + PR_Unlock(ServerStateCVLock); 1.100 + 1.101 + return rv; 1.102 +} 1.103 + 1.104 +/* --- Server Functions ------------------------------------------- */ 1.105 + 1.106 +PRLock *workerThreadsLock; 1.107 +PRInt32 workerThreads; 1.108 +PRInt32 workerThreadsBusy; 1.109 + 1.110 +void 1.111 +WorkerThreadFunc(void *_listenSock) 1.112 +{ 1.113 + PRFileDesc *listenSock = (PRFileDesc *)_listenSock; 1.114 + PRInt32 bytesRead; 1.115 + PRInt32 bytesWritten; 1.116 + char *dataBuf; 1.117 + char *sendBuf; 1.118 + 1.119 + if (debug_mode) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n", 1.120 + _client_data+(2*sizeof(PRNetAddr))+32, _client_data, (2*sizeof(PRNetAddr))+32); 1.121 + dataBuf = (char *)PR_MALLOC(_client_data + 2*sizeof(PRNetAddr) + 32); 1.122 + if (!dataBuf) 1.123 + if (debug_mode) printf("\tServer could not malloc space!?\n"); 1.124 + sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char)); 1.125 + if (!sendBuf) 1.126 + if (debug_mode) printf("\tServer could not malloc space!?\n"); 1.127 + 1.128 + if (debug_mode) DPRINTF("\tServer worker thread running\n"); 1.129 + 1.130 + while(1) { 1.131 + PRInt32 bytesToRead = _client_data; 1.132 + PRInt32 bytesToWrite = _server_data; 1.133 + PRFileDesc *newSock; 1.134 + PRNetAddr *rAddr; 1.135 + PRInt32 loops = 0; 1.136 + 1.137 + loops++; 1.138 + 1.139 + if (debug_mode) DPRINTF("\tServer thread going into accept\n"); 1.140 + 1.141 + bytesRead = PR_AcceptRead(listenSock, 1.142 + &newSock, 1.143 + &rAddr, 1.144 + dataBuf, 1.145 + bytesToRead, 1.146 + PR_INTERVAL_NO_TIMEOUT); 1.147 + 1.148 + if (bytesRead < 0) { 1.149 + if (debug_mode) printf("\tServer error in accept (%d)\n", bytesRead); 1.150 + continue; 1.151 + } 1.152 + 1.153 + if (debug_mode) DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead); 1.154 + 1.155 + PR_AtomicIncrement(&workerThreadsBusy); 1.156 +#ifdef SYMBIAN 1.157 + if (workerThreadsBusy == workerThreads && workerThreads<1) { 1.158 +#else 1.159 + if (workerThreadsBusy == workerThreads) { 1.160 +#endif 1.161 + 1.162 + PR_Lock(workerThreadsLock); 1.163 + if (workerThreadsBusy == workerThreads) { 1.164 + PRThread *WorkerThread; 1.165 + 1.166 + WorkerThread = PR_CreateThread( 1.167 + PR_SYSTEM_THREAD, 1.168 + WorkerThreadFunc, 1.169 + listenSock, 1.170 + PR_PRIORITY_NORMAL, 1.171 + ServerScope, 1.172 + PR_UNJOINABLE_THREAD, 1.173 + THREAD_STACKSIZE); 1.174 + 1.175 + if (!WorkerThread) { 1.176 + if (debug_mode) printf("Error creating client thread %d\n", workerThreads); 1.177 + } else { 1.178 + PR_AtomicIncrement(&workerThreads); 1.179 + if (debug_mode) DPRINTF("\tServer creates worker (%d)\n", workerThreads); 1.180 + } 1.181 + } 1.182 + PR_Unlock(workerThreadsLock); 1.183 + } 1.184 + 1.185 + bytesToRead -= bytesRead; 1.186 + while (bytesToRead) { 1.187 + bytesRead = PR_Recv(newSock, 1.188 + dataBuf, 1.189 + bytesToRead, 1.190 + 0, 1.191 + PR_INTERVAL_NO_TIMEOUT); 1.192 + if (bytesRead < 0) { 1.193 + if (debug_mode) printf("\tServer error receiving data (%d)\n", bytesRead); 1.194 + continue; 1.195 + } 1.196 + if (debug_mode) DPRINTF("\tServer received %d bytes\n", bytesRead); 1.197 + } 1.198 + 1.199 + bytesWritten = PR_Send(newSock, 1.200 + sendBuf, 1.201 + bytesToWrite, 1.202 + 0, 1.203 + PR_INTERVAL_NO_TIMEOUT); 1.204 + if (bytesWritten != _server_data) { 1.205 + if (debug_mode) printf("\tError sending data to client (%d, %d)\n", 1.206 + bytesWritten, PR_GetOSError()); 1.207 + } else { 1.208 + if (debug_mode) DPRINTF("\tServer sent %d bytes\n", bytesWritten); 1.209 + } 1.210 + 1.211 + PR_Close(newSock); 1.212 + PR_AtomicDecrement(&workerThreadsBusy); 1.213 + } 1.214 +} 1.215 + 1.216 +PRFileDesc * 1.217 +ServerSetup(void) 1.218 +{ 1.219 + PRFileDesc *listenSocket; 1.220 + PRSocketOptionData sockOpt; 1.221 + PRNetAddr serverAddr; 1.222 + PRThread *WorkerThread; 1.223 + 1.224 + if ( (listenSocket = PR_NewTCPSocket()) == NULL) { 1.225 + if (debug_mode) printf("\tServer error creating listen socket\n"); 1.226 + else failed_already=1; 1.227 + return NULL; 1.228 + } 1.229 + 1.230 + sockOpt.option = PR_SockOpt_Reuseaddr; 1.231 + sockOpt.value.reuse_addr = PR_TRUE; 1.232 + if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) { 1.233 + if (debug_mode) printf("\tServer error setting socket option: OS error %d\n", 1.234 + PR_GetOSError()); 1.235 + else failed_already=1; 1.236 + PR_Close(listenSocket); 1.237 + return NULL; 1.238 + } 1.239 + 1.240 + memset(&serverAddr, 0, sizeof(PRNetAddr)); 1.241 + serverAddr.inet.family = PR_AF_INET; 1.242 + serverAddr.inet.port = PR_htons(PORT); 1.243 + serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY); 1.244 + 1.245 + if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { 1.246 + if (debug_mode) printf("\tServer error binding to server address: OS error %d\n", 1.247 + PR_GetOSError()); 1.248 + else failed_already=1; 1.249 + PR_Close(listenSocket); 1.250 + return NULL; 1.251 + } 1.252 + 1.253 + if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { 1.254 + if (debug_mode) printf("\tServer error listening to server socket\n"); 1.255 + else failed_already=1; 1.256 + PR_Close(listenSocket); 1.257 + 1.258 + return NULL; 1.259 + } 1.260 + 1.261 + /* Create Clients */ 1.262 + workerThreads = 0; 1.263 + workerThreadsBusy = 0; 1.264 + 1.265 + workerThreadsLock = PR_NewLock(); 1.266 + 1.267 + WorkerThread = PR_CreateThread( 1.268 + PR_SYSTEM_THREAD, 1.269 + WorkerThreadFunc, 1.270 + listenSocket, 1.271 + PR_PRIORITY_NORMAL, 1.272 + ServerScope, 1.273 + PR_UNJOINABLE_THREAD, 1.274 + THREAD_STACKSIZE); 1.275 + 1.276 + if (!WorkerThread) { 1.277 + if (debug_mode) printf("error creating working thread\n"); 1.278 + PR_Close(listenSocket); 1.279 + return NULL; 1.280 + } 1.281 + PR_AtomicIncrement(&workerThreads); 1.282 + if (debug_mode) DPRINTF("\tServer created primordial worker thread\n"); 1.283 + 1.284 + return listenSocket; 1.285 +} 1.286 + 1.287 +/* The main server loop */ 1.288 +void 1.289 +ServerThreadFunc(void *unused) 1.290 +{ 1.291 + PRFileDesc *listenSocket; 1.292 + 1.293 + /* Do setup */ 1.294 + listenSocket = ServerSetup(); 1.295 + 1.296 + if (!listenSocket) { 1.297 + SetServerState(SERVER, SERVER_STATE_DEAD); 1.298 + } else { 1.299 + 1.300 + if (debug_mode) DPRINTF("\tServer up\n"); 1.301 + 1.302 + /* Tell clients they can start now. */ 1.303 + SetServerState(SERVER, SERVER_STATE_READY); 1.304 + 1.305 + /* Now wait for server death signal */ 1.306 + WaitServerState(SERVER, SERVER_STATE_DYING); 1.307 + 1.308 + /* Cleanup */ 1.309 + SetServerState(SERVER, SERVER_STATE_DEAD); 1.310 + } 1.311 +} 1.312 + 1.313 +/* --- Client Functions ------------------------------------------- */ 1.314 + 1.315 +PRInt32 numRequests; 1.316 +PRInt32 numClients; 1.317 +PRMonitor *clientMonitor; 1.318 + 1.319 +void 1.320 +ClientThreadFunc(void *unused) 1.321 +{ 1.322 + PRNetAddr serverAddr; 1.323 + PRFileDesc *clientSocket; 1.324 + char *sendBuf; 1.325 + char *recvBuf; 1.326 + PRInt32 rv; 1.327 + PRInt32 bytesNeeded; 1.328 + 1.329 + sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char)); 1.330 + if (!sendBuf) 1.331 + if (debug_mode) printf("\tClient could not malloc space!?\n"); 1.332 + recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char)); 1.333 + if (!recvBuf) 1.334 + if (debug_mode) printf("\tClient could not malloc space!?\n"); 1.335 + 1.336 + memset(&serverAddr, 0, sizeof(PRNetAddr)); 1.337 + serverAddr.inet.family = PR_AF_INET; 1.338 + serverAddr.inet.port = PR_htons(PORT); 1.339 + serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); 1.340 + 1.341 + while(numRequests > 0) { 1.342 + 1.343 + if ( (numRequests % 10) == 0 ) 1.344 + if (debug_mode) printf("."); 1.345 + if (debug_mode) DPRINTF("\tClient starting request %d\n", numRequests); 1.346 + 1.347 + clientSocket = PR_NewTCPSocket(); 1.348 + if (!clientSocket) { 1.349 + if (debug_mode) printf("Client error creating socket: OS error %d\n", 1.350 + PR_GetOSError()); 1.351 + continue; 1.352 + } 1.353 + 1.354 + if (debug_mode) DPRINTF("\tClient connecting\n"); 1.355 + 1.356 + rv = PR_Connect(clientSocket, 1.357 + &serverAddr, 1.358 + PR_INTERVAL_NO_TIMEOUT); 1.359 + if (!clientSocket) { 1.360 + if (debug_mode) printf("\tClient error connecting\n"); 1.361 + continue; 1.362 + } 1.363 + 1.364 + if (debug_mode) DPRINTF("\tClient connected\n"); 1.365 + 1.366 + rv = PR_Send(clientSocket, 1.367 + sendBuf, 1.368 + _client_data, 1.369 + 0, 1.370 + PR_INTERVAL_NO_TIMEOUT); 1.371 + if (rv != _client_data) { 1.372 + if (debug_mode) printf("Client error sending data (%d)\n", rv); 1.373 + PR_Close(clientSocket); 1.374 + continue; 1.375 + } 1.376 + 1.377 + if (debug_mode) DPRINTF("\tClient sent %d bytes\n", rv); 1.378 + 1.379 + bytesNeeded = _server_data; 1.380 + while(bytesNeeded) { 1.381 + rv = PR_Recv(clientSocket, 1.382 + recvBuf, 1.383 + bytesNeeded, 1.384 + 0, 1.385 + PR_INTERVAL_NO_TIMEOUT); 1.386 + if (rv <= 0) { 1.387 + if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n", 1.388 + rv, (_server_data - bytesNeeded), _server_data); 1.389 + break; 1.390 + } 1.391 + if (debug_mode) DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv); 1.392 + bytesNeeded -= rv; 1.393 + } 1.394 + 1.395 + PR_Close(clientSocket); 1.396 + 1.397 + PR_AtomicDecrement(&numRequests); 1.398 + } 1.399 + 1.400 + PR_EnterMonitor(clientMonitor); 1.401 + --numClients; 1.402 + PR_Notify(clientMonitor); 1.403 + PR_ExitMonitor(clientMonitor); 1.404 + 1.405 + PR_DELETE(sendBuf); 1.406 + PR_DELETE(recvBuf); 1.407 +} 1.408 + 1.409 +void 1.410 +RunClients(void) 1.411 +{ 1.412 + PRInt32 index; 1.413 + 1.414 + numRequests = _iterations; 1.415 + numClients = _clients; 1.416 + clientMonitor = PR_NewMonitor(); 1.417 + 1.418 + for (index=0; index<_clients; index++) { 1.419 + PRThread *clientThread; 1.420 + 1.421 + 1.422 + clientThread = PR_CreateThread( 1.423 + PR_USER_THREAD, 1.424 + ClientThreadFunc, 1.425 + NULL, 1.426 + PR_PRIORITY_NORMAL, 1.427 + ClientScope, 1.428 + PR_UNJOINABLE_THREAD, 1.429 + THREAD_STACKSIZE); 1.430 + 1.431 + if (!clientThread) { 1.432 + if (debug_mode) printf("\terror creating client thread %d\n", index); 1.433 + } else 1.434 + if (debug_mode) DPRINTF("\tMain created client %d/%d\n", index+1, _clients); 1.435 + 1.436 + } 1.437 + 1.438 + PR_EnterMonitor(clientMonitor); 1.439 + while(numClients) 1.440 + PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT); 1.441 + PR_ExitMonitor(clientMonitor); 1.442 +} 1.443 + 1.444 +/* --- Main Function ---------------------------------------------- */ 1.445 + 1.446 +static 1.447 +void do_work() 1.448 +{ 1.449 + PRThread *ServerThread; 1.450 + PRInt32 state; 1.451 + 1.452 + SetServerState(MAIN, SERVER_STATE_STARTUP); 1.453 + ServerThread = PR_CreateThread( 1.454 + PR_USER_THREAD, 1.455 + ServerThreadFunc, 1.456 + NULL, 1.457 + PR_PRIORITY_NORMAL, 1.458 + ServerScope, 1.459 + PR_JOINABLE_THREAD, 1.460 + THREAD_STACKSIZE); 1.461 + if (!ServerThread) { 1.462 + if (debug_mode) printf("error creating main server thread\n"); 1.463 + return; 1.464 + } 1.465 + 1.466 + /* Wait for server to be ready */ 1.467 + state = WaitServerState(MAIN, SERVER_STATE_READY|SERVER_STATE_DEAD); 1.468 + 1.469 + if (!(state & SERVER_STATE_DEAD)) { 1.470 + /* Run Test Clients */ 1.471 + RunClients(); 1.472 + 1.473 + /* Send death signal to server */ 1.474 + SetServerState(MAIN, SERVER_STATE_DYING); 1.475 + } 1.476 + 1.477 + PR_JoinThread(ServerThread); 1.478 +} 1.479 + 1.480 +static void do_workUU(void) 1.481 +{ 1.482 + ServerScope = PR_LOCAL_THREAD; 1.483 + ClientScope = PR_LOCAL_THREAD; 1.484 + do_work(); 1.485 +} 1.486 + 1.487 + 1.488 + 1.489 +static void Measure(void (*func)(void), const char *msg) 1.490 +{ 1.491 + PRIntervalTime start, stop; 1.492 + double d; 1.493 + 1.494 + start = PR_IntervalNow(); 1.495 + (*func)(); 1.496 + stop = PR_IntervalNow(); 1.497 + 1.498 + d = (double)PR_IntervalToMicroseconds(stop - start); 1.499 + 1.500 + if (debug_mode) printf("\n%40s: %6.2f usec\n", msg, d / _iterations); 1.501 +} 1.502 + 1.503 + 1.504 +int main(int argc, char **argv) 1.505 +{ 1.506 + /* The command line argument: -d is used to determine if the test is being run 1.507 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.508 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.509 + test. 1.510 + Usage: test_name -d 1.511 + */ 1.512 + PLOptStatus os; 1.513 + PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 1.514 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.515 + { 1.516 + if (PL_OPT_BAD == os) continue; 1.517 + switch (opt->option) 1.518 + { 1.519 + case 'd': /* debug mode */ 1.520 + debug_mode = 1; 1.521 + break; 1.522 + default: 1.523 + break; 1.524 + } 1.525 + } 1.526 + PL_DestroyOptState(opt); 1.527 + 1.528 + /* main test */ 1.529 +#ifndef SYMBIAN 1.530 + if (debug_mode) { 1.531 + printf("Enter number of iterations: \n"); 1.532 + scanf("%d", &_iterations); 1.533 + printf("Enter number of clients : \n"); 1.534 + scanf("%d", &_clients); 1.535 + printf("Enter size of client data : \n"); 1.536 + scanf("%d", &_client_data); 1.537 + printf("Enter size of server data : \n"); 1.538 + scanf("%d", &_server_data); 1.539 + } 1.540 + else 1.541 +#endif 1.542 + { 1.543 + _iterations = 7; 1.544 + _clients = 7; 1.545 + _client_data = 100; 1.546 + _server_data = 100; 1.547 + } 1.548 + 1.549 + if (debug_mode) { 1.550 + printf("\n\n%d iterations with %d client threads.\n", 1.551 + _iterations, _clients); 1.552 + printf("Sending %d bytes of client data and %d bytes of server data\n", 1.553 + _client_data, _server_data); 1.554 + } 1.555 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.556 + PR_STDIO_INIT(); 1.557 + 1.558 + PR_SetThreadRecycleMode(64); 1.559 + 1.560 + ServerStateCVLock = PR_NewLock(); 1.561 + ServerStateCV = PR_NewCondVar(ServerStateCVLock); 1.562 + 1.563 + Measure(do_workUU, "server loop user/user"); 1.564 + 1.565 + PR_Cleanup(); 1.566 + 1.567 + if(failed_already) 1.568 + return 1; 1.569 + else 1.570 + return 0; 1.571 + 1.572 +}