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