fsmonitor.con commit Merge branch 'cc/perf-bisect' (02d11bb)
   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_VERSION (1)
  10#define HOOK_INTERFACE_VERSION  (1)
  11
  12struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
  13
  14static void fsmonitor_ewah_callback(size_t pos, void *is)
  15{
  16        struct index_state *istate = (struct index_state *)is;
  17        struct cache_entry *ce = istate->cache[pos];
  18
  19        ce->ce_flags &= ~CE_FSMONITOR_VALID;
  20}
  21
  22int read_fsmonitor_extension(struct index_state *istate, const void *data,
  23        unsigned long sz)
  24{
  25        const char *index = data;
  26        uint32_t hdr_version;
  27        uint32_t ewah_size;
  28        struct ewah_bitmap *fsmonitor_dirty;
  29        int ret;
  30
  31        if (sz < sizeof(uint32_t) + sizeof(uint64_t) + sizeof(uint32_t))
  32                return error("corrupt fsmonitor extension (too short)");
  33
  34        hdr_version = get_be32(index);
  35        index += sizeof(uint32_t);
  36        if (hdr_version != INDEX_EXTENSION_VERSION)
  37                return error("bad fsmonitor version %d", hdr_version);
  38
  39        istate->fsmonitor_last_update = get_be64(index);
  40        index += sizeof(uint64_t);
  41
  42        ewah_size = get_be32(index);
  43        index += sizeof(uint32_t);
  44
  45        fsmonitor_dirty = ewah_new();
  46        ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
  47        if (ret != ewah_size) {
  48                ewah_free(fsmonitor_dirty);
  49                return error("failed to parse ewah bitmap reading fsmonitor index extension");
  50        }
  51        istate->fsmonitor_dirty = fsmonitor_dirty;
  52
  53        trace_printf_key(&trace_fsmonitor, "read fsmonitor extension successful");
  54        return 0;
  55}
  56
  57void fill_fsmonitor_bitmap(struct index_state *istate)
  58{
  59        int i;
  60        istate->fsmonitor_dirty = ewah_new();
  61        for (i = 0; i < istate->cache_nr; i++)
  62                if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
  63                        ewah_set(istate->fsmonitor_dirty, i);
  64}
  65
  66void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
  67{
  68        uint32_t hdr_version;
  69        uint64_t tm;
  70        uint32_t ewah_start;
  71        uint32_t ewah_size = 0;
  72        int fixup = 0;
  73
  74        put_be32(&hdr_version, INDEX_EXTENSION_VERSION);
  75        strbuf_add(sb, &hdr_version, sizeof(uint32_t));
  76
  77        put_be64(&tm, istate->fsmonitor_last_update);
  78        strbuf_add(sb, &tm, sizeof(uint64_t));
  79        fixup = sb->len;
  80        strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
  81
  82        ewah_start = sb->len;
  83        ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
  84        ewah_free(istate->fsmonitor_dirty);
  85        istate->fsmonitor_dirty = NULL;
  86
  87        /* fix up size field */
  88        put_be32(&ewah_size, sb->len - ewah_start);
  89        memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
  90
  91        trace_printf_key(&trace_fsmonitor, "write fsmonitor extension successful");
  92}
  93
  94/*
  95 * Call the query-fsmonitor hook passing the time of the last saved results.
  96 */
  97static int query_fsmonitor(int version, uint64_t last_update, struct strbuf *query_result)
  98{
  99        struct child_process cp = CHILD_PROCESS_INIT;
 100        char ver[64];
 101        char date[64];
 102        const char *argv[4];
 103
 104        if (!(argv[0] = core_fsmonitor))
 105                return -1;
 106
 107        snprintf(ver, sizeof(ver), "%d", version);
 108        snprintf(date, sizeof(date), "%" PRIuMAX, (uintmax_t)last_update);
 109        argv[1] = ver;
 110        argv[2] = date;
 111        argv[3] = NULL;
 112        cp.argv = argv;
 113        cp.use_shell = 1;
 114        cp.dir = get_git_work_tree();
 115
 116        return capture_command(&cp, query_result, 1024);
 117}
 118
 119static void fsmonitor_refresh_callback(struct index_state *istate, const char *name)
 120{
 121        int pos = index_name_pos(istate, name, strlen(name));
 122
 123        if (pos >= 0) {
 124                struct cache_entry *ce = istate->cache[pos];
 125                ce->ce_flags &= ~CE_FSMONITOR_VALID;
 126        }
 127
 128        /*
 129         * Mark the untracked cache dirty even if it wasn't found in the index
 130         * as it could be a new untracked file.
 131         */
 132        trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
 133        untracked_cache_invalidate_path(istate, name, 0);
 134}
 135
 136void refresh_fsmonitor(struct index_state *istate)
 137{
 138        static int has_run_once = 0;
 139        struct strbuf query_result = STRBUF_INIT;
 140        int query_success = 0;
 141        size_t bol; /* beginning of line */
 142        uint64_t last_update;
 143        char *buf;
 144        int i;
 145
 146        if (!core_fsmonitor || has_run_once)
 147                return;
 148        has_run_once = 1;
 149
 150        trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
 151        /*
 152         * This could be racy so save the date/time now and query_fsmonitor
 153         * should be inclusive to ensure we don't miss potential changes.
 154         */
 155        last_update = getnanotime();
 156
 157        /*
 158         * If we have a last update time, call query_fsmonitor for the set of
 159         * changes since that time, else assume everything is possibly dirty
 160         * and check it all.
 161         */
 162        if (istate->fsmonitor_last_update) {
 163                query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION,
 164                        istate->fsmonitor_last_update, &query_result);
 165                trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
 166                trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
 167                        core_fsmonitor, query_success ? "success" : "failure");
 168        }
 169
 170        /* a fsmonitor process can return '/' to indicate all entries are invalid */
 171        if (query_success && query_result.buf[0] != '/') {
 172                /* Mark all entries returned by the monitor as dirty */
 173                buf = query_result.buf;
 174                bol = 0;
 175                for (i = 0; i < query_result.len; i++) {
 176                        if (buf[i] != '\0')
 177                                continue;
 178                        fsmonitor_refresh_callback(istate, buf + bol);
 179                        bol = i + 1;
 180                }
 181                if (bol < query_result.len)
 182                        fsmonitor_refresh_callback(istate, buf + bol);
 183        } else {
 184                /* Mark all entries invalid */
 185                for (i = 0; i < istate->cache_nr; i++)
 186                        istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
 187
 188                /* If we're going to check every file, ensure we save the results */
 189                istate->cache_changed |= FSMONITOR_CHANGED;
 190
 191                if (istate->untracked)
 192                        istate->untracked->use_fsmonitor = 0;
 193        }
 194        strbuf_release(&query_result);
 195
 196        /* Now that we've updated istate, save the last_update time */
 197        istate->fsmonitor_last_update = last_update;
 198}
 199
 200void add_fsmonitor(struct index_state *istate)
 201{
 202        int i;
 203
 204        if (!istate->fsmonitor_last_update) {
 205                trace_printf_key(&trace_fsmonitor, "add fsmonitor");
 206                istate->cache_changed |= FSMONITOR_CHANGED;
 207                istate->fsmonitor_last_update = getnanotime();
 208
 209                /* reset the fsmonitor state */
 210                for (i = 0; i < istate->cache_nr; i++)
 211                        istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
 212
 213                /* reset the untracked cache */
 214                if (istate->untracked) {
 215                        add_untracked_cache(istate);
 216                        istate->untracked->use_fsmonitor = 1;
 217                }
 218
 219                /* Update the fsmonitor state */
 220                refresh_fsmonitor(istate);
 221        }
 222}
 223
 224void remove_fsmonitor(struct index_state *istate)
 225{
 226        if (istate->fsmonitor_last_update) {
 227                trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
 228                istate->cache_changed |= FSMONITOR_CHANGED;
 229                istate->fsmonitor_last_update = 0;
 230        }
 231}
 232
 233void tweak_fsmonitor(struct index_state *istate)
 234{
 235        int i;
 236        int fsmonitor_enabled = git_config_get_fsmonitor();
 237
 238        if (istate->fsmonitor_dirty) {
 239                if (fsmonitor_enabled) {
 240                        /* Mark all entries valid */
 241                        for (i = 0; i < istate->cache_nr; i++) {
 242                                istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
 243                        }
 244
 245                        /* Mark all previously saved entries as dirty */
 246                        ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
 247
 248                        /* Now mark the untracked cache for fsmonitor usage */
 249                        if (istate->untracked)
 250                                istate->untracked->use_fsmonitor = 1;
 251                }
 252
 253                ewah_free(istate->fsmonitor_dirty);
 254                istate->fsmonitor_dirty = NULL;
 255        }
 256
 257        switch (fsmonitor_enabled) {
 258        case -1: /* keep: do nothing */
 259                break;
 260        case 0: /* false */
 261                remove_fsmonitor(istate);
 262                break;
 263        case 1: /* true */
 264                add_fsmonitor(istate);
 265                break;
 266        default: /* unknown value: do nothing */
 267                break;
 268        }
 269}