4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
9 #include "environment.h"
11 #include "thread-utils.h"
13 #include "sparse-index.h"
16 struct hashmap_entry ent
;
17 struct dir_entry
*parent
;
20 char name
[FLEX_ARRAY
];
23 static int dir_entry_cmp(const void *cmp_data UNUSED
,
24 const struct hashmap_entry
*eptr
,
25 const struct hashmap_entry
*entry_or_key
,
28 const struct dir_entry
*e1
, *e2
;
29 const char *name
= keydata
;
31 e1
= container_of(eptr
, const struct dir_entry
, ent
);
32 e2
= container_of(entry_or_key
, const struct dir_entry
, ent
);
34 return e1
->namelen
!= e2
->namelen
|| strncasecmp(e1
->name
,
35 name
? name
: e2
->name
, e1
->namelen
);
38 static struct dir_entry
*find_dir_entry__hash(struct index_state
*istate
,
39 const char *name
, unsigned int namelen
, unsigned int hash
)
42 hashmap_entry_init(&key
.ent
, hash
);
43 key
.namelen
= namelen
;
44 return hashmap_get_entry(&istate
->dir_hash
, &key
, ent
, name
);
47 static struct dir_entry
*find_dir_entry(struct index_state
*istate
,
48 const char *name
, unsigned int namelen
)
50 return find_dir_entry__hash(istate
, name
, namelen
, memihash(name
, namelen
));
53 static struct dir_entry
*hash_dir_entry(struct index_state
*istate
,
54 struct cache_entry
*ce
, int namelen
)
57 * Throw each directory component in the hash for quick lookup
58 * during a git status. Directory components are stored without their
59 * closing slash. Despite submodules being a directory, they never
60 * reach this point, because they are stored
61 * in index_state.name_hash (as ordinary cache_entries).
63 struct dir_entry
*dir
;
65 /* get length of parent directory */
66 while (namelen
> 0 && !is_dir_sep(ce
->name
[namelen
- 1]))
72 /* lookup existing entry for that directory */
73 dir
= find_dir_entry(istate
, ce
->name
, namelen
);
75 /* not found, create it and add to hash table */
76 FLEX_ALLOC_MEM(dir
, name
, ce
->name
, namelen
);
77 hashmap_entry_init(&dir
->ent
, memihash(ce
->name
, namelen
));
78 dir
->namelen
= namelen
;
79 hashmap_add(&istate
->dir_hash
, &dir
->ent
);
81 /* recursively add missing parent directories */
82 dir
->parent
= hash_dir_entry(istate
, ce
, namelen
);
87 static void add_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
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
++))
95 static void remove_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
98 * Release reference to the directory entry. If 0, remove and continue
99 * with parent directory.
101 struct dir_entry
*dir
= hash_dir_entry(istate
, ce
, ce_namelen(ce
));
102 while (dir
&& !(--dir
->nr
)) {
103 struct dir_entry
*parent
= dir
->parent
;
104 hashmap_remove(&istate
->dir_hash
, &dir
->ent
, NULL
);
110 static void hash_index_entry(struct index_state
*istate
, struct cache_entry
*ce
)
112 if (ce
->ce_flags
& CE_HASHED
)
114 ce
->ce_flags
|= CE_HASHED
;
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
);
122 add_dir_entry(istate
, ce
);
125 static int cache_entry_cmp(const void *cmp_data UNUSED
,
126 const struct hashmap_entry
*eptr
,
127 const struct hashmap_entry
*entry_or_key
,
130 const struct cache_entry
*ce1
, *ce2
;
132 ce1
= container_of(eptr
, const struct cache_entry
, ent
);
133 ce2
= container_of(entry_or_key
, const struct cache_entry
, ent
);
136 * For remove_name_hash, find the exact entry (pointer equality); for
137 * index_file_exists, find all entries with matching hash code and
138 * decide whether the entry matches in same_name.
140 return remove
? !(ce1
== ce2
) : 0;
143 static int lazy_try_threaded
= 1;
144 static int lazy_nr_dir_threads
;
147 * Set a minimum number of cache_entries that we will handle per
148 * thread and use that to decide how many threads to run (up to
149 * the number on the system).
151 * For guidance setting the lower per-thread bound, see:
152 * t/helper/test-lazy-init-name-hash --analyze
154 #define LAZY_THREAD_COST (2000)
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.)
164 * So, a larger value here decreases the probability of a collision
165 * and the time that each thread must wait for the mutex.
167 #define LAZY_MAX_MUTEX (32)
169 static pthread_mutex_t
*lazy_dir_mutex_array
;
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.
178 struct dir_entry
*dir
;
179 unsigned int hash_dir
;
180 unsigned int hash_name
;
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.
188 static int lookup_lazy_params(struct index_state
*istate
)
192 lazy_nr_dir_threads
= 0;
194 if (!lazy_try_threaded
)
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.
205 nr_cpus
= online_cpus();
209 if (istate
->cache_nr
< 2 * LAZY_THREAD_COST
)
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
;
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
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
228 * decrease the collision rate by (hopefully) a factor of n.
230 static void init_dir_mutex(void)
234 CALLOC_ARRAY(lazy_dir_mutex_array
, LAZY_MAX_MUTEX
);
236 for (j
= 0; j
< LAZY_MAX_MUTEX
; j
++)
237 init_recursive_mutex(&lazy_dir_mutex_array
[j
]);
240 static void cleanup_dir_mutex(void)
244 for (j
= 0; j
< LAZY_MAX_MUTEX
; j
++)
245 pthread_mutex_destroy(&lazy_dir_mutex_array
[j
]);
247 free(lazy_dir_mutex_array
);
250 static void lock_dir_mutex(int j
)
252 pthread_mutex_lock(&lazy_dir_mutex_array
[j
]);
255 static void unlock_dir_mutex(int j
)
257 pthread_mutex_unlock(&lazy_dir_mutex_array
[j
]);
260 static inline int compute_dir_lock_nr(
261 const struct hashmap
*map
,
264 return hashmap_bucket(map
, hash
) % LAZY_MAX_MUTEX
;
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
)
272 struct dir_entry
*dir
;
277 * Either we have a parent directory and path with slash(es)
278 * or the directory is an immediate child of the root directory.
280 assert((parent
!= NULL
) ^ (strchr(prefix
->buf
, '/') == NULL
));
283 hash
= memihash_cont(parent
->ent
.hash
,
284 prefix
->buf
+ parent
->namelen
,
285 prefix
->len
- parent
->namelen
);
287 hash
= memihash(prefix
->buf
, prefix
->len
);
289 lock_nr
= compute_dir_lock_nr(&istate
->dir_hash
, hash
);
290 lock_dir_mutex(lock_nr
);
292 dir
= find_dir_entry__hash(istate
, prefix
->buf
, prefix
->len
, hash
);
294 FLEX_ALLOC_MEM(dir
, name
, prefix
->buf
, prefix
->len
);
295 hashmap_entry_init(&dir
->ent
, hash
);
296 dir
->namelen
= prefix
->len
;
297 dir
->parent
= parent
;
298 hashmap_add(&istate
->dir_hash
, &dir
->ent
);
301 unlock_dir_mutex(lock_nr
);
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
);
310 unlock_dir_mutex(lock_nr
);
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
322 static int handle_range_1(
323 struct index_state
*istate
,
326 struct dir_entry
*parent
,
327 struct strbuf
*prefix
,
328 struct lazy_entry
*lazy_entries
);
330 static int handle_range_dir(
331 struct index_state
*istate
,
334 struct dir_entry
*parent
,
335 struct strbuf
*prefix
,
336 struct lazy_entry
*lazy_entries
,
337 struct dir_entry
**dir_new_out
)
340 int input_prefix_len
= prefix
->len
;
341 struct dir_entry
*dir_new
;
343 dir_new
= hash_dir_entry_with_parent_and_prefix(istate
, parent
, prefix
);
345 strbuf_addch(prefix
, '/');
348 * Scan forward in the index array for index entries having the same
349 * path prefix (that are also in this directory).
351 if (k_start
+ 1 >= k_end
)
353 else if (strncmp(istate
->cache
[k_start
+ 1]->name
, prefix
->buf
, prefix
->len
) > 0)
355 else if (strncmp(istate
->cache
[k_end
- 1]->name
, prefix
->buf
, prefix
->len
) == 0)
361 while (begin
< end
) {
362 int mid
= begin
+ ((end
- begin
) >> 1);
363 int cmp
= strncmp(istate
->cache
[mid
]->name
, prefix
->buf
, prefix
->len
);
364 if (cmp
== 0) /* mid has same prefix; look in second part */
366 else if (cmp
> 0) /* mid is past group; look in first part */
369 die("cache entry out of order");
375 * Recurse and process what we can of this subset [k_start, k).
377 rc
= handle_range_1(istate
, k_start
, k
, dir_new
, prefix
, lazy_entries
);
379 strbuf_setlen(prefix
, input_prefix_len
);
381 *dir_new_out
= dir_new
;
385 static int handle_range_1(
386 struct index_state
*istate
,
389 struct dir_entry
*parent
,
390 struct strbuf
*prefix
,
391 struct lazy_entry
*lazy_entries
)
393 int input_prefix_len
= prefix
->len
;
397 struct cache_entry
*ce_k
= istate
->cache
[k
];
398 const char *name
, *slash
;
400 if (prefix
->len
&& strncmp(ce_k
->name
, prefix
->buf
, prefix
->len
))
403 name
= ce_k
->name
+ prefix
->len
;
404 slash
= strchr(name
, '/');
407 int len
= slash
- name
;
409 struct dir_entry
*dir_new
;
411 strbuf_add(prefix
, name
, len
);
412 processed
= handle_range_dir(istate
, k
, k_end
, parent
, prefix
, lazy_entries
, &dir_new
);
415 strbuf_setlen(prefix
, input_prefix_len
);
419 strbuf_addch(prefix
, '/');
420 processed
= handle_range_1(istate
, k
, k_end
, dir_new
, prefix
, lazy_entries
);
422 strbuf_setlen(prefix
, input_prefix_len
);
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
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.
439 lazy_entries
[k
].dir
= parent
;
441 lazy_entries
[k
].hash_name
= memihash_cont(
443 ce_k
->name
+ parent
->namelen
,
444 ce_namelen(ce_k
) - parent
->namelen
);
445 lazy_entries
[k
].hash_dir
= parent
->ent
.hash
;
447 lazy_entries
[k
].hash_name
= memihash(ce_k
->name
, ce_namelen(ce_k
));
456 struct lazy_dir_thread_data
{
458 struct index_state
*istate
;
459 struct lazy_entry
*lazy_entries
;
464 static void *lazy_dir_thread_proc(void *_data
)
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
);
473 struct lazy_name_thread_data
{
475 struct index_state
*istate
;
476 struct lazy_entry
*lazy_entries
;
479 static void *lazy_name_thread_proc(void *_data
)
481 struct lazy_name_thread_data
*d
= _data
;
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
;
487 hashmap_entry_init(&ce_k
->ent
, d
->lazy_entries
[k
].hash_name
);
488 hashmap_add(&d
->istate
->name_hash
, &ce_k
->ent
);
494 static inline void lazy_update_dir_ref_counts(
495 struct index_state
*istate
,
496 struct lazy_entry
*lazy_entries
)
500 for (k
= 0; k
< istate
->cache_nr
; k
++) {
501 if (lazy_entries
[k
].dir
)
502 lazy_entries
[k
].dir
->nr
++;
506 static void threaded_lazy_init_name_hash(
507 struct index_state
*istate
)
513 struct lazy_entry
*lazy_entries
;
514 struct lazy_dir_thread_data
*td_dir
;
515 struct lazy_name_thread_data
*td_name
;
521 nr_each
= DIV_ROUND_UP(istate
->cache_nr
, lazy_nr_dir_threads
);
523 CALLOC_ARRAY(lazy_entries
, istate
->cache_nr
);
524 CALLOC_ARRAY(td_dir
, lazy_nr_dir_threads
);
525 CALLOC_ARRAY(td_name
, 1);
531 * Build "istate->dir_hash" using n "dir" threads (and a read-only index).
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
;
539 if (k_start
> istate
->cache_nr
)
540 k_start
= istate
->cache_nr
;
541 td_dir_t
->k_end
= k_start
;
542 err
= pthread_create(&td_dir_t
->pthread
, NULL
, lazy_dir_thread_proc
, td_dir_t
);
544 die(_("unable to create lazy_dir thread: %s"), strerror(err
));
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");
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.)
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.)
562 td_name
->istate
= istate
;
563 td_name
->lazy_entries
= lazy_entries
;
564 err
= pthread_create(&td_name
->pthread
, NULL
, lazy_name_thread_proc
, td_name
);
566 die(_("unable to create lazy_name thread: %s"), strerror(err
));
568 lazy_update_dir_ref_counts(istate
, lazy_entries
);
570 err
= pthread_join(td_name
->pthread
, NULL
);
572 die(_("unable to join lazy_name thread: %s"), strerror(err
));
581 static void lazy_init_name_hash(struct index_state
*istate
)
584 if (istate
->name_hash_initialized
)
586 trace_performance_enter();
587 trace2_region_enter("index", "name-hash-init", istate
->repo
);
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
);
591 if (lookup_lazy_params(istate
)) {
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.
598 hashmap_disable_item_counting(&istate
->dir_hash
);
599 threaded_lazy_init_name_hash(istate
);
600 hashmap_enable_item_counting(&istate
->dir_hash
);
603 for (nr
= 0; nr
< istate
->cache_nr
; nr
++)
604 hash_index_entry(istate
, istate
->cache
[nr
]);
607 istate
->name_hash_initialized
= 1;
608 trace2_region_leave("index", "name-hash-init", istate
->repo
);
609 trace_performance_leave("initialize name hash");
613 * A test routine for t/helper/ sources.
615 * Returns the number of threads used or 0 when
616 * the non-threaded code path was used.
618 * Requesting threading WILL NOT override guards
619 * in lookup_lazy_params().
621 int test_lazy_init_name_hash(struct index_state
*istate
, int try_threaded
)
623 lazy_nr_dir_threads
= 0;
624 lazy_try_threaded
= try_threaded
;
626 lazy_init_name_hash(istate
);
628 return lazy_nr_dir_threads
;
631 void add_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
633 if (istate
->name_hash_initialized
)
634 hash_index_entry(istate
, ce
);
637 void remove_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
639 if (!istate
->name_hash_initialized
|| !(ce
->ce_flags
& CE_HASHED
))
641 ce
->ce_flags
&= ~CE_HASHED
;
642 hashmap_remove(&istate
->name_hash
, &ce
->ent
, ce
);
645 remove_dir_entry(istate
, ce
);
648 static int slow_same_name(const char *name1
, int len1
, const char *name2
, int len2
)
654 unsigned char c1
= *name1
++;
655 unsigned char c2
= *name2
++;
667 static int same_name(const struct cache_entry
*ce
, const char *name
, int namelen
, int icase
)
669 int len
= ce_namelen(ce
);
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.
675 if (len
== namelen
&& !memcmp(name
, ce
->name
, len
))
681 return slow_same_name(name
, namelen
, ce
->name
, len
);
684 int index_dir_exists(struct index_state
*istate
, const char *name
, int namelen
)
686 struct dir_entry
*dir
;
688 lazy_init_name_hash(istate
);
689 expand_to_path(istate
, name
, namelen
, 0);
690 dir
= find_dir_entry(istate
, name
, namelen
);
691 return dir
&& dir
->nr
;
694 void adjust_dirname_case(struct index_state
*istate
, char *name
)
696 const char *startPtr
= name
;
697 const char *ptr
= startPtr
;
699 lazy_init_name_hash(istate
);
700 expand_to_path(istate
, name
, strlen(name
), 0);
702 while (*ptr
&& *ptr
!= '/')
706 struct dir_entry
*dir
;
708 dir
= find_dir_entry(istate
, name
, ptr
- name
);
710 memcpy((void *)startPtr
, dir
->name
+ (startPtr
- name
), ptr
- startPtr
);
718 struct cache_entry
*index_file_exists(struct index_state
*istate
, const char *name
, int namelen
, int icase
)
720 struct cache_entry
*ce
;
721 unsigned int hash
= memihash(name
, namelen
);
723 lazy_init_name_hash(istate
);
724 expand_to_path(istate
, name
, namelen
, icase
);
726 ce
= hashmap_get_entry_from_hash(&istate
->name_hash
, hash
, NULL
,
727 struct cache_entry
, ent
);
728 hashmap_for_each_entry_from(&istate
->name_hash
, ce
, ent
) {
729 if (same_name(ce
, name
, namelen
, icase
))
735 void free_name_hash(struct index_state
*istate
)
737 if (!istate
->name_hash_initialized
)
739 istate
->name_hash_initialized
= 0;
741 hashmap_clear(&istate
->name_hash
);
742 hashmap_clear_and_free(&istate
->dir_hash
, struct dir_entry
, ent
);