Merge branch 'ew/packfile-syscall-optim'
[git.git] / fsmonitor.c
blob868cca01e250896054b1f7025c7180c87ab1b88d
1 #include "cache.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "ewah/ewok.h"
5 #include "fsmonitor.h"
6 #include "run-command.h"
7 #include "strbuf.h"
9 #define INDEX_EXTENSION_VERSION (1)
10 #define HOOK_INTERFACE_VERSION (1)
12 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
14 static void fsmonitor_ewah_callback(size_t pos, void *is)
16 struct index_state *istate = (struct index_state *)is;
17 struct cache_entry *ce;
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);
23 ce = istate->cache[pos];
24 ce->ce_flags &= ~CE_FSMONITOR_VALID;
27 int read_fsmonitor_extension(struct index_state *istate, const void *data,
28 unsigned long sz)
30 const char *index = data;
31 uint32_t hdr_version;
32 uint32_t ewah_size;
33 struct ewah_bitmap *fsmonitor_dirty;
34 int ret;
36 if (sz < sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
37 return error("corrupt fsmonitor extension (too short)");
39 hdr_version = get_be32(index);
40 index += sizeof(uint32_t);
41 if (hdr_version != INDEX_EXTENSION_VERSION)
42 return error("bad fsmonitor version %d", hdr_version);
44 istate->fsmonitor_last_update = get_be64(index);
45 index += sizeof(uint64_t);
47 ewah_size = get_be32(index);
48 index += sizeof(uint32_t);
50 fsmonitor_dirty = ewah_new();
51 ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
52 if (ret != ewah_size) {
53 ewah_free(fsmonitor_dirty);
54 return error("failed to parse ewah bitmap reading fsmonitor index extension");
56 istate->fsmonitor_dirty = fsmonitor_dirty;
58 if (!istate->split_index &&
59 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
60 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
61 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
63 trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
64 return 0;
67 void fill_fsmonitor_bitmap(struct index_state *istate)
69 unsigned int i, skipped = 0;
70 istate->fsmonitor_dirty = ewah_new();
71 for (i = 0; i < istate->cache_nr; i++) {
72 if (istate->cache[i]->ce_flags & CE_REMOVE)
73 skipped++;
74 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
75 ewah_set(istate->fsmonitor_dirty, i - skipped);
79 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
81 uint32_t hdr_version;
82 uint64_t tm;
83 uint32_t ewah_start;
84 uint32_t ewah_size = 0;
85 int fixup = 0;
87 if (!istate->split_index &&
88 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
89 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
90 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
92 put_be32(&hdr_version, INDEX_EXTENSION_VERSION);
93 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
95 put_be64(&tm, istate->fsmonitor_last_update);
96 strbuf_add(sb, &tm, sizeof(uint64_t));
97 fixup = sb->len;
98 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
100 ewah_start = sb->len;
101 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
102 ewah_free(istate->fsmonitor_dirty);
103 istate->fsmonitor_dirty = NULL;
105 /* fix up size field */
106 put_be32(&ewah_size, sb->len - ewah_start);
107 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
109 trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
113 * Call the query-fsmonitor hook passing the time of the last saved results.
115 static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result)
117 struct child_process cp = CHILD_PROCESS_INIT;
119 if (!core_fsmonitor)
120 return -1;
122 argv_array_push(&cp.args, core_fsmonitor);
123 argv_array_pushf(&cp.args, "%d", version);
124 argv_array_pushf(&cp.args, "%" PRIuMAX, (uintmax_t)last_update);
125 cp.use_shell = 1;
126 cp.dir = get_git_work_tree();
128 return capture_command(&cp, query_result, 1024);
131 static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
133 int pos = index_name_pos(istate, name, strlen(name));
135 if (pos >= 0) {
136 struct cache_entry *ce = istate->cache[pos];
137 ce->ce_flags &= ~CE_FSMONITOR_VALID;
141 * Mark the untracked cache dirty even if it wasn't found in the index
142 * as it could be a new untracked file.
144 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
145 untracked_cache_invalidate_path(istate, name, 0);
148 void refresh_fsmonitor(struct index_state *istate)
150 struct strbuf query_result = STRBUF_INIT;
151 int query_success = 0;
152 size_t bol; /* beginning of line */
153 uint64_t last_update;
154 char *buf;
155 unsigned int i;
157 if (!core_fsmonitor || istate->fsmonitor_has_run_once)
158 return;
159 istate->fsmonitor_has_run_once = 1;
161 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
163 * This could be racy so save the date/time now and query_fsmonitor
164 * should be inclusive to ensure we don't miss potential changes.
166 last_update = getnanotime();
169 * If we have a last update time, call query_fsmonitor for the set of
170 * changes since that time, else assume everything is possibly dirty
171 * and check it all.
173 if (istate->fsmonitor_last_update) {
174 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
175 istate->fsmonitor_last_update, &query_result);
176 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
177 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
178 core_fsmonitor, query_success ? "success" : "failure");
181 /* a fsmonitor process can return '/' to indicate all entries are invalid */
182 if (query_success && query_result.buf[0] != '/') {
183 /* Mark all entries returned by the monitor as dirty */
184 buf = query_result.buf;
185 bol = 0;
186 for (i = 0; i < query_result.len; i++) {
187 if (buf[i] != '\0')
188 continue;
189 fsmonitor_refresh_callback(istate, buf + bol);
190 bol = i + 1;
192 if (bol < query_result.len)
193 fsmonitor_refresh_callback(istate, buf + bol);
195 /* Now mark the untracked cache for fsmonitor usage */
196 if (istate->untracked)
197 istate->untracked->use_fsmonitor = 1;
198 } else {
200 /* We only want to run the post index changed hook if we've actually changed entries, so keep track
201 * if we actually changed entries or not */
202 int is_cache_changed = 0;
203 /* Mark all entries invalid */
204 for (i = 0; i < istate->cache_nr; i++) {
205 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
206 is_cache_changed = 1;
207 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
211 /* If we're going to check every file, ensure we save the results */
212 if (is_cache_changed)
213 istate->cache_changed |= FSMONITOR_CHANGED;
215 if (istate->untracked)
216 istate->untracked->use_fsmonitor = 0;
218 strbuf_release(&query_result);
220 /* Now that we've updated istate, save the last_update time */
221 istate->fsmonitor_last_update = last_update;
224 void add_fsmonitor(struct index_state *istate)
226 unsigned int i;
228 if (!istate->fsmonitor_last_update) {
229 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
230 istate->cache_changed |= FSMONITOR_CHANGED;
231 istate->fsmonitor_last_update = getnanotime();
233 /* reset the fsmonitor state */
234 for (i = 0; i < istate->cache_nr; i++)
235 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
237 /* reset the untracked cache */
238 if (istate->untracked) {
239 add_untracked_cache(istate);
240 istate->untracked->use_fsmonitor = 1;
243 /* Update the fsmonitor state */
244 refresh_fsmonitor(istate);
248 void remove_fsmonitor(struct index_state *istate)
250 if (istate->fsmonitor_last_update) {
251 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
252 istate->cache_changed |= FSMONITOR_CHANGED;
253 istate->fsmonitor_last_update = 0;
257 void tweak_fsmonitor(struct index_state *istate)
259 unsigned int i;
260 int fsmonitor_enabled = git_config_get_fsmonitor();
262 if (istate->fsmonitor_dirty) {
263 if (fsmonitor_enabled) {
264 /* Mark all entries valid */
265 for (i = 0; i < istate->cache_nr; i++) {
266 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
269 /* Mark all previously saved entries as dirty */
270 if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
271 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
272 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
273 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
275 refresh_fsmonitor(istate);
278 ewah_free(istate->fsmonitor_dirty);
279 istate->fsmonitor_dirty = NULL;
282 switch (fsmonitor_enabled) {
283 case -1: /* keep: do nothing */
284 break;
285 case 0: /* false */
286 remove_fsmonitor(istate);
287 break;
288 case 1: /* true */
289 add_fsmonitor(istate);
290 break;
291 default: /* unknown value: do nothing */
292 break;