1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/servr_ku.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,568 @@ 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 + PR_Lock(workerThreadsLock); 1.162 + if (workerThreadsBusy == workerThreads) { 1.163 + PRThread *WorkerThread; 1.164 + 1.165 + WorkerThread = PR_CreateThread( 1.166 + PR_SYSTEM_THREAD, 1.167 + WorkerThreadFunc, 1.168 + listenSock, 1.169 + PR_PRIORITY_NORMAL, 1.170 + ServerScope, 1.171 + PR_UNJOINABLE_THREAD, 1.172 + THREAD_STACKSIZE); 1.173 + 1.174 + if (!WorkerThread) { 1.175 + if (debug_mode) printf("Error creating client thread %d\n", workerThreads); 1.176 + } else { 1.177 + PR_AtomicIncrement(&workerThreads); 1.178 + if (debug_mode) DPRINTF("\tServer creates worker (%d)\n", workerThreads); 1.179 + } 1.180 + } 1.181 + PR_Unlock(workerThreadsLock); 1.182 + } 1.183 + 1.184 + bytesToRead -= bytesRead; 1.185 + while (bytesToRead) { 1.186 + bytesRead = PR_Recv(newSock, 1.187 + dataBuf, 1.188 + bytesToRead, 1.189 + 0, 1.190 + PR_INTERVAL_NO_TIMEOUT); 1.191 + if (bytesRead < 0) { 1.192 + if (debug_mode) printf("\tServer error receiving data (%d)\n", bytesRead); 1.193 + continue; 1.194 + } 1.195 + if (debug_mode) DPRINTF("\tServer received %d bytes\n", bytesRead); 1.196 + } 1.197 + 1.198 + bytesWritten = PR_Send(newSock, 1.199 + sendBuf, 1.200 + bytesToWrite, 1.201 + 0, 1.202 + PR_INTERVAL_NO_TIMEOUT); 1.203 + if (bytesWritten != _server_data) { 1.204 + if (debug_mode) printf("\tError sending data to client (%d, %d)\n", 1.205 + bytesWritten, PR_GetOSError()); 1.206 + } else { 1.207 + if (debug_mode) DPRINTF("\tServer sent %d bytes\n", bytesWritten); 1.208 + } 1.209 + 1.210 + PR_Close(newSock); 1.211 + PR_AtomicDecrement(&workerThreadsBusy); 1.212 + } 1.213 +} 1.214 + 1.215 +PRFileDesc * 1.216 +ServerSetup(void) 1.217 +{ 1.218 + PRFileDesc *listenSocket; 1.219 + PRSocketOptionData sockOpt; 1.220 + PRNetAddr serverAddr; 1.221 + PRThread *WorkerThread; 1.222 + 1.223 + if ( (listenSocket = PR_NewTCPSocket()) == NULL) { 1.224 + if (debug_mode) printf("\tServer error creating listen socket\n"); 1.225 + else failed_already=1; 1.226 + return NULL; 1.227 + } 1.228 + 1.229 + sockOpt.option = PR_SockOpt_Reuseaddr; 1.230 + sockOpt.value.reuse_addr = PR_TRUE; 1.231 + if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) { 1.232 + if (debug_mode) printf("\tServer error setting socket option: OS error %d\n", 1.233 + PR_GetOSError()); 1.234 + else failed_already=1; 1.235 + PR_Close(listenSocket); 1.236 + return NULL; 1.237 + } 1.238 + 1.239 + memset(&serverAddr, 0, sizeof(PRNetAddr)); 1.240 + serverAddr.inet.family = PR_AF_INET; 1.241 + serverAddr.inet.port = PR_htons(PORT); 1.242 + serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY); 1.243 + 1.244 + if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { 1.245 + if (debug_mode) printf("\tServer error binding to server address: OS error %d\n", 1.246 + PR_GetOSError()); 1.247 + else failed_already=1; 1.248 + PR_Close(listenSocket); 1.249 + return NULL; 1.250 + } 1.251 + 1.252 + if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { 1.253 + if (debug_mode) printf("\tServer error listening to server socket\n"); 1.254 + else failed_already=1; 1.255 + PR_Close(listenSocket); 1.256 + 1.257 + return NULL; 1.258 + } 1.259 + 1.260 + /* Create Clients */ 1.261 + workerThreads = 0; 1.262 + workerThreadsBusy = 0; 1.263 + 1.264 + workerThreadsLock = PR_NewLock(); 1.265 + 1.266 + WorkerThread = PR_CreateThread( 1.267 + PR_SYSTEM_THREAD, 1.268 + WorkerThreadFunc, 1.269 + listenSocket, 1.270 + PR_PRIORITY_NORMAL, 1.271 + ServerScope, 1.272 + PR_UNJOINABLE_THREAD, 1.273 + THREAD_STACKSIZE); 1.274 + 1.275 + if (!WorkerThread) { 1.276 + if (debug_mode) printf("error creating working thread\n"); 1.277 + PR_Close(listenSocket); 1.278 + return NULL; 1.279 + } 1.280 + PR_AtomicIncrement(&workerThreads); 1.281 + if (debug_mode) DPRINTF("\tServer created primordial worker thread\n"); 1.282 + 1.283 + return listenSocket; 1.284 +} 1.285 + 1.286 +/* The main server loop */ 1.287 +void 1.288 +ServerThreadFunc(void *unused) 1.289 +{ 1.290 + PRFileDesc *listenSocket; 1.291 + 1.292 + /* Do setup */ 1.293 + listenSocket = ServerSetup(); 1.294 + 1.295 + if (!listenSocket) { 1.296 + SetServerState(SERVER, SERVER_STATE_DEAD); 1.297 + } else { 1.298 + 1.299 + if (debug_mode) DPRINTF("\tServer up\n"); 1.300 + 1.301 + /* Tell clients they can start now. */ 1.302 + SetServerState(SERVER, SERVER_STATE_READY); 1.303 + 1.304 + /* Now wait for server death signal */ 1.305 + WaitServerState(SERVER, SERVER_STATE_DYING); 1.306 + 1.307 + /* Cleanup */ 1.308 + SetServerState(SERVER, SERVER_STATE_DEAD); 1.309 + } 1.310 +} 1.311 + 1.312 +/* --- Client Functions ------------------------------------------- */ 1.313 + 1.314 +PRInt32 numRequests; 1.315 +PRInt32 numClients; 1.316 +PRMonitor *clientMonitor; 1.317 + 1.318 +void 1.319 +ClientThreadFunc(void *unused) 1.320 +{ 1.321 + PRNetAddr serverAddr; 1.322 + PRFileDesc *clientSocket; 1.323 + char *sendBuf; 1.324 + char *recvBuf; 1.325 + PRInt32 rv; 1.326 + PRInt32 bytesNeeded; 1.327 + 1.328 + sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char)); 1.329 + if (!sendBuf) 1.330 + if (debug_mode) printf("\tClient could not malloc space!?\n"); 1.331 + recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char)); 1.332 + if (!recvBuf) 1.333 + if (debug_mode) printf("\tClient could not malloc space!?\n"); 1.334 + 1.335 + memset(&serverAddr, 0, sizeof(PRNetAddr)); 1.336 + serverAddr.inet.family = PR_AF_INET; 1.337 + serverAddr.inet.port = PR_htons(PORT); 1.338 + serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); 1.339 + 1.340 + while(numRequests > 0) { 1.341 + 1.342 + if ( (numRequests % 10) == 0 ) 1.343 + if (debug_mode) printf("."); 1.344 + if (debug_mode) DPRINTF("\tClient starting request %d\n", numRequests); 1.345 + 1.346 + clientSocket = PR_NewTCPSocket(); 1.347 + if (!clientSocket) { 1.348 + if (debug_mode) printf("Client error creating socket: OS error %d\n", 1.349 + PR_GetOSError()); 1.350 + continue; 1.351 + } 1.352 + 1.353 + if (debug_mode) DPRINTF("\tClient connecting\n"); 1.354 + 1.355 + rv = PR_Connect(clientSocket, 1.356 + &serverAddr, 1.357 + PR_INTERVAL_NO_TIMEOUT); 1.358 + if (!clientSocket) { 1.359 + if (debug_mode) printf("\tClient error connecting\n"); 1.360 + continue; 1.361 + } 1.362 + 1.363 + if (debug_mode) DPRINTF("\tClient connected\n"); 1.364 + 1.365 + rv = PR_Send(clientSocket, 1.366 + sendBuf, 1.367 + _client_data, 1.368 + 0, 1.369 + PR_INTERVAL_NO_TIMEOUT); 1.370 + if (rv != _client_data) { 1.371 + if (debug_mode) printf("Client error sending data (%d)\n", rv); 1.372 + PR_Close(clientSocket); 1.373 + continue; 1.374 + } 1.375 + 1.376 + if (debug_mode) DPRINTF("\tClient sent %d bytes\n", rv); 1.377 + 1.378 + bytesNeeded = _server_data; 1.379 + while(bytesNeeded) { 1.380 + rv = PR_Recv(clientSocket, 1.381 + recvBuf, 1.382 + bytesNeeded, 1.383 + 0, 1.384 + PR_INTERVAL_NO_TIMEOUT); 1.385 + if (rv <= 0) { 1.386 + if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n", 1.387 + rv, (_server_data - bytesNeeded), _server_data); 1.388 + break; 1.389 + } 1.390 + if (debug_mode) DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv); 1.391 + bytesNeeded -= rv; 1.392 + } 1.393 + 1.394 + PR_Close(clientSocket); 1.395 + 1.396 + PR_AtomicDecrement(&numRequests); 1.397 + } 1.398 + 1.399 + PR_EnterMonitor(clientMonitor); 1.400 + --numClients; 1.401 + PR_Notify(clientMonitor); 1.402 + PR_ExitMonitor(clientMonitor); 1.403 + 1.404 + PR_DELETE(sendBuf); 1.405 + PR_DELETE(recvBuf); 1.406 +} 1.407 + 1.408 +void 1.409 +RunClients(void) 1.410 +{ 1.411 + PRInt32 index; 1.412 + 1.413 + numRequests = _iterations; 1.414 + numClients = _clients; 1.415 + clientMonitor = PR_NewMonitor(); 1.416 + 1.417 + for (index=0; index<_clients; index++) { 1.418 + PRThread *clientThread; 1.419 + 1.420 + 1.421 + clientThread = PR_CreateThread( 1.422 + PR_USER_THREAD, 1.423 + ClientThreadFunc, 1.424 + NULL, 1.425 + PR_PRIORITY_NORMAL, 1.426 + ClientScope, 1.427 + PR_UNJOINABLE_THREAD, 1.428 + THREAD_STACKSIZE); 1.429 + 1.430 + if (!clientThread) { 1.431 + if (debug_mode) printf("\terror creating client thread %d\n", index); 1.432 + } else 1.433 + if (debug_mode) DPRINTF("\tMain created client %d/%d\n", index+1, _clients); 1.434 + 1.435 + } 1.436 + 1.437 + PR_EnterMonitor(clientMonitor); 1.438 + while(numClients) 1.439 + PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT); 1.440 + PR_ExitMonitor(clientMonitor); 1.441 +} 1.442 + 1.443 +/* --- Main Function ---------------------------------------------- */ 1.444 + 1.445 +static 1.446 +void do_work() 1.447 +{ 1.448 + PRThread *ServerThread; 1.449 + PRInt32 state; 1.450 + 1.451 + SetServerState(MAIN, SERVER_STATE_STARTUP); 1.452 + ServerThread = PR_CreateThread( 1.453 + PR_USER_THREAD, 1.454 + ServerThreadFunc, 1.455 + NULL, 1.456 + PR_PRIORITY_NORMAL, 1.457 + ServerScope, 1.458 + PR_JOINABLE_THREAD, 1.459 + THREAD_STACKSIZE); 1.460 + if (!ServerThread) { 1.461 + if (debug_mode) printf("error creating main server thread\n"); 1.462 + return; 1.463 + } 1.464 + 1.465 + /* Wait for server to be ready */ 1.466 + state = WaitServerState(MAIN, SERVER_STATE_READY|SERVER_STATE_DEAD); 1.467 + 1.468 + if (!(state & SERVER_STATE_DEAD)) { 1.469 + /* Run Test Clients */ 1.470 + RunClients(); 1.471 + 1.472 + /* Send death signal to server */ 1.473 + SetServerState(MAIN, SERVER_STATE_DYING); 1.474 + } 1.475 + 1.476 + PR_JoinThread(ServerThread); 1.477 +} 1.478 + 1.479 + 1.480 +static void do_workKU(void) 1.481 +{ 1.482 + ServerScope = PR_GLOBAL_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_workKU, "server loop kernel/user"); 1.564 + 1.565 + PR_Cleanup(); 1.566 + if(failed_already) 1.567 + return 1; 1.568 + else 1.569 + return 0; 1.570 + 1.571 +}