Browse Source
When the index is read from disk, the fsmonitor index extension is used to flag the last known potentially dirty index entries. The registered core.fsmonitor command is called with the time the index was last updated and returns the list of files changed since that time. This list is used to flag any additional dirty cache entries and untracked cache directories. We can then use this valid state to speed up preload_index(), ie_match_stat(), and refresh_cache_ent() as they do not need to lstat() files to detect potential changes for those entries marked CE_FSMONITOR_VALID. In addition, if the untracked cache is turned on valid_cached_dir() can skip checking directories for new or changed files as fsmonitor will invalidate the cache only for those directories that have been identified as having potential changes. To keep the CE_FSMONITOR_VALID state accurate during git operations; when git updates a cache entry to match the current state on disk, it will now set the CE_FSMONITOR_VALID bit. Inversely, anytime git changes a cache entry, the CE_FSMONITOR_VALID bit is cleared and the corresponding untracked cache directory is marked invalid. Signed-off-by: Ben Peart <benpeart@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>maint
Ben Peart
7 years ago
committed by
Junio C Hamano
16 changed files with 417 additions and 19 deletions
@ -0,0 +1,253 @@
@@ -0,0 +1,253 @@
|
||||
#include "cache.h" |
||||
#include "config.h" |
||||
#include "dir.h" |
||||
#include "ewah/ewok.h" |
||||
#include "fsmonitor.h" |
||||
#include "run-command.h" |
||||
#include "strbuf.h" |
||||
|
||||
#define INDEX_EXTENSION_VERSION (1) |
||||
#define HOOK_INTERFACE_VERSION (1) |
||||
|
||||
struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR); |
||||
|
||||
static void fsmonitor_ewah_callback(size_t pos, void *is) |
||||
{ |
||||
struct index_state *istate = (struct index_state *)is; |
||||
struct cache_entry *ce = istate->cache[pos]; |
||||
|
||||
ce->ce_flags &= ~CE_FSMONITOR_VALID; |
||||
} |
||||
|
||||
int read_fsmonitor_extension(struct index_state *istate, const void *data, |
||||
unsigned long sz) |
||||
{ |
||||
const char *index = data; |
||||
uint32_t hdr_version; |
||||
uint32_t ewah_size; |
||||
struct ewah_bitmap *fsmonitor_dirty; |
||||
int i; |
||||
int ret; |
||||
|
||||
if (sz < sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t)) |
||||
return error("corrupt fsmonitor extension (too short)"); |
||||
|
||||
hdr_version = get_be32(index); |
||||
index += sizeof(uint32_t); |
||||
if (hdr_version != INDEX_EXTENSION_VERSION) |
||||
return error("bad fsmonitor version %d", hdr_version); |
||||
|
||||
istate->fsmonitor_last_update = get_be64(index); |
||||
index += sizeof(uint64_t); |
||||
|
||||
ewah_size = get_be32(index); |
||||
index += sizeof(uint32_t); |
||||
|
||||
fsmonitor_dirty = ewah_new(); |
||||
ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size); |
||||
if (ret != ewah_size) { |
||||
ewah_free(fsmonitor_dirty); |
||||
return error("failed to parse ewah bitmap reading fsmonitor index extension"); |
||||
} |
||||
|
||||
if (git_config_get_fsmonitor()) { |
||||
/* Mark all entries valid */ |
||||
for (i = 0; i < istate->cache_nr; i++) |
||||
istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID; |
||||
|
||||
/* Mark all previously saved entries as dirty */ |
||||
ewah_each_bit(fsmonitor_dirty, fsmonitor_ewah_callback, istate); |
||||
|
||||
/* Now mark the untracked cache for fsmonitor usage */ |
||||
if (istate->untracked) |
||||
istate->untracked->use_fsmonitor = 1; |
||||
} |
||||
ewah_free(fsmonitor_dirty); |
||||
|
||||
trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful"); |
||||
return 0; |
||||
} |
||||
|
||||
void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate) |
||||
{ |
||||
uint32_t hdr_version; |
||||
uint64_t tm; |
||||
struct ewah_bitmap *bitmap; |
||||
int i; |
||||
uint32_t ewah_start; |
||||
uint32_t ewah_size = 0; |
||||
int fixup = 0; |
||||
|
||||
put_be32(&hdr_version, INDEX_EXTENSION_VERSION); |
||||
strbuf_add(sb, &hdr_version, sizeof(uint32_t)); |
||||
|
||||
put_be64(&tm, istate->fsmonitor_last_update); |
||||
strbuf_add(sb, &tm, sizeof(uint64_t)); |
||||
fixup = sb->len; |
||||
strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */ |
||||
|
||||
ewah_start = sb->len; |
||||
bitmap = ewah_new(); |
||||
for (i = 0; i < istate->cache_nr; i++) |
||||
if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID)) |
||||
ewah_set(bitmap, i); |
||||
ewah_serialize_strbuf(bitmap, sb); |
||||
ewah_free(bitmap); |
||||
|
||||
/* fix up size field */ |
||||
put_be32(&ewah_size, sb->len - ewah_start); |
||||
memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t)); |
||||
|
||||
trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful"); |
||||
} |
||||
|
||||
/* |
||||
* Call the query-fsmonitor hook passing the time of the last saved results. |
||||
*/ |
||||
static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result) |
||||
{ |
||||
struct child_process cp = CHILD_PROCESS_INIT; |
||||
char ver[64]; |
||||
char date[64]; |
||||
const char *argv[4]; |
||||
|
||||
if (!(argv[0] = core_fsmonitor)) |
||||
return -1; |
||||
|
||||
snprintf(ver, sizeof(version), "%d", version); |
||||
snprintf(date, sizeof(date), "%" PRIuMAX, (uintmax_t)last_update); |
||||
argv[1] = ver; |
||||
argv[2] = date; |
||||
argv[3] = NULL; |
||||
cp.argv = argv; |
||||
cp.use_shell = 1; |
||||
|
||||
return capture_command(&cp, query_result, 1024); |
||||
} |
||||
|
||||
static void fsmonitor_refresh_callback(struct index_state *istate, const char *name) |
||||
{ |
||||
int pos = index_name_pos(istate, name, strlen(name)); |
||||
|
||||
if (pos >= 0) { |
||||
struct cache_entry *ce = istate->cache[pos]; |
||||
ce->ce_flags &= ~CE_FSMONITOR_VALID; |
||||
} |
||||
|
||||
/* |
||||
* Mark the untracked cache dirty even if it wasn't found in the index |
||||
* as it could be a new untracked file. |
||||
*/ |
||||
trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name); |
||||
untracked_cache_invalidate_path(istate, name); |
||||
} |
||||
|
||||
void refresh_fsmonitor(struct index_state *istate) |
||||
{ |
||||
static int has_run_once = 0; |
||||
struct strbuf query_result = STRBUF_INIT; |
||||
int query_success = 0; |
||||
size_t bol; /* beginning of line */ |
||||
uint64_t last_update; |
||||
char *buf; |
||||
int i; |
||||
|
||||
if (!core_fsmonitor || has_run_once) |
||||
return; |
||||
has_run_once = 1; |
||||
|
||||
trace_printf_key(&trace_fsmonitor, "refresh fsmonitor"); |
||||
/* |
||||
* This could be racy so save the date/time now and query_fsmonitor |
||||
* should be inclusive to ensure we don't miss potential changes. |
||||
*/ |
||||
last_update = getnanotime(); |
||||
|
||||
/* |
||||
* If we have a last update time, call query_fsmonitor for the set of |
||||
* changes since that time, else assume everything is possibly dirty |
||||
* and check it all. |
||||
*/ |
||||
if (istate->fsmonitor_last_update) { |
||||
query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION, |
||||
istate->fsmonitor_last_update, &query_result); |
||||
trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor); |
||||
trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s", |
||||
core_fsmonitor, query_success ? "success" : "failure"); |
||||
} |
||||
|
||||
/* a fsmonitor process can return '/' to indicate all entries are invalid */ |
||||
if (query_success && query_result.buf[0] != '/') { |
||||
/* Mark all entries returned by the monitor as dirty */ |
||||
buf = query_result.buf; |
||||
bol = 0; |
||||
for (i = 0; i < query_result.len; i++) { |
||||
if (buf[i] != '\0') |
||||
continue; |
||||
fsmonitor_refresh_callback(istate, buf + bol); |
||||
bol = i + 1; |
||||
} |
||||
if (bol < query_result.len) |
||||
fsmonitor_refresh_callback(istate, buf + bol); |
||||
} else { |
||||
/* Mark all entries invalid */ |
||||
for (i = 0; i < istate->cache_nr; i++) |
||||
istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; |
||||
|
||||
if (istate->untracked) |
||||
istate->untracked->use_fsmonitor = 0; |
||||
} |
||||
strbuf_release(&query_result); |
||||
|
||||
/* Now that we've updated istate, save the last_update time */ |
||||
istate->fsmonitor_last_update = last_update; |
||||
} |
||||
|
||||
void add_fsmonitor(struct index_state *istate) |
||||
{ |
||||
int i; |
||||
|
||||
if (!istate->fsmonitor_last_update) { |
||||
trace_printf_key(&trace_fsmonitor, "add fsmonitor"); |
||||
istate->cache_changed |= FSMONITOR_CHANGED; |
||||
istate->fsmonitor_last_update = getnanotime(); |
||||
|
||||
/* reset the fsmonitor state */ |
||||
for (i = 0; i < istate->cache_nr; i++) |
||||
istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID; |
||||
|
||||
/* reset the untracked cache */ |
||||
if (istate->untracked) { |
||||
add_untracked_cache(istate); |
||||
istate->untracked->use_fsmonitor = 1; |
||||
} |
||||
|
||||
/* Update the fsmonitor state */ |
||||
refresh_fsmonitor(istate); |
||||
} |
||||
} |
||||
|
||||
void remove_fsmonitor(struct index_state *istate) |
||||
{ |
||||
if (istate->fsmonitor_last_update) { |
||||
trace_printf_key(&trace_fsmonitor, "remove fsmonitor"); |
||||
istate->cache_changed |= FSMONITOR_CHANGED; |
||||
istate->fsmonitor_last_update = 0; |
||||
} |
||||
} |
||||
|
||||
void tweak_fsmonitor(struct index_state *istate) |
||||
{ |
||||
switch (git_config_get_fsmonitor()) { |
||||
case -1: /* keep: do nothing */ |
||||
break; |
||||
case 0: /* false */ |
||||
remove_fsmonitor(istate); |
||||
break; |
||||
case 1: /* true */ |
||||
add_fsmonitor(istate); |
||||
break; |
||||
default: /* unknown value: do nothing */ |
||||
break; |
||||
} |
||||
} |
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
#ifndef FSMONITOR_H |
||||
#define FSMONITOR_H |
||||
|
||||
extern struct trace_key trace_fsmonitor; |
||||
|
||||
/* |
||||
* Read the fsmonitor index extension and (if configured) restore the |
||||
* CE_FSMONITOR_VALID state. |
||||
*/ |
||||
extern int read_fsmonitor_extension(struct index_state *istate, const void *data, unsigned long sz); |
||||
|
||||
/* |
||||
* Write the CE_FSMONITOR_VALID state into the fsmonitor index extension. |
||||
*/ |
||||
extern void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate); |
||||
|
||||
/* |
||||
* Add/remove the fsmonitor index extension |
||||
*/ |
||||
extern void add_fsmonitor(struct index_state *istate); |
||||
extern void remove_fsmonitor(struct index_state *istate); |
||||
|
||||
/* |
||||
* Add/remove the fsmonitor index extension as necessary based on the current |
||||
* core.fsmonitor setting. |
||||
*/ |
||||
extern void tweak_fsmonitor(struct index_state *istate); |
||||
|
||||
/* |
||||
* Run the configured fsmonitor integration script and clear the |
||||
* CE_FSMONITOR_VALID bit for any files returned as dirty. Also invalidate |
||||
* any corresponding untracked cache directory structures. Optimized to only |
||||
* run the first time it is called. |
||||
*/ |
||||
extern void refresh_fsmonitor(struct index_state *istate); |
||||
|
||||
/* |
||||
* Set the given cache entries CE_FSMONITOR_VALID bit. This should be |
||||
* called any time the cache entry has been updated to reflect the |
||||
* current state of the file on disk. |
||||
*/ |
||||
static inline void mark_fsmonitor_valid(struct cache_entry *ce) |
||||
{ |
||||
if (core_fsmonitor) { |
||||
ce->ce_flags |= CE_FSMONITOR_VALID; |
||||
trace_printf_key(&trace_fsmonitor, "mark_fsmonitor_clean '%s'", ce->name); |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* Clear the given cache entry's CE_FSMONITOR_VALID bit and invalidate |
||||
* any corresponding untracked cache directory structures. This should |
||||
* be called any time git creates or modifies a file that should |
||||
* trigger an lstat() or invalidate the untracked cache for the |
||||
* corresponding directory |
||||
*/ |
||||
static inline void mark_fsmonitor_invalid(struct index_state *istate, struct cache_entry *ce) |
||||
{ |
||||
if (core_fsmonitor) { |
||||
ce->ce_flags &= ~CE_FSMONITOR_VALID; |
||||
untracked_cache_invalidate_path(istate, ce->name); |
||||
trace_printf_key(&trace_fsmonitor, "mark_fsmonitor_invalid '%s'", ce->name); |
||||
} |
||||
} |
||||
|
||||
#endif |
Loading…
Reference in new issue