t5583: fix shebang line
[alt-git.git] / preload-index.c
blob4abf9c983b28df9718415e675c1c1fa0946ebfa4
1 /*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4 #include "cache.h"
5 #include "pathspec.h"
6 #include "dir.h"
7 #include "environment.h"
8 #include "fsmonitor.h"
9 #include "gettext.h"
10 #include "config.h"
11 #include "progress.h"
12 #include "thread-utils.h"
13 #include "repository.h"
14 #include "trace2.h"
17 * Mostly randomly chosen maximum thread counts: we
18 * cap the parallelism to 20 threads, and we want
19 * to have at least 500 lstat's per thread for it to
20 * be worth starting a thread.
22 #define MAX_PARALLEL (20)
23 #define THREAD_COST (500)
25 struct progress_data {
26 unsigned long n;
27 struct progress *progress;
28 pthread_mutex_t mutex;
31 struct thread_data {
32 pthread_t pthread;
33 struct index_state *index;
34 struct pathspec pathspec;
35 struct progress_data *progress;
36 int offset, nr;
37 int t2_nr_lstat;
40 static void *preload_thread(void *_data)
42 int nr, last_nr;
43 struct thread_data *p = _data;
44 struct index_state *index = p->index;
45 struct cache_entry **cep = index->cache + p->offset;
46 struct cache_def cache = CACHE_DEF_INIT;
48 nr = p->nr;
49 if (nr + p->offset > index->cache_nr)
50 nr = index->cache_nr - p->offset;
51 last_nr = nr;
53 do {
54 struct cache_entry *ce = *cep++;
55 struct stat st;
57 if (ce_stage(ce))
58 continue;
59 if (S_ISGITLINK(ce->ce_mode))
60 continue;
61 if (ce_uptodate(ce))
62 continue;
63 if (ce_skip_worktree(ce))
64 continue;
65 if (ce->ce_flags & CE_FSMONITOR_VALID)
66 continue;
67 if (p->progress && !(nr & 31)) {
68 struct progress_data *pd = p->progress;
70 pthread_mutex_lock(&pd->mutex);
71 pd->n += last_nr - nr;
72 display_progress(pd->progress, pd->n);
73 pthread_mutex_unlock(&pd->mutex);
74 last_nr = nr;
76 if (!ce_path_match(index, ce, &p->pathspec, NULL))
77 continue;
78 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
79 continue;
80 p->t2_nr_lstat++;
81 if (lstat(ce->name, &st))
82 continue;
83 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
84 continue;
85 ce_mark_uptodate(ce);
86 mark_fsmonitor_valid(index, ce);
87 } while (--nr > 0);
88 if (p->progress) {
89 struct progress_data *pd = p->progress;
91 pthread_mutex_lock(&pd->mutex);
92 display_progress(pd->progress, pd->n + last_nr);
93 pthread_mutex_unlock(&pd->mutex);
95 cache_def_clear(&cache);
96 return NULL;
99 void preload_index(struct index_state *index,
100 const struct pathspec *pathspec,
101 unsigned int refresh_flags)
103 int threads, i, work, offset;
104 struct thread_data data[MAX_PARALLEL];
105 struct progress_data pd;
106 int t2_sum_lstat = 0;
108 if (!HAVE_THREADS || !core_preload_index)
109 return;
111 threads = index->cache_nr / THREAD_COST;
112 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
113 threads = 2;
114 if (threads < 2)
115 return;
117 trace2_region_enter("index", "preload", NULL);
119 trace_performance_enter();
120 if (threads > MAX_PARALLEL)
121 threads = MAX_PARALLEL;
122 offset = 0;
123 work = DIV_ROUND_UP(index->cache_nr, threads);
124 memset(&data, 0, sizeof(data));
126 memset(&pd, 0, sizeof(pd));
127 if (refresh_flags & REFRESH_PROGRESS && isatty(2)) {
128 pd.progress = start_delayed_progress(_("Refreshing index"), index->cache_nr);
129 pthread_mutex_init(&pd.mutex, NULL);
132 for (i = 0; i < threads; i++) {
133 struct thread_data *p = data+i;
134 int err;
136 p->index = index;
137 if (pathspec)
138 copy_pathspec(&p->pathspec, pathspec);
139 p->offset = offset;
140 p->nr = work;
141 if (pd.progress)
142 p->progress = &pd;
143 offset += work;
144 err = pthread_create(&p->pthread, NULL, preload_thread, p);
146 if (err)
147 die(_("unable to create threaded lstat: %s"), strerror(err));
149 for (i = 0; i < threads; i++) {
150 struct thread_data *p = data+i;
151 if (pthread_join(p->pthread, NULL))
152 die("unable to join threaded lstat");
153 t2_sum_lstat += p->t2_nr_lstat;
155 stop_progress(&pd.progress);
157 if (pathspec) {
158 /* earlier we made deep copies for each thread to work with */
159 for (i = 0; i < threads; i++)
160 clear_pathspec(&data[i].pathspec);
163 trace_performance_leave("preload index");
165 trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
166 trace2_region_leave("index", "preload", NULL);
169 int repo_read_index_preload(struct repository *repo,
170 const struct pathspec *pathspec,
171 unsigned int refresh_flags)
173 int retval = repo_read_index(repo);
175 preload_index(repo->index, pathspec, refresh_flags);
176 return retval;