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