Git fork
at reftables-rust 189 lines 4.6 kB view raw
1/* 2 * Copyright (C) 2008 Linus Torvalds 3 */ 4 5#define DISABLE_SIGN_COMPARE_WARNINGS 6 7#include "git-compat-util.h" 8#include "pathspec.h" 9#include "dir.h" 10#include "environment.h" 11#include "fsmonitor.h" 12#include "gettext.h" 13#include "parse.h" 14#include "preload-index.h" 15#include "progress.h" 16#include "read-cache.h" 17#include "thread-utils.h" 18#include "repository.h" 19#include "symlinks.h" 20#include "trace2.h" 21#include "config.h" 22 23/* 24 * Mostly randomly chosen maximum thread counts: we 25 * cap the parallelism to 20 threads, and we want 26 * to have at least 500 lstat's per thread for it to 27 * be worth starting a thread. 28 */ 29#define MAX_PARALLEL (20) 30#define THREAD_COST (500) 31 32struct progress_data { 33 unsigned long n; 34 struct progress *progress; 35 pthread_mutex_t mutex; 36}; 37 38struct thread_data { 39 pthread_t pthread; 40 struct index_state *index; 41 struct pathspec pathspec; 42 struct progress_data *progress; 43 int offset, nr; 44 int t2_nr_lstat; 45}; 46 47static void *preload_thread(void *_data) 48{ 49 int nr, last_nr; 50 struct thread_data *p = _data; 51 struct index_state *index = p->index; 52 struct cache_entry **cep = index->cache + p->offset; 53 struct cache_def cache = CACHE_DEF_INIT; 54 55 nr = p->nr; 56 if (nr + p->offset > index->cache_nr) 57 nr = index->cache_nr - p->offset; 58 last_nr = nr; 59 60 do { 61 struct cache_entry *ce = *cep++; 62 struct stat st; 63 64 if (ce_stage(ce)) 65 continue; 66 if (S_ISGITLINK(ce->ce_mode)) 67 continue; 68 if (ce_uptodate(ce)) 69 continue; 70 if (ce_skip_worktree(ce)) 71 continue; 72 if (ce->ce_flags & CE_FSMONITOR_VALID) 73 continue; 74 if (p->progress && !(nr & 31)) { 75 struct progress_data *pd = p->progress; 76 77 pthread_mutex_lock(&pd->mutex); 78 pd->n += last_nr - nr; 79 display_progress(pd->progress, pd->n); 80 pthread_mutex_unlock(&pd->mutex); 81 last_nr = nr; 82 } 83 if (!ce_path_match(index, ce, &p->pathspec, NULL)) 84 continue; 85 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce))) 86 continue; 87 p->t2_nr_lstat++; 88 if (lstat(ce->name, &st)) 89 continue; 90 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR)) 91 continue; 92 ce_mark_uptodate(ce); 93 mark_fsmonitor_valid(index, ce); 94 } while (--nr > 0); 95 if (p->progress) { 96 struct progress_data *pd = p->progress; 97 98 pthread_mutex_lock(&pd->mutex); 99 display_progress(pd->progress, pd->n + last_nr); 100 pthread_mutex_unlock(&pd->mutex); 101 } 102 cache_def_clear(&cache); 103 return NULL; 104} 105 106void preload_index(struct index_state *index, 107 const struct pathspec *pathspec, 108 unsigned int refresh_flags) 109{ 110 int threads, i, work, offset; 111 struct thread_data data[MAX_PARALLEL]; 112 struct progress_data pd; 113 int t2_sum_lstat = 0; 114 int core_preload_index = 1; 115 116 repo_config_get_bool(index->repo, "core.preloadindex", &core_preload_index); 117 118 if (!HAVE_THREADS || !core_preload_index) 119 return; 120 121 threads = index->cache_nr / THREAD_COST; 122 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0)) 123 threads = 2; 124 if (threads < 2) 125 return; 126 127 trace2_region_enter("index", "preload", NULL); 128 129 trace_performance_enter(); 130 if (threads > MAX_PARALLEL) 131 threads = MAX_PARALLEL; 132 offset = 0; 133 work = DIV_ROUND_UP(index->cache_nr, threads); 134 memset(&data, 0, sizeof(data)); 135 136 memset(&pd, 0, sizeof(pd)); 137 if (refresh_flags & REFRESH_PROGRESS && isatty(2)) { 138 pd.progress = start_delayed_progress(index->repo, 139 _("Refreshing index"), 140 index->cache_nr); 141 pthread_mutex_init(&pd.mutex, NULL); 142 } 143 144 for (i = 0; i < threads; i++) { 145 struct thread_data *p = data+i; 146 int err; 147 148 p->index = index; 149 if (pathspec) 150 copy_pathspec(&p->pathspec, pathspec); 151 p->offset = offset; 152 p->nr = work; 153 if (pd.progress) 154 p->progress = &pd; 155 offset += work; 156 err = pthread_create(&p->pthread, NULL, preload_thread, p); 157 158 if (err) 159 die(_("unable to create threaded lstat: %s"), strerror(err)); 160 } 161 for (i = 0; i < threads; i++) { 162 struct thread_data *p = data+i; 163 if (pthread_join(p->pthread, NULL)) 164 die("unable to join threaded lstat"); 165 t2_sum_lstat += p->t2_nr_lstat; 166 } 167 stop_progress(&pd.progress); 168 169 if (pathspec) { 170 /* earlier we made deep copies for each thread to work with */ 171 for (i = 0; i < threads; i++) 172 clear_pathspec(&data[i].pathspec); 173 } 174 175 trace_performance_leave("preload index"); 176 177 trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat); 178 trace2_region_leave("index", "preload", NULL); 179} 180 181int repo_read_index_preload(struct repository *repo, 182 const struct pathspec *pathspec, 183 unsigned int refresh_flags) 184{ 185 int retval = repo_read_index(repo); 186 187 preload_index(repo->index, pathspec, refresh_flags); 188 return retval; 189}