Skip to content

Commit abf38ab

Browse files
neerajsi-msftgitster
authored andcommitted
core.fsyncmethod: add writeout-only mode
This commit introduces the `core.fsyncMethod` configuration knob, which can currently be set to `fsync` or `writeout-only`. The new writeout-only mode attempts to tell the operating system to flush its in-memory page cache to the storage hardware without issuing a CACHE_FLUSH command to the storage controller. Writeout-only fsync is significantly faster than a vanilla fsync on common hardware, since data is written to a disk-side cache rather than all the way to a durable medium. Later changes in this patch series will take advantage of this primitive to implement batching of hardware flushes. When git_fsync is called with FSYNC_WRITEOUT_ONLY, it may fail and the caller is expected to do an ordinary fsync as needed. On Apple platforms, the fsync system call does not issue a CACHE_FLUSH directive to the storage controller. This change updates fsync to do fcntl(F_FULLFSYNC) to make fsync actually durable. We maintain parity with existing behavior on Apple platforms by setting the default value of the new core.fsyncMethod option. Signed-off-by: Neeraj Singh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 19d3f22 commit abf38ab

File tree

13 files changed

+184
-8
lines changed

13 files changed

+184
-8
lines changed

Documentation/config/core.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,15 @@ core.whitespace::
547547
is relevant for `indent-with-non-tab` and when Git fixes `tab-in-indent`
548548
errors. The default tab width is 8. Allowed values are 1 to 63.
549549

550+
core.fsyncMethod::
551+
A value indicating the strategy Git will use to harden repository data
552+
using fsync and related primitives.
553+
+
554+
* `fsync` uses the fsync() system call or platform equivalents.
555+
* `writeout-only` issues pagecache writeback requests, but depending on the
556+
filesystem and storage hardware, data added to the repository may not be
557+
durable in the event of a system crash. This is the default mode on macOS.
558+
550559
core.fsyncObjectFiles::
551560
This boolean will enable 'fsync()' when writing object files.
552561
+

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ all::
411411
#
412412
# Define HAVE_CLOCK_MONOTONIC if your platform has CLOCK_MONOTONIC.
413413
#
414+
# Define HAVE_SYNC_FILE_RANGE if your platform has sync_file_range.
415+
#
414416
# Define NEEDS_LIBRT if your platform requires linking with librt (glibc version
415417
# before 2.17) for clock_gettime and CLOCK_MONOTONIC.
416418
#
@@ -1897,6 +1899,10 @@ ifdef HAVE_CLOCK_MONOTONIC
18971899
BASIC_CFLAGS += -DHAVE_CLOCK_MONOTONIC
18981900
endif
18991901

1902+
ifdef HAVE_SYNC_FILE_RANGE
1903+
BASIC_CFLAGS += -DHAVE_SYNC_FILE_RANGE
1904+
endif
1905+
19001906
ifdef NEEDS_LIBRT
19011907
EXTLIBS += -lrt
19021908
endif

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,13 @@ extern char *git_replace_ref_base;
995995

996996
extern int fsync_object_files;
997997
extern int use_fsync;
998+
999+
enum fsync_method {
1000+
FSYNC_METHOD_FSYNC,
1001+
FSYNC_METHOD_WRITEOUT_ONLY
1002+
};
1003+
1004+
extern enum fsync_method fsync_method;
9981005
extern int core_preload_index;
9991006
extern int precomposed_unicode;
10001007
extern int protect_hfs;

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ int mingw_getpagesize(void);
329329
#define getpagesize mingw_getpagesize
330330
#endif
331331

332+
int win32_fsync_no_flush(int fd);
333+
#define fsync_no_flush win32_fsync_no_flush
334+
332335
struct rlimit {
333336
unsigned int rlim_cur;
334337
};

compat/win32/flush.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "git-compat-util.h"
2+
#include <winternl.h>
3+
#include "lazyload.h"
4+
5+
int win32_fsync_no_flush(int fd)
6+
{
7+
IO_STATUS_BLOCK io_status;
8+
9+
#define FLUSH_FLAGS_FILE_DATA_ONLY 1
10+
11+
DECLARE_PROC_ADDR(ntdll.dll, NTSTATUS, NTAPI, NtFlushBuffersFileEx,
12+
HANDLE FileHandle, ULONG Flags, PVOID Parameters, ULONG ParameterSize,
13+
PIO_STATUS_BLOCK IoStatusBlock);
14+
15+
if (!INIT_PROC_ADDR(NtFlushBuffersFileEx)) {
16+
errno = ENOSYS;
17+
return -1;
18+
}
19+
20+
memset(&io_status, 0, sizeof(io_status));
21+
if (NtFlushBuffersFileEx((HANDLE)_get_osfhandle(fd), FLUSH_FLAGS_FILE_DATA_ONLY,
22+
NULL, 0, &io_status)) {
23+
errno = EINVAL;
24+
return -1;
25+
}
26+
27+
return 0;
28+
}

