Research on the Linux Native AIO asynchronous AIO
http://rango.swoole.com/archives/282
First of all, the Epoll+nonblock from a macro point of view can be called full async, but from a microscopic point of view or synchronous IO. Only after the data arrives to get the system notification, then synchronously executes recv retrieves the data, does not have the iowait.
The real asynchronous IO (called AIO below) should be like Windows IOCP, passing in file handles, buffers, dimension parameters and a function pointer, when the operating system actually completes the IO operation, and then executes the corresponding function.
In fact, for the socket, Epoll is already the most efficient model, although more than one recv system call, but overall there is no IO waiting, high efficiency. And Epoll is a natural reactor model, and the program is easier to implement. AIO, such as the IOCP of Windows, is a way of asynchronous callbacks that are very difficult to develop.
Why you still need AIO because the file handle is completely different from the socket and it is always readable. You cannot use Epoll+nonblock to implement Asynchrony. If you are in a Epoll fully asynchronous server, you must use AIO to read and write files. Below is a few of the AIO implementation scenarios.
GCC AIO
GCC implements AIO in compliance with the POSIX standard. header file is <aio.h>, support Freebsd/linux. is achieved by blocking the io+ thread pool. A few of the main functions are aio_read/aio_write/aio_return.
Advantages: The support platform is many, the compatibility is good, does not need to rely on the third party library, the blocking IO can utilize to the operating system Pagecache.
Cons: Some bugs and pitfalls are said to have been unresolved. But this is the article in the online, GCC development for so many years, there is no left bug it. This is still to be tested.
Linux Native Aio
AIO provided by the operating system kernel, <linux/aio_abi.h> header file. Native Aio is a true AIO, completely non-blocking asynchronous, rather than using blocking IO and thread pool impersonation. Several major system calls are io_submit/io_setup/io_getevents.
Advantages: Provided by the operating system, read and write operations can be delivered directly to the hardware, without wasting the CPU.
Disadvantage: Linux only is supported and directio must be used, so the pagecache of the operating system cannot be exploited. For writing files, the role of native Aio, should be written for itself is written to Pagecache, direct return, no IO wait.
Libeio
The AIO implementation developed by the Libev author, similar to GCC AIO, is also implemented using a blocking io+ thread pool. Advantages and disadvantages see above. Unlike GCC AIO, the code is more concise, so fewer bugs are more secure and stable. But this is a third-party library, and your code needs to rely on Libeio.
Summarize
If your program reads and writes files that are large and then strong, so that the Pagecache hit rate is low, you can choose native AIO to reduce CPU usage.
If you read and write a file that is small, and is a fixed number of files, so that the Pagecache hit rate, you can choose GCC aio or Libeio.
Research on the Linux Native AIO asynchronous AIO