2 * Copyright (C) 2008 Linus Torvalds
7 #include "environment.h"
12 #include "thread-utils.h"
13 #include "repository.h"
17 * Mostly randomly chosen maximum thread counts: we
18 * cap the parallelism to 20 threads, and we want
19 * to have at least 500 lstat's per thread for it to
20 * be worth starting a thread.
22 #define MAX_PARALLEL (20)
23 #define THREAD_COST (500)
25 struct progress_data
{
27 struct progress
*progress
;
28 pthread_mutex_t mutex
;
33 struct index_state
*index
;
34 struct pathspec pathspec
;
35 struct progress_data
*progress
;
40 static void *preload_thread(void *_data
)
43 struct thread_data
*p
= _data
;
44 struct index_state
*index
= p
->index
;
45 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
46 struct cache_def cache
= CACHE_DEF_INIT
;
49 if (nr
+ p
->offset
> index
->cache_nr
)
50 nr
= index
->cache_nr
- p
->offset
;
54 struct cache_entry
*ce
= *cep
++;
59 if (S_ISGITLINK(ce
->ce_mode
))
63 if (ce_skip_worktree(ce
))
65 if (ce
->ce_flags
& CE_FSMONITOR_VALID
)
67 if (p
->progress
&& !(nr
& 31)) {
68 struct progress_data
*pd
= p
->progress
;
70 pthread_mutex_lock(&pd
->mutex
);
71 pd
->n
+= last_nr
- nr
;
72 display_progress(pd
->progress
, pd
->n
);
73 pthread_mutex_unlock(&pd
->mutex
);
76 if (!ce_path_match(index
, ce
, &p
->pathspec
, NULL
))
78 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
81 if (lstat(ce
->name
, &st
))
83 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
|CE_MATCH_IGNORE_FSMONITOR
))
86 mark_fsmonitor_valid(index
, ce
);
89 struct progress_data
*pd
= p
->progress
;
91 pthread_mutex_lock(&pd
->mutex
);
92 display_progress(pd
->progress
, pd
->n
+ last_nr
);
93 pthread_mutex_unlock(&pd
->mutex
);
95 cache_def_clear(&cache
);
99 void preload_index(struct index_state
*index
,
100 const struct pathspec
*pathspec
,
101 unsigned int refresh_flags
)
103 int threads
, i
, work
, offset
;
104 struct thread_data data
[MAX_PARALLEL
];
105 struct progress_data pd
;
106 int t2_sum_lstat
= 0;
108 if (!HAVE_THREADS
|| !core_preload_index
)
111 threads
= index
->cache_nr
/ THREAD_COST
;
112 if ((index
->cache_nr
> 1) && (threads
< 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
117 trace2_region_enter("index", "preload", NULL
);
119 trace_performance_enter();
120 if (threads
> MAX_PARALLEL
)
121 threads
= MAX_PARALLEL
;
123 work
= DIV_ROUND_UP(index
->cache_nr
, threads
);
124 memset(&data
, 0, sizeof(data
));
126 memset(&pd
, 0, sizeof(pd
));
127 if (refresh_flags
& REFRESH_PROGRESS
&& isatty(2)) {
128 pd
.progress
= start_delayed_progress(_("Refreshing index"), index
->cache_nr
);
129 pthread_mutex_init(&pd
.mutex
, NULL
);
132 for (i
= 0; i
< threads
; i
++) {
133 struct thread_data
*p
= data
+i
;
138 copy_pathspec(&p
->pathspec
, pathspec
);
144 err
= pthread_create(&p
->pthread
, NULL
, preload_thread
, p
);
147 die(_("unable to create threaded lstat: %s"), strerror(err
));
149 for (i
= 0; i
< threads
; i
++) {
150 struct thread_data
*p
= data
+i
;
151 if (pthread_join(p
->pthread
, NULL
))
152 die("unable to join threaded lstat");
153 t2_sum_lstat
+= p
->t2_nr_lstat
;
155 stop_progress(&pd
.progress
);
158 /* earlier we made deep copies for each thread to work with */
159 for (i
= 0; i
< threads
; i
++)
160 clear_pathspec(&data
[i
].pathspec
);
163 trace_performance_leave("preload index");
165 trace2_data_intmax("index", NULL
, "preload/sum_lstat", t2_sum_lstat
);
166 trace2_region_leave("index", "preload", NULL
);
169 int repo_read_index_preload(struct repository
*repo
,
170 const struct pathspec
*pathspec
,
171 unsigned int refresh_flags
)
173 int retval
= repo_read_index(repo
);
175 preload_index(repo
->index
, pathspec
, refresh_flags
);