Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /*********************************************************************** |
michael@0 | 7 | ** |
michael@0 | 8 | ** This server simulates a server running in loopback mode. |
michael@0 | 9 | ** |
michael@0 | 10 | ** The idea is that a single server is created. The server initially creates |
michael@0 | 11 | ** a number of worker threads. Then, with the server running, a number of |
michael@0 | 12 | ** clients are created which start requesting service from the server. |
michael@0 | 13 | ** |
michael@0 | 14 | ** |
michael@0 | 15 | ** Modification History: |
michael@0 | 16 | ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. |
michael@0 | 17 | ** The debug mode will print all of the printfs associated with this test. |
michael@0 | 18 | ** The regress mode will be the default mode. Since the regress tool limits |
michael@0 | 19 | ** the output to a one line status:PASS or FAIL,all of the printf statements |
michael@0 | 20 | ** have been handled with an if (debug_mode) statement. |
michael@0 | 21 | ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to |
michael@0 | 22 | ** recognize the return code from tha main program. |
michael@0 | 23 | ***********************************************************************/ |
michael@0 | 24 | |
michael@0 | 25 | /*********************************************************************** |
michael@0 | 26 | ** Includes |
michael@0 | 27 | ***********************************************************************/ |
michael@0 | 28 | /* Used to get the command line option */ |
michael@0 | 29 | #include "plgetopt.h" |
michael@0 | 30 | |
michael@0 | 31 | #include "nspr.h" |
michael@0 | 32 | #include "pprthred.h" |
michael@0 | 33 | |
michael@0 | 34 | #include <string.h> |
michael@0 | 35 | |
michael@0 | 36 | #define PORT 15004 |
michael@0 | 37 | #define THREAD_STACKSIZE 0 |
michael@0 | 38 | |
michael@0 | 39 | static int _iterations = 1000; |
michael@0 | 40 | static int _clients = 1; |
michael@0 | 41 | static int _client_data = 250; |
michael@0 | 42 | static int _server_data = (8*1024); |
michael@0 | 43 | |
michael@0 | 44 | static PRThreadScope ServerScope, ClientScope; |
michael@0 | 45 | |
michael@0 | 46 | #define SERVER "Server" |
michael@0 | 47 | #define MAIN "Main" |
michael@0 | 48 | |
michael@0 | 49 | #define SERVER_STATE_STARTUP 0 |
michael@0 | 50 | #define SERVER_STATE_READY 1 |
michael@0 | 51 | #define SERVER_STATE_DYING 2 |
michael@0 | 52 | #define SERVER_STATE_DEAD 4 |
michael@0 | 53 | int ServerState; |
michael@0 | 54 | PRLock *ServerStateCVLock; |
michael@0 | 55 | PRCondVar *ServerStateCV; |
michael@0 | 56 | |
michael@0 | 57 | #ifdef DEBUGPRINTS |
michael@0 | 58 | #define DPRINTF printf |
michael@0 | 59 | #else |
michael@0 | 60 | #define DPRINTF |
michael@0 | 61 | #endif |
michael@0 | 62 | |
michael@0 | 63 | PRIntn failed_already=0; |
michael@0 | 64 | PRIntn debug_mode; |
michael@0 | 65 | |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | static void do_work(void); |
michael@0 | 69 | |
michael@0 | 70 | /* --- Server state functions --------------------------------------------- */ |
michael@0 | 71 | void |
michael@0 | 72 | SetServerState(char *waiter, PRInt32 state) |
michael@0 | 73 | { |
michael@0 | 74 | PR_Lock(ServerStateCVLock); |
michael@0 | 75 | ServerState = state; |
michael@0 | 76 | PR_NotifyCondVar(ServerStateCV); |
michael@0 | 77 | |
michael@0 | 78 | if (debug_mode) DPRINTF("\t%s changed state to %d\n", waiter, state); |
michael@0 | 79 | |
michael@0 | 80 | PR_Unlock(ServerStateCVLock); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | int |
michael@0 | 84 | WaitServerState(char *waiter, PRInt32 state) |
michael@0 | 85 | { |
michael@0 | 86 | PRInt32 rv; |
michael@0 | 87 | |
michael@0 | 88 | PR_Lock(ServerStateCVLock); |
michael@0 | 89 | |
michael@0 | 90 | if (debug_mode) DPRINTF("\t%s waiting for state %d\n", waiter, state); |
michael@0 | 91 | |
michael@0 | 92 | while(!(ServerState & state)) |
michael@0 | 93 | PR_WaitCondVar(ServerStateCV, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 94 | rv = ServerState; |
michael@0 | 95 | |
michael@0 | 96 | if (debug_mode) DPRINTF("\t%s resuming from wait for state %d; state now %d\n", |
michael@0 | 97 | waiter, state, ServerState); |
michael@0 | 98 | PR_Unlock(ServerStateCVLock); |
michael@0 | 99 | |
michael@0 | 100 | return rv; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /* --- Server Functions ------------------------------------------- */ |
michael@0 | 104 | |
michael@0 | 105 | PRLock *workerThreadsLock; |
michael@0 | 106 | PRInt32 workerThreads; |
michael@0 | 107 | PRInt32 workerThreadsBusy; |
michael@0 | 108 | |
michael@0 | 109 | void |
michael@0 | 110 | WorkerThreadFunc(void *_listenSock) |
michael@0 | 111 | { |
michael@0 | 112 | PRFileDesc *listenSock = (PRFileDesc *)_listenSock; |
michael@0 | 113 | PRInt32 bytesRead; |
michael@0 | 114 | PRInt32 bytesWritten; |
michael@0 | 115 | char *dataBuf; |
michael@0 | 116 | char *sendBuf; |
michael@0 | 117 | |
michael@0 | 118 | if (debug_mode) DPRINTF("\tServer buffer is %d bytes; %d data, %d netaddrs\n", |
michael@0 | 119 | _client_data+(2*sizeof(PRNetAddr))+32, _client_data, (2*sizeof(PRNetAddr))+32); |
michael@0 | 120 | dataBuf = (char *)PR_MALLOC(_client_data + 2*sizeof(PRNetAddr) + 32); |
michael@0 | 121 | if (!dataBuf) |
michael@0 | 122 | if (debug_mode) printf("\tServer could not malloc space!?\n"); |
michael@0 | 123 | sendBuf = (char *)PR_MALLOC(_server_data *sizeof(char)); |
michael@0 | 124 | if (!sendBuf) |
michael@0 | 125 | if (debug_mode) printf("\tServer could not malloc space!?\n"); |
michael@0 | 126 | |
michael@0 | 127 | if (debug_mode) DPRINTF("\tServer worker thread running\n"); |
michael@0 | 128 | |
michael@0 | 129 | while(1) { |
michael@0 | 130 | PRInt32 bytesToRead = _client_data; |
michael@0 | 131 | PRInt32 bytesToWrite = _server_data; |
michael@0 | 132 | PRFileDesc *newSock; |
michael@0 | 133 | PRNetAddr *rAddr; |
michael@0 | 134 | PRInt32 loops = 0; |
michael@0 | 135 | |
michael@0 | 136 | loops++; |
michael@0 | 137 | |
michael@0 | 138 | if (debug_mode) DPRINTF("\tServer thread going into accept\n"); |
michael@0 | 139 | |
michael@0 | 140 | bytesRead = PR_AcceptRead(listenSock, |
michael@0 | 141 | &newSock, |
michael@0 | 142 | &rAddr, |
michael@0 | 143 | dataBuf, |
michael@0 | 144 | bytesToRead, |
michael@0 | 145 | PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 146 | |
michael@0 | 147 | if (bytesRead < 0) { |
michael@0 | 148 | if (debug_mode) printf("\tServer error in accept (%d)\n", bytesRead); |
michael@0 | 149 | continue; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | if (debug_mode) DPRINTF("\tServer accepted connection (%d bytes)\n", bytesRead); |
michael@0 | 153 | |
michael@0 | 154 | PR_AtomicIncrement(&workerThreadsBusy); |
michael@0 | 155 | #ifdef SYMBIAN |
michael@0 | 156 | if (workerThreadsBusy == workerThreads && workerThreads<1) { |
michael@0 | 157 | #else |
michael@0 | 158 | if (workerThreadsBusy == workerThreads) { |
michael@0 | 159 | #endif |
michael@0 | 160 | PR_Lock(workerThreadsLock); |
michael@0 | 161 | if (workerThreadsBusy == workerThreads) { |
michael@0 | 162 | PRThread *WorkerThread; |
michael@0 | 163 | |
michael@0 | 164 | WorkerThread = PR_CreateThread( |
michael@0 | 165 | PR_SYSTEM_THREAD, |
michael@0 | 166 | WorkerThreadFunc, |
michael@0 | 167 | listenSock, |
michael@0 | 168 | PR_PRIORITY_NORMAL, |
michael@0 | 169 | ServerScope, |
michael@0 | 170 | PR_UNJOINABLE_THREAD, |
michael@0 | 171 | THREAD_STACKSIZE); |
michael@0 | 172 | |
michael@0 | 173 | if (!WorkerThread) { |
michael@0 | 174 | if (debug_mode) printf("Error creating client thread %d\n", workerThreads); |
michael@0 | 175 | } else { |
michael@0 | 176 | PR_AtomicIncrement(&workerThreads); |
michael@0 | 177 | if (debug_mode) DPRINTF("\tServer creates worker (%d)\n", workerThreads); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | PR_Unlock(workerThreadsLock); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | bytesToRead -= bytesRead; |
michael@0 | 184 | while (bytesToRead) { |
michael@0 | 185 | bytesRead = PR_Recv(newSock, |
michael@0 | 186 | dataBuf, |
michael@0 | 187 | bytesToRead, |
michael@0 | 188 | 0, |
michael@0 | 189 | PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 190 | if (bytesRead < 0) { |
michael@0 | 191 | if (debug_mode) printf("\tServer error receiving data (%d)\n", bytesRead); |
michael@0 | 192 | continue; |
michael@0 | 193 | } |
michael@0 | 194 | if (debug_mode) DPRINTF("\tServer received %d bytes\n", bytesRead); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | bytesWritten = PR_Send(newSock, |
michael@0 | 198 | sendBuf, |
michael@0 | 199 | bytesToWrite, |
michael@0 | 200 | 0, |
michael@0 | 201 | PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 202 | if (bytesWritten != _server_data) { |
michael@0 | 203 | if (debug_mode) printf("\tError sending data to client (%d, %d)\n", |
michael@0 | 204 | bytesWritten, PR_GetOSError()); |
michael@0 | 205 | } else { |
michael@0 | 206 | if (debug_mode) DPRINTF("\tServer sent %d bytes\n", bytesWritten); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | PR_Close(newSock); |
michael@0 | 210 | PR_AtomicDecrement(&workerThreadsBusy); |
michael@0 | 211 | } |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | PRFileDesc * |
michael@0 | 215 | ServerSetup(void) |
michael@0 | 216 | { |
michael@0 | 217 | PRFileDesc *listenSocket; |
michael@0 | 218 | PRSocketOptionData sockOpt; |
michael@0 | 219 | PRNetAddr serverAddr; |
michael@0 | 220 | PRThread *WorkerThread; |
michael@0 | 221 | |
michael@0 | 222 | if ( (listenSocket = PR_NewTCPSocket()) == NULL) { |
michael@0 | 223 | if (debug_mode) printf("\tServer error creating listen socket\n"); |
michael@0 | 224 | else |
michael@0 | 225 | return NULL; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | sockOpt.option = PR_SockOpt_Reuseaddr; |
michael@0 | 229 | sockOpt.value.reuse_addr = PR_TRUE; |
michael@0 | 230 | if ( PR_SetSocketOption(listenSocket, &sockOpt) == PR_FAILURE) { |
michael@0 | 231 | if (debug_mode) printf("\tServer error setting socket option: OS error %d\n", |
michael@0 | 232 | PR_GetOSError()); |
michael@0 | 233 | else failed_already=1; |
michael@0 | 234 | PR_Close(listenSocket); |
michael@0 | 235 | return NULL; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | memset(&serverAddr, 0, sizeof(PRNetAddr)); |
michael@0 | 239 | serverAddr.inet.family = PR_AF_INET; |
michael@0 | 240 | serverAddr.inet.port = PR_htons(PORT); |
michael@0 | 241 | serverAddr.inet.ip = PR_htonl(PR_INADDR_ANY); |
michael@0 | 242 | |
michael@0 | 243 | if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { |
michael@0 | 244 | if (debug_mode) printf("\tServer error binding to server address: OS error %d\n", |
michael@0 | 245 | PR_GetOSError()); |
michael@0 | 246 | else failed_already=1; |
michael@0 | 247 | PR_Close(listenSocket); |
michael@0 | 248 | return NULL; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { |
michael@0 | 252 | if (debug_mode) printf("\tServer error listening to server socket\n"); |
michael@0 | 253 | else failed_already=1; |
michael@0 | 254 | PR_Close(listenSocket); |
michael@0 | 255 | |
michael@0 | 256 | return NULL; |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | /* Create Clients */ |
michael@0 | 260 | workerThreads = 0; |
michael@0 | 261 | workerThreadsBusy = 0; |
michael@0 | 262 | |
michael@0 | 263 | workerThreadsLock = PR_NewLock(); |
michael@0 | 264 | |
michael@0 | 265 | WorkerThread = PR_CreateThread( |
michael@0 | 266 | PR_SYSTEM_THREAD, |
michael@0 | 267 | WorkerThreadFunc, |
michael@0 | 268 | listenSocket, |
michael@0 | 269 | PR_PRIORITY_NORMAL, |
michael@0 | 270 | ServerScope, |
michael@0 | 271 | PR_UNJOINABLE_THREAD, |
michael@0 | 272 | THREAD_STACKSIZE); |
michael@0 | 273 | |
michael@0 | 274 | if (!WorkerThread) { |
michael@0 | 275 | if (debug_mode) printf("error creating working thread\n"); |
michael@0 | 276 | PR_Close(listenSocket); |
michael@0 | 277 | return NULL; |
michael@0 | 278 | } |
michael@0 | 279 | PR_AtomicIncrement(&workerThreads); |
michael@0 | 280 | if (debug_mode) DPRINTF("\tServer created primordial worker thread\n"); |
michael@0 | 281 | |
michael@0 | 282 | return listenSocket; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | /* The main server loop */ |
michael@0 | 286 | void |
michael@0 | 287 | ServerThreadFunc(void *unused) |
michael@0 | 288 | { |
michael@0 | 289 | PRFileDesc *listenSocket; |
michael@0 | 290 | |
michael@0 | 291 | /* Do setup */ |
michael@0 | 292 | listenSocket = ServerSetup(); |
michael@0 | 293 | |
michael@0 | 294 | if (!listenSocket) { |
michael@0 | 295 | SetServerState(SERVER, SERVER_STATE_DEAD); |
michael@0 | 296 | } else { |
michael@0 | 297 | |
michael@0 | 298 | if (debug_mode) DPRINTF("\tServer up\n"); |
michael@0 | 299 | |
michael@0 | 300 | /* Tell clients they can start now. */ |
michael@0 | 301 | SetServerState(SERVER, SERVER_STATE_READY); |
michael@0 | 302 | |
michael@0 | 303 | /* Now wait for server death signal */ |
michael@0 | 304 | WaitServerState(SERVER, SERVER_STATE_DYING); |
michael@0 | 305 | |
michael@0 | 306 | /* Cleanup */ |
michael@0 | 307 | SetServerState(SERVER, SERVER_STATE_DEAD); |
michael@0 | 308 | } |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | /* --- Client Functions ------------------------------------------- */ |
michael@0 | 312 | |
michael@0 | 313 | PRInt32 numRequests; |
michael@0 | 314 | PRInt32 numClients; |
michael@0 | 315 | PRMonitor *clientMonitor; |
michael@0 | 316 | |
michael@0 | 317 | void |
michael@0 | 318 | ClientThreadFunc(void *unused) |
michael@0 | 319 | { |
michael@0 | 320 | PRNetAddr serverAddr; |
michael@0 | 321 | PRFileDesc *clientSocket; |
michael@0 | 322 | char *sendBuf; |
michael@0 | 323 | char *recvBuf; |
michael@0 | 324 | PRInt32 rv; |
michael@0 | 325 | PRInt32 bytesNeeded; |
michael@0 | 326 | |
michael@0 | 327 | sendBuf = (char *)PR_MALLOC(_client_data * sizeof(char)); |
michael@0 | 328 | if (!sendBuf) |
michael@0 | 329 | if (debug_mode) printf("\tClient could not malloc space!?\n"); |
michael@0 | 330 | recvBuf = (char *)PR_MALLOC(_server_data * sizeof(char)); |
michael@0 | 331 | if (!recvBuf) |
michael@0 | 332 | if (debug_mode) printf("\tClient could not malloc space!?\n"); |
michael@0 | 333 | |
michael@0 | 334 | memset(&serverAddr, 0, sizeof(PRNetAddr)); |
michael@0 | 335 | serverAddr.inet.family = PR_AF_INET; |
michael@0 | 336 | serverAddr.inet.port = PR_htons(PORT); |
michael@0 | 337 | serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); |
michael@0 | 338 | |
michael@0 | 339 | while(numRequests > 0) { |
michael@0 | 340 | |
michael@0 | 341 | if ( (numRequests % 10) == 0 ) |
michael@0 | 342 | if (debug_mode) printf("."); |
michael@0 | 343 | if (debug_mode) DPRINTF("\tClient starting request %d\n", numRequests); |
michael@0 | 344 | |
michael@0 | 345 | clientSocket = PR_NewTCPSocket(); |
michael@0 | 346 | if (!clientSocket) { |
michael@0 | 347 | if (debug_mode) printf("Client error creating socket: OS error %d\n", |
michael@0 | 348 | PR_GetOSError()); |
michael@0 | 349 | continue; |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | if (debug_mode) DPRINTF("\tClient connecting\n"); |
michael@0 | 353 | |
michael@0 | 354 | rv = PR_Connect(clientSocket, |
michael@0 | 355 | &serverAddr, |
michael@0 | 356 | PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 357 | if (!clientSocket) { |
michael@0 | 358 | if (debug_mode) printf("\tClient error connecting\n"); |
michael@0 | 359 | continue; |
michael@0 | 360 | } |
michael@0 | 361 | |
michael@0 | 362 | if (debug_mode) DPRINTF("\tClient connected\n"); |
michael@0 | 363 | |
michael@0 | 364 | rv = PR_Send(clientSocket, |
michael@0 | 365 | sendBuf, |
michael@0 | 366 | _client_data, |
michael@0 | 367 | 0, |
michael@0 | 368 | PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 369 | if (rv != _client_data) { |
michael@0 | 370 | if (debug_mode) printf("Client error sending data (%d)\n", rv); |
michael@0 | 371 | PR_Close(clientSocket); |
michael@0 | 372 | continue; |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | if (debug_mode) DPRINTF("\tClient sent %d bytes\n", rv); |
michael@0 | 376 | |
michael@0 | 377 | bytesNeeded = _server_data; |
michael@0 | 378 | while(bytesNeeded) { |
michael@0 | 379 | rv = PR_Recv(clientSocket, |
michael@0 | 380 | recvBuf, |
michael@0 | 381 | bytesNeeded, |
michael@0 | 382 | 0, |
michael@0 | 383 | PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 384 | if (rv <= 0) { |
michael@0 | 385 | if (debug_mode) printf("Client error receiving data (%d) (%d/%d)\n", |
michael@0 | 386 | rv, (_server_data - bytesNeeded), _server_data); |
michael@0 | 387 | break; |
michael@0 | 388 | } |
michael@0 | 389 | if (debug_mode) DPRINTF("\tClient received %d bytes; need %d more\n", rv, bytesNeeded - rv); |
michael@0 | 390 | bytesNeeded -= rv; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | PR_Close(clientSocket); |
michael@0 | 394 | |
michael@0 | 395 | PR_AtomicDecrement(&numRequests); |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | PR_EnterMonitor(clientMonitor); |
michael@0 | 399 | --numClients; |
michael@0 | 400 | PR_Notify(clientMonitor); |
michael@0 | 401 | PR_ExitMonitor(clientMonitor); |
michael@0 | 402 | |
michael@0 | 403 | PR_DELETE(sendBuf); |
michael@0 | 404 | PR_DELETE(recvBuf); |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | void |
michael@0 | 408 | RunClients(void) |
michael@0 | 409 | { |
michael@0 | 410 | PRInt32 index; |
michael@0 | 411 | |
michael@0 | 412 | numRequests = _iterations; |
michael@0 | 413 | numClients = _clients; |
michael@0 | 414 | clientMonitor = PR_NewMonitor(); |
michael@0 | 415 | |
michael@0 | 416 | for (index=0; index<_clients; index++) { |
michael@0 | 417 | PRThread *clientThread; |
michael@0 | 418 | |
michael@0 | 419 | |
michael@0 | 420 | clientThread = PR_CreateThread( |
michael@0 | 421 | PR_USER_THREAD, |
michael@0 | 422 | ClientThreadFunc, |
michael@0 | 423 | NULL, |
michael@0 | 424 | PR_PRIORITY_NORMAL, |
michael@0 | 425 | ClientScope, |
michael@0 | 426 | PR_UNJOINABLE_THREAD, |
michael@0 | 427 | THREAD_STACKSIZE); |
michael@0 | 428 | |
michael@0 | 429 | if (!clientThread) { |
michael@0 | 430 | if (debug_mode) printf("\terror creating client thread %d\n", index); |
michael@0 | 431 | } else |
michael@0 | 432 | if (debug_mode) DPRINTF("\tMain created client %d/%d\n", index+1, _clients); |
michael@0 | 433 | |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | PR_EnterMonitor(clientMonitor); |
michael@0 | 437 | while(numClients) |
michael@0 | 438 | PR_Wait(clientMonitor, PR_INTERVAL_NO_TIMEOUT); |
michael@0 | 439 | PR_ExitMonitor(clientMonitor); |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | /* --- Main Function ---------------------------------------------- */ |
michael@0 | 443 | |
michael@0 | 444 | static |
michael@0 | 445 | void do_work() |
michael@0 | 446 | { |
michael@0 | 447 | PRThread *ServerThread; |
michael@0 | 448 | PRInt32 state; |
michael@0 | 449 | |
michael@0 | 450 | SetServerState(MAIN, SERVER_STATE_STARTUP); |
michael@0 | 451 | ServerThread = PR_CreateThread( |
michael@0 | 452 | PR_USER_THREAD, |
michael@0 | 453 | ServerThreadFunc, |
michael@0 | 454 | NULL, |
michael@0 | 455 | PR_PRIORITY_NORMAL, |
michael@0 | 456 | ServerScope, |
michael@0 | 457 | PR_JOINABLE_THREAD, |
michael@0 | 458 | THREAD_STACKSIZE); |
michael@0 | 459 | if (!ServerThread) { |
michael@0 | 460 | if (debug_mode) printf("error creating main server thread\n"); |
michael@0 | 461 | return; |
michael@0 | 462 | } |
michael@0 | 463 | |
michael@0 | 464 | /* Wait for server to be ready */ |
michael@0 | 465 | state = WaitServerState(MAIN, SERVER_STATE_READY|SERVER_STATE_DEAD); |
michael@0 | 466 | |
michael@0 | 467 | if (!(state & SERVER_STATE_DEAD)) { |
michael@0 | 468 | /* Run Test Clients */ |
michael@0 | 469 | RunClients(); |
michael@0 | 470 | |
michael@0 | 471 | /* Send death signal to server */ |
michael@0 | 472 | SetServerState(MAIN, SERVER_STATE_DYING); |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | PR_JoinThread(ServerThread); |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | |
michael@0 | 479 | static void do_workUK(void) |
michael@0 | 480 | { |
michael@0 | 481 | ServerScope = PR_LOCAL_THREAD; |
michael@0 | 482 | ClientScope = PR_GLOBAL_THREAD; |
michael@0 | 483 | do_work(); |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | |
michael@0 | 487 | |
michael@0 | 488 | static void Measure(void (*func)(void), const char *msg) |
michael@0 | 489 | { |
michael@0 | 490 | PRIntervalTime start, stop; |
michael@0 | 491 | double d; |
michael@0 | 492 | |
michael@0 | 493 | start = PR_IntervalNow(); |
michael@0 | 494 | (*func)(); |
michael@0 | 495 | stop = PR_IntervalNow(); |
michael@0 | 496 | |
michael@0 | 497 | d = (double)PR_IntervalToMicroseconds(stop - start); |
michael@0 | 498 | |
michael@0 | 499 | if (debug_mode) printf("\n%40s: %6.2f usec\n", msg, d / _iterations); |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | |
michael@0 | 503 | int main(int argc, char **argv) |
michael@0 | 504 | { |
michael@0 | 505 | /* The command line argument: -d is used to determine if the test is being run |
michael@0 | 506 | in debug mode. The regress tool requires only one line output:PASS or FAIL. |
michael@0 | 507 | All of the printfs associated with this test has been handled with a if (debug_mode) |
michael@0 | 508 | test. |
michael@0 | 509 | Usage: test_name -d |
michael@0 | 510 | */ |
michael@0 | 511 | PLOptStatus os; |
michael@0 | 512 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); |
michael@0 | 513 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
michael@0 | 514 | { |
michael@0 | 515 | if (PL_OPT_BAD == os) continue; |
michael@0 | 516 | switch (opt->option) |
michael@0 | 517 | { |
michael@0 | 518 | case 'd': /* debug mode */ |
michael@0 | 519 | debug_mode = 1; |
michael@0 | 520 | break; |
michael@0 | 521 | default: |
michael@0 | 522 | break; |
michael@0 | 523 | } |
michael@0 | 524 | } |
michael@0 | 525 | PL_DestroyOptState(opt); |
michael@0 | 526 | |
michael@0 | 527 | /* main test */ |
michael@0 | 528 | #ifndef SYMBIAN |
michael@0 | 529 | if (debug_mode) { |
michael@0 | 530 | printf("Enter number of iterations: \n"); |
michael@0 | 531 | scanf("%d", &_iterations); |
michael@0 | 532 | printf("Enter number of clients : \n"); |
michael@0 | 533 | scanf("%d", &_clients); |
michael@0 | 534 | printf("Enter size of client data : \n"); |
michael@0 | 535 | scanf("%d", &_client_data); |
michael@0 | 536 | printf("Enter size of server data : \n"); |
michael@0 | 537 | scanf("%d", &_server_data); |
michael@0 | 538 | } |
michael@0 | 539 | else |
michael@0 | 540 | #endif |
michael@0 | 541 | { |
michael@0 | 542 | _iterations = 7; |
michael@0 | 543 | _clients = 7; |
michael@0 | 544 | _client_data = 100; |
michael@0 | 545 | _server_data = 100; |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | if (debug_mode) { |
michael@0 | 549 | printf("\n\n%d iterations with %d client threads.\n", |
michael@0 | 550 | _iterations, _clients); |
michael@0 | 551 | printf("Sending %d bytes of client data and %d bytes of server data\n", |
michael@0 | 552 | _client_data, _server_data); |
michael@0 | 553 | } |
michael@0 | 554 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
michael@0 | 555 | PR_STDIO_INIT(); |
michael@0 | 556 | |
michael@0 | 557 | PR_SetThreadRecycleMode(64); |
michael@0 | 558 | |
michael@0 | 559 | ServerStateCVLock = PR_NewLock(); |
michael@0 | 560 | ServerStateCV = PR_NewCondVar(ServerStateCVLock); |
michael@0 | 561 | |
michael@0 | 562 | Measure(do_workUK, "server loop user/kernel"); |
michael@0 | 563 | |
michael@0 | 564 | PR_Cleanup(); |
michael@0 | 565 | |
michael@0 | 566 | if(failed_already) |
michael@0 | 567 | return 1; |
michael@0 | 568 | else |
michael@0 | 569 | return 0; |
michael@0 | 570 | } |