Merge branch 'jk/fsmonitor-event-listener-race-fix' into maint-2.47
[git/gitster.git] / preload-index.c
blob7926eb09a695f7c5ea0e0de1e48690caf6c6fa6f
1 /*
2 * Copyright (C) 2008 Linus Torvalds
3 */
5 #define USE_THE_REPOSITORY_VARIABLE
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"
23 * Mostly randomly chosen maximum thread counts: we
24 * cap the parallelism to 20 threads, and we want
25 * to have at least 500 lstat's per thread for it to
26 * be worth starting a thread.
28 #define MAX_PARALLEL (20)
29 #define THREAD_COST (500)
31 struct progress_data {
32 unsigned long n;
33 struct progress *progress;
34 pthread_mutex_t mutex;
37 struct thread_data {
38 pthread_t pthread;
39 struct index_state *index;
40 struct pathspec pathspec;
41 struct progress_data *progress;
42 int offset, nr;
43 int t2_nr_lstat;
46 static void *preload_thread(void *_data)
48 int nr, last_nr;
49 struct thread_data *p = _data;
50 struct index_state *index = p->index;
51 struct cache_entry **cep = index->cache + p->offset;
52 struct cache_def cache = CACHE_DEF_INIT;
54 nr = p->nr;
55 if (nr + p->offset > index->cache_nr)
56 nr = index->cache_nr - p->offset;
57 last_nr = nr;
59 do {
60 struct cache_entry *ce = *cep++;
61 struct stat st;
63 if (ce_stage(ce))
64 continue;
65 if (S_ISGITLINK(ce->ce_mode))
66 continue;
67 if (ce_uptodate(ce))
68 continue;
69 if (ce_skip_worktree(ce))
70 continue;
71 if (ce->ce_flags & CE_FSMONITOR_VALID)
72 continue;
73 if (p->progress && !(nr & 31)) {
74 struct progress_data *pd = p->progress;
76 pthread_mutex_lock(&pd->mutex);
77 pd->n += last_nr - nr;
78 display_progress(pd->progress, pd->n);
79 pthread_mutex_unlock(&pd->mutex);
80 last_nr = nr;
82 if (!ce_path_match(index, ce, &p->pathspec, NULL))
83 continue;
84 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
85 continue;
86 p->t2_nr_lstat++;
87 if (lstat(ce->name, &st))
88 continue;
89 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
90 continue;
91 ce_mark_uptodate(ce);
92 mark_fsmonitor_valid(index, ce);
93 } while (--nr > 0);
94 if (p->progress) {
95 struct progress_data *pd = p->progress;
97 pthread_mutex_lock(&pd->mutex);
98 display_progress(pd->progress, pd->n + last_nr);
99 pthread_mutex_unlock(&pd->mutex);
101 cache_def_clear(&cache);
102 return NULL;
105 void preload_index(struct index_state *index,
106 const struct pathspec *pathspec,
107 unsigned int refresh_flags)
109 int threads, i, work, offset;
110 struct thread_data data[MAX_PARALLEL];
111 struct progress_data pd;
112 int t2_sum_lstat = 0;
114 if (!HAVE_THREADS || !core_preload_index)
115 return;
117 threads = index->cache_nr / THREAD_COST;
118 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
119 threads = 2;
120 if (threads < 2)
121 return;
123 trace2_region_enter("index", "preload", NULL);
125 trace_performance_enter();
126 if (threads > MAX_PARALLEL)
127 threads = MAX_PARALLEL;
128 offset = 0;
129 work = DIV_ROUND_UP(index->cache_nr, threads);
130 memset(&data, 0, sizeof(data));
132 memset(&pd, 0, sizeof(pd));
133 if (refresh_flags & REFRESH_PROGRESS && isatty(2)) {
134 pd.progress = start_delayed_progress(_("Refreshing index"), index->cache_nr);
135 pthread_mutex_init(&pd.mutex, NULL);
138 for (i = 0; i < threads; i++) {
139 struct thread_data *p = data+i;
140 int err;
142 p->index = index;
143 if (pathspec)
144 copy_pathspec(&p->pathspec, pathspec);
145 p->offset = offset;
146 p->nr = work;
147 if (pd.progress)
148 p->progress = &pd;
149 offset += work;
150 err = pthread_create(&p->pthread, NULL, preload_thread, p);
152 if (err)
153 die(_("unable to create threaded lstat: %s"), strerror(err));
155 for (i = 0; i < threads; i++) {
156 struct thread_data *p = data+i;
157 if (pthread_join(p->pthread, NULL))
158 die("unable to join threaded lstat");
159 t2_sum_lstat += p->t2_nr_lstat;
161 stop_progress(&pd.progress);
163 if (pathspec) {
164 /* earlier we made deep copies for each thread to work with */
165 for (i = 0; i < threads; i++)
166 clear_pathspec(&data[i].pathspec);
169 trace_performance_leave("preload index");
171 trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
172 trace2_region_leave("index", "preload", NULL);
175 int repo_read_index_preload(struct repository *repo,
176 const struct pathspec *pathspec,
177 unsigned int refresh_flags)
179 int retval = repo_read_index(repo);
181 preload_index(repo->index, pathspec, refresh_flags);
182 return retval;