kvm tools: Enable O_DIRECT support
PATCH v2 1/1 kvm tools: Enable O_DIRECT support
With Direct I/O, file reads and writes go directly from the applications to the storage device, bypassing the operating system read and write caches. This is useful for applications that manage their own caches.
Open a disk image with O_DIRECT:
$ lkvm run -d ~/img/test.img,direct
The original readonly flag is still supported.
Open a disk image with O_DIRECT and readonly:
$ lkvm run -d ~/img/test.img,direct,ro
--- tools/kvm/builtin-run.c | 23 +++++++++++++++-------- tools/kvm/disk/blk.c | 4 ++-- tools/kvm/disk/core.c | 19 ++++++++++++++----- tools/kvm/include/kvm/disk-image.h | 5 +++-- 4 files changed, 34 insertions(+), 17 deletions(-)
tools/kvm/builtin-run.c +15 -8
@@ -127,9 +127,10 @@ void kvm_run_set_wrapper_sandbox(void)static int img_name_parser (const struct option *opt,const char *arg,int unset) { -char *sep; -struct stat st;char path[PATH_MAX ]; +const char *cur; +struct stat st; +char *sep;if (stat (arg, &st) ==0 &&S_ISDIR (st.st_mode)) {
@@ -169,12 +170,18 @@ static int img_name_parser(const struct option *opt, const char *arg, int unset)die ("Currently only 4 images are supported" ); disk_image[image_count].filename = arg; - sep =strstr (arg,"," ); -if (sep) { -if (strcmp (sep +1 ,"ro" ) ==0 ) - disk_image[image_count].readonly = true; - *sep =0 ; - } + cur = arg; +do { + sep =strstr (cur,"," ); +if (sep) { +if (strncmp (sep +1 ,"ro" ,2 ) ==0 ) + disk_image[image_count].readonly = true; +else if (strncmp (sep +1 ,"direct" ,6 ) ==0 ) + disk_image[image_count].direct = true; + *sep =0 ; + cur = sep +1 ; + } + }while (sep); image_count++;
tools/kvm/disk/blk.c +2 -2
@@ -33,7 +33,7 @@ static bool is_mounted(struct stat *st)return false; } -struct disk_image *blkdev__probe (const char *filename,struct stat *st) +struct disk_image *blkdev__probe (const char *filename,int flags,struct stat *st) {struct disk_image *disk;int fd, r;
@@ -52,7 +52,7 @@ struct disk_image *blkdev__probe(const char *filename, struct stat *st)* Be careful! We are opening host block device! * Open it readonly since we do not want to break user's data on disk. */ - fd =open (filename,O_RDWR ); + fd =open (filename, flags);if (fd <0 )return ERR_PTR (fd);
tools/kvm/disk/core.c +14 -5
@@ -75,21 +75,28 @@ struct disk_image *disk_image__new(int fd, u64 size,return disk; } -struct disk_image *disk_image__open (const char *filename,bool readonly) +struct disk_image *disk_image__open (const char *filename,bool readonly,bool direct) {struct disk_image *disk;struct stat st; -int fd; +int fd, flags; + +if (readonly) + flags =O_RDONLY ; +else + flags =O_RDWR ; +if (direct) + flags |=O_DIRECT ;if (stat (filename, &st) <0 )return ERR_PTR (-errno);/* blk device ?*/ - disk =blkdev__probe (filename, &st); + disk =blkdev__probe (filename, flags, &st);if (!IS_ERR_OR_NULL (disk))return disk; - fd =open (filename, readonly ?O_RDONLY :O_RDWR ); + fd =open (filename, flags);if (fd <0 )return ERR_PTR (fd);
@@ -116,6 +123,7 @@ struct disk_image **disk_image__open_all(struct disk_image_params *params, int cstruct disk_image **disks;const char *filename;bool readonly; +bool direct;void *err;int i;
@@ -131,10 +139,11 @@ struct disk_image **disk_image__open_all(struct disk_image_params *params, int cfor (i =0 ; i < count; i++) { filename = params[i].filename; readonly = params[i].readonly; + direct = params[i].direct;if (!filename)continue ; - disks[i] =disk_image__open (filename, readonly); + disks[i] =disk_image__open (filename, readonly, direct);if (IS_ERR_OR_NULL (disks[i])) {pr_err ("Loading disk image '%s' failed" , filename); err = disks[i];
tools/kvm/include/kvm/disk-image.h +3 -2
@@ -42,6 +42,7 @@ struct disk_image_operations {struct disk_image_params {const char *filename;bool readonly; +bool direct; };struct disk_image {
@@ -58,7 +59,7 @@ struct disk_image {#endif }; -struct disk_image *disk_image__open (const char *filename,bool readonly); +struct disk_image *disk_image__open (const char *filename,bool readonly,bool direct);struct disk_image **disk_image__open_all (struct disk_image_params *params,int count);struct disk_image *disk_image__new (int fd,u64 size,struct disk_image_operations *ops,int mmap);int disk_image__close (struct disk_image *disk);
@@ -71,7 +72,7 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovessize_t disk_image__get_serial (struct disk_image *disk,void *buffer,ssize_t *len);struct disk_image *raw_image__probe (int fd,struct stat *st,bool readonly); -struct disk_image *blkdev__probe (const char *filename,struct stat *st); +struct disk_image *blkdev__probe (const char *filename,int flags,struct stat *st);ssize_t raw_image__read (struct disk_image *disk,u64 sector,const struct iovec *iov,int iovcount,void *param);
Signature · 1 line
1.7.10.4
Quoted 4 lines · Asias He
With Direct I/O, file reads and writes go directly from the applications to the storage device, bypassing the operating system read and write caches. This is useful for applications that manage their own caches.
Quoted 8 lines
Open a disk image with
O_DIRECT:$ lkvm run -d ~/img/test.img,directThe original readonly flag is still supported.
Open a disk image withO_DIRECTand readonly:$ lkvm run -d ~/img/test.img,direct,roSigned-off-by Asias He <[email protected]>
Well, what's the use case for this?
Pekka
Quoted 8 lines · Pekka Enberg
Quoted 7 lines · Asias He
With Direct I/O, file reads and writes go directly from the applications to the storage device, bypassing the operating system read and write caches. This is useful for applications that manage their own caches.
Open a disk image with
O_DIRECT:$ lkvm run -d ~/img/test.img,direct
Quoted 7 lines
Quoted 5 lines
The original readonly flag is still supported.
Open a disk image withO_DIRECTand readonly:$ lkvm run -d ~/img/test.img,direct,roSigned-off-by Asias He <[email protected]>Well, what's the use case for this?
With O_DIRECT, host page cache will be skipped. It saves the copying of data between page cache and user space, thus reduces cpu utilization and memory usage. This might be useful for users with high-end storage.
It's also useful when we benchmark the performance of the storage stack in kvm tool side because it excludes one factor we are not interested in.
Signature · 1 line
Asias He