git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/5] Speed up cache loading time
@ 2014-06-10 13:24 Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 1/5] read-cache: allow to keep mmap'd memory after reading Nguyễn Thái Ngọc Duy
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2014-06-10 13:24 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

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

-- 
1.9.1.346.ga2b5940

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/5] read-cache: allow to keep mmap'd memory after reading
  2014-06-10 13:24 [PATCH v2 0/5] Speed up cache loading time Nguyễn Thái Ngọc Duy
@ 2014-06-10 13:24 ` Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 2/5] index-helper: new daemon for caching index and related stuff Nguyễn Thái Ngọc Duy
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2014-06-10 13:24 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 cache.h      |  3 +++
 read-cache.c | 13 ++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/cache.h b/cache.h
index c6b7770..6549e02 100644
--- a/cache.h
+++ b/cache.h
@@ -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;
diff --git a/read-cache.c b/read-cache.c
index 342fe52..a5031f3 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -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;
 }
-- 
1.9.1.346.ga2b5940

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/5] index-helper: new daemon for caching index and related stuff
  2014-06-10 13:24 [PATCH v2 0/5] Speed up cache loading time Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 1/5] read-cache: allow to keep mmap'd memory after reading Nguyễn Thái Ngọc Duy
@ 2014-06-10 13:24 ` Nguyễn Thái Ngọc Duy
  2014-06-10 18:44   ` David Turner
  2014-06-10 13:24 ` [PATCH 3/5] index-helper: add Windows support Nguyễn Thái Ngọc Duy
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 7+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2014-06-10 13:24 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

The 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.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 .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

diff --git a/.gitignore b/.gitignore
index 70992a4..5a829dd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -71,6 +71,7 @@
 /git-http-fetch
 /git-http-push
 /git-imap-send
+/git-index-helper
 /git-index-pack
 /git-init
 /git-init-db
