completion: improve docs for using __git_complete
[alt-git.git] / preload-index.c
blob63fd35d64b141f6f1488c40c4fb34b1fe1e53ae0
1 /*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4 #include "git-compat-util.h"
5 #include "pathspec.h"
6 #include "dir.h"
7 #include "environment.h"
8 #include "fsmonitor.h"
9 #include "gettext.h"
10 #include "parse.h"
11 #include "preload-index.h"
12 #include "progress.h"
13 #include "read-cache.h"
14 #include "thread-utils.h"
15 #include "repository.h"
16 #include "symlinks.h"
17 #include "trace2.h"
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.
25 #define MAX_PARALLEL (20)
26 #define THREAD_COST (500)
28 struct progress_data {
29 unsigned long n;
30 struct progress *progress;
31 pthread_mutex_t mutex;
34 struct thread_data {
35 pthread_t pthread;
36 struct index_state *index;
37 struct pathspec pathspec;
38 struct progress_data *progress;
39 int offset, nr;
40 int t2_nr_lstat;
43 static void *preload_thread(void *_data)
45 int nr, last_nr;
46 struct thread_data *p = _data;
47 struct index_state *index = p->index;
48 struct cache_entry **cep = index->cache + p->offset;
49 struct cache_def cache = CACHE_DEF_INIT;
51 nr = p->nr;
52 if (nr + p->offset > index->cache_nr)
53 nr = index->cache_nr - p->offset;
54 last_nr = nr;
56 do {
57 struct cache_entry *ce = *cep++;
58 struct stat st;
60 if (ce_stage(ce))
61 continue;
62 if (S_ISGITLINK(ce->ce_mode))
63 continue;
64 if (ce_uptodate(ce))
65 continue;
66 if (ce_skip_worktree(ce))
67 continue;
68 if (ce->ce_flags & CE_FSMONITOR_VALID)
69 continue;
70 if (p->progress && !(nr & 31)) {
71 struct progress_data *pd = p->progress;
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;
79 if (!ce_path_match(index, ce, &p->pathspec, NULL))
80 continue;
81 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
82 continue;
83 p->t2_nr_lstat++;
84 if (lstat(ce->name, &st))
85 continue;
86 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
87 continue;
88 ce_mark_uptodate(ce);
89 mark_fsmonitor_valid(index, ce);
90 } while (--nr > 0);
91 if (p->progress) {
92 struct progress_data *pd = p->progress;
94 pthread_mutex_lock(&pd->mutex);
95 display_progress(pd->progress, pd->n + last_nr);
96 pthread_mutex_unlock(&pd->mutex);
98 cache_def_clear(&cache);
99 return NULL;
102 void preload_index(struct index_state *index,
103 const struct pathspec *pathspec,
104 unsigned int refresh_flags)
106 int threads, i, work, offset;
107 struct thread_data data[MAX_PARALLEL];
108 struct progress_data pd;
109 int t2_sum_lstat = 0;
111 if (!HAVE_THREADS || !core_preload_index)
112 return;
114 threads = index->cache_nr / THREAD_COST;
115 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
116 threads = 2;
117 if (threads < 2)
118 return;
120 trace2_region_enter("index", "preload", NULL);
122 trace_performance_enter();
123 if (threads > MAX_PARALLEL)
124 threads = MAX_PARALLEL;
125 offset = 0;
126 work = DIV_ROUND_UP(index->cache_nr, threads);
127 memset(&data, 0, sizeof(data));
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);
135 for (i = 0; i < threads; i++) {
136 struct thread_data *p = data+i;
137 int err;
139 p->index = index;
140 if (pathspec)
141 copy_pathspec(&p->pathspec, pathspec);
142 p->offset = offset;
143 p->nr = work;
144 if (pd.progress)
145 p->progress = &pd;
146 offset += work;
147 err = pthread_create(&p->pthread, NULL, preload_thread, p);
149 if (err)
150 die(_("unable to create threaded lstat: %s"), strerror(err));
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");
156 t2_sum_lstat += p->t2_nr_lstat;
158 stop_progress(&pd.progress);
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);
166 trace_performance_leave("preload index");
168 trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
169 trace2_region_leave("index", "preload", NULL);
172 int repo_read_index_preload(struct repository *repo,
173 const struct pathspec *pathspec,
174 unsigned int refresh_flags)
176 int retval = repo_read_index(repo);
178 preload_index(repo->index, pathspec, refresh_flags);
179 return retval;