Line | Count | Source |
1 | | /* |
2 | | * name-hash.c |
3 | | * |
4 | | * Hashing names in the index state |
5 | | * |
6 | | * Copyright (C) 2008 Linus Torvalds |
7 | | */ |
8 | | |
9 | | #define USE_THE_REPOSITORY_VARIABLE |
10 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
11 | | |
12 | | #include "git-compat-util.h" |
13 | | #include "environment.h" |
14 | | #include "gettext.h" |
15 | | #include "name-hash.h" |
16 | | #include "object.h" |
17 | | #include "read-cache-ll.h" |
18 | | #include "thread-utils.h" |
19 | | #include "trace.h" |
20 | | #include "trace2.h" |
21 | | #include "sparse-index.h" |
22 | | |
23 | | struct dir_entry { |
24 | | struct hashmap_entry ent; |
25 | | struct dir_entry *parent; |
26 | | int nr; |
27 | | unsigned int namelen; |
28 | | char name[FLEX_ARRAY]; |
29 | | }; |
30 | | |
31 | | static int dir_entry_cmp(const void *cmp_data UNUSED, |
32 | | const struct hashmap_entry *eptr, |
33 | | const struct hashmap_entry *entry_or_key, |
34 | | const void *keydata) |
35 | 0 | { |
36 | 0 | const struct dir_entry *e1, *e2; |
37 | 0 | const char *name = keydata; |
38 | |
|
39 | 0 | e1 = container_of(eptr, const struct dir_entry, ent); |
40 | 0 | e2 = container_of(entry_or_key, const struct dir_entry, ent); |
41 | |
|
42 | 0 | return e1->namelen != e2->namelen || strncasecmp(e1->name, |
43 | 0 | name ? name : e2->name, e1->namelen); |
44 | 0 | } |
45 | | |
46 | | static struct dir_entry *find_dir_entry__hash(struct index_state *istate, |
47 | | const char *name, unsigned int namelen, unsigned int hash) |
48 | 0 | { |
49 | 0 | struct dir_entry key; |
50 | 0 | hashmap_entry_init(&key.ent, hash); |
51 | 0 | key.namelen = namelen; |
52 | 0 | return hashmap_get_entry(&istate->dir_hash, &key, ent, name); |
53 | 0 | } |
54 | | |
55 | | static struct dir_entry *find_dir_entry(struct index_state *istate, |
56 | | const char *name, unsigned int namelen) |
57 | 0 | { |
58 | 0 | return find_dir_entry__hash(istate, name, namelen, memihash(name, namelen)); |
59 | 0 | } |
60 | | |
61 | | static struct dir_entry *hash_dir_entry(struct index_state *istate, |
62 | | struct cache_entry *ce, int namelen) |
63 | 0 | { |
64 | | /* |
65 | | * Throw each directory component in the hash for quick lookup |
66 | | * during a git status. Directory components are stored without their |
67 | | * closing slash. Despite submodules being a directory, they never |
68 | | * reach this point, because they are stored |
69 | | * in index_state.name_hash (as ordinary cache_entries). |
70 | | */ |
71 | 0 | struct dir_entry *dir; |
72 | | |
73 | | /* get length of parent directory */ |
74 | 0 | while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1])) |
75 | 0 | namelen--; |
76 | 0 | if (namelen <= 0) |
77 | 0 | return NULL; |
78 | 0 | namelen--; |
79 | | |
80 | | /* lookup existing entry for that directory */ |
81 | 0 | dir = find_dir_entry(istate, ce->name, namelen); |
82 | 0 | if (!dir) { |
83 | | /* not found, create it and add to hash table */ |
84 | 0 | FLEX_ALLOC_MEM(dir, name, ce->name, namelen); |
85 | 0 | hashmap_entry_init(&dir->ent, memihash(ce->name, namelen)); |
86 | 0 | dir->namelen = namelen; |
87 | 0 | hashmap_add(&istate->dir_hash, &dir->ent); |
88 | | |
89 | | /* recursively add missing parent directories */ |
90 | 0 | dir->parent = hash_dir_entry(istate, ce, namelen); |
91 | 0 | } |
92 | 0 | return dir; |
93 | 0 | } |
94 | | |
95 | | static void add_dir_entry(struct index_state *istate, struct cache_entry *ce) |
96 | 0 | { |
97 | | /* Add reference to the directory entry (and parents if 0). */ |
98 | 0 | struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce)); |
99 | 0 | while (dir && !(dir->nr++)) |
100 | 0 | dir = dir->parent; |
101 | 0 | } |
102 | | |
103 | | static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce) |
104 | 0 | { |
105 | | /* |
106 | | * Release reference to the directory entry. If 0, remove and continue |
107 | | * with parent directory. |
108 | | */ |
109 | 0 | struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce)); |
110 | 0 | while (dir && !(--dir->nr)) { |
111 | 0 | struct dir_entry *parent = dir->parent; |
112 | 0 | hashmap_remove(&istate->dir_hash, &dir->ent, NULL); |
113 | 0 | free(dir); |
114 | 0 | dir = parent; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | static void hash_index_entry(struct index_state *istate, struct cache_entry *ce) |
119 | 0 | { |
120 | 0 | if (ce->ce_flags & CE_HASHED) |
121 | 0 | return; |
122 | 0 | ce->ce_flags |= CE_HASHED; |
123 | |
|
124 | 0 | if (!S_ISSPARSEDIR(ce->ce_mode)) { |
125 | 0 | hashmap_entry_init(&ce->ent, memihash(ce->name, ce_namelen(ce))); |
126 | 0 | hashmap_add(&istate->name_hash, &ce->ent); |
127 | 0 | } |
128 | |
|
129 | 0 | if (ignore_case) |
130 | 0 | add_dir_entry(istate, ce); |
131 | 0 | } |
132 | | |
133 | | static int cache_entry_cmp(const void *cmp_data UNUSED, |
134 | | const struct hashmap_entry *eptr, |
135 | | const struct hashmap_entry *entry_or_key, |
136 | | const void *remove) |
137 | 0 | { |
138 | 0 | const struct cache_entry *ce1, *ce2; |
139 | |
|
140 | 0 | ce1 = container_of(eptr, const struct cache_entry, ent); |
141 | 0 | ce2 = container_of(entry_or_key, const struct cache_entry, ent); |
142 | | |
143 | | /* |
144 | | * For remove_name_hash, find the exact entry (pointer equality); for |
145 | | * index_file_exists, find all entries with matching hash code and |
146 | | * decide whether the entry matches in same_name. |
147 | | */ |
148 | 0 | return remove ? !(ce1 == ce2) : 0; |
149 | 0 | } |
150 | | |
151 | | static int lazy_try_threaded = 1; |
152 | | static int lazy_nr_dir_threads; |
153 | | |
154 | | /* |
155 | | * Set a minimum number of cache_entries that we will handle per |
156 | | * thread and use that to decide how many threads to run (up to |
157 | | * the number on the system). |
158 | | * |
159 | | * For guidance setting the lower per-thread bound, see: |
160 | | * t/helper/test-lazy-init-name-hash --analyze |
161 | | */ |
162 | 0 | #define LAZY_THREAD_COST (2000) |
163 | | |
164 | | /* |
165 | | * We use n mutexes to guard n partitions of the "istate->dir_hash" |
166 | | * hashtable. Since "find" and "insert" operations will hash to a |
167 | | * particular bucket and modify/search a single chain, we can say |
168 | | * that "all chains mod n" are guarded by the same mutex -- rather |
169 | | * than having a single mutex to guard the entire table. (This does |
170 | | * require that we disable "rehashing" on the hashtable.) |
171 | | * |
172 | | * So, a larger value here decreases the probability of a collision |
173 | | * and the time that each thread must wait for the mutex. |
174 | | */ |
175 | 0 | #define LAZY_MAX_MUTEX (32) |
176 | | |
177 | | static pthread_mutex_t *lazy_dir_mutex_array; |
178 | | |
179 | | /* |
180 | | * An array of lazy_entry items is used by the n threads in |
181 | | * the directory parse (first) phase to (lock-free) store the |
182 | | * intermediate results. These values are then referenced by |
183 | | * the 2 threads in the second phase. |
184 | | */ |
185 | | struct lazy_entry { |
186 | | struct dir_entry *dir; |
187 | | unsigned int hash_dir; |
188 | | unsigned int hash_name; |
189 | | }; |
190 | | |
191 | | /* |
192 | | * Decide if we want to use threads (if available) to load |
193 | | * the hash tables. We set "lazy_nr_dir_threads" to zero when |
194 | | * it is not worth it. |
195 | | */ |
196 | | static int lookup_lazy_params(struct index_state *istate) |
197 | 0 | { |
198 | 0 | int nr_cpus; |
199 | |
|
200 | 0 | lazy_nr_dir_threads = 0; |
201 | |
|
202 | 0 | if (!lazy_try_threaded) |
203 | 0 | return 0; |
204 | | |
205 | | /* |
206 | | * If we are respecting case, just use the original |
207 | | * code to build the "istate->name_hash". We don't |
208 | | * need the complexity here. |
209 | | */ |
210 | 0 | if (!ignore_case) |
211 | 0 | return 0; |
212 | | |
213 | 0 | nr_cpus = online_cpus(); |
214 | 0 | if (nr_cpus < 2) |
215 | 0 | return 0; |
216 | | |
217 | 0 | if (istate->cache_nr < 2 * LAZY_THREAD_COST) |
218 | 0 | return 0; |
219 | | |
220 | 0 | if (istate->cache_nr < nr_cpus * LAZY_THREAD_COST) |
221 | 0 | nr_cpus = istate->cache_nr / LAZY_THREAD_COST; |
222 | 0 | lazy_nr_dir_threads = nr_cpus; |
223 | 0 | return lazy_nr_dir_threads; |
224 | 0 | } |
225 | | |
226 | | /* |
227 | | * Initialize n mutexes for use when searching and inserting |
228 | | * into "istate->dir_hash". All "dir" threads are trying |
229 | | * to insert partial pathnames into the hash as they iterate |
230 | | * over their portions of the index, so lock contention is |
231 | | * high. |
232 | | * |
233 | | * However, the hashmap is going to put items into bucket |
234 | | * chains based on their hash values. Use that to create n |
235 | | * mutexes and lock on mutex[bucket(hash) % n]. This will |
236 | | * decrease the collision rate by (hopefully) a factor of n. |
237 | | */ |
238 | | static void init_dir_mutex(void) |
239 | 0 | { |
240 | 0 | int j; |
241 | |
|
242 | 0 | CALLOC_ARRAY(lazy_dir_mutex_array, LAZY_MAX_MUTEX); |
243 | |
|
244 | 0 | for (j = 0; j < LAZY_MAX_MUTEX; j++) |
245 | 0 | init_recursive_mutex(&lazy_dir_mutex_array[j]); |
246 | 0 | } |
247 | | |
248 | | static void cleanup_dir_mutex(void) |
249 | 0 | { |
250 | 0 | int j; |
251 | |
|
252 | 0 | for (j = 0; j < LAZY_MAX_MUTEX; j++) |
253 | 0 | pthread_mutex_destroy(&lazy_dir_mutex_array[j]); |
254 | |
|
255 | 0 | free(lazy_dir_mutex_array); |
256 | 0 | } |
257 | | |
258 | | static void lock_dir_mutex(int j) |
259 | 0 | { |
260 | 0 | pthread_mutex_lock(&lazy_dir_mutex_array[j]); |
261 | 0 | } |
262 | | |
263 | | static void unlock_dir_mutex(int j) |
264 | 0 | { |
265 | 0 | pthread_mutex_unlock(&lazy_dir_mutex_array[j]); |
266 | 0 | } |
267 | | |
268 | | static inline int compute_dir_lock_nr( |
269 | | const struct hashmap *map, |
270 | | unsigned int hash) |
271 | 0 | { |
272 | 0 | return hashmap_bucket(map, hash) % LAZY_MAX_MUTEX; |
273 | 0 | } |
274 | | |
275 | | static struct dir_entry *hash_dir_entry_with_parent_and_prefix( |
276 | | struct index_state *istate, |
277 | | struct dir_entry *parent, |
278 | | struct strbuf *prefix) |
279 | 0 | { |
280 | 0 | struct dir_entry *dir; |
281 | 0 | unsigned int hash; |
282 | 0 | int lock_nr; |
283 | | |
284 | | /* |
285 | | * Either we have a parent directory and path with slash(es) |
286 | | * or the directory is an immediate child of the root directory. |
287 | | */ |
288 | 0 | assert((parent != NULL) ^ (strchr(prefix->buf, '/') == NULL)); |
289 | |
|
290 | 0 | if (parent) |
291 | 0 | hash = memihash_cont(parent->ent.hash, |
292 | 0 | prefix->buf + parent->namelen, |
293 | 0 | prefix->len - parent->namelen); |
294 | 0 | else |
295 | 0 | hash = memihash(prefix->buf, prefix->len); |
296 | |
|
297 | 0 | lock_nr = compute_dir_lock_nr(&istate->dir_hash, hash); |
298 | 0 | lock_dir_mutex(lock_nr); |
299 | |
|
300 | 0 | dir = find_dir_entry__hash(istate, prefix->buf, prefix->len, hash); |
301 | 0 | if (!dir) { |
302 | 0 | FLEX_ALLOC_MEM(dir, name, prefix->buf, prefix->len); |
303 | 0 | hashmap_entry_init(&dir->ent, hash); |
304 | 0 | dir->namelen = prefix->len; |
305 | 0 | dir->parent = parent; |
306 | 0 | hashmap_add(&istate->dir_hash, &dir->ent); |
307 | |
|
308 | 0 | if (parent) { |
309 | 0 | unlock_dir_mutex(lock_nr); |
310 | | |
311 | | /* All I really need here is an InterlockedIncrement(&(parent->nr)) */ |
312 | 0 | lock_nr = compute_dir_lock_nr(&istate->dir_hash, parent->ent.hash); |
313 | 0 | lock_dir_mutex(lock_nr); |
314 | 0 | parent->nr++; |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | 0 | unlock_dir_mutex(lock_nr); |
319 | |
|
320 | 0 | return dir; |
321 | 0 | } |
322 | | |
323 | | /* |
324 | | * handle_range_1() and handle_range_dir() are derived from |
325 | | * clear_ce_flags_1() and clear_ce_flags_dir() in unpack-trees.c |
326 | | * and handle the iteration over the entire array of index entries. |
327 | | * They use recursion for adjacent entries in the same parent |
328 | | * directory. |
329 | | */ |
330 | | static int handle_range_1( |
331 | | struct index_state *istate, |
332 | | int k_start, |
333 | | int k_end, |
334 | | struct dir_entry *parent, |
335 | | struct strbuf *prefix, |
336 | | struct lazy_entry *lazy_entries); |
337 | | |
338 | | static int handle_range_dir( |
339 | | struct index_state *istate, |
340 | | int k_start, |
341 | | int k_end, |
342 | | struct dir_entry *parent, |
343 | | struct strbuf *prefix, |
344 | | struct lazy_entry *lazy_entries, |
345 | | struct dir_entry **dir_new_out) |
346 | 0 | { |
347 | 0 | int rc, k; |
348 | 0 | int input_prefix_len = prefix->len; |
349 | 0 | struct dir_entry *dir_new; |
350 | |
|
351 | 0 | dir_new = hash_dir_entry_with_parent_and_prefix(istate, parent, prefix); |
352 | |
|
353 | 0 | strbuf_addch(prefix, '/'); |
354 | | |
355 | | /* |
356 | | * Scan forward in the index array for index entries having the same |
357 | | * path prefix (that are also in this directory). |
358 | | */ |
359 | 0 | if (k_start + 1 >= k_end) |
360 | 0 | k = k_end; |
361 | 0 | else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0) |
362 | 0 | k = k_start + 1; |
363 | 0 | else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0) |
364 | 0 | k = k_end; |
365 | 0 | else { |
366 | 0 | int begin = k_start; |
367 | 0 | int end = k_end; |
368 | 0 | assert(begin >= 0); |
369 | 0 | while (begin < end) { |
370 | 0 | int mid = begin + ((end - begin) >> 1); |
371 | 0 | int cmp = strncmp(istate->cache[mid]->name, prefix->buf, prefix->len); |
372 | 0 | if (cmp == 0) /* mid has same prefix; look in second part */ |
373 | 0 | begin = mid + 1; |
374 | 0 | else if (cmp > 0) /* mid is past group; look in first part */ |
375 | 0 | end = mid; |
376 | 0 | else |
377 | 0 | die("cache entry out of order"); |
378 | 0 | } |
379 | 0 | k = begin; |
380 | 0 | } |
381 | | |
382 | | /* |
383 | | * Recurse and process what we can of this subset [k_start, k). |
384 | | */ |
385 | 0 | rc = handle_range_1(istate, k_start, k, dir_new, prefix, lazy_entries); |
386 | |
|
387 | 0 | strbuf_setlen(prefix, input_prefix_len); |
388 | |
|
389 | 0 | *dir_new_out = dir_new; |
390 | 0 | return rc; |
391 | 0 | } |
392 | | |
393 | | static int handle_range_1( |
394 | | struct index_state *istate, |
395 | | int k_start, |
396 | | int k_end, |
397 | | struct dir_entry *parent, |
398 | | struct strbuf *prefix, |
399 | | struct lazy_entry *lazy_entries) |
400 | 0 | { |
401 | 0 | int input_prefix_len = prefix->len; |
402 | 0 | int k = k_start; |
403 | |
|
404 | 0 | while (k < k_end) { |
405 | 0 | struct cache_entry *ce_k = istate->cache[k]; |
406 | 0 | const char *name, *slash; |
407 | |
|
408 | 0 | if (prefix->len && strncmp(ce_k->name, prefix->buf, prefix->len)) |
409 | 0 | break; |
410 | | |
411 | 0 | name = ce_k->name + prefix->len; |
412 | 0 | slash = strchr(name, '/'); |
413 | |
|
414 | 0 | if (slash) { |
415 | 0 | int len = slash - name; |
416 | 0 | int processed; |
417 | 0 | struct dir_entry *dir_new; |
418 | |
|
419 | 0 | strbuf_add(prefix, name, len); |
420 | 0 | processed = handle_range_dir(istate, k, k_end, parent, prefix, lazy_entries, &dir_new); |
421 | 0 | if (processed) { |
422 | 0 | k += processed; |
423 | 0 | strbuf_setlen(prefix, input_prefix_len); |
424 | 0 | continue; |
425 | 0 | } |
426 | | |
427 | 0 | strbuf_addch(prefix, '/'); |
428 | 0 | processed = handle_range_1(istate, k, k_end, dir_new, prefix, lazy_entries); |
429 | 0 | k += processed; |
430 | 0 | strbuf_setlen(prefix, input_prefix_len); |
431 | 0 | continue; |
432 | 0 | } |
433 | | |
434 | | /* |
435 | | * It is too expensive to take a lock to insert "ce_k" |
436 | | * into "istate->name_hash" and increment the ref-count |
437 | | * on the "parent" dir. So we defer actually updating |
438 | | * permanent data structures until phase 2 (where we |
439 | | * can change the locking requirements) and simply |
440 | | * accumulate our current results into the lazy_entries |
441 | | * data array). |
442 | | * |
443 | | * We do not need to lock the lazy_entries array because |
444 | | * we have exclusive access to the cells in the range |
445 | | * [k_start,k_end) that this thread was given. |
446 | | */ |
447 | 0 | lazy_entries[k].dir = parent; |
448 | 0 | if (parent) { |
449 | 0 | lazy_entries[k].hash_name = memihash_cont( |
450 | 0 | parent->ent.hash, |
451 | 0 | ce_k->name + parent->namelen, |
452 | 0 | ce_namelen(ce_k) - parent->namelen); |
453 | 0 | lazy_entries[k].hash_dir = parent->ent.hash; |
454 | 0 | } else { |
455 | 0 | lazy_entries[k].hash_name = memihash(ce_k->name, ce_namelen(ce_k)); |
456 | 0 | } |
457 | |
|
458 | 0 | k++; |
459 | 0 | } |
460 | |
|
461 | 0 | return k - k_start; |
462 | 0 | } |
463 | | |
464 | | struct lazy_dir_thread_data { |
465 | | pthread_t pthread; |
466 | | struct index_state *istate; |
467 | | struct lazy_entry *lazy_entries; |
468 | | int k_start; |
469 | | int k_end; |
470 | | }; |
471 | | |
472 | | static void *lazy_dir_thread_proc(void *_data) |
473 | 0 | { |
474 | 0 | struct lazy_dir_thread_data *d = _data; |
475 | 0 | struct strbuf prefix = STRBUF_INIT; |
476 | 0 | handle_range_1(d->istate, d->k_start, d->k_end, NULL, &prefix, d->lazy_entries); |
477 | 0 | strbuf_release(&prefix); |
478 | 0 | return NULL; |
479 | 0 | } |
480 | | |
481 | | struct lazy_name_thread_data { |
482 | | pthread_t pthread; |
483 | | struct index_state *istate; |
484 | | struct lazy_entry *lazy_entries; |
485 | | }; |
486 | | |
487 | | static void *lazy_name_thread_proc(void *_data) |
488 | 0 | { |
489 | 0 | struct lazy_name_thread_data *d = _data; |
490 | 0 | int k; |
491 | |
|
492 | 0 | for (k = 0; k < d->istate->cache_nr; k++) { |
493 | 0 | struct cache_entry *ce_k = d->istate->cache[k]; |
494 | 0 | ce_k->ce_flags |= CE_HASHED; |
495 | 0 | if (!S_ISSPARSEDIR(ce_k->ce_mode)) { |
496 | 0 | hashmap_entry_init(&ce_k->ent, d->lazy_entries[k].hash_name); |
497 | 0 | hashmap_add(&d->istate->name_hash, &ce_k->ent); |
498 | 0 | } |
499 | 0 | } |
500 | |
|
501 | 0 | return NULL; |
502 | 0 | } |
503 | | |
504 | | static inline void lazy_update_dir_ref_counts( |
505 | | struct index_state *istate, |
506 | | struct lazy_entry *lazy_entries) |
507 | 0 | { |
508 | 0 | int k; |
509 | |
|
510 | 0 | for (k = 0; k < istate->cache_nr; k++) { |
511 | 0 | if (lazy_entries[k].dir) |
512 | 0 | lazy_entries[k].dir->nr++; |
513 | 0 | } |
514 | 0 | } |
515 | | |
516 | | static void threaded_lazy_init_name_hash( |
517 | | struct index_state *istate) |
518 | 0 | { |
519 | 0 | int err; |
520 | 0 | int nr_each; |
521 | 0 | int k_start; |
522 | 0 | int t; |
523 | 0 | struct lazy_entry *lazy_entries; |
524 | 0 | struct lazy_dir_thread_data *td_dir; |
525 | 0 | struct lazy_name_thread_data *td_name; |
526 | |
|
527 | 0 | if (!HAVE_THREADS) |
528 | 0 | return; |
529 | | |
530 | 0 | k_start = 0; |
531 | 0 | nr_each = DIV_ROUND_UP(istate->cache_nr, lazy_nr_dir_threads); |
532 | |
|
533 | 0 | CALLOC_ARRAY(lazy_entries, istate->cache_nr); |
534 | 0 | CALLOC_ARRAY(td_dir, lazy_nr_dir_threads); |
535 | 0 | CALLOC_ARRAY(td_name, 1); |
536 | |
|
537 | 0 | init_dir_mutex(); |
538 | | |
539 | | /* |
540 | | * Phase 1: |
541 | | * Build "istate->dir_hash" using n "dir" threads (and a read-only index). |
542 | | */ |
543 | 0 | for (t = 0; t < lazy_nr_dir_threads; t++) { |
544 | 0 | struct lazy_dir_thread_data *td_dir_t = td_dir + t; |
545 | 0 | td_dir_t->istate = istate; |
546 | 0 | td_dir_t->lazy_entries = lazy_entries; |
547 | 0 | td_dir_t->k_start = k_start; |
548 | 0 | k_start += nr_each; |
549 | 0 | if (k_start > istate->cache_nr) |
550 | 0 | k_start = istate->cache_nr; |
551 | 0 | td_dir_t->k_end = k_start; |
552 | 0 | err = pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t); |
553 | 0 | if (err) |
554 | 0 | die(_("unable to create lazy_dir thread: %s"), strerror(err)); |
555 | 0 | } |
556 | 0 | for (t = 0; t < lazy_nr_dir_threads; t++) { |
557 | 0 | struct lazy_dir_thread_data *td_dir_t = td_dir + t; |
558 | 0 | if (pthread_join(td_dir_t->pthread, NULL)) |
559 | 0 | die("unable to join lazy_dir_thread"); |
560 | 0 | } |
561 | | |
562 | | /* |
563 | | * Phase 2: |
564 | | * Iterate over all index entries and add them to the "istate->name_hash" |
565 | | * using a single "name" background thread. |
566 | | * (Testing showed it wasn't worth running more than 1 thread for this.) |
567 | | * |
568 | | * Meanwhile, finish updating the parent directory ref-counts for each |
569 | | * index entry using the current thread. (This step is very fast and |
570 | | * doesn't need threading.) |
571 | | */ |
572 | 0 | td_name->istate = istate; |
573 | 0 | td_name->lazy_entries = lazy_entries; |
574 | 0 | err = pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name); |
575 | 0 | if (err) |
576 | 0 | die(_("unable to create lazy_name thread: %s"), strerror(err)); |
577 | | |
578 | 0 | lazy_update_dir_ref_counts(istate, lazy_entries); |
579 | |
|
580 | 0 | err = pthread_join(td_name->pthread, NULL); |
581 | 0 | if (err) |
582 | 0 | die(_("unable to join lazy_name thread: %s"), strerror(err)); |
583 | | |
584 | 0 | cleanup_dir_mutex(); |
585 | |
|
586 | 0 | free(td_name); |
587 | 0 | free(td_dir); |
588 | 0 | free(lazy_entries); |
589 | 0 | } |
590 | | |
591 | | static void lazy_init_name_hash(struct index_state *istate) |
592 | 0 | { |
593 | |
|
594 | 0 | if (istate->name_hash_initialized) |
595 | 0 | return; |
596 | 0 | trace_performance_enter(); |
597 | 0 | trace2_region_enter("index", "name-hash-init", istate->repo); |
598 | 0 | hashmap_init(&istate->name_hash, cache_entry_cmp, NULL, istate->cache_nr); |
599 | 0 | hashmap_init(&istate->dir_hash, dir_entry_cmp, NULL, istate->cache_nr); |
600 | |
|
601 | 0 | if (lookup_lazy_params(istate)) { |
602 | | /* |
603 | | * Disable item counting and automatic rehashing because |
604 | | * we do per-chain (mod n) locking rather than whole hashmap |
605 | | * locking and we need to prevent the table-size from changing |
606 | | * and bucket items from being redistributed. |
607 | | */ |
608 | 0 | hashmap_disable_item_counting(&istate->dir_hash); |
609 | 0 | threaded_lazy_init_name_hash(istate); |
610 | 0 | hashmap_enable_item_counting(&istate->dir_hash); |
611 | 0 | } else { |
612 | 0 | int nr; |
613 | 0 | for (nr = 0; nr < istate->cache_nr; nr++) |
614 | 0 | hash_index_entry(istate, istate->cache[nr]); |
615 | 0 | } |
616 | |
|
617 | 0 | istate->name_hash_initialized = 1; |
618 | 0 | trace2_region_leave("index", "name-hash-init", istate->repo); |
619 | 0 | trace_performance_leave("initialize name hash"); |
620 | 0 | } |
621 | | |
622 | | /* |
623 | | * A test routine for t/helper/ sources. |
624 | | * |
625 | | * Returns the number of threads used or 0 when |
626 | | * the non-threaded code path was used. |
627 | | * |
628 | | * Requesting threading WILL NOT override guards |
629 | | * in lookup_lazy_params(). |
630 | | */ |
631 | | int test_lazy_init_name_hash(struct index_state *istate, int try_threaded) |
632 | 0 | { |
633 | 0 | lazy_nr_dir_threads = 0; |
634 | 0 | lazy_try_threaded = try_threaded; |
635 | |
|
636 | 0 | lazy_init_name_hash(istate); |
637 | |
|
638 | 0 | return lazy_nr_dir_threads; |
639 | 0 | } |
640 | | |
641 | | void add_name_hash(struct index_state *istate, struct cache_entry *ce) |
642 | 0 | { |
643 | 0 | if (istate->name_hash_initialized) |
644 | 0 | hash_index_entry(istate, ce); |
645 | 0 | } |
646 | | |
647 | | void remove_name_hash(struct index_state *istate, struct cache_entry *ce) |
648 | 0 | { |
649 | 0 | if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED)) |
650 | 0 | return; |
651 | 0 | ce->ce_flags &= ~CE_HASHED; |
652 | 0 | hashmap_remove(&istate->name_hash, &ce->ent, ce); |
653 | |
|
654 | 0 | if (ignore_case) |
655 | 0 | remove_dir_entry(istate, ce); |
656 | 0 | } |
657 | | |
658 | | static int slow_same_name(const char *name1, int len1, const char *name2, int len2) |
659 | 0 | { |
660 | 0 | if (len1 != len2) |
661 | 0 | return 0; |
662 | | |
663 | 0 | while (len1) { |
664 | 0 | unsigned char c1 = *name1++; |
665 | 0 | unsigned char c2 = *name2++; |
666 | 0 | len1--; |
667 | 0 | if (c1 != c2) { |
668 | 0 | c1 = toupper(c1); |
669 | 0 | c2 = toupper(c2); |
670 | 0 | if (c1 != c2) |
671 | 0 | return 0; |
672 | 0 | } |
673 | 0 | } |
674 | 0 | return 1; |
675 | 0 | } |
676 | | |
677 | | static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase) |
678 | 0 | { |
679 | 0 | int len = ce_namelen(ce); |
680 | | |
681 | | /* |
682 | | * Always do exact compare, even if we want a case-ignoring comparison; |
683 | | * we do the quick exact one first, because it will be the common case. |
684 | | */ |
685 | 0 | if (len == namelen && !memcmp(name, ce->name, len)) |
686 | 0 | return 1; |
687 | | |
688 | 0 | if (!icase) |
689 | 0 | return 0; |
690 | | |
691 | 0 | return slow_same_name(name, namelen, ce->name, len); |
692 | 0 | } |
693 | | |
694 | | int index_dir_find(struct index_state *istate, const char *name, int namelen, |
695 | | struct strbuf *canonical_path) |
696 | 0 | { |
697 | 0 | struct dir_entry *dir; |
698 | |
|
699 | 0 | lazy_init_name_hash(istate); |
700 | 0 | expand_to_path(istate, name, namelen, 0); |
701 | 0 | dir = find_dir_entry(istate, name, namelen); |
702 | |
|
703 | 0 | if (canonical_path && dir && dir->nr) { |
704 | 0 | strbuf_reset(canonical_path); |
705 | 0 | strbuf_add(canonical_path, dir->name, dir->namelen); |
706 | 0 | } |
707 | |
|
708 | 0 | return dir && dir->nr; |
709 | 0 | } |
710 | | |
711 | | void adjust_dirname_case(struct index_state *istate, char *name) |
712 | 0 | { |
713 | 0 | const char *startPtr = name; |
714 | 0 | const char *ptr = startPtr; |
715 | |
|
716 | 0 | lazy_init_name_hash(istate); |
717 | 0 | expand_to_path(istate, name, strlen(name), 0); |
718 | 0 | while (*ptr) { |
719 | 0 | while (*ptr && *ptr != '/') |
720 | 0 | ptr++; |
721 | |
|
722 | 0 | if (*ptr == '/') { |
723 | 0 | struct dir_entry *dir; |
724 | |
|
725 | 0 | dir = find_dir_entry(istate, name, ptr - name); |
726 | 0 | if (dir) { |
727 | 0 | memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr); |
728 | 0 | startPtr = ptr + 1; |
729 | 0 | } |
730 | 0 | ptr++; |
731 | 0 | } |
732 | 0 | } |
733 | 0 | } |
734 | | |
735 | | struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase) |
736 | 0 | { |
737 | 0 | struct cache_entry *ce; |
738 | 0 | unsigned int hash = memihash(name, namelen); |
739 | |
|
740 | 0 | lazy_init_name_hash(istate); |
741 | 0 | expand_to_path(istate, name, namelen, icase); |
742 | |
|
743 | 0 | ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL, |
744 | 0 | struct cache_entry, ent); |
745 | 0 | hashmap_for_each_entry_from(&istate->name_hash, ce, ent) { |
746 | 0 | if (same_name(ce, name, namelen, icase)) |
747 | 0 | return ce; |
748 | 0 | } |
749 | 0 | return NULL; |
750 | 0 | } |
751 | | |
752 | | void free_name_hash(struct index_state *istate) |
753 | 0 | { |
754 | 0 | if (!istate->name_hash_initialized) |
755 | 0 | return; |
756 | 0 | istate->name_hash_initialized = 0; |
757 | |
|
758 | 0 | hashmap_clear(&istate->name_hash); |
759 | | hashmap_clear_and_free(&istate->dir_hash, struct dir_entry, ent); |
760 | 0 | } |