t1092: stress test 'git sparse-checkout set'
[git/debian.git] / fsmonitor.c
blob292a6742b4fa23332000a61ad3db6ca28cf88018
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 if (name[len - 1] == '/') {
190 * TODO We should binary search to find the first path with
191 * TODO this directory prefix. Then linearly update entries
192 * TODO while the prefix matches. Taking care to search without
193 * TODO the trailing slash -- because '/' sorts after a few
194 * TODO interesting special chars, like '.' and ' '.
197 /* Mark all entries for the folder invalid */
198 for (i = 0; i < istate->cache_nr; i++) {
199 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID &&
200 starts_with(istate->cache[i]->name, name))
201 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
203 /* Need to remove the / from the path for the untracked cache */
204 name[len - 1] = '\0';
205 } else {
206 int pos = index_name_pos(istate, name, strlen(name));
208 if (pos >= 0) {
209 struct cache_entry *ce = istate->cache[pos];
210 ce->ce_flags &= ~CE_FSMONITOR_VALID;
215 * Mark the untracked cache dirty even if it wasn't found in the index
216 * as it could be a new untracked file.
218 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
219 untracked_cache_invalidate_path(istate, name, 0);
223 * The number of pathnames that we need to receive from FSMonitor
224 * before we force the index to be updated.
226 * Note that any pathname within the set of received paths MAY cause
227 * cache-entry or istate flag bits to be updated and thus cause the
228 * index to be updated on disk.
230 * However, the response may contain many paths (such as ignored
231 * paths) that will not update any flag bits. And thus not force the
232 * index to be updated. (This is fine and normal.) It also means
233 * that the token will not be updated in the FSMonitor index
234 * extension. So the next Git command will find the same token in the
235 * index, make the same token-relative request, and receive the same
236 * response (plus any newly changed paths). If this response is large
237 * (and continues to grow), performance could be impacted.
239 * For example, if the user runs a build and it writes 100K object
240 * files but doesn't modify any source files, the index would not need
241 * to be updated. The FSMonitor response (after the build and
242 * relative to a pre-build token) might be 5MB. Each subsequent Git
243 * command will receive that same 100K/5MB response until something
244 * causes the index to be updated. And `refresh_fsmonitor()` will
245 * have to iterate over those 100K paths each time.
247 * Performance could be improved if we optionally force update the
248 * index after a very large response and get an updated token into
249 * the FSMonitor index extension. This should allow subsequent
250 * commands to get smaller and more current responses.
252 * The value chosen here does not need to be precise. The index
253 * will be updated automatically the first time the user touches
254 * a tracked file and causes a command like `git status` to
255 * update an mtime to be updated and/or set a flag bit.
257 static int fsmonitor_force_update_threshold = 100;
259 void refresh_fsmonitor(struct index_state *istate)
261 struct strbuf query_result = STRBUF_INIT;
262 int query_success = 0, hook_version = -1;
263 size_t bol = 0; /* beginning of line */
264 uint64_t last_update;
265 struct strbuf last_update_token = STRBUF_INIT;
266 char *buf;
267 unsigned int i;
268 int is_trivial = 0;
269 struct repository *r = istate->repo ? istate->repo : the_repository;
270 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(r);
272 if (fsm_mode <= FSMONITOR_MODE_DISABLED ||
273 istate->fsmonitor_has_run_once)
274 return;
276 istate->fsmonitor_has_run_once = 1;
278 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
280 if (fsm_mode == FSMONITOR_MODE_IPC) {
281 query_success = !fsmonitor_ipc__send_query(
282 istate->fsmonitor_last_update ?
283 istate->fsmonitor_last_update : "builtin:fake",
284 &query_result);
285 if (query_success) {
287 * The response contains a series of nul terminated
288 * strings. The first is the new token.
290 * Use `char *buf` as an interlude to trick the CI
291 * static analysis to let us use `strbuf_addstr()`
292 * here (and only copy the token) rather than
293 * `strbuf_addbuf()`.
295 buf = query_result.buf;
296 strbuf_addstr(&last_update_token, buf);
297 bol = last_update_token.len + 1;
298 is_trivial = query_result.buf[bol] == '/';
299 if (is_trivial)
300 trace2_data_intmax("fsm_client", NULL,
301 "query/trivial-response", 1);
302 } else {
304 * The builtin daemon is not available on this
305 * platform -OR- we failed to get a response.
307 * Generate a fake token (rather than a V1
308 * timestamp) for the index extension. (If
309 * they switch back to the hook API, we don't
310 * want ambiguous state.)
312 strbuf_addstr(&last_update_token, "builtin:fake");
315 goto apply_results;
318 assert(fsm_mode == FSMONITOR_MODE_HOOK);
320 hook_version = fsmonitor_hook_version();
323 * This could be racy so save the date/time now and query_fsmonitor_hook
324 * should be inclusive to ensure we don't miss potential changes.
326 last_update = getnanotime();
327 if (hook_version == HOOK_INTERFACE_VERSION1)
328 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
331 * If we have a last update token, call query_fsmonitor_hook for the set of
332 * changes since that token, else assume everything is possibly dirty
333 * and check it all.
335 if (istate->fsmonitor_last_update) {
336 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
337 query_success = !query_fsmonitor_hook(
338 r, HOOK_INTERFACE_VERSION2,
339 istate->fsmonitor_last_update, &query_result);
341 if (query_success) {
342 if (hook_version < 0)
343 hook_version = HOOK_INTERFACE_VERSION2;
346 * First entry will be the last update token
347 * Need to use a char * variable because static
348 * analysis was suggesting to use strbuf_addbuf
349 * but we don't want to copy the entire strbuf
350 * only the chars up to the first NUL
352 buf = query_result.buf;
353 strbuf_addstr(&last_update_token, buf);
354 if (!last_update_token.len) {
355 warning("Empty last update token.");
356 query_success = 0;
357 } else {
358 bol = last_update_token.len + 1;
359 is_trivial = query_result.buf[bol] == '/';
361 } else if (hook_version < 0) {
362 hook_version = HOOK_INTERFACE_VERSION1;
363 if (!last_update_token.len)
364 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
368 if (hook_version == HOOK_INTERFACE_VERSION1) {
369 query_success = !query_fsmonitor_hook(
370 r, HOOK_INTERFACE_VERSION1,
371 istate->fsmonitor_last_update, &query_result);
372 if (query_success)
373 is_trivial = query_result.buf[0] == '/';
376 if (is_trivial)
377 trace2_data_intmax("fsm_hook", NULL,
378 "query/trivial-response", 1);
380 trace_performance_since(last_update, "fsmonitor process '%s'",
381 fsm_settings__get_hook_path(r));
382 trace_printf_key(&trace_fsmonitor,
383 "fsmonitor process '%s' returned %s",
384 fsm_settings__get_hook_path(r),
385 query_success ? "success" : "failure");
388 apply_results:
390 * The response from FSMonitor (excluding the header token) is
391 * either:
393 * [a] a (possibly empty) list of NUL delimited relative
394 * pathnames of changed paths. This list can contain
395 * files and directories. Directories have a trailing
396 * slash.
398 * [b] a single '/' to indicate the provider had no
399 * information and that we should consider everything
400 * invalid. We call this a trivial response.
402 trace2_region_enter("fsmonitor", "apply_results", istate->repo);
404 if (query_success && !is_trivial) {
406 * Mark all pathnames returned by the monitor as dirty.
408 * This updates both the cache-entries and the untracked-cache.
410 int count = 0;
412 buf = query_result.buf;
413 for (i = bol; i < query_result.len; i++) {
414 if (buf[i] != '\0')
415 continue;
416 fsmonitor_refresh_callback(istate, buf + bol);
417 bol = i + 1;
418 count++;
420 if (bol < query_result.len) {
421 fsmonitor_refresh_callback(istate, buf + bol);
422 count++;
425 /* Now mark the untracked cache for fsmonitor usage */
426 if (istate->untracked)
427 istate->untracked->use_fsmonitor = 1;
429 if (count > fsmonitor_force_update_threshold)
430 istate->cache_changed |= FSMONITOR_CHANGED;
432 trace2_data_intmax("fsmonitor", istate->repo, "apply_count",
433 count);
435 } else {
437 * We failed to get a response or received a trivial response,
438 * so invalidate everything.
440 * We only want to run the post index changed hook if
441 * we've actually changed entries, so keep track if we
442 * actually changed entries or not.
444 int is_cache_changed = 0;
446 for (i = 0; i < istate->cache_nr; i++) {
447 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
448 is_cache_changed = 1;
449 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
454 * If we're going to check every file, ensure we save
455 * the results.
457 if (is_cache_changed)
458 istate->cache_changed |= FSMONITOR_CHANGED;
460 if (istate->untracked)
461 istate->untracked->use_fsmonitor = 0;
463 trace2_region_leave("fsmonitor", "apply_results", istate->repo);
465 strbuf_release(&query_result);
467 /* Now that we've updated istate, save the last_update_token */
468 FREE_AND_NULL(istate->fsmonitor_last_update);
469 istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
473 * The caller wants to turn on FSMonitor. And when the caller writes
474 * the index to disk, a FSMonitor extension should be included. This
475 * requires that `istate->fsmonitor_last_update` not be NULL. But we
476 * have not actually talked to a FSMonitor process yet, so we don't
477 * have an initial value for this field.
479 * For a protocol V1 FSMonitor process, this field is a formatted
480 * "nanoseconds since epoch" field. However, for a protocol V2
481 * FSMonitor process, this field is an opaque token.
483 * Historically, `add_fsmonitor()` has initialized this field to the
484 * current time for protocol V1 processes. There are lots of race
485 * conditions here, but that code has shipped...
487 * The only true solution is to use a V2 FSMonitor and get a current
488 * or default token value (that it understands), but we cannot do that
489 * until we have actually talked to an instance of the FSMonitor process
490 * (but the protocol requires that we send a token first...).
492 * For simplicity, just initialize like we have a V1 process and require
493 * that V2 processes adapt.
495 static void initialize_fsmonitor_last_update(struct index_state *istate)
497 struct strbuf last_update = STRBUF_INIT;
499 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
500 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
503 void add_fsmonitor(struct index_state *istate)
505 unsigned int i;
507 if (!istate->fsmonitor_last_update) {
508 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
509 istate->cache_changed |= FSMONITOR_CHANGED;
510 initialize_fsmonitor_last_update(istate);
512 /* reset the fsmonitor state */
513 for (i = 0; i < istate->cache_nr; i++)
514 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
516 /* reset the untracked cache */
517 if (istate->untracked) {
518 add_untracked_cache(istate);
519 istate->untracked->use_fsmonitor = 1;
522 /* Update the fsmonitor state */
523 refresh_fsmonitor(istate);
527 void remove_fsmonitor(struct index_state *istate)
529 if (istate->fsmonitor_last_update) {
530 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
531 istate->cache_changed |= FSMONITOR_CHANGED;
532 FREE_AND_NULL(istate->fsmonitor_last_update);
536 void tweak_fsmonitor(struct index_state *istate)
538 unsigned int i;
539 int fsmonitor_enabled = (fsm_settings__get_mode(istate->repo)
540 > FSMONITOR_MODE_DISABLED);
542 if (istate->fsmonitor_dirty) {
543 if (fsmonitor_enabled) {
544 /* Mark all entries valid */
545 for (i = 0; i < istate->cache_nr; i++) {
546 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
549 /* Mark all previously saved entries as dirty */
550 assert_index_minimum(istate, istate->fsmonitor_dirty->bit_size);
551 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
553 refresh_fsmonitor(istate);
556 ewah_free(istate->fsmonitor_dirty);
557 istate->fsmonitor_dirty = NULL;
560 if (fsmonitor_enabled)
561 add_fsmonitor(istate);
562 else
563 remove_fsmonitor(istate);