checkout: avoid the_index when possible
[git/git.git] / preload-index.c
CommitLineData
671c9b7e
LT
1/*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4#include "cache.h"
64acde94 5#include "pathspec.h"
429bb40a 6#include "dir.h"
883e248b 7#include "fsmonitor.h"
5aacc63f 8#include "config.h"
ae9af122 9#include "progress.h"
e8d40566 10#include "thread-utils.h"
671c9b7e
LT
11
12/*
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.
17 */
18#define MAX_PARALLEL (20)
19#define THREAD_COST (500)
20
ae9af122
NTND
21struct progress_data {
22 unsigned long n;
23 struct progress *progress;
24 pthread_mutex_t mutex;
25};
26
671c9b7e
LT
27struct thread_data {
28 pthread_t pthread;
29 struct index_state *index;
5ab2a2da 30 struct pathspec pathspec;
ae9af122 31 struct progress_data *progress;
671c9b7e
LT
32 int offset, nr;
33};
34
35static void *preload_thread(void *_data)
36{
ae9af122 37 int nr, last_nr;
671c9b7e
LT
38 struct thread_data *p = _data;
39 struct index_state *index = p->index;
40 struct cache_entry **cep = index->cache + p->offset;
e7c73053 41 struct cache_def cache = CACHE_DEF_INIT;
671c9b7e
LT
42
43 nr = p->nr;
44 if (nr + p->offset > index->cache_nr)
45 nr = index->cache_nr - p->offset;
ae9af122 46 last_nr = nr;
671c9b7e
LT
47
48 do {
49 struct cache_entry *ce = *cep++;
50 struct stat st;
51
52 if (ce_stage(ce))
53 continue;
125fd984
JH
54 if (S_ISGITLINK(ce->ce_mode))
55 continue;
671c9b7e
LT
56 if (ce_uptodate(ce))
57 continue;
e596acc2
JH
58 if (ce_skip_worktree(ce))
59 continue;
883e248b
BP
60 if (ce->ce_flags & CE_FSMONITOR_VALID)
61 continue;
ae9af122
NTND
62 if (p->progress && !(nr & 31)) {
63 struct progress_data *pd = p->progress;
64
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;
70 }
f9beff03 71 if (!ce_path_match(index, ce, &p->pathspec, NULL))
671c9b7e 72 continue;
f62ce3de
LT
73 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
74 continue;
671c9b7e
LT
75 if (lstat(ce->name, &st))
76 continue;
883e248b 77 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
671c9b7e
LT
78 continue;
79 ce_mark_uptodate(ce);
883e248b 80 mark_fsmonitor_valid(ce);
671c9b7e 81 } while (--nr > 0);
ae9af122
NTND
82 if (p->progress) {
83 struct progress_data *pd = p->progress;
84
85 pthread_mutex_lock(&pd->mutex);
86 display_progress(pd->progress, pd->n + last_nr);
87 pthread_mutex_unlock(&pd->mutex);
88 }
2a608391 89 cache_def_clear(&cache);
671c9b7e
LT
90 return NULL;
91}
92
99ce720c
BP
93void preload_index(struct index_state *index,
94 const struct pathspec *pathspec,
95 unsigned int refresh_flags)
671c9b7e
LT
96{
97 int threads, i, work, offset;
98 struct thread_data data[MAX_PARALLEL];
ae9af122 99 struct progress_data pd;
671c9b7e 100
e8d40566 101 if (!HAVE_THREADS || !core_preload_index)
671c9b7e
LT
102 return;
103
104 threads = index->cache_nr / THREAD_COST;
5765d97b 105 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
3e2c6696 106 threads = 2;
671c9b7e
LT
107 if (threads < 2)
108 return;
c46c406a 109 trace_performance_enter();
671c9b7e
LT
110 if (threads > MAX_PARALLEL)
111 threads = MAX_PARALLEL;
112 offset = 0;
98cb6f30 113 work = DIV_ROUND_UP(index->cache_nr, threads);
5ab2a2da 114 memset(&data, 0, sizeof(data));
ae9af122
NTND
115
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);
120 }
121
671c9b7e
LT
122 for (i = 0; i < threads; i++) {
123 struct thread_data *p = data+i;
2179045f
NTND
124 int err;
125
671c9b7e 126 p->index = index;
5ab2a2da
NTND
127 if (pathspec)
128 copy_pathspec(&p->pathspec, pathspec);
671c9b7e
LT
129 p->offset = offset;
130 p->nr = work;
ae9af122
NTND
131 if (pd.progress)
132 p->progress = &pd;
671c9b7e 133 offset += work;
2179045f
NTND
134 err = pthread_create(&p->pthread, NULL, preload_thread, p);
135
136 if (err)
137 die(_("unable to create threaded lstat: %s"), strerror(err));
671c9b7e
LT
138 }
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");
143 }
ae9af122
NTND
144 stop_progress(&pd.progress);
145
c46c406a 146 trace_performance_leave("preload index");
671c9b7e
LT
147}
148
5ab2a2da 149int read_index_preload(struct index_state *index,
ae9af122
NTND
150 const struct pathspec *pathspec,
151 unsigned int refresh_flags)
671c9b7e
LT
152{
153 int retval = read_index(index);
154
ae9af122 155 preload_index(index, pathspec, refresh_flags);
671c9b7e
LT
156 return retval;
157}