]>
Commit | Line | Data |
---|---|---|
671c9b7e LT |
1 | /* |
2 | * Copyright (C) 2008 Linus Torvalds | |
3 | */ | |
4 | #include "cache.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" |
ae9af122 | 11 | #include "progress.h" |
e8d40566 | 12 | #include "thread-utils.h" |
e1ff0a32 | 13 | #include "repository.h" |
671c9b7e LT |
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 | ||
ae9af122 NTND |
24 | struct progress_data { |
25 | unsigned long n; | |
26 | struct progress *progress; | |
27 | pthread_mutex_t mutex; | |
28 | }; | |
29 | ||
671c9b7e LT |
30 | struct thread_data { |
31 | pthread_t pthread; | |
32 | struct index_state *index; | |
5ab2a2da | 33 | struct pathspec pathspec; |
ae9af122 | 34 | struct progress_data *progress; |
671c9b7e | 35 | int offset, nr; |
8c4b7503 | 36 | int t2_nr_lstat; |
671c9b7e LT |
37 | }; |
38 | ||
39 | static void *preload_thread(void *_data) | |
40 | { | |
ae9af122 | 41 | int nr, last_nr; |
671c9b7e LT |
42 | struct thread_data *p = _data; |
43 | struct index_state *index = p->index; | |
44 | struct cache_entry **cep = index->cache + p->offset; | |
e7c73053 | 45 | struct cache_def cache = CACHE_DEF_INIT; |
671c9b7e LT |
46 | |
47 | nr = p->nr; | |
48 | if (nr + p->offset > index->cache_nr) | |
49 | nr = index->cache_nr - p->offset; | |
ae9af122 | 50 | last_nr = nr; |
671c9b7e LT |
51 | |
52 | do { | |
53 | struct cache_entry *ce = *cep++; | |
54 | struct stat st; | |
55 | ||
56 | if (ce_stage(ce)) | |
57 | continue; | |
125fd984 JH |
58 | if (S_ISGITLINK(ce->ce_mode)) |
59 | continue; | |
671c9b7e LT |
60 | if (ce_uptodate(ce)) |
61 | continue; | |
e596acc2 JH |
62 | if (ce_skip_worktree(ce)) |
63 | continue; | |
883e248b BP |
64 | if (ce->ce_flags & CE_FSMONITOR_VALID) |
65 | continue; | |
ae9af122 NTND |
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 | } | |
f9beff03 | 75 | if (!ce_path_match(index, ce, &p->pathspec, NULL)) |
671c9b7e | 76 | continue; |
f62ce3de LT |
77 | if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce))) |
78 | continue; | |
8c4b7503 | 79 | p->t2_nr_lstat++; |
671c9b7e LT |
80 | if (lstat(ce->name, &st)) |
81 | continue; | |
883e248b | 82 | if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR)) |
671c9b7e LT |
83 | continue; |
84 | ce_mark_uptodate(ce); | |
b5a81697 | 85 | mark_fsmonitor_valid(index, ce); |
671c9b7e | 86 | } while (--nr > 0); |
ae9af122 NTND |
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 | } | |
2a608391 | 94 | cache_def_clear(&cache); |
671c9b7e LT |
95 | return NULL; |
96 | } | |
97 | ||
99ce720c BP |
98 | void preload_index(struct index_state *index, |
99 | const struct pathspec *pathspec, | |
100 | unsigned int refresh_flags) | |
671c9b7e LT |
101 | { |
102 | int threads, i, work, offset; | |
103 | struct thread_data data[MAX_PARALLEL]; | |
ae9af122 | 104 | struct progress_data pd; |
8c4b7503 | 105 | int t2_sum_lstat = 0; |
671c9b7e | 106 | |
e8d40566 | 107 | if (!HAVE_THREADS || !core_preload_index) |
671c9b7e LT |
108 | return; |
109 | ||
110 | threads = index->cache_nr / THREAD_COST; | |
5765d97b | 111 | if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0)) |
3e2c6696 | 112 | threads = 2; |
671c9b7e LT |
113 | if (threads < 2) |
114 | return; | |
8c4b7503 JH |
115 | |
116 | trace2_region_enter("index", "preload", NULL); | |
117 | ||
c46c406a | 118 | trace_performance_enter(); |
671c9b7e LT |
119 | if (threads > MAX_PARALLEL) |
120 | threads = MAX_PARALLEL; | |
121 | offset = 0; | |
98cb6f30 | 122 | work = DIV_ROUND_UP(index->cache_nr, threads); |
5ab2a2da | 123 | memset(&data, 0, sizeof(data)); |
ae9af122 NTND |
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 | ||
671c9b7e LT |
131 | for (i = 0; i < threads; i++) { |
132 | struct thread_data *p = data+i; | |
2179045f NTND |
133 | int err; |
134 | ||
671c9b7e | 135 | p->index = index; |
5ab2a2da NTND |
136 | if (pathspec) |
137 | copy_pathspec(&p->pathspec, pathspec); | |
671c9b7e LT |
138 | p->offset = offset; |
139 | p->nr = work; | |
ae9af122 NTND |
140 | if (pd.progress) |
141 | p->progress = &pd; | |
671c9b7e | 142 | offset += work; |
2179045f NTND |
143 | err = pthread_create(&p->pthread, NULL, preload_thread, p); |
144 | ||
145 | if (err) | |
146 | die(_("unable to create threaded lstat: %s"), strerror(err)); | |
671c9b7e LT |
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"); | |
8c4b7503 | 152 | t2_sum_lstat += p->t2_nr_lstat; |
671c9b7e | 153 | } |
ae9af122 NTND |
154 | stop_progress(&pd.progress); |
155 | ||
23578904 AD |
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 | ||
c46c406a | 162 | trace_performance_leave("preload index"); |
8c4b7503 JH |
163 | |
164 | trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat); | |
165 | trace2_region_leave("index", "preload", NULL); | |
671c9b7e LT |
166 | } |
167 | ||
e1ff0a32 NTND |
168 | int repo_read_index_preload(struct repository *repo, |
169 | const struct pathspec *pathspec, | |
170 | unsigned int refresh_flags) | |
671c9b7e | 171 | { |
e1ff0a32 | 172 | int retval = repo_read_index(repo); |
671c9b7e | 173 | |
e1ff0a32 | 174 | preload_index(repo->index, pathspec, refresh_flags); |
671c9b7e LT |
175 | return retval; |
176 | } |