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