non-blocking event loop for network & disk I/O
RFC 1/1 non-blocking event loop for network & disk I/O
In designing a streaming media application, I want to use a single event processing loop for both disk and network I/O. The idea is that the I/O done in this loop should never block. Doing this for network I/O is pretty standard (i.e. set O_NONBLOCK and use a poll variant).
Asynchronous/nonblocking file I/O has its limitations, however. AIO can only be used in conjunction with O_DIRECT, and I'd rather be able to utilize the page cache. Even so, it is difficult to mix io_getevents with poll in a single event loop. You could use mmap/mincore, but there is still a race between the mincore call and the actual read of the data.
So, have I missed any other tools which would help to solve this problem?
It seems that if we introduced a nonblocking flag for file I/O, this problem would be addressed. Call it O_ATOMICREAD or some such thing. It's actually quite easy to do (patch attached).
I even went so far as to modify squid's aufs store type to use this, so there is use outside of just the streaming media example given above (though I haven't had time to benchmark it).
Comments?
Thanks in advance,
Jeff
fs/fcntl.c renamed from fs/fcntl.c.orig +1 -1
@@ -181,7 +181,7 @@ asmlinkage long sys_dup(unsigned int filreturn ret; } -#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME) +#define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | FASYNC | O_DIRECT | O_NOATIME | O_ATOMICREAD)static int setfl (int fd,struct file * filp,unsigned long arg) {
include/asm-mips/fcntl.h renamed from include/asm-mips/fcntl.h.orig +1 -0
@@ -27,6 +27,7 @@#define O_DIRECTORY 0x10000/* must be a directory */ #define O_NOFOLLOW 0x20000/* don't follow links */ #define O_NOATIME 0x40000 +#define O_ATOMICREAD 0x80000/* non-blocking file i/o */ #define O_NDELAY O_NONBLOCK
include/asm-parisc/fcntl.h renamed from include/asm-parisc/fcntl.h.orig +1 -0
@@ -20,6 +20,7 @@#define O_DSYNC 01000000/* HPUX only */ #define O_RSYNC 02000000/* HPUX only */ #define O_NOATIME 04000000 +#define O_ATOMICREAD 10000000/* non-blocking file i/o */ #define FASYNC 00020000/* fcntl, for BSD compatibility */ #define O_DIRECT 00040000/* direct disk access hint - currently ignored */
include/asm-ppc64/fcntl.h renamed from include/asm-ppc64/fcntl.h.orig +1 -0
@@ -28,6 +28,7 @@#define O_LARGEFILE 0200000#define O_DIRECT 0400000/* direct disk access hint */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-x86_64/fcntl.h renamed from include/asm-x86_64/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-arm/fcntl.h renamed from include/asm-arm/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECT 0200000/* direct disk access hint - currently ignored */ #define O_LARGEFILE 0400000#define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-m68k/fcntl.h renamed from include/asm-m68k/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECT 0200000/* direct disk access hint - currently ignored */ #define O_LARGEFILE 0400000#define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-ppc/fcntl.h renamed from include/asm-ppc/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_LARGEFILE 0200000#define O_DIRECT 0400000/* direct disk access hint */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-sparc/fcntl.h renamed from include/asm-sparc/fcntl.h.orig +1 -0
@@ -22,6 +22,7 @@#define O_LARGEFILE 0x40000#define O_DIRECT 0x100000/* direct disk access hint */ #define O_NOATIME 0x200000 +#define O_ATOMICREAD 0x400000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-alpha/fcntl.h renamed from include/asm-alpha/fcntl.h.orig +1 -0
@@ -22,6 +22,7 @@#define O_LARGEFILE 0400000/* will be set by the kernel on every open */ #define O_DIRECT 02000000/* direct disk access - should check with OSF/1 */ #define O_NOATIME 04000000 +#define O_ATOMICREAD 10000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-v850/fcntl.h renamed from include/asm-v850/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECT 0200000/* direct disk access hint - currently ignored */ #define O_LARGEFILE 0400000#define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-i386/fcntl.h renamed from include/asm-i386/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-frv/fcntl.h renamed from include/asm-frv/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-m32r/fcntl.h renamed from include/asm-m32r/fcntl.h.orig +1 -0
@@ -25,6 +25,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-h8300/fcntl.h renamed from include/asm-h8300/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECT 0200000/* direct disk access hint - currently ignored */ #define O_LARGEFILE 0400000#define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-s390/fcntl.h renamed from include/asm-s390/fcntl.h.orig +1 -0
@@ -28,6 +28,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-ia64/fcntl.h renamed from include/asm-ia64/fcntl.h.orig +1 -0
@@ -29,6 +29,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-cris/fcntl.h renamed from include/asm-cris/fcntl.h.orig +1 -0
@@ -23,6 +23,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get f_flags */
include/asm-sparc64/fcntl.h renamed from include/asm-sparc64/fcntl.h.orig +1 -0
@@ -22,6 +22,7 @@#define O_LARGEFILE 0x40000#define O_DIRECT 0x100000/* direct disk access hint */ #define O_NOATIME 0x200000 +#define O_ATOMICREAD 0x400000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */
include/asm-arm26/fcntl.h renamed from include/asm-arm26/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECT 0200000/* direct disk access hint - currently ignored */ #define O_LARGEFILE 0400000#define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
include/asm-sh/fcntl.h renamed from include/asm-sh/fcntl.h.orig +1 -0
@@ -21,6 +21,7 @@#define O_DIRECTORY 0200000/* must be a directory */ #define O_NOFOLLOW 0400000/* don't follow links */ #define O_NOATIME 01000000 +#define O_ATOMICREAD 02000000/* non-blocking file i/o */ #define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */
mm/filemap.c renamed from mm/filemap.c.orig +14 -4
@@ -696,7 +696,7 @@ void do_generic_mapping_read(struct addrunsigned long prev_index;loff_t isize;struct page *cached_page; -int error; +int error, nonblock = filp->f_flags &O_ATOMICREAD ;struct file_ra_state ra = *_ra; cached_page =NULL ;
@@ -728,7 +728,7 @@ void do_generic_mapping_read(struct addr nr = nr - offset;cond_resched (); -if (index == next_index && req_size) { +if (index == next_index && req_size && !nonblock) { ret_size =page_cache_readahead (mapping, &ra, filp, index, req_size); next_index += ret_size;
@@ -738,11 +738,21 @@ void do_generic_mapping_read(struct addr find_page: page =find_get_page (mapping, index);if (unlikely (page ==NULL )) { +if (nonblock) { + desc->error = -EWOULDBLOCK ; +break ; + }handle_ra_miss (mapping, &ra, index); goto no_cached_page; } -if (!PageUptodate (page)) +if (!PageUptodate (page)) { +if (nonblock) { +page_cache_release (page); + desc->error = -EWOULDBLOCK ; +break ; + } goto page_not_up_to_date; + } page_ok:/* If users can be writing to this page using arbitrary
@@ -1000,7 +1010,7 @@ __generic_file_aio_read(struct kiocb *io desc.error =0 ;do_generic_file_read (filp,ppos,&desc,file_read_actor); retval += desc.written; -if (!retval) { +if (!retval || desc.error) { retval = desc.error;break ; }