preload-index.con commit gitweb: Incremental blame (using JavaScript) (4af819d)
   1/*
   2 * Copyright (C) 2008 Linus Torvalds
   3 */
   4#include "cache.h"
   5
   6#ifdef NO_PTHREADS
   7static void preload_index(struct index_state *index, const char **pathspec)
   8{
   9        ; /* nothing */
  10}
  11#else
  12
  13#include <pthread.h>
  14
  15/*
  16 * Mostly randomly chosen maximum thread counts: we
  17 * cap the parallelism to 20 threads, and we want
  18 * to have at least 500 lstat's per thread for it to
  19 * be worth starting a thread.
  20 */
  21#define MAX_PARALLEL (20)
  22#define THREAD_COST (500)
  23
  24struct thread_data {
  25        pthread_t pthread;
  26        struct index_state *index;
  27        const char **pathspec;
  28        int offset, nr;
  29};
  30
  31static void *preload_thread(void *_data)
  32{
  33        int nr;
  34        struct thread_data *p = _data;
  35        struct index_state *index = p->index;
  36        struct cache_entry **cep = index->cache + p->offset;
  37        struct cache_def cache;
  38
  39        memset(&cache, 0, sizeof(cache));
  40        nr = p->nr;
  41        if (nr + p->offset > index->cache_nr)
  42                nr = index->cache_nr - p->offset;
  43
  44        do {
  45                struct cache_entry *ce = *cep++;
  46                struct stat st;
  47
  48                if (ce_stage(ce))
  49                        continue;
  50                if (ce_uptodate(ce))
  51                        continue;
  52                if (!ce_path_match(ce, p->pathspec))
  53                        continue;
  54                if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
  55                        continue;
  56                if (lstat(ce->name, &st))
  57                        continue;
  58                if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
  59                        continue;
  60                ce_mark_uptodate(ce);
  61        } while (--nr > 0);
  62        return NULL;
  63}
  64
  65static void preload_index(struct index_state *index, const char **pathspec)
  66{
  67        int threads, i, work, offset;
  68        struct thread_data data[MAX_PARALLEL];
  69
  70        if (!core_preload_index)
  71                return;
  72
  73        threads = index->cache_nr / THREAD_COST;
  74        if (threads < 2)
  75                return;
  76        if (threads > MAX_PARALLEL)
  77                threads = MAX_PARALLEL;
  78        offset = 0;
  79        work = DIV_ROUND_UP(index->cache_nr, threads);
  80        for (i = 0; i < threads; i++) {
  81                struct thread_data *p = data+i;
  82                p->index = index;
  83                p->pathspec = pathspec;
  84                p->offset = offset;
  85                p->nr = work;
  86                offset += work;
  87                if (pthread_create(&p->pthread, NULL, preload_thread, p))
  88                        die("unable to create threaded lstat");
  89        }
  90        for (i = 0; i < threads; i++) {
  91                struct thread_data *p = data+i;
  92                if (pthread_join(p->pthread, NULL))
  93                        die("unable to join threaded lstat");
  94        }
  95}
  96#endif
  97
  98int read_index_preload(struct index_state *index, const char **pathspec)
  99{
 100        int retval = read_index(index);
 101
 102        preload_index(index, pathspec);
 103        return retval;
 104}