config.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,18 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
16001600
return 0;
16011601
}
16021602

1603+
if (!strcmp(var, "core.fsyncmethod")) {
1604+
if (!value)
1605+
return config_error_nonbool(var);
1606+
if (!strcmp(value, "fsync"))
1607+
fsync_method = FSYNC_METHOD_FSYNC;
1608+
else if (!strcmp(value, "writeout-only"))
1609+
fsync_method = FSYNC_METHOD_WRITEOUT_ONLY;
1610+
else
1611+
warning(_("ignoring unknown core.fsyncMethod value '%s'"), value);
1612+
1613+
}
1614+
16031615
if (!strcmp(var, "core.fsyncobjectfiles")) {
16041616
fsync_object_files = git_config_bool(var, value);
16051617
return 0;

config.mak.uname

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ifeq ($(uname_S),Linux)
5757
HAVE_CLOCK_MONOTONIC = YesPlease
5858
# -lrt is needed for clock_gettime on glibc <= 2.16
5959
NEEDS_LIBRT = YesPlease
60+
HAVE_SYNC_FILE_RANGE = YesPlease
6061
HAVE_GETDELIM = YesPlease
6162
FREAD_READS_DIRECTORIES = UnfortunatelyYes
6263
BASIC_CFLAGS += -DHAVE_SYSINFO
@@ -463,6 +464,7 @@ endif
463464
CFLAGS =
464465
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
465466
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
467+
compat/win32/flush.o \
466468
compat/win32/path-utils.o \
467469
compat/win32/pthread.o compat/win32/syslog.o \
468470
compat/win32/trace2_win32_process_info.o \
@@ -640,6 +642,7 @@ ifeq ($(uname_S),MINGW)
640642
COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\"
641643
COMPAT_OBJS += compat/mingw.o compat/winansi.o \
642644
compat/win32/trace2_win32_process_info.o \
645+
compat/win32/flush.o \
643646
compat/win32/path-utils.o \
644647
compat/win32/pthread.o compat/win32/syslog.o \
645648
compat/win32/dirent.o

configure.ac

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,14 @@ AC_COMPILE_IFELSE([CLOCK_MONOTONIC_SRC],
10821082
[AC_MSG_RESULT([no])
10831083
HAVE_CLOCK_MONOTONIC=])
10841084
GIT_CONF_SUBST([HAVE_CLOCK_MONOTONIC])
1085+
1086+
#
1087+
# Define HAVE_SYNC_FILE_RANGE=YesPlease if sync_file_range is available.
1088+
GIT_CHECK_FUNC(sync_file_range,
1089+
[HAVE_SYNC_FILE_RANGE=YesPlease],
1090+
[HAVE_SYNC_FILE_RANGE])
1091+
GIT_CONF_SUBST([HAVE_SYNC_FILE_RANGE])
1092+
10851093
#
10861094
# Define NO_SETITIMER if you don't have setitimer.
10871095
GIT_CHECK_FUNC(setitimer,

contrib/buildsystems/CMakeLists.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,18 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
261261
NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
262262
USE_NED_ALLOCATOR OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
263263
UNICODE _UNICODE HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET HAVE_RTLGENRANDOM)
264-
list(APPEND compat_SOURCES compat/mingw.c compat/winansi.c compat/win32/path-utils.c
265-
compat/win32/pthread.c compat/win32mmap.c compat/win32/syslog.c
266-
compat/win32/trace2_win32_process_info.c compat/win32/dirent.c
267-
compat/nedmalloc/nedmalloc.c compat/strdup.c)
264+
list(APPEND compat_SOURCES
265+
compat/mingw.c
266+
compat/winansi.c
267+
compat/win32/flush.c
268+
compat/win32/path-utils.c
269+
compat/win32/pthread.c
270+
compat/win32mmap.c
271+
compat/win32/syslog.c
272+
compat/win32/trace2_win32_process_info.c
273+
compat/win32/dirent.c
274+
compat/nedmalloc/nedmalloc.c
275+
compat/strdup.c)
268276
set(NO_UNIX_SOCKETS 1)
269277

270278
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ int zlib_compression_level = Z_BEST_SPEED;
4444
int pack_compression_level = Z_DEFAULT_COMPRESSION;
4545
int fsync_object_files;
4646
int use_fsync = -1;
47+
enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
4748
size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
4849
size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
4950
size_t delta_base_cache_limit = 96 * 1024 * 1024;

0 commit comments

Comments
 (0)