]>
Commit | Line | Data |
---|---|---|
671c9b7e LT |
1 | /* |
2 | * Copyright (C) 2008 Linus Torvalds | |
3 | */ | |
4 | #include "cache.h" | |
64acde94 | 5 | #include "pathspec.h" |
46059cc6 JH |
6 | |
7 | #ifdef NO_PTHREADS | |
5ab2a2da NTND |
8 | static void preload_index(struct index_state *index, |
9 | const struct pathspec *pathspec) | |
46059cc6 JH |
10 | { |
11 | ; /* nothing */ | |
12 | } | |
13 | #else | |
14 | ||
671c9b7e LT |
15 | #include <pthread.h> |
16 | ||
17 | /* | |
18 | * Mostly randomly chosen maximum thread counts: we | |
19 | * cap the parallelism to 20 threads, and we want | |
20 | * to have at least 500 lstat's per thread for it to | |
21 | * be worth starting a thread. | |
22 | */ | |
23 | #define MAX_PARALLEL (20) | |
24 | #define THREAD_COST (500) | |
25 | ||
26 | struct thread_data { | |
27 | pthread_t pthread; | |
28 | struct index_state *index; | |
5ab2a2da | 29 | struct pathspec pathspec; |
671c9b7e LT |
30 | int offset, nr; |
31 | }; | |
32 | ||
33 | static void *preload_thread(void *_data) | |
34 | { | |
35 | int nr; | |
36 | struct thread_data *p = _data; | |
37 | struct index_state *index = p->index; | |
38 | struct cache_entry **cep = index->cache + p->offset; | |
f62ce3de | 39 | struct cache_def cache; |
671c9b7e | 40 | |
f62ce3de | 41 | memset(&cache, 0, sizeof(cache)); |
671c9b7e LT |
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; | |
5ab2a2da | 56 | if (!ce_path_match(ce, &p->pathspec)) |
671c9b7e | 57 | continue; |
f62ce3de LT |
58 | if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce))) |
59 | continue; | |
671c9b7e LT |
60 | if (lstat(ce->name, &st)) |
61 | continue; | |
7c4ea599 | 62 | if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY)) |
671c9b7e LT |
63 | continue; |
64 | ce_mark_uptodate(ce); | |
65 | } while (--nr > 0); | |
66 | return NULL; | |
67 | } | |
68 | ||
5ab2a2da NTND |
69 | static void preload_index(struct index_state *index, |
70 | const struct pathspec *pathspec) | |
671c9b7e LT |
71 | { |
72 | int threads, i, work, offset; | |
73 | struct thread_data data[MAX_PARALLEL]; | |
74 | ||
75 | if (!core_preload_index) | |
76 | return; | |
77 | ||
78 | threads = index->cache_nr / THREAD_COST; | |
79 | if (threads < 2) | |
80 | return; | |
81 | if (threads > MAX_PARALLEL) | |
82 | threads = MAX_PARALLEL; | |
83 | offset = 0; | |
98cb6f30 | 84 | work = DIV_ROUND_UP(index->cache_nr, threads); |
5ab2a2da | 85 | memset(&data, 0, sizeof(data)); |
671c9b7e LT |
86 | for (i = 0; i < threads; i++) { |
87 | struct thread_data *p = data+i; | |
88 | p->index = index; | |
5ab2a2da NTND |
89 | if (pathspec) |
90 | copy_pathspec(&p->pathspec, pathspec); | |
671c9b7e LT |
91 | p->offset = offset; |
92 | p->nr = work; | |
93 | offset += work; | |
94 | if (pthread_create(&p->pthread, NULL, preload_thread, p)) | |
95 | die("unable to create threaded lstat"); | |
96 | } | |
97 | for (i = 0; i < threads; i++) { | |
98 | struct thread_data *p = data+i; | |
99 | if (pthread_join(p->pthread, NULL)) | |
100 | die("unable to join threaded lstat"); | |
101 | } | |
102 | } | |
46059cc6 | 103 | #endif |
671c9b7e | 104 | |
5ab2a2da NTND |
105 | int read_index_preload(struct index_state *index, |
106 | const struct pathspec *pathspec) | |
671c9b7e LT |
107 | { |
108 | int retval = read_index(index); | |
109 | ||
110 | preload_index(index, pathspec); | |
111 | return retval; | |
112 | } |