2 * Copyright (C) 2008 Linus Torvalds
7 #include "environment.h"
11 #include "preload-index.h"
13 #include "thread-utils.h"
14 #include "repository.h"
19 * Mostly randomly chosen maximum thread counts: we
20 * cap the parallelism to 20 threads, and we want
21 * to have at least 500 lstat's per thread for it to
22 * be worth starting a thread.
24 #define MAX_PARALLEL (20)
25 #define THREAD_COST (500)
27 struct progress_data
{
29 struct progress
*progress
;
30 pthread_mutex_t mutex
;
35 struct index_state
*index
;
36 struct pathspec pathspec
;
37 struct progress_data
*progress
;
42 static void *preload_thread(void *_data
)
45 struct thread_data
*p
= _data
;
46 struct index_state
*index
= p
->index
;
47 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
48 struct cache_def cache
= CACHE_DEF_INIT
;
51 if (nr
+ p
->offset
> index
->cache_nr
)
52 nr
= index
->cache_nr
- p
->offset
;
56 struct cache_entry
*ce
= *cep
++;
61 if (S_ISGITLINK(ce
->ce_mode
))
65 if (ce_skip_worktree(ce
))
67 if (ce
->ce_flags
& CE_FSMONITOR_VALID
)
69 if (p
->progress
&& !(nr
& 31)) {
70 struct progress_data
*pd
= p
->progress
;
72 pthread_mutex_lock(&pd
->mutex
);
73 pd
->n
+= last_nr
- nr
;
74 display_progress(pd
->progress
, pd
->n
);
75 pthread_mutex_unlock(&pd
->mutex
);
78 if (!ce_path_match(index
, ce
, &p
->pathspec
, NULL
))
80 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
83 if (lstat(ce
->name
, &st
))
85 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
|CE_MATCH_IGNORE_FSMONITOR
))
88 mark_fsmonitor_valid(index
, ce
);
91 struct progress_data
*pd
= p
->progress
;
93 pthread_mutex_lock(&pd
->mutex
);
94 display_progress(pd
->progress
, pd
->n
+ last_nr
);
95 pthread_mutex_unlock(&pd
->mutex
);
97 cache_def_clear(&cache
);
101 void preload_index(struct index_state
*index
,
102 const struct pathspec
*pathspec
,
103 unsigned int refresh_flags
)
105 int threads
, i
, work
, offset
;
106 struct thread_data data
[MAX_PARALLEL
];
107 struct progress_data pd
;
108 int t2_sum_lstat
= 0;
110 if (!HAVE_THREADS
|| !core_preload_index
)
113 threads
= index
->cache_nr
/ THREAD_COST
;
114 if ((index
->cache_nr
> 1) && (threads
< 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
119 trace2_region_enter("index", "preload", NULL
);
121 trace_performance_enter();
122 if (threads
> MAX_PARALLEL
)
123 threads
= MAX_PARALLEL
;
125 work
= DIV_ROUND_UP(index
->cache_nr
, threads
);
126 memset(&data
, 0, sizeof(data
));
128 memset(&pd
, 0, sizeof(pd
));
129 if (refresh_flags
& REFRESH_PROGRESS
&& isatty(2)) {
130 pd
.progress
= start_delayed_progress(_("Refreshing index"), index
->cache_nr
);
131 pthread_mutex_init(&pd
.mutex
, NULL
);
134 for (i
= 0; i
< threads
; i
++) {
135 struct thread_data
*p
= data
+i
;
140 copy_pathspec(&p
->pathspec
, pathspec
);
146 err
= pthread_create(&p
->pthread
, NULL
, preload_thread
, p
);
149 die(_("unable to create threaded lstat: %s"), strerror(err
));
151 for (i
= 0; i
< threads
; i
++) {
152 struct thread_data
*p
= data
+i
;
153 if (pthread_join(p
->pthread
, NULL
))
154 die("unable to join threaded lstat");
155 t2_sum_lstat
+= p
->t2_nr_lstat
;
157 stop_progress(&pd
.progress
);
160 /* earlier we made deep copies for each thread to work with */
161 for (i
= 0; i
< threads
; i
++)
162 clear_pathspec(&data
[i
].pathspec
);
165 trace_performance_leave("preload index");
167 trace2_data_intmax("index", NULL
, "preload/sum_lstat", t2_sum_lstat
);
168 trace2_region_leave("index", "preload", NULL
);
171 int repo_read_index_preload(struct repository
*repo
,
172 const struct pathspec
*pathspec
,
173 unsigned int refresh_flags
)
175 int retval
= repo_read_index(repo
);
177 preload_index(repo
->index
, pathspec
, refresh_flags
);