diff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt
new file mode 100644
index 0000000..d0b1365
--- /dev/null
+++ b/Documentation/git-index-helper.txt
@@ -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
diff --git a/Makefile b/Makefile
index f7058a6..d42f3cc 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/cache.h b/cache.h
index 6549e02..f05e062 100644
--- a/cache.h
+++ b/cache.h
@@ -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 *);
diff --git a/config.mak.uname b/config.mak.uname
index eee0fc2..8de61a4 100644
--- a/config.mak.uname
+++ b/config.mak.uname
@@ -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
diff --git a/git-compat-util.h b/git-compat-util.h
index f6d3a46..a6ebecc 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -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
diff --git a/index-helper.c b/index-helper.c
new file mode 100644
index 0000000..e10d0c3
--- /dev/null
+++ b/index-helper.c
@@ -0,0 +1,145 @@
+#include "cache.h"
+#include "parse-options.h"
+#include "sigchain.h"
+#include "split-index.h"
+#include "shm.h"
+
+static unsigned char cached_sha1[20];
+static unsigned char cached_shared_sha1[20];
+static void *shm_index, *shm_sharedindex;
+static size_t size_index, size_sharedindex;
+static int daemonized;
+
+static void cleanup(void)
+{
+	if (daemonized)
+		return;
+	unlink(git_path("read-cache--daemon.pid"));
+	if (shm_index) {
+		munmap(shm_index, size_index);
+		git_shm_unlink("git-index-%s", sha1_to_hex(cached_sha1));
+	}
+	if (shm_sharedindex) {
+		munmap(shm_sharedindex, size_sharedindex);
+		git_shm_unlink("git-index-%s", sha1_to_hex(cached_shared_sha1));
+	}
+}
+
+static void cleanup_on_signal(int sig)
+{
+	cleanup();
+	sigchain_pop(sig);
+	raise(sig);
+}
+
+static void share_index(struct index_state *istate, void **mmap,
+			size_t *length, unsigned char *sha1)
+{
+	void *new_mmap;
+	if (istate->mmap_size <= 20 ||
+	    hashcmp(istate->sha1,
+		    (unsigned char *)istate->mmap + istate->mmap_size - 20) ||
+	    !hashcmp(istate->sha1, sha1) ||
+	    git_shm_map(O_CREAT | O_EXCL | O_RDWR, 0700, istate->mmap_size,
+			&new_mmap, PROT_READ | PROT_WRITE, MAP_SHARED,
+			"git-index-%s", sha1_to_hex(istate->sha1)) < 0)
+		return;
+
+	if (*mmap) {
+		munmap(*mmap, *length);
+		git_shm_unlink("git-index-%s", sha1_to_hex(sha1));
+	}
+
+	*length = istate->mmap_size;
+	*mmap = new_mmap;
+	hashcpy(sha1, istate->sha1);
+	memcpy(new_mmap, istate->mmap, istate->mmap_size - 20);
+
+	/*
+	 * The trailing hash must be written last after everything is
+	 * written. It's the indication that the shared memory is now
+	 * ready.
+	 */
+	hashcpy((unsigned char *)new_mmap + istate->mmap_size - 20, sha1);
+}
+
+static void refresh(int sig)
+{
+	the_index.keep_mmap = 1;
+	if (read_cache() < 0)
+		die("could not read index");
+	share_index(&the_index, &shm_index, &size_index, cached_sha1);
+	if (the_index.split_index && the_index.split_index->base)
+		share_index(the_index.split_index->base, &shm_sharedindex,
+			    &size_sharedindex, cached_shared_sha1);
+	discard_index(&the_index);
+}
+
+#ifdef HAVE_SHM
+
+static void do_nothing(int sig)
+{
+	/*
+	 * what we need is the signal received and interrupts
+	 * sleep(). We don't need to do anything else when receving
+	 * the signal
+	 */
+}
+
+static void loop(const char *pid_file, int idle_in_seconds)
+{
+	sigchain_push(SIGHUP, refresh);
+	sigchain_push(SIGUSR1, do_nothing);
+	refresh(0);
+	while (sleep(idle_in_seconds))
+		utime(git_path("read-cache--daemon.pid"), NULL);
+}
+
+#else
+
+static void loop(const char *pid_file, int idle_in_seconds)
+{
+}
+
+#endif
+
+static const char * const usage_text[] = {
+	"git read-cache--daemon [options]",
+	NULL
+};
+
+int main(int argc, char **argv)
+{
+	static struct lock_file lock;
+	struct strbuf sb = STRBUF_INIT;
+	const char *prefix;
+	int fd;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	if (argc == 2 && !strcmp(argv[1], "-h"))
+		usage_with_options(usage_text, options);
+	prefix = setup_git_directory();
+	if (parse_options(argc, (const char **)argv, prefix,
+			  options, usage_text, 0))
+		die("too many arguments");
+
+	fd = hold_lock_file_for_update(&lock,
+				       git_path("read-cache--daemon.pid"),
+				       LOCK_DIE_ON_ERROR);
+	strbuf_addf(&sb,
+		    "%" PRIuMAX, (uintmax_t) getpid());
+	write_in_full(fd, sb.buf, sb.len);
+	commit_lock_file(&lock);
+
+	atexit(cleanup);
+	sigchain_push(SIGINT,  cleanup_on_signal);
+	sigchain_push(SIGTERM, cleanup_on_signal);
+	sigchain_push(SIGQUIT, cleanup_on_signal);
+	sigchain_push(SIGPIPE, cleanup_on_signal);
+
+	loop(sb.buf, 600);
+	strbuf_release(&sb);
+	return 0;
+}
diff --git a/read-cache.c b/read-cache.c
index a5031f3..f9df984 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -16,6 +16,7 @@
 #include "varint.h"
 #include "split-index.h"
 #include "sigchain.h"
+#include "shm.h"
 
 static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
 					       unsigned int options);
@@ -1332,6 +1333,8 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
 	hdr_version = ntohl(hdr->hdr_version);
 	if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
 		return error("bad index version %d", hdr_version);
+	if (!size)
+		return 0;
 	git_SHA1_Init(&c);
 	git_SHA1_Update(&c, hdr, size - 20);
 	git_SHA1_Final(sha1, &c);
@@ -1462,6 +1465,58 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,
 	return ce;
 }
 
