index-helper, watchman
Subsystems (2)
PATCH v4 1/16 read-cache.c: fix constness of verify_hdr()
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
--- read-cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
read-cache.c +1 -1
@@ -1345,7 +1345,7 @@ struct ondisk_cache_entry_extended { ondisk_cache_entry_extended_size(ce_namelen(ce)) : \ ondisk_cache_entry_size(ce_namelen(ce))) -static int verify_hdr (struct cache_header *hdr,unsigned long size) +static int verify_hdr (const struct cache_header *hdr,unsigned long size) {git_SHA_CTX c;unsigned char sha1[20 ];
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 2/16 read-cache: allow to keep mmap'd memory after reading
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
Later, we will introduce git index-helper to share this memory with other git processes.
Since the memory will be shared, it will never be unmapped (although the kernel may of course choose to page it out).
--- cache.h | 3 +++ read-cache.c | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
cache.h +3 -0
@@ -333,11 +333,14 @@ struct index_state {struct split_index *split_index;struct cache_time timestamp; unsigned name_hash_initialized : 1, + keep_mmap : 1, initialized : 1;struct hashmap name_hash;struct hashmap dir_hash;unsigned char sha1[20 ];struct untracked_cache *untracked; +void *mmap; +size_t mmap_size; };extern struct index_state the_index;
read-cache.c +12 -1
@@ -1574,6 +1574,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) mmap =xmmap (NULL , mmap_size,PROT_READ ,MAP_PRIVATE , fd,0 );if (mmap ==MAP_FAILED )die_errno ("unable to map index file" ); +if (istate->keep_mmap) { + istate->mmap = mmap; + istate->mmap_size = mmap_size; + }close (fd); hdr = mmap;
@@ -1626,10 +1630,12 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) src_offset +=8 ; src_offset += extsize; } -munmap (mmap, mmap_size); +if (!istate->keep_mmap) +munmap (mmap, mmap_size);return istate->cache_nr; unmap: + istate->mmap =NULL ;munmap (mmap, mmap_size);die ("index file corrupt" ); }
@@ -1655,6 +1661,7 @@ int read_index_from(struct index_state *istate, const char *path)discard_index (split_index->base); else split_index->base = xcalloc(1, sizeof(*split_index->base)); + split_index->base->keep_mmap = istate->keep_mmap; ret =do_read_index (split_index->base,git_path ("sharedindex.%s" ,sha1_to_hex (split_index->base_sha1)),1 );
@@ -1698,6 +1705,10 @@ int discard_index(struct index_state *istate)free (istate->cache); istate->cache =NULL ; istate->cache_alloc =0 ; +if (istate->keep_mmap && istate->mmap) { +munmap (istate->mmap, istate->mmap_size); + istate->mmap =NULL ; + }discard_split_index (istate);free_untracked_cache (istate->untracked); istate->untracked =NULL ;
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 3/16 index-helper: new daemon for caching index and related stuff
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
Instead of reading the index from disk and worrying about disk corruption, the index is cached in memory (memory bit-flips happen too, but hopefully less often). The result is faster read. Read time is reduced by 70%.
The biggest gain is not having to verify the trailing SHA-1, which takes lots of time especially on large index files. But this also opens doors for further optimiztions:
- we could create an in-memory format that's essentially the memory dump of the index to eliminate most of parsing/allocation overhead. The mmap'd memory can be used straight away. Experiment
[1] shows we could reduce read time by 88%.
- we could cache non-index info such as name hash
Shared memory is done by storing files in a per-repository temporary directory. This is more portable than shm (which requires posix-realtime and has various quirks on OS X). It might even work on Windows, although this has not been tested. The shared memory file's name folows the template "git-<object>-<SHA1>" where <SHA1> is the trailing SHA-1 of the index file. <object> is "index" for cached index files (and might later be "name-hash" for name-hash cache). If such shared memory exists, it contains the same index content as on disk. The content is already validated by the daemon and git won't validate it again (except comparing the trailing SHA-1s).
We keep this daemon's logic as thin as possible. The "brain" stays in git. So the daemon can read and validate stuff, but that's all it's allowed to do. It does not add/create new information. It doesn't even accept direct updates from git.
Git can poke the daemon via unix domain sockets to tell it to refresh the index cache, or to keep it alive some more minutes. It can't give any real index data directly to the daemon. Real data goes to disk first, then the daemon reads and verifies it from there. Poking only happens for $GIT_DIR/index, not temporary index files.
$GIT_DIR/index-helper.path is a symlink to the socket for the daemon process. The daemon reads from the socket and executes commands.
Named pipes were considered for portability reasons, but then commands that need replies from the daemon would have open their own pipes, since a named pipe should only have one reader. Unix domain sockets don't have this problem.
On webkit.git with index format v2, duplicating 8 times to 1.4m entries and 200MB in size:
(vanilla) 0.986986364 s: read_index_from .git/index
(index-helper) 0.267850279 s: read_index_from .git/index
Interestingly with index v4, we get less out of index-helper. It makes sense as v4 requires more processing after loading the index:
(vanilla) 0.722496666 s: read_index_from .git/index
(index-helper) 0.302741500 s: read_index_from .git/index
(these benchmarks are from an earlier version of this patch, but should still be valid).
[1] http://thread.gmane.org/gmane.comp.version-control.git/247268/focus=248771
--- .gitignore | 1 + Documentation/git-index-helper.txt | 48 +++++ Makefile | 5 + cache.h | 2 + git-compat-util.h | 18 ++ index-helper.c | 354 +++++++++++++++++++++++++++++++++++++ read-cache.c | 172 +++++++++++++++++- t/t7900-index-helper.sh | 23 +++ 8 files changed, 614 insertions(+), 9 deletions(-) create mode 100644 Documentation/git-index-helper.txt create mode 100644 index-helper.c create mode 100755 t/t7900-index-helper.sh
.gitignore +1 -0
@@ -71,6 +71,7 @@ /git-http-fetch /git-http-push /git-imap-send +/git-index-helper /git-index-pack /git-init /git-init-db
Documentation/git-index-helper.txt new file +48 -0
48 lines not on this page · raw message
Makefile +5 -0
@@ -1433,6 +1433,10 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifndef NO_MMAP + PROGRAM_OBJS += index-helper.o +endif + ifdef DIR_HAS_BSD_GROUP_SEMANTICS COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS endif
@@ -2159,6 +2163,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+ @echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+ @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+ + @echo NO_MMAP=\''$(subst ','\'',$(subst ','\'',$(NO_MMAP)))'\' >>$@+ ifdef TEST_OUTPUT_DIRECTORY @echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@+ endif
cache.h +2 -0
@@ -334,6 +334,8 @@ struct index_state {struct cache_time timestamp; unsigned name_hash_initialized : 1, keep_mmap : 1, + from_shm : 1, + to_shm : 1, initialized : 1;struct hashmap name_hash;struct hashmap dir_hash;
git-compat-util.h +18 -0
@@ -513,6 +513,7 @@ static inline int ends_with(const char *str, const char *suffix)#define PROT_READ 1#define PROT_WRITE 2#define MAP_PRIVATE 1 +#define MAP_SHARED 2#endif #define mmap git_mmap
@@ -1045,4 +1046,21 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);#define getc_unlocked (fh) getc(fh)#endif +#ifdef __linux__ +#define UNIX_PATH_MAX 108 +#elif defined(__APPLE__) || defined(BSD ) +#define UNIX_PATH_MAX 104 +#else +/* +* Quoth POSIX: The size of sun_path has intentionally been left +* undefined. This is because different implementations use different +* sizes. For example, 4.3 BSD uses a size of 108, and 4.4 BSD uses a +* size of 104. Since most implementations originate from BSD +* versions, the size is typically in the range 92 to 108. +* +* Thanks, POSIX! Super-helpful! Hope we don't overflow any buffers! +*/ +#define UNIX_PATH_MAX 92 +#endif +#endif
index-helper.c new file +354 -0
354 lines not on this page · raw message
read-cache.c +163 -9
223 lines not on this page · raw message
t/t7900-index-helper.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/sh +# +# Copyright (c) 2016, Twitter, Inc +# + +test_description='git-index-helper + +Testing git index-helper +' + +. ./test-lib.sh + +test -n "$NO_MMAP" && { + skip_all='skipping index-helper tests: no mmap' + test_done +} + +test_expect_success 'index-helper smoke test' ' + git index-helper --exit-after 1 && + test_path_is_missing .git/index-helper.path +' + +test_done
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 4/16 index-helper: add --strict
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
There are "holes" in the index-helper approach because the shared memory is not verified again by git. If $USER is compromised, shared memory could be modified. But anyone who could do this could already modify $GIT_DIR/index. A more realistic risk is some bugs in index-helper that produce corrupt shared memory. --strict is added to avoid that.
Strictly speaking there's still a very small gap where corrupt shared memory could still be read by git: after we write the trailing SHA-1 in the shared memory (thus signaling "this shm is ready") and before verify_shm() detects an error.
--- Documentation/git-index-helper.txt | 9 +++++++ cache.h | 1 + index-helper.c | 48 ++++++++++++++++++++++++++++++++++++++ read-cache.c | 9 ++++--- 4 files changed, 64 insertions(+), 3 deletions(-)
Documentation/git-index-helper.txt +9 -0
@@ -22,6 +22,15 @@ OPTIONS Exit if the cached index is not accessed for `<n>` seconds. Specify 0 to wait forever. Default is 600. +--strict:: +--no-strict:: + Strict mode makes index-helper verify the shared memory after + it's created. If the result does not match what's read from + $GIT_DIR/index, the shared memory is destroyed. This makes + index-helper take more than double the amount of time required + for reading an index, but because it will happen in the + background, it's not noticable. `--strict` is enabled by default. + NOTES -----
cache.h +1 -0
@@ -336,6 +336,7 @@ struct index_state { keep_mmap : 1, from_shm : 1, to_shm : 1, + always_verify_trailing_sha1 : 1, initialized : 1;struct hashmap name_hash;struct hashmap dir_hash;
index-helper.c +48 -0
71 lines not on this page · raw message
read-cache.c +6 -3
@@ -1721,9 +1721,12 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) istate->mmap = mmap; istate->mmap_size = mmap_size; -if (try_shm (istate) && -verify_hdr (istate->mmap, istate->mmap_size) <0 ) - goto unmap; +if (try_shm (istate)) { +if (verify_hdr (istate->mmap, istate->mmap_size) <0 ) + goto unmap; + }else if (istate->always_verify_trailing_sha1 && +verify_hdr (istate->mmap, istate->mmap_size) <0 ) + goto unmap; hdr = mmap = istate->mmap; mmap_size = istate->mmap_size;if (!istate->keep_mmap)
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 5/16 daemonize(): set a flag before exiting the main process
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
This allows signal handlers and atexit functions to realize this situation and not clean up.
--- builtin/gc.c | 2 +- cache.h | 2 +- daemon.c | 2 +- setup.c | 4 +++- 4 files changed, 6 insertions(+), 4 deletions(-)
builtin/gc.c +1 -1
@@ -385,7 +385,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)* failure to daemonize is ok, we'll continue * in foreground */ - daemonized = !daemonize (); + daemonized = !daemonize (NULL ); } }else add_repack_all_option ();
cache.h +1 -1
@@ -530,7 +530,7 @@ extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);extern int init_db (const char *template_dir,unsigned int flags);extern void sanitize_stdfds (void ); -extern int daemonize (void ); +extern int daemonize (int *);#define alloc_nr (x) (((x)+16)*3/2)
daemon.c +1 -1
@@ -1365,7 +1365,7 @@ int main(int argc, char **argv)return execute ();if (detach) { -if (daemonize ()) +if (daemonize (NULL ))die ("--detach not supported on this platform" ); }else sanitize_stdfds ();
setup.c +3 -1
@@ -1017,7 +1017,7 @@ void sanitize_stdfds(void)close (fd); } -int daemonize (void ) +int daemonize (int *daemonized) {#ifdef NO_POSIX_GOODIES errno =ENOSYS ;
@@ -1029,6 +1029,8 @@ int daemonize(void)case -1 :die_errno ("fork failed" );default : +if (daemonized) + *daemonized =1 ;exit (0 ); }if (setsid () ==-1 )
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
v4 cover index-helper, watchman
This version has been tested on OS X and Linux. It switches from POSIX-Realtime shm to plain old mmap. Thanks to JI for this suggestion.
This version incorporates a couple of fixes from Ramsay Jones.
There might be a couple of minor spelling/grammar fixes too.
David Turner (6):
unpack-trees: preserve index extensions
index-helper: kill mode
index-helper: don't run if already running
index-helper: autorun mode
index-helper: optionally automatically run
read-cache: config for waiting for index-helper
Nguyễn Thái Ngọc Duy (10):
read-cache.c: fix constness of verify_hdr()
read-cache: allow to keep mmap'd memory after reading
index-helper: new daemon for caching index and related stuff
index-helper: add --strict
daemonize(): set a flag before exiting the main process
index-helper: add --detach
read-cache: add watchman 'WAMA' extension
Add watchman support to reduce index refresh cost
index-helper: use watchman to avoid refreshing index with lstat()
update-index: enable/disable watchman support
.gitignore | 1 + Documentation/config.txt | 10 + Documentation/git-index-helper.txt | 66 +++++ Makefile | 17 ++ builtin/gc.c | 2 +- builtin/update-index.c | 11 + cache.h | 23 +- config.c | 10 + configure.ac | 8 + daemon.c | 2 +- dir.c | 23 +- dir.h | 6 + environment.c | 8 + git-compat-util.h | 18 ++ git.c | 35 ++- index-helper.c | 534 +++++++++++++++++++++++++++++++++++++ read-cache.c | 520 +++++++++++++++++++++++++++++++++++- setup.c | 4 +- t/t7063-status-untracked-cache.sh | 22 ++ t/t7900-index-helper.sh | 68 +++++ t/test-lib-functions.sh | 4 + unpack-trees.c | 1 + watchman-support.c | 135 ++++++++++ watchman-support.h | 7 + 24 files changed, 1513 insertions(+), 22 deletions(-) create mode 100644 Documentation/git-index-helper.txt create mode 100644 index-helper.c create mode 100755 t/t7900-index-helper.sh create mode 100644 watchman-support.c create mode 100644 watchman-support.h
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 6/16 index-helper: add --detach
From: Nguyễn Thái Ngọc Duy <[email protected]>
We detach after creating and opening the socket, because otherwise we might return control to the shell before index-helper is ready to accept commands. This might lead to flaky tests.
--- Documentation/git-index-helper.txt | 3 +++ index-helper.c | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-)
Documentation/git-index-helper.txt +3 -0
@@ -31,6 +31,9 @@ OPTIONS for reading an index, but because it will happen in the background, it's not noticable. `--strict` is enabled by default. +--detach:: + Detach from the shell. + NOTES -----
index-helper.c +9 -2
@@ -16,7 +16,7 @@ struct shm {static struct shm shm_index;static struct shm shm_base_index; -static int to_verify =1 ; +static int daemonized, to_verify =1 ;static char *my_tmp_dir;
@@ -103,6 +103,8 @@ static void cleanup_shm(void)static void cleanup (void ) { +if (daemonized) +return ;unlink (git_path ("index-helper.path" ));cleanup_shm (); }
@@ -354,7 +356,7 @@ static const char * const usage_text[] = {int main (int argc,char **argv) {const char *prefix; -int idle_in_seconds =600 ; +int idle_in_seconds =600 , detach =0 ;int fd;struct strbuf socket_path =STRBUF_INIT ;struct option options[] = {
@@ -362,6 +364,7 @@ int main(int argc, char **argv) N_("exit if not used after some seconds")),OPT_BOOL (0 ,"strict" , &to_verify,"verify shared memory after creating" ), +OPT_BOOL (0 ,"detach" , &detach,"detach the process" ), OPT_END() };
@@ -396,6 +399,10 @@ int main(int argc, char **argv)die (_ ("failed to delete old index-helper.path" ));if (symlink (get_my_tmp_dir (),git_path ("index-helper.path" )))die (_ ("failed to symlink socket path into index-helper.path" )); + +if (detach &&daemonize (&daemonized)) +die_errno (_ ("unable to detach" )); +loop (fd, idle_in_seconds);return 0 ;
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 7/16 read-cache: add watchman 'WAMA' extension
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
The extension contains a bitmap, one bit for each entry in the index. If the n-th bit is zero, the n-th entry is considered unchanged, we can ce_mark_uptodate() it without refreshing. If the bit is non-zero and we found out the corresponding file is clean after refresh, we can clear the bit.
In addition, there's a list of directories in the untracked-cache to invalidate (because they have new or modified entries).
The 'skipping refresh' bit is not in this patch yet as we would need watchman. More details in later patches.
--- cache.h | 4 ++ dir.h | 3 ++ read-cache.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 122 insertions(+), 2 deletions(-)
cache.h +4 -0
@@ -182,6 +182,8 @@ struct cache_entry {#define CE_VALID (0x8000)#define CE_STAGESHIFT 12 +#define CE_WATCHMAN_DIRTY (0x0001) +/* * Range 0xFFFF0FFF in ce_flags is divided into * two parts: in-memory flags and on-disk ones.
@@ -320,6 +322,7 @@ static inline unsigned int canon_mode(unsigned int mode)#define CACHE_TREE_CHANGED (1 << 5)#define SPLIT_INDEX_ORDERED (1 << 6)#define UNTRACKED_CHANGED (1 << 7) +#define WATCHMAN_CHANGED (1 << 8)struct split_index ;struct untracked_cache ;
@@ -344,6 +347,7 @@ struct index_state {struct untracked_cache *untracked;void *mmap;size_t mmap_size; +char *last_update; };extern struct index_state the_index;
dir.h +3 -0
@@ -142,6 +142,9 @@ struct untracked_cache {int gitignore_invalidated;int dir_invalidated;int dir_opened; +/* watchman invalidation data */ + unsigned int use_watchman : 1; +struct string_list invalid_untracked; };struct dir_struct {
read-cache.c +115 -2
164 lines not on this page · raw message
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 8/16 Add watchman support to reduce index refresh cost
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
The previous patch has the logic to clear bits in 'WAMA' bitmap. This patch has logic to set bits as told by watchman. The missing bit, _using_ these bits, are not here yet.
A lot of this code is written by David Turner originally, mostly from [1]. I'm just copying and polishing it a bit.
[1] http://article.gmane.org/gmane.comp.version-control.git/248006
--- Makefile | 12 +++++ cache.h | 1 + config.c | 5 ++ configure.ac | 8 ++++ environment.c | 3 ++ watchman-support.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++ watchman-support.h | 7 +++ 7 files changed, 171 insertions(+) create mode 100644 watchman-support.c create mode 100644 watchman-support.h
Makefile +12 -0
@@ -451,6 +451,7 @@ MSGFMT = msgfmt CURL_CONFIG = curl-config PTHREAD_LIBS = -lpthread PTHREAD_CFLAGS = +WATCHMAN_LIBS = GCOV = gcov export TCL_PATH TCLTK_PATH
@@ -1416,6 +1417,13 @@ else LIB_OBJS += thread-utils.o endif +ifdef USE_WATCHMAN + LIB_H += watchman-support.h + LIB_OBJS += watchman-support.o + WATCHMAN_LIBS = -lwatchman + BASIC_CFLAGS += -DUSE_WATCHMAN +endif + ifdef HAVE_PATHS_H BASIC_CFLAGS += -DHAVE_PATHS_H endif
@@ -2025,6 +2033,9 @@ git-remote-testsvn$X: remote-testsvn.o GIT-LDFLAGS $(GITLIBS) $(VCSSVN_LIB) $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) \ $(VCSSVN_LIB) +git-index-helper$X: index-helper.o GIT-LDFLAGS $(GITLIBS) + $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) $(WATCHMAN_LIBS) + $(REMOTE_CURL_ALIASES): $(REMOTE_CURL_PRIMARY) $(QUIET_LNCP)$(RM) $@ && \ ln $< $@ 2>/dev/null || \
@@ -2164,6 +2175,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+ @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+ @echo NO_MMAP=\''$(subst ','\'',$(subst ','\'',$(NO_MMAP)))'\' >>$@+ + @echo USE_WATCHMAN=\''$(subst ','\'',$(subst ','\'',$(USE_WATCHMAN)))'\' >>$@+ ifdef TEST_OUTPUT_DIRECTORY @echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@+ endif
cache.h +1 -0
@@ -687,6 +687,7 @@ extern char *git_replace_ref_base;extern int fsync_object_files;extern int core_preload_index; +extern int core_watchman_sync_timeout;extern int core_apply_sparse_checkout;extern int precomposed_unicode;extern int protect_hfs;
config.c +5 -0
@@ -882,6 +882,11 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } +if (!strcmp (var,"core.watchmansynctimeout" )) { + core_watchman_sync_timeout =git_config_int (var, value); +return 0 ; + } +if (!strcmp (var,"core.createobject" )) {if (!strcmp (value,"rename" )) object_creation_mode =OBJECT_CREATION_USES_RENAMES ;
configure.ac +8 -0
@@ -1099,6 +1099,14 @@ AC_COMPILE_IFELSE([BSD_SYSCTL_SRC], HAVE_BSD_SYSCTL=]) GIT_CONF_SUBST([HAVE_BSD_SYSCTL]) +# +# Check for watchman client library + +AC_CHECK_LIB([watchman], [watchman_connect], + [USE_WATCHMAN=YesPlease], + [USE_WATCHMAN=]) +GIT_CONF_SUBST([USE_WATCHMAN]) + ## Other checks. # Define USE_PIC if you need the main git objects to be built with -fPIC # in order to build and link perl/Git.so. x86-64 seems to need this.
environment.c +3 -0
@@ -94,6 +94,9 @@ int core_preload_index = 1;*/ int ignore_untracked_cache_config; +int core_watchman_sync_timeout =300 ; + +/* This is set by setup_git_dir_gently() and/or git_default_config() */ char *git_work_tree_cfg;static char *work_tree;
watchman-support.c new file +135 -0
135 lines not on this page · raw message
watchman-support.h new file +7 -0
@@ -0,0 +1,7 @@ +#ifndef WATCHMAN_SUPPORT_H +#define WATCHMAN_SUPPORT_H + +struct index_state ; +int check_watchman (struct index_state *index); + +#endif /* WATCHMAN_SUPPORT_H */
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 9/16 index-helper: use watchman to avoid refreshing index with lstat()
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
Watchman is hidden behind index-helper. Before git tries to read the index from shm, it notifies index-helper through the socket and waits for index-helper to prepare a file for sharing memory (with MAP_SHARED). index-helper then contacts watchman, updates 'WAMA' extension and put it in a separate file and wakes git up with a reply to git's socket.
Git uses this extension to not lstat unchanged entries. Git only trusts the 'WAMA' extension when it's received from the separate file, not from disk. Unmarked entries are "clean". Marked entries are dirty from watchman point of view. If it finds out some entries are 'watchman-dirty', but are really unchanged (e.g. the file was changed, then reverted back), then Git will clear the marking in 'WAMA' before writing it down.
Hiding watchman behind index-helper means you need both daemons. You can't run watchman alone. Not so good. But on the other hand, 'git' binary is not linked to watchman/json libraries, which is good for packaging. Core git package will run fine without watchman-related packages. If they need watchman, they can install git-index-helper and dependencies.
This also lets us trust anything in the untracked cache that we haven't marked invalid, saving those stat() calls.
Another reason for tying watchman to index-helper is, when used with untracked cache, we need to keep track of $GIT_WORK_TREE file listing. That kind of list can be kept in index-helper.
--- Documentation/git-index-helper.txt | 6 + cache.h | 8 ++ dir.c | 23 +++- dir.h | 3 + index-helper.c | 86 ++++++++++++-- read-cache.c | 232 ++++++++++++++++++++++++++++++++++--- 6 files changed, 332 insertions(+), 26 deletions(-)
Documentation/git-index-helper.txt +6 -0
@@ -53,6 +53,12 @@ command. The following commands are used to control the daemon: Let the daemon know the index is to be read. It keeps the daemon alive longer, unless `--exit-after=0` is used. +"poke <path>": + Like "poke", but replies with "OK". If watchman is + configured, index-helper queries watchman, then prepares a + shared memory object with the watchman index extension before + replying. + All commands and replies are terminated by a 0 byte. GIT
cache.h +8 -0
@@ -558,6 +558,7 @@ extern int daemonize(int *);/* Initialize and use the cache information */ struct lock_file ; +extern int verify_index (const struct index_state *);extern int read_index (struct index_state *);extern int read_index_preload (struct index_state *,const struct pathspec *pathspec); extern int do_read_index(struct index_state *istate, const char *path,
@@ -565,6 +566,7 @@ extern int do_read_index(struct index_state *istate, const char *path,extern int read_index_from (struct index_state *,const char *path);extern int is_index_unborn (struct index_state *);extern int read_index_unmerged (struct index_state *); +extern void write_watchman_ext (struct strbuf *sb,struct index_state * istate);#define COMMIT_LOCK (1 << 0)#define CLOSE_LOCK (1 << 1)extern int write_locked_index (struct index_state *,struct lock_file *lock,unsigned flags);
@@ -1839,4 +1841,10 @@ void sleep_millisec(int millisec);*/ void safe_create_dir (const char *dir,int share); +/* +* Open a socket to the git index-helper. Return the fd of that +* socket, or -1 on error. +*/ +int connect_to_index_helper (void ); +#endif /* CACHE_H */
dir.c +19 -4
@@ -597,9 +597,9 @@ static void trim_trailing_spaces(char *buf)* * If "name" has the trailing slash, it'll be excluded in the search. */ -static struct untracked_cache_dir *lookup_untracked (struct untracked_cache *uc, -struct untracked_cache_dir *dir, -const char *name,int len) +struct untracked_cache_dir *lookup_untracked (struct untracked_cache *uc, +struct untracked_cache_dir *dir, +const char *name,int len) {int first, last;struct untracked_cache_dir *d;
@@ -1726,6 +1726,17 @@ static int valid_cached_dir(struct dir_struct *dir,if (!untracked)return 0 ; +if (dir->untracked->use_watchman) { +/* +* With watchman, we can trust the untracked cache's +* valid field. +*/ +if (untracked->valid) + goto skip_stat; +else +invalidate_directory (dir->untracked, untracked); + } +if (stat (path->len ? path->buf :"." , &st)) {invalidate_directory (dir->untracked, untracked);memset (&untracked->stat_data,0 ,sizeof (untracked->stat_data));
@@ -1739,6 +1750,7 @@ static int valid_cached_dir(struct dir_struct *dir,return 0 ; } +skip_stat:if (untracked->check_only != !!check_only) {invalidate_directory (dir->untracked, untracked);return 0 ;
@@ -2625,8 +2637,10 @@ static void free_untracked(struct untracked_cache_dir *ucd)void free_untracked_cache (struct untracked_cache *uc) { -if (uc) +if (uc) {free_untracked (uc->root); +string_list_clear (&uc->invalid_untracked,0 ); + }free (uc); }
@@ -2775,6 +2789,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned longreturn NULL ; uc =xcalloc (1 ,sizeof (*uc)); +string_list_init (&uc->invalid_untracked,1 );strbuf_init (&uc->ident, ident_len);strbuf_add (&uc->ident, ident, ident_len); load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,
dir.h +3 -0
@@ -315,4 +315,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned longvoid write_untracked_extension (struct strbuf *out,struct untracked_cache *untracked);void add_untracked_cache (struct index_state *istate);void remove_untracked_cache (struct index_state *istate); +struct untracked_cache_dir *lookup_untracked (struct untracked_cache *uc, +struct untracked_cache_dir *dir, +const char *name,int len);#endif
index-helper.c +79 -7
130 lines not on this page · raw message
read-cache.c +217 -15
310 lines not on this page · raw message
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 10/16 update-index: enable/disable watchman support
In reply to David TurnerFrom: Nguyễn Thái Ngọc Duy <[email protected]>
--- builtin/update-index.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
builtin/update-index.c +11 -0
@@ -914,6 +914,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) {int newfd, entries, has_errors =0 , nul_term_line =0 ;enum uc_mode untracked_cache =UC_UNSPECIFIED ; +int use_watchman =-1 ;int read_from_stdin =0 ;int prefix_length = prefix ?strlen (prefix) :0 ;int preferred_index_format =0 ;
@@ -1012,6 +1013,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) N_("test if the filesystem supports untracked cache"), UC_TEST),OPT_SET_INT (0 ,"force-untracked-cache" , &untracked_cache,N_ ("enable untracked cache without testing the filesystem" ),UC_FORCE ), +OPT_BOOL (0 ,"watchman" , &use_watchman, +N_ ("use or not use watchman to reduce refresh cost" )), OPT_END() };
@@ -1149,6 +1152,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)die ("Bug: bad untracked_cache value: %d" , untracked_cache); } +if (use_watchman >0 ) { + the_index.last_update =xstrdup ("" ); + the_index.cache_changed |=WATCHMAN_CHANGED ; + }else if (!use_watchman) { + the_index.last_update =NULL ; + the_index.cache_changed |=WATCHMAN_CHANGED ; + } +if (active_cache_changed) {if (newfd <0 ) {if (refresh_args.flags &REFRESH_QUIET )
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 11/16 unpack-trees: preserve index extensions
In reply to David TurnerMake git checkout (and other unpack_tree operations) preserve the untracked cache and watchman status. This is valuable for two reasons:
1. Often, an unpack_tree operation will not touch large parts of the working tree, and thus most of the untracked cache will continue to be valid.
2. Even if the untracked cache were entirely invalidated by such an operation, the user has signaled their intention to have such a cache, and we don't want to throw it away.
The same logic applies to the watchman state.
--- cache.h | 1 + read-cache.c | 8 ++++++++ t/t7063-status-untracked-cache.sh | 22 ++++++++++++++++++++++ t/test-lib-functions.sh | 4 ++++ unpack-trees.c | 1 + 5 files changed, 36 insertions(+)
cache.h +1 -0
@@ -571,6 +571,7 @@ extern void write_watchman_ext(struct strbuf *sb, struct index_state* istate);#define CLOSE_LOCK (1 << 1)extern int write_locked_index (struct index_state *,struct lock_file *lock,unsigned flags);extern int discard_index (struct index_state *); +extern void move_index_extensions (struct index_state *dst,struct index_state *src);extern int unmerged_index (const struct index_state *);extern int verify_path (const char *path);extern int index_dir_exists (struct index_state *istate,const char *name,int namelen);
read-cache.c +8 -0
@@ -2807,3 +2807,11 @@ void stat_validity_update(struct stat_validity *sv, int fd)fill_stat_data (sv->sd, &st); } } + +void move_index_extensions (struct index_state *dst,struct index_state *src) +{ + dst->untracked = src->untracked; + src->untracked =NULL ; + dst->last_update = src->last_update; + src->last_update =NULL ; +}
t/t7063-status-untracked-cache.sh +22 -0
@@ -646,4 +646,26 @@ test_expect_success 'test ident field is working' ' test_cmp ../expect ../err ' +test_expect_success 'untracked cache survives a checkout' ' + git commit --allow-empty -m empty && + test-dump-untracked-cache >../before && + test_when_finished "git checkout master" && + git checkout -b other_branch && + test-dump-untracked-cache >../after && + test_cmp ../before ../after && + test_commit test && + test-dump-untracked-cache >../before && + git checkout master && + test-dump-untracked-cache >../after && + test_cmp ../before ../after +' + +test_expect_success 'untracked cache survives a commit' ' + test-dump-untracked-cache >../before && + git add done/two && + git commit -m commit && + test-dump-untracked-cache >../after && + test_cmp ../before ../after +' + test_done
t/test-lib-functions.sh +4 -0
@@ -186,6 +186,10 @@ test_commit () { test_tick fi && git commit $signoff -m "$1" && + if [ "$(git config core.bare)" = false ] + then + git update-index --force-untracked-cache + fi git tag "${4:-$1}" }
unpack-trees.c +1 -0
@@ -1215,6 +1215,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_optionsWRITE_TREE_SILENT | WRITE_TREE_REPAIR); } +move_index_extensions (&o->result, o->dst_index);discard_index (o->dst_index); *o->dst_index = o->result; }else {
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 12/16 index-helper: kill mode
In reply to David TurnerAdd a new command (and command-line arg) to allow index-helpers to exit cleanly.
This is mainly useful for tests.
--- index-helper.c | 31 ++++++++++++++++++++++++++++++- t/t7900-index-helper.sh | 12 ++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-)
index-helper.c +30 -1
@@ -395,6 +395,8 @@ static void loop(int fd, int idle_in_seconds)* alive, nothing to do. */ } + }else if (!strcmp (command.buf,"die" )) { +break ; }else {warning ("BUG: Bogus command %s" , command.buf); }
@@ -425,10 +427,29 @@ static const char * const usage_text[] = {NULL }; +static void request_kill (void ) +{ +int fd =connect_to_index_helper (); + +if (fd >=0 ) { +write_in_full (fd,"die" ,4 ); +close (fd); + } + +/* +* The child will try to do this anyway, but we want to be +* ready to launch a new daemon immediately after this command +* returns. +*/ + +unlink (git_path ("index-helper.path" )); +return ; +} +int main (int argc,char **argv) {const char *prefix; -int idle_in_seconds =600 , detach =0 ; +int idle_in_seconds =600 , detach =0 , kill =0 ;int fd;struct strbuf socket_path =STRBUF_INIT ;struct option options[] = {
@@ -437,6 +458,7 @@ int main(int argc, char **argv)OPT_BOOL (0 ,"strict" , &to_verify,"verify shared memory after creating" ),OPT_BOOL (0 ,"detach" , &detach,"detach the process" ), +OPT_BOOL (0 ,"kill" , &kill,"request that existing index helper processes exit" ), OPT_END() };
@@ -451,6 +473,13 @@ int main(int argc, char **argv) options, usage_text, 0))die (_ ("too many arguments" )); +if (kill) { +if (detach) +die (_ ("--kill doesn't want any other options" )); +request_kill (); +return 0 ; + } +atexit (cleanup);sigchain_push_common (cleanup_on_signal);
t/t7900-index-helper.sh +12 -0
@@ -20,4 +20,16 @@ test_expect_success 'index-helper smoke test' ' test_path_is_missing .git/index-helper.path ' +test_expect_success 'index-helper creates usable path file and can be killed' ' + test_when_finished "git index-helper --kill" && + test_path_is_missing .git/index-helper.path && + git index-helper --detach && + test -L .git/index-helper.path && + dir="$(readlink .git/index-helper.path)" && + test -S "$dir/s" && + ls -ld "$dir" | grep ^drwx...--- && + git index-helper --kill && + test_path_is_missing .git/index-helper.path +' + test_done
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 13/16 index-helper: don't run if already running
In reply to David Turner--- index-helper.c | 7 +++++++ t/t7900-index-helper.sh | 9 +++++++++ 2 files changed, 16 insertions(+)
index-helper.c +7 -0
@@ -480,6 +480,13 @@ int main(int argc, char **argv)return 0 ; } +/* check that no other copy is running */ + fd =connect_to_index_helper (); +if (fd >0 ) +die (_ ("Already running" )); +if (errno !=ECONNREFUSED && errno !=ENOENT ) +die_errno (_ ("Unexpected error checking socket" )); +atexit (cleanup);sigchain_push_common (cleanup_on_signal);
t/t7900-index-helper.sh +9 -0
@@ -32,4 +32,13 @@ test_expect_success 'index-helper creates usable path file and can be killed' ' test_path_is_missing .git/index-helper.path ' +test_expect_success 'index-helper will not start if already running' ' + test_when_finished "git index-helper --kill" && + git index-helper --detach && + test -L .git/index-helper.path && + test_must_fail git index-helper 2>err && + test -L .git/index-helper.path && + grep "Already running" err +' + test_done
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 14/16 index-helper: autorun mode
In reply to David TurnerSoon, we'll want to automatically start index-helper, so we need a mode that silently exits if it can't start up (either because it's not in a git dir, or because another one is already running).
--- index-helper.c | 29 +++++++++++++++++++++++------ t/t7900-index-helper.sh | 8 ++++++++ 2 files changed, 31 insertions(+), 6 deletions(-)
index-helper.c +23 -6
@@ -449,8 +449,9 @@ static void request_kill(void) int main(int argc, char **argv) { const char *prefix; - int idle_in_seconds = 600, detach = 0, kill = 0; + int idle_in_seconds = 600, detach = 0, kill = 0, autorun = 0; int fd; + int nongit; struct strbuf socket_path = STRBUF_INIT; struct option options[] = { OPT_INTEGER(0, "exit-after", &idle_in_seconds,
@@ -459,6 +460,7 @@ int main(int argc, char **argv) "verify shared memory after creating"),OPT_BOOL (0 ,"detach" , &detach,"detach the process" ),OPT_BOOL (0 ,"kill" , &kill,"request that existing index helper processes exit" ), +OPT_BOOL (0 ,"autorun" , &autorun,"this is an automatic run of git index-helper, so certain errors can be solved by silently exiting" ), OPT_END() };
@@ -468,7 +470,14 @@ int main(int argc, char **argv)if (argc ==2 && !strcmp (argv[1 ],"-h" ))usage_with_options (usage_text, options); - prefix =setup_git_directory (); + prefix =setup_git_directory_gently (&nongit); +if (nongit) { +if (autorun) +exit (0 ); +else +die (_ ("not a git repository" )); + } +if (parse_options (argc, (const char **)argv, prefix, options, usage_text,0 ))die (_ ("too many arguments" ));
@@ -482,10 +491,18 @@ int main(int argc, char **argv)/* check that no other copy is running */ fd =connect_to_index_helper (); -if (fd >0 ) -die (_ ("Already running" )); -if (errno !=ECONNREFUSED && errno !=ENOENT ) -die_errno (_ ("Unexpected error checking socket" )); +if (fd >0 ) { +if (autorun) +exit (0 ); +else +die (_ ("Already running" )); + } +if (errno !=ECONNREFUSED && errno !=ENOENT ) { +if (autorun) +return 0 ; +else +die_errno (_ ("Unexpected error checking socket" )); + }atexit (cleanup);sigchain_push_common (cleanup_on_signal);
t/t7900-index-helper.sh +8 -0
@@ -41,4 +41,12 @@ test_expect_success 'index-helper will not start if already running' ' grep "Already running" err ' +test_expect_success 'index-helper is quiet with --autorun' ' + test_when_finished "git index-helper --kill" && + git index-helper --kill && + git index-helper --detach && + test -L .git/index-helper.path && + git index-helper --autorun +' + test_done
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 15/16 index-helper: optionally automatically run
In reply to David TurnerIntroduce a new config option, indexhelper.autorun, to automatically run git index-helper before starting up a builtin git command. This enables users to keep index-helper running without manual intervention.
--- Documentation/config.txt | 4 ++++ git.c | 35 ++++++++++++++++++++++++++++++++++- t/t7900-index-helper.sh | 16 ++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-)
Documentation/config.txt +4 -0
@@ -1852,6 +1852,10 @@ index.version:: Specify the version with which new index files should be initialized. This does not affect existing repositories. +indexhelper.autorun:: + Automatically run git index-helper when any builtin git + command is run inside a repository. + init.templateDir:: Specify the directory from which templates will be copied. (See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
git.c +34 -1
@@ -521,6 +521,37 @@ static void strip_extension(const char **argv)#define strip_extension (cmd)#endif +static int want_auto_index_helper (void ) +{ +int want =-1 ; +const char *value =NULL ; +const char *key ="indexhelper.autorun" ; + +if (git_config_key_is_valid (key) && + !git_config_get_value (key, &value)) { +int b =git_config_maybe_bool (key, value); + want = b >=0 ? b :0 ; + } +return want; +} + +static void maybe_run_index_helper (struct cmd_struct *cmd) +{ +const char *argv[] = {"git-index-helper" ,"--detach" ,"--autorun" ,NULL }; + +if (!(cmd->option &NEED_WORK_TREE )) +return ; + +if (want_auto_index_helper () <=0 ) +return ; + +trace_argv_printf (argv,"trace: auto index-helper:" ); + +if (run_command_v_opt (argv, +RUN_SILENT_EXEC_FAILURE |RUN_CLEAN_ON_EXIT )) +warning (_ ("You specified indexhelper.autorun, but running git-index-helper failed." )); +} +static void handle_builtin (int argc,const char **argv) {const char *cmd;
@@ -536,8 +567,10 @@ static void handle_builtin(int argc, const char **argv) } builtin =get_builtin (cmd); -if (builtin) +if (builtin) { +maybe_run_index_helper (builtin);exit (run_builtin (builtin, argc, argv)); + } }static void execv_dashed_external (const char **argv)
t/t7900-index-helper.sh +16 -0
@@ -49,4 +49,20 @@ test_expect_success 'index-helper is quiet with --autorun' ' git index-helper --autorun ' +test_expect_success 'index-helper autorun works' ' + rm -f .git/index-helper.path && + git status && + test_path_is_missing .git/index-helper.path && + test_config indexhelper.autorun true && + git status && + test -L .git/index-helper.path && + git status 2>err && + test -L .git/index-helper.path && + ! grep -q . err && + git index-helper --kill && + test_config indexhelper.autorun false && + git status && + test_path_is_missing .git/index-helper.path +' + test_done
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
PATCH v4 16/16 read-cache: config for waiting for index-helper
In reply to David TurnerWhen we poke index-helper, and index-helper is using watchman, we want to wait for a response (showing that the watchman extension shm has been prepared). If it's not using watchman, we don't.
So add a new config, core.waitforindexhelper, with sensible defaults, to configure this behavior.
--- Documentation/config.txt | 6 ++++++ cache.h | 1 + config.c | 5 +++++ environment.c | 5 +++++ read-cache.c | 5 ++++- 5 files changed, 21 insertions(+), 1 deletion(-)
Documentation/config.txt +6 -0
@@ -670,6 +670,12 @@ Likewise, when the `LV` environment variable is unset, Git sets it to `-c`. You can override this setting by exporting `LV` with another value or setting `core.pager` to `lv +c`. +core.waitforindexhelper:: + If true, git status and other commands which use the index + will wait for a response from the index-helper before continuing; + this gives time for the index-helper to communicate with watchman + and give information about modified files. + core.whitespace:: A comma separated list of common whitespace problems to notice. 'git diff' will use `color.diff.whitespace` to
cache.h +1 -0
@@ -691,6 +691,7 @@ extern char *git_replace_ref_base;extern int fsync_object_files;extern int core_preload_index;extern int core_watchman_sync_timeout; +extern int wait_for_index_helper;extern int core_apply_sparse_checkout;extern int precomposed_unicode;extern int protect_hfs;
config.c +5 -0
@@ -887,6 +887,11 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } +if (!strcmp (var,"core.waitforindexhelper" )) { + wait_for_index_helper =git_config_bool (var, value); +return 0 ; + } +if (!strcmp (var,"core.createobject" )) {if (!strcmp (value,"rename" )) object_creation_mode =OBJECT_CREATION_USES_RENAMES ;
environment.c +5 -0
@@ -95,6 +95,11 @@ int core_preload_index = 1;int ignore_untracked_cache_config;int core_watchman_sync_timeout =300 ; +#ifdef USE_WATCHMAN +int wait_for_index_helper =1 ; +#else +int wait_for_index_helper =0 ; +#endif /* This is set by setup_git_dir_gently() and/or git_default_config() */
read-cache.c +4 -1
@@ -1846,7 +1846,10 @@ static int poke_daemon(struct index_state *istate,if (refresh_cache) { ret =write_in_full (fd,"refresh" ,8 ) !=8 ; }else { - ret =poke_and_wait_for_reply (fd); +if (wait_for_index_helper) + ret =poke_and_wait_for_reply (fd); +else + ret =write_in_full (fd,"poke" ,5 ) !=5 ; }close (fd);
Signature · 1 line
2.4.2.767.g62658d5-twtrsrc
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to David TurnerOn 13/04/16 01:32, David Turner wrote:
[snip]
Quoted 23 lines
git-compat-util.h +8
-0@@ -513,6 +513,7 @@ static inline int ends_with(const char *str, const char *suffix)#define PROT_READ 1#define PROT_WRITE 2#define MAP_PRIVATE 1 +#define MAP_SHARED 2#endif #define mmap git_mmap@@ -1045,4 +1046,21 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);#define getc_unlocked (fh) getc(fh)#endif +#ifdef __linux__ +#define UNIX_PATH_MAX 108 +#elif defined(__APPLE__) || defined(BSD ) +#define UNIX_PATH_MAX 104 +#else +/* +* Quoth POSIX: The size of sun_path has intentionally been left
git-compat-util.h struct tm *git_gmtime_r(const time_t *, struct tm *); · line 1065
Quoted 10 lines
git-compat-util.h +10
-0+* undefined. This is because different implementations use different +* sizes. For example, 4.3 BSD uses a size of 108, and 4.4 BSD uses a +* size of 104. Since most implementations originate from BSD +* versions, the size is typically in the range 92 to 108. +* +* Thanks, POSIX! Super-helpful! Hope we don't overflow any buffers! +*/ +#define UNIX_PATH_MAX 92 +#endif +
It seems you forgot to delete this hunk. ;-)
git-compat-util.h struct tm *git_gmtime_r(const time_t *, struct tm *); · line 1066
Quoted 1 line
git-compat-util.h +0
-0#endif
ATB,
Ramsay Jones
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
Quoted 37 lines · Ramsay Jones
On 13/04/16 01:32, David Turner wrote:
[snip]
Quoted 32 lines
git-compat-util.h +1
-0@@ -513,6 +513,7 @@ static inline int ends_with(const char *str, const char *suffix)#define PROT_READ 1#define PROT_WRITE 2#define MAP_PRIVATE 1 +#define MAP_SHARED 2#endif #define mmap
git_mmapgit-compat-util.h +8
-0@@ -1045,4 +1046,21 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);#define getc_unlocked (fh) getc(fh)#endif +#ifdef __linux__ +#define UNIX_PATH_MAX 108 +#elif defined(__APPLE__) || defined(BSD ) +#define UNIX_PATH_MAX 104 +#else +/* +* Quoth POSIX: The size of sun_path has intentionally been left +* undefined. This is because different implementations use different
+ * sizes. For example, 4.3 BSD uses a size of 108, and 4.4 BSD uses a
+ * size of 104. Since most implementations originate from BSD
+ * versions, the size is typically in the range 92 to 108.
+ *
Quoted 8 lines
git-compat-util.h struct tm *git_gmtime_r(const time_t *, struct tm *); · line 1065
Quoted 6 lines
+ * Thanks, POSIX! Super-helpful! Hope we don't overflow any buffers!
+ */git-compat-util.h +3
-0+#define UNIX_PATH_MAX 92 +#endif +It seems you forgot to delete this hunk. ;-)
Oops. Thanks.
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to David TurnerDocumentation/git-index-helper.txt
Quoted 4 lines · David Turner
Documentation/git-index-helper.txt new file +4
-0+NOTES +----- + +$GIT_DIR/index-helper.path is a symlink
In multiple worktree context, this file will be per-worktree. So we have one daemon per worktree. I think that's fine.
Review
Quoted 3 lines
to a directory in $TMPDIR
(quoted diff) +2
-0+containing a Unix domain socket called 's' that the daemon reads +commands from.
Oops. I stand corrected, now it's one daemon per repository... Probably good to hide the socket path in $GIT_DIR though, people may protect it with dir permission of one of ancestor directories.
Documentation/git-index-helper.txt
Quoted 6 lines
The directory will also contain files named +"git-index-<SHA1>". These are used as backing stores for shared
Documentation/git-index-helper.txt new file +4
-0+memory. Normally the daemon will clean up these files when it exits +or when they are no longer relevant. But if it crashes, some objects +could remain there and they can be safely deleted with "rm" +command.
Alternatively, we could store all these in $GIT_DIR/helper or something and clean up automatically when index-helper starts. But I guess at least with TMPDIR we have a chance to put them on tmpfs.
Quoted 15 lines
git-compat-util.h +7
-0@@ -1045,4 +1046,21 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);#define getc_unlocked (fh) getc(fh)#endif +#ifdef __linux__ +#define UNIX_PATH_MAX 108 +#elif defined(__APPLE__) || defined(BSD ) +#define UNIX_PATH_MAX 104 +#else +/* +* Quoth POSIX: The size of sun_path has intentionally been left
git-compat-util.h struct tm *git_gmtime_r(const time_t *, struct tm *); · line 1065
Quoted 10 lines
git-compat-util.h +10
-0+* undefined. This is because different implementations use different +* sizes. For example, 4.3 BSD uses a size of 108, and 4.4 BSD uses a +* size of 104. Since most implementations originate from BSD +* versions, the size is typically in the range 92 to 108. +* +* Thanks, POSIX! Super-helpful! Hope we don't overflow any buffers! +*/ +#define UNIX_PATH_MAX 92 +#endif +
This looks like dead code (or at least not used in this patch).
Quoted 8 lines
git-compat-util.h +8
-0+static int poke_daemon (struct index_state *istate, +const struct stat *st,int refresh_cache) +{ +int fd; +int ret =0 ; +const char *socket_path; + +/* if this is from index-helper, do not poke itself (recursively) */
git-compat-util.h struct tm *git_gmtime_r(const time_t *, struct tm *);
Quoted 10 lines
git-compat-util.h +10
-0+if (istate->to_shm) +return 0 ; + + socket_path =index_helper_path ("s" ); +if (!socket_path) +return -1 ; + + fd =unix_stream_connect (socket_path); +if (refresh_cache) { + ret =write_in_full (fd,"refresh" ,8 ) !=8 ;
Since we've moved to unix socket and had bidirectional communication, it's probably a good idea to read an "ok" back, giving index-helper time to prepare the cache. As I recall the last discussion with Johannes, missing a cache here when the index is around 300MB could hurt more than wait patiently once and have it ready next time.
Quoted 7 lines
git-compat-util.h +7
-0+ }else { + ret =write_in_full (fd,"poke" ,5 ) !=5 ; + } + +close (fd); +return ret; +}
Signature · 1 line
Duy
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
Quoted 10 lines · Duy Nguyen
Documentation/git-index-helper.txt
Quoted 4 lines · David Turner
Documentation/git-index-helper.txt new file +4
-0+NOTES +----- + +$GIT_DIR/index-helper.path is a symlinkIn multiple worktree context, this file will be per-worktree. So we have one daemon per worktree. I think that's fine.
Quoted 7 lines
Review
Quoted 3 lines
to a directory in $TMPDIR
(quoted diff) +2
-0+containing a Unix domain socket called 's' that the daemon reads +commands from.Oops. I stand corrected, now it's one daemon per repository... Probably good to hide the socket path in $GIT_DIR though, people may protect it with dir permission of one of ancestor directories.
I'm not sure I understand what you're saying here. It should be one daemon per worktree, I think. And as far as I know, it is.
Socket paths must be short (less than 104 chars on Mac). That's why I do the weird symlink-to-tmpdir thing.
Quoted 12 lines
Review
Quoted 8 lines
The directory will also contain files named +"git-index-<SHA1>". These are used as backing stores for shared +memory. Normally the daemon will clean up these files when it exits
+or when they are no longer relevant. But if it crashes, some objects(quoted diff) +2
-0+could remain there and they can be safely deleted with "rm" +command.Alternatively, we could store all these in $GIT_DIR/helper or something and clean up automatically when index-helper starts. But I guess at least with TMPDIR we have a chance to put them on tmpfs.
Quoted 5 lines
git-compat-util.h struct tm *git_gmtime_r(const time_t *, struct tm *); · line 1065
Quoted 3 lines
git-compat-util.h +3
-0+#define UNIX_PATH_MAX 92 +#endif +This looks like dead code (or at least not used in this patch).
Yep, thanks.
Quoted 9 lines
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1616
Quoted 3 lines
read-cache.c +3
-0+ fd =unix_stream_connect (socket_path); +if (refresh_cache) { + ret =write_in_full (fd,"refresh" ,8 ) !=8 ;Since we've moved to unix socket and had bidirectional communication, it's probably a good idea to read an "ok" back, giving index-helper time to prepare the cache. As I recall the last discussion with Johannes, missing a cache here when the index is around 300MB could hurt more than wait patiently once and have it ready next time.
It is somewhat slower to wait for the daemon (which requires a disk load + a memcpy) than it is to just load it ourselves (which is just a disk load).
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
Quoted 19 lines · David Turner
Quoted 10 lines · Duy Nguyen
Documentation/git-index-helper.txt
Quoted 4 lines · David Turner
Documentation/git-index-helper.txt new file +4
-0+NOTES +----- + +$GIT_DIR/index-helper.path is a symlinkIn multiple worktree context, this file will be per-worktree. So we have one daemon per worktree. I think that's fine.
Quoted 7 lines
Review
Quoted 3 lines
to a directory in $TMPDIR
(quoted diff) +2
-0+containing a Unix domain socket called 's' that the daemon reads +commands from.Oops. I stand corrected, now it's one daemon per repository... Probably good to hide the socket path in $GIT_DIR though, people may protect it with dir permission of one of ancestor directories.
Quoted 2 lines
I'm not sure I understand what you're saying here. It should be one daemon per worktree, I think. And as far as I know, it is.
No you're right, it's still per worktree. I assumed $GIT_DIR/index-helper.path points to the same $TMPDIR, but it's not.
Quoted 2 lines
Socket paths must be short (less than 104 chars on Mac). That's why I do the weird symlink-to-tmpdir thing.
Is relative path in sun_path portable? We could just chdir() there, open the socket and chdir() back. Though if the current solution's already good enough, I don't think we need to change this again.
Hmm.. googling a bit pointed me back to Jeff's patch that does exactly that. The commit is 1eb10f4 (unix-socket: handle long socket pathnames
- 2012-01-09). It does not mention Mac though, neither does the related discussion on mailing list..
Signature · 1 line
Duy
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to David TurnerQuoted 10 lines · David Turner
Quoted 9 lines
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1616
Quoted 3 lines
read-cache.c +3
-0+ fd =unix_stream_connect (socket_path); +if (refresh_cache) { + ret =write_in_full (fd,"refresh" ,8 ) !=8 ;Since we've moved to unix socket and had bidirectional communication, it's probably a good idea to read an "ok" back, giving index-helper time to prepare the cache. As I recall the last discussion with Johannes, missing a cache here when the index is around 300MB could hurt more than wait patiently once and have it ready next time.
Quoted 3 lines
It is somewhat slower to wait for the daemon (which requires a disk load + a memcpy) than it is to just load it ourselves (which is just a disk load).
You forgot the most costly part, SHA-1 verification. For very large index, I assume the index-helper is already in the middle of hashing the index content. If you ignore index-helper, you need to go hash the whole thing again. The index-helper can hand it to you if you wait just a bit more. This wait time should be shorter because index-helper is already in the middle of hashing (and in optimistic case, very close to finishing it).
Signature · 1 line
Duy
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to David TurnerQuoted 2 lines · David Turner
$GIT_DIR/index-helper.path is a symlink to the socket for the daemon process. The daemon reads from the socket and executes commands.
We generally avoid using symbolic links inside $GIT_DIR (we used to represent the current branch by a symbolic link HEAD pointing at the underlying ref, but switched to the textual symref long time ago, and we did the ".git pointing at the real repository location" to support submodules also in a similar way).
I wonder if we can change this to a file that records the location of the socket without bending over backwards too much?
Quoted 11 lines
Named pipes were considered for portability reasons, but then commands that need replies from the daemon would have open their own pipes, since a named pipe should only have one reader. Unix domain sockets don't have this problem.
On webkit.git with index format v2, duplicating 8 times to 1.4m entries and 200MB in size:
(vanilla) 0.986986364 s:
read_index_from.git/index
(index-helper) 0.267850279 s:read_index_from.git/index
Quoted 8 lines
Interestingly with index v4, we get less out of index-helper. It makes sense as v4 requires more processing after loading the index:
(vanilla) 0.722496666 s:
read_index_from.git/index
(index-helper) 0.302741500 s:read_index_from.git/index(these benchmarks are from an earlier version of this patch, but should still be valid).
Just something to keep in mind; we'd need to see updated numbers when the series matures.
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
Quoted 35 lines · Junio C Hamano
Quoted 3 lines · David Turner
$GIT_DIR/index-helper.path is a symlink to the socket for the daemon
process. The daemon reads from the socket and executes commands.We generally avoid using symbolic links inside $GIT_DIR (we used to represent the current branch by a symbolic link HEAD pointing at the underlying ref, but switched to the textual symref long time ago, and we did the ".git pointing at the real repository location" to support submodules also in a similar way).
I wonder if we can change this to a file that records the location of the socket without bending over backwards too much?
Quoted 20 lines
Named pipes were considered for portability reasons, but then commands
that need replies from the daemon would have open their own pipes, since a named pipe should only have one reader. Unix domain sockets
don't have this problem.On webkit.git with index format v2, duplicating 8 times to 1.4m entries and 200MB in size:
(vanilla) 0.986986364 s:
read_index_from.git/index
(index-helper) 0.267850279 s:read_index_from.git/indexInterestingly with index v4, we get less out of index-helper. It makes
sense as v4 requires more processing after loading the index:(vanilla) 0.722496666 s:
read_index_from.git/index
(index-helper) 0.302741500 s:read_index_from.git/index
Quoted 6 lines
Quoted 3 lines
(these benchmarks are from an earlier version of this patch, but should
still be valid).Just something to keep in mind; we'd need to see updated numbers when the series matures.
I don't actually know how Duy did the index duplication for this benchmark. Duy?
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
Quoted 17 lines · David Turner
Quoted 17 lines
Quoted 16 lines
On webkit.git with index format v2, duplicating 8 times to 1.4m entries and 200MB in size:
(vanilla) 0.986986364 s:
read_index_from.git/index
(index-helper) 0.267850279 s:read_index_from.git/indexInterestingly with index v4, we get less out of index-helper. It makes
sense as v4 requires more processing after loading the index:(vanilla) 0.722496666 s:
read_index_from.git/index
(index-helper) 0.302741500 s:read_index_from.git/index(these benchmarks are from an earlier version of this patch, but should
still be valid).
Quoted 5 lines
Quoted 2 lines
Just something to keep in mind; we'd need to see updated numbers when the series matures.
I don't actually know how Duy did the index duplication for this benchmark. Duy?
Start with webkit.git (which was one of the biggest worktree i found), then put the entire directory structure in a subdirectory e.g.
git ls-files --stage | sed 's/\t/\tsubdir1\//' | git update-index --add --index-info --stdin
repeat until you get to a reasonable size.
Signature · 1 line
Duy
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to Duy NguyenQuoted 39 lines · Duy Nguyen
Quoted 22 lines · David Turner
Quoted 11 lines · Duy Nguyen
Documentation/git-index-helper.txt
Quoted 4 lines · David Turner
Documentation/git-index-helper.txt new file +4
-0+NOTES +----- + +$GIT_DIR/index-helper.path is a symlinkIn multiple worktree context, this file will be per-worktree. So we
have one daemon per worktree. I think that's fine.Quoted 9 lines
Quoted 4 lines
to a directory in $TMPDIR
+containing a Unix domain socket called 's' that the daemon reads
+commands from.Oops. I stand corrected, now it's one daemon per repository... Probably good to hide the socket path in $GIT_DIR though, people may
protect it with dir permission of one of ancestor directories.Quoted 3 lines
I'm not sure I understand what you're saying here. It should be one
daemon per worktree, I think. And as far as I know, it is.No you're right, it's still per worktree. I assumed $GIT_DIR/index-helper.path points to the same $TMPDIR, but it's not.
Quoted 3 lines
Socket paths must be short (less than 104 chars on Mac). That's why I
do the weird symlink-to-tmpdir thing.Is relative path in
sun_pathportable? We could justchdir()there, open the socket andchdir()back. Though if the current solution's already good enough, I don't think we need to change this again.
Quoted 6 lines
Hmm.. googling a bit pointed me back to Jeff's patch that does exactly
that. The commit is 1eb10f4 (unix-socket: handle long socket pathnames
- 2012-01-09). It does not mention Mac though, neither does the related discussion on mailing list..
In that case, I guess we can put the socket in $GITDIR and save the annoyance of the temp dir. Seems legit to me.
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to Duy NguyenQuoted 20 lines · Duy Nguyen
Quoted 13 lines · David Turner
Quoted 4 lines
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1616
Quoted 3 lines
read-cache.c +3
-0+ fd =unix_stream_connect (socket_path); +if (refresh_cache) { + ret =write_in_full (fd,"refresh" ,8 ) !=8 ;Quoted 8 lines
Since we've moved to unix socket and had bidirectional communication,
it's probably a good idea to read an "ok" back, giving index -helper
time to prepare the cache. As I recall the last discussion with Johannes, missing a cache here when the index is around 300MB could
hurt more than wait patiently once and have it ready next time.Quoted 4 lines
It is somewhat slower to wait for the daemon (which requires a disk load + a memcpy) than it is to just load it ourselves (which is just a
disk load).
Quoted 9 lines
You forgot the most costly part, SHA-1 verification. For very large index, I assume the index-helper is already in the middle of hashing the index content. If you ignore index-helper, you need to go hash the
whole thing again. The index-helper can hand it to you if you wait just a bit more. This wait time should be shorter because index -helper
is already in the middle of hashing (and in optimistic case, very close to finishing it).
You're right -- I did forget that part.
In "index-helper: use watchman to avoid refreshing index with lstat()", we switch from just poking to poking and waiting for a reply. Then in "read-cache: config for waiting for index-helper", we make that waiting optional. So what if I just remove that patch? Does that solve it?
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
Quoted 31 lines · David Turner
Quoted 20 lines · Duy Nguyen
Quoted 13 lines · David Turner
Quoted 4 lines
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1616
Quoted 3 lines
read-cache.c +3
-0+ fd =unix_stream_connect (socket_path); +if (refresh_cache) { + ret =write_in_full (fd,"refresh" ,8 ) !=8 ;Quoted 8 lines
Since we've moved to unix socket and had bidirectional communication,
it's probably a good idea to read an "ok" back, giving index -helper
time to prepare the cache. As I recall the last discussion with Johannes, missing a cache here when the index is around 300MB could
hurt more than wait patiently once and have it ready next time.Quoted 4 lines
It is somewhat slower to wait for the daemon (which requires a disk load + a memcpy) than it is to just load it ourselves (which is just a
disk load).Quoted 9 lines
You forgot the most costly part, SHA-1 verification. For very large index, I assume the index-helper is already in the middle of hashing the index content. If you ignore index-helper, you need to go hash the
whole thing again. The index-helper can hand it to you if you wait just a bit more. This wait time should be shorter because index -helper
is already in the middle of hashing (and in optimistic case, very close to finishing it).
Quoted 7 lines
You're right -- I did forget that part.
In "index-helper: use watchman to avoid refreshing index with
lstat()", we switch from just poking to poking and waiting for a reply. Then in "read-cache: config for waiting for index-helper", we make that waiting optional. So what if I just remove that patch? Does that solve it?
Yes. I think so.
Signature · 1 line
Duy
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to David TurnerQuoted 23 lines
read-cache.c +23
-0+static int try_shm (struct index_state *istate) +{ +void *new_mmap =NULL ; +size_t old_size = istate->mmap_size; +ssize_t new_size; +const unsigned char *sha1; +struct stat st; +int fd; + +if (!is_main_index (istate) || + old_size <=20 || +stat (git_path ("index-helper.path" ), &st)) +return -1 ; +if (poke_daemon (istate, &st,0 )) +return -1 ; + sha1 = (unsigned char *)istate->mmap + old_size -20 ; + + fd =open (index_helper_path ("git-index-%s" ,sha1_to_hex (sha1)), +O_RDONLY ); +if (fd <0 ) + goto fail; + +if (fstat (fd, &st))
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1669
Quoted 10 lines
read-cache.c +10
-0+ goto fail; + + new_size = st.st_size; + new_mmap =mmap (NULL , new_size,PROT_READ ,MAP_SHARED , fd,0 ); +if (new_size <=20 || +hashcmp ((unsigned char *)istate->mmap + old_size -20 , + (unsigned char *)new_mmap + new_size -20 )) { +if (new_mmap) +munmap (new_mmap, new_size); + goto fail;
coming from here
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1681
Quoted 12 lines
read-cache.c +12
-0+ } + +/* The memory barrier here matches index-helper.c:share_index. */ +__sync_synchronize (); + +munmap (istate->mmap, istate->mmap_size); + istate->mmap = new_mmap; + istate->mmap_size = new_size; + istate->from_shm =1 ; +return 0 ; + +fail:
fd may be leaking here?
Quoted 4 lines
read-cache.c +4
-0+poke_daemon (istate, &st,1 ); +return -1 ; +} +
Re: [PATCH v4 09/16] index-helper: use watchman to avoid refreshing index with lstat()
In reply to David TurnerQuoted 56 lines
read-cache.c +56
-0+static void refresh_by_watchman (struct index_state *istate) +{ +void *shm =NULL ; +int length; +int i; +struct stat st; +int fd =-1 ; +const char *path =index_helper_path ("git-watchman-%s-%" PRIuMAX, +sha1_to_hex (istate->sha1), + (uintmax_t )getpid ()); + + fd =open (path,O_RDONLY ); +if (fd <0 ) +return ; + +/* +* This watchman data is just for us -- no need to keep it +* around once we've got it open. +*/ +unlink (path); + +if (fstat (fd, &st) <0 ) + goto done; + + length = st.st_size; + shm =mmap (NULL , length,PROT_READ ,MAP_SHARED , fd,0 ); + +if (shm ==MAP_FAILED ) + goto done; + +close (fd); + fd =-1 ; + +if (length <=20 || +hashcmp (istate->sha1, (unsigned char *)shm + length -20 ) || +/* +* No need to clear CE_WATCHMAN_DIRTY set by 'WAMA' on +* disk. Watchman can only set more, not clear any, so +* this is OR mask. +*/ +read_watchman_ext (istate, shm, length -20 )) + goto done; + +/* +* Now that we've marked the invalid entries in the +* untracked-cache itself, we can erase them from the list of +* entries to be processed and mark the untracked cache for +* watchman usage. +*/ +if (istate->untracked) { +string_list_clear (&istate->untracked->invalid_untracked,0 ); + istate->untracked->use_watchman =1 ; + } + +for (i =0 ; i < istate->cache_nr; i++) { +struct cache_entry *ce = istate->cache[i];
read-cache.c fail: · line 1982
Quoted 10 lines
read-cache.c +10
-0+if (ce_stage (ce) || (ce->ce_flags &CE_WATCHMAN_DIRTY )) +continue ; +ce_mark_uptodate (ce); + } +done: +if (shm) +munmap (shm, length); + +if (fd >0 ) +close (fd);
coverities opinion:
Quoted 2 lines
off_by_one: Testing whether handle fd is strictly greater than zero is suspicious. fd leaks when it is zero. Did you intend to include equality with zero?
We can assert that fd is never 0, because that is where stdin is?
Quoted 2 lines
(quoted diff) +2
-0+} +
Re: [PATCH v4 12/16] index-helper: kill mode
In reply to David TurnerQuoted 4 lines · David Turner
Add a new command (and command-line arg) to allow index-helpers to exit cleanly.
This is mainly useful for tests.
Both --kill and --autorun are missing documentation in Documentation/git-index-helper.txt.
Re: [PATCH v4 08/16] Add watchman support to reduce index refresh cost
In reply to David TurnerQuoted 38 lines · David Turner
watchman-support.c new file +38
-0+static void update_index (struct index_state *istate, +struct watchman_query_result *result) +{ +int i; + +if (result->is_fresh_instance) { +/* let refresh clear them later */ +for (i =0 ; i < istate->cache_nr; i++) + istate->cache[i]->ce_flags |=CE_WATCHMAN_DIRTY ; + goto done; + } + +for (i =0 ; i < result->nr; i++) { +struct watchman_stat *wm = result->stats + i; +int pos; + +if (S_ISDIR (wm->mode) || + !strncmp (wm->name,".git/" ,5 ) || +strstr (wm->name,"/.git/" )) +continue ; + + pos =index_name_pos (istate, wm->name,strlen (wm->name)); +if (pos <0 ) { +if (istate->untracked) { +char *name =xstrdup (wm->name); +char *dname =dirname (name); + +/* +* dirname() returns '.' for the root, +* but we call it ''. +*/ +if (dname[0 ] =='.' && dname[1 ] ==0 ) +string_list_append (&istate->untracked->invalid_untracked,"" ); +else +string_list_append (&istate->untracked->invalid_untracked, + dname); +free (name); + }
watchman-support.c line 90
Quoted 10 lines
watchman-support.c new file +10
-0+continue ; + } +/* FIXME: ignore staged entries and gitlinks too? */ + + istate->cache[pos]->ce_flags |=CE_WATCHMAN_DIRTY ; + } + +done: +free (istate->last_update); + istate->last_update =xstrdup (result->clock);
I made a note in private that, because we update this field every time, the index must be rewritten every time with new WAMA extension, which results in worse performance when the index file is big and update cost is really high (SHA-1 hashing again).
What I did not notice was, if we detect no changes since the last clock here (result->nr == 0 _or_ no new entries marked DIRTY), we could skip this clock update here. That results in no changes in index (on read-cache side), so no index update, no hashing cost.
Signature · 1 line
Duy
Re: [PATCH v4 15/16] index-helper: optionally automatically run
In reply to David Turnergit.c static void handle_builtin(int argc, const char **argv) · line 574
Quoted 10 lines · David Turner
git.c +3
-1@@ -536,8 +567,10 @@ static void handle_builtin(int argc, const char **argv) } builtin =get_builtin (cmd); -if (builtin) +if (builtin) { +maybe_run_index_helper (builtin);exit (run_builtin (builtin, argc, argv)); + } }
Isn't it too early to start index-helper here? Unrelated commands like git-log are affected. And config handling this early could be tricky. A better place may be in the socket connection code. When we fail to connect, we can check config key and run index-helper then.
Signature · 1 line
Duy
Re: [PATCH v4 09/16] index-helper: use watchman to avoid refreshing index with lstat()
In reply to Stefan BellerQuoted 74 lines · Stefan Beller
Quoted 63 lines
read-cache.c +35
-2+static void refresh_by_watchman(struct index_state *istate) +{ + void *shm = NULL; + int length; + int i; + struct stat st; + int fd = -1; + const char *path = index_helper_path("git-watchman-%s -%"PRIuMAX, + sha1_to_hex(istate ->sha1), + (uintmax_t)getpid()); + + fd = open(path, O_RDONLY); + if (fd < 0) + return; + + /* + * This watchman data is just for us -- no need to keep it + * around once we've got it open. + */ + unlink(path); + + if (fstat(fd, &st) < 0) + goto done; + + length = st.st_size; + shm = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0); + + if (shm == MAP_FAILED) + goto done; + + close(fd); + fd = -1; + + if (length <= 20 || + hashcmp(istate->sha1, (unsigned char *)shm + length -20) ||
read-cache.c +11
-0+/* +* No need to clear CE_WATCHMAN_DIRTY set by 'WAMA' on +* disk. Watchman can only set more, not clear any, so +* this is OR mask. +*/ + read_watchman_ext(istate, shm, length - 20)) + goto done; + +/* +* Now that we've marked the invalid entries in the +* untracked-cache itself, we can erase them from the list of
read-cache.c +11
-1+* entries to be processed and mark the untracked cache for +* watchman usage. +*/ +if (istate->untracked) { +string_list_clear (&istate->untracked ->invalid_untracked, 0); + istate->untracked->use_watchman = 1; + } + +for (i =0 ; i < istate->cache_nr; i++) { +struct cache_entry *ce = istate->cache[i]; + if (ce_stage(ce) || (ce->ce_flags &
CE_WATCHMAN_DIRTY))read-cache.c fail: · line 1982
Quoted 9 lines
read-cache.c +9
-0+continue ; +ce_mark_uptodate (ce); + } +done: +if (shm) +munmap (shm, length); + +if (fd >0 ) +close (fd);coverities opinion:
Quoted 6 lines
Quoted 4 lines
off_by_one: Testing whether handle fd is strictly greater than zero is suspicious.
fd leaks when it is zero. Did you intend to include equality with zero?We can assert that fd is never 0, because that is where stdin is?
You are right: it should be if (fd >= 0). Will fix. Thanks.
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to Stefan BellerQuoted 54 lines · Stefan Beller
Quoted 29 lines
read-cache.c +18
-0+static int try_shm (struct index_state *istate) +{ +void *new_mmap =NULL ; +size_t old_size = istate->mmap_size; +ssize_t new_size; +const unsigned char *sha1; +struct stat st; +int fd; + +if (!is_main_index (istate) || + old_size <=20 || +stat (git_path ("index-helper.path" ), &st)) +return -1 ; +if (poke_daemon (istate, &st,0 )) +return -1 ; + sha1 = (unsigned char *)istate->mmap + old_size -20 ; + + fd = open(index_helper_path("git-index-%s",
sha1_to_hex(sha1)),read-cache.c +9
-0+ O_RDONLY); +if (fd <0 ) + goto fail; + +if (fstat (fd, &st)) + goto fail; + + new_size = st.st_size; + new_mmap = mmap(NULL, new_size, PROT_READ, MAP_SHARED, fd,0);
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1669
Quoted 6 lines
read-cache.c +6
-0+if (new_size <=20 || +hashcmp ((unsigned char *)istate->mmap + old_size -20 , + (unsigned char *)new_mmap + new_size -20 )) { +if (new_mmap) +munmap (new_mmap, new_size); + goto fail;coming from here
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1681
Quoted 13 lines
read-cache.c +12
-1+ } + +/* The memory barrier here matches index -helper.c:share_index. */ +__sync_synchronize(); + +munmap(istate->mmap, istate->mmap_size); +istate->mmap = new_mmap; +istate->mmap_size = new_size; +istate->from_shm = 1; +return 0; + +fail: fd may be leaking here?
Quoted 4 lines
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1685
Quoted 4 lines
read-cache.c +4
-0+poke_daemon (istate, &st,1 ); +return -1 ; +} +
Good point. (It's also leaking on the happy path -- will fix all of those)
Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff
In reply to Stefan BellerQuoted 54 lines · Stefan Beller
Quoted 29 lines
read-cache.c +18
-0+static int try_shm (struct index_state *istate) +{ +void *new_mmap =NULL ; +size_t old_size = istate->mmap_size; +ssize_t new_size; +const unsigned char *sha1; +struct stat st; +int fd; + +if (!is_main_index (istate) || + old_size <=20 || +stat (git_path ("index-helper.path" ), &st)) +return -1 ; +if (poke_daemon (istate, &st,0 )) +return -1 ; + sha1 = (unsigned char *)istate->mmap + old_size -20 ; + + fd = open(index_helper_path("git-index-%s",
sha1_to_hex(sha1)),read-cache.c +9
-0+ O_RDONLY); +if (fd <0 ) + goto fail; + +if (fstat (fd, &st)) + goto fail; + + new_size = st.st_size; + new_mmap = mmap(NULL, new_size, PROT_READ, MAP_SHARED, fd,0);
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1669
Quoted 6 lines
read-cache.c +6
-0+if (new_size <=20 || +hashcmp ((unsigned char *)istate->mmap + old_size -20 , + (unsigned char *)new_mmap + new_size -20 )) { +if (new_mmap) +munmap (new_mmap, new_size); + goto fail;coming from here
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1681
Quoted 13 lines
read-cache.c +12
-1+ } + +/* The memory barrier here matches index -helper.c:share_index. */ +__sync_synchronize(); + +munmap(istate->mmap, istate->mmap_size); +istate->mmap = new_mmap; +istate->mmap_size = new_size; +istate->from_shm = 1; +return 0; + +fail: fd may be leaking here?
Quoted 4 lines
read-cache.c static void post_read_index_from(struct index_state *istate) · line 1685
Quoted 4 lines
read-cache.c +4
-0+poke_daemon (istate, &st,1 ); +return -1 ; +} +
Good point. (It's also leaking on the happy path -- will fix all of those)
Re: [PATCH v4 12/16] index-helper: kill mode
In reply to Ævar Arnfjörð BjarmasonQuoted 9 lines · Ævar Arnfjörð Bjarmason
Quoted 4 lines · David Turner
Add a new command (and command-line arg) to allow index-helpers to exit cleanly.
This is mainly useful for tests.
Both --kill and --autorun are missing documentation in
Documentation/git-index-helper.txt.
Will fix, thanks.
Re: [PATCH v4 15/16] index-helper: optionally automatically run
In reply to Duy NguyenQuoted 6 lines · Duy Nguyen
Quoted 4 lines · David Turner
(quoted diff) +0
-0@@ -536,8 +567,10 @@ static void handle_builtin(int argc, const char **argv) }
Quoted 13 lines
git.c static void handle_builtin(int argc, const char **argv) · line 574
Quoted 7 lines
git.c +3
-1builtin =get_builtin (cmd); -if (builtin) +if (builtin) { +maybe_run_index_helper (builtin);exit (run_builtin (builtin, argc, argv)); + } }Isn't it too early to start index-helper here? Unrelated commands like
git-log are affected. And config handling this early could be tricky. A better place may be in the socket connection code. When we fail to connect, we can check config key and run index-helper then.
Will move.