Commit | Line | Data |
---|---|---|
671c9b7e LT |
1 | /* |
2 | * Copyright (C) 2008 Linus Torvalds | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include <pthread.h> | |
6 | ||
7 | /* | |
8 | * Mostly randomly chosen maximum thread counts: we | |
9 | * cap the parallelism to 20 threads, and we want | |
10 | * to have at least 500 lstat's per thread for it to | |
11 | * be worth starting a thread. | |
12 | */ | |
13 | #define MAX_PARALLEL (20) | |
14 | #define THREAD_COST (500) | |
15 | ||
16 | struct thread_data { | |
17 | pthread_t pthread; | |
18 | struct index_state *index; | |
19 | const char **pathspec; | |
20 | int offset, nr; | |
21 | }; | |
22 | ||
23 | static void *preload_thread(void *_data) | |
24 | { | |
25 | int nr; | |
26 | struct thread_data *p = _data; | |
27 | struct index_state *index = p->index; | |
28 | struct cache_entry **cep = index->cache + p->offset; | |
29 | ||
30 | nr = p->nr; | |
31 | if (nr + p->offset > index->cache_nr) | |
32 | nr = index->cache_nr - p->offset; | |
33 | ||
34 | do { | |
35 | struct cache_entry *ce = *cep++; | |
36 | struct stat st; | |
37 | ||
38 | if (ce_stage(ce)) | |
39 | continue; | |
40 | if (ce_uptodate(ce)) | |
41 | continue; | |
42 | if (!ce_path_match(ce, p->pathspec)) | |
43 | continue; | |
44 | if (lstat(ce->name, &st)) | |
45 | continue; | |
7c4ea599 | 46 | if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY)) |
671c9b7e LT |
47 | continue; |
48 | ce_mark_uptodate(ce); | |
49 | } while (--nr > 0); | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | static void preload_index(struct index_state *index, const char **pathspec) | |
54 | { | |
55 | int threads, i, work, offset; | |
56 | struct thread_data data[MAX_PARALLEL]; | |
57 | ||
58 | if (!core_preload_index) | |
59 | return; | |
60 | ||
61 | threads = index->cache_nr / THREAD_COST; | |
62 | if (threads < 2) | |
63 | return; | |
64 | if (threads > MAX_PARALLEL) | |
65 | threads = MAX_PARALLEL; | |
66 | offset = 0; | |
67 | work = (index->cache_nr + threads - 1) / threads; | |
68 | for (i = 0; i < threads; i++) { | |
69 | struct thread_data *p = data+i; | |
70 | p->index = index; | |
71 | p->pathspec = pathspec; | |
72 | p->offset = offset; | |
73 | p->nr = work; | |
74 | offset += work; | |
75 | if (pthread_create(&p->pthread, NULL, preload_thread, p)) | |
76 | die("unable to create threaded lstat"); | |
77 | } | |
78 | for (i = 0; i < threads; i++) { | |
79 | struct thread_data *p = data+i; | |
80 | if (pthread_join(p->pthread, NULL)) | |
81 | die("unable to join threaded lstat"); | |
82 | } | |
83 | } | |
84 | ||
85 | int read_index_preload(struct index_state *index, const char **pathspec) | |
86 | { | |
87 | int retval = read_index(index); | |
88 | ||
89 | preload_index(index, pathspec); | |
90 | return retval; | |
91 | } |