+static void poke_daemon(struct stat *st, int refresh_cache)
+{
+	int fd;
+	struct strbuf sb;
+
+	if (st->st_mtime + 600 < time(NULL))
+		return;	  /* don't try to read from stale .pid file */
+
+	fd = open(git_path("read-cache--daemon.pid"), O_RDONLY);
+	if (fd < 0)
+		return;
+	strbuf_init(&sb, st->st_size);
+	if (read_in_full(fd, sb.buf, st->st_size) == st->st_size) {
+#ifdef HAVE_SHM
+		char *end = NULL;
+		pid_t pid = strtoul(sb.buf, &end, 10);
+		if (end && !*end)
+			kill(pid, refresh_cache ? SIGHUP : SIGUSR1);
+#endif
+	}
+	close(fd);
+	strbuf_release(&sb);
+}
+
+static void *try_shm(struct index_state *istate, void *mmap, size_t *mmap_size)
+{
+	void *new_mmap = NULL;
+	size_t old_size = *mmap_size;
+	ssize_t new_length;
+	const unsigned char *sha1;
+	struct stat st;
+
+	if (old_size <= 20)
+		return mmap;
+	sha1 = (unsigned char *)mmap + old_size - 20;
+	if (stat(git_path("read-cache--daemon.pid"), &st))
+		return mmap;
+	new_length = git_shm_map(O_RDONLY, 0700, -1, &new_mmap,
+				 PROT_READ, MAP_SHARED,
+				 "git-index-%s", sha1_to_hex(sha1));
+	if (new_length <= 20 ||
+	    hashcmp((unsigned char *)mmap + old_size - 20,
+		    (unsigned char *)new_mmap + new_length - 20)) {
+		if (new_mmap)
+			munmap(new_mmap, new_length);
+		return mmap;
+	}
+	poke_daemon(&st, 0);
+	*mmap_size = new_length;
+	return new_mmap;
+}
+
 /* remember to discard_cache() before reading a different cache! */
 int do_read_index(struct index_state *istate, const char *path, int must_exist)
 {
@@ -1469,8 +1524,8 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
 	struct stat st;
 	unsigned long src_offset;
 	struct cache_header *hdr;
-	void *mmap;
-	size_t mmap_size;
+	void *mmap, *old_mmap;
+	size_t mmap_size, old_mmap_size;
 	struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
 
 	if (istate->initialized)
@@ -1501,9 +1556,14 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
 	}
 	close(fd);
 
-	hdr = mmap;
-	if (verify_hdr(hdr, mmap_size) < 0)
+	old_mmap      = mmap;
+	old_mmap_size = mmap_size;
+	mmap	      = try_shm(istate, mmap, &mmap_size);
+	hdr	      = mmap;
+	if (mmap == old_mmap && verify_hdr(hdr, mmap_size) < 0)
 		goto unmap;
+	if (mmap != old_mmap)
+		munmap(old_mmap, old_mmap_size);
 
 	hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
 	istate->version = ntohl(hdr->hdr_version);
@@ -2006,9 +2066,13 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
 		return ret;
 	assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
 	       (COMMIT_LOCK | CLOSE_LOCK));
-	if (flags & COMMIT_LOCK)
-		return commit_locked_index(lock);
-	else if (flags & CLOSE_LOCK)
+	if (flags & COMMIT_LOCK) {
+		struct stat st;
+		if (!commit_locked_index(lock) &&
+		    !stat(git_path("read-cache--daemon.pid"), &st))
+			poke_daemon(&st, 1);
+		return ret;
+	} else if (flags & CLOSE_LOCK)
 		return close_lock_file(lock);
 	else
 		return ret;
