Speed up cache loading time
Subsystems (2)
v2 cover Speed up cache loading time
Compared to v1 [1], this is like a new series
- git-read-cache--daemon is renamed to git-index-helper (easier to guess what it's for)
- simplified locking mechanism on shared memory
- UNIX signals are used for notification instead of UNIX sockets
- Windows support (only tested with wine)
I think I'm getting closer to something that can finally have a chance of merging. Still don't know how to write tests for this though.
[1] http://thread.gmane.org/gmane.comp.version-control.git/247268/focus=248760
Nguyễn Thái Ngọc Duy (5):
read-cache: allow to keep mmap'd memory after reading
index-helper: new daemon for caching index and related stuff
index-helper: add Windows support
daemonize(): set a flag before exiting the main process
index-helper: add --detach
.gitignore | 1 + Documentation/git-index-helper.txt (new) | 26 ++++ Makefile | 7 ++ builtin/gc.c | 2 +- cache.h | 6 +- config.mak.uname | 3 + daemon.c | 2 +- git-compat-util.h | 1 + index-helper.c (new) | 198 +++++++++++++++++++++++++++++++ read-cache.c | 97 +++++++++++++-- setup.c | 4 +- shm.c (new) | 163 +++++++++++++++++++++++++ shm.h (new) | 23 ++++ 13 files changed, 521 insertions(+), 12 deletions(-) create mode 100644 Documentation/git-index-helper.txt create mode 100644 index-helper.c create mode 100644 shm.c create mode 100644 shm.h
Signature · 1 line
1.9.1.346.ga2b5940
PATCH v2 1/5 read-cache: allow to keep mmap'd memory after reading
--- cache.h | 3 +++ read-cache.c | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
cache.h +3 -0
@@ -290,10 +290,13 @@ 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 ]; +void *mmap; +size_t mmap_size; };extern struct index_state the_index;
read-cache.c +12 -1
@@ -1495,6 +1495,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) mmap =xmmap (NULL , mmap_size,PROT_READ |PROT_WRITE ,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;
@@ -1547,10 +1551,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" ); }
@@ -1576,6 +1582,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 );
@@ -1618,6 +1625,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);return 0 ; }
Signature · 1 line
1.9.1.346.ga2b5940
PATCH v2 2/5 index-helper: new daemon for caching index and related stuff
In reply to Nguyễn Thái Ngọc DuyThe shared memory's name folows the template "git-<something>-<SHA1>" where <SHA1> is the trailing SHA-1 of the index file. <something> is "index" for caching index files. 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. Note that it does not necessarily use the same format as the on-disk version. The content could be in a format that can be parsed much faster, or even reused without parsing).
Git can poke the daemon to tell it to refresh the index cache, or to not exit for another some minutes via UNIX signals. It can't give any real data directly to the daemon. Real data goes to disk first, then the daemon reads and verifies it from there.
--- .gitignore | 1 + Documentation/git-index-helper.txt (new) | 24 +++++ Makefile | 7 ++ cache.h | 1 + config.mak.uname | 1 + git-compat-util.h | 1 + index-helper.c (new) | 145 +++++++++++++++++++++++++++++++ read-cache.c | 78 +++++++++++++++-- shm.c (new) | 67 ++++++++++++++ shm.h (new) | 23 +++++ 10 files changed, 341 insertions(+), 7 deletions(-) create mode 100644 Documentation/git-index-helper.txt create mode 100644 index-helper.c create mode 100644 shm.c create mode 100644 shm.h
.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 +24 -0
@@ -0,0 +1,24 @@ +git-index-helper(1) +============= + +NAME +---- +git-index-helper - A simple cache server for speeding up index file access + +SYNOPSIS +-------- +[verse] +'git index-helper + +DESCRIPTION +----------- +Keep the index file in memory for faster access. This daemon is per +repository. This daemon is only available on POSIX system with +shared memory support (e.g. Linux) + +OPTIONS +------- + +GIT +--- +Part of the linkgit:git[1] suite
Makefile +7 -0
@@ -886,6 +886,7 @@ LIB_OBJS += sha1-lookup.o LIB_OBJS += sha1_file.o LIB_OBJS += sha1_name.o LIB_OBJS += shallow.o +LIB_OBJS += shm.o LIB_OBJS += sideband.o LIB_OBJS += sigchain.o LIB_OBJS += split-index.o
@@ -1498,6 +1499,12 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifdef HAVE_SHM + BASIC_CFLAGS += -DHAVE_SHM + EXTLIBS += -lrt + PROGRAM_OBJS += index-helper.o +endif + ifdef DIR_HAS_BSD_GROUP_SEMANTICS COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS endif
cache.h +1 -0
@@ -483,6 +483,7 @@ extern int is_index_unborn(struct index_state *);extern int read_index_unmerged (struct index_state *);#define COMMIT_LOCK (1 << 0)#define CLOSE_LOCK (1 << 1) +#define REFRESH_DAEMON (1 << 2)extern int write_locked_index (struct index_state *,struct lock_file *lock,unsigned flags);extern int discard_index (struct index_state *);extern int unmerged_index (const struct index_state *);
config.mak.uname +1 -0
@@ -39,6 +39,7 @@ ifeq ($(uname_S),Linux) HAVE_PATHS_H = YesPlease LIBC_CONTAINS_LIBINTL = YesPlease HAVE_DEV_TTY = YesPlease + HAVE_SHM = YesPlease endif ifeq ($(uname_S),GNU/kFreeBSD) NO_STRLCPY = YesPlease
git-compat-util.h +1 -0
@@ -356,6 +356,7 @@ static inline const char *skip_prefix(const char *str, const char *prefix)#define PROT_READ 1#define PROT_WRITE 2#define MAP_PRIVATE 1 +#define MAP_SHARED 2#endif #define mmap git_mmap
index-helper.c new file +145 -0
145 lines not on this page · raw message
read-cache.c +71 -7
115 lines not on this page · raw message
shm.c new file +67 -0
67 lines not on this page · raw message
shm.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef SHM_H +#define SHM_H + +/* +* Create or open a shared memory and mmap it. Return mmap size if +* successful, -1 otherwise. If successful mmap contains the mmap'd +* pointer. If oflag does not contain O_CREAT and length is negative, +* the mmap size is retrieved from existing shared memory object. +* +* The mmap could be freed by munmap, even on Windows. Note that on +* Windows, git_shm_unlink() is no-op, so the last unmap will destroy +* the shared memory. +*/ +ssize_t git_shm_map (int oflag,int perm,ssize_t length,void **mmap, +int prot,int flags,const char *fmt, ...); + +/* +* Unlink a shared memory object. Only needed on POSIX platforms. On +* Windows this is no-op. +*/ +void git_shm_unlink (const char *fmt, ...); + +#endif
Signature · 1 line
1.9.1.346.ga2b5940
Re: [PATCH 2/5] index-helper: new daemon for caching index and related stuff
Quoted 1 line · Nguyễn Thái Ngọc Duy
index-helper.c new file +1
-0+loop (sb.buf,600 );
...
read-cache.c static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk, · line 1473
Quoted 1 line
read-cache.c +1
-0+if (st->st_mtime +600 <time (NULL ))
s/600/INDEX_HELPER_TIMEOUT/ or something.
read-cache.c static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk, · line 1476
Quoted 3 lines
read-cache.c +3
-0+return ;/* don't try to read from stale .pid file */ + + fd =open (git_path ("read-cache--daemon.pid" ),O_RDONLY );
should be index-helper.pid (actually, there are a few other instances of this as well); maybe make it a constant.
PATCH v2 3/5 index-helper: add Windows support
In reply to Nguyễn Thái Ngọc DuyWindows supports shared memory, but a bit different that POSIX shm. The most noticeable thing is there's no way to get the shared memory's size from the reader. So the size is added near the end in the shared memory, hidden away from shm users (storing it in headers would cause more problems with munmap, storing it as a separate shm is even worse).
PostMessage is used instead of UNIX signals for notification. Lightweight (at least code-wise) on the client side.
--- Documentation/git-index-helper.txt | 2 +- config.mak.uname | 2 + index-helper.c | 49 +++++++++++++++++++ read-cache.c | 6 +++ shm.c | 96 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-)
Documentation/git-index-helper.txt +1 -1
@@ -14,7 +14,7 @@ DESCRIPTION ----------- Keep the index file in memory for faster access. This daemon is per repository. This daemon is only available on POSIX system with -shared memory support (e.g. Linux) +shared memory support (e.g. Linux or Windows) OPTIONS -------
config.mak.uname +2 -0
@@ -376,6 +376,7 @@ ifndef DEBUG else BASIC_CFLAGS += -Zi -MTd endif + PROGRAM_OBJS += index-helper.o X = .exe endif ifeq ($(uname_S),Interix)
@@ -529,6 +530,7 @@ ifneq (,$(wildcard ../THIS_IS_MSYSGIT)) else NO_CURL = YesPlease endif + PROGRAM_OBJS += index-helper.o endif ifeq ($(uname_S),QNX) COMPAT_CFLAGS += -DSA_RESTART=0
index-helper.c +49 -0
61 lines not on this page · raw message
read-cache.c +6 -0
@@ -1483,6 +1483,12 @@ static void poke_daemon(struct stat *st, int refresh_cache)pid_t pid =strtoul (sb.buf, &end,10 );if (end && !*end)kill (pid, refresh_cache ?SIGHUP :SIGUSR1 ); +#elif defined(GIT_WINDOWS_NATIVE ) +if (starts_with (sb.buf,"HWND" )) { +HWND hwnd =FindWindow ("git-read-cache--daemon" , sb.buf); +PostMessage (hwnd, refresh_cache ?WM_USER :WM_USER +1 , +0 ,0 ); + }#endif }close (fd);
shm.c +96 -0
102 lines not on this page · raw message
Signature · 1 line
1.9.1.346.ga2b5940
PATCH v2 4/5 daemonize(): set a flag before exiting the main process
In reply to Nguyễn Thái Ngọc DuyThis 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
@@ -325,7 +325,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)* failure to daemonize is ok, we'll continue * in foreground */ -daemonize (); +daemonize (NULL ); }else add_repack_all_option ();
cache.h +1 -1
@@ -450,7 +450,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
@@ -1311,7 +1311,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
@@ -842,7 +842,7 @@ void sanitize_stdfds(void)close (fd); } -int daemonize (void ) +int daemonize (int *daemonized) {#ifdef NO_POSIX_GOODIES errno =ENOSYS ;
@@ -854,6 +854,8 @@ int daemonize(void)case -1 :die_errno ("fork failed" );default : +if (daemonized) + *daemonized =1 ;exit (0 ); }if (setsid () ==-1 )
Signature · 1 line
1.9.1.346.ga2b5940
PATCH v2 5/5 index-helper: add --detach
In reply to Nguyễn Thái Ngọc Duy--- Documentation/git-index-helper.txt | 4 +++- index-helper.c | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-)
Documentation/git-index-helper.txt +3 -1
@@ -8,7 +8,7 @@ git-index-helper - A simple cache server for speeding up index file access SYNOPSIS -------- [verse] -'git index-helper +'git index-helper [--detach] DESCRIPTION -----------
@@ -18,6 +18,8 @@ shared memory support (e.g. Linux or Windows) OPTIONS ------- +--detach:: + Detach from the shell. GIT ---
index-helper.c +5 -1
@@ -159,8 +159,9 @@ int main(int argc, char **argv)static struct lock_file lock;struct strbuf sb =STRBUF_INIT ;const char *prefix; -int fd; +int fd, detach =0 ;struct option options[] = { +OPT_BOOL (0 ,"detach" , &detach,"detach the process" ),OPT_END () };
@@ -188,6 +189,9 @@ int main(int argc, char **argv)sigchain_push (SIGQUIT , cleanup_on_signal);sigchain_push (SIGPIPE , cleanup_on_signal); +if (detach &&daemonize (&daemonized)) +die_errno ("unable to detach" ); +loop (sb.buf,600 );strbuf_release (&sb);return 0 ;
Signature · 1 line
1.9.1.346.ga2b5940