2 * Copyright (C) 2008 Linus Torvalds
10 #include "thread-utils.h"
13 * Mostly randomly chosen maximum thread counts: we
14 * cap the parallelism to 20 threads, and we want
15 * to have at least 500 lstat's per thread for it to
16 * be worth starting a thread.
18 #define MAX_PARALLEL (20)
19 #define THREAD_COST (500)
21 struct progress_data
{
23 struct progress
*progress
;
24 pthread_mutex_t mutex
;
29 struct index_state
*index
;
30 struct pathspec pathspec
;
31 struct progress_data
*progress
;
35 static void *preload_thread(void *_data
)
38 struct thread_data
*p
= _data
;
39 struct index_state
*index
= p
->index
;
40 struct cache_entry
**cep
= index
->cache
+ p
->offset
;
41 struct cache_def cache
= CACHE_DEF_INIT
;
44 if (nr
+ p
->offset
> index
->cache_nr
)
45 nr
= index
->cache_nr
- p
->offset
;
49 struct cache_entry
*ce
= *cep
++;
54 if (S_ISGITLINK(ce
->ce_mode
))
58 if (ce_skip_worktree(ce
))
60 if (ce
->ce_flags
& CE_FSMONITOR_VALID
)
62 if (p
->progress
&& !(nr
& 31)) {
63 struct progress_data
*pd
= p
->progress
;
65 pthread_mutex_lock(&pd
->mutex
);
66 pd
->n
+= last_nr
- nr
;
67 display_progress(pd
->progress
, pd
->n
);
68 pthread_mutex_unlock(&pd
->mutex
);
71 if (!ce_path_match(index
, ce
, &p
->pathspec
, NULL
))
73 if (threaded_has_symlink_leading_path(&cache
, ce
->name
, ce_namelen(ce
)))
75 if (lstat(ce
->name
, &st
))
77 if (ie_match_stat(index
, ce
, &st
, CE_MATCH_RACY_IS_DIRTY
|CE_MATCH_IGNORE_FSMONITOR
))
80 mark_fsmonitor_valid(ce
);
83 struct progress_data
*pd
= p
->progress
;
85 pthread_mutex_lock(&pd
->mutex
);
86 display_progress(pd
->progress
, pd
->n
+ last_nr
);
87 pthread_mutex_unlock(&pd
->mutex
);
89 cache_def_clear(&cache
);
93 void preload_index(struct index_state
*index
,
94 const struct pathspec
*pathspec
,
95 unsigned int refresh_flags
)
97 int threads
, i
, work
, offset
;
98 struct thread_data data
[MAX_PARALLEL
];
99 struct progress_data pd
;
101 if (!HAVE_THREADS
|| !core_preload_index
)
104 threads
= index
->cache_nr
/ THREAD_COST
;
105 if ((index
->cache_nr
> 1) && (threads
< 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
109 trace_performance_enter();
110 if (threads
> MAX_PARALLEL
)
111 threads
= MAX_PARALLEL
;
113 work
= DIV_ROUND_UP(index
->cache_nr
, threads
);
114 memset(&data
, 0, sizeof(data
));
116 memset(&pd
, 0, sizeof(pd
));
117 if (refresh_flags
& REFRESH_PROGRESS
&& isatty(2)) {
118 pd
.progress
= start_delayed_progress(_("Refreshing index"), index
->cache_nr
);
119 pthread_mutex_init(&pd
.mutex
, NULL
);
122 for (i
= 0; i
< threads
; i
++) {
123 struct thread_data
*p
= data
+i
;
128 copy_pathspec(&p
->pathspec
, pathspec
);
134 err
= pthread_create(&p
->pthread
, NULL
, preload_thread
, p
);
137 die(_("unable to create threaded lstat: %s"), strerror(err
));
139 for (i
= 0; i
< threads
; i
++) {
140 struct thread_data
*p
= data
+i
;
141 if (pthread_join(p
->pthread
, NULL
))
142 die("unable to join threaded lstat");
144 stop_progress(&pd
.progress
);
146 trace_performance_leave("preload index");
149 int read_index_preload(struct index_state
*index
,
150 const struct pathspec
*pathspec
,
151 unsigned int refresh_flags
)
153 int retval
= read_index(index
);
155 preload_index(index
, pathspec
, refresh_flags
);