2 * Copyright (C) 2008 Linus Torvalds
12 static void preload_index(struct index_state *index,
13 const struct pathspec *pathspec,
14 unsigned int refresh_flags)
23 * Mostly randomly chosen maximum thread counts: we
24 * cap the parallelism to 20 threads, and we want
25 * to have at least 500 lstat's per thread for it to
26 * be worth starting a thread.
28 #define MAX_PARALLEL (20)
29 #define THREAD_COST (500)
31 struct progress_data {
33 struct progress *progress;
34 pthread_mutex_t mutex;
39 struct index_state *index;
40 struct pathspec pathspec;
41 struct progress_data *progress;
45 static void *preload_thread(void *_data)
48 struct thread_data *p = _data;
49 struct index_state *index = p->index;
50 struct cache_entry **cep = index->cache + p->offset;
51 struct cache_def cache = CACHE_DEF_INIT;
54 if (nr + p->offset > index->cache_nr)
55 nr = index->cache_nr - p->offset;
59 struct cache_entry *ce = *cep++;
64 if (S_ISGITLINK(ce->ce_mode))
68 if (ce_skip_worktree(ce))
70 if (ce->ce_flags & CE_FSMONITOR_VALID)
72 if (p->progress && !(nr & 31)) {
73 struct progress_data *pd = p->progress;
75 pthread_mutex_lock(&pd->mutex);
76 pd->n += last_nr - nr;
77 display_progress(pd->progress, pd->n);
78 pthread_mutex_unlock(&pd->mutex);
81 if (!ce_path_match(index, ce, &p->pathspec, NULL))
83 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
85 if (lstat(ce->name, &st))
87 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
90 mark_fsmonitor_valid(ce);
93 struct progress_data *pd = p->progress;
95 pthread_mutex_lock(&pd->mutex);
96 display_progress(pd->progress, pd->n + last_nr);
97 pthread_mutex_unlock(&pd->mutex);
99 cache_def_clear(&cache);
103 static void preload_index(struct index_state *index,
104 const struct pathspec *pathspec,
105 unsigned int refresh_flags)
107 int threads, i, work, offset;
108 struct thread_data data[MAX_PARALLEL];
109 struct progress_data pd;
111 if (!core_preload_index)
114 threads = index->cache_nr / THREAD_COST;
115 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
119 trace_performance_enter();
120 if (threads > MAX_PARALLEL)
121 threads = MAX_PARALLEL;
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;
136 copy_pathspec(&p->pathspec, pathspec);
142 if (pthread_create(&p->pthread, NULL, preload_thread, p))
143 die("unable to create threaded lstat");
145 for (i = 0; i < threads; i++) {
146 struct thread_data *p = data+i;
147 if (pthread_join(p->pthread, NULL))
148 die("unable to join threaded lstat");
150 stop_progress(&pd.progress);
152 trace_performance_leave("preload index");
156 int read_index_preload(struct index_state *index,
157 const struct pathspec *pathspec,
158 unsigned int refresh_flags)
160 int retval = read_index(index);
162 preload_index(index, pathspec, refresh_flags);