diff --git a/shm.c b/shm.c
new file mode 100644
index 0000000..4ec1a00
--- /dev/null
+++ b/shm.c
@@ -0,0 +1,67 @@
+#include "git-compat-util.h"
+#include "shm.h"
+
+#ifdef HAVE_SHM
+
+#define SHM_PATH_LEN 72		/* we don't create very long paths.. */
+
+ssize_t git_shm_map(int oflag, int perm, ssize_t length, void **mmap,
+		    int prot, int flags, const char *fmt, ...)
+{
+	va_list ap;
+	char path[SHM_PATH_LEN];
+	int fd;
+
+	path[0] = '/';
+	va_start(ap, fmt);
+	vsprintf(path + 1, fmt, ap);
+	va_end(ap);
+	fd = shm_open(path, oflag, perm);
+	if (fd < 0)
+		return -1;
+	if (length > 0 && ftruncate(fd, length)) {
+		shm_unlink(path);
+		close(fd);
+		return -1;
+	}
+	if (length < 0 && !(oflag & O_CREAT)) {
+		struct stat st;
+		if (fstat(fd, &st))
+			die_errno("unable to stat %s", path);
+		length = st.st_size;
+	}
+	*mmap = xmmap(NULL, length, prot, flags, fd, 0);
+	close(fd);
+	if (*mmap == MAP_FAILED) {
+		*mmap = NULL;
+		shm_unlink(path);
+		return -1;
+	}
+	return length;
+}
+
+void git_shm_unlink(const char *fmt, ...)
+{
+	va_list ap;
+	char path[SHM_PATH_LEN];
+
+	path[0] = '/';
+	va_start(ap, fmt);
+	vsprintf(path + 1, fmt, ap);
+	va_end(ap);
+	shm_unlink(path);
+}
+
+#else
+
+ssize_t git_shm_map(int oflag, int perm, ssize_t length, void **mmap,
+		    int prot, int flags, const char *fmt, ...)
+{
+	return -1;
+}
+
+void git_shm_unlink(const char *fmt, ...)
+{
+}
+
+#endif
diff --git a/shm.h b/shm.h
new file mode 100644
index 0000000..798d3fd
--- /dev/null
+++ b/shm.h
@@ -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
-- 
1.9.1.346.ga2b5940

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/5] index-helper: add Windows support
  2014-06-10 13:24 [PATCH v2 0/5] Speed up cache loading time Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 1/5] read-cache: allow to keep mmap'd memory after reading Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 2/5] index-helper: new daemon for caching index and related stuff Nguyễn Thái Ngọc Duy
