blob: 63fd35d64b141f6f1488c40c4fb34b1fe1e53ae0 [file] [log] [blame]
Linus Torvalds671c9b72008-11-13 16:36:30 -08001/*
2 * Copyright (C) 2008 Linus Torvalds
3 */
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00004#include "git-compat-util.h"
Nguyễn Thái Ngọc Duy64acde92013-07-14 15:35:25 +07005#include "pathspec.h"
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +07006#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00007#include "environment.h"
Ben Peart883e2482017-09-22 12:35:40 -04008#include "fsmonitor.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00009#include "gettext.h"
Calvin Wanb1bda752023-09-29 14:20:51 -070010#include "parse.h"
Elijah Newrenfbffdfb2023-05-16 06:33:52 +000011#include "preload-index.h"
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020012#include "progress.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000013#include "read-cache.h"
Nguyễn Thái Ngọc Duye8d40562018-11-03 09:48:45 +010014#include "thread-utils.h"
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +070015#include "repository.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +000016#include "symlinks.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000017#include "trace2.h"
Linus Torvalds671c9b72008-11-13 16:36:30 -080018
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 Duyae9af122018-09-15 19:56:04 +020028struct progress_data {
29 unsigned long n;
30 struct progress *progress;
31 pthread_mutex_t mutex;
32};
33
Linus Torvalds671c9b72008-11-13 16:36:30 -080034struct thread_data {
35 pthread_t pthread;
36 struct index_state *index;
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070037 struct pathspec pathspec;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020038 struct progress_data *progress;
Linus Torvalds671c9b72008-11-13 16:36:30 -080039 int offset, nr;
Jeff Hostetler8c4b7502021-02-03 15:34:44 +000040 int t2_nr_lstat;
Linus Torvalds671c9b72008-11-13 16:36:30 -080041};
42
43static void *preload_thread(void *_data)
44{
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020045 int nr, last_nr;
Linus Torvalds671c9b72008-11-13 16:36:30 -080046 struct thread_data *p = _data;
47 struct index_state *index = p->index;
48 struct cache_entry **cep = index->cache + p->offset;
Karsten Bleese7c73052014-07-05 00:41:46 +020049 struct cache_def cache = CACHE_DEF_INIT;
Linus Torvalds671c9b72008-11-13 16:36:30 -080050
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 Duyae9af122018-09-15 19:56:04 +020054 last_nr = nr;
Linus Torvalds671c9b72008-11-13 16:36:30 -080055
56 do {
57 struct cache_entry *ce = *cep++;
58 struct stat st;
59
60 if (ce_stage(ce))
61 continue;
Junio C Hamano125fd982010-01-24 00:10:20 -080062 if (S_ISGITLINK(ce->ce_mode))
63 continue;
Linus Torvalds671c9b72008-11-13 16:36:30 -080064 if (ce_uptodate(ce))
65 continue;
Jeff Hostetlere596acc2017-02-10 16:10:39 +010066 if (ce_skip_worktree(ce))
67 continue;
Ben Peart883e2482017-09-22 12:35:40 -040068 if (ce->ce_flags & CE_FSMONITOR_VALID)
69 continue;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020070 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 Duyf9beff02018-08-13 18:14:23 +020079 if (!ce_path_match(index, ce, &p->pathspec, NULL))
Linus Torvalds671c9b72008-11-13 16:36:30 -080080 continue;
Linus Torvaldsf62ce3d2009-07-09 13:37:02 -070081 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
82 continue;
Jeff Hostetler8c4b7502021-02-03 15:34:44 +000083 p->t2_nr_lstat++;
Linus Torvalds671c9b72008-11-13 16:36:30 -080084 if (lstat(ce->name, &st))
85 continue;
Ben Peart883e2482017-09-22 12:35:40 -040086 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
Linus Torvalds671c9b72008-11-13 16:36:30 -080087 continue;
88 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -070089 mark_fsmonitor_valid(index, ce);
Linus Torvalds671c9b72008-11-13 16:36:30 -080090 } while (--nr > 0);
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020091 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 Blees2a608392014-07-12 01:02:34 +020098 cache_def_clear(&cache);
Linus Torvalds671c9b72008-11-13 16:36:30 -080099 return NULL;
100}
101
Ben Peart99ce7202018-10-29 16:41:59 -0400102void preload_index(struct index_state *index,
103 const struct pathspec *pathspec,
104 unsigned int refresh_flags)
Linus Torvalds671c9b72008-11-13 16:36:30 -0800105{
106 int threads, i, work, offset;
107 struct thread_data data[MAX_PARALLEL];
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +0200108 struct progress_data pd;
Jeff Hostetler8c4b7502021-02-03 15:34:44 +0000109 int t2_sum_lstat = 0;
Linus Torvalds671c9b72008-11-13 16:36:30 -0800110
Nguyễn Thái Ngọc Duye8d40562018-11-03 09:48:45 +0100111 if (!HAVE_THREADS || !core_preload_index)
Linus Torvalds671c9b72008-11-13 16:36:30 -0800112 return;
113
114 threads = index->cache_nr / THREAD_COST;
Ben Peart5765d972018-09-18 23:29:37 +0000115 if ((index->cache_nr > 1) && (threads < 2) && git_env_bool("GIT_TEST_PRELOAD_INDEX", 0))
Ben Peart3e2c6692017-09-22 12:35:38 -0400116 threads = 2;
Linus Torvalds671c9b72008-11-13 16:36:30 -0800117 if (threads < 2)
118 return;
Jeff Hostetler8c4b7502021-02-03 15:34:44 +0000119
120 trace2_region_enter("index", "preload", NULL);
121
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +0200122 trace_performance_enter();
Linus Torvalds671c9b72008-11-13 16:36:30 -0800123 if (threads > MAX_PARALLEL)
124 threads = MAX_PARALLEL;
125 offset = 0;
Pierre Habouzit98cb6f32009-07-22 23:34:35 +0200126 work = DIV_ROUND_UP(index->cache_nr, threads);
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700127 memset(&data, 0, sizeof(data));
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +0200128
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 Torvalds671c9b72008-11-13 16:36:30 -0800135 for (i = 0; i < threads; i++) {
136 struct thread_data *p = data+i;
Nguyễn Thái Ngọc Duy21790452018-11-03 09:48:50 +0100137 int err;
138
Linus Torvalds671c9b72008-11-13 16:36:30 -0800139 p->index = index;
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700140 if (pathspec)
141 copy_pathspec(&p->pathspec, pathspec);
Linus Torvalds671c9b72008-11-13 16:36:30 -0800142 p->offset = offset;
143 p->nr = work;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +0200144 if (pd.progress)
145 p->progress = &pd;
Linus Torvalds671c9b72008-11-13 16:36:30 -0800146 offset += work;
Nguyễn Thái Ngọc Duy21790452018-11-03 09:48:50 +0100147 err = pthread_create(&p->pthread, NULL, preload_thread, p);
148
149 if (err)
150 die(_("unable to create threaded lstat: %s"), strerror(err));
Linus Torvalds671c9b72008-11-13 16:36:30 -0800151 }
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 Hostetler8c4b7502021-02-03 15:34:44 +0000156 t2_sum_lstat += p->t2_nr_lstat;
Linus Torvalds671c9b72008-11-13 16:36:30 -0800157 }
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +0200158 stop_progress(&pd.progress);
159
Anthony Delannoy23578902022-08-22 23:15:07 +0200160 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 Duyc46c4062018-08-18 16:41:22 +0200166 trace_performance_leave("preload index");
Jeff Hostetler8c4b7502021-02-03 15:34:44 +0000167
168 trace2_data_intmax("index", NULL, "preload/sum_lstat", t2_sum_lstat);
169 trace2_region_leave("index", "preload", NULL);
Linus Torvalds671c9b72008-11-13 16:36:30 -0800170}
Linus Torvalds671c9b72008-11-13 16:36:30 -0800171
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700172int repo_read_index_preload(struct repository *repo,
173 const struct pathspec *pathspec,
174 unsigned int refresh_flags)
Linus Torvalds671c9b72008-11-13 16:36:30 -0800175{
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700176 int retval = repo_read_index(repo);
Linus Torvalds671c9b72008-11-13 16:36:30 -0800177
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700178 preload_index(repo->index, pathspec, refresh_flags);
Linus Torvalds671c9b72008-11-13 16:36:30 -0800179 return retval;
180}