Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Linus Torvalds |
| 3 | */ |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 4 | #include "git-compat-util.h" |
Nguyễn Thái Ngọc Duy | 64acde9 | 2013-07-14 15:35:25 +0700 | [diff] [blame] | 5 | #include "pathspec.h" |
Nguyễn Thái Ngọc Duy | 429bb40 | 2014-01-24 20:40:28 +0700 | [diff] [blame] | 6 | #include "dir.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 7 | #include "environment.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 8 | #include "fsmonitor.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 9 | #include "gettext.h" |
Calvin Wan | b1bda75 | 2023-09-29 14:20:51 -0700 | [diff] [blame] | 10 | #include "parse.h" |
Elijah Newren | fbffdfb | 2023-05-16 06:33:52 +0000 | [diff] [blame] | 11 | #include "preload-index.h" |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 12 | #include "progress.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 13 | #include "read-cache.h" |
Nguyễn Thái Ngọc Duy | e8d4056 | 2018-11-03 09:48:45 +0100 | [diff] [blame] | 14 | #include "thread-utils.h" |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 15 | #include "repository.h" |
Elijah Newren | cb2a513 | 2023-04-22 20:17:09 +0000 | [diff] [blame] | 16 | #include "symlinks.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 17 | #include "trace2.h" |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Mostly randomly chosen maximum thread counts: we |
| 21 | * cap the parallelism to 20 threads, and we want |
| 22 | * to have at least 500 lstat's per thread for it to |
| 23 | * be worth starting a thread. |
| 24 | */ |
| 25 | #define MAX_PARALLEL (20) |
| 26 | #define THREAD_COST (500) |
| 27 | |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 28 | struct progress_data { |
| 29 | unsigned long n; |
| 30 | struct progress *progress; |
| 31 | pthread_mutex_t mutex; |
| 32 | }; |
| 33 | |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 34 | struct thread_data { |
| 35 | pthread_t pthread; |
| 36 | struct index_state *index; |
Nguyễn Thái Ngọc Duy | 5ab2a2d | 2013-07-14 15:35:49 +0700 | [diff] [blame] | 37 | struct pathspec pathspec; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 38 | struct progress_data *progress; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 39 | int offset, nr; |
Jeff Hostetler | 8c4b750 | 2021-02-03 15:34:44 +0000 | [diff] [blame] | 40 | int t2_nr_lstat; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static void *preload_thread(void *_data) |
| 44 | { |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 45 | int nr, last_nr; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 46 | struct thread_data *p = _data; |
| 47 | struct index_state *index = p->index; |
| 48 | struct cache_entry **cep = index->cache + p->offset; |
Karsten Blees | e7c7305 | 2014-07-05 00:41:46 +0200 | [diff] [blame] | 49 | struct cache_def cache = CACHE_DEF_INIT; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 50 | |
| 51 | nr = p->nr; |
| 52 | if (nr + p->offset > index->cache_nr) |
| 53 | nr = index->cache_nr - p->offset; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 54 | last_nr = nr; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 55 | |
| 56 | do { |
| 57 | struct cache_entry *ce = *cep++; |
| 58 | struct stat st; |
| 59 | |
| 60 | if (ce_stage(ce)) |
| 61 | continue; |
Junio C Hamano | 125fd98 | 2010-01-24 00:10:20 -0800 | [diff] [blame] | 62 | if (S_ISGITLINK(ce->ce_mode)) |
| 63 | continue; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 64 | if (ce_uptodate(ce)) |
| 65 | continue; |
Jeff Hostetler | e596acc | 2017-02-10 16:10:39 +0100 | [diff] [blame] | 66 | if (ce_skip_worktree(ce)) |
| 67 | continue; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 68 | if (ce->ce_flags & CE_FSMONITOR_VALID) |
| 69 | continue; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 70 | if (p->progress && !(nr & 31)) { |
| 71 | struct progress_data *pd = p->progress; |
| 72 | |
| 73 | pthread_mutex_lock(&pd->mutex); |
| 74 | pd->n += last_nr - nr; |
| 75 | display_progress(pd->progress, pd->n); |
| 76 | pthread_mutex_unlock(&pd->mutex); |
| 77 | last_nr = nr; |
| 78 | } |
Nguyễn Thái Ngọc Duy | f9beff0 | 2018-08-13 18:14:23 +0200 | [diff] [blame] | 79 | if (!ce_path_match(index, ce, &p->pathspec, NULL)) |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 80 | continue; |
Linus Torvalds | f62ce3d | 2009-07-09 13:37:02 -0700 | [diff] [blame] | 81 | if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce))) |
| 82 | continue; |
Jeff Hostetler | 8c4b750 | 2021-02-03 15:34:44 +0000 | [diff] [blame] | 83 | p->t2_nr_lstat++; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 84 | if (lstat(ce->name, &st)) |
| 85 | continue; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 86 | if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR)) |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 87 | continue; |
| 88 | ce_mark_uptodate(ce); |
Johannes Schindelin | b5a8169 | 2019-05-24 05:23:48 -0700 | [diff] [blame] | 89 | mark_fsmonitor_valid(index, ce); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 90 | } while (--nr > 0); |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 91 | if (p->progress) { |
| 92 | struct progress_data *pd = p->progress; |
| 93 | |
| 94 | pthread_mutex_lock(&pd->mutex); |
| 95 | display_progress(pd->progress, pd->n + last_nr); |
| 96 | pthread_mutex_unlock(&pd->mutex); |
| 97 | } |
Karsten Blees | 2a60839 | 2014-07-12 01:02:34 +0200 | [diff] [blame] | 98 | cache_def_clear(&cache); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
| 101 | |
Ben Peart | 99ce720 | 2018-10-29 16:41:59 -0400 | [diff] [blame] | 102 | void preload_index(struct index_state *index, |
| 103 | const struct pathspec *pathspec, |
| 104 | unsigned int refresh_flags) |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 105 | { |
| 106 | int threads, i, work, offset; |
| 107 | struct thread_data data[MAX_PARALLEL]; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 108 | struct progress_data pd; |
Jeff Hostetler | 8c4b750 | 2021-02-03 15:34:44 +0000 | [diff] [blame] | 109 | int t2_sum_lstat = 0; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 110 | |
Nguyễn Thái Ngọc Duy | e8d4056 | 2018-11-03 09:48:45 +0100 | [diff] [blame] | 111 | if (!HAVE_THREADS || !core_preload_index) |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 112 | return; |
| 113 | |
| 114 | threads = index->cache_nr / THREAD_COST; |
Ben Peart | 5765d97 | 2018-09-18 23:29:37 +0000 | [diff] [blame] | 115 | if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0)) |
Ben Peart | 3e2c669 | 2017-09-22 12:35:38 -0400 | [diff] [blame] | 116 | threads = 2; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 117 | if (threads < 2) |
| 118 | return; |
Jeff Hostetler | 8c4b750 | 2021-02-03 15:34:44 +0000 | [diff] [blame] | 119 | |
| 120 | trace2_region_enter("index", "preload", NULL); |
| 121 | |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 122 | trace_performance_enter(); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 123 | if (threads > MAX_PARALLEL) |
| 124 | threads = MAX_PARALLEL; |
| 125 | offset = 0; |
Pierre Habouzit | 98cb6f3 | 2009-07-22 23:34:35 +0200 | [diff] [blame] | 126 | work = DIV_ROUND_UP(index->cache_nr, threads); |
Nguyễn Thái Ngọc Duy | 5ab2a2d | 2013-07-14 15:35:49 +0700 | [diff] [blame] | 127 | memset(&data, 0, sizeof(data)); |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 128 | |
| 129 | memset(&pd, 0, sizeof(pd)); |
| 130 | if (refresh_flags & REFRESH_PROGRESS && isatty(2)) { |
| 131 | pd.progress = start_delayed_progress(_("Refreshing index"), index->cache_nr); |
| 132 | pthread_mutex_init(&pd.mutex, NULL); |
| 133 | } |
| 134 | |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 135 | for (i = 0; i < threads; i++) { |
| 136 | struct thread_data *p = data+i; |
Nguyễn Thái Ngọc Duy | 2179045 | 2018-11-03 09:48:50 +0100 | [diff] [blame] | 137 | int err; |
| 138 | |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 139 | p->index = index; |
Nguyễn Thái Ngọc Duy | 5ab2a2d | 2013-07-14 15:35:49 +0700 | [diff] [blame] | 140 | if (pathspec) |
| 141 | copy_pathspec(&p->pathspec, pathspec); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 142 | p->offset = offset; |
| 143 | p->nr = work; |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 144 | if (pd.progress) |
| 145 | p->progress = &pd; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 146 | offset += work; |
Nguyễn Thái Ngọc Duy | 2179045 | 2018-11-03 09:48:50 +0100 | [diff] [blame] | 147 | err = pthread_create(&p->pthread, NULL, preload_thread, p); |
| 148 | |
| 149 | if (err) |
| 150 | die(_("unable to create threaded lstat: %s"), strerror(err)); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 151 | } |
| 152 | for (i = 0; i < threads; i++) { |
| 153 | struct thread_data *p = data+i; |
| 154 | if (pthread_join(p->pthread, NULL)) |
| 155 | die("unable to join threaded lstat"); |
Jeff Hostetler | 8c4b750 | 2021-02-03 15:34:44 +0000 | [diff] [blame] | 156 | t2_sum_lstat += p->t2_nr_lstat; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 157 | } |
Nguyễn Thái Ngọc Duy | ae9af12 | 2018-09-15 19:56:04 +0200 | [diff] [blame] | 158 | stop_progress(&pd.progress); |
| 159 | |
Anthony Delannoy | 2357890 | 2022-08-22 23:15:07 +0200 | [diff] [blame] | 160 | if (pathspec) { |
| 161 | /* earlier we made deep copies for each thread to work with */ |
| 162 | for (i = 0; i < threads; i++) |
| 163 | clear_pathspec(&data[i].pathspec); |
| 164 | } |
| 165 | |
Nguyễn Thái Ngọc Duy | c46c406 | 2018-08-18 16:41:22 +0200 | [diff] [blame] | 166 | trace_performance_leave("preload index"); |
Jeff Hostetler | 8c4b750 | 2021-02-03 15:34:44 +0000 | [diff] [blame] | 167 | |
| 168 | trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat); |
| 169 | trace2_region_leave("index", "preload", NULL); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 170 | } |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 171 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 172 | int repo_read_index_preload(struct repository *repo, |
| 173 | const struct pathspec *pathspec, |
| 174 | unsigned int refresh_flags) |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 175 | { |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 176 | int retval = repo_read_index(repo); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 177 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 178 | preload_index(repo->index, pathspec, refresh_flags); |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 179 | return retval; |
| 180 | } |