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