2 * Copyright (C) 2008 Linus Torvalds
7 #include "environment.h"
12 #include "thread-utils.h"
13 #include "repository.h"
18 * Mostly randomly chosen maximum thread counts: we
19 * cap the parallelism to 20 threads, and we want
20 * to have at least 500 lstat's per thread for it to
21 * be worth starting a thread.
23 #define MAX_PARALLEL (20)
24 #define THREAD_COST (500)
26 struct progress_data
{
28 struct progress
*progress
;
29 pthread_mutex_t mutex
;
34 struct index_state
*index
;
35 struct pathspec pathspec
;
36 struct progress_data
*progress
;
41 static void *preload_thread(void *_data
)
44 struct thread_data
*p
= _data
;
45 struct index_state
*index
= p
->index
;
46 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
47 struct cache_def cache
= CACHE_DEF_INIT
;
50 if (nr
+ p
->offset
> index
->cache_nr
)
51 nr
= index
->cache_nr
- p
->offset
;
55 struct cache_entry
*ce
= *cep
++;
60 if (S_ISGITLINK(ce
->ce_mode
))
64 if (ce_skip_worktree(ce
))
66 if (ce
->ce_flags
& CE_FSMONITOR_VALID
)
68 if (p
->progress
&& !(nr
& 31)) {
69 struct progress_data
*pd
= p
->progress
;
71 pthread_mutex_lock(&pd
->mutex
);
72 pd
->n
+= last_nr
- nr
;
73 display_progress(pd
->progress
, pd
->n
);
74 pthread_mutex_unlock(&pd
->mutex
);
77 if (!ce_path_match(index
, ce
, &p
->pathspec
, NULL
))
79 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
82 if (lstat(ce
->name
, &st
))
84 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
|CE_MATCH_IGNORE_FSMONITOR
))
87 mark_fsmonitor_valid(index
, ce
);
90 struct progress_data
*pd
= p
->progress
;
92 pthread_mutex_lock(&pd
->mutex
);
93 display_progress(pd
->progress
, pd
->n
+ last_nr
);
94 pthread_mutex_unlock(&pd
->mutex
);
96 cache_def_clear(&cache
);
100 void preload_index(struct index_state
*index
,
101 const struct pathspec
*pathspec
,
102 unsigned int refresh_flags
)
104 int threads
, i
, work
, offset
;
105 struct thread_data data
[MAX_PARALLEL
];
106 struct progress_data pd
;
107 int t2_sum_lstat
= 0;
109 if (!HAVE_THREADS
|| !core_preload_index
)
112 threads
= index
->cache_nr
/ THREAD_COST
;
113 if ((index
->cache_nr
> 1) && (threads
< 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
118 trace2_region_enter("index", "preload", NULL
);
120 trace_performance_enter();
121 if (threads
> MAX_PARALLEL
)
122 threads
= MAX_PARALLEL
;
124 work
= DIV_ROUND_UP(index
->cache_nr
, threads
);
125 memset(&data
, 0, sizeof(data
));
127 memset(&pd
, 0, sizeof(pd
));
128 if (refresh_flags
& REFRESH_PROGRESS
&& isatty(2)) {
129 pd
.progress
= start_delayed_progress(_("Refreshing index"), index
->cache_nr
);
130 pthread_mutex_init(&pd
.mutex
, NULL
);
133 for (i
= 0; i
< threads
; i
++) {
134 struct thread_data
*p
= data
+i
;
139 copy_pathspec(&p
->pathspec
, pathspec
);
145 err
= pthread_create(&p
->pthread
, NULL
, preload_thread
, p
);
148 die(_("unable to create threaded lstat: %s"), strerror(err
));
150 for (i
= 0; i
< threads
; i
++) {
151 struct thread_data
*p
= data
+i
;
152 if (pthread_join(p
->pthread
, NULL
))
153 die("unable to join threaded lstat");
154 t2_sum_lstat
+= p
->t2_nr_lstat
;
156 stop_progress(&pd
.progress
);
159 /* earlier we made deep copies for each thread to work with */
160 for (i
= 0; i
< threads
; i
++)
161 clear_pathspec(&data
[i
].pathspec
);
164 trace_performance_leave("preload index");
166 trace2_data_intmax("index", NULL
, "preload/sum_lstat", t2_sum_lstat
);
167 trace2_region_leave("index", "preload", NULL
);
170 int repo_read_index_preload(struct repository
*repo
,
171 const struct pathspec
*pathspec
,
172 unsigned int refresh_flags
)
174 int retval
= repo_read_index(repo
);
176 preload_index(repo
->index
, pathspec
, refresh_flags
);