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