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