push: improve grammar of branch.autoSetupMerge advice
[alt-git.git] / fsmonitor.c
blob57d6a483beede8e3b254634cdb944b19445875d1
1 #include "cache.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "ewah/ewok.h"
5 #include "fsmonitor.h"
6 #include "fsmonitor-ipc.h"
7 #include "run-command.h"
8 #include "strbuf.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)
37 int hook_version;
39 if (git_config_get_int("core.fsmonitorhookversion", &hook_version))
40 return -1;
42 if (hook_version == HOOK_INTERFACE_VERSION1 ||
43 hook_version == HOOK_INTERFACE_VERSION2)
44 return hook_version;
46 warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
47 "Must be 1 or 2.", hook_version);
48 return -1;
51 int read_fsmonitor_extension(struct index_state *istate, const void *data,
52 unsigned long sz)
54 const char *index = data;
55 uint32_t hdr_version;
56 uint32_t ewah_size;
57 struct ewah_bitmap *fsmonitor_dirty;
58 int ret;
59 uint64_t timestamp;
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;
74 } else {
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);
99 return 0;
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)
108 skipped++;
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;
117 uint32_t ewah_start;
118 uint32_t ewah_size = 0;
119 int fixup = 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 */
130 fixup = sb->len;
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_hook(struct repository *r,
153 int version,
154 const char *last_update,
155 struct strbuf *query_result)
157 struct child_process cp = CHILD_PROCESS_INIT;
158 int result;
160 if (fsm_settings__get_mode(r) != FSMONITOR_MODE_HOOK)
161 return -1;
163 strvec_push(&cp.args, fsm_settings__get_hook_path(r));
164 strvec_pushf(&cp.args, "%d", version);
165 strvec_pushf(&cp.args, "%s", last_update);
166 cp.use_shell = 1;
167 cp.dir = get_git_work_tree();
169 trace2_region_enter("fsm_hook", "query", NULL);
171 result = capture_command(&cp, query_result, 1024);
173 if (result)
174 trace2_data_intmax("fsm_hook", NULL, "query/failed", result);
175 else
176 trace2_data_intmax("fsm_hook", NULL, "query/response-length",
177 query_result->len);
179 trace2_region_leave("fsm_hook", "query", NULL);
181 return result;
184 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
186 int i, len = strlen(name);
187 int pos = index_name_pos(istate, name, len);
189 trace_printf_key(&trace_fsmonitor,
190 "fsmonitor_refresh_callback '%s' (pos %d)",
191 name, pos);
193 if (name[len - 1] == '/') {
195 * The daemon can decorate directory events, such as
196 * moves or renames, with a trailing slash if the OS
197 * FS Event contains sufficient information, such as
198 * MacOS.
200 * Use this to invalidate the entire cone under that
201 * directory.
203 * We do not expect an exact match because the index
204 * does not normally contain directory entries, so we
205 * start at the insertion point and scan.
207 if (pos < 0)
208 pos = -pos - 1;
210 /* Mark all entries for the folder invalid */
211 for (i = pos; i < istate->cache_nr; i++) {
212 if (!starts_with(istate->cache[i]->name, name))
213 break;
214 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
218 * We need to remove the traling "/" from the path
219 * for the untracked cache.
221 name[len - 1] = '\0';
222 } else if (pos >= 0) {
224 * We have an exact match for this path and can just
225 * invalidate it.
227 istate->cache[pos]->ce_flags &= ~CE_FSMONITOR_VALID;
228 } else {
230 * The path is not a tracked file -or- it is a
231 * directory event on a platform that cannot
232 * distinguish between file and directory events in
233 * the event handler, such as Windows.
235 * Scan as if it is a directory and invalidate the
236 * cone under it. (But remember to ignore items
237 * between "name" and "name/", such as "name-" and
238 * "name.".
240 pos = -pos - 1;
242 for (i = pos; i < istate->cache_nr; i++) {
243 if (!starts_with(istate->cache[i]->name, name))
244 break;
245 if ((unsigned char)istate->cache[i]->name[len] > '/')
246 break;
247 if (istate->cache[i]->name[len] == '/')
248 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
253 * Mark the untracked cache dirty even if it wasn't found in the index
254 * as it could be a new untracked file.
256 untracked_cache_invalidate_path(istate, name, 0);
260 * The number of pathnames that we need to receive from FSMonitor
261 * before we force the index to be updated.
263 * Note that any pathname within the set of received paths MAY cause
264 * cache-entry or istate flag bits to be updated and thus cause the
265 * index to be updated on disk.
267 * However, the response may contain many paths (such as ignored
268 * paths) that will not update any flag bits. And thus not force the
269 * index to be updated. (This is fine and normal.) It also means
270 * that the token will not be updated in the FSMonitor index
271 * extension. So the next Git command will find the same token in the
272 * index, make the same token-relative request, and receive the same
273 * response (plus any newly changed paths). If this response is large
274 * (and continues to grow), performance could be impacted.
276 * For example, if the user runs a build and it writes 100K object
277 * files but doesn't modify any source files, the index would not need
278 * to be updated. The FSMonitor response (after the build and
279 * relative to a pre-build token) might be 5MB. Each subsequent Git
280 * command will receive that same 100K/5MB response until something
281 * causes the index to be updated. And `refresh_fsmonitor()` will
282 * have to iterate over those 100K paths each time.
284 * Performance could be improved if we optionally force update the
285 * index after a very large response and get an updated token into
286 * the FSMonitor index extension. This should allow subsequent
287 * commands to get smaller and more current responses.
289 * The value chosen here does not need to be precise. The index
290 * will be updated automatically the first time the user touches
291 * a tracked file and causes a command like `git status` to
292 * update an mtime to be updated and/or set a flag bit.
294 static int fsmonitor_force_update_threshold = 100;
296 void refresh_fsmonitor(struct index_state *istate)
298 struct strbuf query_result = STRBUF_INIT;
299 int query_success = 0, hook_version = -1;
300 size_t bol = 0; /* beginning of line */
301 uint64_t last_update;
302 struct strbuf last_update_token = STRBUF_INIT;
303 char *buf;
304 unsigned int i;
305 int is_trivial = 0;
306 struct repository *r = istate->repo ? istate->repo : the_repository;
307 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
309 if (fsm_mode <= FSMONITOR_MODE_DISABLED ||
310 istate->fsmonitor_has_run_once)
311 return;
313 istate->fsmonitor_has_run_once = 1;
315 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
317 if (fsm_mode == FSMONITOR_MODE_IPC) {
318 query_success = !fsmonitor_ipc__send_query(
319 istate->fsmonitor_last_update ?
320 istate->fsmonitor_last_update : "builtin:fake",
321 &query_result);
322 if (query_success) {
324 * The response contains a series of nul terminated
325 * strings. The first is the new token.
327 * Use `char *buf` as an interlude to trick the CI
328 * static analysis to let us use `strbuf_addstr()`
329 * here (and only copy the token) rather than
330 * `strbuf_addbuf()`.
332 buf = query_result.buf;
333 strbuf_addstr(&last_update_token, buf);
334 bol = last_update_token.len + 1;
335 is_trivial = query_result.buf[bol] == '/';
336 if (is_trivial)
337 trace2_data_intmax("fsm_client", NULL,
338 "query/trivial-response", 1);
339 } else {
341 * The builtin daemon is not available on this
342 * platform -OR- we failed to get a response.
344 * Generate a fake token (rather than a V1
345 * timestamp) for the index extension. (If
346 * they switch back to the hook API, we don't
347 * want ambiguous state.)
349 strbuf_addstr(&last_update_token, "builtin:fake");
352 goto apply_results;
355 assert(fsm_mode == FSMONITOR_MODE_HOOK);
357 hook_version = fsmonitor_hook_version();
360 * This could be racy so save the date/time now and query_fsmonitor_hook
361 * should be inclusive to ensure we don't miss potential changes.
363 last_update = getnanotime();
364 if (hook_version == HOOK_INTERFACE_VERSION1)
365 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
368 * If we have a last update token, call query_fsmonitor_hook for the set of
369 * changes since that token, else assume everything is possibly dirty
370 * and check it all.
372 if (istate->fsmonitor_last_update) {
373 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
374 query_success = !query_fsmonitor_hook(
375 r, HOOK_INTERFACE_VERSION2,
376 istate->fsmonitor_last_update, &query_result);
378 if (query_success) {
379 if (hook_version < 0)
380 hook_version = HOOK_INTERFACE_VERSION2;
383 * First entry will be the last update token
384 * Need to use a char * variable because static
385 * analysis was suggesting to use strbuf_addbuf
386 * but we don't want to copy the entire strbuf
387 * only the chars up to the first NUL
389 buf = query_result.buf;
390 strbuf_addstr(&last_update_token, buf);
391 if (!last_update_token.len) {
392 warning("Empty last update token.");
393 query_success = 0;
394 } else {
395 bol = last_update_token.len + 1;
396 is_trivial = query_result.buf[bol] == '/';
398 } else if (hook_version < 0) {
399 hook_version = HOOK_INTERFACE_VERSION1;
400 if (!last_update_token.len)
401 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
405 if (hook_version == HOOK_INTERFACE_VERSION1) {
406 query_success = !query_fsmonitor_hook(
407 r, HOOK_INTERFACE_VERSION1,
408 istate->fsmonitor_last_update, &query_result);
409 if (query_success)
410 is_trivial = query_result.buf[0] == '/';
413 if (is_trivial)
414 trace2_data_intmax("fsm_hook", NULL,
415 "query/trivial-response", 1);
417 trace_performance_since(last_update, "fsmonitor process '%s'",
418 fsm_settings__get_hook_path(r));
419 trace_printf_key(&trace_fsmonitor,
420 "fsmonitor process '%s' returned %s",
421 fsm_settings__get_hook_path(r),
422 query_success ? "success" : "failure");
425 apply_results:
427 * The response from FSMonitor (excluding the header token) is
428 * either:
430 * [a] a (possibly empty) list of NUL delimited relative
431 * pathnames of changed paths. This list can contain
432 * files and directories. Directories have a trailing
433 * slash.
435 * [b] a single '/' to indicate the provider had no
436 * information and that we should consider everything
437 * invalid. We call this a trivial response.
439 trace2_region_enter("fsmonitor", "apply_results", istate->repo);
441 if (query_success && !is_trivial) {
443 * Mark all pathnames returned by the monitor as dirty.
445 * This updates both the cache-entries and the untracked-cache.
447 int count = 0;
449 buf = query_result.buf;
450 for (i = bol; i < query_result.len; i++) {
451 if (buf[i] != '\0')
452 continue;
453 fsmonitor_refresh_callback(istate, buf + bol);
454 bol = i + 1;
455 count++;
457 if (bol < query_result.len) {
458 fsmonitor_refresh_callback(istate, buf + bol);
459 count++;
462 /* Now mark the untracked cache for fsmonitor usage */
463 if (istate->untracked)
464 istate->untracked->use_fsmonitor = 1;
466 if (count > fsmonitor_force_update_threshold)
467 istate->cache_changed |= FSMONITOR_CHANGED;
469 trace2_data_intmax("fsmonitor", istate->repo, "apply_count",
470 count);
472 } else {
474 * We failed to get a response or received a trivial response,
475 * so invalidate everything.
477 * We only want to run the post index changed hook if
478 * we've actually changed entries, so keep track if we
479 * actually changed entries or not.
481 int is_cache_changed = 0;
483 for (i = 0; i < istate->cache_nr; i++) {
484 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
485 is_cache_changed = 1;
486 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
491 * If we're going to check every file, ensure we save
492 * the results.
494 if (is_cache_changed)
495 istate->cache_changed |= FSMONITOR_CHANGED;
497 if (istate->untracked)
498 istate->untracked->use_fsmonitor = 0;
500 trace2_region_leave("fsmonitor", "apply_results", istate->repo);
502 strbuf_release(&query_result);
504 /* Now that we've updated istate, save the last_update_token */
505 FREE_AND_NULL(istate->fsmonitor_last_update);
506 istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
510 * The caller wants to turn on FSMonitor. And when the caller writes
511 * the index to disk, a FSMonitor extension should be included. This
512 * requires that `istate->fsmonitor_last_update` not be NULL. But we
513 * have not actually talked to a FSMonitor process yet, so we don't
514 * have an initial value for this field.
516 * For a protocol V1 FSMonitor process, this field is a formatted
517 * "nanoseconds since epoch" field. However, for a protocol V2
518 * FSMonitor process, this field is an opaque token.
520 * Historically, `add_fsmonitor()` has initialized this field to the
521 * current time for protocol V1 processes. There are lots of race
522 * conditions here, but that code has shipped...
524 * The only true solution is to use a V2 FSMonitor and get a current
525 * or default token value (that it understands), but we cannot do that
526 * until we have actually talked to an instance of the FSMonitor process
527 * (but the protocol requires that we send a token first...).
529 * For simplicity, just initialize like we have a V1 process and require
530 * that V2 processes adapt.
532 static void initialize_fsmonitor_last_update(struct index_state *istate)
534 struct strbuf last_update = STRBUF_INIT;
536 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
537 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
540 void add_fsmonitor(struct index_state *istate)
542 unsigned int i;
544 if (!istate->fsmonitor_last_update) {
545 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
546 istate->cache_changed |= FSMONITOR_CHANGED;
547 initialize_fsmonitor_last_update(istate);
549 /* reset the fsmonitor state */
550 for (i = 0; i < istate->cache_nr; i++)
551 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
553 /* reset the untracked cache */
554 if (istate->untracked) {
555 add_untracked_cache(istate);
556 istate->untracked->use_fsmonitor = 1;
559 /* Update the fsmonitor state */
560 refresh_fsmonitor(istate);
564 void remove_fsmonitor(struct index_state *istate)
566 if (istate->fsmonitor_last_update) {
567 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
568 istate->cache_changed |= FSMONITOR_CHANGED;
569 FREE_AND_NULL(istate->fsmonitor_last_update);
573 void tweak_fsmonitor(struct index_state *istate)
575 unsigned int i;
576 int fsmonitor_enabled = (fsm_settings__get_mode(istate->repo)
577 > FSMONITOR_MODE_DISABLED);
579 if (istate->fsmonitor_dirty) {
580 if (fsmonitor_enabled) {
581 /* Mark all entries valid */
582 for (i = 0; i < istate->cache_nr; i++) {
583 if (S_ISGITLINK(istate->cache[i]->ce_mode))
584 continue;
585 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
588 /* Mark all previously saved entries as dirty */
589 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
590 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
592 refresh_fsmonitor(istate);
595 ewah_free(istate->fsmonitor_dirty);
596 istate->fsmonitor_dirty = NULL;
599 if (fsmonitor_enabled)
600 add_fsmonitor(istate);
601 else
602 remove_fsmonitor(istate);