pack-bitmap-write: build fewer intermediate bitmaps
[git] / pack-bitmap-write.c
1 #include "cache.h"
2 #include "object-store.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "list-objects.h"
8 #include "progress.h"
9 #include "pack-revindex.h"
10 #include "pack.h"
11 #include "pack-bitmap.h"
12 #include "sha1-lookup.h"
13 #include "pack-objects.h"
14 #include "commit-reach.h"
15 #include "prio-queue.h"
16
17 struct bitmapped_commit {
18         struct commit *commit;
19         struct ewah_bitmap *bitmap;
20         struct ewah_bitmap *write_as;
21         int flags;
22         int xor_offset;
23         uint32_t commit_pos;
24 };
25
26 struct bitmap_writer {
27         struct ewah_bitmap *commits;
28         struct ewah_bitmap *trees;
29         struct ewah_bitmap *blobs;
30         struct ewah_bitmap *tags;
31
32         kh_oid_map_t *bitmaps;
33         kh_oid_map_t *reused;
34         struct packing_data *to_pack;
35
36         struct bitmapped_commit *selected;
37         unsigned int selected_nr, selected_alloc;
38
39         struct progress *progress;
40         int show_progress;
41         unsigned char pack_checksum[GIT_MAX_RAWSZ];
42 };
43
44 static struct bitmap_writer writer;
45
46 void bitmap_writer_show_progress(int show)
47 {
48         writer.show_progress = show;
49 }
50
51 /**
52  * Build the initial type index for the packfile
53  */
54 void bitmap_writer_build_type_index(struct packing_data *to_pack,
55                                     struct pack_idx_entry **index,
56                                     uint32_t index_nr)
57 {
58         uint32_t i;
59
60         writer.commits = ewah_new();
61         writer.trees = ewah_new();
62         writer.blobs = ewah_new();
63         writer.tags = ewah_new();
64         ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
65
66         for (i = 0; i < index_nr; ++i) {
67                 struct object_entry *entry = (struct object_entry *)index[i];
68                 enum object_type real_type;
69
70                 oe_set_in_pack_pos(to_pack, entry, i);
71
72                 switch (oe_type(entry)) {
73                 case OBJ_COMMIT:
74                 case OBJ_TREE:
75                 case OBJ_BLOB:
76                 case OBJ_TAG:
77                         real_type = oe_type(entry);
78                         break;
79
80                 default:
81                         real_type = oid_object_info(to_pack->repo,
82                                                     &entry->idx.oid, NULL);
83                         break;
84                 }
85
86                 switch (real_type) {
87                 case OBJ_COMMIT:
88                         ewah_set(writer.commits, i);
89                         break;
90
91                 case OBJ_TREE:
92                         ewah_set(writer.trees, i);
93                         break;
94
95                 case OBJ_BLOB:
96                         ewah_set(writer.blobs, i);
97                         break;
98
99                 case OBJ_TAG:
100                         ewah_set(writer.tags, i);
101                         break;
102
103                 default:
104                         die("Missing type information for %s (%d/%d)",
105                             oid_to_hex(&entry->idx.oid), real_type,
106                             oe_type(entry));
107                 }
108         }
109 }
110
111 /**
112  * Compute the actual bitmaps
113  */
114
115 static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitmap *reused)
116 {
117         if (writer.selected_nr >= writer.selected_alloc) {
118                 writer.selected_alloc = (writer.selected_alloc + 32) * 2;
119                 REALLOC_ARRAY(writer.selected, writer.selected_alloc);
120         }
121
122         writer.selected[writer.selected_nr].commit = commit;
123         writer.selected[writer.selected_nr].bitmap = reused;
124         writer.selected[writer.selected_nr].flags = 0;
125
126         writer.selected_nr++;
127 }
128
129 static uint32_t find_object_pos(const struct object_id *oid)
130 {
131         struct object_entry *entry = packlist_find(writer.to_pack, oid);
132
133         if (!entry) {
134                 die("Failed to write bitmap index. Packfile doesn't have full closure "
135                         "(object %s is missing)", oid_to_hex(oid));
136         }
137
138         return oe_in_pack_pos(writer.to_pack, entry);
139 }
140
141 static void compute_xor_offsets(void)
142 {
143         static const int MAX_XOR_OFFSET_SEARCH = 10;
144
145         int i, next = 0;
146
147         while (next < writer.selected_nr) {
148                 struct bitmapped_commit *stored = &writer.selected[next];
149
150                 int best_offset = 0;
151                 struct ewah_bitmap *best_bitmap = stored->bitmap;
152                 struct ewah_bitmap *test_xor;
153
154                 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
155                         int curr = next - i;
156
157                         if (curr < 0)
158                                 break;
159
160                         test_xor = ewah_pool_new();
161                         ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
162
163                         if (test_xor->buffer_size < best_bitmap->buffer_size) {
164                                 if (best_bitmap != stored->bitmap)
165                                         ewah_pool_free(best_bitmap);
166
167                                 best_bitmap = test_xor;
168                                 best_offset = i;
169                         } else {
170                                 ewah_pool_free(test_xor);
171                         }
172                 }
173
174                 stored->xor_offset = best_offset;
175                 stored->write_as = best_bitmap;
176
177                 next++;
178         }
179 }
180
181 struct bb_commit {
182         struct commit_list *reverse_edges;
183         struct bitmap *commit_mask;
184         struct bitmap *bitmap;
185         unsigned selected:1,
186                  maximal:1;
187         unsigned idx; /* within selected array */
188 };
189
190 define_commit_slab(bb_data, struct bb_commit);
191
192 struct bitmap_builder {
193         struct bb_data data;
194         struct commit **commits;
195         size_t commits_nr, commits_alloc;
196 };
197
198 static void bitmap_builder_init(struct bitmap_builder *bb,
199                                 struct bitmap_writer *writer)
200 {
201         struct rev_info revs;
202         struct commit *commit;
203         unsigned int i, num_maximal;
204
205         memset(bb, 0, sizeof(*bb));
206         init_bb_data(&bb->data);
207
208         reset_revision_walk();
209         repo_init_revisions(writer->to_pack->repo, &revs, NULL);
210         revs.topo_order = 1;
211
212         for (i = 0; i < writer->selected_nr; i++) {
213                 struct commit *c = writer->selected[i].commit;
214                 struct bb_commit *ent = bb_data_at(&bb->data, c);
215
216                 ent->selected = 1;
217                 ent->maximal = 1;
218                 ent->idx = i;
219
220                 ent->commit_mask = bitmap_new();
221                 bitmap_set(ent->commit_mask, i);
222
223                 add_pending_object(&revs, &c->object, "");
224         }
225         num_maximal = writer->selected_nr;
226
227         if (prepare_revision_walk(&revs))
228                 die("revision walk setup failed");
229
230         while ((commit = get_revision(&revs))) {
231                 struct commit_list *p;
232                 struct bb_commit *c_ent;
233
234                 parse_commit_or_die(commit);
235
236                 c_ent = bb_data_at(&bb->data, commit);
237
238                 if (c_ent->maximal) {
239                         if (!c_ent->selected) {
240                                 bitmap_set(c_ent->commit_mask, num_maximal);
241                                 num_maximal++;
242                         }
243
244                         ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
245                         bb->commits[bb->commits_nr++] = commit;
246                 }
247
248                 for (p = commit->parents; p; p = p->next) {
249                         struct bb_commit *p_ent = bb_data_at(&bb->data, p->item);
250                         int c_not_p, p_not_c;
251
252                         if (!p_ent->commit_mask) {
253                                 p_ent->commit_mask = bitmap_new();
254                                 c_not_p = 1;
255                                 p_not_c = 0;
256                         } else {
257                                 c_not_p = bitmap_is_subset(c_ent->commit_mask, p_ent->commit_mask);
258                                 p_not_c = bitmap_is_subset(p_ent->commit_mask, c_ent->commit_mask);
259                         }
260
261                         if (!c_not_p)
262                                 continue;
263
264                         bitmap_or(p_ent->commit_mask, c_ent->commit_mask);
265
266                         if (p_not_c)
267                                 p_ent->maximal = 1;
268                         else {
269                                 p_ent->maximal = 0;
270                                 free_commit_list(p_ent->reverse_edges);
271                                 p_ent->reverse_edges = NULL;
272                         }
273
274                         if (c_ent->maximal) {
275                                 commit_list_insert(commit, &p_ent->reverse_edges);
276                         } else {
277                                 struct commit_list *cc = c_ent->reverse_edges;
278
279                                 for (; cc; cc = cc->next) {
280                                         if (!commit_list_contains(cc->item, p_ent->reverse_edges))
281                                                 commit_list_insert(cc->item, &p_ent->reverse_edges);
282                                 }
283                         }
284                 }
285
286                 bitmap_free(c_ent->commit_mask);
287                 c_ent->commit_mask = NULL;
288         }
289
290         trace2_data_intmax("pack-bitmap-write", the_repository,
291                            "num_selected_commits", writer->selected_nr);
292         trace2_data_intmax("pack-bitmap-write", the_repository,
293                            "num_maximal_commits", num_maximal);
294 }
295
296 static void bitmap_builder_clear(struct bitmap_builder *bb)
297 {
298         clear_bb_data(&bb->data);
299         free(bb->commits);
300         bb->commits_nr = bb->commits_alloc = 0;
301 }
302
303 static void fill_bitmap_tree(struct bitmap *bitmap,
304                              struct tree *tree)
305 {
306         uint32_t pos;
307         struct tree_desc desc;
308         struct name_entry entry;
309
310         /*
311          * If our bit is already set, then there is nothing to do. Both this
312          * tree and all of its children will be set.
313          */
314         pos = find_object_pos(&tree->object.oid);
315         if (bitmap_get(bitmap, pos))
316                 return;
317         bitmap_set(bitmap, pos);
318
319         if (parse_tree(tree) < 0)
320                 die("unable to load tree object %s",
321                     oid_to_hex(&tree->object.oid));
322         init_tree_desc(&desc, tree->buffer, tree->size);
323
324         while (tree_entry(&desc, &entry)) {
325                 switch (object_type(entry.mode)) {
326                 case OBJ_TREE:
327                         fill_bitmap_tree(bitmap,
328                                          lookup_tree(the_repository, &entry.oid));
329                         break;
330                 case OBJ_BLOB:
331                         bitmap_set(bitmap, find_object_pos(&entry.oid));
332                         break;
333                 default:
334                         /* Gitlink, etc; not reachable */
335                         break;
336                 }
337         }
338
339         free_tree_buffer(tree);
340 }
341
342 static void fill_bitmap_commit(struct bb_commit *ent,
343                                struct commit *commit,
344                                struct prio_queue *queue)
345 {
346         if (!ent->bitmap)
347                 ent->bitmap = bitmap_new();
348
349         bitmap_set(ent->bitmap, find_object_pos(&commit->object.oid));
350         prio_queue_put(queue, commit);
351
352         while (queue->nr) {
353                 struct commit_list *p;
354                 struct commit *c = prio_queue_get(queue);
355
356                 bitmap_set(ent->bitmap, find_object_pos(&c->object.oid));
357                 fill_bitmap_tree(ent->bitmap, get_commit_tree(c));
358
359                 for (p = c->parents; p; p = p->next) {
360                         int pos = find_object_pos(&p->item->object.oid);
361                         if (!bitmap_get(ent->bitmap, pos)) {
362                                 bitmap_set(ent->bitmap, pos);
363                                 prio_queue_put(queue, p->item);
364                         }
365                 }
366         }
367 }
368
369 static void store_selected(struct bb_commit *ent, struct commit *commit)
370 {
371         struct bitmapped_commit *stored = &writer.selected[ent->idx];
372         khiter_t hash_pos;
373         int hash_ret;
374
375         /*
376          * the "reuse bitmaps" phase may have stored something here, but
377          * our new algorithm doesn't use it. Drop it.
378          */
379         if (stored->bitmap)
380                 ewah_free(stored->bitmap);
381
382         stored->bitmap = bitmap_to_ewah(ent->bitmap);
383
384         hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret);
385         if (hash_ret == 0)
386                 die("Duplicate entry when writing index: %s",
387                     oid_to_hex(&commit->object.oid));
388         kh_value(writer.bitmaps, hash_pos) = stored;
389 }
390
391 void bitmap_writer_build(struct packing_data *to_pack)
392 {
393         struct bitmap_builder bb;
394         size_t i;
395         int nr_stored = 0; /* for progress */
396         struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
397
398         writer.bitmaps = kh_init_oid_map();
399         writer.to_pack = to_pack;
400
401         if (writer.show_progress)
402                 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
403         trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
404                             the_repository);
405
406         bitmap_builder_init(&bb, &writer);
407         for (i = bb.commits_nr; i > 0; i--) {
408                 struct commit *commit = bb.commits[i-1];
409                 struct bb_commit *ent = bb_data_at(&bb.data, commit);
410                 struct commit *child;
411                 int reused = 0;
412
413                 fill_bitmap_commit(ent, commit, &queue);
414
415                 if (ent->selected) {
416                         store_selected(ent, commit);
417                         nr_stored++;
418                         display_progress(writer.progress, nr_stored);
419                 }
420
421                 while ((child = pop_commit(&ent->reverse_edges))) {
422                         struct bb_commit *child_ent =
423                                 bb_data_at(&bb.data, child);
424
425                         if (child_ent->bitmap)
426                                 bitmap_or(child_ent->bitmap, ent->bitmap);
427                         else if (reused)
428                                 child_ent->bitmap = bitmap_dup(ent->bitmap);
429                         else {
430                                 child_ent->bitmap = ent->bitmap;
431                                 reused = 1;
432                         }
433                 }
434                 if (!reused)
435                         bitmap_free(ent->bitmap);
436                 ent->bitmap = NULL;
437         }
438         clear_prio_queue(&queue);
439         bitmap_builder_clear(&bb);
440
441         trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
442                             the_repository);
443
444         stop_progress(&writer.progress);
445
446         compute_xor_offsets();
447 }
448
449 /**
450  * Select the commits that will be bitmapped
451  */
452 static inline unsigned int next_commit_index(unsigned int idx)
453 {
454         static const unsigned int MIN_COMMITS = 100;
455         static const unsigned int MAX_COMMITS = 5000;
456
457         static const unsigned int MUST_REGION = 100;
458         static const unsigned int MIN_REGION = 20000;
459
460         unsigned int offset, next;
461
462         if (idx <= MUST_REGION)
463                 return 0;
464
465         if (idx <= MIN_REGION) {
466                 offset = idx - MUST_REGION;
467                 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
468         }
469
470         offset = idx - MIN_REGION;
471         next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
472
473         return (next > MIN_COMMITS) ? next : MIN_COMMITS;
474 }
475
476 static int date_compare(const void *_a, const void *_b)
477 {
478         struct commit *a = *(struct commit **)_a;
479         struct commit *b = *(struct commit **)_b;
480         return (long)b->date - (long)a->date;
481 }
482
483 void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
484 {
485         struct bitmap_index *bitmap_git;
486         if (!(bitmap_git = prepare_bitmap_git(to_pack->repo)))
487                 return;
488
489         writer.reused = kh_init_oid_map();
490         rebuild_existing_bitmaps(bitmap_git, to_pack, writer.reused,
491                                  writer.show_progress);
492         /*
493          * NEEDSWORK: rebuild_existing_bitmaps() makes writer.reused reference
494          * some bitmaps in bitmap_git, so we can't free the latter.
495          */
496 }
497
498 static struct ewah_bitmap *find_reused_bitmap(const struct object_id *oid)
499 {
500         khiter_t hash_pos;
501
502         if (!writer.reused)
503                 return NULL;
504
505         hash_pos = kh_get_oid_map(writer.reused, *oid);
506         if (hash_pos >= kh_end(writer.reused))
507                 return NULL;
508
509         return kh_value(writer.reused, hash_pos);
510 }
511
512 void bitmap_writer_select_commits(struct commit **indexed_commits,
513                                   unsigned int indexed_commits_nr,
514                                   int max_bitmaps)
515 {
516         unsigned int i = 0, j, next;
517
518         QSORT(indexed_commits, indexed_commits_nr, date_compare);
519
520         if (writer.show_progress)
521                 writer.progress = start_progress("Selecting bitmap commits", 0);
522
523         if (indexed_commits_nr < 100) {
524                 for (i = 0; i < indexed_commits_nr; ++i)
525                         push_bitmapped_commit(indexed_commits[i], NULL);
526                 return;
527         }
528
529         for (;;) {
530                 struct ewah_bitmap *reused_bitmap = NULL;
531                 struct commit *chosen = NULL;
532
533                 next = next_commit_index(i);
534
535                 if (i + next >= indexed_commits_nr)
536                         break;
537
538                 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
539                         writer.selected_nr = max_bitmaps;
540                         break;
541                 }
542
543                 if (next == 0) {
544                         chosen = indexed_commits[i];
545                         reused_bitmap = find_reused_bitmap(&chosen->object.oid);
546                 } else {
547                         chosen = indexed_commits[i + next];
548
549                         for (j = 0; j <= next; ++j) {
550                                 struct commit *cm = indexed_commits[i + j];
551
552                                 reused_bitmap = find_reused_bitmap(&cm->object.oid);
553                                 if (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {
554                                         chosen = cm;
555                                         break;
556                                 }
557
558                                 if (cm->parents && cm->parents->next)
559                                         chosen = cm;
560                         }
561                 }
562
563                 push_bitmapped_commit(chosen, reused_bitmap);
564
565                 i += next + 1;
566                 display_progress(writer.progress, i);
567         }
568
569         stop_progress(&writer.progress);
570 }
571
572
573 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
574 {
575         /* hashwrite will die on error */
576         hashwrite(f, buf, len);
577         return len;
578 }
579
580 /**
581  * Write the bitmap index to disk
582  */
583 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
584 {
585         if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
586                 die("Failed to write bitmap index");
587 }
588
589 static const unsigned char *sha1_access(size_t pos, void *table)
590 {
591         struct pack_idx_entry **index = table;
592         return index[pos]->oid.hash;
593 }
594
595 static void write_selected_commits_v1(struct hashfile *f,
596                                       struct pack_idx_entry **index,
597                                       uint32_t index_nr)
598 {
599         int i;
600
601         for (i = 0; i < writer.selected_nr; ++i) {
602                 struct bitmapped_commit *stored = &writer.selected[i];
603
604                 int commit_pos =
605                         sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
606
607                 if (commit_pos < 0)
608                         BUG("trying to write commit not in index");
609
610                 hashwrite_be32(f, commit_pos);
611                 hashwrite_u8(f, stored->xor_offset);
612                 hashwrite_u8(f, stored->flags);
613
614                 dump_bitmap(f, stored->write_as);
615         }
616 }
617
618 static void write_hash_cache(struct hashfile *f,
619                              struct pack_idx_entry **index,
620                              uint32_t index_nr)
621 {
622         uint32_t i;
623
624         for (i = 0; i < index_nr; ++i) {
625                 struct object_entry *entry = (struct object_entry *)index[i];
626                 hashwrite_be32(f, entry->hash);
627         }
628 }
629
630 void bitmap_writer_set_checksum(unsigned char *sha1)
631 {
632         hashcpy(writer.pack_checksum, sha1);
633 }
634
635 void bitmap_writer_finish(struct pack_idx_entry **index,
636                           uint32_t index_nr,
637                           const char *filename,
638                           uint16_t options)
639 {
640         static uint16_t default_version = 1;
641         static uint16_t flags = BITMAP_OPT_FULL_DAG;
642         struct strbuf tmp_file = STRBUF_INIT;
643         struct hashfile *f;
644
645         struct bitmap_disk_header header;
646
647         int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
648
649         f = hashfd(fd, tmp_file.buf);
650
651         memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
652         header.version = htons(default_version);
653         header.options = htons(flags | options);
654         header.entry_count = htonl(writer.selected_nr);
655         hashcpy(header.checksum, writer.pack_checksum);
656
657         hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
658         dump_bitmap(f, writer.commits);
659         dump_bitmap(f, writer.trees);
660         dump_bitmap(f, writer.blobs);
661         dump_bitmap(f, writer.tags);
662         write_selected_commits_v1(f, index, index_nr);
663
664         if (options & BITMAP_OPT_HASH_CACHE)
665                 write_hash_cache(f, index, index_nr);
666
667         finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
668
669         if (adjust_shared_perm(tmp_file.buf))
670                 die_errno("unable to make temporary bitmap file readable");
671
672         if (rename(tmp_file.buf, filename))
673                 die_errno("unable to rename temporary bitmap file to '%s'", filename);
674
675         strbuf_release(&tmp_file);
676 }