t4053: avoid writing to unopened pipe
[git.git] / preload-index.c
blob7a26b08c2133d428c59ff46485c0211afe52a06d
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 "symlinks.h"
15 #include "trace2.h"
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.
23 #define MAX_PARALLEL (20)
24 #define THREAD_COST (500)
26 struct progress_data {
27 unsigned long n;
28 struct progress *progress;
29 pthread_mutex_t mutex;
32 struct thread_data {
33 pthread_t pthread;
34 struct index_state *index;
35 struct pathspec pathspec;
36 struct progress_data *progress;
37 int offset, nr;
38 int t2_nr_lstat;
41 static void *preload_thread(void *_data)
43 int nr, last_nr;
44 struct thread_data *p = _data;
45 struct index_state *index = p->index;
46 struct cache_entry **cep = index->cache + p->offset;
47 struct cache_def cache = CACHE_DEF_INIT;
49 nr = p->nr;
50 if (nr + p->offset > index->cache_nr)
51 nr = index->cache_nr - p->offset;
52 last_nr = nr;
54 do {
55 struct cache_entry *ce = *cep++;
56 struct stat st;
58 if (ce_stage(ce))
59 continue;
60 if (S_ISGITLINK(ce->ce_mode))
61 continue;
62 if (ce_uptodate(ce))
63 continue;
64 if (ce_skip_worktree(ce))
65 continue;
66 if (ce->ce_flags & CE_FSMONITOR_VALID)
67 continue;
68 if (p->progress && !(nr & 31)) {
69 struct progress_data *pd = p->progress;
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;
77 if (!ce_path_match(index, ce, &p->pathspec, NULL))
78 continue;
79 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
80 continue;
81 p->t2_nr_lstat++;
82 if (lstat(ce->name, &st))
83 continue;
84 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
85 continue;
86 ce_mark_uptodate(ce);
87 mark_fsmonitor_valid(index, ce);
88 } while (--nr > 0);
89 if (p->progress) {
90 struct progress_data *pd = p->progress;
92 pthread_mutex_lock(&pd->mutex);
93 display_progress(pd->progress, pd->n + last_nr);
94 pthread_mutex_unlock(&pd->mutex);
96 cache_def_clear(&cache);
97 return NULL;
100 void preload_index(struct index_state *index,
101 const struct pathspec *pathspec,
102 unsigned int refresh_flags)
104 int threads, i, work, offset;
105 struct thread_data data[MAX_PARALLEL];
106 struct progress_data pd;
107 int t2_sum_lstat = 0;
109 if (!HAVE_THREADS || !core_preload_index)
110 return;
112 threads = index->cache_nr / THREAD_COST;
113 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
114 threads = 2;
115 if (threads < 2)
116 return;
118 trace2_region_enter("index", "preload", NULL);
120 trace_performance_enter();
121 if (threads > MAX_PARALLEL)
122 threads = MAX_PARALLEL;
123 offset = 0;
124 work = DIV_ROUND_UP(index->cache_nr, threads);
125 memset(&data, 0, sizeof(data));
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);
133 for (i = 0; i < threads; i++) {
134 struct thread_data *p = data+i;
135 int err;
137 p->index = index;
138 if (pathspec)
139 copy_pathspec(&p->pathspec, pathspec);
140 p->offset = offset;
141 p->nr = work;
142 if (pd.progress)
143 p->progress = &pd;
144 offset += work;
145 err = pthread_create(&p->pthread, NULL, preload_thread, p);
147 if (err)
148 die(_("unable to create threaded lstat: %s"), strerror(err));
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");
154 t2_sum_lstat += p->t2_nr_lstat;
156 stop_progress(&pd.progress);
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);
164 trace_performance_leave("preload index");
166 trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
167 trace2_region_leave("index", "preload", NULL);
170 int repo_read_index_preload(struct repository *repo,
171 const struct pathspec *pathspec,
172 unsigned int refresh_flags)
174 int retval = repo_read_index(repo);
176 preload_index(repo->index, pathspec, refresh_flags);
177 return retval;