Merge branch 'cb/test-lint-cp-a'
[git.git] / preload-index.c
blobc7dc3f2b9f62a762efd7f590d6e8959fc1707479
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"
13 * Mostly randomly chosen maximum thread counts: we
14 * cap the parallelism to 20 threads, and we want
15 * to have at least 500 lstat's per thread for it to
16 * be worth starting a thread.
18 #define MAX_PARALLEL (20)
19 #define THREAD_COST (500)
21 struct progress_data {
22 unsigned long n;
23 struct progress *progress;
24 pthread_mutex_t mutex;
27 struct thread_data {
28 pthread_t pthread;
29 struct index_state *index;
30 struct pathspec pathspec;
31 struct progress_data *progress;
32 int offset, nr;
35 static void *preload_thread(void *_data)
37 int nr, last_nr;
38 struct thread_data *p = _data;
39 struct index_state *index = p->index;
40 struct cache_entry **cep = index->cache + p->offset;
41 struct cache_def cache = CACHE_DEF_INIT;
43 nr = p->nr;
44 if (nr + p->offset > index->cache_nr)
45 nr = index->cache_nr - p->offset;
46 last_nr = nr;
48 do {
49 struct cache_entry *ce = *cep++;
50 struct stat st;
52 if (ce_stage(ce))
53 continue;
54 if (S_ISGITLINK(ce->ce_mode))
55 continue;
56 if (ce_uptodate(ce))
57 continue;
58 if (ce_skip_worktree(ce))
59 continue;
60 if (ce->ce_flags & CE_FSMONITOR_VALID)
61 continue;
62 if (p->progress && !(nr & 31)) {
63 struct progress_data *pd = p->progress;
65 pthread_mutex_lock(&pd->mutex);
66 pd->n += last_nr - nr;
67 display_progress(pd->progress, pd->n);
68 pthread_mutex_unlock(&pd->mutex);
69 last_nr = nr;
71 if (!ce_path_match(index, ce, &p->pathspec, NULL))
72 continue;
73 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
74 continue;
75 if (lstat(ce->name, &st))
76 continue;
77 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
78 continue;
79 ce_mark_uptodate(ce);
80 mark_fsmonitor_valid(ce);
81 } while (--nr > 0);
82 if (p->progress) {
83 struct progress_data *pd = p->progress;
85 pthread_mutex_lock(&pd->mutex);
86 display_progress(pd->progress, pd->n + last_nr);
87 pthread_mutex_unlock(&pd->mutex);
89 cache_def_clear(&cache);
90 return NULL;
93 void preload_index(struct index_state *index,
94 const struct pathspec *pathspec,
95 unsigned int refresh_flags)
97 int threads, i, work, offset;
98 struct thread_data data[MAX_PARALLEL];
99 struct progress_data pd;
101 if (!HAVE_THREADS || !core_preload_index)
102 return;
104 threads = index->cache_nr / THREAD_COST;
105 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
106 threads = 2;
107 if (threads < 2)
108 return;
109 trace_performance_enter();
110 if (threads > MAX_PARALLEL)
111 threads = MAX_PARALLEL;
112 offset = 0;
113 work = DIV_ROUND_UP(index->cache_nr, threads);
114 memset(&data, 0, sizeof(data));
116 memset(&pd, 0, sizeof(pd));
117 if (refresh_flags & REFRESH_PROGRESS && isatty(2)) {
118 pd.progress = start_delayed_progress(_("Refreshing index"), index->cache_nr);
119 pthread_mutex_init(&pd.mutex, NULL);
122 for (i = 0; i < threads; i++) {
123 struct thread_data *p = data+i;
124 int err;
126 p->index = index;
127 if (pathspec)
128 copy_pathspec(&p->pathspec, pathspec);
129 p->offset = offset;
130 p->nr = work;
131 if (pd.progress)
132 p->progress = &pd;
133 offset += work;
134 err = pthread_create(&p->pthread, NULL, preload_thread, p);
136 if (err)
137 die(_("unable to create threaded lstat: %s"), strerror(err));
139 for (i = 0; i < threads; i++) {
140 struct thread_data *p = data+i;
141 if (pthread_join(p->pthread, NULL))
142 die("unable to join threaded lstat");
144 stop_progress(&pd.progress);
146 trace_performance_leave("preload index");
149 int read_index_preload(struct index_state *index,
150 const struct pathspec *pathspec,
151 unsigned int refresh_flags)
153 int retval = read_index(index);
155 preload_index(index, pathspec, refresh_flags);
156 return retval;