summaryrefslogtreecommitdiff
path: root/tests/Aio_Platform_Test.cpp
blob: 3dc5b153417974e55916cd87dd4bd1ee047c4ef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// $Id$
// ============================================================================
//
// = FILENAME
//    aio_platform_test.cpp
//
// =  DESCRITPTION
//     Testing the platform for POSIX Asynchronous I/O. If
//     ACE_HAS_AIO_CALLS is defined, we also test the POSIX <aio_>
//     calls with real time signals on. We dont test the things with
//     aio_suspend.  
//
// = AUTHOR
//    Programming for the Real World. Bill O. GallMeister.
//    Modified by Alexander Babu Arulanthu <alex@cs.wustl.edu>
//
// =====================================================================

#include "test_config.h"
#include "ace/OS.h"

ACE_RCSID(tests, Aio_Platform_Test, "$Id$")

#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */

#include "ace/Message_Block.h"

#if defined (ACE_HAS_AIO_CALLS)
static ACE_HANDLE file_handle = ACE_INVALID_HANDLE;
static ACE_Message_Block mb1 (BUFSIZ + 1);
static ACE_Message_Block mb2 (BUFSIZ + 1);
static aiocb aiocb1;
static aiocb aiocb2;
static sigset_t completion_signal;

// Function prototypes.
static int have_asynchio (void);
static int query_aio_completions (void);
static int issue_aio_calls (void);
static int setup_signal_delivery (void);
static int test_aio_calls (void);
#endif /* ACE_HAS_AIO_CALLS */

#if defined (_POSIX_ASYNCHRONOUS_IO)
static int do_sysconf (void)
{
  // Call sysconf to find out runtime values.
  
  errno = 0;
#if defined (_SC_LISTIO_AIO_MAX)
  ACE_DEBUG ((LM_DEBUG,
              "Runtime value of LISTIO_AIO_MAX is %d, errno = %d\n",
              ACE_OS::sysconf (_SC_LISTIO_AIO_MAX),
              errno));
#elif defined (_SC_AIO_LISTIO_MAX)
  ACE_DEBUG ((LM_DEBUG,
              "Runtime value of AIO_LISTIO_MAX is %d, errno = %d\n",
              ACE_OS::sysconf (_SC_AIO_LISTIO_MAX),
              errno));
#else
  ACE_ERROR ((LM_ERROR,
              "_SC_LISTIO_AIO_MAX or _SC_AIO_LISTIO_MAX"
              " do not exist on this platform\n"));
#endif /* _SC_LISTIO_AIO_MAX */

#if defined (_SC_AIO_MAX)
  errno = 0;
  ACE_DEBUG ((LM_DEBUG,
              "Runtime value of AIO_MAX is %d, errno = %d\n",
              ACE_OS::sysconf (_SC_AIO_MAX),
              errno));
#else
  ACE_ERROR ((LM_ERROR,
              "_SC_AIO_MAX does not exist on this platform\n"));
#endif /* _SC_AIO_MAX */

#if defined (_SC_ASYNCHRONOUS_IO)
  errno = 0;
  ACE_DEBUG ((LM_DEBUG,
              "Runtime value of _POSIX_ASYNCHRONOUS_IO is %d, errno = %d\n",
              ACE_OS::sysconf (_SC_ASYNCHRONOUS_IO),
              errno));
#else /* Not _SC_ASYNCHRONOUS_IO */
  ACE_ERROR ((LM_ERROR,
              "_SC_ASYNCHRONOUS_IO does not exist on this platform\n"));
#endif /* _SC_ASYNCHRONOUS_IO */

#if defined (_SC_REALTIME_SIGNALS)
  errno = 0;
  ACE_DEBUG ((LM_DEBUG,
              "Runtime value of _POSIX_REALTIME_SIGNALS is %d, errno = %d\n",
              ACE_OS::sysconf (_SC_REALTIME_SIGNALS),
              errno));
#else /* Not _SC_REALTIME_SIGNALS */
  ACE_ERROR ((LM_ERROR,
              "_SC_REALTIME_SIGNALS does not exist on this platform\n"));
#endif /* _SC_REALTIME_SIGNALS */
  

#if defined (_SC_RTSIG_MAX)
  errno = 0;
  ACE_DEBUG ((LM_DEBUG,
              "Runtime value of RTSIG_MAX %d, Errno = %d\n",
              ACE_OS::sysconf (_SC_RTSIG_MAX),
              errno));
#else /* Not _SC_RTSIG_MAX */
  ACE_ERROR ((LM_ERROR,
              "_SC_RTSIG_MAX does not exist on this platform\n"));
#endif /* _SC_RTSIG_MAX */

#if defined (_SC_SIGQUEUE_MAX)
  errno = 0;
  ACE_DEBUG ((LM_DEBUG,
              "Runtime value of SIGQUEUE_MAX %d, Errno = %d\n",
              ACE_OS::sysconf (_SC_SIGQUEUE_MAX),
              errno));
#else /* Not _SC_SIGQUEUE_MAX */
  ACE_ERROR ((LM_ERROR,
              "_SC_SIGQUEUE_MAX does not exist on this platform\n"));
#endif /*  _SC_SIGQUEUE_MAX */
  return 0;
}
#endif /* _POSIX_ASYNCHRONOUS_IO */

