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