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