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