@ 2014-06-10 13:24 ` Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 4/5] daemonize(): set a flag before exiting the main process Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 5/5] index-helper: add --detach Nguyễn Thái Ngọc Duy
  4 siblings, 0 replies; 7+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2014-06-10 13:24 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

Windows 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.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 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(-)

diff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt
index d0b1365..afeb334 100644
--- a/Documentation/git-index-helper.txt
+++ b/Documentation/git-index-helper.txt
@@ -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
 -------
diff --git a/config.mak.uname b/config.mak.uname
index 8de61a4..17d35e3 100644
--- a/config.mak.uname
+++ b/config.mak.uname
@@ -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
diff --git a/index-helper.c b/index-helper.c
index e10d0c3..f92326a 100644
--- a/index-helper.c
+++ b/index-helper.c
@@ -95,6 +95,52 @@ static void loop(const char *pid_file, int idle_in_seconds)
 		utime(git_path("read-cache--daemon.pid"), NULL);
 }
 
+#elif defined(GIT_WINDOWS_NATIVE)
+
+static void loop(const char *pid_file, int idle_in_seconds)
+{
+	HWND hwnd;
+	UINT_PTR timer = 0;
+	MSG msg;
+	HINSTANCE hinst = GetModuleHandle(NULL);
+	WNDCLASS wc;
+
+	/*
+	 * Emulate UNIX signals by sending WM_USER+x to a
+	 * window. Register window class and create a new window to
+	 * catch these messages.
+	 */
+	memset(&wc, 0, sizeof(wc));
+	wc.lpfnWndProc	 = DefWindowProc;
+	wc.hInstance	 = hinst;
+	wc.lpszClassName = "git-read-cache--daemon";
+	if (!RegisterClass(&wc))
+		die_errno("could not register new window class");
+
+	hwnd = CreateWindow("git-read-cache--daemon", pid_file,
+			    0, 0, 0, 1, 1, NULL, NULL, hinst, NULL);
+	if (!hwnd)
+		die_errno("could not register new window");
+
+	refresh(0);
+	while (1) {
+		timer = SetTimer(hwnd, timer, idle_in_seconds * 1000, NULL);
+		if (!timer)
+			die("no timer!");
+		if (!GetMessage(&msg, hwnd, 0, 0) || msg.message == WM_TIMER)
+			break;
+		switch (msg.message) {
+		case WM_USER:
+			refresh(0);
+			break;
+		default:
+			/* just reset the timer */
+			break;
+		}
+		utime(git_path("read-cache--daemon.pid"), NULL);
+	}
+}
+
 #else
 
 static void loop(const char *pid_file, int idle_in_seconds)
@@ -129,6 +175,9 @@ int main(int argc, char **argv)
 				       git_path("read-cache--daemon.pid"),
 				       LOCK_DIE_ON_ERROR);
 	strbuf_addf(&sb,
+#ifdef GIT_WINDOWS_NATIVE
+		    "HWND"
+#endif
 		    "%" PRIuMAX, (uintmax_t) getpid());
 	write_in_full(fd, sb.buf, sb.len);
 	commit_lock_file(&lock);
diff --git a/read-cache.c b/read-cache.c
index f9df984..8001c29 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -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);
diff --git a/shm.c b/shm.c
index 4ec1a00..04d8a35 100644
--- a/shm.c
+++ b/shm.c
@@ -52,6 +52,102 @@ void git_shm_unlink(const char *fmt, ...)
 	shm_unlink(path);
 }
 
+#elif defined(GIT_WINDOWS_NATIVE)
+
+#define SHM_PATH_LEN 82	/* a little bit longer than POSIX because of "Local\\" */
+
+static ssize_t create_shm_map(int oflag, int perm, ssize_t length,
+			      void **mmap, int prot, int flags,
+			      const char *path, unsigned long page_size)
+{
+	size_t real_length;
+	void *last_page;
+	HANDLE h;
+
+	assert(perm   == 0700);
+	assert(oflag  == (O_CREAT | O_EXCL | O_RDWR));
+	assert(prot   == (PROT_READ | PROT_WRITE));
+	assert(flags  == MAP_SHARED);
+	assert(length >= 0);
+
+	real_length = length;
+	if (real_length % page_size)
+		real_length += page_size - (real_length % page_size);
+	real_length += page_size;
+	h = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
+			      real_length, path);
+	if (!h)
+		return -1;
+	*mmap = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, real_length);
+	CloseHandle(h);
+	if (!*mmap)
+		return -1;
+	last_page = (unsigned char *)*mmap + real_length - page_size;
+	*(unsigned long *)last_page = length;
+	return length;
+}
+
+static ssize_t open_shm_map(int oflag, int perm, ssize_t length, void **mmap,
+			    int prot, int flags, const char *path,
+			    unsigned long page_size)
+{
+	void *last_page;
+	HANDLE h;
+
+	assert(perm   == 0700);
+	assert(oflag  == O_RDONLY);
+	assert(prot   == PROT_READ);
+	assert(flags  == MAP_SHARED);
+	assert(length <= 0);
+
+	h = OpenFileMapping(FILE_MAP_READ, FALSE, path);
+	if (!h)
+		return -1;
+	*mmap = MapViewOfFile(h, FILE_MAP_READ, 0, 0, 0);
+	CloseHandle(h);
+	if (!*mmap)
+		return -1;
+	if (length < 0) {
+		MEMORY_BASIC_INFORMATION mbi;
+		if (!VirtualQuery(*mmap, &mbi, sizeof(mbi))) {
+			UnmapViewOfFile(*mmap);
+			return -1;
+		}
+		if (mbi.RegionSize % page_size)
+			die("expected size %lu to be %lu aligned",
+				    mbi.RegionSize, page_size);
+		last_page = (unsigned char *)*mmap + mbi.RegionSize - page_size;
+		length = *(unsigned long *)last_page;
+	}
+	return length;
+}
+
+ssize_t git_shm_map(int oflag, int perm, ssize_t length, void **mmap,
+		    int prot, int flags, const char *fmt, ...)
+{
+	SYSTEM_INFO si;
+	va_list ap;
+	char path[SHM_PATH_LEN];
+
+	GetSystemInfo(&si);
+
+	strcpy(path, "Local\\");
+	va_start(ap, fmt);
+	vsprintf(path + strlen(path), fmt, ap);
+	va_end(ap);
+
+	if (oflag & O_CREAT)
+		return create_shm_map(oflag, perm, length, mmap, prot,
+				      flags, path, si.dwPageSize);
+	else
+		return open_shm_map(oflag, perm, length, mmap, prot,
+				    flags, path, si.dwPageSize);
+}
+
+void git_shm_unlink(const char *fmt, ...)
+{
+}
+
 #else
 
 ssize_t git_shm_map(int oflag, int perm, ssize_t length, void **mmap,
-- 
1.9.1.346.ga2b5940

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/5] daemonize(): set a flag before exiting the main process
  2014-06-10 13:24 [PATCH v2 0/5] Speed up cache loading time Nguyễn Thái Ngọc Duy
                   ` (2 preceding siblings ...)
  2014-06-10 13:24 ` [PATCH 3/5] index-helper: add Windows support Nguyễn Thái Ngọc Duy
@ 2014-06-10 13:24 ` Nguyễn Thái Ngọc Duy
  2014-06-10 13:24 ` [PATCH 5/5] index-helper: add --detach Nguyễn Thái Ngọc Duy
  4 siblings, 0 replies; 7+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2014-06-10 13:24 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

This allows signal handlers and atexit functions to realize this
situation and not clean up.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/gc.c | 2 +-
 cache.h      | 2 +-
 daemon.c     | 2 +-
 setup.c      | 4 +++-
 4 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/builtin/gc.c b/builtin/gc.c
index 85f5c2b..50275af 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -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();
 
diff --git a/cache.h b/cache.h
index f05e062..6f4b863 100644
--- a/cache.h
+++ b/cache.h
@@ -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)
 
diff --git a/daemon.c b/daemon.c
index eba1255..2650504 100644
--- a/daemon.c
+++ b/daemon.c
@@ -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();
diff --git a/setup.c b/setup.c
index 613e3b3..e8e129a 100644
--- a/setup.c
+++ b/setup.c
@@ -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)
-- 
1.9.1.346.ga2b5940

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 5/5] index-helper: add --detach
  2014-06-10 13:24 [PATCH v2 0/5] Speed up cache loading time Nguyễn Thái Ngọc Duy
                   ` (3 preceding siblings ...)
  2014-06-10 13:24 ` [PATCH 4/5] daemonize(): set a flag before exiting the main process Nguyễn Thái Ngọc Duy
@ 2014-06-10 13:24 ` Nguyễn Thái Ngọc Duy
  4 siblings, 0 replies; 7+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2014-06-10 13:24 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-index-helper.txt | 4 +++-
 index-helper.c                     | 6 +++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt
