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