#if defined (ACE_HAS_AIO_CALLS)
static int
test_aio_calls (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "test_aio_calls: Errno : %d\n",
              errno));
  // Set up the input file.
  // Open file (in SEQUENTIAL_SCAN mode)
  file_handle = ACE_OS::open ("Aio_Platform_Test.cpp",
                              O_RDONLY);

  if (file_handle == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "ACE_OS::open"),
                      -1);

  if (setup_signal_delivery () < 0)
    return -1;

  if (issue_aio_calls () < 0)
    return -1;

  if (query_aio_completions () < 0)
    return -1;

  return 0;
}

static int
setup_signal_delivery (void)
{
  // Make the sigset_t consisting of the completion signal.
  if (sigemptyset (&completion_signal) < 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error:%p:Couldnt init the RT completion signal set\n"),
                      -1);

  if (sigaddset (&completion_signal,
                 SIGRTMIN) < 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error:%p:Couldnt init the RT completion signal set\n"),
                      -1);

  // Mask them.
  if (sigprocmask (SIG_BLOCK, &completion_signal, 0) < 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error:%p:Couldnt maks the RT completion signals\n"),
                      -1);

  // Setting up the handler(!) for these signals.
  struct sigaction reaction;
  sigemptyset (&reaction.sa_mask);   // Nothing else to mask.
  reaction.sa_flags = SA_SIGINFO;    // Realtime flag.
#if defined (SA_SIGACTION)
  // Lynx says, it is better to set this bit to be portable.
  reaction.sa_flags &= SA_SIGACTION;
#endif /* SA_SIGACTION */
  reaction.sa_sigaction = 0;         // No handler.
  int sigaction_return = sigaction (SIGRTMIN,
                                    &reaction,
                                    0);
  if (sigaction_return == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error:%p:Proactor couldnt do sigaction for the RT SIGNAL"),
                      -1);
  return 0;
}

static int
issue_aio_calls (void)
{
  // Setup AIOCB.
  aiocb1.aio_fildes = file_handle;
  aiocb1.aio_offset = 0;
  aiocb1.aio_buf = mb1.wr_ptr ();
  aiocb1.aio_nbytes = BUFSIZ;
  aiocb1.aio_reqprio = 0;
  aiocb1.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
  aiocb1.aio_sigevent.sigev_signo = SIGRTMIN;
  aiocb1.aio_sigevent.sigev_value.sival_ptr = (void *) &aiocb1;

  // Fire off the aio write.
  if (aio_read (&aiocb1) == -1)
    // Queueing failed.
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Erro:%p:Asynch_Read_Stream: aio_read queueing failed\n"),
                      -1);

  // Setup AIOCB.
  aiocb2.aio_fildes = file_handle;
  aiocb2.aio_offset = BUFSIZ + 1;
  aiocb2.aio_buf = mb2.wr_ptr ();
  aiocb2.aio_nbytes = BUFSIZ;
  aiocb2.aio_reqprio = 0;
  aiocb2.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
  aiocb2.aio_sigevent.sigev_signo = SIGRTMIN;
  aiocb2.aio_sigevent.sigev_value.sival_ptr = (void *) &aiocb2;

  // Fire off the aio write.
  if (aio_read (&aiocb2) == -1)
    // Queueing failed.
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Erro:%p:Asynch_Read_Stream: aio_read queueing failed\n"),
                      -1);

  return 0;
}

