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