{"thread":{"id":"42390","subject":"[PATCH v12 02/20] read-cache: allow to keep mmap'd memory after reading","startedAt":"2016-05-19T21:45:37Z","lastAt":"2016-06-27T05:50:48Z","messageCount":49,"participants":["David Turner","Duy Nguyen","Christian Couder","Eric Wong"],"isPatch":true,"patchVersion":12,"patchTotal":20},"messages":[{"id":"287027","messageId":"1463694357-6503-1-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":null,"subject":"[PATCH v12 00/20] index-helper/watchman","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:37Z","receivedAt":"2016-05-19T21:45:37Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Of course, as soon as I pinged on the previous version, I noticed an issue.\n\nIn that version, git index-helper --exit-after 0 was documented to\nnever exit, but it would in fact exit immediately.  This changes patch\n04/20.\n\nIn addition, I noticed that there was no way to control the timeout on\nautomatically-run index-helpers, so I fixed that (with a new patch at\nthe end of the series).\n\nEverything else is the same as the updated version of v11.\n\nDavid Turner (9):\n  pkt-line: add gentle version of packet_write\n  index-helper: log warnings\n  unpack-trees: preserve index extensions\n  watchman: add a config option to enable the extension\n  index-helper: kill mode\n  index-helper: don't run if already running\n  index-helper: autorun mode\n  index-helper: optionally automatically run\n  index-helper: indexhelper.exitafter config\n\nNguyễn Thái Ngọc Duy (11):\n  read-cache.c: fix constness of verify_hdr()\n  read-cache: allow to keep mmap'd memory after reading\n  index-helper: new daemon for caching index and related stuff\n  index-helper: add --strict\n  daemonize(): set a flag before exiting the main process\n  index-helper: add --detach\n  read-cache: add watchman 'WAMA' extension\n  watchman: support watchman to reduce index refresh cost\n  index-helper: use watchman to avoid refreshing index with lstat()\n  update-index: enable/disable watchman support\n  trace: measure where the time is spent in the index-heavy operations\n\n .gitignore                               |   2 +\n Documentation/config.txt                 |  12 +\n Documentation/git-index-helper.txt       |  81 +++++\n Documentation/git-update-index.txt       |   6 +\n Documentation/technical/index-format.txt |  22 ++\n Makefile                                 |  18 ++\n builtin/gc.c                             |   2 +-\n builtin/update-index.c                   |  16 +\n cache.h                                  |  25 +-\n config.c                                 |   5 +\n configure.ac                             |   8 +\n contrib/completion/git-completion.bash   |   1 +\n daemon.c                                 |   2 +-\n diff-lib.c                               |   4 +\n dir.c                                    |  25 +-\n dir.h                                    |   6 +\n environment.c                            |   3 +\n git-compat-util.h                        |   1 +\n index-helper.c                           | 491 ++++++++++++++++++++++++++++\n name-hash.c                              |   2 +\n pkt-line.c                               |  18 ++\n pkt-line.h                               |   2 +\n preload-index.c                          |   2 +\n read-cache.c                             | 527 ++++++++++++++++++++++++++++++-\n refs/files-backend.c                     |   2 +\n setup.c                                  |   4 +-\n t/t1701-watchman-extension.sh            |  37 +++\n t/t7063-status-untracked-cache.sh        |  22 ++\n t/t7900-index-helper.sh                  |  77 +++++\n t/test-lib-functions.sh                  |   4 +\n test-dump-watchman.c                     |  16 +\n unpack-trees.c                           |   1 +\n watchman-support.c                       | 135 ++++++++\n watchman-support.h                       |   7 +\n 34 files changed, 1565 insertions(+), 21 deletions(-)\n create mode 100644 Documentation/git-index-helper.txt\n create mode 100644 index-helper.c\n create mode 100755 t/t1701-watchman-extension.sh\n create mode 100755 t/t7900-index-helper.sh\n create mode 100644 test-dump-watchman.c\n create mode 100644 watchman-support.c\n create mode 100644 watchman-support.h\n\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287029","messageId":"1463694357-6503-2-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 01/20] read-cache.c: fix constness of verify_hdr()","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:38Z","receivedAt":"2016-05-19T21:45:38Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n read-cache.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/read-cache.c b/read-cache.c\nindex d9fb78b..16cc487 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1345,7 +1345,7 @@ struct ondisk_cache_entry_extended {\n \t\t\t    ondisk_cache_entry_extended_size(ce_namelen(ce)) : \\\n \t\t\t    ondisk_cache_entry_size(ce_namelen(ce)))\n \n-static int verify_hdr(struct cache_header *hdr, unsigned long size)\n+static int verify_hdr(const struct cache_header *hdr, unsigned long size)\n {\n \tgit_SHA_CTX c;\n \tunsigned char sha1[20];\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287026","messageId":"1463694357-6503-3-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 02/20] read-cache: allow to keep mmap'd memory after reading","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:39Z","receivedAt":"2016-05-19T21:45:39Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nLater, we will introduce git index-helper to share this memory with\nother git processes.\n\nWe only unmap it when we discard the index (although the kernel may of\ncourse choose to page it out).\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n cache.h      |  3 +++\n read-cache.c | 13 ++++++++++++-\n 2 files changed, 15 insertions(+), 1 deletion(-)\n\ndiff --git a/cache.h b/cache.h\nindex b829410..4180e2b 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -333,11 +333,14 @@ struct index_state {\n \tstruct split_index *split_index;\n \tstruct cache_time timestamp;\n \tunsigned name_hash_initialized : 1,\n+\t\t keep_mmap : 1,\n \t\t initialized : 1;\n \tstruct hashmap name_hash;\n \tstruct hashmap dir_hash;\n \tunsigned char sha1[20];\n \tstruct untracked_cache *untracked;\n+\tvoid *mmap;\n+\tsize_t mmap_size;\n };\n \n extern struct index_state the_index;\ndiff --git a/read-cache.c b/read-cache.c\nindex 16cc487..3cb0ec3 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1574,6 +1574,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)\n \tmmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);\n \tif (mmap == MAP_FAILED)\n \t\tdie_errno(\"unable to map index file\");\n+\tif (istate->keep_mmap) {\n+\t\tistate->mmap = mmap;\n+\t\tistate->mmap_size = mmap_size;\n+\t}\n \tclose(fd);\n \n \thdr = mmap;\n@@ -1626,11 +1630,13 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)\n \t\tsrc_offset += 8;\n \t\tsrc_offset += extsize;\n \t}\n-\tmunmap(mmap, mmap_size);\n+\tif (!istate->keep_mmap)\n+\t\tmunmap(mmap, mmap_size);\n \treturn istate->cache_nr;\n \n unmap:\n \tmunmap(mmap, mmap_size);\n+\tistate->mmap = NULL;\n \tdie(\"index file corrupt\");\n }\n \n@@ -1655,6 +1661,7 @@ int read_index_from(struct index_state *istate, const char *path)\n \t\tdiscard_index(split_index->base);\n \telse\n \t\tsplit_index->base = xcalloc(1, sizeof(*split_index->base));\n+\tsplit_index->base->keep_mmap = istate->keep_mmap;\n \tret = do_read_index(split_index->base,\n \t\t\t    git_path(\"sharedindex.%s\",\n \t\t\t\t     sha1_to_hex(split_index->base_sha1)), 1);\n@@ -1698,6 +1705,10 @@ int discard_index(struct index_state *istate)\n \tfree(istate->cache);\n \tistate->cache = NULL;\n \tistate->cache_alloc = 0;\n+\tif (istate->keep_mmap && istate->mmap) {\n+\t\tmunmap(istate->mmap, istate->mmap_size);\n+\t\tistate->mmap = NULL;\n+\t}\n \tdiscard_split_index(istate);\n \tfree_untracked_cache(istate->untracked);\n \tistate->untracked = NULL;\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287028","messageId":"1463694357-6503-4-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 03/20] pkt-line: add gentle version of packet_write","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:40Z","receivedAt":"2016-05-19T21:45:40Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"packet_write calls write_or_die, which dies with a sigpipe even if\ncalling code has explicitly blocked that signal.\n\nAdd packet_write_gently and packet_flush_gently, which don't.  Soon,\nwe will use this for communication with git index-helper, which, being\nmerely an optimization, should be permitted to die without disrupting\nclients.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n pkt-line.c | 18 ++++++++++++++++++\n pkt-line.h |  2 ++\n 2 files changed, 20 insertions(+)\n\ndiff --git a/pkt-line.c b/pkt-line.c\nindex 62fdb37..f964446 100644\n--- a/pkt-line.c\n+++ b/pkt-line.c\n@@ -91,6 +91,12 @@ void packet_flush(int fd)\n \twrite_or_die(fd, \"0000\", 4);\n }\n \n+int packet_flush_gently(int fd)\n+{\n+\tpacket_trace(\"0000\", 4, 1);\n+\treturn write_in_full(fd, \"0000\", 4) != 4;\n+}\n+\n void packet_buf_flush(struct strbuf *buf)\n {\n \tpacket_trace(\"0000\", 4, 1);\n@@ -130,6 +136,18 @@ void packet_write(int fd, const char *fmt, ...)\n \twrite_or_die(fd, buf.buf, buf.len);\n }\n \n+int packet_write_gently(int fd, const char *fmt, ...)\n+{\n+\tstatic struct strbuf buf = STRBUF_INIT;\n+\tva_list args;\n+\n+\tstrbuf_reset(&buf);\n+\tva_start(args, fmt);\n+\tformat_packet(&buf, fmt, args);\n+\tva_end(args);\n+\treturn write_in_full(fd, buf.buf, buf.len) != buf.len;\n+}\n+\n void packet_buf_write(struct strbuf *buf, const char *fmt, ...)\n {\n \tva_list args;\ndiff --git a/pkt-line.h b/pkt-line.h\nindex 3cb9d91..deffcb5 100644\n--- a/pkt-line.h\n+++ b/pkt-line.h\n@@ -20,7 +20,9 @@\n  * side can't, we stay with pure read/write interfaces.\n  */\n void packet_flush(int fd);\n+int packet_flush_gently(int fd);\n void packet_write(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n+int packet_write_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n void packet_buf_flush(struct strbuf *buf);\n void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));\n \n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287045","messageId":"1463694357-6503-5-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:41Z","receivedAt":"2016-05-19T21:45:41Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nInstead of reading the index from disk and worrying about disk\ncorruption, the index is cached in memory (memory bit-flips happen\ntoo, but hopefully less often). The result is faster read. Read time\nis reduced by 70%.\n\nThe biggest gain is not having to verify the trailing SHA-1, which\ntakes lots of time especially on large index files. But this also\nopens doors for further optimizations:\n\n - we could create an in-memory format that's essentially the memory\n   dump of the index to eliminate most of parsing/allocation\n   overhead. The mmap'd memory can be used straight away. Experiment\n   [1] shows we could reduce read time by 88%.\n\n - we could cache non-index info such as name hash\n\nShared memory is done by storing files in a per-repository temporary\ndirectory.  This is more portable than shm (which requires\nposix-realtime and has various quirks on OS X).  It might even work on\nWindows, although this has not been tested. The shared memory file's\nname follows the template \"shm-<object>-<SHA1>\" where <SHA1> is the\ntrailing SHA-1 of the index file. <object> is \"index\" for cached index\nfiles (and might later be \"name-hash\" for name-hash cache). If such\nshared memory exists, it contains the same index content as on\ndisk. The content is already validated by the daemon and git won't\nvalidate it again (except comparing the trailing SHA-1s).\n\nWe also add some bits to the index (to_shm and from_shm) to track\nwhen an index came from shared memory or is going to shared memory.\n\nWe keep this daemon's logic as thin as possible. The \"brain\" stays in\ngit. So the daemon can read and validate stuff, but that's all it's\nallowed to do. It does not add/create new information. It doesn't even\naccept direct updates from git.\n\nGit can poke the daemon via unix domain sockets to tell it to refresh\nthe index cache, or to keep it alive some more minutes. It can't give\nany real index data directly to the daemon. Real data goes to disk\nfirst, then the daemon reads and verifies it from there. The daemon\nonly handles $GIT_DIR/index, not temporary index files; it only gets\npoked for the former.\n\n$GIT_DIR/index-helper.sock is the socket for the daemon process. The\ndaemon reads from the socket and executes commands.\n\nNamed pipes were considered for portability reasons, but then commands\nthat need replies from the daemon would have to open their own pipes,\nsince a named pipe should only have one reader.  Unix domain sockets\ndon't have this problem.\n\nOn webkit.git with index format v2, duplicating 8 times to 1.5m\nentries and 236MB in size:\n\n(vanilla)      0.50 s: read_index_from .git/index\n(index-helper) 0.18 s: read_index_from .git/index\n\nInterestingly with index v4, we get less out of index-helper. It makes\nsense as v4 requires more processing after loading the index:\n\n(vanilla)      0.37 s: read_index_from .git/index\n(index-helper) 0.22 s: read_index_from .git/index\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/247268/focus=248771\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\nSigned-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>\n---\n .gitignore                             |   1 +\n Documentation/git-index-helper.txt     |  50 ++++++\n Makefile                               |   5 +\n cache.h                                |  11 ++\n contrib/completion/git-completion.bash |   1 +\n git-compat-util.h                      |   1 +\n index-helper.c                         | 280 +++++++++++++++++++++++++++++++++\n read-cache.c                           | 125 +++++++++++++--\n t/t7900-index-helper.sh                |  23 +++\n 9 files changed, 488 insertions(+), 9 deletions(-)\n create mode 100644 Documentation/git-index-helper.txt\n create mode 100644 index-helper.c\n create mode 100755 t/t7900-index-helper.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex 5087ce1..b92f122 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -71,6 +71,7 @@\n /git-http-fetch\n /git-http-push\n /git-imap-send\n+/git-index-helper\n /git-index-pack\n /git-init\n /git-init-db\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nnew file mode 100644\nindex 0000000..f892184\n--- /dev/null\n+++ b/Documentation/git-index-helper.txt\n@@ -0,0 +1,50 @@\n+git-index-helper(1)\n+===================\n+\n+NAME\n+----\n+git-index-helper - A simple cache daemon for speeding up index file access\n+\n+SYNOPSIS\n+--------\n+[verse]\n+'git index-helper' [options]\n+\n+DESCRIPTION\n+-----------\n+Keep the index file in memory for faster access. This daemon is per\n+repository and per working tree.  So if you have two working trees\n+each with a submodule, you might need four index-helpers.  (In practice,\n+this is only worthwhile for large indexes, so only use it if you notice\n+that git status is slow).\n+\n+OPTIONS\n+-------\n+\n+--exit-after=<n>::\n+\tExit if the cached index is not accessed for `<n>`\n+\tseconds. Specify 0 to wait forever. Default is 600.\n+\n+NOTES\n+-----\n+\n+$GIT_DIR/index-helper.sock a Unix domain socket that the daemon reads\n+commands from.  The directory will also contain files named\n+\"shm-index-<SHA1>\".  These are used as backing stores for shared\n+memory.  Normally the daemon will clean up these files when it exits\n+or when they are no longer relevant.  But if it crashes, some objects\n+could remain there and they can be safely deleted with \"rm\"\n+command. The following commands are used to control the daemon:\n+\n+\"refresh\"::\n+\tReread the index.\n+\n+\"poke\":\n+\tLet the daemon know the index is to be read. It keeps the\n+\tdaemon alive longer, unless `--exit-after=0` is used.\n+\n+All commands and replies are terminated by a NUL byte.\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\ndiff --git a/Makefile b/Makefile\nindex 2742a69..c8be0e7 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -1433,6 +1433,10 @@ ifdef HAVE_DEV_TTY\n \tBASIC_CFLAGS += -DHAVE_DEV_TTY\n endif\n \n+ifndef NO_MMAP\n+\tPROGRAM_OBJS += index-helper.o\n+endif\n+\n ifdef DIR_HAS_BSD_GROUP_SEMANTICS\n \tCOMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS\n endif\n@@ -2159,6 +2163,7 @@ GIT-BUILD-OPTIONS: FORCE\n \t@echo NO_PERL=\\''$(subst ','\\'',$(subst ','\\'',$(NO_PERL)))'\\' >>$@+\n \t@echo NO_PYTHON=\\''$(subst ','\\'',$(subst ','\\'',$(NO_PYTHON)))'\\' >>$@+\n \t@echo NO_UNIX_SOCKETS=\\''$(subst ','\\'',$(subst ','\\'',$(NO_UNIX_SOCKETS)))'\\' >>$@+\n+\t@echo NO_MMAP=\\''$(subst ','\\'',$(subst ','\\'',$(NO_MMAP)))'\\' >>$@+\n ifdef TEST_OUTPUT_DIRECTORY\n \t@echo TEST_OUTPUT_DIRECTORY=\\''$(subst ','\\'',$(subst ','\\'',$(TEST_OUTPUT_DIRECTORY)))'\\' >>$@+\n endif\ndiff --git a/cache.h b/cache.h\nindex 4180e2b..2d7af6f 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -334,6 +334,17 @@ struct index_state {\n \tstruct cache_time timestamp;\n \tunsigned name_hash_initialized : 1,\n \t\t keep_mmap : 1,\n+\n+\t\t /*\n+\t\t  * This index came from index-helper originally.\n+\t\t  */\n+\t\t from_shm : 1,\n+\t\t /*\n+\t\t  * We're moving this index to shared memory, so we\n+\t\t  * don't need to poke the daemon to request updates\n+\t\t  * on it.\n+\t\t  */\n+\t\t to_shm : 1,\n \t\t initialized : 1;\n \tstruct hashmap name_hash;\n \tstruct hashmap dir_hash;\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex e3918c8..98794d2 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -684,6 +684,7 @@ __git_list_porcelain_commands ()\n \t\tfor-each-ref)     : plumbing;;\n \t\thash-object)      : plumbing;;\n \t\thttp-*)           : transport;;\n+\t\tindex-helper)     : plumbing;;\n \t\tindex-pack)       : plumbing;;\n \t\tinit-db)          : deprecated;;\n \t\tlocal-fetch)      : plumbing;;\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex c07e0c1..56945a7 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -513,6 +513,7 @@ static inline int ends_with(const char *str, const char *suffix)\n #define PROT_READ 1\n #define PROT_WRITE 2\n #define MAP_PRIVATE 1\n+#define MAP_SHARED 2\n #endif\n \n #define mmap git_mmap\ndiff --git a/index-helper.c b/index-helper.c\nnew file mode 100644\nindex 0000000..260ef4a\n--- /dev/null\n+++ b/index-helper.c\n@@ -0,0 +1,280 @@\n+#include \"cache.h\"\n+#include \"parse-options.h\"\n+#include \"sigchain.h\"\n+#include \"strbuf.h\"\n+#include \"exec_cmd.h\"\n+#include \"split-index.h\"\n+#include \"lockfile.h\"\n+#include \"cache.h\"\n+#include \"unix-socket.h\"\n+#include \"pkt-line.h\"\n+\n+struct shm {\n+\tunsigned char sha1[20];\n+\tvoid *shm;\n+\tsize_t size;\n+};\n+\n+static struct shm shm_index;\n+static struct shm shm_base_index;\n+\n+static void release_index_shm(struct shm *is)\n+{\n+\tif (!is->shm)\n+\t\treturn;\n+\tmunmap(is->shm, is->size);\n+\tunlink(git_path(\"shm-index-%s\", sha1_to_hex(is->sha1)));\n+\tis->shm = NULL;\n+}\n+\n+static void cleanup_shm(void)\n+{\n+\trelease_index_shm(&shm_index);\n+\trelease_index_shm(&shm_base_index);\n+}\n+\n+static void cleanup(void)\n+{\n+\tunlink(git_path(\"index-helper.sock\"));\n+\tcleanup_shm();\n+}\n+\n+static void cleanup_on_signal(int sig)\n+{\n+\t/*\n+\t * We ignore sigpipes -- that's just a client suddenly dying.\n+\t */\n+\tif (sig == SIGPIPE)\n+\t\treturn;\n+\tcleanup();\n+\tsigchain_pop(sig);\n+\traise(sig);\n+}\n+\n+static int shared_mmap_create(size_t size, void **new_mmap, const char *path)\n+{\n+\tint fd = -1;\n+\tint ret = -1;\n+\n+\tfd = open(path, O_CREAT | O_EXCL | O_RDWR, 0600);\n+\n+\tif (fd < 0)\n+\t\tgoto done;\n+\n+\tif (ftruncate(fd, size))\n+\t\tgoto done;\n+\n+\t*new_mmap = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,\n+\t\t\t fd, 0);\n+\n+\tif (*new_mmap == MAP_FAILED) {\n+\t\t*new_mmap = NULL;\n+\t\tgoto done;\n+\t}\n+\tmadvise(new_mmap, size, MADV_WILLNEED);\n+\n+\tret = 0;\n+done:\n+\tif (fd > 0)\n+\t\tclose(fd);\n+\treturn ret;\n+}\n+\n+static void share_index(struct index_state *istate, struct shm *is)\n+{\n+\tvoid *new_mmap;\n+\tif (istate->mmap_size <= 20 ||\n+\t    hashcmp(istate->sha1,\n+\t\t    (unsigned char *)istate->mmap + istate->mmap_size - 20) ||\n+\t    !hashcmp(istate->sha1, is->sha1))\n+\t\t/* Nothing to do */\n+\t\treturn;\n+\tif (shared_mmap_create(istate->mmap_size, &new_mmap,\n+\t\t\t       git_path(\"shm-index-%s\",\n+\t\t\t\t\tsha1_to_hex(istate->sha1))) < 0) {\n+\t\tdie(\"Failed to create shm-index file\");\n+\t}\n+\n+\n+\trelease_index_shm(is);\n+\tis->size = istate->mmap_size;\n+\tis->shm = new_mmap;\n+\thashcpy(is->sha1, istate->sha1);\n+\n+\tmemcpy(new_mmap, istate->mmap, istate->mmap_size - 20);\n+\n+\t/*\n+\t * The trailing hash must be written last after everything is\n+\t * written. It's the indication that the shared memory is now\n+\t * ready.\n+\t * The memory barrier here matches read-cache.c:try_shm.\n+\t */\n+\t__sync_synchronize();\n+\n+\thashcpy((unsigned char *)new_mmap + istate->mmap_size - 20, is->sha1);\n+}\n+\n+static void share_the_index(void)\n+{\n+\tif (the_index.split_index && the_index.split_index->base)\n+\t\tshare_index(the_index.split_index->base, &shm_base_index);\n+\tshare_index(&the_index, &shm_index);\n+\tdiscard_index(&the_index);\n+}\n+\n+static void set_socket_blocking_flag(int fd, int make_nonblocking)\n+{\n+\tint flags;\n+\n+\tflags = fcntl(fd, F_GETFL, NULL);\n+\n+\tif (flags < 0)\n+\t\tdie(_(\"fcntl failed\"));\n+\n+\tif (make_nonblocking)\n+\t\tflags |= O_NONBLOCK;\n+\telse\n+\t\tflags &= ~O_NONBLOCK;\n+\n+\tif (fcntl(fd, F_SETFL, flags) < 0)\n+\t\tdie(_(\"fcntl failed\"));\n+}\n+\n+static void refresh(void)\n+{\n+\tdiscard_index(&the_index);\n+\tthe_index.keep_mmap = 1;\n+\tthe_index.to_shm    = 1;\n+\tif (read_cache() < 0)\n+\t\tdie(_(\"could not read index\"));\n+\tshare_the_index();\n+}\n+\n+#ifndef NO_MMAP\n+\n+static void loop(int fd, int idle_in_seconds)\n+{\n+\tassert(idle_in_seconds < INT_MAX / 1000);\n+\n+\tif (idle_in_seconds == 0)\n+\t\tidle_in_seconds = -1;\n+\n+\twhile (1) {\n+\t\tstruct pollfd pollfd;\n+\t\tint result, client_fd;\n+\t\tint flags;\n+\t\tchar buf[4096];\n+\t\tint bytes_read;\n+\n+\t\t/* Wait for a request */\n+\t\tpollfd.fd = fd;\n+\t\tpollfd.events = POLLIN;\n+\t\tresult = poll(&pollfd, 1, idle_in_seconds * 1000);\n+\t\tif (result < 0) {\n+\t\t\tif (errno == EINTR)\n+\t\t\t\t/*\n+\t\t\t\t * This can lead to an overlong keepalive,\n+\t\t\t\t * but that is better than a premature exit.\n+\t\t\t\t */\n+\t\t\t\tcontinue;\n+\t\t\tdie_errno(_(\"poll() failed\"));\n+\t\t} else if (result == 0)\n+\t\t\t/* timeout */\n+\t\t\tbreak;\n+\n+\t\tclient_fd = accept(fd, NULL, NULL);\n+\t\tif (client_fd < 0)\n+\t\t\t/*\n+\t\t\t * An error here is unlikely -- it probably\n+\t\t\t * indicates that the connecting process has\n+\t\t\t * already dropped the connection.\n+\t\t\t */\n+\t\t\tcontinue;\n+\n+\t\t/*\n+\t\t * Our connection to the client is blocking since a client\n+\t\t * can always be killed by SIGINT or similar.\n+\t\t */\n+\t\tset_socket_blocking_flag(client_fd, 0);\n+\n+\t\tflags = PACKET_READ_GENTLE_ON_EOF | PACKET_READ_CHOMP_NEWLINE;\n+\t\tbytes_read = packet_read(client_fd, NULL, NULL, buf,\n+\t\t\t\t\t sizeof(buf), flags);\n+\n+\t\tif (bytes_read > 0) {\n+\t\t\t/* ensure string termination */\n+\t\t\tbuf[bytes_read] = 0;\n+\t\t\tif (!strcmp(buf, \"refresh\")) {\n+\t\t\t\trefresh();\n+\t\t\t} else if (!strcmp(buf, \"poke\")) {\n+\t\t\t\t/*\n+\t\t\t\t * Just a poke to keep us\n+\t\t\t\t * alive, nothing to do.\n+\t\t\t\t */\n+\t\t\t} else {\n+\t\t\t\twarning(\"BUG: Bogus command %s\", buf);\n+\t\t\t}\n+\t\t} else {\n+\t\t\t/*\n+\t\t\t * No command from client.  Probably it's just\n+\t\t\t * a liveness check or client error.  Just\n+\t\t\t * close up.\n+\t\t\t */\n+\t\t}\n+\t\tclose(client_fd);\n+\t}\n+\n+\tclose(fd);\n+}\n+\n+#else\n+\n+static void loop(int fd, int idle_in_seconds)\n+{\n+\tdie(_(\"index-helper is not supported on this platform\"));\n+}\n+\n+#endif\n+\n+static const char * const usage_text[] = {\n+\tN_(\"git index-helper [options]\"),\n+\tNULL\n+};\n+\n+int main(int argc, char **argv)\n+{\n+\tconst char *prefix;\n+\tint idle_in_seconds = 600;\n+\tint fd;\n+\tstruct strbuf socket_path = STRBUF_INIT;\n+\tstruct option options[] = {\n+\t\tOPT_INTEGER(0, \"exit-after\", &idle_in_seconds,\n+\t\t\t    N_(\"exit if not used after some seconds\")),\n+\t\tOPT_END()\n+\t};\n+\n+\tgit_extract_argv0_path(argv[0]);\n+\tgit_setup_gettext();\n+\n+\tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n+\t\tusage_with_options(usage_text, options);\n+\n+\tprefix = setup_git_directory();\n+\tif (parse_options(argc, (const char **)argv, prefix,\n+\t\t\t  options, usage_text, 0))\n+\t\tdie(_(\"too many arguments\"));\n+\n+\tatexit(cleanup);\n+\tsigchain_push_common(cleanup_on_signal);\n+\n+\tstrbuf_git_path(&socket_path, \"index-helper.sock\");\n+\n+\tfd = unix_stream_listen(socket_path.buf);\n+\tif (fd < 0)\n+\t\tdie_errno(_(\"could not set up index-helper socket\"));\n+\n+\tloop(fd, idle_in_seconds);\n+\n+\tclose(fd);\n+\treturn 0;\n+}\ndiff --git a/read-cache.c b/read-cache.c\nindex 3cb0ec3..e3c8f3e 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -18,6 +18,9 @@\n #include \"varint.h\"\n #include \"split-index.h\"\n #include \"utf8.h\"\n+#include \"unix-socket.h\"\n+#include \"pkt-line.h\"\n+#include \"sigchain.h\"\n \n static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \t\t\t\t\t       unsigned int options);\n@@ -1541,6 +1544,98 @@ static void post_read_index_from(struct index_state *istate)\n \ttweak_untracked_cache(istate);\n }\n \n+static int poke_daemon(struct index_state *istate,\n+\t\t       const struct stat *st, int refresh_cache)\n+{\n+\tint fd;\n+\tint ret = 0;\n+\tconst char *socket_path;\n+\n+\t/* if this is from index-helper, do not poke itself (recursively) */\n+\tif (istate->to_shm)\n+\t\treturn 0;\n+\n+\tsocket_path = git_path(\"index-helper.sock\");\n+\tif (!socket_path)\n+\t\treturn -1;\n+\n+\tfd = unix_stream_connect(socket_path);\n+\tsigchain_push(SIGPIPE, SIG_IGN);\n+\tif (refresh_cache) {\n+\t\tpacket_write_gently(fd, \"refresh\");\n+\t} else {\n+\t\tpacket_write_gently(fd, \"poke\");\n+\t}\n+\tpacket_flush_gently(fd);\n+\n+\tclose(fd);\n+\tsigchain_pop(SIGPIPE);\n+\treturn ret;\n+}\n+\n+static int is_main_index(struct index_state *istate)\n+{\n+\treturn istate == &the_index ||\n+\t\t(the_index.split_index &&\n+\t\t istate == the_index.split_index->base);\n+}\n+\n+/*\n+ * Try to open and verify a cached shm index if available. Return 0 if\n+ * succeeds (istate->mmap and istate->mmap_size are updated). Return\n+ * negative otherwise.\n+ */\n+static int try_shm(struct index_state *istate)\n+{\n+\tvoid *new_mmap = NULL;\n+\tsize_t old_size = istate->mmap_size;\n+\tssize_t new_size;\n+\tconst unsigned char *sha1;\n+\tstruct stat st;\n+\tint fd = -1;\n+\n+\tif (!is_main_index(istate) ||\n+\t    old_size <= 20 ||\n+\t    stat(git_path(\"index-helper.sock\"), &st))\n+\t\treturn -1;\n+\tif (poke_daemon(istate, &st, 0))\n+\t\treturn -1;\n+\tsha1 = (unsigned char *)istate->mmap + old_size - 20;\n+\n+\tfd = open(git_path(\"shm-index-%s\", sha1_to_hex(sha1)), O_RDONLY);\n+\tif (fd < 0)\n+\t\tgoto fail;\n+\n+\tif (fstat(fd, &st))\n+\t\tgoto fail;\n+\n+\tnew_size = st.st_size;\n+\tnew_mmap = mmap(NULL, new_size, PROT_READ, MAP_SHARED, fd, 0);\n+\tif (new_size <= 20 ||\n+\t    hashcmp((unsigned char *)istate->mmap + old_size - 20,\n+\t\t    (unsigned char *)new_mmap + new_size - 20)) {\n+\t\tif (new_mmap)\n+\t\t\tmunmap(new_mmap, new_size);\n+\t\tgoto fail;\n+\t}\n+\n+\t/* The memory barrier here matches index-helper.c:share_index. */\n+\t__sync_synchronize();\n+\n+\tmunmap(istate->mmap, istate->mmap_size);\n+\tistate->mmap = new_mmap;\n+\tistate->mmap_size = new_size;\n+\tistate->from_shm = 1;\n+\tclose(fd);\n+\treturn 0;\n+\n+fail:\n+\tif (fd >= 0)\n+\t\tclose(fd);\n+\tpoke_daemon(istate, &st, 1);\n+\treturn -1;\n+}\n+\n /* remember to discard_cache() before reading a different cache! */\n int do_read_index(struct index_state *istate, const char *path, int must_exist)\n {\n@@ -1555,6 +1650,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)\n \tif (istate->initialized)\n \t\treturn istate->cache_nr;\n \n+\tistate->from_shm = 0;\n \tistate->timestamp.sec = 0;\n \tistate->timestamp.nsec = 0;\n \tfd = open(path, O_RDONLY);\n@@ -1574,15 +1670,17 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)\n \tmmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);\n \tif (mmap == MAP_FAILED)\n \t\tdie_errno(\"unable to map index file\");\n-\tif (istate->keep_mmap) {\n-\t\tistate->mmap = mmap;\n-\t\tistate->mmap_size = mmap_size;\n-\t}\n \tclose(fd);\n \n-\thdr = mmap;\n-\tif (verify_hdr(hdr, mmap_size) < 0)\n+\tistate->mmap = mmap;\n+\tistate->mmap_size = mmap_size;\n+\tif (try_shm(istate) &&\n+\t    verify_hdr(istate->mmap, istate->mmap_size) < 0)\n \t\tgoto unmap;\n+\thdr = mmap = istate->mmap;\n+\tmmap_size = istate->mmap_size;\n+\tif (!istate->keep_mmap)\n+\t\tistate->mmap = NULL;\n \n \thashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);\n \tistate->version = ntohl(hdr->hdr_version);\n@@ -1662,6 +1760,8 @@ int read_index_from(struct index_state *istate, const char *path)\n \telse\n \t\tsplit_index->base = xcalloc(1, sizeof(*split_index->base));\n \tsplit_index->base->keep_mmap = istate->keep_mmap;\n+\tsplit_index->base->to_shm    = istate->to_shm;\n+\tsplit_index->base->from_shm  = istate->from_shm;\n \tret = do_read_index(split_index->base,\n \t\t\t    git_path(\"sharedindex.%s\",\n \t\t\t\t     sha1_to_hex(split_index->base_sha1)), 1);\n@@ -1712,6 +1812,8 @@ int discard_index(struct index_state *istate)\n \tdiscard_split_index(istate);\n \tfree_untracked_cache(istate->untracked);\n \tistate->untracked = NULL;\n+\tistate->from_shm = 0;\n+\tistate->to_shm   = 0;\n \treturn 0;\n }\n \n@@ -2138,9 +2240,14 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l\n \t\treturn ret;\n \tassert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=\n \t       (COMMIT_LOCK | CLOSE_LOCK));\n-\tif (flags & COMMIT_LOCK)\n-\t\treturn commit_locked_index(lock);\n-\telse if (flags & CLOSE_LOCK)\n+\tif (flags & COMMIT_LOCK) {\n+\t\tstruct stat st;\n+\t\tret = commit_locked_index(lock);\n+\t\tif (!ret && is_main_index(istate) &&\n+\t\t    !stat(git_path(\"index-helper.sock\"), &st))\n+\t\t\tpoke_daemon(istate, &st, 1);\n+\t\treturn ret;\n+\t} else if (flags & CLOSE_LOCK)\n \t\treturn close_lock_file(lock);\n \telse\n \t\treturn ret;\ndiff --git a/t/t7900-index-helper.sh b/t/t7900-index-helper.sh\nnew file mode 100755\nindex 0000000..114c112\n--- /dev/null\n+++ b/t/t7900-index-helper.sh\n@@ -0,0 +1,23 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2016, Twitter, Inc\n+#\n+\n+test_description='git-index-helper\n+\n+Testing git index-helper\n+'\n+\n+. ./test-lib.sh\n+\n+test -n \"$NO_MMAP\" && {\n+\tskip_all='skipping index-helper tests: no mmap'\n+\ttest_done\n+}\n+\n+test_expect_success 'index-helper smoke test' '\n+\tgit index-helper --exit-after 1 &&\n+\ttest_path_is_missing .git/index-helper.sock\n+'\n+\n+test_done\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287046","messageId":"1463694357-6503-6-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 05/20] index-helper: add --strict","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:42Z","receivedAt":"2016-05-19T21:45:42Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nThere are \"holes\" in the index-helper approach because the shared\nmemory is not verified again by git. If $USER is compromised, shared\nmemory could be modified. But anyone who could do this could already\nmodify $GIT_DIR/index. A more realistic risk is some bugs in\nindex-helper that produce corrupt shared memory. --strict is added to\navoid that.\n\nStrictly speaking there's still a very small gap where corrupt shared\nmemory could still be read by git: after we write the trailing SHA-1 in\nthe shared memory (thus signaling \"this shm is ready\") and before\nverify_shm() detects an error.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/git-index-helper.txt |  9 +++++++\n cache.h                            |  1 +\n index-helper.c                     | 48 ++++++++++++++++++++++++++++++++++++++\n read-cache.c                       |  9 ++++---\n 4 files changed, 64 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nindex f892184..1f92c89 100644\n--- a/Documentation/git-index-helper.txt\n+++ b/Documentation/git-index-helper.txt\n@@ -25,6 +25,15 @@ OPTIONS\n \tExit if the cached index is not accessed for `<n>`\n \tseconds. Specify 0 to wait forever. Default is 600.\n \n+--strict::\n+--no-strict::\n+\tStrict mode makes index-helper verify the shared memory after\n+\tit's created. If the result does not match what's read from\n+\t$GIT_DIR/index, the shared memory is destroyed. This makes\n+\tindex-helper take more than double the amount of time required\n+\tfor reading an index, but because it will happen in the\n+\tbackground, it's not noticable. `--strict` is enabled by default.\n+\n NOTES\n -----\n \ndiff --git a/cache.h b/cache.h\nindex 2d7af6f..6cb0d02 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -345,6 +345,7 @@ struct index_state {\n \t\t  * on it.\n \t\t  */\n \t\t to_shm : 1,\n+\t\t always_verify_trailing_sha1 : 1,\n \t\t initialized : 1;\n \tstruct hashmap name_hash;\n \tstruct hashmap dir_hash;\ndiff --git a/index-helper.c b/index-helper.c\nindex 260ef4a..a7f8a42 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -17,6 +17,7 @@ struct shm {\n \n static struct shm shm_index;\n static struct shm shm_base_index;\n+static int to_verify = 1;\n \n static void release_index_shm(struct shm *is)\n {\n@@ -114,11 +115,56 @@ static void share_index(struct index_state *istate, struct shm *is)\n \thashcpy((unsigned char *)new_mmap + istate->mmap_size - 20, is->sha1);\n }\n \n+static int verify_shm(void)\n+{\n+\tint i;\n+\tstruct index_state istate;\n+\tmemset(&istate, 0, sizeof(istate));\n+\tistate.always_verify_trailing_sha1 = 1;\n+\tistate.to_shm = 1;\n+\ti = read_index(&istate);\n+\tif (i != the_index.cache_nr)\n+\t\tgoto done;\n+\tfor (; i < the_index.cache_nr; i++) {\n+\t\tstruct cache_entry *base, *ce;\n+\t\t/* namelen is checked separately */\n+\t\tconst unsigned int ondisk_flags =\n+\t\t\tCE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;\n+\t\tunsigned int ce_flags, base_flags, ret;\n+\t\tbase = the_index.cache[i];\n+\t\tce = istate.cache[i];\n+\t\tif (ce->ce_namelen != base->ce_namelen ||\n+\t\t    strcmp(ce->name, base->name)) {\n+\t\t\twarning(\"mismatch at entry %d\", i);\n+\t\t\tbreak;\n+\t\t}\n+\t\tce_flags = ce->ce_flags;\n+\t\tbase_flags = base->ce_flags;\n+\t\t/* only on-disk flags matter */\n+\t\tce->ce_flags   &= ondisk_flags;\n+\t\tbase->ce_flags &= ondisk_flags;\n+\t\tret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,\n+\t\t\t     offsetof(struct cache_entry, name) -\n+\t\t\t     offsetof(struct cache_entry, ce_stat_data));\n+\t\tce->ce_flags = ce_flags;\n+\t\tbase->ce_flags = base_flags;\n+\t\tif (ret) {\n+\t\t\twarning(\"mismatch at entry %d\", i);\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+done:\n+\tdiscard_index(&istate);\n+\treturn i == the_index.cache_nr;\n+}\n+\n static void share_the_index(void)\n {\n \tif (the_index.split_index && the_index.split_index->base)\n \t\tshare_index(the_index.split_index->base, &shm_base_index);\n \tshare_index(&the_index, &shm_index);\n+\tif (to_verify && !verify_shm())\n+\t\tcleanup_shm();\n \tdiscard_index(&the_index);\n }\n \n@@ -250,6 +296,8 @@ int main(int argc, char **argv)\n \tstruct option options[] = {\n \t\tOPT_INTEGER(0, \"exit-after\", &idle_in_seconds,\n \t\t\t    N_(\"exit if not used after some seconds\")),\n+\t\tOPT_BOOL(0, \"strict\", &to_verify,\n+\t\t\t \"verify shared memory after creating\"),\n \t\tOPT_END()\n \t};\n \ndiff --git a/read-cache.c b/read-cache.c\nindex e3c8f3e..41647ea 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1674,9 +1674,12 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)\n \n \tistate->mmap = mmap;\n \tistate->mmap_size = mmap_size;\n-\tif (try_shm(istate) &&\n-\t    verify_hdr(istate->mmap, istate->mmap_size) < 0)\n-\t\tgoto unmap;\n+\tif (try_shm(istate)) {\n+\t\tif (verify_hdr(istate->mmap, istate->mmap_size) < 0)\n+\t\t\tgoto unmap;\n+\t} else if (istate->always_verify_trailing_sha1 &&\n+\t\t   verify_hdr(istate->mmap, istate->mmap_size) < 0)\n+\t\t\tgoto unmap;\n \thdr = mmap = istate->mmap;\n \tmmap_size = istate->mmap_size;\n \tif (!istate->keep_mmap)\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287041","messageId":"1463694357-6503-7-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 06/20] daemonize(): set a flag before exiting the main process","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:43Z","receivedAt":"2016-05-19T21:45:43Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nThis allows signal handlers and atexit functions to realize this\nsituation and not clean up.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n builtin/gc.c | 2 +-\n cache.h      | 2 +-\n daemon.c     | 2 +-\n setup.c      | 4 +++-\n 4 files changed, 6 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/gc.c b/builtin/gc.c\nindex c583aad..37180de 100644\n--- a/builtin/gc.c\n+++ b/builtin/gc.c\n@@ -385,7 +385,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)\n \t\t\t * failure to daemonize is ok, we'll continue\n \t\t\t * in foreground\n \t\t\t */\n-\t\t\tdaemonized = !daemonize();\n+\t\t\tdaemonized = !daemonize(NULL);\n \t\t}\n \t} else\n \t\tadd_repack_all_option();\ndiff --git a/cache.h b/cache.h\nindex 6cb0d02..4c1529a 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -539,7 +539,7 @@ extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);\n extern int init_db(const char *template_dir, unsigned int flags);\n \n extern void sanitize_stdfds(void);\n-extern int daemonize(void);\n+extern int daemonize(int *);\n \n #define alloc_nr(x) (((x)+16)*3/2)\n \ndiff --git a/daemon.c b/daemon.c\nindex 8d45c33..a5cf954 100644\n--- a/daemon.c\n+++ b/daemon.c\n@@ -1365,7 +1365,7 @@ int main(int argc, char **argv)\n \t\treturn execute();\n \n \tif (detach) {\n-\t\tif (daemonize())\n+\t\tif (daemonize(NULL))\n \t\t\tdie(\"--detach not supported on this platform\");\n \t} else\n \t\tsanitize_stdfds();\ndiff --git a/setup.c b/setup.c\nindex de1a2a7..9adf13f 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1017,7 +1017,7 @@ void sanitize_stdfds(void)\n \t\tclose(fd);\n }\n \n-int daemonize(void)\n+int daemonize(int *daemonized)\n {\n #ifdef NO_POSIX_GOODIES\n \terrno = ENOSYS;\n@@ -1029,6 +1029,8 @@ int daemonize(void)\n \t\tcase -1:\n \t\t\tdie_errno(\"fork failed\");\n \t\tdefault:\n+\t\t\tif (daemonized)\n+\t\t\t\t*daemonized = 1;\n \t\t\texit(0);\n \t}\n \tif (setsid() == -1)\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287031","messageId":"1463694357-6503-8-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 07/20] index-helper: add --detach","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:44Z","receivedAt":"2016-05-19T21:45:44Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nWe detach after creating and opening the socket, because otherwise\nwe might return control to the shell before index-helper is ready to\naccept commands.  This might lead to flaky tests.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/git-index-helper.txt | 3 +++\n index-helper.c                     | 9 +++++++--\n 2 files changed, 10 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nindex 1f92c89..f853960 100644\n--- a/Documentation/git-index-helper.txt\n+++ b/Documentation/git-index-helper.txt\n@@ -34,6 +34,9 @@ OPTIONS\n \tfor reading an index, but because it will happen in the\n \tbackground, it's not noticable. `--strict` is enabled by default.\n \n+--detach::\n+\tDetach from the shell.\n+\n NOTES\n -----\n \ndiff --git a/index-helper.c b/index-helper.c\nindex a7f8a42..6c8108f 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -17,7 +17,7 @@ struct shm {\n \n static struct shm shm_index;\n static struct shm shm_base_index;\n-static int to_verify = 1;\n+static int daemonized, to_verify = 1;\n \n static void release_index_shm(struct shm *is)\n {\n@@ -36,6 +36,8 @@ static void cleanup_shm(void)\n \n static void cleanup(void)\n {\n+\tif (daemonized)\n+\t\treturn;\n \tunlink(git_path(\"index-helper.sock\"));\n \tcleanup_shm();\n }\n@@ -290,7 +292,7 @@ static const char * const usage_text[] = {\n int main(int argc, char **argv)\n {\n \tconst char *prefix;\n-\tint idle_in_seconds = 600;\n+\tint idle_in_seconds = 600, detach = 0;\n \tint fd;\n \tstruct strbuf socket_path = STRBUF_INIT;\n \tstruct option options[] = {\n@@ -298,6 +300,7 @@ int main(int argc, char **argv)\n \t\t\t    N_(\"exit if not used after some seconds\")),\n \t\tOPT_BOOL(0, \"strict\", &to_verify,\n \t\t\t \"verify shared memory after creating\"),\n+\t\tOPT_BOOL(0, \"detach\", &detach, \"detach the process\"),\n \t\tOPT_END()\n \t};\n \n@@ -321,6 +324,8 @@ int main(int argc, char **argv)\n \tif (fd < 0)\n \t\tdie_errno(_(\"could not set up index-helper socket\"));\n \n+\tif (detach && daemonize(&daemonized))\n+\t\tdie_errno(_(\"unable to detach\"));\n \tloop(fd, idle_in_seconds);\n \n \tclose(fd);\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287030","messageId":"1463694357-6503-9-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 08/20] index-helper: log warnings","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:45Z","receivedAt":"2016-05-19T21:45:45Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Instead of writing warnings to stderr, write them to a log.  Later, we'll\nprobably be daemonized, so writing to stderr will be pointless.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/git-index-helper.txt |  3 +++\n index-helper.c                     | 12 +++++++++++-\n 2 files changed, 14 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nindex f853960..e144752 100644\n--- a/Documentation/git-index-helper.txt\n+++ b/Documentation/git-index-helper.txt\n@@ -57,6 +57,9 @@ command. The following commands are used to control the daemon:\n \n All commands and replies are terminated by a NUL byte.\n \n+In the event of an error, messages may be written to\n+$GIT_DIR/index-helper.log.\n+\n GIT\n ---\n Part of the linkgit:git[1] suite\ndiff --git a/index-helper.c b/index-helper.c\nindex 6c8108f..e99109a 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -95,7 +95,8 @@ static void share_index(struct index_state *istate, struct shm *is)\n \tif (shared_mmap_create(istate->mmap_size, &new_mmap,\n \t\t\t       git_path(\"shm-index-%s\",\n \t\t\t\t\tsha1_to_hex(istate->sha1))) < 0) {\n-\t\tdie(\"Failed to create shm-index file\");\n+\t\twarning(\"Failed to create shm-index file\");\n+\t\texit(1);\n \t}\n \n \n@@ -324,8 +325,17 @@ int main(int argc, char **argv)\n \tif (fd < 0)\n \t\tdie_errno(_(\"could not set up index-helper socket\"));\n \n+\tif (detach) {\n+\t\tFILE *fp = fopen(git_path(\"index-helper.log\"), \"a\");\n+\t\tif (!fp)\n+\t\t\tdie(\"failed to open %s for writing\",\n+\t\t\t    git_path(\"index-helper.log\"));\n+\t\tset_error_handle(fp);\n+\t}\n+\n \tif (detach && daemonize(&daemonized))\n \t\tdie_errno(_(\"unable to detach\"));\n+\n \tloop(fd, idle_in_seconds);\n \n \tclose(fd);\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287044","messageId":"1463694357-6503-10-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 09/20] read-cache: add watchman 'WAMA' extension","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:46Z","receivedAt":"2016-05-19T21:45:46Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nThe extension contains a bitmap, one bit for each entry in the\nindex. If the n-th bit is zero, the n-th entry is considered\nunchanged, we can ce_mark_uptodate() it without refreshing. If the bit\nis non-zero and we found out the corresponding file is clean after\nrefresh, we can clear the bit.\n\nIn addition, there's a list of directories in the untracked-cache\nto invalidate (because they have new or modified entries).\n\nThe 'skipping refresh' bit is not in this patch yet as we would need\nwatchman. More details in later patches.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/technical/index-format.txt |  22 ++++++\n cache.h                                  |   4 ++\n dir.h                                    |   3 +\n read-cache.c                             | 117 ++++++++++++++++++++++++++++++-\n 4 files changed, 144 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/technical/index-format.txt b/Documentation/technical/index-format.txt\nindex ade0b0c..86ed3a6 100644\n--- a/Documentation/technical/index-format.txt\n+++ b/Documentation/technical/index-format.txt\n@@ -295,3 +295,25 @@ The remaining data of each directory block is grouped by type:\n     in the previous ewah bitmap.\n \n   - One NUL.\n+\n+== Watchman cache\n+\n+  The watchman cache tracks files for which watchman has told us about\n+  changes.  The signature for this extension is { 'W', 'A', 'M', 'A' }.\n+\n+  The extension starts with\n+\n+  - A NUL-terminated string: the watchman vector clock at the last\n+    time we heard from watchman.\n+\n+  - 32-bit bitmap size: the size of the CE_WATCHMAN_DIRTY bitmap\n+\n+  - 32-bit untracked cache entry count: the number of dirty untracked\n+    cache entries\n+\n+  - An ewah bitmap, the n-th bit indicates whether the n-th index entry\n+    is CE_WATCHMAN_DIRTY.\n+\n+  - a list of N NUL-terminated strings.  Each is a directory that should\n+    be marked dirty in the untracked cache because watchman has told us\n+    about an update to a file in it.\ndiff --git a/cache.h b/cache.h\nindex 4c1529a..f10992d 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -182,6 +182,8 @@ struct cache_entry {\n #define CE_VALID     (0x8000)\n #define CE_STAGESHIFT 12\n \n+#define CE_WATCHMAN_DIRTY  (0x0001)\n+\n /*\n  * Range 0xFFFF0FFF in ce_flags is divided into\n  * two parts: in-memory flags and on-disk ones.\n@@ -320,6 +322,7 @@ static inline unsigned int canon_mode(unsigned int mode)\n #define CACHE_TREE_CHANGED\t(1 << 5)\n #define SPLIT_INDEX_ORDERED\t(1 << 6)\n #define UNTRACKED_CHANGED\t(1 << 7)\n+#define WATCHMAN_CHANGED\t(1 << 8)\n \n struct split_index;\n struct untracked_cache;\n@@ -353,6 +356,7 @@ struct index_state {\n \tstruct untracked_cache *untracked;\n \tvoid *mmap;\n \tsize_t mmap_size;\n+\tchar *last_update;\n };\n \n extern struct index_state the_index;\ndiff --git a/dir.h b/dir.h\nindex 3ec3fb0..3d540de 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -142,6 +142,9 @@ struct untracked_cache {\n \tint gitignore_invalidated;\n \tint dir_invalidated;\n \tint dir_opened;\n+\t/* watchman invalidation data */\n+\tunsigned int use_watchman : 1;\n+\tstruct string_list invalid_untracked;\n };\n \n struct dir_struct {\ndiff --git a/read-cache.c b/read-cache.c\nindex 41647ea..1719f5a 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -21,6 +21,7 @@\n #include \"unix-socket.h\"\n #include \"pkt-line.h\"\n #include \"sigchain.h\"\n+#include \"ewah/ewok.h\"\n \n static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \t\t\t\t\t       unsigned int options);\n@@ -43,11 +44,13 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n #define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* \"REUC\" */\n #define CACHE_EXT_LINK 0x6c696e6b\t  /* \"link\" */\n #define CACHE_EXT_UNTRACKED 0x554E5452\t  /* \"UNTR\" */\n+#define CACHE_EXT_WATCHMAN 0x57414D41\t  /* \"WAMA\" */\n \n /* changes that can be kept in $GIT_DIR/index (basically all extensions) */\n #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \\\n \t\t CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \\\n-\t\t SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED)\n+\t\t SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | \\\n+\t\t WATCHMAN_CHANGED)\n \n struct index_state the_index;\n static const char *alternate_index_output;\n@@ -1222,8 +1225,13 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n \t\t\tcontinue;\n \n \t\tnew = refresh_cache_ent(istate, ce, options, &cache_errno, &changed);\n-\t\tif (new == ce)\n+\t\tif (new == ce) {\n+\t\t\tif (ce->ce_flags & CE_WATCHMAN_DIRTY) {\n+\t\t\t\tce->ce_flags          &= ~CE_WATCHMAN_DIRTY;\n+\t\t\t\tistate->cache_changed |= WATCHMAN_CHANGED;\n+\t\t\t}\n \t\t\tcontinue;\n+\t\t}\n \t\tif (!new) {\n \t\t\tconst char *fmt;\n \n@@ -1367,6 +1375,94 @@ static int verify_hdr(const struct cache_header *hdr, unsigned long size)\n \treturn 0;\n }\n \n+static void mark_no_watchman(size_t pos, void *data)\n+{\n+\tstruct index_state *istate = data;\n+\tassert(pos < istate->cache_nr);\n+\tistate->cache[pos]->ce_flags |= CE_WATCHMAN_DIRTY;\n+}\n+\n+static int read_watchman_ext(struct index_state *istate, const void *data,\n+\t\t\t     unsigned long sz)\n+{\n+\tstruct ewah_bitmap *bitmap;\n+\tint ret, len;\n+\tuint32_t bitmap_size;\n+\tuint32_t untracked_nr;\n+\n+\tif (memchr(data, 0, sz) == NULL)\n+\t\treturn error(\"invalid extension\");\n+\n+\tlen = strlen(data) + 1;\n+\tmemcpy(&bitmap_size, (const char *)data + len, 4);\n+\tmemcpy(&untracked_nr, (const char *)data + len + 4, 4);\n+\tuntracked_nr = ntohl(untracked_nr);\n+\tbitmap_size = ntohl(bitmap_size);\n+\n+\tbitmap = ewah_new();\n+\tret = ewah_read_mmap(bitmap, (const char *)data + len + 8, bitmap_size);\n+\tif (ret != bitmap_size) {\n+\t\tewah_free(bitmap);\n+\t\treturn error(\"failed to parse ewah bitmap reading watchman index extension\");\n+\t}\n+\tistate->last_update = xstrdup(data);\n+\tewah_each_bit(bitmap, mark_no_watchman, istate);\n+\tewah_free(bitmap);\n+\n+\t/*\n+\t * TODO: update the untracked cache from the untracked data in this\n+\t * extension.\n+\t */\n+\treturn 0;\n+}\n+\n+static int untracked_entry_append(struct string_list_item *item, void *sbvoid)\n+{\n+\tstruct strbuf *sb = sbvoid;\n+\n+\tstrbuf_addstr(sb, item->string);\n+\tstrbuf_addch(sb, 0);\n+\treturn 0;\n+}\n+\n+void write_watchman_ext(struct strbuf *sb, struct index_state *istate)\n+{\n+\tstruct ewah_bitmap *bitmap;\n+\tint i;\n+\tint ewah_start;\n+\tint ewah_size = 0;\n+\tint fixup = 0;\n+\n+\tstrbuf_add(sb, istate->last_update, strlen(istate->last_update) + 1);\n+\tfixup = sb->len;\n+\tstrbuf_add(sb, &ewah_size, 4); /* we'll fix this up later */\n+\tif (istate->untracked) {\n+\t\tuint32_t nr = istate->untracked->invalid_untracked.nr;\n+\t\tnr = htonl(nr);\n+\t\tstrbuf_add(sb, &nr, 4);\n+\t} else {\n+\t\t/* zero */\n+\t\tstrbuf_add(sb, &ewah_size, 4);\n+\t}\n+\n+\tewah_start = sb->len;\n+\tbitmap = ewah_new();\n+\tfor (i = 0; i < istate->cache_nr; i++)\n+\t\tif (istate->cache[i]->ce_flags & CE_WATCHMAN_DIRTY)\n+\t\t\tewah_set(bitmap, i);\n+\tewah_serialize_strbuf(bitmap, sb);\n+\tewah_free(bitmap);\n+\n+\t/* fix up size field */\n+\tewah_size = sb->len - ewah_start;\n+\tewah_size = htonl(ewah_size);\n+\tmemcpy(sb->buf + fixup, &ewah_size, 4);\n+\n+\tif (istate->untracked)\n+\t\tfor_each_string_list(&istate->untracked->invalid_untracked,\n+\t\t\t\t     untracked_entry_append, sb);\n+}\n+\n static int read_index_extension(struct index_state *istate,\n \t\t\t\tconst char *ext, void *data, unsigned long sz)\n {\n@@ -1384,6 +1480,11 @@ static int read_index_extension(struct index_state *istate,\n \tcase CACHE_EXT_UNTRACKED:\n \t\tistate->untracked = read_untracked_extension(data, sz);\n \t\tbreak;\n+\n+\tcase CACHE_EXT_WATCHMAN:\n+\t\tread_watchman_ext(istate, data, sz);\n+\t\tbreak;\n+\n \tdefault:\n \t\tif (*ext < 'A' || 'Z' < *ext)\n \t\t\treturn error(\"index uses %.4s extension, which we do not understand\",\n@@ -1817,6 +1918,8 @@ int discard_index(struct index_state *istate)\n \tistate->untracked = NULL;\n \tistate->from_shm = 0;\n \tistate->to_shm   = 0;\n+\tfree(istate->last_update);\n+\tistate->last_update = NULL;\n \treturn 0;\n }\n \n@@ -2214,6 +2317,16 @@ static int do_write_index(struct index_state *istate, int newfd,\n \t\tif (err)\n \t\t\treturn -1;\n \t}\n+\tif (!strip_extensions && istate->last_update) {\n+\t\tstruct strbuf sb = STRBUF_INIT;\n+\n+\t\twrite_watchman_ext(&sb, istate);\n+\t\terr = write_index_ext_header(&c, newfd, CACHE_EXT_WATCHMAN, sb.len) < 0\n+\t\t\t|| ce_write(&c, newfd, sb.buf, sb.len) < 0;\n+\t\tstrbuf_release(&sb);\n+\t\tif (err)\n+\t\t\treturn -1;\n+\t}\n \n \tif (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))\n \t\treturn -1;\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287032","messageId":"1463694357-6503-11-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 10/20] watchman: support watchman to reduce index refresh cost","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:47Z","receivedAt":"2016-05-19T21:45:47Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nThe previous patch has the logic to clear bits in 'WAMA' bitmap. This\npatch has logic to set bits as told by watchman. The missing bit,\n_using_ these bits, are not here yet.\n\nA lot of this code is written by David Turner originally, mostly from\n[1]. I'm just copying and polishing it a bit.\n\n[1] http://article.gmane.org/gmane.comp.version-control.git/248006\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Makefile           |  12 +++++\n cache.h            |   1 +\n config.c           |   5 ++\n configure.ac       |   8 ++++\n environment.c      |   3 ++\n watchman-support.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n watchman-support.h |   7 +++\n 7 files changed, 171 insertions(+)\n create mode 100644 watchman-support.c\n create mode 100644 watchman-support.h\n\ndiff --git a/Makefile b/Makefile\nindex c8be0e7..65ab0f4 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -451,6 +451,7 @@ MSGFMT = msgfmt\n CURL_CONFIG = curl-config\n PTHREAD_LIBS = -lpthread\n PTHREAD_CFLAGS =\n+WATCHMAN_LIBS =\n GCOV = gcov\n \n export TCL_PATH TCLTK_PATH\n@@ -1416,6 +1417,13 @@ else\n \tLIB_OBJS += thread-utils.o\n endif\n \n+ifdef USE_WATCHMAN\n+\tLIB_H += watchman-support.h\n+\tLIB_OBJS += watchman-support.o\n+\tWATCHMAN_LIBS = -lwatchman\n+\tBASIC_CFLAGS += -DUSE_WATCHMAN\n+endif\n+\n ifdef HAVE_PATHS_H\n \tBASIC_CFLAGS += -DHAVE_PATHS_H\n endif\n@@ -2025,6 +2033,9 @@ git-remote-testsvn$X: remote-testsvn.o GIT-LDFLAGS $(GITLIBS) $(VCSSVN_LIB)\n \t$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) \\\n \t$(VCSSVN_LIB)\n \n+git-index-helper$X: index-helper.o GIT-LDFLAGS $(GITLIBS)\n+\t$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) $(WATCHMAN_LIBS)\n+\n $(REMOTE_CURL_ALIASES): $(REMOTE_CURL_PRIMARY)\n \t$(QUIET_LNCP)$(RM) $@ && \\\n \tln $< $@ 2>/dev/null || \\\n@@ -2164,6 +2175,7 @@ GIT-BUILD-OPTIONS: FORCE\n \t@echo NO_PYTHON=\\''$(subst ','\\'',$(subst ','\\'',$(NO_PYTHON)))'\\' >>$@+\n \t@echo NO_UNIX_SOCKETS=\\''$(subst ','\\'',$(subst ','\\'',$(NO_UNIX_SOCKETS)))'\\' >>$@+\n \t@echo NO_MMAP=\\''$(subst ','\\'',$(subst ','\\'',$(NO_MMAP)))'\\' >>$@+\n+\t@echo USE_WATCHMAN=\\''$(subst ','\\'',$(subst ','\\'',$(USE_WATCHMAN)))'\\' >>$@+\n ifdef TEST_OUTPUT_DIRECTORY\n \t@echo TEST_OUTPUT_DIRECTORY=\\''$(subst ','\\'',$(subst ','\\'',$(TEST_OUTPUT_DIRECTORY)))'\\' >>$@+\n endif\ndiff --git a/cache.h b/cache.h\nindex f10992d..452aea2 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -696,6 +696,7 @@ extern char *git_replace_ref_base;\n \n extern int fsync_object_files;\n extern int core_preload_index;\n+extern int core_watchman_sync_timeout;\n extern int core_apply_sparse_checkout;\n extern int precomposed_unicode;\n extern int protect_hfs;\ndiff --git a/config.c b/config.c\nindex 9ba40bc..e6dc141 100644\n--- a/config.c\n+++ b/config.c\n@@ -882,6 +882,11 @@ static int git_default_core_config(const char *var, const char *value)\n \t\treturn 0;\n \t}\n \n+\tif (!strcmp(var, \"core.watchmansynctimeout\")) {\n+\t\tcore_watchman_sync_timeout = git_config_int(var, value);\n+\t\treturn 0;\n+\t}\n+\n \tif (!strcmp(var, \"core.createobject\")) {\n \t\tif (!strcmp(value, \"rename\"))\n \t\t\tobject_creation_mode = OBJECT_CREATION_USES_RENAMES;\ndiff --git a/configure.ac b/configure.ac\nindex 0cd9f46..334d63b 100644\n--- a/configure.ac\n+++ b/configure.ac\n@@ -1099,6 +1099,14 @@ AC_COMPILE_IFELSE([BSD_SYSCTL_SRC],\n \tHAVE_BSD_SYSCTL=])\n GIT_CONF_SUBST([HAVE_BSD_SYSCTL])\n \n+#\n+# Check for watchman client library\n+\n+AC_CHECK_LIB([watchman], [watchman_connect],\n+\t[USE_WATCHMAN=YesPlease],\n+\t[USE_WATCHMAN=])\n+GIT_CONF_SUBST([USE_WATCHMAN])\n+\n ## Other checks.\n # Define USE_PIC if you need the main git objects to be built with -fPIC\n # in order to build and link perl/Git.so.  x86-64 seems to need this.\ndiff --git a/environment.c b/environment.c\nindex 6dec9d0..35e03c7 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -94,6 +94,9 @@ int core_preload_index = 1;\n  */\n int ignore_untracked_cache_config;\n \n+int core_watchman_sync_timeout = 300;\n+\n+\n /* This is set by setup_git_dir_gently() and/or git_default_config() */\n char *git_work_tree_cfg;\n static char *work_tree;\ndiff --git a/watchman-support.c b/watchman-support.c\nnew file mode 100644\nindex 0000000..dc8cd51\n--- /dev/null\n+++ b/watchman-support.c\n@@ -0,0 +1,135 @@\n+#include \"cache.h\"\n+#include \"watchman-support.h\"\n+#include \"strbuf.h\"\n+#include \"dir.h\"\n+#include <watchman.h>\n+\n+static struct watchman_query *make_query(const char *last_update)\n+{\n+\tstruct watchman_query *query = watchman_query();\n+\twatchman_query_set_fields(query, WATCHMAN_FIELD_NAME |\n+\t\t\t\t\t WATCHMAN_FIELD_EXISTS |\n+\t\t\t\t\t WATCHMAN_FIELD_NEWER);\n+\twatchman_query_set_empty_on_fresh(query, 1);\n+\tquery->sync_timeout = core_watchman_sync_timeout;\n+\tif (*last_update)\n+\t\twatchman_query_set_since_oclock(query, last_update);\n+\treturn query;\n+}\n+\n+static struct watchman_query_result *query_watchman(\n+\tstruct index_state *istate, struct watchman_connection *connection,\n+\tconst char *fs_path, const char *last_update)\n+{\n+\tstruct watchman_error wm_error;\n+\tstruct watchman_query *query;\n+\tstruct watchman_expression *expr;\n+\tstruct watchman_query_result *result;\n+\n+\tquery = make_query(last_update);\n+\texpr = watchman_true_expression();\n+\tresult = watchman_do_query(connection, fs_path, query, expr, &wm_error);\n+\twatchman_free_query(query);\n+\twatchman_free_expression(expr);\n+\n+\tif (!result)\n+\t\twarning(\"Watchman query error: %s (at %s)\",\n+\t\t\twm_error.message,\n+\t\t\t*last_update ? last_update : \"the beginning\");\n+\n+\treturn result;\n+}\n+\n+static void update_index(struct index_state *istate,\n+\t\t\t struct watchman_query_result *result)\n+{\n+\tint i;\n+\n+\tif (result->is_fresh_instance) {\n+\t\t/* let refresh clear them later */\n+\t\tfor (i = 0; i < istate->cache_nr; i++)\n+\t\t\tistate->cache[i]->ce_flags |= CE_WATCHMAN_DIRTY;\n+\t\tgoto done;\n+\t}\n+\n+\tfor (i = 0; i < result->nr; i++) {\n+\t\tstruct watchman_stat *wm = result->stats + i;\n+\t\tint pos;\n+\n+\t\tif (S_ISDIR(wm->mode) ||\n+\t\t    !strncmp(wm->name, \".git/\", 5) ||\n+\t\t    strstr(wm->name, \"/.git/\"))\n+\t\t\tcontinue;\n+\n+\t\tpos = index_name_pos(istate, wm->name, strlen(wm->name));\n+\t\tif (pos < 0) {\n+\t\t\tif (istate->untracked) {\n+\t\t\t\tchar *name = xstrdup(wm->name);\n+\t\t\t\tchar *dname = dirname(name);\n+\n+\t\t\t\t/*\n+\t\t\t\t * dirname() returns '.' for the root,\n+\t\t\t\t * but we call it ''.\n+\t\t\t\t */\n+\t\t\t\tif (dname[0] == '.' && dname[1] == 0)\n+\t\t\t\t\tstring_list_append(&istate->untracked->invalid_untracked, \"\");\n+\t\t\t\telse\n+\t\t\t\t\tstring_list_append(&istate->untracked->invalid_untracked,\n+\t\t\t\t\t\t\t   dname);\n+\t\t\t\tfree(name);\n+\t\t\t}\n+\t\t\tcontinue;\n+\t\t}\n+\t\t/* FIXME: ignore staged entries and gitlinks too? */\n+\n+\t\tistate->cache[pos]->ce_flags |= CE_WATCHMAN_DIRTY;\n+\t}\n+\n+done:\n+\tfree(istate->last_update);\n+\tistate->last_update    = xstrdup(result->clock);\n+\tistate->cache_changed |= WATCHMAN_CHANGED;\n+\tif (istate->untracked)\n+\t\tstring_list_remove_duplicates(&istate->untracked->invalid_untracked, 0);\n+}\n+\n+int check_watchman(struct index_state *istate)\n+{\n+\tstruct watchman_error wm_error;\n+\tstruct watchman_connection *connection;\n+\tstruct watchman_query_result *result;\n+\tconst char *fs_path;\n+\tstruct timeval timeout;\n+\t/*\n+\t * Convert core_watchman_sync_timeout, in milliseconds, to\n+\t * struct timeval, in seconds and microseconds.\n+\t */\n+\n+\tfs_path = get_git_work_tree();\n+\tif (!fs_path)\n+\t\treturn -1;\n+\n+\ttimeout.tv_sec = core_watchman_sync_timeout / 1000;\n+\ttimeout.tv_usec = (core_watchman_sync_timeout % 1000) * 1000;\n+\tconnection = watchman_connect(timeout, &wm_error);\n+\n+\tif (!connection) {\n+\t\twarning(\"Watchman watch error: %s\", wm_error.message);\n+\t\treturn -1;\n+\t}\n+\n+\tif (watchman_watch(connection, fs_path, &wm_error)) {\n+\t\twarning(\"Watchman watch error: %s\", wm_error.message);\n+\t\twatchman_connection_close(connection);\n+\t\treturn -1;\n+\t}\n+\n+\n+\tresult = query_watchman(istate, connection, fs_path, istate->last_update);\n+\twatchman_connection_close(connection);\n+\tif (!result)\n+\t\treturn -1;\n+\tupdate_index(istate, result);\n+\twatchman_free_query_result(result);\n+\treturn 0;\n+}\ndiff --git a/watchman-support.h b/watchman-support.h\nnew file mode 100644\nindex 0000000..ee1ef2c\n--- /dev/null\n+++ b/watchman-support.h\n@@ -0,0 +1,7 @@\n+#ifndef WATCHMAN_SUPPORT_H\n+#define WATCHMAN_SUPPORT_H\n+\n+struct index_state;\n+int check_watchman(struct index_state *index);\n+\n+#endif /* WATCHMAN_SUPPORT_H */\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287040","messageId":"1463694357-6503-12-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 11/20] index-helper: use watchman to avoid refreshing index with lstat()","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:48Z","receivedAt":"2016-05-19T21:45:48Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nWatchman is hidden behind index-helper. Before git tries to read the\nindex from shm, it notifies index-helper through the socket and waits\nfor index-helper to prepare a file for sharing memory (with\nMAP_SHARED). index-helper then contacts watchman, updates 'WAMA'\nextension and put it in a separate file and wakes git up with a reply\nto git's socket.\n\nGit uses this extension to not lstat unchanged entries. Git only\ntrusts the 'WAMA' extension when it's received from the separate file,\nnot from disk. Unmarked entries are \"clean\". Marked entries are dirty\nfrom watchman point of view. If it finds out some entries are\n'watchman-dirty', but are really unchanged (e.g. the file was changed,\nthen reverted back), then Git will clear the marking in 'WAMA' before\nwriting it down.\n\nHiding watchman behind index-helper means you need both daemons. You\ncan't run watchman alone. Not so good. But on the other hand, 'git'\nbinary is not linked to watchman/json libraries, which is good for\npackaging. Core git package will run fine without watchman-related\npackages. If they need watchman, they can install git-index-helper and\ndependencies.\n\nThis also lets us trust anything in the untracked cache that we haven't\nmarked invalid, saving those stat() calls.\n\nAnother reason for tying watchman to index-helper is, when used with\nuntracked cache, we need to keep track of $GIT_WORK_TREE file\nlisting. That kind of list can be kept in index-helper.\n\nHelped-by: Ramsay Jones <ramsay@ramsayjones.plus.com>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/git-index-helper.txt |   6 +\n cache.h                            |   2 +\n dir.c                              |  23 +++-\n dir.h                              |   3 +\n index-helper.c                     | 107 ++++++++++++++--\n read-cache.c                       | 241 ++++++++++++++++++++++++++++++++++---\n 6 files changed, 355 insertions(+), 27 deletions(-)\n\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nindex e144752..cce00cb 100644\n--- a/Documentation/git-index-helper.txt\n+++ b/Documentation/git-index-helper.txt\n@@ -55,6 +55,12 @@ command. The following commands are used to control the daemon:\n \tLet the daemon know the index is to be read. It keeps the\n \tdaemon alive longer, unless `--exit-after=0` is used.\n \n+\"poke <path>\":\n+\tLike \"poke\", but replies with \"OK\".  If the index has the\n+\twatchman extension, index-helper queries watchman, then\n+\tprepares a shared memory object with the watchman index\n+\textension before replying.\n+\n All commands and replies are terminated by a NUL byte.\n \n In the event of an error, messages may be written to\ndiff --git a/cache.h b/cache.h\nindex 452aea2..633e1dd 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -567,6 +567,7 @@ extern int daemonize(int *);\n \n /* Initialize and use the cache information */\n struct lock_file;\n+extern int verify_index(const struct index_state *);\n extern int read_index(struct index_state *);\n extern int read_index_preload(struct index_state *, const struct pathspec *pathspec);\n extern int do_read_index(struct index_state *istate, const char *path,\n@@ -574,6 +575,7 @@ extern int do_read_index(struct index_state *istate, const char *path,\n extern int read_index_from(struct index_state *, const char *path);\n extern int is_index_unborn(struct index_state *);\n extern int read_index_unmerged(struct index_state *);\n+extern void write_watchman_ext(struct strbuf *sb, struct index_state *istate);\n #define COMMIT_LOCK\t\t(1 << 0)\n #define CLOSE_LOCK\t\t(1 << 1)\n extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);\ndiff --git a/dir.c b/dir.c\nindex 69e0be6..5058b29 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -597,9 +597,9 @@ static void trim_trailing_spaces(char *buf)\n  *\n  * If \"name\" has the trailing slash, it'll be excluded in the search.\n  */\n-static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,\n-\t\t\t\t\t\t    struct untracked_cache_dir *dir,\n-\t\t\t\t\t\t    const char *name, int len)\n+struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,\n+\t\t\t\t\t     struct untracked_cache_dir *dir,\n+\t\t\t\t\t     const char *name, int len)\n {\n \tint first, last;\n \tstruct untracked_cache_dir *d;\n@@ -1726,6 +1726,17 @@ static int valid_cached_dir(struct dir_struct *dir,\n \tif (!untracked)\n \t\treturn 0;\n \n+\tif (dir->untracked->use_watchman) {\n+\t\t/*\n+\t\t * With watchman, we can trust the untracked cache's\n+\t\t * valid field.\n+\t\t */\n+\t\tif (untracked->valid)\n+\t\t\tgoto skip_stat;\n+\t\telse\n+\t\t\tinvalidate_directory(dir->untracked, untracked);\n+\t}\n+\n \tif (stat(path->len ? path->buf : \".\", &st)) {\n \t\tinvalidate_directory(dir->untracked, untracked);\n \t\tmemset(&untracked->stat_data, 0, sizeof(untracked->stat_data));\n@@ -1739,6 +1750,7 @@ static int valid_cached_dir(struct dir_struct *dir,\n \t\treturn 0;\n \t}\n \n+skip_stat:\n \tif (untracked->check_only != !!check_only) {\n \t\tinvalidate_directory(dir->untracked, untracked);\n \t\treturn 0;\n@@ -2625,8 +2637,10 @@ static void free_untracked(struct untracked_cache_dir *ucd)\n \n void free_untracked_cache(struct untracked_cache *uc)\n {\n-\tif (uc)\n+\tif (uc) {\n \t\tfree_untracked(uc->root);\n+\t\tstring_list_clear(&uc->invalid_untracked, 0);\n+\t}\n \tfree(uc);\n }\n \n@@ -2775,6 +2789,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long\n \t\treturn NULL;\n \n \tuc = xcalloc(1, sizeof(*uc));\n+\tstring_list_init(&uc->invalid_untracked, 1);\n \tstrbuf_init(&uc->ident, ident_len);\n \tstrbuf_add(&uc->ident, ident, ident_len);\n \tload_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,\ndiff --git a/dir.h b/dir.h\nindex 3d540de..8fd3f9e 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -315,4 +315,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long\n void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);\n void add_untracked_cache(struct index_state *istate);\n void remove_untracked_cache(struct index_state *istate);\n+struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,\n+\t\t\t\t\t     struct untracked_cache_dir *dir,\n+\t\t\t\t\t     const char *name, int len);\n #endif\ndiff --git a/index-helper.c b/index-helper.c\nindex e99109a..d3a1f39 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -8,15 +8,18 @@\n #include \"cache.h\"\n #include \"unix-socket.h\"\n #include \"pkt-line.h\"\n+#include \"watchman-support.h\"\n \n struct shm {\n \tunsigned char sha1[20];\n \tvoid *shm;\n \tsize_t size;\n+\tpid_t pid;\n };\n \n static struct shm shm_index;\n static struct shm shm_base_index;\n+static struct shm shm_watchman;\n static int daemonized, to_verify = 1;\n \n static void release_index_shm(struct shm *is)\n@@ -28,10 +31,21 @@ static void release_index_shm(struct shm *is)\n \tis->shm = NULL;\n }\n \n+static void release_watchman_shm(struct shm *is)\n+{\n+\tif (!is->shm)\n+\t\treturn;\n+\tmunmap(is->shm, is->size);\n+\tunlink(git_path(\"shm-watchman-%s-%\" PRIuMAX,\n+\t\t\tsha1_to_hex(is->sha1), (uintmax_t)is->pid));\n+\tis->shm = NULL;\n+}\n+\n static void cleanup_shm(void)\n {\n \trelease_index_shm(&shm_index);\n \trelease_index_shm(&shm_base_index);\n+\trelease_watchman_shm(&shm_watchman);\n }\n \n static void cleanup(void)\n@@ -166,9 +180,10 @@ static void share_the_index(void)\n \tif (the_index.split_index && the_index.split_index->base)\n \t\tshare_index(the_index.split_index->base, &shm_base_index);\n \tshare_index(&the_index, &shm_index);\n-\tif (to_verify && !verify_shm())\n+\tif (to_verify && !verify_shm()) {\n \t\tcleanup_shm();\n-\tdiscard_index(&the_index);\n+\t\tdiscard_index(&the_index);\n+\t}\n }\n \n static void set_socket_blocking_flag(int fd, int make_nonblocking)\n@@ -201,6 +216,80 @@ static void refresh(void)\n \n #ifndef NO_MMAP\n \n+#ifdef USE_WATCHMAN\n+static void share_watchman(struct index_state *istate,\n+\t\t\t   struct shm *is, pid_t pid)\n+{\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tvoid *shm;\n+\n+\twrite_watchman_ext(&sb, istate);\n+\tif (!shared_mmap_create(sb.len + 20, &shm,\n+\t\t\t\tgit_path(\"shm-watchman-%s-%\" PRIuMAX,\n+\t\t\t\t\t sha1_to_hex(istate->sha1),\n+\t\t\t\t\t (uintmax_t)pid))) {\n+\t\tis->size = sb.len + 20;\n+\t\tis->shm = shm;\n+\t\tis->pid = pid;\n+\t\thashcpy(is->sha1, istate->sha1);\n+\n+\t\tmemcpy(shm, sb.buf, sb.len);\n+\t\thashcpy((unsigned char *)shm + is->size - 20, is->sha1);\n+\t}\n+\tstrbuf_release(&sb);\n+}\n+\n+\n+static void prepare_with_watchman(pid_t pid)\n+{\n+\t/*\n+\t * TODO: with the help of watchman, maybe we could detect if\n+\t * $GIT_DIR/index is updated.\n+\t */\n+\tif (!verify_index(&the_index))\n+\t\trefresh();\n+\n+\tif (check_watchman(&the_index))\n+\t\treturn;\n+\n+\tshare_watchman(&the_index, &shm_watchman, pid);\n+}\n+\n+static void prepare_index(pid_t pid)\n+{\n+\tif (shm_index.shm == NULL)\n+\t\trefresh();\n+\trelease_watchman_shm(&shm_watchman);\n+\tif (the_index.last_update)\n+\t\tprepare_with_watchman(pid);\n+}\n+\n+#endif\n+\n+static void reply_to_poke(int client_fd, const char *pid_buf)\n+{\n+\tchar *capabilities;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\n+#ifdef USE_WATCHMAN\n+\tpid_t client_pid = strtoull(pid_buf, NULL, 10);\n+\n+\tprepare_index(client_pid);\n+#endif\n+\tcapabilities = strchr(pid_buf, ' ');\n+\n+\tif (capabilities && !strcmp(capabilities, \" watchman\"))\n+#ifdef USE_WATCHMAN\n+\t\tpacket_buf_write(&sb, \"OK watchman\");\n+#else\n+\t\tpacket_buf_write(&sb, \"NAK watchman\");\n+#endif\n+\telse\n+\t\tpacket_buf_write(&sb, \"OK\");\n+\tif (write_in_full(client_fd, sb.buf, sb.len) != sb.len)\n+\t\twarning(_(\"client write failed\"));\n+}\n+\n static void loop(int fd, int idle_in_seconds)\n {\n \tassert(idle_in_seconds < INT_MAX / 1000);\n@@ -255,11 +344,15 @@ static void loop(int fd, int idle_in_seconds)\n \t\t\tbuf[bytes_read] = 0;\n \t\t\tif (!strcmp(buf, \"refresh\")) {\n \t\t\t\trefresh();\n-\t\t\t} else if (!strcmp(buf, \"poke\")) {\n-\t\t\t\t/*\n-\t\t\t\t * Just a poke to keep us\n-\t\t\t\t * alive, nothing to do.\n-\t\t\t\t */\n+\t\t\t} else if (starts_with(buf, \"poke\")) {\n+\t\t\t\tif (buf[4] == ' ') {\n+\t\t\t\t\treply_to_poke(client_fd, buf + 5);\n+\t\t\t\t} else {\n+\t\t\t\t\t/*\n+\t\t\t\t\t * Just a poke to keep us\n+\t\t\t\t\t * alive, nothing to do.\n+\t\t\t\t\t */\n+\t\t\t\t}\n \t\t\t} else {\n \t\t\t\twarning(\"BUG: Bogus command %s\", buf);\n \t\t\t}\ndiff --git a/read-cache.c b/read-cache.c\nindex 1719f5a..8ec4be3 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1235,7 +1235,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n \t\tif (!new) {\n \t\t\tconst char *fmt;\n \n-\t\t\tif (really && cache_errno == EINVAL) {\n+\t\t\tif (really || cache_errno == EINVAL) {\n \t\t\t\t/* If we are doing --really-refresh that\n \t\t\t\t * means the index is not valid anymore.\n \t\t\t\t */\n@@ -1375,11 +1375,75 @@ static int verify_hdr(const struct cache_header *hdr, unsigned long size)\n \treturn 0;\n }\n \n+static struct untracked_cache_dir *find_untracked_cache_dir(\n+\tstruct untracked_cache *uc, struct untracked_cache_dir *ucd,\n+\tconst char *name)\n+{\n+\tint component_len;\n+\tconst char *end;\n+\tstruct untracked_cache_dir *dir;\n+\n+\tif (!*name)\n+\t\treturn ucd;\n+\n+\tend = strchr(name, '/');\n+\tif (end)\n+\t\tcomponent_len = end - name;\n+\telse\n+\t\tcomponent_len = strlen(name);\n+\n+\tdir = lookup_untracked(uc, ucd, name, component_len);\n+\tif (dir)\n+\t\treturn find_untracked_cache_dir(uc, dir, name + component_len + 1);\n+\n+\treturn NULL;\n+}\n+\n static void mark_no_watchman(size_t pos, void *data)\n {\n \tstruct index_state *istate = data;\n+\tstruct cache_entry *ce = istate->cache[pos];\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tchar *c;\n+\tstruct untracked_cache_dir *dir;\n+\n \tassert(pos < istate->cache_nr);\n-\tistate->cache[pos]->ce_flags |= CE_WATCHMAN_DIRTY;\n+\tce->ce_flags |= CE_WATCHMAN_DIRTY;\n+\n+\tif (!istate->untracked || !istate->untracked->root)\n+\t\treturn;\n+\n+\tstrbuf_add(&sb, ce->name, ce_namelen(ce));\n+\n+\tfor (c = sb.buf + sb.len - 1; c > sb.buf; c--) {\n+\t\tif (*c == '/') {\n+\t\t\tstrbuf_setlen(&sb, c - sb.buf);\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (c == sb.buf)\n+\t\tstrbuf_setlen(&sb, 0);\n+\n+\tdir = find_untracked_cache_dir(istate->untracked,\n+\t\t\t\t       istate->untracked->root, sb.buf);\n+\tif (dir)\n+\t\tdir->valid = 0;\n+\n+\tstrbuf_release(&sb);\n+}\n+\n+static int mark_untracked_invalid(struct string_list_item *item, void *uc)\n+{\n+\tstruct untracked_cache *untracked = uc;\n+\tstruct untracked_cache_dir *dir;\n+\n+\tdir = find_untracked_cache_dir(untracked, untracked->root,\n+\t\t\t\t       item->string);\n+\tif (dir)\n+\t\tdir->valid = 0;\n+\n+\treturn 0;\n }\n \n static int read_watchman_ext(struct index_state *istate, const void *data,\n@@ -1409,10 +1473,24 @@ static int read_watchman_ext(struct index_state *istate, const void *data,\n \tewah_each_bit(bitmap, mark_no_watchman, istate);\n \tewah_free(bitmap);\n \n-\t/*\n-\t * TODO: update the untracked cache from the untracked data in this\n-\t * extension.\n-\t */\n+\tif (istate->untracked && istate->untracked->root) {\n+\t\tint i;\n+\t\tconst char *untracked;\n+\n+\t\tuntracked = (const char *)data + len + 8 + bitmap_size;\n+\t\tfor (i = 0; i < untracked_nr; ++i) {\n+\t\t\tint len = strlen(untracked);\n+\t\t\tstring_list_append(&istate->untracked->invalid_untracked,\n+\t\t\t\t\t   untracked);\n+\t\t\tuntracked += len + 1;\n+\t\t}\n+\n+\t\tfor_each_string_list(&istate->untracked->invalid_untracked,\n+\t\t\t mark_untracked_invalid, istate->untracked);\n+\n+\t\tif (untracked_nr)\n+\t\t\tistate->cache_changed |= WATCHMAN_CHANGED;\n+\t}\n \treturn 0;\n }\n \n@@ -1645,29 +1723,88 @@ static void post_read_index_from(struct index_state *istate)\n \ttweak_untracked_cache(istate);\n }\n \n+/* in ms */\n+#define WATCHMAN_TIMEOUT 1000\n+\n+static int poke_and_wait_for_reply(int fd)\n+{\n+\tint ret = -1;\n+\tstruct pollfd pollfd;\n+\tint bytes_read;\n+\tchar reply_buf[4096];\n+\tconst char *requested_capabilities = \"\";\n+\n+#ifdef USE_WATCHMAN\n+\trequested_capabilities = \"watchman\";\n+#endif\n+\n+\tif (fd < 0)\n+\t\treturn -1;\n+\n+\tif (packet_write_gently(fd, \"poke %d %s\", getpid(), requested_capabilities))\n+\t\treturn -1;\n+\tif (packet_flush_gently(fd))\n+\t\treturn -1;\n+\n+\t/* Now wait for a reply */\n+\tpollfd.fd = fd;\n+\tpollfd.events = POLLIN;\n+\tif (poll(&pollfd, 1, WATCHMAN_TIMEOUT) <= 0)\n+\t\t/* No reply or error, giving up */\n+\t\tgoto done_poke;\n+\n+\tbytes_read = packet_read(fd, NULL, NULL, reply_buf, sizeof(reply_buf),\n+\t\t\t\t PACKET_READ_GENTLE_ON_EOF |\n+\t\t\t\t PACKET_READ_CHOMP_NEWLINE);\n+\n+\tif (bytes_read < 0)\n+\t\tgoto done_poke;\n+\n+\tif (!strcmp(reply_buf, \"NAK watchman\")\n+#ifdef USE_WATCHMAN\n+\t    || !ends_with(reply_buf, \"watchman\")\n+#endif\n+\t\t) {\n+\t\twarning(\"We requested watchman support from index-helper, but \"\n+\t\t\t\"it doesn't support it. Please use a version of git \"\n+\t\t\t\"index-helper with watchman support.\");\n+\t\tgoto done_poke;\n+\t}\n+\n+\tif (!starts_with(reply_buf, \"OK\"))\n+\t\tgoto done_poke;\n+\n+\tret = 0;\n+done_poke:\n+\tclose(fd);\n+\treturn ret;\n+}\n+\n static int poke_daemon(struct index_state *istate,\n \t\t       const struct stat *st, int refresh_cache)\n {\n \tint fd;\n-\tint ret = 0;\n-\tconst char *socket_path;\n+\tint ret = -1;\n \n \t/* if this is from index-helper, do not poke itself (recursively) */\n \tif (istate->to_shm)\n \t\treturn 0;\n \n-\tsocket_path = git_path(\"index-helper.sock\");\n-\tif (!socket_path)\n+\tfd = unix_stream_connect(git_path(\"index-helper.sock\"));\n+\tif (fd < 0) {\n+\t\twarning(\"Failed to connect to index-helper socket\");\n+\t\tunlink(git_path(\"index-helper.sock\"));\n \t\treturn -1;\n-\n-\tfd = unix_stream_connect(socket_path);\n+\t}\n \tsigchain_push(SIGPIPE, SIG_IGN);\n+\n \tif (refresh_cache) {\n \t\tpacket_write_gently(fd, \"refresh\");\n+\t\tpacket_flush_gently(fd);\n+\t\tret = 0;\n \t} else {\n-\t\tpacket_write_gently(fd, \"poke\");\n+\t\tret = poke_and_wait_for_reply(fd);\n \t}\n-\tpacket_flush_gently(fd);\n \n \tclose(fd);\n \tsigchain_pop(SIGPIPE);\n@@ -1737,6 +1874,74 @@ fail:\n \treturn -1;\n }\n \n+static void refresh_by_watchman(struct index_state *istate)\n+{\n+\tvoid *shm = NULL;\n+\tint length;\n+\tint i;\n+\tstruct stat st;\n+\tint fd = -1;\n+\tconst char *path = git_path(\"shm-watchman-%s-%\"PRIuMAX,\n+\t\t\t\t    sha1_to_hex(istate->sha1),\n+\t\t\t\t    (uintmax_t)getpid());\n+\n+\tfd = open(path, O_RDONLY);\n+\tif (fd < 0)\n+\t\treturn;\n+\n+\t/*\n+\t * This watchman data is just for us -- no need to keep it\n+\t * around once we've got it open.\n+\t */\n+\tunlink(path);\n+\n+\tif (fstat(fd, &st) < 0)\n+\t\tgoto done;\n+\n+\tlength = st.st_size;\n+\tshm = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);\n+\n+\tif (shm == MAP_FAILED)\n+\t\tgoto done;\n+\n+\tclose(fd);\n+\tfd = -1;\n+\n+\tif (length <= 20 ||\n+\t    hashcmp(istate->sha1, (unsigned char *)shm + length - 20) ||\n+\t    /*\n+\t     * No need to clear CE_WATCHMAN_DIRTY set by 'WAMA' on\n+\t     * disk. Watchman can only set more, not clear any, so\n+\t     * this is OR mask.\n+\t     */\n+\t    read_watchman_ext(istate, shm, length - 20))\n+\t\tgoto done;\n+\n+\t/*\n+\t * Now that we've marked the invalid entries in the\n+\t * untracked-cache itself, we can erase them from the list of\n+\t * entries to be processed and mark the untracked cache for\n+\t * watchman usage.\n+\t */\n+\tif (istate->untracked) {\n+\t\tstring_list_clear(&istate->untracked->invalid_untracked, 0);\n+\t\tistate->untracked->use_watchman = 1;\n+\t}\n+\n+\tfor (i = 0; i < istate->cache_nr; i++) {\n+\t\tstruct cache_entry *ce = istate->cache[i];\n+\t\tif (ce_stage(ce) || (ce->ce_flags & CE_WATCHMAN_DIRTY))\n+\t\t\tcontinue;\n+\t\tce_mark_uptodate(ce);\n+\t}\n+done:\n+\tif (shm)\n+\t\tmunmap(shm, length);\n+\n+\tif (fd >= 0)\n+\t\tclose(fd);\n+}\n+\n /* remember to discard_cache() before reading a different cache! */\n int do_read_index(struct index_state *istate, const char *path, int must_exist)\n {\n@@ -1856,7 +2061,7 @@ int read_index_from(struct index_state *istate, const char *path)\n \tsplit_index = istate->split_index;\n \tif (!split_index || is_null_sha1(split_index->base_sha1)) {\n \t\tpost_read_index_from(istate);\n-\t\treturn ret;\n+\t\tgoto done;\n \t}\n \n \tif (split_index->base)\n@@ -1877,6 +2082,10 @@ int read_index_from(struct index_state *istate, const char *path)\n \t\t    sha1_to_hex(split_index->base->sha1));\n \tmerge_base_index(istate);\n \tpost_read_index_from(istate);\n+\n+done:\n+\tif (ret > 0 && istate->from_shm && istate->last_update)\n+\t\trefresh_by_watchman(istate);\n \treturn ret;\n }\n \n@@ -2178,7 +2387,7 @@ out:\n \treturn 0;\n }\n \n-static int verify_index(const struct index_state *istate)\n+int verify_index(const struct index_state *istate)\n {\n \treturn verify_index_from(istate, get_index_file());\n }\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287034","messageId":"1463694357-6503-13-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 12/20] update-index: enable/disable watchman support","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:49Z","receivedAt":"2016-05-19T21:45:49Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/git-index-helper.txt |  3 +++\n Documentation/git-update-index.txt |  6 ++++++\n builtin/update-index.c             | 16 ++++++++++++++++\n 3 files changed, 25 insertions(+)\n\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nindex cce00cb..55a8a5a 100644\n--- a/Documentation/git-index-helper.txt\n+++ b/Documentation/git-index-helper.txt\n@@ -18,6 +18,9 @@ each with a submodule, you might need four index-helpers.  (In practice,\n this is only worthwhile for large indexes, so only use it if you notice\n that git status is slow).\n \n+If you want the index-helper to accelerate untracked file checking,\n+run git update-index --watchman before using it.\n+\n OPTIONS\n -------\n \ndiff --git a/Documentation/git-update-index.txt b/Documentation/git-update-index.txt\nindex c6cbed1..6736487 100644\n--- a/Documentation/git-update-index.txt\n+++ b/Documentation/git-update-index.txt\n@@ -19,6 +19,7 @@ SYNOPSIS\n \t     [--ignore-submodules]\n \t     [--[no-]split-index]\n \t     [--[no-|test-|force-]untracked-cache]\n+\t     [--[no-]watchman]\n \t     [--really-refresh] [--unresolve] [--again | -g]\n \t     [--info-only] [--index-info]\n \t     [-z] [--stdin] [--index-version <n>]\n@@ -176,6 +177,11 @@ may not support it yet.\n --no-untracked-cache::\n \tEnable or disable untracked cache feature. Please use\n \t`--test-untracked-cache` before enabling it.\n+\n+--watchman::\n+--no-watchman::\n+\tEnable or disable watchman support. This is, at present,\n+\tonly useful with git index-helper.\n +\n These options take effect whatever the value of the `core.untrackedCache`\n configuration variable (see linkgit:git-config[1]). But a warning is\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex 1c94ca5..a3b4b5d 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -914,6 +914,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n {\n \tint newfd, entries, has_errors = 0, nul_term_line = 0;\n \tenum uc_mode untracked_cache = UC_UNSPECIFIED;\n+\tint use_watchman = -1;\n \tint read_from_stdin = 0;\n \tint prefix_length = prefix ? strlen(prefix) : 0;\n \tint preferred_index_format = 0;\n@@ -1012,6 +1013,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\t\t    N_(\"test if the filesystem supports untracked cache\"), UC_TEST),\n \t\tOPT_SET_INT(0, \"force-untracked-cache\", &untracked_cache,\n \t\t\t    N_(\"enable untracked cache without testing the filesystem\"), UC_FORCE),\n+\t\tOPT_BOOL(0, \"watchman\", &use_watchman,\n+\t\t\tN_(\"use or not use watchman to reduce refresh cost\")),\n \t\tOPT_END()\n \t};\n \n@@ -1149,6 +1152,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n \t\tdie(\"Bug: bad untracked_cache value: %d\", untracked_cache);\n \t}\n \n+\tif (use_watchman > 0) {\n+\t\tthe_index.last_update    = xstrdup(\"\");\n+\t\tthe_index.cache_changed |= WATCHMAN_CHANGED;\n+#ifndef USE_WATCHMAN\n+\t\twarning(\"git was built without watchman support -- I'm \"\n+\t\t\t\"adding the extension here, but it probably won't \"\n+\t\t\t\"do you any good.\");\n+#endif\n+\t} else if (!use_watchman) {\n+\t\tthe_index.last_update    = NULL;\n+\t\tthe_index.cache_changed |= WATCHMAN_CHANGED;\n+\t}\n+\n \tif (active_cache_changed) {\n \t\tif (newfd < 0) {\n \t\t\tif (refresh_args.flags & REFRESH_QUIET)\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287035","messageId":"1463694357-6503-14-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 13/20] unpack-trees: preserve index extensions","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:50Z","receivedAt":"2016-05-19T21:45:50Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Make git checkout (and other unpack_tree operations) preserve the\nuntracked cache and watchman status. This is valuable for two reasons:\n\n1. Often, an unpack_tree operation will not touch large parts of the\nworking tree, and thus most of the untracked cache will continue to be\nvalid.\n\n2. Even if the untracked cache were entirely invalidated by such an\noperation, the user has signaled their intention to have such a cache,\nand we don't want to throw it away.\n\nThe same logic applies to the watchman state.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n cache.h                           |  1 +\n read-cache.c                      |  8 ++++++++\n t/t7063-status-untracked-cache.sh | 22 ++++++++++++++++++++++\n t/test-lib-functions.sh           |  4 ++++\n unpack-trees.c                    |  1 +\n 5 files changed, 36 insertions(+)\n\ndiff --git a/cache.h b/cache.h\nindex 633e1dd..1b372ed 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -580,6 +580,7 @@ extern void write_watchman_ext(struct strbuf *sb, struct index_state *istate);\n #define CLOSE_LOCK\t\t(1 << 1)\n extern int write_locked_index(struct index_state *, struct lock_file *lock, unsigned flags);\n extern int discard_index(struct index_state *);\n+extern void move_index_extensions(struct index_state *dst, struct index_state *src);\n extern int unmerged_index(const struct index_state *);\n extern int verify_path(const char *path);\n extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);\ndiff --git a/read-cache.c b/read-cache.c\nindex 8ec4be3..82d4446 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -2767,3 +2767,11 @@ void stat_validity_update(struct stat_validity *sv, int fd)\n \t\tfill_stat_data(sv->sd, &st);\n \t}\n }\n+\n+void move_index_extensions(struct index_state *dst, struct index_state *src)\n+{\n+\tdst->untracked = src->untracked;\n+\tsrc->untracked = NULL;\n+\tdst->last_update = src->last_update;\n+\tsrc->last_update = NULL;\n+}\ndiff --git a/t/t7063-status-untracked-cache.sh b/t/t7063-status-untracked-cache.sh\nindex a971884..083516d 100755\n--- a/t/t7063-status-untracked-cache.sh\n+++ b/t/t7063-status-untracked-cache.sh\n@@ -646,4 +646,26 @@ test_expect_success 'test ident field is working' '\n \ttest_cmp ../expect ../err\n '\n \n+test_expect_success 'untracked cache survives a checkout' '\n+\tgit commit --allow-empty -m empty &&\n+\ttest-dump-untracked-cache >../before &&\n+\ttest_when_finished  \"git checkout master\" &&\n+\tgit checkout -b other_branch &&\n+\ttest-dump-untracked-cache >../after &&\n+\ttest_cmp ../before ../after &&\n+\ttest_commit test &&\n+\ttest-dump-untracked-cache >../before &&\n+\tgit checkout master &&\n+\ttest-dump-untracked-cache >../after &&\n+\ttest_cmp ../before ../after\n+'\n+\n+test_expect_success 'untracked cache survives a commit' '\n+\ttest-dump-untracked-cache >../before &&\n+\tgit add done/two &&\n+\tgit commit -m commit &&\n+\ttest-dump-untracked-cache >../after &&\n+\ttest_cmp ../before ../after\n+'\n+\n test_done\ndiff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh\nindex 8d99eb3..e974b5b 100644\n--- a/t/test-lib-functions.sh\n+++ b/t/test-lib-functions.sh\n@@ -186,6 +186,10 @@ test_commit () {\n \t\ttest_tick\n \tfi &&\n \tgit commit $signoff -m \"$1\" &&\n+\tif [ \"$(git config core.bare)\" = false ]\n+\tthen\n+\t    git update-index --force-untracked-cache\n+\tfi\n \tgit tag \"${4:-$1}\"\n }\n \ndiff --git a/unpack-trees.c b/unpack-trees.c\nindex 9f55cc2..fc90eb3 100644\n--- a/unpack-trees.c\n+++ b/unpack-trees.c\n@@ -1215,6 +1215,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options\n \t\t\t\t\t\t  WRITE_TREE_SILENT |\n \t\t\t\t\t\t  WRITE_TREE_REPAIR);\n \t\t}\n+\t\tmove_index_extensions(&o->result, o->dst_index);\n \t\tdiscard_index(o->dst_index);\n \t\t*o->dst_index = o->result;\n \t} else {\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287042","messageId":"1463694357-6503-15-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 14/20] watchman: add a config option to enable the extension","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:51Z","receivedAt":"2016-05-19T21:45:51Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"For installations that have centrally-managed configuration, it's\neasier to set a config once than to run update-index on every\nrepository.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n .gitignore                    |  1 +\n Documentation/config.txt      |  4 ++++\n Makefile                      |  1 +\n read-cache.c                  |  6 ++++++\n t/t1701-watchman-extension.sh | 37 +++++++++++++++++++++++++++++++++++++\n test-dump-watchman.c          | 16 ++++++++++++++++\n 6 files changed, 65 insertions(+)\n create mode 100755 t/t1701-watchman-extension.sh\n create mode 100644 test-dump-watchman.c\n\ndiff --git a/.gitignore b/.gitignore\nindex b92f122..e6a5b2c 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -188,6 +188,7 @@\n /test-dump-cache-tree\n /test-dump-split-index\n /test-dump-untracked-cache\n+/test-dump-watchman\n /test-fake-ssh\n /test-scrap-cache-tree\n /test-genrandom\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 2cd6bdd..15001ce 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1848,6 +1848,10 @@ imap::\n \tThe configuration variables in the 'imap' section are described\n \tin linkgit:git-imap-send[1].\n \n+index.addwatchmanextension::\n+\tAutomatically add the watchman extension to the index whenever\n+\tit is written.\n+\n index.version::\n \tSpecify the version with which new index files should be\n \tinitialized.  This does not affect existing repositories.\ndiff --git a/Makefile b/Makefile\nindex 65ab0f4..5f0a954 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -599,6 +599,7 @@ TEST_PROGRAMS_NEED_X += test-delta\n TEST_PROGRAMS_NEED_X += test-dump-cache-tree\n TEST_PROGRAMS_NEED_X += test-dump-split-index\n TEST_PROGRAMS_NEED_X += test-dump-untracked-cache\n+TEST_PROGRAMS_NEED_X += test-dump-watchman\n TEST_PROGRAMS_NEED_X += test-fake-ssh\n TEST_PROGRAMS_NEED_X += test-genrandom\n TEST_PROGRAMS_NEED_X += test-hashmap\ndiff --git a/read-cache.c b/read-cache.c\nindex 82d4446..6d3fe71 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -2426,6 +2426,7 @@ static int do_write_index(struct index_state *istate, int newfd,\n \tint entries = istate->cache_nr;\n \tstruct stat st;\n \tstruct strbuf previous_name_buf = STRBUF_INIT, *previous_name;\n+\tint watchman = 0;\n \n \tfor (i = removed = extended = 0; i < entries; i++) {\n \t\tif (cache[i]->ce_flags & CE_REMOVE)\n@@ -2449,6 +2450,11 @@ static int do_write_index(struct index_state *istate, int newfd,\n \tif (istate->version == 3 || istate->version == 2)\n \t\tistate->version = extended ? 3 : 2;\n \n+\tif (!git_config_get_bool(\"index.addwatchmanextension\", &watchman) &&\n+\t    watchman &&\n+\t    !the_index.last_update)\n+\t\tthe_index.last_update = xstrdup(\"\");\n+\n \thdr_version = istate->version;\n \n \thdr.hdr_signature = htonl(CACHE_SIGNATURE);\ndiff --git a/t/t1701-watchman-extension.sh b/t/t1701-watchman-extension.sh\nnew file mode 100755\nindex 0000000..71f1d46\n--- /dev/null\n+++ b/t/t1701-watchman-extension.sh\n@@ -0,0 +1,37 @@\n+#!/bin/sh\n+\n+test_description='watchman extension smoke tests'\n+\n+# These don't actually test watchman interaction -- just the\n+# index extension\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'enable watchman' '\n+\ttest_commit a &&\n+\ttest-dump-watchman .git/index >actual &&\n+\techo \"last_update: (null)\" >expect &&\n+\ttest_cmp expect actual &&\n+\tgit update-index --watchman &&\n+\ttest-dump-watchman .git/index >actual &&\n+\techo \"last_update: \" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'disable watchman' '\n+\tgit update-index --no-watchman &&\n+\ttest-dump-watchman .git/index >actual &&\n+\techo \"last_update: (null)\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'auto-enable watchman' '\n+\ttest_config index.addwatchmanextension true &&\n+\ttest_commit c &&\n+\ttest-dump-watchman .git/index >actual &&\n+\techo \"last_update: \" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+\n+test_done\ndiff --git a/test-dump-watchman.c b/test-dump-watchman.c\nnew file mode 100644\nindex 0000000..0314fa5\n--- /dev/null\n+++ b/test-dump-watchman.c\n@@ -0,0 +1,16 @@\n+#include \"cache.h\"\n+#include \"ewah/ewok.h\"\n+\n+int main(int argc, char **argv)\n+{\n+\tdo_read_index(&the_index, argv[1], 1);\n+\tprintf(\"last_update: %s\\n\", the_index.last_update ?\n+\t       the_index.last_update : \"(null)\");\n+\n+\t/*\n+\t * For now, we just dump last_update, since it is not reasonable\n+\t * to populate the extension itself in tests.\n+\t */\n+\n+\treturn 0;\n+}\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287043","messageId":"1463694357-6503-16-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 15/20] index-helper: kill mode","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:52Z","receivedAt":"2016-05-19T21:45:52Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Add a new command (and command-line arg) to allow index-helpers to\nexit cleanly.\n\nThis is mainly useful for tests.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/git-index-helper.txt |  3 +++\n index-helper.c                     | 31 ++++++++++++++++++++++++++++++-\n t/t7900-index-helper.sh            |  9 +++++++++\n 3 files changed, 42 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nindex 55a8a5a..addf694 100644\n--- a/Documentation/git-index-helper.txt\n+++ b/Documentation/git-index-helper.txt\n@@ -40,6 +40,9 @@ OPTIONS\n --detach::\n \tDetach from the shell.\n \n+--kill::\n+\tKill any running index-helper and clean up the socket\n+\n NOTES\n -----\n \ndiff --git a/index-helper.c b/index-helper.c\nindex d3a1f39..4a171e6 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -353,6 +353,8 @@ static void loop(int fd, int idle_in_seconds)\n \t\t\t\t\t * alive, nothing to do.\n \t\t\t\t\t */\n \t\t\t\t}\n+\t\t\t} else if (!strcmp(buf, \"die\")) {\n+\t\t\t\tbreak;\n \t\t\t} else {\n \t\t\t\twarning(\"BUG: Bogus command %s\", buf);\n \t\t\t}\n@@ -383,10 +385,29 @@ static const char * const usage_text[] = {\n \tNULL\n };\n \n+static void request_kill(void)\n+{\n+\tint fd = unix_stream_connect(git_path(\"index-helper.sock\"));\n+\n+\tif (fd >= 0) {\n+\t\twrite_in_full(fd, \"die\", 4);\n+\t\tclose(fd);\n+\t}\n+\n+\t/*\n+\t * The child will try to do this anyway, but we want to be\n+\t * ready to launch a new daemon immediately after this command\n+\t * returns.\n+\t */\n+\n+\tunlink(git_path(\"index-helper.sock\"));\n+\treturn;\n+}\n+\n int main(int argc, char **argv)\n {\n \tconst char *prefix;\n-\tint idle_in_seconds = 600, detach = 0;\n+\tint idle_in_seconds = 600, detach = 0, kill = 0;\n \tint fd;\n \tstruct strbuf socket_path = STRBUF_INIT;\n \tstruct option options[] = {\n@@ -395,6 +416,7 @@ int main(int argc, char **argv)\n \t\tOPT_BOOL(0, \"strict\", &to_verify,\n \t\t\t \"verify shared memory after creating\"),\n \t\tOPT_BOOL(0, \"detach\", &detach, \"detach the process\"),\n+\t\tOPT_BOOL(0, \"kill\", &kill, \"request that existing index helper processes exit\"),\n \t\tOPT_END()\n \t};\n \n@@ -409,6 +431,13 @@ int main(int argc, char **argv)\n \t\t\t  options, usage_text, 0))\n \t\tdie(_(\"too many arguments\"));\n \n+\tif (kill) {\n+\t\tif (detach)\n+\t\t\tdie(_(\"--kill doesn't want any other options\"));\n+\t\trequest_kill();\n+\t\treturn 0;\n+\t}\n+\n \tatexit(cleanup);\n \tsigchain_push_common(cleanup_on_signal);\n \ndiff --git a/t/t7900-index-helper.sh b/t/t7900-index-helper.sh\nindex 114c112..e71b5af 100755\n--- a/t/t7900-index-helper.sh\n+++ b/t/t7900-index-helper.sh\n@@ -20,4 +20,13 @@ test_expect_success 'index-helper smoke test' '\n \ttest_path_is_missing .git/index-helper.sock\n '\n \n+test_expect_success 'index-helper creates usable path file and can be killed' '\n+\ttest_when_finished \"git index-helper --kill\" &&\n+\ttest_path_is_missing .git/index-helper.sock &&\n+\tgit index-helper --detach &&\n+\ttest -S .git/index-helper.sock &&\n+\tgit index-helper --kill &&\n+\ttest_path_is_missing .git/index-helper.sock\n+'\n+\n test_done\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287039","messageId":"1463694357-6503-17-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 16/20] index-helper: don't run if already running","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:53Z","receivedAt":"2016-05-19T21:45:53Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Signed-off-by: David Turner <dturner@twopensource.com>\n---\n index-helper.c          | 7 +++++++\n t/t7900-index-helper.sh | 9 +++++++++\n 2 files changed, 16 insertions(+)\n\ndiff --git a/index-helper.c b/index-helper.c\nindex 4a171e6..ddc641a 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -438,6 +438,13 @@ int main(int argc, char **argv)\n \t\treturn 0;\n \t}\n \n+\t/* check that no other copy is running */\n+\tfd = unix_stream_connect(git_path(\"index-helper.sock\"));\n+\tif (fd > 0)\n+\t\tdie(_(\"Already running\"));\n+\tif (errno != ECONNREFUSED && errno != ENOENT)\n+\t\tdie_errno(_(\"Unexpected error checking socket\"));\n+\n \tatexit(cleanup);\n \tsigchain_push_common(cleanup_on_signal);\n \ndiff --git a/t/t7900-index-helper.sh b/t/t7900-index-helper.sh\nindex e71b5af..7159971 100755\n--- a/t/t7900-index-helper.sh\n+++ b/t/t7900-index-helper.sh\n@@ -29,4 +29,13 @@ test_expect_success 'index-helper creates usable path file and can be killed' '\n \ttest_path_is_missing .git/index-helper.sock\n '\n \n+test_expect_success 'index-helper will not start if already running' '\n+\ttest_when_finished \"git index-helper --kill\" &&\n+\tgit index-helper --detach &&\n+\ttest -S .git/index-helper.sock &&\n+\ttest_must_fail git index-helper 2>err &&\n+\ttest -S .git/index-helper.sock &&\n+\tgrep \"Already running\" err\n+'\n+\n test_done\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287033","messageId":"1463694357-6503-18-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 17/20] index-helper: autorun mode","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:54Z","receivedAt":"2016-05-19T21:45:54Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Soon, we'll want to automatically start index-helper, so we need\na mode that silently exits if it can't start up (either because\nit's not in a git dir, or because another one is already running).\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/git-index-helper.txt |  4 ++++\n index-helper.c                     | 29 +++++++++++++++++++++++------\n t/t7900-index-helper.sh            |  8 ++++++++\n 3 files changed, 35 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-index-helper.txt b/Documentation/git-index-helper.txt\nindex addf694..0466296 100644\n--- a/Documentation/git-index-helper.txt\n+++ b/Documentation/git-index-helper.txt\n@@ -43,6 +43,10 @@ OPTIONS\n --kill::\n \tKill any running index-helper and clean up the socket\n \n+--autorun::\n+\tIf index-helper is not already running, start it.  Else, do\n+\tnothing.\n+\n NOTES\n -----\n \ndiff --git a/index-helper.c b/index-helper.c\nindex ddc641a..2d97c77 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -407,8 +407,9 @@ static void request_kill(void)\n int main(int argc, char **argv)\n {\n \tconst char *prefix;\n-\tint idle_in_seconds = 600, detach = 0, kill = 0;\n+\tint idle_in_seconds = 600, detach = 0, kill = 0, autorun = 0;\n \tint fd;\n+\tint nongit;\n \tstruct strbuf socket_path = STRBUF_INIT;\n \tstruct option options[] = {\n \t\tOPT_INTEGER(0, \"exit-after\", &idle_in_seconds,\n@@ -417,6 +418,7 @@ int main(int argc, char **argv)\n \t\t\t \"verify shared memory after creating\"),\n \t\tOPT_BOOL(0, \"detach\", &detach, \"detach the process\"),\n \t\tOPT_BOOL(0, \"kill\", &kill, \"request that existing index helper processes exit\"),\n+\t\tOPT_BOOL(0, \"autorun\", &autorun, \"this is an automatic run of git index-helper, so certain errors can be solved by silently exiting\"),\n \t\tOPT_END()\n \t};\n \n@@ -426,7 +428,14 @@ int main(int argc, char **argv)\n \tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n \t\tusage_with_options(usage_text, options);\n \n-\tprefix = setup_git_directory();\n+\tprefix = setup_git_directory_gently(&nongit);\n+\tif (nongit) {\n+\t\tif (autorun)\n+\t\t\texit(0);\n+\t\telse\n+\t\t\tdie(_(\"not a git repository\"));\n+\t}\n+\n \tif (parse_options(argc, (const char **)argv, prefix,\n \t\t\t  options, usage_text, 0))\n \t\tdie(_(\"too many arguments\"));\n@@ -440,10 +449,18 @@ int main(int argc, char **argv)\n \n \t/* check that no other copy is running */\n \tfd = unix_stream_connect(git_path(\"index-helper.sock\"));\n-\tif (fd > 0)\n-\t\tdie(_(\"Already running\"));\n-\tif (errno != ECONNREFUSED && errno != ENOENT)\n-\t\tdie_errno(_(\"Unexpected error checking socket\"));\n+\tif (fd > 0) {\n+\t\tif (autorun)\n+\t\t\texit(0);\n+\t\telse\n+\t\t\tdie(_(\"Already running\"));\n+\t}\n+\tif (errno != ECONNREFUSED && errno != ENOENT) {\n+\t\tif (autorun)\n+\t\t\treturn 0;\n+\t\telse\n+\t\t\tdie_errno(_(\"Unexpected error checking socket\"));\n+\t}\n \n \tatexit(cleanup);\n \tsigchain_push_common(cleanup_on_signal);\ndiff --git a/t/t7900-index-helper.sh b/t/t7900-index-helper.sh\nindex 7159971..aa6e5fc 100755\n--- a/t/t7900-index-helper.sh\n+++ b/t/t7900-index-helper.sh\n@@ -38,4 +38,12 @@ test_expect_success 'index-helper will not start if already running' '\n \tgrep \"Already running\" err\n '\n \n+test_expect_success 'index-helper is quiet with --autorun' '\n+\ttest_when_finished \"git index-helper --kill\" &&\n+\tgit index-helper --kill &&\n+\tgit index-helper --detach &&\n+\ttest -S .git/index-helper.sock &&\n+\tgit index-helper --autorun\n+'\n+\n test_done\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287038","messageId":"1463694357-6503-19-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 18/20] index-helper: optionally automatically run","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:55Z","receivedAt":"2016-05-19T21:45:55Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Introduce a new config option, indexhelper.autorun, to automatically\nrun git index-helper before starting up a builtin git command.  This\nenables users to keep index-helper running without manual\nintervention.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/config.txt |  4 ++++\n read-cache.c             | 37 +++++++++++++++++++++++++++++++++++--\n t/t7900-index-helper.sh  | 20 ++++++++++++++++++++\n 3 files changed, 59 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 15001ce..385ea66 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1856,6 +1856,10 @@ index.version::\n \tSpecify the version with which new index files should be\n \tinitialized.  This does not affect existing repositories.\n \n+indexhelper.autorun::\n+\tAutomatically run git index-helper when any builtin git\n+\tcommand is run inside a repository.\n+\n init.templateDir::\n \tSpecify the directory from which templates will be copied.\n \t(See the \"TEMPLATE DIRECTORY\" section of linkgit:git-init[1].)\ndiff --git a/read-cache.c b/read-cache.c\nindex 6d3fe71..76aecca 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -22,6 +22,7 @@\n #include \"pkt-line.h\"\n #include \"sigchain.h\"\n #include \"ewah/ewok.h\"\n+#include \"run-command.h\"\n \n static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,\n \t\t\t\t\t       unsigned int options);\n@@ -1723,6 +1724,33 @@ static void post_read_index_from(struct index_state *istate)\n \ttweak_untracked_cache(istate);\n }\n \n+static int want_auto_index_helper(void)\n+{\n+\tint want = -1;\n+\tconst char *value = NULL;\n+\tconst char *key = \"indexhelper.autorun\";\n+\n+\tif (git_config_key_is_valid(key) &&\n+\t    !git_config_get_value(key, &value)) {\n+\t\tint b = git_config_maybe_bool(key, value);\n+\t\twant = b >= 0 ? b : 0;\n+\t}\n+\treturn want;\n+}\n+\n+static void autorun_index_helper(void)\n+{\n+\tconst char *argv[] = {\"git-index-helper\", \"--detach\", \"--autorun\", NULL};\n+\tif (want_auto_index_helper() <= 0)\n+\t\treturn;\n+\n+\ttrace_argv_printf(argv, \"trace: auto index-helper:\");\n+\n+\tif (run_command_v_opt(argv,\n+\t\t\t      RUN_SILENT_EXEC_FAILURE | RUN_CLEAN_ON_EXIT))\n+\t\twarning(_(\"You specified indexhelper.autorun, but running git-index-helper failed.\"));\n+}\n+\n /* in ms */\n #define WATCHMAN_TIMEOUT 1000\n \n@@ -1794,6 +1822,7 @@ static int poke_daemon(struct index_state *istate,\n \tif (fd < 0) {\n \t\twarning(\"Failed to connect to index-helper socket\");\n \t\tunlink(git_path(\"index-helper.sock\"));\n+\t\tautorun_index_helper();\n \t\treturn -1;\n \t}\n \tsigchain_push(SIGPIPE, SIG_IGN);\n@@ -1833,9 +1862,13 @@ static int try_shm(struct index_state *istate)\n \tint fd = -1;\n \n \tif (!is_main_index(istate) ||\n-\t    old_size <= 20 ||\n-\t    stat(git_path(\"index-helper.sock\"), &st))\n+\t    old_size <= 20)\n \t\treturn -1;\n+\n+\tif (stat(git_path(\"index-helper.sock\"), &st)) {\n+\t\tautorun_index_helper();\n+\t\treturn -1;\n+\t}\n \tif (poke_daemon(istate, &st, 0))\n \t\treturn -1;\n \tsha1 = (unsigned char *)istate->mmap + old_size - 20;\ndiff --git a/t/t7900-index-helper.sh b/t/t7900-index-helper.sh\nindex aa6e5fc..3cfdf63 100755\n--- a/t/t7900-index-helper.sh\n+++ b/t/t7900-index-helper.sh\n@@ -16,6 +16,9 @@ test -n \"$NO_MMAP\" && {\n }\n \n test_expect_success 'index-helper smoke test' '\n+\t# We need an existing commit so that the index exists (otherwise,\n+\t# the index-helper will not be autostarted)\n+\ttest_commit x &&\n \tgit index-helper --exit-after 1 &&\n \ttest_path_is_missing .git/index-helper.sock\n '\n@@ -46,4 +49,21 @@ test_expect_success 'index-helper is quiet with --autorun' '\n \tgit index-helper --autorun\n '\n \n+test_expect_success 'index-helper autorun works' '\n+\ttest_when_finished \"git index-helper --kill\" &&\n+\trm -f .git/index-helper.sock &&\n+\tgit status &&\n+\ttest_path_is_missing .git/index-helper.sock &&\n+\ttest_config indexhelper.autorun true &&\n+\tgit status &&\n+\ttest -S .git/index-helper.sock &&\n+\tgit status 2>err &&\n+\ttest -S .git/index-helper.sock &&\n+\ttest_must_be_empty err &&\n+\tgit index-helper --kill &&\n+\ttest_config indexhelper.autorun false &&\n+\tgit status &&\n+\ttest_path_is_missing .git/index-helper.sock\n+'\n+\n test_done\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287036","messageId":"1463694357-6503-20-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 19/20] trace: measure where the time is spent in the index-heavy operations","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:56Z","receivedAt":"2016-05-19T21:45:56Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nAll the known heavy code blocks are measured (except object database\naccess). This should help identify if an optimization is effective or\nnot. An unoptimized git-status would give something like below (92% of\ntime is accounted). To sum up the effort of making git scale better:\n\n - read cache line is being addressed by index-helper\n - preload/refresh index by watchman\n - read packed refs by lmdb backend\n - diff-index by rebuilding cache-tree\n - read directory by untracked cache and watchman\n - write index by split index\n - name hash potientially by index-helper\n\nread-cache.c:2075         performance: 0.004058570 s: read cache .../index\npreload-index.c:104       performance: 0.004419864 s: preload index\nread-cache.c:1265         performance: 0.000185224 s: refresh index\nrefs/files-backend.c:1100 performance: 0.001935788 s: read packed refs\ndiff-lib.c:240            performance: 0.000144132 s: diff-files\ndiff-lib.c:506            performance: 0.013592000 s: diff-index\nname-hash.c:128           performance: 0.000614177 s: initialize name hash\ndir.c:2030                performance: 0.015814103 s: read directory\nread-cache.c:2565         performance: 0.004052343 s: write index, changed mask = 2\ntrace.c:420               performance: 0.048365509 s: git command: './git' 'status'\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n diff-lib.c           |  4 ++++\n dir.c                |  2 ++\n name-hash.c          |  2 ++\n preload-index.c      |  2 ++\n read-cache.c         | 11 +++++++++++\n refs/files-backend.c |  2 ++\n 6 files changed, 23 insertions(+)\n\ndiff --git a/diff-lib.c b/diff-lib.c\nindex bc49c70..7af7f9a 100644\n--- a/diff-lib.c\n+++ b/diff-lib.c\n@@ -90,6 +90,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)\n \tint diff_unmerged_stage = revs->max_count;\n \tunsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)\n \t\t\t      ? CE_MATCH_RACY_IS_DIRTY : 0);\n+\tuint64_t start = getnanotime();\n \n \tdiff_set_mnemonic_prefix(&revs->diffopt, \"i/\", \"w/\");\n \n@@ -236,6 +237,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)\n \t}\n \tdiffcore_std(&revs->diffopt);\n \tdiff_flush(&revs->diffopt);\n+\ttrace_performance_since(start, \"diff-files\");\n \treturn 0;\n }\n \n@@ -491,6 +493,7 @@ static int diff_cache(struct rev_info *revs,\n int run_diff_index(struct rev_info *revs, int cached)\n {\n \tstruct object_array_entry *ent;\n+\tuint64_t start = getnanotime();\n \n \tent = revs->pending.objects;\n \tif (diff_cache(revs, ent->item->oid.hash, ent->name, cached))\n@@ -500,6 +503,7 @@ int run_diff_index(struct rev_info *revs, int cached)\n \tdiffcore_fix_diff_index(&revs->diffopt);\n \tdiffcore_std(&revs->diffopt);\n \tdiff_flush(&revs->diffopt);\n+\ttrace_performance_since(start, \"diff-index\");\n \treturn 0;\n }\n \ndiff --git a/dir.c b/dir.c\nindex 5058b29..c56a8b9 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -2183,6 +2183,7 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru\n {\n \tstruct path_simplify *simplify;\n \tstruct untracked_cache_dir *untracked;\n+\tuint64_t start = getnanotime();\n \n \t/*\n \t * Check out create_simplify()\n@@ -2224,6 +2225,7 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru\n \tfree_simplify(simplify);\n \tqsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);\n \tqsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);\n+\ttrace_performance_since(start, \"read directory %.*s\", len, path);\n \tif (dir->untracked) {\n \t\tstatic struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);\n \t\ttrace_printf_key(&trace_untracked_stats,\ndiff --git a/name-hash.c b/name-hash.c\nindex 6d9f23e..b3966d8 100644\n--- a/name-hash.c\n+++ b/name-hash.c\n@@ -115,6 +115,7 @@ static int cache_entry_cmp(const struct cache_entry *ce1,\n static void lazy_init_name_hash(struct index_state *istate)\n {\n \tint nr;\n+\tuint64_t start = getnanotime();\n \n \tif (istate->name_hash_initialized)\n \t\treturn;\n@@ -124,6 +125,7 @@ static void lazy_init_name_hash(struct index_state *istate)\n \tfor (nr = 0; nr < istate->cache_nr; nr++)\n \t\thash_index_entry(istate, istate->cache[nr]);\n \tistate->name_hash_initialized = 1;\n+\ttrace_performance_since(start, \"initialize name hash\");\n }\n \n void add_name_hash(struct index_state *istate, struct cache_entry *ce)\ndiff --git a/preload-index.c b/preload-index.c\nindex c1fe3a3..7bb4809 100644\n--- a/preload-index.c\n+++ b/preload-index.c\n@@ -72,6 +72,7 @@ static void preload_index(struct index_state *index,\n {\n \tint threads, i, work, offset;\n \tstruct thread_data data[MAX_PARALLEL];\n+\tuint64_t start = getnanotime();\n \n \tif (!core_preload_index)\n \t\treturn;\n@@ -100,6 +101,7 @@ static void preload_index(struct index_state *index,\n \t\tif (pthread_join(p->pthread, NULL))\n \t\t\tdie(\"unable to join threaded lstat\");\n \t}\n+\ttrace_performance_since(start, \"preload index\");\n }\n #endif\n \ndiff --git a/read-cache.c b/read-cache.c\nindex 76aecca..bdf3586 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1189,6 +1189,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n \tconst char *typechange_fmt;\n \tconst char *added_fmt;\n \tconst char *unmerged_fmt;\n+\tuint64_t start = getnanotime();\n \n \tmodified_fmt = (in_porcelain ? \"M\\t%s\\n\" : \"%s: needs update\\n\");\n \tdeleted_fmt = (in_porcelain ? \"D\\t%s\\n\" : \"%s: needs update\\n\");\n@@ -1263,6 +1264,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n \n \t\treplace_index_entry(istate, i, new);\n \t}\n+\ttrace_performance_since(start, \"refresh index\");\n \treturn has_errors;\n }\n \n@@ -2084,12 +2086,15 @@ int read_index_from(struct index_state *istate, const char *path)\n {\n \tstruct split_index *split_index;\n \tint ret;\n+\tuint64_t start;\n \n \t/* istate->initialized covers both .git/index and .git/sharedindex.xxx */\n \tif (istate->initialized)\n \t\treturn istate->cache_nr;\n \n+\tstart = getnanotime();\n \tret = do_read_index(istate, path, 0);\n+\ttrace_performance_since(start, \"read cache %s\", path);\n \n \tsplit_index = istate->split_index;\n \tif (!split_index || is_null_sha1(split_index->base_sha1)) {\n@@ -2104,6 +2109,7 @@ int read_index_from(struct index_state *istate, const char *path)\n \tsplit_index->base->keep_mmap = istate->keep_mmap;\n \tsplit_index->base->to_shm    = istate->to_shm;\n \tsplit_index->base->from_shm  = istate->from_shm;\n+\tstart = getnanotime();\n \tret = do_read_index(split_index->base,\n \t\t\t    git_path(\"sharedindex.%s\",\n \t\t\t\t     sha1_to_hex(split_index->base_sha1)), 1);\n@@ -2115,6 +2121,9 @@ int read_index_from(struct index_state *istate, const char *path)\n \t\t    sha1_to_hex(split_index->base->sha1));\n \tmerge_base_index(istate);\n \tpost_read_index_from(istate);\n+\ttrace_performance_since(start, \"read cache %s\",\n+\t\t\t\tgit_path(\"sharedindex.%s\",\n+\t\t\t\t\t sha1_to_hex(split_index->base_sha1)));\n \n done:\n \tif (ret > 0 && istate->from_shm && istate->last_update)\n@@ -2460,6 +2469,7 @@ static int do_write_index(struct index_state *istate, int newfd,\n \tstruct stat st;\n \tstruct strbuf previous_name_buf = STRBUF_INIT, *previous_name;\n \tint watchman = 0;\n+\tuint64_t start = getnanotime();\n \n \tfor (i = removed = extended = 0; i < entries; i++) {\n \t\tif (cache[i]->ce_flags & CE_REMOVE)\n@@ -2580,6 +2590,7 @@ static int do_write_index(struct index_state *istate, int newfd,\n \t\treturn -1;\n \tistate->timestamp.sec = (unsigned int)st.st_mtime;\n \tistate->timestamp.nsec = ST_MTIME_NSEC(st);\n+\ttrace_performance_since(start, \"write index, changed mask = %x\", istate->cache_changed);\n \treturn 0;\n }\n \ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 81f68f8..57571ce 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -1048,6 +1048,7 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)\n \tstruct ref_entry *last = NULL;\n \tstruct strbuf line = STRBUF_INIT;\n \tenum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;\n+\tuint64_t start = getnanotime();\n \n \twhile (strbuf_getwholeline(&line, f, '\\n') != EOF) {\n \t\tunsigned char sha1[20];\n@@ -1096,6 +1097,7 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)\n \t}\n \n \tstrbuf_release(&line);\n+\ttrace_performance_since(start, \"read packed refs\");\n }\n \n /*\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"287037","messageId":"1463694357-6503-21-git-send-email-dturner@twopensource.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"[PATCH v12 20/20] index-helper: indexhelper.exitafter config","fromName":"David Turner","fromEmail":"dturner@twopensource.com","sentAt":"2016-05-19T21:45:57Z","receivedAt":"2016-05-19T21:45:57Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Add a configuration variable, indexhelper.exitafter, which provides a\ndefault time to keep the index-helper alive.  This is useful with\nindexhelper.autorun; some users will want to keep the\nautomatically-run index-helper alive across their lunch break and will\nthus set indexhelper.exitafter to a high value.\n\nSigned-off-by: David Turner <dturner@twopensource.com>\n---\n Documentation/config.txt | 4 ++++\n index-helper.c           | 2 ++\n t/t7900-index-helper.sh  | 8 ++++++++\n 3 files changed, 14 insertions(+)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 385ea66..336d5a2 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1860,6 +1860,10 @@ indexhelper.autorun::\n \tAutomatically run git index-helper when any builtin git\n \tcommand is run inside a repository.\n \n+indexhelper.exitafter::\n+\tWhen no exit-after argument is given, git index-helper defaults\n+\tto this number of seconds. Specify 0 to wait forever. Default is 600.\n+\n init.templateDir::\n \tSpecify the directory from which templates will be copied.\n \t(See the \"TEMPLATE DIRECTORY\" section of linkgit:git-init[1].)\ndiff --git a/index-helper.c b/index-helper.c\nindex 2d97c77..a639de8 100644\n--- a/index-helper.c\n+++ b/index-helper.c\n@@ -425,6 +425,8 @@ int main(int argc, char **argv)\n \tgit_extract_argv0_path(argv[0]);\n \tgit_setup_gettext();\n \n+\tgit_config_get_int(\"indexhelper.exitafter\", &idle_in_seconds);\n+\n \tif (argc == 2 && !strcmp(argv[1], \"-h\"))\n \t\tusage_with_options(usage_text, options);\n \ndiff --git a/t/t7900-index-helper.sh b/t/t7900-index-helper.sh\nindex 3cfdf63..6c9b4dd 100755\n--- a/t/t7900-index-helper.sh\n+++ b/t/t7900-index-helper.sh\n@@ -66,4 +66,12 @@ test_expect_success 'index-helper autorun works' '\n \ttest_path_is_missing .git/index-helper.sock\n '\n \n+test_expect_success 'indexhelper.exitafter config works' '\n+\ttest_when_finished \"git index-helper --kill\" &&\n+\ttest_config indexhelper.exitafter 1 &&\n+\tgit index-helper --detach &&\n+\tsleep 3 &&\n+\ttest_path_is_missing .git/index-helper.sock\n+'\n+\n test_done\n-- \n2.4.2.767.g62658d5-twtrsrc\n"},{"id":"289439","messageId":"CACsJy8DzyvFOHU_8xoWUcjej=Ws7_v475=Q2iZmwKhkzYaZ7sg@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-12-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 11/20] index-helper: use watchman to avoid refreshing index with lstat()","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-17T13:02:43Z","receivedAt":"2016-06-17T13:03:18Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Fri, May 20, 2016 at 4:45 AM, David Turner <dturner@twopensource.com> wrote:\n> diff --git a/read-cache.c b/read-cache.c\n> index 1719f5a..8ec4be3 100644\n> --- a/read-cache.c\n> +++ b/read-cache.c\n> @@ -1235,7 +1235,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n>                 if (!new) {\n>                         const char *fmt;\n>\n> -                       if (really && cache_errno == EINVAL) {\n> +                       if (really || cache_errno == EINVAL) {\n>                                 /* If we are doing --really-refresh that\n>                                  * means the index is not valid anymore.\n>                                  */\n\nThis looks really odd. I don't see why we would need this. It seems\nfirst appeared in your \"do not apply\" patch [1]. Maybe leftover?\n\nI found this while re-reading the series and I have not put much time\nin studying this code yet. So I may be wrong. I'll post again if I\nfind that it's true after some more staring.\n\n[1] http://article.gmane.org/gmane.comp.version-control.git/288567\n-- \nDuy\n"},{"id":"289449","messageId":"CAP8UFD3ar3uZmrOtp9TUGGZpk6vMeJgZxmPmu8grENbaZwZETA@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-11-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 10/20] watchman: support watchman to reduce index refresh cost","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-06-17T16:01:33Z","receivedAt":"2016-06-17T16:01:42Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>\n> The previous patch has the logic to clear bits in 'WAMA' bitmap. This\n> patch has logic to set bits as told by watchman. The missing bit,\n> _using_ these bits, are not here yet.\n>\n> A lot of this code is written by David Turner originally, mostly from\n> [1]. I'm just copying and polishing it a bit.\n>\n> [1] http://article.gmane.org/gmane.comp.version-control.git/248006\n>\n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n> Signed-off-by: David Turner <dturner@twopensource.com>\n> ---\n>  Makefile           |  12 +++++\n>  cache.h            |   1 +\n>  config.c           |   5 ++\n>  configure.ac       |   8 ++++\n>  environment.c      |   3 ++\n>  watchman-support.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n>  watchman-support.h |   7 +++\n>  7 files changed, 171 insertions(+)\n>  create mode 100644 watchman-support.c\n>  create mode 100644 watchman-support.h\n>\n> diff --git a/Makefile b/Makefile\n> index c8be0e7..65ab0f4 100644\n> --- a/Makefile\n> +++ b/Makefile\n> @@ -451,6 +451,7 @@ MSGFMT = msgfmt\n>  CURL_CONFIG = curl-config\n>  PTHREAD_LIBS = -lpthread\n>  PTHREAD_CFLAGS =\n> +WATCHMAN_LIBS =\n>  GCOV = gcov\n>\n>  export TCL_PATH TCLTK_PATH\n> @@ -1416,6 +1417,13 @@ else\n>         LIB_OBJS += thread-utils.o\n>  endif\n>\n> +ifdef USE_WATCHMAN\n> +       LIB_H += watchman-support.h\n> +       LIB_OBJS += watchman-support.o\n> +       WATCHMAN_LIBS = -lwatchman\n> +       BASIC_CFLAGS += -DUSE_WATCHMAN\n> +endif\n> +\n>  ifdef HAVE_PATHS_H\n>         BASIC_CFLAGS += -DHAVE_PATHS_H\n>  endif\n> @@ -2025,6 +2033,9 @@ git-remote-testsvn$X: remote-testsvn.o GIT-LDFLAGS $(GITLIBS) $(VCSSVN_LIB)\n>         $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) \\\n>         $(VCSSVN_LIB)\n>\n> +git-index-helper$X: index-helper.o GIT-LDFLAGS $(GITLIBS)\n> +       $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS) $(WATCHMAN_LIBS)\n> +\n>  $(REMOTE_CURL_ALIASES): $(REMOTE_CURL_PRIMARY)\n>         $(QUIET_LNCP)$(RM) $@ && \\\n>         ln $< $@ 2>/dev/null || \\\n> @@ -2164,6 +2175,7 @@ GIT-BUILD-OPTIONS: FORCE\n>         @echo NO_PYTHON=\\''$(subst ','\\'',$(subst ','\\'',$(NO_PYTHON)))'\\' >>$@+\n>         @echo NO_UNIX_SOCKETS=\\''$(subst ','\\'',$(subst ','\\'',$(NO_UNIX_SOCKETS)))'\\' >>$@+\n>         @echo NO_MMAP=\\''$(subst ','\\'',$(subst ','\\'',$(NO_MMAP)))'\\' >>$@+\n> +       @echo USE_WATCHMAN=\\''$(subst ','\\'',$(subst ','\\'',$(USE_WATCHMAN)))'\\' >>$@+\n>  ifdef TEST_OUTPUT_DIRECTORY\n>         @echo TEST_OUTPUT_DIRECTORY=\\''$(subst ','\\'',$(subst ','\\'',$(TEST_OUTPUT_DIRECTORY)))'\\' >>$@+\n>  endif\n> diff --git a/cache.h b/cache.h\n> index f10992d..452aea2 100644\n> --- a/cache.h\n> +++ b/cache.h\n> @@ -696,6 +696,7 @@ extern char *git_replace_ref_base;\n>\n>  extern int fsync_object_files;\n>  extern int core_preload_index;\n> +extern int core_watchman_sync_timeout;\n>  extern int core_apply_sparse_checkout;\n>  extern int precomposed_unicode;\n>  extern int protect_hfs;\n> diff --git a/config.c b/config.c\n> index 9ba40bc..e6dc141 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -882,6 +882,11 @@ static int git_default_core_config(const char *var, const char *value)\n>                 return 0;\n>         }\n>\n> +       if (!strcmp(var, \"core.watchmansynctimeout\")) {\n> +               core_watchman_sync_timeout = git_config_int(var, value);\n> +               return 0;\n> +       }\n> +\n>         if (!strcmp(var, \"core.createobject\")) {\n>                 if (!strcmp(value, \"rename\"))\n>                         object_creation_mode = OBJECT_CREATION_USES_RENAMES;\n> diff --git a/configure.ac b/configure.ac\n> index 0cd9f46..334d63b 100644\n> --- a/configure.ac\n> +++ b/configure.ac\n> @@ -1099,6 +1099,14 @@ AC_COMPILE_IFELSE([BSD_SYSCTL_SRC],\n>         HAVE_BSD_SYSCTL=])\n>  GIT_CONF_SUBST([HAVE_BSD_SYSCTL])\n>\n> +#\n> +# Check for watchman client library\n> +\n> +AC_CHECK_LIB([watchman], [watchman_connect],\n> +       [USE_WATCHMAN=YesPlease],\n> +       [USE_WATCHMAN=])\n> +GIT_CONF_SUBST([USE_WATCHMAN])\n> +\n>  ## Other checks.\n>  # Define USE_PIC if you need the main git objects to be built with -fPIC\n>  # in order to build and link perl/Git.so.  x86-64 seems to need this.\n> diff --git a/environment.c b/environment.c\n> index 6dec9d0..35e03c7 100644\n> --- a/environment.c\n> +++ b/environment.c\n> @@ -94,6 +94,9 @@ int core_preload_index = 1;\n>   */\n>  int ignore_untracked_cache_config;\n>\n> +int core_watchman_sync_timeout = 300;\n> +\n> +\n\nMaybe remove one blank line above.\n\n[...]\n\n> +\n> +       if (!connection) {\n> +               warning(\"Watchman watch error: %s\", wm_error.message);\n> +               return -1;\n> +       }\n> +\n> +       if (watchman_watch(connection, fs_path, &wm_error)) {\n> +               warning(\"Watchman watch error: %s\", wm_error.message);\n> +               watchman_connection_close(connection);\n> +               return -1;\n> +       }\n> +\n> +\n\nHere also.\n"},{"id":"289453","messageId":"CAP8UFD0qW3Xja2F3CyXzeA+-mVCAQ40iGcx6+xuvQnA2BBa3tA@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-17-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 16/20] index-helper: don't run if already running","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-06-17T16:18:56Z","receivedAt":"2016-06-17T16:19:02Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n>\n> diff --git a/index-helper.c b/index-helper.c\n> index 4a171e6..ddc641a 100644\n> --- a/index-helper.c\n> +++ b/index-helper.c\n> @@ -438,6 +438,13 @@ int main(int argc, char **argv)\n>                 return 0;\n>         }\n>\n> +       /* check that no other copy is running */\n> +       fd = unix_stream_connect(git_path(\"index-helper.sock\"));\n> +       if (fd > 0)\n> +               die(_(\"Already running\"));\n\nI wonder is we should also die if fd == 0.\n"},{"id":"289456","messageId":"CAP8UFD0RAgUmKr4tG7xHcz75vpXAeuWi9t1+9mvbGOVzOcHcFA@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-5-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-06-17T16:43:13Z","receivedAt":"2016-06-17T16:43:19Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>\n> +static void loop(int fd, int idle_in_seconds)\n> +{\n> +       assert(idle_in_seconds < INT_MAX / 1000);\n\nThis assert may not be very nice to users setting the value using --exit-after.\n"},{"id":"289515","messageId":"8737ob1on3.fsf@novalis.org","threadId":"42390","inReplyTo":"CACsJy8DzyvFOHU_8xoWUcjej=Ws7_v475=Q2iZmwKhkzYaZ7sg@mail.gmail.com","subject":"Re: [PATCH v12 11/20] index-helper: use watchman to avoid refreshing index with lstat()","fromName":"David Turner","fromEmail":"novalis@novalis.org","sentAt":"2016-06-17T21:39:12Z","receivedAt":"2016-06-17T21:41:26Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"> > -                       if (really && cache_errno == EINVAL) {\n> > +                       if (really || cache_errno == EINVAL) {\n> >                                 /* If we are doing --really-refresh that\n> >                                  * means the index is not valid anymore.\n> >                                  */\n> \n> This looks really odd. I don't see why we would need this. It seems\n > first appeared in your \"do not apply\" patch [1]. Maybe leftover?\n> \n> I found this while re-reading the series and I have not put much time\n> in studying this code yet. So I may be wrong. I'll post again if I\n> find that it's true after some more staring.\nI don't remember the exact details here, but I think we needed this so\nthat we would ever refresh a file that watchman told us had been\nmodified.  We definitely were missing some invalidation if we didn't\nhave it.\nP.S. Please CC novalis@novalis.org on these emails as I don't always\ncheck the git mailing list.\nP.P.S. I am testing out using emacs to send this message, so if\nit ends up screwed up, I apologize.\nDate: Fri, 17 Jun 2016 17:38:03 -0400\nMessage-ID: <874m8r1op0.fsf@novalis.org>\n"},{"id":"289606","messageId":"57662876.3070209@novalis.org","threadId":"42390","inReplyTo":"CACsJy8DzyvFOHU_8xoWUcjej=Ws7_v475=Q2iZmwKhkzYaZ7sg@mail.gmail.com","subject":"Re: [PATCH v12 11/20] index-helper: use watchman to avoid refreshing, index with lstat()","fromName":"David Turner","fromEmail":"novalis@novalis.org","sentAt":"2016-06-19T05:07:02Z","receivedAt":"2016-06-19T05:07:23Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"Duy Nguyen <pclouds@gmail.com> wrote:\n > On Fri, May 20, 2016 at 4:45 AM, David Turner \n<dturner@twopensource.com> wrote:\n > > diff --git a/read-cache.c b/read-cache.c\n > > index 1719f5a..8ec4be3 100644\n > > --- a/read-cache.c\n > >  +++ b/read-cache.c\n > > @@ -1235,7 +1235,7 @@ int refresh_index(struct index_state *istate, \nunsigned int flags,\n > >                 if (!new) {\n > >                         const char *fmt;\n > >\n > > -                       if (really && cache_errno == EINVAL) {\n > > +                       if (really || cache_errno == EINVAL) {\n > >                                 /* If we are doing --really-refresh \nthat\n > >                                  * means the index is not valid \nanymore.\n > >                                  */\n >\n > This looks really odd. I don't see why we would need this. It seems\n > first appeared in your \"do not apply\" patch [1]. Maybe leftover?\n >\n > I found this while re-reading the series and I have not put much time\n > in studying this code yet. So I may be wrong. I'll post again if I\n > find that it's true after some more staring.\n\nI don't remember the exact details here, but I think we needed this so\nthat we would ever refresh a file that watchman told us had been\nmodified.  We definitely were missing some invalidation if we didn't\nhave it.\n\nP.S. Please CC novalis@novalis.org on these emails as I don't always\ncheck the git mailing list.\n\nP.P.S. Since I don't have the original message in my mailbox, I am \ntrying something weird to send this message with a correct in-reply-to \nheader. I something ends up screwed up, I apologize.\n\n-- \nDuy\n"},{"id":"289942","messageId":"576B80AA.4040508@novalis.org","threadId":"42390","inReplyTo":"57662876.3070209@novalis.org","subject":"Re: [PATCH v12 11/20] index-helper: use watchman to avoid refreshing, index with lstat()","fromName":"David Turner","fromEmail":"novalis@novalis.org","sentAt":"2016-06-23T06:24:42Z","receivedAt":"2016-06-23T06:24:47Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"On 06/19/2016 01:07 AM, David Turner wrote:\n> Duy Nguyen <pclouds@gmail.com> wrote:\n>  > On Fri, May 20, 2016 at 4:45 AM, David Turner\n> <dturner@twopensource.com> wrote:\n>  > > diff --git a/read-cache.c b/read-cache.c\n>  > > index 1719f5a..8ec4be3 100644\n>  > > --- a/read-cache.c\n>  > >  +++ b/read-cache.c\n>  > > @@ -1235,7 +1235,7 @@ int refresh_index(struct index_state *istate,\n> unsigned int flags,\n>  > >                 if (!new) {\n>  > >                         const char *fmt;\n>  > >\n>  > > -                       if (really && cache_errno == EINVAL) {\n>  > > +                       if (really || cache_errno == EINVAL) {\n>  > >                                 /* If we are doing --really-refresh\n> that\n>  > >                                  * means the index is not valid\n> anymore.\n>  > >                                  */\n>  >\n>  > This looks really odd. I don't see why we would need this. It seems\n>  > first appeared in your \"do not apply\" patch [1]. Maybe leftover?\n>  >\n>  > I found this while re-reading the series and I have not put much time\n>  > in studying this code yet. So I may be wrong. I'll post again if I\n>  > find that it's true after some more staring.\n>\n> I don't remember the exact details here, but I think we needed this so\n> that we would ever refresh a file that watchman told us had been\n> modified.  We definitely were missing some invalidation if we didn't\n> have it.\n\n... and now I can't repro the problem.  So I'll remove it and re-roll \n(with some other changes)\n\n\n"},{"id":"289953","messageId":"CACsJy8Ao0AhJjQdcUmWTQLKb94S-z8bHJ6eMnHDOW9O9ztHAeg@mail.gmail.com","threadId":"42390","inReplyTo":"576B80AA.4040508@novalis.org","subject":"Re: [PATCH v12 11/20] index-helper: use watchman to avoid refreshing, index with lstat()","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-23T15:33:49Z","receivedAt":"2016-06-23T15:34:23Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, Jun 23, 2016 at 8:24 AM, David Turner <novalis@novalis.org> wrote:\n> So I'll remove it and re-roll (with some other changes)\n\nPlease hold it for a few days. I wanted to have another look at this\nseries, but I've been busy. I should be able to really look at it this\nweekend.\n-- \nDuy\n"},{"id":"290130","messageId":"CACsJy8CKxzpphPB4iST_QzP1BqxYZuPKftyvsovwphysXzGa3A@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-1-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 00/20] index-helper/watchman","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T13:28:28Z","receivedAt":"2016-06-25T13:29:39Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> Of course, as soon as I pinged on the previous version, I noticed an issue.\n> ...\n\nA bit off-topic, but you may want to cut a release for libwatchman\n(even 0.1.0 would do) so it can be properly packaged.\n-- \nDuy\n"},{"id":"290133","messageId":"CACsJy8C82KMjPUdYA-QHz7zCx_pXtSUCL8O4nWJY2Jyit9Q3hw@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-4-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 03/20] pkt-line: add gentle version of packet_write","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T14:01:50Z","receivedAt":"2016-06-25T14:02:52Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> +int packet_flush_gently(int fd)\n> +{\n> +       packet_trace(\"0000\", 4, 1);\n> +       return write_in_full(fd, \"0000\", 4) != 4;\n> +}\n\nThe return value convention here is a bit weird, isn't it? Usually we\nhave \"zero good, minus one  (or negative) bad\" or \"zero bad, positive\ngood\". This one goes \"zero good, one bad\". Same goes for the other\n_gently() function.\n-- \nDuy\n"},{"id":"290134","messageId":"CACsJy8A6C8i_XbXRRBBmT5iqq=mSovVGhFqe0d9WroyMVKf5RA@mail.gmail.com","threadId":"42390","inReplyTo":"CAP8UFD0RAgUmKr4tG7xHcz75vpXAeuWi9t1+9mvbGOVzOcHcFA@mail.gmail.com","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T14:15:32Z","receivedAt":"2016-06-25T14:16:10Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Fri, Jun 17, 2016 at 6:43 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n>> From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>>\n>> +static void loop(int fd, int idle_in_seconds)\n>> +{\n>> +       assert(idle_in_seconds < INT_MAX / 1000);\n>\n> This assert may not be very nice to users setting the value using --exit-after.\n\nYeah. We could catch this after parsing --exit-after (and before\nforking to background, losing stderr) for invalid values and forget\nabout this assert.\n-- \nDuy\n"},{"id":"290135","messageId":"CACsJy8CftPGmrKP8Yeok90T9=whzj69bfE3_X6wHyWOEp6vRzg@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-5-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T14:33:42Z","receivedAt":"2016-06-25T14:34:30Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n>  .gitignore                             |   1 +\n>  Documentation/git-index-helper.txt     |  50 ++++++\n>  Makefile                               |   5 +\n>  cache.h                                |  11 ++\n>  contrib/completion/git-completion.bash |   1 +\n>  git-compat-util.h                      |   1 +\n>  index-helper.c                         | 280 +++++++++++++++++++++++++++++++++\n>  read-cache.c                           | 125 +++++++++++++--\n>  t/t7900-index-helper.sh                |  23 +++\n>  9 files changed, 488 insertions(+), 9 deletions(-)\n>  create mode 100644 Documentation/git-index-helper.txt\n>  create mode 100644 index-helper.c\n>  create mode 100755 t/t7900-index-helper.sh\n\nWe still need to classify this new command in command-list.txt.\npurehelpers probably the easiest choice.\n\n> +NOTES\n> +-----\n> +\n> +$GIT_DIR/index-helper.sock a Unix domain socket that the daemon reads\n> +commands from.  The directory will also contain files named\n> +\"shm-index-<SHA1>\".  These are used as backing stores for shared\n> +memory.  Normally the daemon will clean up these files when it exits\n> +or when they are no longer relevant.  But if it crashes, some objects\n> +could remain there and they can be safely deleted with \"rm\"\n> +command. The following commands are used to control the daemon:\n> +\n> +\"refresh\"::\n> +       Reread the index.\n> +\n> +\"poke\":\n> +       Let the daemon know the index is to be read. It keeps the\n> +       daemon alive longer, unless `--exit-after=0` is used.\n> +\n> +All commands and replies are terminated by a NUL byte.\n\nI think it's \"all commands are replies are in pkt-line format\" now.\n\n> diff --git a/Makefile b/Makefile\n> index 2742a69..c8be0e7 100644\n> --- a/Makefile\n> +++ b/Makefile\n> @@ -1433,6 +1433,10 @@ ifdef HAVE_DEV_TTY\n>         BASIC_CFLAGS += -DHAVE_DEV_TTY\n>  endif\n>\n> +ifndef NO_MMAP\n> +       PROGRAM_OBJS += index-helper.o\n> +endif\n\nWe need unix-socket.c too, which is only built when NO_UNIX_SOCKETS\nare not defined. We don't have any platform where mmap is supported\nbut unix socket is not, do we?\n\n(a couple of minutes later, reading to read-cache.c..) well\npoke_daemon() calls unix_stream_connect() without any #ifdef, if\nNO_UNIX_SOCKETS is set, then it would lead to undefined reference\nerror.\n\n> +static void set_socket_blocking_flag(int fd, int make_nonblocking)\n> +{\n> +       int flags;\n> +\n> +       flags = fcntl(fd, F_GETFL, NULL);\n> +\n> +       if (flags < 0)\n> +               die(_(\"fcntl failed\"));\n> +\n> +       if (make_nonblocking)\n> +               flags |= O_NONBLOCK;\n> +       else\n> +               flags &= ~O_NONBLOCK;\n> +\n> +       if (fcntl(fd, F_SETFL, flags) < 0)\n> +               die(_(\"fcntl failed\"));\n> +}\n> ...\n> +               client_fd = accept(fd, NULL, NULL);\n> +               if (client_fd < 0)\n> +                       /*\n> +                        * An error here is unlikely -- it probably\n> +                        * indicates that the connecting process has\n> +                        * already dropped the connection.\n> +                        */\n> +                       continue;\n> +\n> +               /*\n> +                * Our connection to the client is blocking since a client\n> +                * can always be killed by SIGINT or similar.\n> +                */\n> +               set_socket_blocking_flag(client_fd, 0);\n\nOut of curiosity, do we really need this? I thought default behavior\nwas always blocking (and checked linux kernel, it seemed to agree with\nme). Maybe for extra safety because other OSes may default to\nsomething else?\n\n> +static int poke_daemon(struct index_state *istate,\n> +                      const struct stat *st, int refresh_cache)\n> +{\n> +       int fd;\n> +       int ret = 0;\n> +       const char *socket_path;\n> +\n> +       /* if this is from index-helper, do not poke itself (recursively) */\n> +       if (istate->to_shm)\n> +               return 0;\n> +\n> +       socket_path = git_path(\"index-helper.sock\");\n> +       if (!socket_path)\n> +               return -1;\n\nThis \"if\" is later removed. But you can remove it now if you re-roll,\ngit_path() must not fail or we are screwed up long before this.\n-- \nDuy\n"},{"id":"290136","messageId":"CACsJy8BbfjoyTpgB6+rgHW-HORjs20KLP-gU2q19Od1HgjkYcA@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-6-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 05/20] index-helper: add --strict","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T14:43:52Z","receivedAt":"2016-06-25T14:44:27Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> +--strict::\n> +--no-strict::\n> +       Strict mode makes index-helper verify the shared memory after\n> +       it's created. If the result does not match what's read from\n> +       $GIT_DIR/index, the shared memory is destroyed. This makes\n> +       index-helper take more than double the amount of time required\n> +       for reading an index, but because it will happen in the\n> +       background, it's not noticable.\n\nSlightly off-topic, but for big indexes, running by a script or\nsomething, then the window for reading/verifying is not big and this\nstrict verification will become noticable.\n\n>  `--strict` is enabled by default.\n\n.. so we probably still want --strict by default for now. After this\nfeature exits the experimental stage, we can switch it off.\n-- \nDuy\n"},{"id":"290137","messageId":"CACsJy8ANHtf800O+ZGxYRV4mGrLighx+R3kd19jYJA1Vatd+Ow@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-8-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 07/20] index-helper: add --detach","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T14:46:16Z","receivedAt":"2016-06-25T14:46:51Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>\n> We detach after creating and opening the socket, because otherwise\n> we might return control to the shell before index-helper is ready to\n> accept commands.  This might lead to flaky tests.\n\nAnother good reason for delaying detach as much as possible is we can\nprint errors to stderr instead of to file.\n-- \nDuy\n"},{"id":"290138","messageId":"CACsJy8CG3y3VM-RgzV8G46BP5POqEV7vBa5uN-5GJq4gSEKx3A@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-10-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 09/20] read-cache: add watchman 'WAMA' extension","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T14:59:11Z","receivedAt":"2016-06-25T14:59:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> @@ -1222,8 +1225,13 @@ int refresh_index(struct index_state *istate, unsigned int flags,\n>                         continue;\n>\n>                 new = refresh_cache_ent(istate, ce, options, &cache_errno, &changed);\n> -               if (new == ce)\n> +               if (new == ce) {\n> +                       if (ce->ce_flags & CE_WATCHMAN_DIRTY) {\n\nI would add a note here since it took me a bit of time to understand\nwhy we cleared this. Maybe something like this?\n\n\"The rule is index-helper sets watchman-dirty when a file is changed,\nthen git clears it when it has verified that in-index entry now\nmatches the worktree version. index-helper does not clear the bit and\ngit does not set it. We have verified here that stat info (and even\ncontent) matches, so it's safe to clear watchman-dirty now\"\n-- \nDuy\n"},{"id":"290140","messageId":"CACsJy8DYTcF_W+5K9uAHuqqBFu8J=xj7VRx-aqM6Dtc2+GmS1Q@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-12-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 11/20] index-helper: use watchman to avoid refreshing index with lstat()","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T16:27:48Z","receivedAt":"2016-06-25T16:28:23Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> From: Nguyễn Thái Ngọc Duy <pclouds@g\n> +\"poke <path>\":\n\nThe new format is \"poke <pid> <capabilities>\".\n\n> +       Like \"poke\", but replies with \"OK\".  If the index has the\n> +       watchman extension, index-helper queries watchman, then\n> +       prepares a shared memory object with the watchman index\n> +       extension before replying.\n> +\n-- \nDuy\n"},{"id":"290141","messageId":"CACsJy8B3t+ChhgDJJyr+W3PiF2QmavnU19BvQV84w_=4x9QobA@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-13-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 12/20] update-index: enable/disable watchman support","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T16:31:01Z","receivedAt":"2016-06-25T16:32:11Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> @@ -1149,6 +1152,19 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)\n>                 die(\"Bug: bad untracked_cache value: %d\", untracked_cache);\n>         }\n>\n> +       if (use_watchman > 0) {\n> +               the_index.last_update    = xstrdup(\"\");\n> +               the_index.cache_changed |= WATCHMAN_CHANGED;\n> +#ifndef USE_WATCHMAN\n> +               warning(\"git was built without watchman support -- I'm \"\n> +                       \"adding the extension here, but it probably won't \"\n> +                       \"do you any good.\");\n\n_() this string. And maybe rephrase a bit, avoiding \"I'm adding\", e.g.\n\"git was built without watchman support, this command probably will\nnot result in any speedups\"\n\n> +#endif\n> +       } else if (!use_watchman) {\n> +               the_index.last_update    = NULL;\n> +               the_index.cache_changed |= WATCHMAN_CHANGED;\n> +       }\n> +\n>         if (active_cache_changed) {\n>                 if (newfd < 0) {\n>                         if (refresh_args.flags & REFRESH_QUIET)\n> --\n> 2.4.2.767.g62658d5-twtrsrc\n>\n-- \nDuy\n"},{"id":"290142","messageId":"CACsJy8BRQdWygX1Cp9d_2K7jUx0R5e0EiExmY0_wOA_Hi8TfuQ@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-18-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 17/20] index-helper: autorun mode","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T16:35:47Z","receivedAt":"2016-06-25T16:36:22Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> @@ -417,6 +418,7 @@ int main(int argc, char **argv)\n>                          \"verify shared memory after creating\"),\n>                 OPT_BOOL(0, \"detach\", &detach, \"detach the process\"),\n>                 OPT_BOOL(0, \"kill\", &kill, \"request that existing index helper processes exit\"),\n> +               OPT_BOOL(0, \"autorun\", &autorun, \"this is an automatic run of git index-helper, so certain errors can be solved by silently exiting\"),\n\nI just noticed (lately) that all options since --strict has been added\nwithout _N() wrapped around the help string and they won't be\ntranslated as a result. If autorun, for instance, is meant for\ninternal use only (which does not make sense since you document it in\nindex-helper.txt), you can hide it away and not worry about N_().\n-- \nDuy\n"},{"id":"290143","messageId":"CACsJy8CaN4fkd9tzvR8NeqpVHmKH5p6S151-A+SN0_LtrU3_fw@mail.gmail.com","threadId":"42390","inReplyTo":"1463694357-6503-19-git-send-email-dturner@twopensource.com","subject":"Re: [PATCH v12 18/20] index-helper: optionally automatically run","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T16:38:36Z","receivedAt":"2016-06-25T16:39:12Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n> +static void autorun_index_helper(void)\n> +{\n> +       const char *argv[] = {\"git-index-helper\", \"--detach\", \"--autorun\", NULL};\n> +       if (want_auto_index_helper() <= 0)\n> +               return;\n> +\n> +       trace_argv_printf(argv, \"trace: auto index-helper:\");\n\nI'm pretty sure run_command() does call this (with \"trace:\nrun_command:\" prefix instead, maybe drop this line?\n\n> +\n> +       if (run_command_v_opt(argv,\n> +                             RUN_SILENT_EXEC_FAILURE | RUN_CLEAN_ON_EXIT))\n> +               warning(_(\"You specified indexhelper.autorun, but running git-index-helper failed.\"));\n> +}\n-- \nDuy\n"},{"id":"290144","messageId":"CACsJy8Dmr8QSSMycetPH2WNECc0A=cOzG6o+jffT4RSWwVC+Vw@mail.gmail.com","threadId":"42390","inReplyTo":"CACsJy8CKxzpphPB4iST_QzP1BqxYZuPKftyvsovwphysXzGa3A@mail.gmail.com","subject":"Re: [PATCH v12 00/20] index-helper/watchman","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-25T16:42:48Z","receivedAt":"2016-06-25T16:43:23Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sat, Jun 25, 2016 at 3:28 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n>> Of course, as soon as I pinged on the previous version, I noticed an issue.\n>> ...\n>\n> A bit off-topic, but you may want to cut a release for libwatchman\n> (even 0.1.0 would do) so it can be properly packaged.\n\nAnd I'm done with v12! I think most of my comments are minor thing\nexcept the \"really || cache_errno == EINVAL\" change (which I still\ndon't see how it's related to watchman).\n-- \nDuy\n"},{"id":"290149","messageId":"576ED9A2.8070202@novalis.org","threadId":"42390","inReplyTo":"CACsJy8CftPGmrKP8Yeok90T9=whzj69bfE3_X6wHyWOEp6vRzg@mail.gmail.com","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"David Turner","fromEmail":"novalis@novalis.org","sentAt":"2016-06-25T19:21:06Z","receivedAt":"2016-06-25T19:21:12Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"On 06/25/2016 10:33 AM, Duy Nguyen wrote:\n>> +               /*\n>> +                * Our connection to the client is blocking since a client\n>> +                * can always be killed by SIGINT or similar.\n>> +                */\n>> +               set_socket_blocking_flag(client_fd, 0);\n>\n> Out of curiosity, do we really need this? I thought default behavior\n> was always blocking (and checked linux kernel, it seemed to agree with\n> me). Maybe for extra safety because other OSes may default to\n> something else?\n\nYes -- see this bug report for details:\nhttps://bugs.python.org/issue7995\n\n"},{"id":"290153","messageId":"576F553C.8020107@novalis.org","threadId":"42390","inReplyTo":"CACsJy8CKxzpphPB4iST_QzP1BqxYZuPKftyvsovwphysXzGa3A@mail.gmail.com","subject":"Re: [PATCH v12 00/20] index-helper/watchman","fromName":"David Turner","fromEmail":"novalis@novalis.org","sentAt":"2016-06-26T04:08:28Z","receivedAt":"2016-06-26T04:08:49Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"On 06/25/2016 09:28 AM, Duy Nguyen wrote:\n> On Thu, May 19, 2016 at 11:45 PM, David Turner <dturner@twopensource.com> wrote:\n>> Of course, as soon as I pinged on the previous version, I noticed an issue.\n>> ...\n>\n> A bit off-topic, but you may want to cut a release for libwatchman\n> (even 0.1.0 would do) so it can be properly packaged.\n>\n\nI've mentioned this to Keith.  Unfortunately, he doesn't seem to have \nwrite access to the repo yet, but hopefully the Twitter folks will soon \nfix this.\n"},{"id":"290173","messageId":"CACsJy8Dqvv-Ty-wG0qkenyvLNLyqVueJmhjiQXnr0zVUGFvDeA@mail.gmail.com","threadId":"42390","inReplyTo":"576ED9A2.8070202@novalis.org","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-26T04:27:46Z","receivedAt":"2016-06-26T04:28:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sat, Jun 25, 2016 at 9:21 PM, David Turner <novalis@novalis.org> wrote:\n> On 06/25/2016 10:33 AM, Duy Nguyen wrote:\n>>>\n>>> +               /*\n>>> +                * Our connection to the client is blocking since a\n>>> client\n>>> +                * can always be killed by SIGINT or similar.\n>>> +                */\n>>> +               set_socket_blocking_flag(client_fd, 0);\n>>\n>>\n>> Out of curiosity, do we really need this? I thought default behavior\n>> was always blocking (and checked linux kernel, it seemed to agree with\n>> me). Maybe for extra safety because other OSes may default to\n>> something else?\n>\n>\n> Yes -- see this bug report for details:\n> https://bugs.python.org/issue7995\n>\n\nI think we should refer to this issue in the comment block right\nbefore set_socket_blocking_flag() call. Imagine a year from now, I may\nread the code, decide this code is useless and try to remove it.\n\n-- \nDuy\n"},{"id":"290184","messageId":"20160626085347.GA4053@dcvr.yhbt.net","threadId":"42390","inReplyTo":"576ED9A2.8070202@novalis.org","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"Eric Wong","fromEmail":"e@80x24.org","sentAt":"2016-06-26T08:53:47Z","receivedAt":"2016-06-26T08:54:07Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"David Turner <novalis@novalis.org> wrote:\n> On 06/25/2016 10:33 AM, Duy Nguyen wrote:\n> >>+               /*\n> >>+                * Our connection to the client is blocking since a client\n> >>+                * can always be killed by SIGINT or similar.\n> >>+                */\n> >>+               set_socket_blocking_flag(client_fd, 0);\n> >\n> >Out of curiosity, do we really need this? I thought default behavior\n> >was always blocking (and checked linux kernel, it seemed to agree with\n> >me). Maybe for extra safety because other OSes may default to\n> >something else?\n> \n> Yes -- see this bug report for details:\n> https://bugs.python.org/issue7995\n\nI realize it's an issue with BSDs, but it still seems\nunnecessary, here:\n\n1) the packet_read => get_packet_data => read_in_full => xread\n   call chain already poll()s on EAGAIN/EWOULDBLOCK.\n   write_in_full => xwrite busy loops on EAGAIN/EWOULDBLOCK.\n   xwrite should probably poll, too; but I guess EAGAIN is\n   uncommon with small writes.\n\n2) you create the listen fd you call accept on and never set\n   non-blocking on it.  It might be an issue one day if\n   we use socket activation and inherit a socket, but the\n   retries mentioned in 1) should cover that case.\n"},{"id":"290198","messageId":"577015C5.8070407@novalis.org","threadId":"42390","inReplyTo":"20160626085347.GA4053@dcvr.yhbt.net","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"David Turner","fromEmail":"novalis@novalis.org","sentAt":"2016-06-26T17:49:57Z","receivedAt":"2016-06-26T17:50:18Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"On 06/26/2016 04:53 AM, Eric Wong wrote:\n> David Turner <novalis@novalis.org> wrote:\n>> On 06/25/2016 10:33 AM, Duy Nguyen wrote:\n>>>> +               /*\n>>>> +                * Our connection to the client is blocking since a client\n>>>> +                * can always be killed by SIGINT or similar.\n>>>> +                */\n>>>> +               set_socket_blocking_flag(client_fd, 0);\n>>>\n>>> Out of curiosity, do we really need this? I thought default behavior\n>>> was always blocking (and checked linux kernel, it seemed to agree with\n>>> me). Maybe for extra safety because other OSes may default to\n>>> something else?\n>>\n>> Yes -- see this bug report for details:\n>> https://bugs.python.org/issue7995\n>\n> I realize it's an issue with BSDs, but it still seems\n> unnecessary, here:\n>\n> 1) the packet_read => get_packet_data => read_in_full => xread\n>     call chain already poll()s on EAGAIN/EWOULDBLOCK.\n>     write_in_full => xwrite busy loops on EAGAIN/EWOULDBLOCK.\n>     xwrite should probably poll, too; but I guess EAGAIN is\n>     uncommon with small writes.\n\nThat is a CPU-burning busy loop on a non-blocking socket.\n\n> 2) you create the listen fd you call accept on and never set\n>     non-blocking on it.  It might be an issue one day if\n>     we use socket activation and inherit a socket, but the\n>     retries mentioned in 1) should cover that case.\n\nHm.  I wonder why I thought I had made it non-blocking.\n\nI'm sort of reluctant to change this as I no longer have a Mac to test \non.  Does someone else have a Mac that they can use to test this?\n\n"},{"id":"290202","messageId":"57702D2F.9080306@novalis.org","threadId":"42390","inReplyTo":"CACsJy8Dqvv-Ty-wG0qkenyvLNLyqVueJmhjiQXnr0zVUGFvDeA@mail.gmail.com","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"David Turner","fromEmail":"novalis@novalis.org","sentAt":"2016-06-26T19:29:51Z","receivedAt":"2016-06-26T19:30:14Z","isPatch":true,"sender":{"key":"novalis@novalis.org","avatar":"https://avatars.githubusercontent.com/u/77003?v=4"},"body":"On 06/26/2016 12:27 AM, Duy Nguyen wrote:\n> On Sat, Jun 25, 2016 at 9:21 PM, David Turner <novalis@novalis.org> wrote:\n>> On 06/25/2016 10:33 AM, Duy Nguyen wrote:\n>>>>\n>>>> +               /*\n>>>> +                * Our connection to the client is blocking since a\n>>>> client\n>>>> +                * can always be killed by SIGINT or similar.\n>>>> +                */\n>>>> +               set_socket_blocking_flag(client_fd, 0);\n>>>\n>>>\n>>> Out of curiosity, do we really need this? I thought default behavior\n>>> was always blocking (and checked linux kernel, it seemed to agree with\n>>> me). Maybe for extra safety because other OSes may default to\n>>> something else?\n>>\n>>\n>> Yes -- see this bug report for details:\n>> https://bugs.python.org/issue7995\n>>\n>\n> I think we should refer to this issue in the comment block right\n> before set_socket_blocking_flag() call. Imagine a year from now, I may\n> read the code, decide this code is useless and try to remove it.\n\nAssuming that we do keep this (see Eric Wong's note), I do not think we \nneed a comment.  It is documented in the man page for accept[1], and it \nis the reader's responsibility to understand standard POSIX APIs.\n\n\n\n[1] \"On Linux, the new socket returned by accept()  does  not  inherit \nfile status  flags such as O_NONBLOCK and O_ASYNC from the listening \nsocket. This behavior differs from the canonical  BSD  sockets \nimplementation.\"\n\n\n"},{"id":"290211","messageId":"20160626232500.GA3426@dcvr.yhbt.net","threadId":"42390","inReplyTo":"577015C5.8070407@novalis.org","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"Eric Wong","fromEmail":"e@80x24.org","sentAt":"2016-06-26T23:25:00Z","receivedAt":"2016-06-26T23:25:07Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"David Turner <novalis@novalis.org> wrote:\n> On 06/26/2016 04:53 AM, Eric Wong wrote:\n> >David Turner <novalis@novalis.org> wrote:\n> >>On 06/25/2016 10:33 AM, Duy Nguyen wrote:\n> >>>>+               /*\n> >>>>+                * Our connection to the client is blocking since a client\n> >>>>+                * can always be killed by SIGINT or similar.\n> >>>>+                */\n> >>>>+               set_socket_blocking_flag(client_fd, 0);\n> >>>\n> >>>Out of curiosity, do we really need this? I thought default behavior\n> >>>was always blocking (and checked linux kernel, it seemed to agree with\n> >>>me). Maybe for extra safety because other OSes may default to\n> >>>something else?\n> >>\n> >>Yes -- see this bug report for details:\n> >>https://bugs.python.org/issue7995\n> >\n> >I realize it's an issue with BSDs, but it still seems\n> >unnecessary, here:\n> >\n> >1) the packet_read => get_packet_data => read_in_full => xread\n> >    call chain already poll()s on EAGAIN/EWOULDBLOCK.\n> >    write_in_full => xwrite busy loops on EAGAIN/EWOULDBLOCK.\n> >    xwrite should probably poll, too; but I guess EAGAIN is\n> >    uncommon with small writes.\n> \n> That is a CPU-burning busy loop on a non-blocking socket.\n\nIndeed, fixes proposed in (xread was also broken(!)):\nhttp://mid.gmane.org/20160626232112.721-3-e@80x24.org\nhttp://mid.gmane.org/20160626232112.721-2-e@80x24.org\nhttp://mid.gmane.org/20160626232112.721-1-e@80x24.org\n"},{"id":"290220","messageId":"CACsJy8Au47OpLcK14=gqkSXMhmeHXRCKRR8rQaBbBxVBLgWo6Q@mail.gmail.com","threadId":"42390","inReplyTo":"57702D2F.9080306@novalis.org","subject":"Re: [PATCH v12 04/20] index-helper: new daemon for caching index and related stuff","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-06-27T05:50:14Z","receivedAt":"2016-06-27T05:50:48Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Jun 26, 2016 at 9:29 PM, David Turner <novalis@novalis.org> wrote:\n> On 06/26/2016 12:27 AM, Duy Nguyen wrote:\n>>\n>> On Sat, Jun 25, 2016 at 9:21 PM, David Turner <novalis@novalis.org> wrote:\n>>>\n>>> On 06/25/2016 10:33 AM, Duy Nguyen wrote:\n>>>>>\n>>>>>\n>>>>> +               /*\n>>>>> +                * Our connection to the client is blocking since a\n>>>>> client\n>>>>> +                * can always be killed by SIGINT or similar.\n>>>>> +                */\n>>>>> +               set_socket_blocking_flag(client_fd, 0);\n>>>>\n>>>>\n>>>>\n>>>> Out of curiosity, do we really need this? I thought default behavior\n>>>> was always blocking (and checked linux kernel, it seemed to agree with\n>>>> me). Maybe for extra safety because other OSes may default to\n>>>> something else?\n>>>\n>>>\n>>>\n>>> Yes -- see this bug report for details:\n>>> https://bugs.python.org/issue7995\n>>>\n>>\n>> I think we should refer to this issue in the comment block right\n>> before set_socket_blocking_flag() call. Imagine a year from now, I may\n>> read the code, decide this code is useless and try to remove it.\n>\n>\n> Assuming that we do keep this (see Eric Wong's note), I do not think we need\n> a comment.  It is documented in the man page for accept[1], and it is the\n> reader's responsibility to understand standard POSIX APIs.\n>\n>\n>\n> [1] \"On Linux, the new socket returned by accept()  does  not  inherit file\n> status  flags such as O_NONBLOCK and O_ASYNC from the listening socket. This\n> behavior differs from the canonical  BSD  sockets implementation.\"\n\nBut we do not ever set O_NONBLOCK on listening socket, and the default\nshould be no O_NONBLOCK at socket creation even on BSDs, right? If we\nknow where this O_NONBLOCK comes from then we can clear it at that\nplace (maybe with #if BSD to emphasize) instead of after accept(). The\nbug report actually confuses me because \"timeout is set\" then\nO_NONBLOCK is also set, but I don't know exactly what \"timeout is set\"\nis, on poll()? I'll check out socketmodule.c later.\n-- \nDuy\n"}]}