1 /* Test for nonblocking read and write.
3 Copyright (C) 2011-2020 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Whether to print debugging messages. */
19 #define ENABLE_DEBUGGING 0
21 /* Delay (in microseconds) to sleep when write() or read() returned -1 with
23 #define SMALL_DELAY 10000
25 /* Return a memory area, filled with the data to be transferred. */
26 static unsigned char *
27 init_data (size_t data_block_size
)
32 data
= (unsigned char *) malloc (2 * data_block_size
);
33 ASSERT (data
!= NULL
);
35 for (i
= 0; i
< 2 * data_block_size
; i
++)
36 data
[i
] = (unsigned char) (i
* i
+ (7 * i
) % 61 + 4);
43 static int dbgfprintf (FILE *fp
, const char *format
, ...)
44 _GL_ATTRIBUTE_FORMAT_PRINTF (2, 3);
46 dbgfprintf (FILE *fp
, const char *format
, ...)
48 /* Accumulate the entire line in a buffer, so that the output on fp
49 is done atomically. */
52 struct timeval current_time
;
57 gettimeofday (¤t_time
, NULL
);
58 ret
= snprintf (line
, sizeof (line
), "%.6f ",
59 current_time
.tv_sec
+ (double) current_time
.tv_usec
* 1e-6);
62 line_len
= strlen (line
);
64 va_start (args
, format
);
65 ret
= vsnprintf (line
+ line_len
, sizeof (line
) - line_len
, format
, args
);
69 line_len
+= strlen (line
+ line_len
);
71 ret
= fwrite (line
, 1, line_len
, fp
);
73 /* Make sure the debugging information is output, so that the order of the
74 messages reflects the timeline of events, and so that the output is not
75 lost if the program crashes afterwards (relevant on mingw). */
80 # define dbgfprintf if (1) ; else fprintf
83 /* Return a textual description of the error code ERR, if FAILED is true.
84 Return an empty string if FAILED is false. */
86 dbgstrerror (bool failed
, int err
)
91 sprintf (buf
, " %d %s", err
, strerror (err
));
98 #define TIMING_DECLS \
99 struct timeval before_time; \
100 struct timeval after_time; \
102 #define START_TIMING \
103 gettimeofday (&before_time, NULL);
105 gettimeofday (&after_time, NULL); \
107 (after_time.tv_sec - before_time.tv_sec) \
108 + ((double) after_time.tv_usec - (double) before_time.tv_usec) * 1e-6;