The purpose of the AIO layer is to provide a unified approach to asynchronous I/O. The layer hides details of native asynchronous I/O interfaces of an operating system. Studying the patterns of I/O accesses of external memory algorithms and data structures, we have identified the following functionality that should be provided by the AIO layer:
The AIO layer exposes two user objects: file and request_ptr. Together with I/O waiting functions wait_all, wait_any, and poll_any they provide the functionality mentioned above. Using a file object, the user can submit asynchronous read and asynchronous write requests (methods file::aread and file::awrite). These methods return a request_ptr object which is used to track the status of an issued request. The AIO layer functions wait_all, wait_any, and poll_any facilitate tracking a set of request_ptrs. The last parameter of the methods file::aread and file::awrite is a reference to a callback function object (callback functor). The functor's operator()(request_ptr ) method is called when the I/O request is completed.
As a part of the AIO layer STXXL library provides various I/O performance counters (stats class). The class counts the number and the duration of performed I/O operations as well as the transferred volume. The counting of read and write operations is done separately. STXXL also measures the time spent by the processing thread(s) waiting for the completions of I/Os. This metric helps to evaluate the degree and the impact of overlapping between I/O and computation in an application.
Listing 1 shows a simple example how to use
AIO objects to program asynchronous I/O.
All STXXL library objects are defined in the namespace
stxxl. For convenience, in
Line 1
we bring all names from the STXXL namespace to the local scope.
In the Line 8
a file object myfile is constructed. syscall_file is an implementation
of STXXL file interface which uses UNIX/POSIX read and
write system calls to perform I/O. The file named "storage" in the current directory
is opened in read-only mode.
In Line 9 an asynchronous read of the 1 MB region of the file
starting at position is issued. The data will be read into the array
mybuffer. When the read operation
completes, my_handler::operator()
will be called with a pointer to the completed request.
The execution stops at Line 11 waiting for the completion
of the issued read operation. Note that the work done in the function do_something1() is overlapped with reading.
When the I/O is finished, one can process the read buffer
(Line 12) and free
it (Line 13).