index afeb334..303cda0 100644
--- a/Documentation/git-index-helper.txt
+++ b/Documentation/git-index-helper.txt
@@ -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
 ---
diff --git a/index-helper.c b/index-helper.c
index f92326a..9420591 100644
--- a/index-helper.c
+++ b/index-helper.c
@@ -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;
-- 
1.9.1.346.ga2b5940

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/5] index-helper: new daemon for caching index and related stuff
  2014-06-10 13:24 ` [PATCH 2/5] index-helper: new daemon for caching index and related stuff Nguyễn Thái Ngọc Duy
@ 2014-06-10 18:44   ` David Turner
  0 siblings, 0 replies; 7+ messages in thread
From: David Turner @ 2014-06-10 18:44 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

On Tue, 2014-06-10 at 20:24 +0700, Nguyễn Thái Ngọc Duy wrote:

> +	loop(sb.buf, 600);
...
> +	if (st->st_mtime + 600 < time(NULL))

s/600/INDEX_HELPER_TIMEOUT/ or something.

> +		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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-06-10 18:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-10 13:24 [PATCH v2 0/5] Speed up cache loading time Nguyễn Thái Ngọc Duy
2014-06-10 13:24 ` [PATCH 1/5] read-cache: allow to keep mmap'd memory after reading Nguyễn Thái Ngọc Duy
2014-06-10 13:24 ` [PATCH 2/5] index-helper: new daemon for caching index and related stuff Nguyễn Thái Ngọc Duy
2014-06-10 18:44   ` David Turner
2014-06-10 13:24 ` [PATCH 3/5] index-helper: add Windows support Nguyễn Thái Ngọc Duy
2014-06-10 13:24 ` [PATCH 4/5] daemonize(): set a flag before exiting the main process Nguyễn Thái Ngọc Duy
2014-06-10 13:24 ` [PATCH 5/5] index-helper: add --detach Nguyễn Thái Ngọc Duy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).