Skip to contentSkip to conversation

Update UBD to use pread/pwrite family of functions

PATCH In review Anton Ivanov 3 participants 5 messages Nov 8, 2015 – Nov 24, 2015 User-mode Linux (UML)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Update UBD to use pread/pwrite family of functions

This decreases the number of syscalls per read/write by half.

---
 arch/um/drivers/ubd_kern.c  | 27 +++++----------------------
 arch/um/include/shared/os.h |  2 ++
 arch/um/os-Linux/file.c     | 19 +++++++++++++++++++
 3 files changed, 26 insertions(+), 22 deletions(-)
arch/um/drivers/ubd_kern.c +5 -22
index e8ab93c..39ba207 100644
@@ -535,11 +535,7 @@ static int read_cow_bitmap(int fd, void *buf, int offset, int len)
 {
 	int err;
 
-	err = os_seek_file(fd, offset);
-	if (err < 0)
-		return err;
-
-	err = os_read_file(fd, buf, len);
+	err = os_pread_file(fd, buf, len, offset);
 	if (err < 0)
 		return err;
 
@@ -1377,14 +1373,8 @@ static int update_bitmap(struct io_thread_req *req)
 	if(req->cow_offset == -1)
 		return 0;
 
-	n = os_seek_file(req->fds[1], req->cow_offset);
-	if(n < 0){
-		printk("do_io - bitmap lseek failed : err = %d\n", -n);
-		return 1;
-	}
-
-	n = os_write_file(req->fds[1], &req->bitmap_words,
-			  sizeof(req->bitmap_words));
+	n = os_pwrite_file(req->fds[1], &req->bitmap_words,
+			  sizeof(req->bitmap_words), req->cow_offset);
 	if(n != sizeof(req->bitmap_words)){
 		printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
 		       req->fds[1]);
@@ -1399,7 +1389,6 @@ static void do_io(struct io_thread_req *req)
 	char *buf;
 	unsigned long len;
 	int n, nsectors, start, end, bit;
-	int err;
 	__u64 off;
 
 	if (req->op == UBD_FLUSH) {
@@ -1428,18 +1417,12 @@ static void do_io(struct io_thread_req *req)
 		len = (end - start) * req->sectorsize;
 		buf = &req->buffer[start * req->sectorsize];
 
-		err = os_seek_file(req->fds[bit], off);
-		if(err < 0){
-			printk("do_io - lseek failed : err = %d\n", -err);
-			req->error = 1;
-			return;
-		}
 		if(req->op == UBD_READ){
 			n = 0;
 			do {
 				buf = &buf[n];
 				len -= n;
-				n = os_read_file(req->fds[bit], buf, len);
+				n = os_pread_file(req->fds[bit], buf, len, off);
 				if (n < 0) {
 					printk("do_io - read failed, err = %d "
 					       "fd = %d\n", -n, req->fds[bit]);
@@ -1449,7 +1432,7 @@ static void do_io(struct io_thread_req *req)
 			} while((n < len) && (n != 0));
 			if (n < len) memset(&buf[n], 0, len - n);
 		} else {
-			n = os_write_file(req->fds[bit], buf, len);
+			n = os_pwrite_file(req->fds[bit], buf, len, off);
 			if(n != len){
 				printk("do_io - write failed err = %d "
 				       "fd = %d\n", -n, req->fds[bit]);
arch/um/include/shared/os.h +2 -0
index 21d704b..de5d572 100644
@@ -146,6 +146,8 @@ extern int os_read_file(int fd, void *buf, int len);
 extern int os_write_file(int fd, const void *buf, int count);
 extern int os_sync_file(int fd);
 extern int os_file_size(const char *file, unsigned long long *size_out);
+extern int os_pread_file(int fd, void *buf, int len, unsigned long long offset);
+extern int os_pwrite_file(int fd, const void *buf, int count, unsigned long long offset);
 extern int os_file_modtime(const char *file, unsigned long *modtime);
 extern int os_pipe(int *fd, int stream, int close_on_exec);
 extern int os_set_fd_async(int fd);
arch/um/os-Linux/file.c +19 -0
index 26e0164..2db18cb 100644
@@ -264,6 +264,15 @@ int os_read_file(int fd, void *buf, int len)
 	return n;
 }
 
+int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
+{
+	int n = pread(fd, buf, len, offset);
+
+	if (n < 0)
+		return -errno;
+	return n;
+}
+
 int os_write_file(int fd, const void *buf, int len)
 {
 	int n = write(fd, (void *) buf, len);
@@ -282,6 +291,16 @@ int os_sync_file(int fd)
 	return n;
 }
 
+int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
+{
+	int n = pwrite(fd, (void *) buf, len, offset);
+
+	if (n < 0)
+		return -errno;
+	return n;
+}
+
+
 int os_file_size(const char *file, unsigned long long *size_out)
 {
 	struct uml_stat buf;
Signature · 8 lines
2.1.4


------------------------------------------------------------------------------
_______________________________________________
User-mode-linux-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel
#1 lore raw

I am going to send in a couple of more which bulk-up the transactions (this forms most of the improvement) later this week.

This one is obvious - qemu has been using it for ages.

A.

Quoted 120 lines · Anton Ivanov

This decreases the number of syscalls per read/write by half.

---
  arch/um/drivers/ubd_kern.c  | 27 +++++----------------------
  arch/um/include/shared/os.h |  2 ++
  arch/um/os-Linux/file.c     | 19 +++++++++++++++++++
  3 files changed, 26 insertions(+), 22 deletions(-)
arch/um/drivers/ubd_kern.c +5 -22
index e8ab93c..39ba207 100644
@@ -535,11 +535,7 @@ static int read_cow_bitmap(int fd, void *buf, int offset, int len)
  {
  	int err;
 
-	err = os_seek_file(fd, offset);
-	if (err < 0)
-		return err;
-
-	err = os_read_file(fd, buf, len);
+	err = os_pread_file(fd, buf, len, offset);
  	if (err < 0)
  		return err;
 
@@ -1377,14 +1373,8 @@ static int update_bitmap(struct io_thread_req *req)
  	if(req->cow_offset == -1)
  		return 0;
 
-	n = os_seek_file(req->fds[1], req->cow_offset);
-	if(n < 0){
-		printk("do_io - bitmap lseek failed : err = %d\n", -n);
-		return 1;
-	}
-
-	n = os_write_file(req->fds[1], &req->bitmap_words,
-			  sizeof(req->bitmap_words));
+	n = os_pwrite_file(req->fds[1], &req->bitmap_words,
+			  sizeof(req->bitmap_words), req->cow_offset);
  	if(n != sizeof(req->bitmap_words)){
  		printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  		       req->fds[1]);
@@ -1399,7 +1389,6 @@ static void do_io(struct io_thread_req *req)
  	char *buf;
  	unsigned long len;
  	int n, nsectors, start, end, bit;
-	int err;
  	__u64 off;
 
  	if (req->op == UBD_FLUSH) {
@@ -1428,18 +1417,12 @@ static void do_io(struct io_thread_req *req)
  		len = (end - start) * req->sectorsize;
  		buf = &req->buffer[start * req->sectorsize];
 
-		err = os_seek_file(req->fds[bit], off);
-		if(err < 0){
-			printk("do_io - lseek failed : err = %d\n", -err);
-			req->error = 1;
-			return;
-		}
  		if(req->op == UBD_READ){
  			n = 0;
  			do {
  				buf = &buf[n];
  				len -= n;
-				n = os_read_file(req->fds[bit], buf, len);
+				n = os_pread_file(req->fds[bit], buf, len, off);
  				if (n < 0) {
  					printk("do_io - read failed, err = %d "
  					       "fd = %d\n", -n, req->fds[bit]);
@@ -1449,7 +1432,7 @@ static void do_io(struct io_thread_req *req)
  			} while((n < len) && (n != 0));
  			if (n < len) memset(&buf[n], 0, len - n);
  		} else {
-			n = os_write_file(req->fds[bit], buf, len);
+			n = os_pwrite_file(req->fds[bit], buf, len, off);
  			if(n != len){
  				printk("do_io - write failed err = %d "
  				       "fd = %d\n", -n, req->fds[bit]);
arch/um/include/shared/os.h +2 -0
index 21d704b..de5d572 100644
@@ -146,6 +146,8 @@ extern int os_read_file(int fd, void *buf, int len);
  extern int os_write_file(int fd, const void *buf, int count);
  extern int os_sync_file(int fd);
  extern int os_file_size(const char *file, unsigned long long *size_out);
+extern int os_pread_file(int fd, void *buf, int len, unsigned long long offset);
+extern int os_pwrite_file(int fd, const void *buf, int count, unsigned long long offset);
  extern int os_file_modtime(const char *file, unsigned long *modtime);
  extern int os_pipe(int *fd, int stream, int close_on_exec);
  extern int os_set_fd_async(int fd);
arch/um/os-Linux/file.c +12 -0
index 26e0164..2db18cb 100644
@@ -264,6 +264,15 @@ int os_read_file(int fd, void *buf, int len)
  	return n;
  }
 
+int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
+{
+	int n = pread(fd, buf, len, offset);
+
+	if (n < 0)
+		return -errno;
+	return n;
+}
+
  int os_write_file(int fd, const void *buf, int len)
  {
  	int n = write(fd, (void *) buf, len);
@@ -282,6 +291,16 @@ int os_sync_file(int fd)
  	return n;
  }
 
+int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
+{
+	int n = pwrite(fd, (void *) buf, len, offset);

arch/um/os-Linux/file.c int os_sync_file(int fd) · line 306

Quoted 10 lines
arch/um/os-Linux/file.c +7 -0
+
+	if (n < 0)
+		return -errno;
+	return n;
+}
+
+
  int os_file_size(const char *file, unsigned long long *size_out)
  {
  	struct uml_stat buf;

------------------------------------------------------------------------------
_______________________________________________
User-mode-linux-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

#2 lore raw
In reply to Anton Ivanov

Hi list, hi Richard.

Have you had time to review this one? I think it is not contentious - it is something QEMU has been doing for a very long time now. It also gives a measurable speed up, especially for random IO.

I am going to hold off on the rest of the ubd patches because they bulk up the transactions. This is dangerous if you end up in a reentrant situation.

A.

Quoted 120 lines · Anton Ivanov

This decreases the number of syscalls per read/write by half.

---
  arch/um/drivers/ubd_kern.c  | 27 +++++----------------------
  arch/um/include/shared/os.h |  2 ++
  arch/um/os-Linux/file.c     | 19 +++++++++++++++++++
  3 files changed, 26 insertions(+), 22 deletions(-)
arch/um/drivers/ubd_kern.c +5 -22
index e8ab93c..39ba207 100644
@@ -535,11 +535,7 @@ static int read_cow_bitmap(int fd, void *buf, int offset, int len)
  {
  	int err;
 
-	err = os_seek_file(fd, offset);
-	if (err < 0)
-		return err;
-
-	err = os_read_file(fd, buf, len);
+	err = os_pread_file(fd, buf, len, offset);
  	if (err < 0)
  		return err;
 
@@ -1377,14 +1373,8 @@ static int update_bitmap(struct io_thread_req *req)
  	if(req->cow_offset == -1)
  		return 0;
 
-	n = os_seek_file(req->fds[1], req->cow_offset);
-	if(n < 0){
-		printk("do_io - bitmap lseek failed : err = %d\n", -n);
-		return 1;
-	}
-
-	n = os_write_file(req->fds[1], &req->bitmap_words,
-			  sizeof(req->bitmap_words));
+	n = os_pwrite_file(req->fds[1], &req->bitmap_words,
+			  sizeof(req->bitmap_words), req->cow_offset);
  	if(n != sizeof(req->bitmap_words)){
  		printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  		       req->fds[1]);
@@ -1399,7 +1389,6 @@ static void do_io(struct io_thread_req *req)
  	char *buf;
  	unsigned long len;
  	int n, nsectors, start, end, bit;
-	int err;
  	__u64 off;
 
  	if (req->op == UBD_FLUSH) {
@@ -1428,18 +1417,12 @@ static void do_io(struct io_thread_req *req)
  		len = (end - start) * req->sectorsize;
  		buf = &req->buffer[start * req->sectorsize];
 
-		err = os_seek_file(req->fds[bit], off);
-		if(err < 0){
-			printk("do_io - lseek failed : err = %d\n", -err);
-			req->error = 1;
-			return;
-		}
  		if(req->op == UBD_READ){
  			n = 0;
  			do {
  				buf = &buf[n];
  				len -= n;
-				n = os_read_file(req->fds[bit], buf, len);
+				n = os_pread_file(req->fds[bit], buf, len, off);
  				if (n < 0) {
  					printk("do_io - read failed, err = %d "
  					       "fd = %d\n", -n, req->fds[bit]);
@@ -1449,7 +1432,7 @@ static void do_io(struct io_thread_req *req)
  			} while((n < len) && (n != 0));
  			if (n < len) memset(&buf[n], 0, len - n);
  		} else {
-			n = os_write_file(req->fds[bit], buf, len);
+			n = os_pwrite_file(req->fds[bit], buf, len, off);
  			if(n != len){
  				printk("do_io - write failed err = %d "
  				       "fd = %d\n", -n, req->fds[bit]);
arch/um/include/shared/os.h +2 -0
index 21d704b..de5d572 100644
@@ -146,6 +146,8 @@ extern int os_read_file(int fd, void *buf, int len);
  extern int os_write_file(int fd, const void *buf, int count);
  extern int os_sync_file(int fd);
  extern int os_file_size(const char *file, unsigned long long *size_out);
+extern int os_pread_file(int fd, void *buf, int len, unsigned long long offset);
+extern int os_pwrite_file(int fd, const void *buf, int count, unsigned long long offset);
  extern int os_file_modtime(const char *file, unsigned long *modtime);
  extern int os_pipe(int *fd, int stream, int close_on_exec);
  extern int os_set_fd_async(int fd);
arch/um/os-Linux/file.c +12 -0
index 26e0164..2db18cb 100644
@@ -264,6 +264,15 @@ int os_read_file(int fd, void *buf, int len)
  	return n;
  }
 
+int os_pread_file(int fd, void *buf, int len, unsigned long long offset)
+{
+	int n = pread(fd, buf, len, offset);
+
+	if (n < 0)
+		return -errno;
+	return n;
+}
+
  int os_write_file(int fd, const void *buf, int len)
  {
  	int n = write(fd, (void *) buf, len);
@@ -282,6 +291,16 @@ int os_sync_file(int fd)
  	return n;
  }
 
+int os_pwrite_file(int fd, const void *buf, int len, unsigned long long offset)
+{
+	int n = pwrite(fd, (void *) buf, len, offset);

arch/um/os-Linux/file.c int os_sync_file(int fd) · line 306

Quoted 10 lines
arch/um/os-Linux/file.c +7 -0
+
+	if (n < 0)
+		return -errno;
+	return n;
+}
+
+
  int os_file_size(const char *file, unsigned long long *size_out)
  {
  	struct uml_stat buf;

------------------------------------------------------------------------------
_______________________________________________
User-mode-linux-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel

#3 lore raw
Quoted 9 lines · Anton Ivanov

Hi list, hi Richard.

Have you had time to review this one? I think it is not contentious - it is something QEMU has been doing for a very long time now. It also gives a measurable speed up, especially for random IO.

I am going to hold off on the rest of the ubd patches because they bulk up the transactions. This is dangerous if you end up in a reentrant situation.

Sorry for the delay.
The patch looks useful! Thanks a lot!

Please CC me in future such that patches don't get lost. I handle CC'ed mails with higher priority. (This is my way to deal with the LKML storm ;))

Signature · 13 lines
Thanks,
//richard

------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
_______________________________________________
User-mode-linux-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel
#4 lore raw
Quoted 8 lines · Richard Weinberger
Quoted 6 lines · Anton Ivanov

Hi list, hi Richard.

Have you had time to review this one? I think it is not contentious - it is something QEMU has been doing for a very long time now. It also gives a measurable speed up, especially for random IO.

Quoted 7 lines
Quoted 3 lines

I am going to hold off on the rest of the ubd patches because they bulk up the transactions. This is dangerous if you end up in a reentrant situation.

Sorry for the delay.
The patch looks useful! Thanks a lot!

Please CC me in future such that patches don't get lost.

OK.

I have a few that follow it to improve UBD even further, but I am going to hold them off for now until we have the IRQ controller sorted.

Multi-read/write and irq/io handler reentrancy do not get along very well :(

A.

Quoted 3 lines

I handle CC'ed mails with higher priority.
(This is my way to deal with the LKML storm ;))

------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK. Use one codebase in this all-in-one HTML5 development environment. Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
_______________________________________________
User-mode-linux-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/user-mode-linux-devel