2 * Copyright (C) 2008 Linus Torvalds
11 static void preload_index(struct index_state *index,
12 const struct pathspec *pathspec,
13 unsigned int refresh_flags)
22 * Mostly randomly chosen maximum thread counts: we
23 * cap the parallelism to 20 threads, and we want
24 * to have at least 500 lstat's per thread for it to
25 * be worth starting a thread.
27 #define MAX_PARALLEL (20)
28 #define THREAD_COST (500)
30 struct progress_data {
32 struct progress *progress;
33 pthread_mutex_t mutex;
38 struct index_state *index;
39 struct pathspec pathspec;
40 struct progress_data *progress;
44 static void *preload_thread(void *_data)
47 struct thread_data *p = _data;
48 struct index_state *index = p->index;
49 struct cache_entry **cep = index->cache + p->offset;
50 struct cache_def cache = CACHE_DEF_INIT;
53 if (nr + p->offset > index->cache_nr)
54 nr = index->cache_nr - p->offset;
58 struct cache_entry *ce = *cep++;
63 if (S_ISGITLINK(ce->ce_mode))
67 if (ce_skip_worktree(ce))
69 if (ce->ce_flags & CE_FSMONITOR_VALID)
71 if (p->progress && !(nr & 31)) {
72 struct progress_data *pd = p->progress;
74 pthread_mutex_lock(&pd->mutex);
75 pd->n += last_nr - nr;
76 display_progress(pd->progress, pd->n);
77 pthread_mutex_unlock(&pd->mutex);
80 if (!ce_path_match(index, ce, &p->pathspec, NULL))
82 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
84 if (lstat(ce->name, &st))
86 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
89 mark_fsmonitor_valid(ce);
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);
102 static 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 uint64_t start = getnanotime();
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) && getenv("GIT_FORCE_PRELOAD_TEST"))
119 if (threads > MAX_PARALLEL)
120 threads = MAX_PARALLEL;
122 work = DIV_ROUND_UP(index->cache_nr, threads);
123 memset(&data, 0, sizeof(data));
125 memset(&pd, 0, sizeof(pd));
126 if (refresh_flags & REFRESH_PROGRESS && isatty(2)) {
127 pd.progress = start_delayed_progress(_("Refreshing index"), index->cache_nr);
128 pthread_mutex_init(&pd.mutex, NULL);
131 for (i = 0; i < threads; i++) {
132 struct thread_data *p = data+i;
135 copy_pathspec(&p->pathspec, pathspec);
141 if (pthread_create(&p->pthread, NULL, preload_thread, p))
142 die("unable to create threaded lstat");
144 for (i = 0; i < threads; i++) {
145 struct thread_data *p = data+i;
146 if (pthread_join(p->pthread, NULL))
147 die("unable to join threaded lstat");
149 stop_progress(&pd.progress);
151 trace_performance_since(start, "preload index");
155 int read_index_preload(struct index_state *index,
156 const struct pathspec *pathspec,
157 unsigned int refresh_flags)
159 int retval = read_index(index);
161 preload_index(index, pathspec, refresh_flags);