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