static int
query_aio_completions (void)
{
  for (size_t number_of_compleions = 0;
       number_of_compleions < 2;
       number_of_compleions ++)
    {
      // Wait for <milli_seconds> amount of time.  @@ Assigning
      // <milli_seconds> to tv_sec.
      timespec timeout;
      timeout.tv_sec = ACE_INFINITE;
      timeout.tv_nsec = 0;

      // To get back the signal info.
      siginfo_t sig_info;

      // Await the RT completion signal.
      int sig_return = sigtimedwait (&completion_signal,
                                     &sig_info,
                                     &timeout);

      // Error case.
      // If failure is coz of timeout, then return *0* but set
      // errno appropriately. This is what the WinNT proactor
      // does.
      if (sig_return == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Error:%p:Error waiting for RT completion signals\n"),
                          0);

      // RT completion signals returned.
      if (sig_return != SIGRTMIN)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unexpected signal (%d) has been received while waiting for RT Completion Signals\n",
                           sig_return),
                          -1);

      // @@ Debugging.
      ACE_DEBUG ((LM_DEBUG,
                  "Sig number found in the sig_info block : %d\n",
                  sig_info.si_signo));

      // Is the signo returned consistent?
      if (sig_info.si_signo != sig_return)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Inconsistent signal number (%d) in the signal info block\n",
                           sig_info.si_signo),
                          -1);

      // @@ Debugging.
      ACE_DEBUG ((LM_DEBUG,
                  "Signal code for this signal delivery : %d\n",
                  sig_info.si_code));

      // Is the signal code an aio completion one?
      if ((sig_info.si_code != SI_ASYNCIO) &&
          (sig_info.si_code != SI_QUEUE))
        ACE_ERROR_RETURN ((LM_DEBUG,
                           "Unexpected signal code (%d) returned on completion querying\n",
                           sig_info.si_code),
                          0);

      // Retrive the aiocb.
      aiocb* aiocb_ptr = (aiocb *) sig_info.si_value.sival_ptr;

      // Analyze error and return values. Return values are
      // actually <errno>'s associated with the <aio_> call
      // corresponding to aiocb_ptr.
      int error_code = aio_error (aiocb_ptr);
      if (error_code == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%p:Invalid control block was sent to <aio_error> for compleion querying\n"),
                          -1);

      if (error_code != 0)
        // Error occurred in the <aio_>call. Return the errno
        // corresponding to that <aio_> call.
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%p:An AIO call has failed\n"),
                          error_code);

      // No error occured in the AIO operation.
      int nbytes = aio_return (aiocb_ptr);
      if (nbytes == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%p:Invalid control block was send to <aio_return>\n"),
                          -1);
      if (number_of_compleions == 0)
        // Print the buffer.
        ACE_DEBUG ((LM_DEBUG,
                    "\n Number of bytes transferred : %d\n The buffer : %s \n",
                    nbytes,
                    mb1.rd_ptr ()));
      else
        // Print the buffer.
        ACE_DEBUG ((LM_DEBUG,
                    "\n Number of bytes transferred : %d\n The buffer : %s \n",
                    nbytes,
                    mb2.rd_ptr ()));
    }

  return 0;
}
#endif /* ACE_HAS_AIO_CALLS */

static int
have_asynchio (void)
{
#if defined (_POSIX_ASYNCHRONOUS_IO)
#if defined (_POSIX_ASYNC_IO)
#if _POSIX_ASYNC_IO == -1
  ACE_DEBUG ((LM_DEBUG,
              "_POSIX_ASYNC_IO = -1.. ASYNCH IO NOT supported at all\n"));
  return -1;
#else /* Not _POSIX_ASYNC_IO == -1 */
  ACE_DEBUG ((LM_DEBUG,
              "_POSIX_ASYNC_IO = %d\n ASYNCH IO is supported FULLY\n",
              _POSIX_ASYNC_IO));
#endif /* _POSIX_ASYNC_IO == -1 */

#else  /* Not defined  _POSIX_ASYNC_IO */
  ACE_ERROR ((LM_DEBUG,
              "_POSIX_ASYNC_IO is not defined.\n"));
  ACE_DEBUG ((LM_DEBUG,
              "AIO might *not* be supported on some paths\n"));
#endif /* _POSIX_ASYNC_IO */

  // System defined POSIX Values.
  ACE_DEBUG ((LM_DEBUG,
              "System claims to have  POSIX_ASYNCHRONOUS_IO\n"));
  ACE_DEBUG ((LM_DEBUG,
              "_POSIX_AIO_LISTIO_MAX = %d\n",
              _POSIX_AIO_LISTIO_MAX));
  ACE_DEBUG ((LM_DEBUG,
              "_POSIX_AIO_MAX = %d\n",
              _POSIX_AIO_MAX));

  // @@ Debugging.
  ACE_DEBUG ((LM_DEBUG,
              "Before do_sysconf : Errno %d\n",
              errno));
  
  // Check and print the run time values.
  do_sysconf ();

  // @@ Debugging.
  ACE_DEBUG ((LM_DEBUG,
              "After do_sysconf: Errno : %d\n", errno));

#if defined (ACE_HAS_AIO_CALLS)
  // Test the aio calls. Issue two <aio_read>s. Assing SIGRTMIN as the
  // notification signal. Mask these signal from delivery. Receive
  // this signal by doing <sigtimedwait>.
  test_aio_calls ();
#endif /* ACE_HAS_AIO_CALLS */

  return 0;

#else /* Not _POSIX_ASYNCHRONOUS_IO */
  ACE_DEBUG ((LM_DEBUG,
              "No support._POSIX_ASYNCHRONOUS_IO itself is not defined\n"));
  return -1;
#endif /* _POSIX_ASYNCHRONOUS_IO */
}

int
main (int, char *[])
{
  ACE_START_TEST ("Aio_Platform_Test");

  have_asynchio ();

  ACE_END_TEST;
  return 0;
}