]> git.ipfire.org Git - thirdparty/git.git/blame - preload-index.c
t5318: avoid unnecessary command substitutions
[thirdparty/git.git] / preload-index.c
CommitLineData
671c9b7e
LT
1/*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4#include "cache.h"
64acde94 5#include "pathspec.h"
429bb40a 6#include "dir.h"
883e248b 7#include "fsmonitor.h"
46059cc6
JH
8
9#ifdef NO_PTHREADS
5ab2a2da
NTND
10static void preload_index(struct index_state *index,
11 const struct pathspec *pathspec)
46059cc6
JH
12{
13 ; /* nothing */
14}
15#else
16
671c9b7e
LT
17#include <pthread.h>
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
28struct thread_data {
29 pthread_t pthread;
30 struct index_state *index;
5ab2a2da 31 struct pathspec pathspec;
671c9b7e
LT
32 int offset, nr;
33};
34
35static void *preload_thread(void *_data)
36{
37 int nr;
38 struct thread_data *p = _data;
39 struct index_state *index = p->index;
40 struct cache_entry **cep = index->cache + p->offset;
e7c73053 41 struct cache_def cache = CACHE_DEF_INIT;
671c9b7e
LT
42
43 nr = p->nr;
44 if (nr + p->offset > index->cache_nr)
45 nr = index->cache_nr - p->offset;
46
47 do {
48 struct cache_entry *ce = *cep++;
49 struct stat st;
50
51 if (ce_stage(ce))
52 continue;
125fd984
JH
53 if (S_ISGITLINK(ce->ce_mode))
54 continue;
671c9b7e
LT
55 if (ce_uptodate(ce))
56 continue;
e596acc2
JH
57 if (ce_skip_worktree(ce))
58 continue;
883e248b
BP
59 if (ce->ce_flags & CE_FSMONITOR_VALID)
60 continue;
429bb40a 61 if (!ce_path_match(ce, &p->pathspec, NULL))
671c9b7e 62 continue;
f62ce3de
LT
63 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
64 continue;
671c9b7e
LT
65 if (lstat(ce->name, &st))
66 continue;
883e248b 67 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY|CE_MATCH_IGNORE_FSMONITOR))
671c9b7e
LT
68 continue;
69 ce_mark_uptodate(ce);
883e248b 70 mark_fsmonitor_valid(ce);
671c9b7e 71 } while (--nr > 0);
2a608391 72 cache_def_clear(&cache);
671c9b7e
LT
73 return NULL;
74}
75
5ab2a2da
NTND
76static void preload_index(struct index_state *index,
77 const struct pathspec *pathspec)
671c9b7e
LT
78{
79 int threads, i, work, offset;
80 struct thread_data data[MAX_PARALLEL];
ca54d9ba 81 uint64_t start = getnanotime();
671c9b7e
LT
82
83 if (!core_preload_index)
84 return;
85
86 threads = index->cache_nr / THREAD_COST;
3e2c6696
BP
87 if ((index->cache_nr > 1) && (threads < 2) && getenv("GIT_FORCE_PRELOAD_TEST"))
88 threads = 2;
671c9b7e
LT
89 if (threads < 2)
90 return;
91 if (threads > MAX_PARALLEL)
92 threads = MAX_PARALLEL;
93 offset = 0;
98cb6f30 94 work = DIV_ROUND_UP(index->cache_nr, threads);
5ab2a2da 95 memset(&data, 0, sizeof(data));
671c9b7e
LT
96 for (i = 0; i < threads; i++) {
97 struct thread_data *p = data+i;
98 p->index = index;
5ab2a2da
NTND
99 if (pathspec)
100 copy_pathspec(&p->pathspec, pathspec);
671c9b7e
LT
101 p->offset = offset;
102 p->nr = work;
103 offset += work;
104 if (pthread_create(&p->pthread, NULL, preload_thread, p))
105 die("unable to create threaded lstat");
106 }
107 for (i = 0; i < threads; i++) {
108 struct thread_data *p = data+i;
109 if (pthread_join(p->pthread, NULL))
110 die("unable to join threaded lstat");
111 }
ca54d9ba 112 trace_performance_since(start, "preload index");
671c9b7e 113}
46059cc6 114#endif
671c9b7e 115
5ab2a2da
NTND
116int read_index_preload(struct index_state *index,
117 const struct pathspec *pathspec)
671c9b7e
LT
118{
119 int retval = read_index(index);
120
121 preload_index(index, pathspec);
122 return retval;
123}