Skip to contentSkip to conversation

Replacing the system call pread() with lseek()/xread()/lseek() sequence.

PATCH New Stefan-W. Hahn 6 participants 8 messages Jun 15, 2016 2 messages not in the archive Kbuild

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 11683766523955-git-send-email-

A message that is not in the archive (sent off-list or not yet received): 11683766521544-git-send-email-

#2 lore raw
Quoted 47 lines · Stefan-W. Hahn

From: Stefan-W. Hahn <[email protected]>

Using cygwin with cygwin.dll before 1.5.22 the system call pread() is buggy. This patch introduces NO_PREAD. If NO_PREAD is set git uses a sequence of lseek()/xread()/lseek() to emulate pread.

---
 Makefile          |    7 +++++++
 compat/pread.c    |   18 ++++++++++++++++++
 git-compat-util.h |    5 +++++
 3 files changed, 30 insertions(+), 0 deletions(-)
Makefile +7 -0
index 6c12bc6..43113e9 100644
@@ -69,6 +69,9 @@ all:
 #
 # Define NO_MMAP if you want to avoid mmap.
 #
+# Define NO_PREAD if you have a problem with pread() system call (e.g.
+# cygwin.dll before v1.5.22).
+#
 # Define NO_FAST_WORKING_DIRECTORY if accessing objects in pack files is
 # generally faster on your platform than accessing the working directory.
 #
@@ -523,6 +526,10 @@ ifdef NO_MMAP
 	COMPAT_CFLAGS += -DNO_MMAP
 	COMPAT_OBJS += compat/mmap.o
 endif
+ifdef NO_PREAD
+	COMPAT_CFLAGS += -DNO_PREAD
+	COMPAT_OBJS += compat/pread.o
+endif
 ifdef NO_FAST_WORKING_DIRECTORY
 	BASIC_CFLAGS += -DNO_FAST_WORKING_DIRECTORY
 endif
compat/pread.c new file +3 -0
new file mode 100644
index 0000000..9183c05
@@ -0,0 +1,18 @@
+#include "../git-compat-util.h"
+
+ssize_t git_pread(int fd, void *buf, size_t count, off_t offset)

compat/pread.c line 13

Quoted 10 lines
compat/pread.c new file +10 -0
+{
+        off_t current_offset;
+        ssize_t rc;
+
+        current_offset = lseek(fd, 0, SEEK_CUR);
+
+        if (lseek(fd, offset, SEEK_SET) < 0)
+                return -1;
+
+        rc=read_in_full(fd, buf, count);

Seems to be style inconsistancy between current_offset = and rc= I believe the former is preferred.

compat/pread.c line 16

Quoted 3 lines
compat/pread.c new file +3 -0
+
+        if (current_offset != lseek(fd, current_offset, SEEK_SET))
+                return -1;

How likely are we ever to be in the right place here? Seems vanishingly small putting us firmly in the four syscalls per call space. I wonder if git ever actually cares about the seek location. ie if we could stop reading and resetting it. Probabally not worth working it out I guess as any _sane_ system has one.

Quoted 8 lines
compat/pread.c new file +2 -0
+        return rc;
+}
git-compat-util.h +0 -0
index e023bf1..f8d46d5 100644
@@ -107,6 +107,11 @@ extern int git_munmap(void *start, size_t length);
 #define DEFAULT_PACKED_GIT_LIMIT \

git-compat-util.h extern int git_munmap(void *start, size_t length); · line 117

