Merge branch 'tb/stash-in-c-unused-param-fix'
[alt-git.git] / t / helper / test-lazy-init-name-hash.c
blobb99a37080d935fa27df4e372e0e8cddd9fd08b3c
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "parse-options.h"
5 static int single;
6 static int multi;
7 static int count = 1;
8 static int dump;
9 static int perf;
10 static int analyze;
11 static int analyze_step;
14 * Dump the contents of the "dir" and "name" hash tables to stdout.
15 * If you sort the result, you can compare it with the other type
16 * mode and verify that both single and multi produce the same set.
18 static void dump_run(void)
20 struct hashmap_iter iter_dir;
21 struct hashmap_iter iter_cache;
23 /* Stolen from name-hash.c */
24 struct dir_entry {
25 struct hashmap_entry ent;
26 struct dir_entry *parent;
27 int nr;
28 unsigned int namelen;
29 char name[FLEX_ARRAY];
32 struct dir_entry *dir;
33 struct cache_entry *ce;
35 read_cache();
36 if (single) {
37 test_lazy_init_name_hash(&the_index, 0);
38 } else {
39 int nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
40 if (!nr_threads_used)
41 die("non-threaded code path used");
44 dir = hashmap_iter_first(&the_index.dir_hash, &iter_dir);
45 while (dir) {
46 printf("dir %08x %7d %s\n", dir->ent.hash, dir->nr, dir->name);
47 dir = hashmap_iter_next(&iter_dir);
50 ce = hashmap_iter_first(&the_index.name_hash, &iter_cache);
51 while (ce) {
52 printf("name %08x %s\n", ce->ent.hash, ce->name);
53 ce = hashmap_iter_next(&iter_cache);
56 discard_cache();
60 * Run the single or multi threaded version "count" times and
61 * report on the time taken.
63 static uint64_t time_runs(int try_threaded)
65 uint64_t t0, t1, t2;
66 uint64_t sum = 0;
67 uint64_t avg;
68 int nr_threads_used;
69 int i;
71 for (i = 0; i < count; i++) {
72 t0 = getnanotime();
73 read_cache();
74 t1 = getnanotime();
75 nr_threads_used = test_lazy_init_name_hash(&the_index, try_threaded);
76 t2 = getnanotime();
78 sum += (t2 - t1);
80 if (try_threaded && !nr_threads_used)
81 die("non-threaded code path used");
83 if (nr_threads_used)
84 printf("%f %f %d multi %d\n",
85 ((double)(t1 - t0))/1000000000,
86 ((double)(t2 - t1))/1000000000,
87 the_index.cache_nr,
88 nr_threads_used);
89 else
90 printf("%f %f %d single\n",
91 ((double)(t1 - t0))/1000000000,
92 ((double)(t2 - t1))/1000000000,
93 the_index.cache_nr);
94 fflush(stdout);
96 discard_cache();
99 avg = sum / count;
100 if (count > 1)
101 printf("avg %f %s\n",
102 (double)avg/1000000000,
103 (try_threaded) ? "multi" : "single");
105 return avg;
109 * Try a series of runs varying the "istate->cache_nr" and
110 * try to find a good value for the multi-threaded criteria.
112 static void analyze_run(void)
114 uint64_t t1s, t1m, t2s, t2m;
115 int cache_nr_limit;
116 int nr_threads_used = 0;
117 int i;
118 int nr;
120 read_cache();
121 cache_nr_limit = the_index.cache_nr;
122 discard_cache();
124 nr = analyze;
125 while (1) {
126 uint64_t sum_single = 0;
127 uint64_t sum_multi = 0;
128 uint64_t avg_single;
129 uint64_t avg_multi;
131 if (nr > cache_nr_limit)
132 nr = cache_nr_limit;
134 for (i = 0; i < count; i++) {
135 read_cache();
136 the_index.cache_nr = nr; /* cheap truncate of index */
137 t1s = getnanotime();
138 test_lazy_init_name_hash(&the_index, 0);
139 t2s = getnanotime();
140 sum_single += (t2s - t1s);
141 the_index.cache_nr = cache_nr_limit;
142 discard_cache();
144 read_cache();
145 the_index.cache_nr = nr; /* cheap truncate of index */
146 t1m = getnanotime();
147 nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
148 t2m = getnanotime();
149 sum_multi += (t2m - t1m);
150 the_index.cache_nr = cache_nr_limit;
151 discard_cache();
153 if (!nr_threads_used)
154 printf(" [size %8d] [single %f] non-threaded code path used\n",
155 nr, ((double)(t2s - t1s))/1000000000);
156 else
157 printf(" [size %8d] [single %f] %c [multi %f %d]\n",
159 ((double)(t2s - t1s))/1000000000,
160 (((t2s - t1s) < (t2m - t1m)) ? '<' : '>'),
161 ((double)(t2m - t1m))/1000000000,
162 nr_threads_used);
163 fflush(stdout);
165 if (count > 1) {
166 avg_single = sum_single / count;
167 avg_multi = sum_multi / count;
168 if (!nr_threads_used)
169 printf("avg [size %8d] [single %f]\n",
171 (double)avg_single/1000000000);
172 else
173 printf("avg [size %8d] [single %f] %c [multi %f %d]\n",
175 (double)avg_single/1000000000,
176 (avg_single < avg_multi ? '<' : '>'),
177 (double)avg_multi/1000000000,
178 nr_threads_used);
179 fflush(stdout);
182 if (nr >= cache_nr_limit)
183 return;
184 nr += analyze_step;
188 int cmd__lazy_init_name_hash(int argc, const char **argv)
190 const char *usage[] = {
191 "test-tool lazy-init-name-hash -d (-s | -m)",
192 "test-tool lazy-init-name-hash -p [-c c]",
193 "test-tool lazy-init-name-hash -a a [--step s] [-c c]",
194 "test-tool lazy-init-name-hash (-s | -m) [-c c]",
195 "test-tool lazy-init-name-hash -s -m [-c c]",
196 NULL
198 struct option options[] = {
199 OPT_BOOL('s', "single", &single, "run single-threaded code"),
200 OPT_BOOL('m', "multi", &multi, "run multi-threaded code"),
201 OPT_INTEGER('c', "count", &count, "number of passes"),
202 OPT_BOOL('d', "dump", &dump, "dump hash tables"),
203 OPT_BOOL('p', "perf", &perf, "compare single vs multi"),
204 OPT_INTEGER('a', "analyze", &analyze, "analyze different multi sizes"),
205 OPT_INTEGER(0, "step", &analyze_step, "analyze step factor"),
206 OPT_END(),
208 const char *prefix;
209 uint64_t avg_single, avg_multi;
211 prefix = setup_git_directory();
213 argc = parse_options(argc, argv, prefix, options, usage, 0);
216 * istate->dir_hash is only created when ignore_case is set.
218 ignore_case = 1;
220 if (dump) {
221 if (perf || analyze > 0)
222 die("cannot combine dump, perf, or analyze");
223 if (count > 1)
224 die("count not valid with dump");
225 if (single && multi)
226 die("cannot use both single and multi with dump");
227 if (!single && !multi)
228 die("dump requires either single or multi");
229 dump_run();
230 return 0;
233 if (perf) {
234 if (analyze > 0)
235 die("cannot combine dump, perf, or analyze");
236 if (single || multi)
237 die("cannot use single or multi with perf");
238 avg_single = time_runs(0);
239 avg_multi = time_runs(1);
240 if (avg_multi > avg_single)
241 die("multi is slower");
242 return 0;
245 if (analyze) {
246 if (analyze < 500)
247 die("analyze must be at least 500");
248 if (!analyze_step)
249 analyze_step = analyze;
250 if (single || multi)
251 die("cannot use single or multi with analyze");
252 analyze_run();
253 return 0;
256 if (!single && !multi)
257 die("require either -s or -m or both");
259 if (single)
260 time_runs(0);
261 if (multi)
262 time_runs(1);
264 return 0;