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