2 * Copyright (C) 2008 Linus Torvalds
5 #define USE_THE_REPOSITORY_VARIABLE
7 #include "git-compat-util.h"
10 #include "environment.h"
11 #include "fsmonitor.h"
14 #include "preload-index.h"
16 #include "read-cache.h"
17 #include "thread-utils.h"
18 #include "repository.h"
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
;
46 static void *preload_thread(void *_data
)
49 struct thread_data
*p
= _data
;
50 struct index_state
*index
= p
->index
;
51 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
52 struct cache_def cache
= CACHE_DEF_INIT
;
55 if (nr
+ p
->offset
> index
->cache_nr
)
56 nr
= index
->cache_nr
- p
->offset
;
60 struct cache_entry
*ce
= *cep
++;
65 if (S_ISGITLINK(ce
->ce_mode
))
69 if (ce_skip_worktree(ce
))
71 if (ce
->ce_flags
& CE_FSMONITOR_VALID
)
73 if (p
->progress
&& !(nr
& 31)) {
74 struct progress_data
*pd
= p
->progress
;
76 pthread_mutex_lock(&pd
->mutex
);
77 pd
->n
+= last_nr
- nr
;
78 display_progress(pd
->progress
, pd
->n
);
79 pthread_mutex_unlock(&pd
->mutex
);
82 if (!ce_path_match(index
, ce
, &p
->pathspec
, NULL
))
84 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
87 if (lstat(ce
->name
, &st
))
89 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
|CE_MATCH_IGNORE_FSMONITOR
))
92 mark_fsmonitor_valid(index
, ce
);
95 struct progress_data
*pd
= p
->progress
;
97 pthread_mutex_lock(&pd
->mutex
);
98 display_progress(pd
->progress
, pd
->n
+ last_nr
);
99 pthread_mutex_unlock(&pd
->mutex
);
101 cache_def_clear(&cache
);
105 void preload_index(struct index_state
*index
,
106 const struct pathspec
*pathspec
,
107 unsigned int refresh_flags
)
109 int threads
, i
, work
, offset
;
110 struct thread_data data
[MAX_PARALLEL
];
111 struct progress_data pd
;
112 int t2_sum_lstat
= 0;
114 if (!HAVE_THREADS
|| !core_preload_index
)
117 threads
= index
->cache_nr
/ THREAD_COST
;
118 if ((index
->cache_nr
> 1) && (threads
< 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
123 trace2_region_enter("index", "preload", NULL
);
125 trace_performance_enter();
126 if (threads
> MAX_PARALLEL
)
127 threads
= MAX_PARALLEL
;
129 work
= DIV_ROUND_UP(index
->cache_nr
, threads
);
130 memset(&data
, 0, sizeof(data
));
132 memset(&pd
, 0, sizeof(pd
));
133 if (refresh_flags
& REFRESH_PROGRESS
&& isatty(2)) {
134 pd
.progress
= start_delayed_progress(_("Refreshing index"), index
->cache_nr
);
135 pthread_mutex_init(&pd
.mutex
, NULL
);
138 for (i
= 0; i
< threads
; i
++) {
139 struct thread_data
*p
= data
+i
;
144 copy_pathspec(&p
->pathspec
, pathspec
);
150 err
= pthread_create(&p
->pthread
, NULL
, preload_thread
, p
);
153 die(_("unable to create threaded lstat: %s"), strerror(err
));
155 for (i
= 0; i
< threads
; i
++) {
156 struct thread_data
*p
= data
+i
;
157 if (pthread_join(p
->pthread
, NULL
))
158 die("unable to join threaded lstat");
159 t2_sum_lstat
+= p
->t2_nr_lstat
;
161 stop_progress(&pd
.progress
);
164 /* earlier we made deep copies for each thread to work with */
165 for (i
= 0; i
< threads
; i
++)
166 clear_pathspec(&data
[i
].pathspec
);
169 trace_performance_leave("preload index");
171 trace2_data_intmax("index", NULL
, "preload/sum_lstat", t2_sum_lstat
);
172 trace2_region_leave("index", "preload", NULL
);
175 int repo_read_index_preload(struct repository
*repo
,
176 const struct pathspec
*pathspec
,
177 unsigned int refresh_flags
)
179 int retval
= repo_read_index(repo
);
181 preload_index(repo
->index
, pathspec
, refresh_flags
);