File: | pr/Linux4.19_x86_64_gcc_glibc_PTH_64_DBG.OBJ/pr/tests/../../../pr/tests/select2.c |
Warning: | line 79, 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 | ** Name: select2.c |
9 | ** |
10 | ** Description: Measure PR_Select and Empty_Select performance. |
11 | ** |
12 | ** Modification History: |
13 | ** 20-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. |
14 | ** The debug mode will print all of the printfs associated with this test. |
15 | ** The regress mode will be the default mode. Since the regress tool limits |
16 | ** the output to a one line status:PASS or FAIL,all of the printf statements |
17 | ** have been handled with an if (debug_mode) statement. |
18 | ***********************************************************************/ |
19 | |
20 | /*********************************************************************** |
21 | ** Includes |
22 | ***********************************************************************/ |
23 | /* Used to get the command line option */ |
24 | #include "plgetopt.h" |
25 | #include "prttools.h" |
26 | #include "primpl.h" |
27 | |
28 | #include <stdio.h> |
29 | #include <stdlib.h> |
30 | #include <string.h> |
31 | #if defined(OS2) |
32 | #include <sys/time.h> |
33 | #endif |
34 | |
35 | #define PORT8000 8000 |
36 | #define DEFAULT_COUNT10 10 |
37 | PRInt32 count; |
38 | |
39 | |
40 | /*********************************************************************** |
41 | ** PRIVATE FUNCTION: Test_Result |
42 | ** DESCRIPTION: Used in conjunction with the regress tool, prints out the |
43 | ** status of the test case. |
44 | ** INPUTS: PASS/FAIL |
45 | ** OUTPUTS: None |
46 | ** RETURN: None |
47 | ** SIDE EFFECTS: |
48 | ** |
49 | ** RESTRICTIONS: |
50 | ** None |
51 | ** MEMORY: NA |
52 | ** ALGORITHM: Determine what the status is and print accordingly. |
53 | ** |
54 | ***********************************************************************/ |
55 | |
56 | |
57 | static void Test_Result (int result) |
58 | { |
59 | switch (result) |
60 | { |
61 | case PASS1: |
62 | printf ("PASS\n"); |
63 | break; |
64 | case FAIL0: |
65 | printf ("FAIL\n"); |
66 | break; |
67 | default: |
68 | printf ("NOSTATUS\n"); |
69 | break; |
70 | } |
71 | } |
72 | |
73 | static void EmptyPRSelect(void) |
74 | { |
75 | PRInt32 index = count; |
76 | PRInt32 rv; |
77 | |
78 | for (; index--;) { |
79 | rv = PR_Select(0, NULL((void*)0), NULL((void*)0), NULL((void*)0), PR_INTERVAL_NO_WAIT0UL); |
Value stored to 'rv' is never read | |
80 | } |
81 | } |
82 | |
83 | static void EmptyNativeSelect(void) |
84 | { |
85 | PRInt32 rv; |
86 | PRInt32 index = count; |
87 | struct timeval timeout; |
88 | |
89 | timeout.tv_sec = timeout.tv_usec = 0; |
90 | for (; index--;) { |
91 | rv = select(0, NULL((void*)0), NULL((void*)0), NULL((void*)0), &timeout); |
92 | } |
93 | } |
94 | |
95 | static void PRSelectTest(void) |
96 | { |
97 | PRFileDesc *listenSocket; |
98 | PRNetAddr serverAddr; |
99 | |
100 | if ( (listenSocket = PR_NewTCPSocket()) == NULL((void*)0)) { |
101 | if (debug_mode) { |
102 | printf("\tServer error creating listen socket\n"); |
103 | } |
104 | return; |
105 | } |
106 | |
107 | memset(&serverAddr, 0, sizeof(PRNetAddr)); |
108 | serverAddr.inet.family = AF_INET2; |
109 | serverAddr.inet.port = PR_htons(PORT8000); |
110 | serverAddr.inet.ip = PR_htonl(INADDR_ANY((in_addr_t) 0x00000000)); |
111 | |
112 | if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { |
113 | if (debug_mode) { |
114 | printf("\tServer error binding to server address\n"); |
115 | } |
116 | PR_Close(listenSocket); |
117 | return; |
118 | } |
119 | |
120 | if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { |
121 | if (debug_mode) { |
122 | printf("\tServer error listening to server socket\n"); |
123 | } |
124 | PR_Close(listenSocket); |
125 | return; |
126 | } |
127 | if (debug_mode) { |
128 | printf("Listening on port %d\n", PORT8000); |
129 | } |
130 | |
131 | { |
132 | PRFileDesc *newSock; |
133 | PRNetAddr rAddr; |
134 | PRInt32 loops = 0; |
135 | PR_fd_set rdset; |
136 | PRInt32 rv; |
137 | PRInt32 bytesRead; |
138 | char buf[11]; |
139 | |
140 | loops++; |
141 | |
142 | if (debug_mode) { |
143 | printf("Going into accept\n"); |
144 | } |
145 | |
146 | newSock = PR_Accept(listenSocket, |
147 | &rAddr, |
148 | PR_INTERVAL_NO_TIMEOUT0xffffffffUL); |
149 | |
150 | if (newSock) { |
151 | if (debug_mode) { |
152 | printf("Got connection!\n"); |
153 | } |
154 | } else { |
155 | if (debug_mode) { |
156 | printf("PR_Accept failed: error code %d\n", PR_GetError()); |
157 | } |
158 | else { |
159 | Test_Result (FAIL0); |
160 | } |
161 | } |
162 | |
163 | PR_FD_ZERO(&rdset); |
164 | PR_FD_SET(newSock, &rdset); |
165 | |
166 | if (debug_mode) { |
167 | printf("Going into select \n"); |
168 | } |
169 | |
170 | rv = PR_Select(0, &rdset, 0, 0, PR_INTERVAL_NO_TIMEOUT0xffffffffUL); |
171 | |
172 | if (debug_mode) { |
173 | printf("return from select is %d\n", rv); |
174 | } |
175 | |
176 | if (PR_FD_ISSET(newSock, &rdset)) { |
177 | if (debug_mode) { |
178 | printf("I can't believe it- the socket is ready okay!\n"); |
179 | } |
180 | } else { |
181 | if (debug_mode) { |
182 | printf("Damn; the select test failed...\n"); |
183 | } |
184 | else { |
185 | Test_Result (FAIL0); |
186 | } |
187 | } |
188 | |
189 | strcpy(buf, "XXXXXXXXXX"); |
190 | bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT0xffffffffUL); |
191 | buf[10] = '\0'; |
192 | |
193 | if (debug_mode) { |
194 | printf("Recv completed with %d bytes, %s\n", bytesRead, buf); |
195 | } |
196 | |
197 | PR_Close(newSock); |
198 | } |
199 | |
200 | } |
201 | |
202 | #if defined(XP_UNIX1) |
203 | |
204 | static void NativeSelectTest(void) |
205 | { |
206 | PRFileDesc *listenSocket; |
207 | PRNetAddr serverAddr; |
208 | |
209 | if ( (listenSocket = PR_NewTCPSocket()) == NULL((void*)0)) { |
210 | if (debug_mode) { |
211 | printf("\tServer error creating listen socket\n"); |
212 | } |
213 | return; |
214 | } |
215 | |
216 | memset(&serverAddr, 0, sizeof(PRNetAddr)); |
217 | serverAddr.inet.family = AF_INET2; |
218 | serverAddr.inet.port = PR_htons(PORT8000); |
219 | serverAddr.inet.ip = PR_htonl(INADDR_ANY((in_addr_t) 0x00000000)); |
220 | |
221 | if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) { |
222 | if (debug_mode) { |
223 | printf("\tServer error binding to server address\n"); |
224 | } |
225 | PR_Close(listenSocket); |
226 | return; |
227 | } |
228 | |
229 | if ( PR_Listen(listenSocket, 128) == PR_FAILURE) { |
230 | if (debug_mode) { |
231 | printf("\tServer error listening to server socket\n"); |
232 | } |
233 | PR_Close(listenSocket); |
234 | return; |
235 | } |
236 | if (debug_mode) { |
237 | printf("Listening on port %d\n", PORT8000); |
238 | } |
239 | |
240 | { |
241 | PRIntn osfd; |
242 | char buf[11]; |
243 | fd_set rdset; |
244 | PRNetAddr rAddr; |
245 | PRFileDesc *newSock; |
246 | struct timeval timeout; |
247 | PRInt32 bytesRead, rv, loops = 0; |
248 | |
249 | loops++; |
250 | |
251 | if (debug_mode) { |
252 | printf("Going into accept\n"); |
253 | } |
254 | |
255 | newSock = PR_Accept(listenSocket, &rAddr, PR_INTERVAL_NO_TIMEOUT0xffffffffUL); |
256 | |
257 | if (newSock) { |
258 | if (debug_mode) { |
259 | printf("Got connection!\n"); |
260 | } |
261 | } else { |
262 | if (debug_mode) { |
263 | printf("PR_Accept failed: error code %d\n", PR_GetError()); |
264 | } |
265 | else { |
266 | Test_Result (FAIL0); |
267 | } |
268 | } |
269 | |
270 | osfd = PR_FileDesc2NativeHandle(newSock); |
271 | FD_ZERO(&rdset)do { unsigned int __i; fd_set *__arr = (&rdset); for (__i = 0; __i < sizeof (fd_set) / sizeof (__fd_mask); ++__i) ( (__arr)->fds_bits)[__i] = 0; } while (0); |
272 | FD_SET(osfd, &rdset)((void) (((&rdset)->fds_bits)[((osfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((osfd) % (8 * ( int) sizeof (__fd_mask))))))); |
273 | |
274 | if (debug_mode) { |
275 | printf("Going into select \n"); |
276 | } |
277 | |
278 | |
279 | timeout.tv_sec = 2; timeout.tv_usec = 0; |
280 | rv = select(osfd + 1, &rdset, NULL((void*)0), NULL((void*)0), &timeout); |
281 | |
282 | if (debug_mode) { |
283 | printf("return from select is %d\n", rv); |
284 | } |
285 | |
286 | if (FD_ISSET(osfd, &rdset)((((&rdset)->fds_bits)[((osfd) / (8 * (int) sizeof (__fd_mask )))] & ((__fd_mask) (1UL << ((osfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) { |
287 | if (debug_mode) { |
288 | printf("I can't believe it- the socket is ready okay!\n"); |
289 | } |
290 | } else { |
291 | if (debug_mode) { |
292 | printf("Damn; the select test failed...\n"); |
293 | } |
294 | else { |
295 | Test_Result (FAIL0); |
296 | } |
297 | } |
298 | |
299 | strcpy(buf, "XXXXXXXXXX"); |
300 | bytesRead = PR_Recv(newSock, buf, 10, 0, PR_INTERVAL_NO_TIMEOUT0xffffffffUL); |
301 | buf[10] = '\0'; |
302 | |
303 | if (debug_mode) { |
304 | printf("Recv completed with %d bytes, %s\n", bytesRead, buf); |
305 | } |
306 | |
307 | PR_Close(newSock); |
308 | } |
309 | |
310 | } /* NativeSelectTest */ |
311 | |
312 | #endif /* defined(XP_UNIX) */ |
313 | |
314 | /************************************************************************/ |
315 | |
316 | static void Measure(void (*func)(void), const char *msg) |
317 | { |
318 | PRIntervalTime start, stop; |
319 | double d; |
320 | PRInt32 tot; |
321 | |
322 | start = PR_IntervalNow(); |
323 | (*func)(); |
324 | stop = PR_IntervalNow(); |
325 | |
326 | d = (double)PR_IntervalToMicroseconds(stop - start); |
327 | tot = PR_IntervalToMilliseconds(stop-start); |
328 | |
329 | if (debug_mode) { |
330 | printf("%40s: %6.2f usec avg, %d msec total\n", msg, d / count, tot); |
331 | } |
332 | } |
333 | |
334 | int main(int argc, char **argv) |
335 | { |
336 | |
337 | /* The command line argument: -d is used to determine if the test is being run |
338 | in debug mode. The regress tool requires only one line output:PASS or FAIL. |
339 | All of the printfs associated with this test has been handled with a if (debug_mode) |
340 | test. |
341 | Usage: test_name -d |
342 | */ |
343 | PLOptStatus os; |
344 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); |
345 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
346 | { |
347 | if (PL_OPT_BAD == os) { |
348 | continue; |
349 | } |
350 | switch (opt->option) |
351 | { |
352 | case 'd': /* debug mode */ |
353 | debug_mode = 1; |
354 | break; |
355 | default: |
356 | break; |
357 | } |
358 | } |
359 | PL_DestroyOptState(opt); |
360 | |
361 | /* main test */ |
362 | |
363 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
364 | PR_STDIO_INIT(); |
365 | |
366 | if (argc > 2) { |
367 | count = atoi(argv[2]); |
368 | } else { |
369 | count = DEFAULT_COUNT10; |
370 | } |
371 | |
372 | #if defined(XP_UNIX1) |
373 | Measure(NativeSelectTest, "time to call 1 element select()"); |
374 | #endif |
375 | Measure(EmptyPRSelect, "time to call Empty PR_select()"); |
376 | Measure(EmptyNativeSelect, "time to call Empty select()"); |
377 | Measure(PRSelectTest, "time to call 1 element PR_select()"); |
378 | |
379 | if (!debug_mode) { |
380 | Test_Result (NOSTATUS2); |
381 | } |
382 | PR_Cleanup(); |
383 | |
384 | |
385 | } |