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