Skip to contentSkip to conversation

tapdisk:check O_DIRECT on hole file for performance

PATCH New Wei Kong 1 participant 1 message Aug 18, 2009

lore
Share & tools
lore

Wei Kong

author
#0 lore raw

PATCH 1/1 tapdisk:check O_DIRECT on hole file for performance

Although tapdisk has been instead of tapdisk-ioemu, but it still used on some old xen-3.2.
So fix a performance problem here.
--

Kernel aio will retry when encounter a block doesn't allocated, then do async io,
but O_DIRECT flag was there, so io_wait on sync data.
And clean a little code style.

tools/blktap/drivers/block-aio.c +20 -14
diff -uNrp xen-unstable.hg/tools/blktap/drivers/block-aio.c xen-unstable.hg/tools/blktap/drivers/block-aio.c
@@ -121,6 +121,7 @@ static inline void init_fds(struct disk_
 static int tdaio_open (struct disk_driver *dd, const char *name, td_flag_t flags)
 {
 	int i, fd, ret = 0, o_flags;
+	struct stat st;
 	struct td_state    *s   = dd->td_state;
 	struct tdaio_state *prv = (struct tdaio_state *)dd->private;
 
@@ -134,23 +135,28 @@ static int tdaio_open (struct disk_drive
 	/* Open the file */
 	o_flags = O_DIRECT | O_LARGEFILE | 
 		((flags == TD_RDONLY) ? O_RDONLY : O_RDWR);
-        fd = open(name, o_flags);
 
-        if ( (fd == -1) && (errno == EINVAL) ) {
-
-                /* Maybe O_DIRECT isn't supported. */
+	/* Check O_DIRECT, for its bad performance in hole file */
+	if (name && (stat(name, &st) == 0)) {
+		if (st.st_blocks < st.st_size/(512 * 2)) 
+			o_flags &= ~O_DIRECT;
+	}
+
+	fd = open(name, o_flags);
+	if ( (fd == -1) && (errno == EINVAL) ) {
+		/* Maybe O_DIRECT isn't supported. */
 		o_flags &= ~O_DIRECT;
-                fd = open(name, o_flags);
-                if (fd != -1) DPRINTF("WARNING: Accessing image without"
-                                     "O_DIRECT! (%s)\n", name);
-
-        } else if (fd != -1) DPRINTF("open(%s) with O_DIRECT\n", name);
-	
-        if (fd == -1) {
+		fd = open(name, o_flags);
+	}
+
+	if (fd == -1) {
 		DPRINTF("Unable to open [%s] (%d)!\n", name, 0 - errno);
-        	ret = 0 - errno;
-        	goto done;
-        }
+		ret = 0 - errno;
+		goto done;
+	}
+
+	DPRINTF("tap:aio open(%s) %s O_DIRECT\n", name,
+		o_flags & O_DIRECT ? "with" : "without");
 
         prv->fd = fd;
 
tools/blktap/drivers/Makefile +1 -1
diff -uNrp xen-unstable.hg/tools/blktap/drivers/Makefile xen-unstable.hg/tools/blktap/drivers/Makefile
@@ -11,7 +11,7 @@ CFLAGS   += -I../lib
 CFLAGS   += $(CFLAGS_libxenctrl)
 CFLAGS   += $(CFLAGS_libxenstore)
 CFLAGS   += -I $(LIBAIO_DIR)
-CFLAGS   += -D_GNU_SOURCE
+CFLAGS   += -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
 
 ifeq ($(shell . ./check_gcrypt $(CC)),yes)
 CFLAGS += -DUSE_GCRYPT

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel