2 * Copyright (C) 2008 Linus Torvalds
7 #include "environment.h"
12 #include "thread-utils.h"
13 #include "repository.h"
16 * Mostly randomly chosen maximum thread counts: we
17 * cap the parallelism to 20 threads, and we want
18 * to have at least 500 lstat's per thread for it to
19 * be worth starting a thread.
21 #define MAX_PARALLEL (20)
22 #define THREAD_COST (500)
24 struct progress_data
{
26 struct progress
*progress
;
27 pthread_mutex_t mutex
;
32 struct index_state
*index
;
33 struct pathspec pathspec
;
34 struct progress_data
*progress
;
39 static void *preload_thread(void *_data
)
42 struct thread_data
*p
= _data
;
43 struct index_state
*index
= p
->index
;
44 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
45 struct cache_def cache
= CACHE_DEF_INIT
;
48 if (nr
+ p
->offset
> index
->cache_nr
)
49 nr
= index
->cache_nr
- p
->offset
;
53 struct cache_entry
*ce
= *cep
++;
58 if (S_ISGITLINK(ce
->ce_mode
))
62 if (ce_skip_worktree(ce
))
64 if (ce
->ce_flags
& CE_FSMONITOR_VALID
)
66 if (p
->progress
&& !(nr
& 31)) {
67 struct progress_data
*pd
= p
->progress
;
69 pthread_mutex_lock(&pd
->mutex
);
70 pd
->n
+= last_nr
- nr
;
71 display_progress(pd
->progress
, pd
->n
);
72 pthread_mutex_unlock(&pd
->mutex
);
75 if (!ce_path_match(index
, ce
, &p
->pathspec
, NULL
))
77 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
80 if (lstat(ce
->name
, &st
))
82 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
|CE_MATCH_IGNORE_FSMONITOR
))
85 mark_fsmonitor_valid(index
, ce
);
88 struct progress_data
*pd
= p
->progress
;
90 pthread_mutex_lock(&pd
->mutex
);
91 display_progress(pd
->progress
, pd
->n
+ last_nr
);
92 pthread_mutex_unlock(&pd
->mutex
);
94 cache_def_clear(&cache
);
98 void preload_index(struct index_state
*index
,
99 const struct pathspec
*pathspec
,
100 unsigned int refresh_flags
)
102 int threads
, i
, work
, offset
;
103 struct thread_data data
[MAX_PARALLEL
];
104 struct progress_data pd
;
105 int t2_sum_lstat
= 0;
107 if (!HAVE_THREADS
|| !core_preload_index
)
110 threads
= index
->cache_nr
/ THREAD_COST
;
111 if ((index
->cache_nr
> 1) && (threads
< 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
116 trace2_region_enter("index", "preload", NULL
);
118 trace_performance_enter();
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
;
137 copy_pathspec(&p
->pathspec
, pathspec
);
143 err
= pthread_create(&p
->pthread
, NULL
, preload_thread
, p
);
146 die(_("unable to create threaded lstat: %s"), strerror(err
));
148 for (i
= 0; i
< threads
; i
++) {
149 struct thread_data
*p
= data
+i
;
150 if (pthread_join(p
->pthread
, NULL
))
151 die("unable to join threaded lstat");
152 t2_sum_lstat
+= p
->t2_nr_lstat
;
154 stop_progress(&pd
.progress
);
157 /* earlier we made deep copies for each thread to work with */
158 for (i
= 0; i
< threads
; i
++)
159 clear_pathspec(&data
[i
].pathspec
);
162 trace_performance_leave("preload index");
164 trace2_data_intmax("index", NULL
, "preload/sum_lstat", t2_sum_lstat
);
165 trace2_region_leave("index", "preload", NULL
);
168 int repo_read_index_preload(struct repository
*repo
,
169 const struct pathspec
*pathspec
,
170 unsigned int refresh_flags
)
172 int retval
= repo_read_index(repo
);
174 preload_index(repo
->index
, pathspec
, refresh_flags
);