Merge branch 'ba/object-info' into maint
[git.git] / preload-index.c
blobe5529a586366d4031a5300d7ab0dfa6ec97489b8
1 /*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4 #include "cache.h"
5 #include "pathspec.h"
6 #include "dir.h"
7 #include "fsmonitor.h"
8 #include "config.h"
9 #include "progress.h"
10 #include "thread-utils.h"
11 #include "repository.h"
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.
19 #define MAX_PARALLEL (20)
20 #define THREAD_COST (500)
22 struct progress_data {
23 unsigned long n;
24 struct progress *progress;
25 pthread_mutex_t mutex;
28 struct thread_data {
29 pthread_t pthread;
30 struct index_state *index;
31 struct pathspec pathspec;
32 struct progress_data *progress;
33 int offset, nr;
34 int t2_nr_lstat;
37 static void *preload_thread(void *_data)
39 int nr, last_nr;
40 struct thread_data *p = _data;
41 struct index_state *index = p->index;
42 struct cache_entry **cep = index->cache + p->offset;
43 struct cache_def cache = CACHE_DEF_INIT;
45 nr = p->nr;
46 if (nr + p->offset > index->cache_nr)
47 nr = index->cache_nr - p->offset;
48 last_nr = nr;
50 do {
51 struct cache_entry *ce = *cep++;
52 struct stat st;
54 if (ce_stage(ce))
55 continue;
56 if (S_ISGITLINK(ce->ce_mode))
57 continue;
58 if (ce_uptodate(ce))
59 continue;
60 if (ce_skip_worktree(ce))
61 continue;
62 if (ce->ce_flags & CE_FSMONITOR_VALID)
63 continue;
64 if (p->progress && !(nr & 31)) {
65 struct progress_data *pd = p->progress;
67 pthread_mutex_lock(&pd->mutex);
68 pd->n += last_nr - nr;
69 display_progress(pd->progress, pd->n);
70 pthread_mutex_unlock(&pd->mutex);
71 last_nr = nr;
73 if (!ce_path_match(index, ce, &p->pathspec, NULL))
74 continue;
75 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
76 continue;
77 p->t2_nr_lstat++;
78 if (lstat(ce->name, &st))
79 continue;
80 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
81 continue;
82 ce_mark_uptodate(ce);
83 mark_fsmonitor_valid(index, ce);
84 } while (--nr > 0);
85 if (p->progress) {
86 struct progress_data *pd = p->progress;
88 pthread_mutex_lock(&pd->mutex);
89 display_progress(pd->progress, pd->n + last_nr);
90 pthread_mutex_unlock(&pd->mutex);
92 cache_def_clear(&cache);
93 return NULL;
96 void preload_index(struct index_state *index,
97 const struct pathspec *pathspec,
98 unsigned int refresh_flags)
100 int threads, i, work, offset;
101 struct thread_data data[MAX_PARALLEL];
102 struct progress_data pd;
103 int t2_sum_lstat = 0;
105 if (!HAVE_THREADS || !core_preload_index)
106 return;
108 threads = index->cache_nr / THREAD_COST;
109 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
110 threads = 2;
111 if (threads < 2)
112 return;
114 trace2_region_enter("index", "preload", NULL);
116 trace_performance_enter();
117 if (threads > MAX_PARALLEL)
118 threads = MAX_PARALLEL;
119 offset = 0;
120 work = DIV_ROUND_UP(index->cache_nr, threads);
121 memset(&data, 0, sizeof(data));
123 memset(&pd, 0, sizeof(pd));
124 if (refresh_flags & REFRESH_PROGRESS && isatty(2)) {
125 pd.progress = start_delayed_progress(_("Refreshing index"), index->cache_nr);
126 pthread_mutex_init(&pd.mutex, NULL);
129 for (i = 0; i < threads; i++) {
130 struct thread_data *p = data+i;
131 int err;
133 p->index = index;
134 if (pathspec)
135 copy_pathspec(&p->pathspec, pathspec);
136 p->offset = offset;
137 p->nr = work;
138 if (pd.progress)
139 p->progress = &pd;
140 offset += work;
141 err = pthread_create(&p->pthread, NULL, preload_thread, p);
143 if (err)
144 die(_("unable to create threaded lstat: %s"), strerror(err));
146 for (i = 0; i < threads; i++) {
147 struct thread_data *p = data+i;
148 if (pthread_join(p->pthread, NULL))
149 die("unable to join threaded lstat");
150 t2_sum_lstat += p->t2_nr_lstat;
152 stop_progress(&pd.progress);
154 trace_performance_leave("preload index");
156 trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
157 trace2_region_leave("index", "preload", NULL);
160 int repo_read_index_preload(struct repository *repo,
161 const struct pathspec *pathspec,
162 unsigned int refresh_flags)
164 int retval = repo_read_index(repo);
166 preload_index(repo->index, pathspec, refresh_flags);
167 return retval;