fsmonitor: log invocation of FSMonitor hook to trace2
[git] / fsmonitor.c
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"
8
9 #define INDEX_EXTENSION_VERSION1        (1)
10 #define INDEX_EXTENSION_VERSION2        (2)
11 #define HOOK_INTERFACE_VERSION1         (1)
12 #define HOOK_INTERFACE_VERSION2         (2)
13
14 struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
15
16 static void fsmonitor_ewah_callback(size_t pos, void *is)
17 {
18         struct index_state *istate = (struct index_state *)is;
19         struct cache_entry *ce;
20
21         if (pos >= istate->cache_nr)
22                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" >= %u)",
23                     (uintmax_t)pos, istate->cache_nr);
24
25         ce = istate->cache[pos];
26         ce->ce_flags &= ~CE_FSMONITOR_VALID;
27 }
28
29 static int fsmonitor_hook_version(void)
30 {
31         int hook_version;
32
33         if (git_config_get_int("core.fsmonitorhookversion", &hook_version))
34                 return -1;
35
36         if (hook_version == HOOK_INTERFACE_VERSION1 ||
37             hook_version == HOOK_INTERFACE_VERSION2)
38                 return hook_version;
39
40         warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
41                 "Must be 1 or 2.", hook_version);
42         return -1;
43 }
44
45 int read_fsmonitor_extension(struct index_state *istate, const void *data,
46         unsigned long sz)
47 {
48         const char *index = data;
49         uint32_t hdr_version;
50         uint32_t ewah_size;
51         struct ewah_bitmap *fsmonitor_dirty;
52         int ret;
53         uint64_t timestamp;
54         struct strbuf last_update = STRBUF_INIT;
55
56         if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
57                 return error("corrupt fsmonitor extension (too short)");
58
59         hdr_version = get_be32(index);
60         index += sizeof(uint32_t);
61         if (hdr_version == INDEX_EXTENSION_VERSION1) {
62                 timestamp = get_be64(index);
63                 strbuf_addf(&last_update, "%"PRIu64"", timestamp);
64                 index += sizeof(uint64_t);
65         } else if (hdr_version == INDEX_EXTENSION_VERSION2) {
66                 strbuf_addstr(&last_update, index);
67                 index += last_update.len + 1;
68         } else {
69                 return error("bad fsmonitor version %d", hdr_version);
70         }
71
72         istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
73
74         ewah_size = get_be32(index);
75         index += sizeof(uint32_t);
76
77         fsmonitor_dirty = ewah_new();
78         ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
79         if (ret != ewah_size) {
80                 ewah_free(fsmonitor_dirty);
81                 return error("failed to parse ewah bitmap reading fsmonitor index extension");
82         }
83         istate->fsmonitor_dirty = fsmonitor_dirty;
84
85         if (!istate->split_index &&
86             istate->fsmonitor_dirty->bit_size > istate->cache_nr)
87                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
88                     (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
89
90         trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
91         return 0;
92 }
93
94 void fill_fsmonitor_bitmap(struct index_state *istate)
95 {
96         unsigned int i, skipped = 0;
97         istate->fsmonitor_dirty = ewah_new();
98         for (i = 0; i < istate->cache_nr; i++) {
99                 if (istate->cache[i]->ce_flags & CE_REMOVE)
100                         skipped++;
101                 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
102                         ewah_set(istate->fsmonitor_dirty, i - skipped);
103         }
104 }
105
106 void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
107 {
108         uint32_t hdr_version;
109         uint32_t ewah_start;
110         uint32_t ewah_size = 0;
111         int fixup = 0;
112
113         if (!istate->split_index &&
114             istate->fsmonitor_dirty->bit_size > istate->cache_nr)
115                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
116                     (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
117
118         put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
119         strbuf_add(sb, &hdr_version, sizeof(uint32_t));
120
121         strbuf_addstr(sb, istate->fsmonitor_last_update);
122         strbuf_addch(sb, 0); /* Want to keep a NUL */
123
124         fixup = sb->len;
125         strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
126
127         ewah_start = sb->len;
128         ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
129         ewah_free(istate->fsmonitor_dirty);
130         istate->fsmonitor_dirty = NULL;
131
132         /* fix up size field */
133         put_be32(&ewah_size, sb->len - ewah_start);
134         memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
135
136         trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
137 }
138
139 /*
140  * Call the query-fsmonitor hook passing the last update token of the saved results.
141  */
142 static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
143 {
144         struct child_process cp = CHILD_PROCESS_INIT;
145         int result;
146
147         if (!core_fsmonitor)
148                 return -1;
149
150         strvec_push(&cp.args, core_fsmonitor);
151         strvec_pushf(&cp.args, "%d", version);
152         strvec_pushf(&cp.args, "%s", last_update);
153         cp.use_shell = 1;
154         cp.dir = get_git_work_tree();
155
156         trace2_region_enter("fsm_hook", "query", NULL);
157
158         result = capture_command(&cp, query_result, 1024);
159
160         if (result)
161                 trace2_data_intmax("fsm_hook", NULL, "query/failed", result);
162         else {
163                 trace2_data_intmax("fsm_hook", NULL, "query/response-length",
164                                    query_result->len);
165
166                 if (fsmonitor_is_trivial_response(query_result))
167                         trace2_data_intmax("fsm_hook", NULL,
168                                            "query/trivial-response", 1);
169         }
170
171         trace2_region_leave("fsm_hook", "query", NULL);
172
173         return result;
174 }
175
176 int fsmonitor_is_trivial_response(const struct strbuf *query_result)
177 {
178         static char trivial_response[3] = { '\0', '/', '\0' };
179         int is_trivial = !memcmp(trivial_response,
180                                  &query_result->buf[query_result->len - 3], 3);
181
182         return is_trivial;
183 }
184
185 static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
186 {
187         int pos = index_name_pos(istate, name, strlen(name));
188
189         if (pos >= 0) {
190                 struct cache_entry *ce = istate->cache[pos];
191                 ce->ce_flags &= ~CE_FSMONITOR_VALID;
192         }
193
194         /*
195          * Mark the untracked cache dirty even if it wasn't found in the index
196          * as it could be a new untracked file.
197          */
198         trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
199         untracked_cache_invalidate_path(istate, name, 0);
200 }
201
202 void refresh_fsmonitor(struct index_state *istate)
203 {
204         struct strbuf query_result = STRBUF_INIT;
205         int query_success = 0, hook_version = -1;
206         size_t bol = 0; /* beginning of line */
207         uint64_t last_update;
208         struct strbuf last_update_token = STRBUF_INIT;
209         char *buf;
210         unsigned int i;
211
212         if (!core_fsmonitor || istate->fsmonitor_has_run_once)
213                 return;
214
215         hook_version = fsmonitor_hook_version();
216
217         istate->fsmonitor_has_run_once = 1;
218
219         trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
220         /*
221          * This could be racy so save the date/time now and query_fsmonitor
222          * should be inclusive to ensure we don't miss potential changes.
223          */
224         last_update = getnanotime();
225         if (hook_version == HOOK_INTERFACE_VERSION1)
226                 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
227
228         /*
229          * If we have a last update token, call query_fsmonitor for the set of
230          * changes since that token, else assume everything is possibly dirty
231          * and check it all.
232          */
233         if (istate->fsmonitor_last_update) {
234                 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
235                         query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION2,
236                                 istate->fsmonitor_last_update, &query_result);
237
238                         if (query_success) {
239                                 if (hook_version < 0)
240                                         hook_version = HOOK_INTERFACE_VERSION2;
241
242                                 /*
243                                  * First entry will be the last update token
244                                  * Need to use a char * variable because static
245                                  * analysis was suggesting to use strbuf_addbuf
246                                  * but we don't want to copy the entire strbuf
247                                  * only the chars up to the first NUL
248                                  */
249                                 buf = query_result.buf;
250                                 strbuf_addstr(&last_update_token, buf);
251                                 if (!last_update_token.len) {
252                                         warning("Empty last update token.");
253                                         query_success = 0;
254                                 } else {
255                                         bol = last_update_token.len + 1;
256                                 }
257                         } else if (hook_version < 0) {
258                                 hook_version = HOOK_INTERFACE_VERSION1;
259                                 if (!last_update_token.len)
260                                         strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
261                         }
262                 }
263
264                 if (hook_version == HOOK_INTERFACE_VERSION1) {
265                         query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION1,
266                                 istate->fsmonitor_last_update, &query_result);
267                 }
268
269                 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
270                 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
271                         core_fsmonitor, query_success ? "success" : "failure");
272         }
273
274         /* a fsmonitor process can return '/' to indicate all entries are invalid */
275         if (query_success && query_result.buf[bol] != '/') {
276                 /* Mark all entries returned by the monitor as dirty */
277                 buf = query_result.buf;
278                 for (i = bol; i < query_result.len; i++) {
279                         if (buf[i] != '\0')
280                                 continue;
281                         fsmonitor_refresh_callback(istate, buf + bol);
282                         bol = i + 1;
283                 }
284                 if (bol < query_result.len)
285                         fsmonitor_refresh_callback(istate, buf + bol);
286
287                 /* Now mark the untracked cache for fsmonitor usage */
288                 if (istate->untracked)
289                         istate->untracked->use_fsmonitor = 1;
290         } else {
291
292                 /* We only want to run the post index changed hook if we've actually changed entries, so keep track
293                  * if we actually changed entries or not */
294                 int is_cache_changed = 0;
295                 /* Mark all entries invalid */
296                 for (i = 0; i < istate->cache_nr; i++) {
297                         if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
298                                 is_cache_changed = 1;
299                                 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
300                         }
301                 }
302
303                 /* If we're going to check every file, ensure we save the results */
304                 if (is_cache_changed)
305                         istate->cache_changed |= FSMONITOR_CHANGED;
306
307                 if (istate->untracked)
308                         istate->untracked->use_fsmonitor = 0;
309         }
310         strbuf_release(&query_result);
311
312         /* Now that we've updated istate, save the last_update_token */
313         FREE_AND_NULL(istate->fsmonitor_last_update);
314         istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
315 }
316
317 void add_fsmonitor(struct index_state *istate)
318 {
319         unsigned int i;
320         struct strbuf last_update = STRBUF_INIT;
321
322         if (!istate->fsmonitor_last_update) {
323                 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
324                 istate->cache_changed |= FSMONITOR_CHANGED;
325                 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
326                 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
327
328                 /* reset the fsmonitor state */
329                 for (i = 0; i < istate->cache_nr; i++)
330                         istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
331
332                 /* reset the untracked cache */
333                 if (istate->untracked) {
334                         add_untracked_cache(istate);
335                         istate->untracked->use_fsmonitor = 1;
336                 }
337
338                 /* Update the fsmonitor state */
339                 refresh_fsmonitor(istate);
340         }
341 }
342
343 void remove_fsmonitor(struct index_state *istate)
344 {
345         if (istate->fsmonitor_last_update) {
346                 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
347                 istate->cache_changed |= FSMONITOR_CHANGED;
348                 FREE_AND_NULL(istate->fsmonitor_last_update);
349         }
350 }
351
352 void tweak_fsmonitor(struct index_state *istate)
353 {
354         unsigned int i;
355         int fsmonitor_enabled = git_config_get_fsmonitor();
356
357         if (istate->fsmonitor_dirty) {
358                 if (fsmonitor_enabled) {
359                         /* Mark all entries valid */
360                         for (i = 0; i < istate->cache_nr; i++) {
361                                 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
362                         }
363
364                         /* Mark all previously saved entries as dirty */
365                         if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
366                                 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
367                                     (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
368                         ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
369
370                         refresh_fsmonitor(istate);
371                 }
372
373                 ewah_free(istate->fsmonitor_dirty);
374                 istate->fsmonitor_dirty = NULL;
375         }
376
377         switch (fsmonitor_enabled) {
378         case -1: /* keep: do nothing */
379                 break;
380         case 0: /* false */
381                 remove_fsmonitor(istate);
382                 break;
383         case 1: /* true */
384                 add_fsmonitor(istate);
385                 break;
386         default: /* unknown value: do nothing */
387                 break;
388         }
389 }