Quoted 10 lines
git-compat-util.h +5 -0
 	((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
 
+#ifdef NO_PREAD
+#define pread git_pread
+extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
+#endif
+
 #ifdef NO_SETENV
 #define setenv gitsetenv
 extern int gitsetenv(const char *, const char *, int);

-apw

#3 lore raw
Quoted 9 lines · Andy Whitcroft

Review

Quoted 5 lines · Stefan-W. Hahn

Using cygwin with cygwin.dll before 1.5.22 the system call pread() is buggy. This patch introduces NO_PREAD. If NO_PREAD is set git uses a sequence of lseek()/xread()/lseek() to emulate pread.

(quoted diff) +2 -0
+
+        rc=read_in_full(fd, buf, count);

Seems to be style inconsistancy between current_offset = and rc= I believe the former is preferred.

With the exception of this style difference, the patch looked pretty good. Nice work Stefan. Andy's right, we do tend to prefer "rc = read_in_full" over "rc=read_in_full". Quite a bit actually, though Junio is the final decider on all such matters as he gets to choose to accept or reject the patch. ;-)

Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+        if (current_offset != lseek(fd, current_offset, SEEK_SET))
+                return -1;

How likely are we ever to be in the right place here? Seems vanishingly small putting us firmly in the four syscalls per call space. I wonder if git ever actually cares about the seek location. ie if we could stop reading and resetting it. Probabally not worth working it out I guess as any _sane_ system has one.

Andy's right actually. If we are using pread() we aren't relying on the current file pointer. Which means its unnecessary to get the current pointer before seeking to the requested offset, and its unnecessary to restore it before the git_pread() function returns.

Though its a possibly unnecessary optimization as like Andy points out, most sane systems already have a working pread() implementation. And those that don't, well, probably should be made to be sane. But we don't need to make Git suffer there if we don't have to.

Signature · 1 line
Shawn.
#4 lore raw
Quoted 5 lines · Shawn O. Pearce

With the exception of this style difference, the patch looked pretty good. Nice work Stefan. Andy's right, we do tend to prefer "rc = read_in_full" over "rc=read_in_full". Quite a bit actually, though Junio is the final decider on all such matters as he gets to choose to accept or reject the patch. ;-)

I am a nice guy and do not reject a patch for missing two SP characters which means I have to --amend it which takes time away from me. Maybe I should stop being nice ;-).

Quoted 10 lines
Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+
+        if (current_offset != lseek(fd, current_offset, SEEK_SET))
+                return -1;

How likely are we ever to be in the right place here? Seems vanishingly small putting us firmly in the four syscalls per call space. I wonder if git ever actually cares about the seek location. ie if we could stop reading and resetting it. Probabally not worth working it out I guess as any _sane_ system has one.

Quoted 4 lines

Andy's right actually. If we are using pread() we aren't relying on the current file pointer. Which means its unnecessary to get the current pointer before seeking to the requested offset, and its unnecessary to restore it before the git_pread() function returns.

The caller of pread() does not care the current position, but that is not to mean it does not care the position after pread() returns. The current callers do not care, though.

#5 lore raw

Hi,

Quoted 3 lines · Junio C Hamano

The caller of pread() does not care the current position, but that is not to mean it does not care the position after pread() returns. The current callers do not care, though.

Not completely true. We fixed in v1.4.4.1~23 a bug which was triggered by NO_MMAP. Since this recently became the default for cygwin, "git-index-pack --fix-thin" would fail in most cases.

Ciao,
Dscho

#6 lore raw
In reply to Shawn O. Pearce
Quoted 11 lines · Shawn O. Pearce
Quoted 5 lines · Andy Whitcroft

How likely are we ever to be in the right place here? Seems vanishingly small putting us firmly in the four syscalls per call space. I wonder if git ever actually cares about the seek location. ie if we could stop reading and resetting it. Probabally not worth working it out I guess as any _sane_ system has one.

Andy's right actually. If we are using pread() we aren't relying on the current file pointer. Which means its unnecessary to get the current pointer before seeking to the requested offset, and its unnecessary to restore it before the git_pread() function returns.

No this is wrong. The original offset _has_ to be preserved. index-pack counts on it.

Nicolas

#7 lore raw
In reply to Andy Whitcroft

Hi,

Quoted 6 lines · Andy Whitcroft

Review

Quoted 2 lines · Stefan-W. Hahn
(quoted diff) +2 -0
+        if (current_offset != lseek(fd, current_offset, SEEK_SET))
+                return -1;

How likely are we ever to be in the right place here?

You mean something like

	if (current_offset != offset + count &&
			current_offset != lseek(fd, current_offset, SEEK_SET))
		return -1;

instead? Seems cheap enough.

Ciao,
Dscho

#8 lore raw
Quoted 11 lines · Johannes Schindelin

Hi,

Quoted 6 lines · Andy Whitcroft

Review

Quoted 2 lines · Stefan-W. Hahn
(quoted diff) +2 -0
+        if (current_offset != lseek(fd, current_offset, SEEK_SET))
+                return -1;

How likely are we ever to be in the right place here?

Quoted 7 lines

You mean something like

	if (current_offset != offset + count &&
			current_offset != lseek(fd, current_offset, SEEK_SET))
		return -1;

instead? Seems cheap enough.

In the index-pack case it simply will never happen.

Nicolas

#9 lore raw

PATCH 1/1 Replacing the system call pread() with lseek()/xread()/lseek() sequence.

In reply to a message not in the archive

From: Stefan-W. Hahn <[email protected]>

Using cygwin with cygwin.dll before 1.5.22 the system call pread() is buggy. This patch introduces NO_PREAD. If NO_PREAD is set git uses a sequence of lseek()/xread()/lseek() to emulate pread.

---
 Makefile          |    7 +++++++
 compat/pread.c    |   18 ++++++++++++++++++
 git-compat-util.h |    5 +++++
 3 files changed, 30 insertions(+), 0 deletions(-)
Makefile +7 -0
index 6c12bc6..43113e9 100644
@@ -69,6 +69,9 @@ all:
 #
 # Define NO_MMAP if you want to avoid mmap.
 #
+# Define NO_PREAD if you have a problem with pread() system call (e.g.
+# cygwin.dll before v1.5.22).
+#
 # Define NO_FAST_WORKING_DIRECTORY if accessing objects in pack files is
 # generally faster on your platform than accessing the working directory.
 #
@@ -523,6 +526,10 @@ ifdef NO_MMAP
 	COMPAT_CFLAGS += -DNO_MMAP
 	COMPAT_OBJS += compat/mmap.o
 endif
+ifdef NO_PREAD
+	COMPAT_CFLAGS += -DNO_PREAD
+	COMPAT_OBJS += compat/pread.o
+endif
 ifdef NO_FAST_WORKING_DIRECTORY
 	BASIC_CFLAGS += -DNO_FAST_WORKING_DIRECTORY
 endif
compat/pread.c new file +18 -0
new file mode 100644
index 0000000..9183c05
@@ -0,0 +1,18 @@
+#include "../git-compat-util.h"
+
+ssize_t git_pread(int fd, void *buf, size_t count, off_t offset)
+{
+        off_t current_offset;
+        ssize_t rc;
+
+        current_offset = lseek(fd, 0, SEEK_CUR);
+
+        if (lseek(fd, offset, SEEK_SET) < 0)
+                return -1;
+
+        rc=read_in_full(fd, buf, count);
+
+        if (current_offset != lseek(fd, current_offset, SEEK_SET))
+                return -1;
+        return rc;
+}
git-compat-util.h +5 -0
index e023bf1..f8d46d5 100644
@@ -107,6 +107,11 @@ extern int git_munmap(void *start, size_t length);
 #define DEFAULT_PACKED_GIT_LIMIT \
 	((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
 
+#ifdef NO_PREAD
+#define pread git_pread
+extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
+#endif
+
 #ifdef NO_SETENV
 #define setenv gitsetenv
 extern int gitsetenv(const char *, const char *, int);
Signature · 1 line
1.4.4.4.gfa432