blob: c1fe3a3ef9cba0c155d02475e24315784bd27ae4 [file] [log] [blame]
Linus Torvalds671c9b72008-11-13 16:36:30 -08001/*
2 * Copyright (C) 2008 Linus Torvalds
3 */
4#include "cache.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"
Junio C Hamano46059cc2008-11-15 04:08:14 -08007
8#ifdef NO_PTHREADS
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +07009static void preload_index(struct index_state *index,
10 const struct pathspec *pathspec)
Junio C Hamano46059cc2008-11-15 04:08:14 -080011{
12 ; /* nothing */
13}
14#else
15
Linus Torvalds671c9b72008-11-13 16:36:30 -080016#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;
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070030 struct pathspec pathspec;
Linus Torvalds671c9b72008-11-13 16:36:30 -080031 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;
Karsten Bleese7c73052014-07-05 00:41:46 +020040 struct cache_def cache = CACHE_DEF_INIT;
Linus Torvalds671c9b72008-11-13 16:36:30 -080041
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;
Junio C Hamano125fd982010-01-24 00:10:20 -080052 if (S_ISGITLINK(ce->ce_mode))
53 continue;
Linus Torvalds671c9b72008-11-13 16:36:30 -080054 if (ce_uptodate(ce))
55 continue;
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +070056 if (!ce_path_match(ce, &p->pathspec, NULL))
Linus Torvalds671c9b72008-11-13 16:36:30 -080057 continue;
Linus Torvaldsf62ce3d2009-07-09 13:37:02 -070058 if (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))
59 continue;
Linus Torvalds671c9b72008-11-13 16:36:30 -080060 if (lstat(ce->name, &st))
61 continue;
Linus Torvalds7c4ea592008-11-17 09:01:20 -080062 if (ie_match_stat(index, ce, &st, CE_MATCH_RACY_IS_DIRTY))
Linus Torvalds671c9b72008-11-13 16:36:30 -080063 continue;
64 ce_mark_uptodate(ce);
65 } while (--nr > 0);
Karsten Blees2a608392014-07-12 01:02:34 +020066 cache_def_clear(&cache);
Linus Torvalds671c9b72008-11-13 16:36:30 -080067 return NULL;
68}
69
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070070static void preload_index(struct index_state *index,
71 const struct pathspec *pathspec)
Linus Torvalds671c9b72008-11-13 16:36:30 -080072{
73 int threads, i, work, offset;
74 struct thread_data data[MAX_PARALLEL];
75
76 if (!core_preload_index)
77 return;
78
79 threads = index->cache_nr / THREAD_COST;
80 if (threads < 2)
81 return;
82 if (threads > MAX_PARALLEL)
83 threads = MAX_PARALLEL;
84 offset = 0;
Pierre Habouzit98cb6f32009-07-22 23:34:35 +020085 work = DIV_ROUND_UP(index->cache_nr, threads);
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070086 memset(&data, 0, sizeof(data));
Linus Torvalds671c9b72008-11-13 16:36:30 -080087 for (i = 0; i < threads; i++) {
88 struct thread_data *p = data+i;
89 p->index = index;
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +070090 if (pathspec)
91 copy_pathspec(&p->pathspec, pathspec);
Linus Torvalds671c9b72008-11-13 16:36:30 -080092 p->offset = offset;
93 p->nr = work;
94 offset += work;
95 if (pthread_create(&p->pthread, NULL, preload_thread, p))
96 die("unable to create threaded lstat");
97 }
98 for (i = 0; i < threads; i++) {
99 struct thread_data *p = data+i;
100 if (pthread_join(p->pthread, NULL))
101 die("unable to join threaded lstat");
102 }
103}
Junio C Hamano46059cc2008-11-15 04:08:14 -0800104#endif
Linus Torvalds671c9b72008-11-13 16:36:30 -0800105
Nguyễn Thái Ngọc Duy5ab2a2d2013-07-14 15:35:49 +0700106int read_index_preload(struct index_state *index,
107 const struct pathspec *pathspec)
Linus Torvalds671c9b72008-11-13 16:36:30 -0800108{
109 int retval = read_index(index);
110
111 preload_index(index, pathspec);
112 return retval;
113}