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