update-index: add a new --force-write-index option
[git/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"
46059cc6
JH
7
8#ifdef NO_PTHREADS
5ab2a2da
NTND
9static void preload_index(struct index_state *index,
10 const struct pathspec *pathspec)
46059cc6
JH
11{
12 ; /* nothing */
13}
14#else
15
671c9b7e
LT
16#include <pthread.h>
17
18/*
19 * Mostly randomly chosen maximum thread counts: we
20 * cap the parallelism to 20 threads, and we want
21 * to have at least 500 lstat's per thread for it to
22 * be worth starting a thread.
23 */
24#define MAX_PARALLEL (20)
25#define THREAD_COST (500)
26
27struct thread_data {
28 pthread_t pthread;
29 struct index_state *index;
5ab2a2da 30 struct pathspec pathspec;
671c9b7e
LT
31 int offset, nr;
32};
33
34static void *preload_thread(void *_data)
35{
36 int nr;
37 struct thread_data *p = _data;
38 struct index_state *index = p->index;
39 struct cache_entry **cep = index->cache + p->offset;
e7c73053 40 struct cache_def cache = CACHE_DEF_INIT;
671c9b7e
LT
41
42 nr = p->nr;
43 if (nr + p->offset > index->cache_nr)
44 nr = index->cache_nr - p->offset;
45
46 do {
47 struct cache_entry *ce = *cep++;
48 struct stat st;
49
50 if (ce_stage(ce))
51 continue;
125fd984
JH
52 if (S_ISGITLINK(ce->ce_mode))
53 continue;
671c9b7e
LT
54 if (ce_uptodate(ce))
55 continue;
e596acc2
JH
56 if (ce_skip_worktree(ce))
57 continue;
429bb40a 58 if (!ce_path_match(ce, &p->pathspec, NULL))
671c9b7e 59 continue;
f62ce3de
LT
60 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
61 continue;
671c9b7e
LT
62 if (lstat(ce->name, &st))
63 continue;
7c4ea599 64 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
671c9b7e
LT
65 continue;
66 ce_mark_uptodate(ce);
67 } while (--nr > 0);
2a608391 68 cache_def_clear(&cache);
671c9b7e
LT
69 return NULL;
70}
71
5ab2a2da
NTND
72static void preload_index(struct index_state *index,
73 const struct pathspec *pathspec)
671c9b7e
LT
74{
75 int threads, i, work, offset;
76 struct thread_data data[MAX_PARALLEL];
77
78 if (!core_preload_index)
79 return;
80
81 threads = index->cache_nr / THREAD_COST;
3e2c6696
BP
82 if ((index->cache_nr > 1) && (threads < 2) && getenv("GIT_FORCE_PRELOAD_TEST"))
83 threads = 2;
671c9b7e
LT
84 if (threads < 2)
85 return;
86 if (threads > MAX_PARALLEL)
87 threads = MAX_PARALLEL;
88 offset = 0;
98cb6f30 89 work = DIV_ROUND_UP(index->cache_nr, threads);
5ab2a2da 90 memset(&data, 0, sizeof(data));
671c9b7e
LT
91 for (i = 0; i < threads; i++) {
92 struct thread_data *p = data+i;
93 p->index = index;
5ab2a2da
NTND
94 if (pathspec)
95 copy_pathspec(&p->pathspec, pathspec);
671c9b7e
LT
96 p->offset = offset;
97 p->nr = work;
98 offset += work;
99 if (pthread_create(&p->pthread, NULL, preload_thread, p))
100 die("unable to create threaded lstat");
101 }
102 for (i = 0; i < threads; i++) {
103 struct thread_data *p = data+i;
104 if (pthread_join(p->pthread, NULL))
105 die("unable to join threaded lstat");
106 }
107}
46059cc6 108#endif
671c9b7e 109
5ab2a2da
NTND
110int read_index_preload(struct index_state *index,
111 const struct pathspec *pathspec)
671c9b7e
LT
112{
113 int retval = read_index(index);
114
115 preload_index(index, pathspec);
116 return retval;
117}