pack-bitmap: remove bitmap_git global variable
[git/git.git] / pack-bitmap-write.c
CommitLineData
7cc8f971
VM
1#include "cache.h"
2#include "commit.h"
3#include "tag.h"
4#include "diff.h"
5#include "revision.h"
6#include "list-objects.h"
7#include "progress.h"
8#include "pack-revindex.h"
9#include "pack.h"
10#include "pack-bitmap.h"
11#include "sha1-lookup.h"
12#include "pack-objects.h"
13
14struct bitmapped_commit {
15 struct commit *commit;
16 struct ewah_bitmap *bitmap;
17 struct ewah_bitmap *write_as;
18 int flags;
19 int xor_offset;
20 uint32_t commit_pos;
21};
22
23struct bitmap_writer {
24 struct ewah_bitmap *commits;
25 struct ewah_bitmap *trees;
26 struct ewah_bitmap *blobs;
27 struct ewah_bitmap *tags;
28
29 khash_sha1 *bitmaps;
30 khash_sha1 *reused;
31 struct packing_data *to_pack;
32
33 struct bitmapped_commit *selected;
34 unsigned int selected_nr, selected_alloc;
35
36 struct progress *progress;
37 int show_progress;
38 unsigned char pack_checksum[20];
39};
40
41static struct bitmap_writer writer;
42
43void bitmap_writer_show_progress(int show)
44{
45 writer.show_progress = show;
46}
47
48/**
49 * Build the initial type index for the packfile
50 */
06af3bba
NTND
51void bitmap_writer_build_type_index(struct packing_data *to_pack,
52 struct pack_idx_entry **index,
7cc8f971
VM
53 uint32_t index_nr)
54{
55 uint32_t i;
56
57 writer.commits = ewah_new();
58 writer.trees = ewah_new();
59 writer.blobs = ewah_new();
60 writer.tags = ewah_new();
06af3bba 61 ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
7cc8f971
VM
62
63 for (i = 0; i < index_nr; ++i) {
64 struct object_entry *entry = (struct object_entry *)index[i];
65 enum object_type real_type;
66
06af3bba 67 oe_set_in_pack_pos(to_pack, entry, i);
7cc8f971 68
fd9b1bae 69 switch (oe_type(entry)) {
7cc8f971
VM
70 case OBJ_COMMIT:
71 case OBJ_TREE:
72 case OBJ_BLOB:
73 case OBJ_TAG:
fd9b1bae 74 real_type = oe_type(entry);
7cc8f971
VM
75 break;
76
77 default:
0df8e965
SB
78 real_type = oid_object_info(the_repository,
79 &entry->idx.oid, NULL);
7cc8f971
VM
80 break;
81 }
82
83 switch (real_type) {
84 case OBJ_COMMIT:
85 ewah_set(writer.commits, i);
86 break;
87
88 case OBJ_TREE:
89 ewah_set(writer.trees, i);
90 break;
91
92 case OBJ_BLOB:
93 ewah_set(writer.blobs, i);
94 break;
95
96 case OBJ_TAG:
97 ewah_set(writer.tags, i);
98 break;
99
100 default:
101 die("Missing type information for %s (%d/%d)",
e6a492b7 102 oid_to_hex(&entry->idx.oid), real_type,
fd9b1bae 103 oe_type(entry));
7cc8f971
VM
104 }
105 }
106}
107
108/**
109 * Compute the actual bitmaps
110 */
111static struct object **seen_objects;
112static unsigned int seen_objects_nr, seen_objects_alloc;
113
114static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitmap *reused)
115{
116 if (writer.selected_nr >= writer.selected_alloc) {
117 writer.selected_alloc = (writer.selected_alloc + 32) * 2;
2756ca43 118 REALLOC_ARRAY(writer.selected, writer.selected_alloc);
7cc8f971
VM
119 }
120
121 writer.selected[writer.selected_nr].commit = commit;
122 writer.selected[writer.selected_nr].bitmap = reused;
123 writer.selected[writer.selected_nr].flags = 0;
124
125 writer.selected_nr++;
126}
127
128static inline void mark_as_seen(struct object *object)
129{
130 ALLOC_GROW(seen_objects, seen_objects_nr + 1, seen_objects_alloc);
131 seen_objects[seen_objects_nr++] = object;
132}
133
134static inline void reset_all_seen(void)
135{
136 unsigned int i;
137 for (i = 0; i < seen_objects_nr; ++i) {
138 seen_objects[i]->flags &= ~(SEEN | ADDED | SHOWN);
139 }
140 seen_objects_nr = 0;
141}
142
143static uint32_t find_object_pos(const unsigned char *sha1)
144{
145 struct object_entry *entry = packlist_find(writer.to_pack, sha1, NULL);
146
147 if (!entry) {
148 die("Failed to write bitmap index. Packfile doesn't have full closure "
149 "(object %s is missing)", sha1_to_hex(sha1));
150 }
151
06af3bba 152 return oe_in_pack_pos(writer.to_pack, entry);
7cc8f971
VM
153}
154
de1e67d0 155static void show_object(struct object *object, const char *name, void *data)
7cc8f971
VM
156{
157 struct bitmap *base = data;
ed1c9977 158 bitmap_set(base, find_object_pos(object->oid.hash));
7cc8f971
VM
159 mark_as_seen(object);
160}
161
162static void show_commit(struct commit *commit, void *data)
163{
164 mark_as_seen((struct object *)commit);
165}
166
167static int
168add_to_include_set(struct bitmap *base, struct commit *commit)
169{
170 khiter_t hash_pos;
ed1c9977 171 uint32_t bitmap_pos = find_object_pos(commit->object.oid.hash);
7cc8f971
VM
172
173 if (bitmap_get(base, bitmap_pos))
174 return 0;
175
ed1c9977 176 hash_pos = kh_get_sha1(writer.bitmaps, commit->object.oid.hash);
7cc8f971
VM
177 if (hash_pos < kh_end(writer.bitmaps)) {
178 struct bitmapped_commit *bc = kh_value(writer.bitmaps, hash_pos);
179 bitmap_or_ewah(base, bc->bitmap);
180 return 0;
181 }
182
183 bitmap_set(base, bitmap_pos);
184 return 1;
185}
186
187static int
188should_include(struct commit *commit, void *_data)
189{
190 struct bitmap *base = _data;
191
192 if (!add_to_include_set(base, commit)) {
193 struct commit_list *parent = commit->parents;
194
195 mark_as_seen((struct object *)commit);
196
197 while (parent) {
198 parent->item->object.flags |= SEEN;
199 mark_as_seen((struct object *)parent->item);
200 parent = parent->next;
201 }
202
203 return 0;
204 }
205
206 return 1;
207}
208
209static void compute_xor_offsets(void)
210{
211 static const int MAX_XOR_OFFSET_SEARCH = 10;
212
213 int i, next = 0;
214
215 while (next < writer.selected_nr) {
216 struct bitmapped_commit *stored = &writer.selected[next];
217
218 int best_offset = 0;
219 struct ewah_bitmap *best_bitmap = stored->bitmap;
220 struct ewah_bitmap *test_xor;
221
222 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
223 int curr = next - i;
224
225 if (curr < 0)
226 break;
227
228 test_xor = ewah_pool_new();
229 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
230
231 if (test_xor->buffer_size < best_bitmap->buffer_size) {
232 if (best_bitmap != stored->bitmap)
233 ewah_pool_free(best_bitmap);
234
235 best_bitmap = test_xor;
236 best_offset = i;
237 } else {
238 ewah_pool_free(test_xor);
239 }
240 }
241
242 stored->xor_offset = best_offset;
243 stored->write_as = best_bitmap;
244
245 next++;
246 }
247}
248
249void bitmap_writer_build(struct packing_data *to_pack)
250{
251 static const double REUSE_BITMAP_THRESHOLD = 0.2;
252
253 int i, reuse_after, need_reset;
254 struct bitmap *base = bitmap_new();
255 struct rev_info revs;
256
257 writer.bitmaps = kh_init_sha1();
258 writer.to_pack = to_pack;
259
260 if (writer.show_progress)
261 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
262
263 init_revisions(&revs, NULL);
264 revs.tag_objects = 1;
265 revs.tree_objects = 1;
266 revs.blob_objects = 1;
267 revs.no_walk = 0;
268
269 revs.include_check = should_include;
270 reset_revision_walk();
271
272 reuse_after = writer.selected_nr * REUSE_BITMAP_THRESHOLD;
273 need_reset = 0;
274
275 for (i = writer.selected_nr - 1; i >= 0; --i) {
276 struct bitmapped_commit *stored;
277 struct object *object;
278
279 khiter_t hash_pos;
280 int hash_ret;
281
282 stored = &writer.selected[i];
283 object = (struct object *)stored->commit;
284
285 if (stored->bitmap == NULL) {
286 if (i < writer.selected_nr - 1 &&
287 (need_reset ||
288 !in_merge_bases(writer.selected[i + 1].commit,
289 stored->commit))) {
290 bitmap_reset(base);
291 reset_all_seen();
292 }
293
294 add_pending_object(&revs, object, "");
295 revs.include_check_data = base;
296
297 if (prepare_revision_walk(&revs))
298 die("revision walk setup failed");
299
300 traverse_commit_list(&revs, show_commit, show_object, base);
301
4d01a7fa 302 object_array_clear(&revs.pending);
7cc8f971
VM
303
304 stored->bitmap = bitmap_to_ewah(base);
305 need_reset = 0;
306 } else
307 need_reset = 1;
308
309 if (i >= reuse_after)
310 stored->flags |= BITMAP_FLAG_REUSE;
311
ed1c9977 312 hash_pos = kh_put_sha1(writer.bitmaps, object->oid.hash, &hash_ret);
7cc8f971
VM
313 if (hash_ret == 0)
314 die("Duplicate entry when writing index: %s",
f2fd0760 315 oid_to_hex(&object->oid));
7cc8f971
VM
316
317 kh_value(writer.bitmaps, hash_pos) = stored;
318 display_progress(writer.progress, writer.selected_nr - i);
319 }
320
321 bitmap_free(base);
322 stop_progress(&writer.progress);
323
324 compute_xor_offsets();
325}
326
327/**
328 * Select the commits that will be bitmapped
329 */
330static inline unsigned int next_commit_index(unsigned int idx)
331{
332 static const unsigned int MIN_COMMITS = 100;
333 static const unsigned int MAX_COMMITS = 5000;
334
335 static const unsigned int MUST_REGION = 100;
336 static const unsigned int MIN_REGION = 20000;
337
338 unsigned int offset, next;
339
340 if (idx <= MUST_REGION)
341 return 0;
342
343 if (idx <= MIN_REGION) {
344 offset = idx - MUST_REGION;
345 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
346 }
347
348 offset = idx - MIN_REGION;
349 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
350
351 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
352}
353
354static int date_compare(const void *_a, const void *_b)
355{
356 struct commit *a = *(struct commit **)_a;
357 struct commit *b = *(struct commit **)_b;
358 return (long)b->date - (long)a->date;
359}
360
361void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
362{
3ae5fa07
JT
363 struct bitmap_index *bitmap_git;
364 if (!(bitmap_git = prepare_bitmap_git()))
7cc8f971
VM
365 return;
366
367 writer.reused = kh_init_sha1();
3ae5fa07
JT
368 rebuild_existing_bitmaps(bitmap_git, to_pack, writer.reused,
369 writer.show_progress);
7cc8f971
VM
370}
371
372static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
373{
374 khiter_t hash_pos;
375
376 if (!writer.reused)
377 return NULL;
378
379 hash_pos = kh_get_sha1(writer.reused, sha1);
380 if (hash_pos >= kh_end(writer.reused))
381 return NULL;
382
383 return kh_value(writer.reused, hash_pos);
384}
385
386void bitmap_writer_select_commits(struct commit **indexed_commits,
387 unsigned int indexed_commits_nr,
388 int max_bitmaps)
389{
390 unsigned int i = 0, j, next;
391
9ed0d8d6 392 QSORT(indexed_commits, indexed_commits_nr, date_compare);
7cc8f971
VM
393
394 if (writer.show_progress)
395 writer.progress = start_progress("Selecting bitmap commits", 0);
396
397 if (indexed_commits_nr < 100) {
398 for (i = 0; i < indexed_commits_nr; ++i)
399 push_bitmapped_commit(indexed_commits[i], NULL);
400 return;
401 }
402
403 for (;;) {
404 struct ewah_bitmap *reused_bitmap = NULL;
405 struct commit *chosen = NULL;
406
407 next = next_commit_index(i);
408
409 if (i + next >= indexed_commits_nr)
410 break;
411
412 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
413 writer.selected_nr = max_bitmaps;
414 break;
415 }
416
417 if (next == 0) {
418 chosen = indexed_commits[i];
ed1c9977 419 reused_bitmap = find_reused_bitmap(chosen->object.oid.hash);
7cc8f971
VM
420 } else {
421 chosen = indexed_commits[i + next];
422
423 for (j = 0; j <= next; ++j) {
424 struct commit *cm = indexed_commits[i + j];
425
ed1c9977 426 reused_bitmap = find_reused_bitmap(cm->object.oid.hash);
7cc8f971
VM
427 if (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {
428 chosen = cm;
429 break;
430 }
431
432 if (cm->parents && cm->parents->next)
433 chosen = cm;
434 }
435 }
436
437 push_bitmapped_commit(chosen, reused_bitmap);
438
439 i += next + 1;
440 display_progress(writer.progress, i);
441 }
442
443 stop_progress(&writer.progress);
444}
445
446
98a3beab 447static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
7cc8f971 448{
98a3beab 449 /* hashwrite will die on error */
450 hashwrite(f, buf, len);
7cc8f971
VM
451 return len;
452}
453
454/**
455 * Write the bitmap index to disk
456 */
98a3beab 457static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
7cc8f971 458{
98a3beab 459 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
7cc8f971
VM
460 die("Failed to write bitmap index");
461}
462
463static const unsigned char *sha1_access(size_t pos, void *table)
464{
465 struct pack_idx_entry **index = table;
e6a492b7 466 return index[pos]->oid.hash;
7cc8f971
VM
467}
468
98a3beab 469static void write_selected_commits_v1(struct hashfile *f,
7cc8f971
VM
470 struct pack_idx_entry **index,
471 uint32_t index_nr)
472{
473 int i;
474
475 for (i = 0; i < writer.selected_nr; ++i) {
476 struct bitmapped_commit *stored = &writer.selected[i];
7cc8f971
VM
477
478 int commit_pos =
ed1c9977 479 sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
7cc8f971
VM
480
481 if (commit_pos < 0)
033abf97 482 BUG("trying to write commit not in index");
7cc8f971 483
98a3beab 484 hashwrite_be32(f, commit_pos);
485 hashwrite_u8(f, stored->xor_offset);
486 hashwrite_u8(f, stored->flags);
7cc8f971 487
7cc8f971
VM
488 dump_bitmap(f, stored->write_as);
489 }
490}
491
98a3beab 492static void write_hash_cache(struct hashfile *f,
ae4f07fb
VM
493 struct pack_idx_entry **index,
494 uint32_t index_nr)
495{
496 uint32_t i;
497
498 for (i = 0; i < index_nr; ++i) {
499 struct object_entry *entry = (struct object_entry *)index[i];
500 uint32_t hash_value = htonl(entry->hash);
98a3beab 501 hashwrite(f, &hash_value, sizeof(hash_value));
ae4f07fb
VM
502 }
503}
504
7cc8f971
VM
505void bitmap_writer_set_checksum(unsigned char *sha1)
506{
507 hashcpy(writer.pack_checksum, sha1);
508}
509
510void bitmap_writer_finish(struct pack_idx_entry **index,
511 uint32_t index_nr,
ae4f07fb
VM
512 const char *filename,
513 uint16_t options)
7cc8f971 514{
7cc8f971
VM
515 static uint16_t default_version = 1;
516 static uint16_t flags = BITMAP_OPT_FULL_DAG;
594fa999 517 struct strbuf tmp_file = STRBUF_INIT;
98a3beab 518 struct hashfile *f;
7cc8f971
VM
519
520 struct bitmap_disk_header header;
521
594fa999 522 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
7cc8f971 523
98a3beab 524 f = hashfd(fd, tmp_file.buf);
7cc8f971
VM
525
526 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
527 header.version = htons(default_version);
ae4f07fb 528 header.options = htons(flags | options);
7cc8f971 529 header.entry_count = htonl(writer.selected_nr);
50546b15 530 hashcpy(header.checksum, writer.pack_checksum);
7cc8f971 531
98a3beab 532 hashwrite(f, &header, sizeof(header));
7cc8f971
VM
533 dump_bitmap(f, writer.commits);
534 dump_bitmap(f, writer.trees);
535 dump_bitmap(f, writer.blobs);
536 dump_bitmap(f, writer.tags);
537 write_selected_commits_v1(f, index, index_nr);
538
ae4f07fb
VM
539 if (options & BITMAP_OPT_HASH_CACHE)
540 write_hash_cache(f, index, index_nr);
541
cfe83216 542 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
7cc8f971 543
594fa999 544 if (adjust_shared_perm(tmp_file.buf))
7cc8f971
VM
545 die_errno("unable to make temporary bitmap file readable");
546
594fa999 547 if (rename(tmp_file.buf, filename))
7cc8f971 548 die_errno("unable to rename temporary bitmap file to '%s'", filename);
594fa999
JK
549
550 strbuf_release(&tmp_file);
7cc8f971 551}