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