6 #include "fsmonitor-ipc.h"
7 #include "run-command.h"
10 #define INDEX_EXTENSION_VERSION1 (1)
11 #define INDEX_EXTENSION_VERSION2 (2)
12 #define HOOK_INTERFACE_VERSION1 (1)
13 #define HOOK_INTERFACE_VERSION2 (2)
15 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
17 static void assert_index_minimum(struct index_state *istate, size_t pos)
19 if (pos > istate->cache_nr)
20 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
21 (uintmax_t)pos, istate->cache_nr);
24 static void fsmonitor_ewah_callback(size_t pos, void *is)
26 struct index_state *istate = (struct index_state *)is;
27 struct cache_entry *ce;
29 assert_index_minimum(istate, pos + 1);
31 ce = istate->cache[pos];
32 ce->ce_flags &= ~CE_FSMONITOR_VALID;
35 static int fsmonitor_hook_version(void)
39 if (git_config_get_int("core.fsmonitorhookversion", &hook_version))
42 if (hook_version == HOOK_INTERFACE_VERSION1 ||
43 hook_version == HOOK_INTERFACE_VERSION2)
46 warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
47 "Must be 1 or 2.", hook_version);
51 int read_fsmonitor_extension(struct index_state *istate, const void *data,
54 const char *index = data;
57 struct ewah_bitmap *fsmonitor_dirty;
60 struct strbuf last_update = STRBUF_INIT;
62 if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
63 return error("corrupt fsmonitor extension (too short)");
65 hdr_version = get_be32(index);
66 index += sizeof(uint32_t);
67 if (hdr_version == INDEX_EXTENSION_VERSION1) {
68 timestamp = get_be64(index);
69 strbuf_addf(&last_update, "%"PRIu64"", timestamp);
70 index += sizeof(uint64_t);
71 } else if (hdr_version == INDEX_EXTENSION_VERSION2) {
72 strbuf_addstr(&last_update, index);
73 index += last_update.len + 1;
75 return error("bad fsmonitor version %d", hdr_version);
78 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
80 ewah_size = get_be32(index);
81 index += sizeof(uint32_t);
83 fsmonitor_dirty = ewah_new();
84 ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
85 if (ret != ewah_size) {
86 ewah_free(fsmonitor_dirty);
87 return error("failed to parse ewah bitmap reading fsmonitor index extension");
89 istate->fsmonitor_dirty = fsmonitor_dirty;
91 if (!istate->split_index)
92 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
94 trace2_data_string("index", NULL, "extension/fsmn/read/token",
95 istate->fsmonitor_last_update);
96 trace_printf_key(&trace_fsmonitor,
97 "read fsmonitor extension successful '%s'",
98 istate->fsmonitor_last_update);
102 void fill_fsmonitor_bitmap(struct index_state *istate)
104 unsigned int i, skipped = 0;
105 istate->fsmonitor_dirty = ewah_new();
106 for (i = 0; i < istate->cache_nr; i++) {
107 if (istate->cache[i]->ce_flags & CE_REMOVE)
109 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
110 ewah_set(istate->fsmonitor_dirty, i - skipped);
114 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
116 uint32_t hdr_version;
118 uint32_t ewah_size = 0;
121 if (!istate->split_index)
122 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
124 put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
125 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
127 strbuf_addstr(sb, istate->fsmonitor_last_update);
128 strbuf_addch(sb, 0); /* Want to keep a NUL */
131 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
133 ewah_start = sb->len;
134 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
135 ewah_free(istate->fsmonitor_dirty);
136 istate->fsmonitor_dirty = NULL;
138 /* fix up size field */
139 put_be32(&ewah_size, sb->len - ewah_start);
140 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
142 trace2_data_string("index", NULL, "extension/fsmn/write/token",
143 istate->fsmonitor_last_update);
144 trace_printf_key(&trace_fsmonitor,
145 "write fsmonitor extension successful '%s'",
146 istate->fsmonitor_last_update);
150 * Call the query-fsmonitor hook passing the last update token of the saved results.
152 static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
154 struct child_process cp = CHILD_PROCESS_INIT;
160 strvec_push(&cp.args, core_fsmonitor);
161 strvec_pushf(&cp.args, "%d", version);
162 strvec_pushf(&cp.args, "%s", last_update);
164 cp.dir = get_git_work_tree();
166 trace2_region_enter("fsm_hook", "query", NULL);
168 result = capture_command(&cp, query_result, 1024);
171 trace2_data_intmax("fsm_hook", NULL, "query/failed", result);
173 trace2_data_intmax("fsm_hook", NULL, "query/response-length",
176 if (fsmonitor_is_trivial_response(query_result))
177 trace2_data_intmax("fsm_hook", NULL,
178 "query/trivial-response", 1);
181 trace2_region_leave("fsm_hook", "query", NULL);
186 int fsmonitor_is_trivial_response(const struct strbuf *query_result)
188 static char trivial_response[3] = { '\0', '/', '\0' };
190 return query_result->len >= 3 &&
191 !memcmp(trivial_response,
192 &query_result->buf[query_result->len - 3], 3);
195 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
197 int i, len = strlen(name);
198 if (name[len - 1] == '/') {
201 * TODO We should binary search to find the first path with
202 * TODO this directory prefix. Then linearly update entries
203 * TODO while the prefix matches. Taking care to search without
204 * TODO the trailing slash -- because '/' sorts after a few
205 * TODO interesting special chars, like '.' and ' '.
208 /* Mark all entries for the folder invalid */
209 for (i = 0; i < istate->cache_nr; i++) {
210 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID &&
211 starts_with(istate->cache[i]->name, name))
212 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
214 /* Need to remove the / from the path for the untracked cache */
215 name[len - 1] = '\0';
217 int pos = index_name_pos(istate, name, strlen(name));
220 struct cache_entry *ce = istate->cache[pos];
221 ce->ce_flags &= ~CE_FSMONITOR_VALID;
226 * Mark the untracked cache dirty even if it wasn't found in the index
227 * as it could be a new untracked file.
229 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
230 untracked_cache_invalidate_path(istate, name, 0);
234 * The number of pathnames that we need to receive from FSMonitor
235 * before we force the index to be updated.
237 * Note that any pathname within the set of received paths MAY cause
238 * cache-entry or istate flag bits to be updated and thus cause the
239 * index to be updated on disk.
241 * However, the response may contain many paths (such as ignored
242 * paths) that will not update any flag bits. And thus not force the
243 * index to be updated. (This is fine and normal.) It also means
244 * that the token will not be updated in the FSMonitor index
245 * extension. So the next Git command will find the same token in the
246 * index, make the same token-relative request, and receive the same
247 * response (plus any newly changed paths). If this response is large
248 * (and continues to grow), performance could be impacted.
250 * For example, if the user runs a build and it writes 100K object
251 * files but doesn't modify any source files, the index would not need
252 * to be updated. The FSMonitor response (after the build and
253 * relative to a pre-build token) might be 5MB. Each subsequent Git
254 * command will receive that same 100K/5MB response until something
255 * causes the index to be updated. And `refresh_fsmonitor()` will
256 * have to iterate over those 100K paths each time.
258 * Performance could be improved if we optionally force update the
259 * index after a very large response and get an updated token into
260 * the FSMonitor index extension. This should allow subsequent
261 * commands to get smaller and more current responses.
263 * The value chosen here does not need to be precise. The index
264 * will be updated automatically the first time the user touches
265 * a tracked file and causes a command like `git status` to
266 * update an mtime to be updated and/or set a flag bit.
268 * NEEDSWORK: Does this need to be a config value?
270 static int fsmonitor_force_update_threshold = 100;
272 void refresh_fsmonitor(struct index_state *istate)
274 struct repository *r = istate->repo ? istate->repo : the_repository;
275 struct strbuf query_result = STRBUF_INIT;
276 int query_success = 0, hook_version = -1;
277 size_t bol = 0; /* beginning of line */
278 uint64_t last_update;
279 struct strbuf last_update_token = STRBUF_INIT;
283 if (!core_fsmonitor || istate->fsmonitor_has_run_once)
286 hook_version = fsmonitor_hook_version();
288 istate->fsmonitor_has_run_once = 1;
290 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
292 if (r->settings.use_builtin_fsmonitor > 0) {
293 query_success = !fsmonitor_ipc__send_query(
294 istate->fsmonitor_last_update, &query_result);
297 * The response contains a series of nul terminated
298 * strings. The first is the new token.
300 * Use `char *buf` as an interlude to trick the CI
301 * static analysis to let us use `strbuf_addstr()`
302 * here (and only copy the token) rather than
305 buf = query_result.buf;
306 strbuf_addstr(&last_update_token, buf);
307 bol = last_update_token.len + 1;
310 * The builtin daemon is not available on this
311 * platform -OR- we failed to get a response.
313 * Generate a fake token (rather than a V1
314 * timestamp) for the index extension. (If
315 * they switch back to the hook API, we don't
316 * want ambiguous state.)
318 strbuf_addstr(&last_update_token, "builtin:fake");
322 * Regardless of whether we successfully talked to a
323 * fsmonitor daemon or not, we skip over and do not
324 * try to use the hook. The "core.useBuiltinFSMonitor"
325 * config setting ALWAYS overrides the "core.fsmonitor"
332 * This could be racy so save the date/time now and query_fsmonitor
333 * should be inclusive to ensure we don't miss potential changes.
335 last_update = getnanotime();
336 if (hook_version == HOOK_INTERFACE_VERSION1)
337 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
340 * If we have a last update token, call query_fsmonitor for the set of
341 * changes since that token, else assume everything is possibly dirty
344 if (istate->fsmonitor_last_update) {
345 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
346 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION2,
347 istate->fsmonitor_last_update, &query_result);
350 if (hook_version < 0)
351 hook_version = HOOK_INTERFACE_VERSION2;
354 * First entry will be the last update token
355 * Need to use a char * variable because static
356 * analysis was suggesting to use strbuf_addbuf
357 * but we don't want to copy the entire strbuf
358 * only the chars up to the first NUL
360 buf = query_result.buf;
361 strbuf_addstr(&last_update_token, buf);
362 if (!last_update_token.len) {
363 warning("Empty last update token.");
366 bol = last_update_token.len + 1;
368 } else if (hook_version < 0) {
369 hook_version = HOOK_INTERFACE_VERSION1;
370 if (!last_update_token.len)
371 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
375 if (hook_version == HOOK_INTERFACE_VERSION1) {
376 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION1,
377 istate->fsmonitor_last_update, &query_result);
380 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
381 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
382 core_fsmonitor, query_success ? "success" : "failure");
387 * The response from FSMonitor (excluding the header token) is
390 * [a] a (possibly empty) list of NUL delimited relative
391 * pathnames of changed paths. This list can contain
392 * files and directories. Directories have a trailing
395 * [b] a single '/' to indicate the provider had no
396 * information and that we should consider everything
397 * invalid. We call this a trivial response.
399 if (query_success && query_result.buf[bol] != '/') {
401 * Mark all pathnames returned by the monitor as dirty.
403 * This updates both the cache-entries and the untracked-cache.
407 buf = query_result.buf;
408 for (i = bol; i < query_result.len; i++) {
411 fsmonitor_refresh_callback(istate, buf + bol);
415 if (bol < query_result.len) {
416 fsmonitor_refresh_callback(istate, buf + bol);
420 /* Now mark the untracked cache for fsmonitor usage */
421 if (istate->untracked)
422 istate->untracked->use_fsmonitor = 1;
424 if (count > fsmonitor_force_update_threshold)
425 istate->cache_changed |= FSMONITOR_CHANGED;
429 * We received a trivial response, so invalidate everything.
431 * We only want to run the post index changed hook if
432 * we've actually changed entries, so keep track if we
433 * actually changed entries or not.
435 int is_cache_changed = 0;
437 for (i = 0; i < istate->cache_nr; i++) {
438 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
439 is_cache_changed = 1;
440 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
445 * If we're going to check every file, ensure we save
448 if (is_cache_changed)
449 istate->cache_changed |= FSMONITOR_CHANGED;
451 if (istate->untracked)
452 istate->untracked->use_fsmonitor = 0;
454 strbuf_release(&query_result);
456 /* Now that we've updated istate, save the last_update_token */
457 FREE_AND_NULL(istate->fsmonitor_last_update);
458 istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
462 * The caller wants to turn on FSMonitor. And when the caller writes
463 * the index to disk, a FSMonitor extension should be included. This
464 * requires that `istate->fsmonitor_last_update` not be NULL. But we
465 * have not actually talked to a FSMonitor process yet, so we don't
466 * have an initial value for this field.
468 * For a protocol V1 FSMonitor process, this field is a formatted
469 * "nanoseconds since epoch" field. However, for a protocol V2
470 * FSMonitor process, this field is an opaque token.
472 * Historically, `add_fsmonitor()` has initialized this field to the
473 * current time for protocol V1 processes. There are lots of race
474 * conditions here, but that code has shipped...
476 * The only true solution is to use a V2 FSMonitor and get a current
477 * or default token value (that it understands), but we cannot do that
478 * until we have actually talked to an instance of the FSMonitor process
479 * (but the protocol requires that we send a token first...).
481 * For simplicity, just initialize like we have a V1 process and require
482 * that V2 processes adapt.
484 static void initialize_fsmonitor_last_update(struct index_state *istate)
486 struct strbuf last_update = STRBUF_INIT;
488 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
489 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
492 void add_fsmonitor(struct index_state *istate)
496 if (!istate->fsmonitor_last_update) {
497 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
498 istate->cache_changed |= FSMONITOR_CHANGED;
499 initialize_fsmonitor_last_update(istate);
501 /* reset the fsmonitor state */
502 for (i = 0; i < istate->cache_nr; i++)
503 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
505 /* reset the untracked cache */
506 if (istate->untracked) {
507 add_untracked_cache(istate);
508 istate->untracked->use_fsmonitor = 1;
511 /* Update the fsmonitor state */
512 refresh_fsmonitor(istate);
516 void remove_fsmonitor(struct index_state *istate)
518 if (istate->fsmonitor_last_update) {
519 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
520 istate->cache_changed |= FSMONITOR_CHANGED;
521 FREE_AND_NULL(istate->fsmonitor_last_update);
525 void tweak_fsmonitor(struct index_state *istate)
528 int fsmonitor_enabled = repo_config_get_fsmonitor(istate->repo ? istate->repo : the_repository);
530 if (istate->fsmonitor_dirty) {
531 if (fsmonitor_enabled) {
532 /* Mark all entries valid */
533 for (i = 0; i < istate->cache_nr; i++) {
534 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
537 /* Mark all previously saved entries as dirty */
538 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
539 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
541 refresh_fsmonitor(istate);
544 ewah_free(istate->fsmonitor_dirty);
545 istate->fsmonitor_dirty = NULL;
548 switch (fsmonitor_enabled) {
549 case -1: /* keep: do nothing */
552 remove_fsmonitor(istate);
555 add_fsmonitor(istate);
557 default: /* unknown value: do nothing */