2 * Copyright (C) 2008 Linus Torvalds
4 #include "git-compat-util.h"
7 #include "environment.h"
11 #include "preload-index.h"
13 #include "read-cache.h"
14 #include "thread-utils.h"
15 #include "repository.h"
20 * Mostly randomly chosen maximum thread counts: we
21 * cap the parallelism to 20 threads, and we want
22 * to have at least 500 lstat's per thread for it to
23 * be worth starting a thread.
25 #define MAX_PARALLEL (20)
26 #define THREAD_COST (500)
28 struct progress_data
{
30 struct progress
*progress
;
31 pthread_mutex_t mutex
;
36 struct index_state
*index
;
37 struct pathspec pathspec
;
38 struct progress_data
*progress
;
43 static void *preload_thread(void *_data
)
46 struct thread_data
*p
= _data
;
47 struct index_state
*index
= p
->index
;
48 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
49 struct cache_def cache
= CACHE_DEF_INIT
;
52 if (nr
+ p
->offset
> index
->cache_nr
)
53 nr
= index
->cache_nr
- p
->offset
;
57 struct cache_entry
*ce
= *cep
++;
62 if (S_ISGITLINK(ce
->ce_mode
))
66 if (ce_skip_worktree(ce
))
68 if (ce
->ce_flags
& CE_FSMONITOR_VALID
)
70 if (p
->progress
&& !(nr
& 31)) {
71 struct progress_data
*pd
= p
->progress
;
73 pthread_mutex_lock(&pd
->mutex
);
74 pd
->n
+= last_nr
- nr
;
75 display_progress(pd
->progress
, pd
->n
);
76 pthread_mutex_unlock(&pd
->mutex
);
79 if (!ce_path_match(index
, ce
, &p
->pathspec
, NULL
))
81 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
84 if (lstat(ce
->name
, &st
))
86 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
|CE_MATCH_IGNORE_FSMONITOR
))
89 mark_fsmonitor_valid(index
, ce
);
92 struct progress_data
*pd
= p
->progress
;
94 pthread_mutex_lock(&pd
->mutex
);
95 display_progress(pd
->progress
, pd
->n
+ last_nr
);
96 pthread_mutex_unlock(&pd
->mutex
);
98 cache_def_clear(&cache
);
102 void preload_index(struct index_state
*index
,
103 const struct pathspec
*pathspec
,
104 unsigned int refresh_flags
)
106 int threads
, i
, work
, offset
;
107 struct thread_data data
[MAX_PARALLEL
];
108 struct progress_data pd
;
109 int t2_sum_lstat
= 0;
111 if (!HAVE_THREADS
|| !core_preload_index
)
114 threads
= index
->cache_nr
/ THREAD_COST
;
115 if ((index
->cache_nr
> 1) && (threads
< 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
120 trace2_region_enter("index", "preload", NULL
);
122 trace_performance_enter();
123 if (threads
> MAX_PARALLEL
)
124 threads
= MAX_PARALLEL
;
126 work
= DIV_ROUND_UP(index
->cache_nr
, threads
);
127 memset(&data
, 0, sizeof(data
));
129 memset(&pd
, 0, sizeof(pd
));
130 if (refresh_flags
& REFRESH_PROGRESS
&& isatty(2)) {
131 pd
.progress
= start_delayed_progress(_("Refreshing index"), index
->cache_nr
);
132 pthread_mutex_init(&pd
.mutex
, NULL
);
135 for (i
= 0; i
< threads
; i
++) {
136 struct thread_data
*p
= data
+i
;
141 copy_pathspec(&p
->pathspec
, pathspec
);
147 err
= pthread_create(&p
->pthread
, NULL
, preload_thread
, p
);
150 die(_("unable to create threaded lstat: %s"), strerror(err
));
152 for (i
= 0; i
< threads
; i
++) {
153 struct thread_data
*p
= data
+i
;
154 if (pthread_join(p
->pthread
, NULL
))
155 die("unable to join threaded lstat");
156 t2_sum_lstat
+= p
->t2_nr_lstat
;
158 stop_progress(&pd
.progress
);
161 /* earlier we made deep copies for each thread to work with */
162 for (i
= 0; i
< threads
; i
++)
163 clear_pathspec(&data
[i
].pathspec
);
166 trace_performance_leave("preload index");
168 trace2_data_intmax("index", NULL
, "preload/sum_lstat", t2_sum_lstat
);
169 trace2_region_leave("index", "preload", NULL
);
172 int repo_read_index_preload(struct repository
*repo
,
173 const struct pathspec
*pathspec
,
174 unsigned int refresh_flags
)
176 int retval
= repo_read_index(repo
);
178 preload_index(repo
->index
, pathspec
, refresh_flags
);