1 #include "git-compat-util.h"
3 #include "environment.h"
6 #include "object-store.h"
11 #include "list-objects.h"
13 #include "pack-revindex.h"
15 #include "pack-bitmap.h"
16 #include "hash-lookup.h"
17 #include "pack-objects.h"
18 #include "commit-reach.h"
19 #include "prio-queue.h"
21 struct bitmapped_commit
{
22 struct commit
*commit
;
23 struct ewah_bitmap
*bitmap
;
24 struct ewah_bitmap
*write_as
;
30 struct bitmap_writer
{
31 struct ewah_bitmap
*commits
;
32 struct ewah_bitmap
*trees
;
33 struct ewah_bitmap
*blobs
;
34 struct ewah_bitmap
*tags
;
36 kh_oid_map_t
*bitmaps
;
37 struct packing_data
*to_pack
;
39 struct bitmapped_commit
*selected
;
40 unsigned int selected_nr
, selected_alloc
;
42 struct progress
*progress
;
44 unsigned char pack_checksum
[GIT_MAX_RAWSZ
];
47 static struct bitmap_writer writer
;
49 void bitmap_writer_show_progress(int show
)
51 writer
.show_progress
= show
;
55 * Build the initial type index for the packfile or multi-pack-index
57 void bitmap_writer_build_type_index(struct packing_data
*to_pack
,
58 struct pack_idx_entry
**index
,
63 writer
.commits
= ewah_new();
64 writer
.trees
= ewah_new();
65 writer
.blobs
= ewah_new();
66 writer
.tags
= ewah_new();
67 ALLOC_ARRAY(to_pack
->in_pack_pos
, to_pack
->nr_objects
);
69 for (i
= 0; i
< index_nr
; ++i
) {
70 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
71 enum object_type real_type
;
73 oe_set_in_pack_pos(to_pack
, entry
, i
);
75 switch (oe_type(entry
)) {
80 real_type
= oe_type(entry
);
84 real_type
= oid_object_info(to_pack
->repo
,
85 &entry
->idx
.oid
, NULL
);
91 ewah_set(writer
.commits
, i
);
95 ewah_set(writer
.trees
, i
);
99 ewah_set(writer
.blobs
, i
);
103 ewah_set(writer
.tags
, i
);
107 die("Missing type information for %s (%d/%d)",
108 oid_to_hex(&entry
->idx
.oid
), real_type
,
115 * Compute the actual bitmaps
118 static inline void push_bitmapped_commit(struct commit
*commit
)
120 if (writer
.selected_nr
>= writer
.selected_alloc
) {
121 writer
.selected_alloc
= (writer
.selected_alloc
+ 32) * 2;
122 REALLOC_ARRAY(writer
.selected
, writer
.selected_alloc
);
125 writer
.selected
[writer
.selected_nr
].commit
= commit
;
126 writer
.selected
[writer
.selected_nr
].bitmap
= NULL
;
127 writer
.selected
[writer
.selected_nr
].flags
= 0;
129 writer
.selected_nr
++;
132 static uint32_t find_object_pos(const struct object_id
*oid
, int *found
)
134 struct object_entry
*entry
= packlist_find(writer
.to_pack
, oid
);
139 warning("Failed to write bitmap index. Packfile doesn't have full closure "
140 "(object %s is missing)", oid_to_hex(oid
));
146 return oe_in_pack_pos(writer
.to_pack
, entry
);
149 static void compute_xor_offsets(void)
151 static const int MAX_XOR_OFFSET_SEARCH
= 10;
155 while (next
< writer
.selected_nr
) {
156 struct bitmapped_commit
*stored
= &writer
.selected
[next
];
159 struct ewah_bitmap
*best_bitmap
= stored
->bitmap
;
160 struct ewah_bitmap
*test_xor
;
162 for (i
= 1; i
<= MAX_XOR_OFFSET_SEARCH
; ++i
) {
168 test_xor
= ewah_pool_new();
169 ewah_xor(writer
.selected
[curr
].bitmap
, stored
->bitmap
, test_xor
);
171 if (test_xor
->buffer_size
< best_bitmap
->buffer_size
) {
172 if (best_bitmap
!= stored
->bitmap
)
173 ewah_pool_free(best_bitmap
);
175 best_bitmap
= test_xor
;
178 ewah_pool_free(test_xor
);
182 stored
->xor_offset
= best_offset
;
183 stored
->write_as
= best_bitmap
;
190 struct commit_list
*reverse_edges
;
191 struct bitmap
*commit_mask
;
192 struct bitmap
*bitmap
;
195 unsigned idx
; /* within selected array */
198 define_commit_slab(bb_data
, struct bb_commit
);
200 struct bitmap_builder
{
202 struct commit
**commits
;
203 size_t commits_nr
, commits_alloc
;
206 static void bitmap_builder_init(struct bitmap_builder
*bb
,
207 struct bitmap_writer
*writer
,
208 struct bitmap_index
*old_bitmap
)
210 struct rev_info revs
;
211 struct commit
*commit
;
212 struct commit_list
*reusable
= NULL
;
213 struct commit_list
*r
;
214 unsigned int i
, num_maximal
= 0;
216 memset(bb
, 0, sizeof(*bb
));
217 init_bb_data(&bb
->data
);
219 reset_revision_walk();
220 repo_init_revisions(writer
->to_pack
->repo
, &revs
, NULL
);
222 revs
.first_parent_only
= 1;
224 for (i
= 0; i
< writer
->selected_nr
; i
++) {
225 struct commit
*c
= writer
->selected
[i
].commit
;
226 struct bb_commit
*ent
= bb_data_at(&bb
->data
, c
);
232 ent
->commit_mask
= bitmap_new();
233 bitmap_set(ent
->commit_mask
, i
);
235 add_pending_object(&revs
, &c
->object
, "");
238 if (prepare_revision_walk(&revs
))
239 die("revision walk setup failed");
241 while ((commit
= get_revision(&revs
))) {
242 struct commit_list
*p
= commit
->parents
;
243 struct bb_commit
*c_ent
;
245 parse_commit_or_die(commit
);
247 c_ent
= bb_data_at(&bb
->data
, commit
);
250 * If there is no commit_mask, there is no reason to iterate
251 * over this commit; it is not selected (if it were, it would
252 * not have a blank commit mask) and all its children have
253 * existing bitmaps (see the comment starting with "This commit
254 * has an existing bitmap" below), so it does not contribute
255 * anything to the final bitmap file or its descendants.
257 if (!c_ent
->commit_mask
)
260 if (old_bitmap
&& bitmap_for_commit(old_bitmap
, commit
)) {
262 * This commit has an existing bitmap, so we can
263 * get its bits immediately without an object
264 * walk. That is, it is reusable as-is and there is no
265 * need to continue walking beyond it.
267 * Mark it as such and add it to bb->commits separately
268 * to avoid allocating a position in the commit mask.
270 commit_list_insert(commit
, &reusable
);
274 if (c_ent
->maximal
) {
276 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
277 bb
->commits
[bb
->commits_nr
++] = commit
;
281 struct bb_commit
*p_ent
= bb_data_at(&bb
->data
, p
->item
);
282 int c_not_p
, p_not_c
;
284 if (!p_ent
->commit_mask
) {
285 p_ent
->commit_mask
= bitmap_new();
289 c_not_p
= bitmap_is_subset(c_ent
->commit_mask
, p_ent
->commit_mask
);
290 p_not_c
= bitmap_is_subset(p_ent
->commit_mask
, c_ent
->commit_mask
);
296 bitmap_or(p_ent
->commit_mask
, c_ent
->commit_mask
);
302 free_commit_list(p_ent
->reverse_edges
);
303 p_ent
->reverse_edges
= NULL
;
306 if (c_ent
->maximal
) {
307 commit_list_insert(commit
, &p_ent
->reverse_edges
);
309 struct commit_list
*cc
= c_ent
->reverse_edges
;
311 for (; cc
; cc
= cc
->next
) {
312 if (!commit_list_contains(cc
->item
, p_ent
->reverse_edges
))
313 commit_list_insert(cc
->item
, &p_ent
->reverse_edges
);
319 bitmap_free(c_ent
->commit_mask
);
320 c_ent
->commit_mask
= NULL
;
323 for (r
= reusable
; r
; r
= r
->next
) {
324 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
325 bb
->commits
[bb
->commits_nr
++] = r
->item
;
328 trace2_data_intmax("pack-bitmap-write", the_repository
,
329 "num_selected_commits", writer
->selected_nr
);
330 trace2_data_intmax("pack-bitmap-write", the_repository
,
331 "num_maximal_commits", num_maximal
);
333 release_revisions(&revs
);
334 free_commit_list(reusable
);
337 static void bitmap_builder_clear(struct bitmap_builder
*bb
)
339 clear_bb_data(&bb
->data
);
341 bb
->commits_nr
= bb
->commits_alloc
= 0;
344 static int fill_bitmap_tree(struct bitmap
*bitmap
,
349 struct tree_desc desc
;
350 struct name_entry entry
;
353 * If our bit is already set, then there is nothing to do. Both this
354 * tree and all of its children will be set.
356 pos
= find_object_pos(&tree
->object
.oid
, &found
);
359 if (bitmap_get(bitmap
, pos
))
361 bitmap_set(bitmap
, pos
);
363 if (parse_tree(tree
) < 0)
364 die("unable to load tree object %s",
365 oid_to_hex(&tree
->object
.oid
));
366 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
368 while (tree_entry(&desc
, &entry
)) {
369 switch (object_type(entry
.mode
)) {
371 if (fill_bitmap_tree(bitmap
,
372 lookup_tree(the_repository
, &entry
.oid
)) < 0)
376 pos
= find_object_pos(&entry
.oid
, &found
);
379 bitmap_set(bitmap
, pos
);
382 /* Gitlink, etc; not reachable */
387 free_tree_buffer(tree
);
391 static int reused_bitmaps_nr
;
393 static int fill_bitmap_commit(struct bb_commit
*ent
,
394 struct commit
*commit
,
395 struct prio_queue
*queue
,
396 struct prio_queue
*tree_queue
,
397 struct bitmap_index
*old_bitmap
,
398 const uint32_t *mapping
)
403 ent
->bitmap
= bitmap_new();
405 prio_queue_put(queue
, commit
);
408 struct commit_list
*p
;
409 struct commit
*c
= prio_queue_get(queue
);
411 if (old_bitmap
&& mapping
) {
412 struct ewah_bitmap
*old
= bitmap_for_commit(old_bitmap
, c
);
414 * If this commit has an old bitmap, then translate that
415 * bitmap and add its bits to this one. No need to walk
416 * parents or the tree for this commit.
418 if (old
&& !rebuild_bitmap(mapping
, old
, ent
->bitmap
)) {
425 * Mark ourselves and queue our tree. The commit
426 * walk ensures we cover all parents.
428 pos
= find_object_pos(&c
->object
.oid
, &found
);
431 bitmap_set(ent
->bitmap
, pos
);
432 prio_queue_put(tree_queue
, get_commit_tree(c
));
434 for (p
= c
->parents
; p
; p
= p
->next
) {
435 pos
= find_object_pos(&p
->item
->object
.oid
, &found
);
438 if (!bitmap_get(ent
->bitmap
, pos
)) {
439 bitmap_set(ent
->bitmap
, pos
);
440 prio_queue_put(queue
, p
->item
);
445 while (tree_queue
->nr
) {
446 if (fill_bitmap_tree(ent
->bitmap
,
447 prio_queue_get(tree_queue
)) < 0)
453 static void store_selected(struct bb_commit
*ent
, struct commit
*commit
)
455 struct bitmapped_commit
*stored
= &writer
.selected
[ent
->idx
];
459 stored
->bitmap
= bitmap_to_ewah(ent
->bitmap
);
461 hash_pos
= kh_put_oid_map(writer
.bitmaps
, commit
->object
.oid
, &hash_ret
);
463 die("Duplicate entry when writing index: %s",
464 oid_to_hex(&commit
->object
.oid
));
465 kh_value(writer
.bitmaps
, hash_pos
) = stored
;
468 int bitmap_writer_build(struct packing_data
*to_pack
)
470 struct bitmap_builder bb
;
472 int nr_stored
= 0; /* for progress */
473 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
474 struct prio_queue tree_queue
= { NULL
};
475 struct bitmap_index
*old_bitmap
;
477 int closed
= 1; /* until proven otherwise */
479 writer
.bitmaps
= kh_init_oid_map();
480 writer
.to_pack
= to_pack
;
482 if (writer
.show_progress
)
483 writer
.progress
= start_progress("Building bitmaps", writer
.selected_nr
);
484 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
487 old_bitmap
= prepare_bitmap_git(to_pack
->repo
);
489 mapping
= create_bitmap_mapping(old_bitmap
, to_pack
);
493 bitmap_builder_init(&bb
, &writer
, old_bitmap
);
494 for (i
= bb
.commits_nr
; i
> 0; i
--) {
495 struct commit
*commit
= bb
.commits
[i
-1];
496 struct bb_commit
*ent
= bb_data_at(&bb
.data
, commit
);
497 struct commit
*child
;
500 if (fill_bitmap_commit(ent
, commit
, &queue
, &tree_queue
,
501 old_bitmap
, mapping
) < 0) {
507 store_selected(ent
, commit
);
509 display_progress(writer
.progress
, nr_stored
);
512 while ((child
= pop_commit(&ent
->reverse_edges
))) {
513 struct bb_commit
*child_ent
=
514 bb_data_at(&bb
.data
, child
);
516 if (child_ent
->bitmap
)
517 bitmap_or(child_ent
->bitmap
, ent
->bitmap
);
519 child_ent
->bitmap
= bitmap_dup(ent
->bitmap
);
521 child_ent
->bitmap
= ent
->bitmap
;
526 bitmap_free(ent
->bitmap
);
529 clear_prio_queue(&queue
);
530 clear_prio_queue(&tree_queue
);
531 bitmap_builder_clear(&bb
);
532 free_bitmap_index(old_bitmap
);
535 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
537 trace2_data_intmax("pack-bitmap-write", the_repository
,
538 "building_bitmaps_reused", reused_bitmaps_nr
);
540 stop_progress(&writer
.progress
);
543 compute_xor_offsets();
544 return closed
? 0 : -1;
548 * Select the commits that will be bitmapped
550 static inline unsigned int next_commit_index(unsigned int idx
)
552 static const unsigned int MIN_COMMITS
= 100;
553 static const unsigned int MAX_COMMITS
= 5000;
555 static const unsigned int MUST_REGION
= 100;
556 static const unsigned int MIN_REGION
= 20000;
558 unsigned int offset
, next
;
560 if (idx
<= MUST_REGION
)
563 if (idx
<= MIN_REGION
) {
564 offset
= idx
- MUST_REGION
;
565 return (offset
< MIN_COMMITS
) ? offset
: MIN_COMMITS
;
568 offset
= idx
- MIN_REGION
;
569 next
= (offset
< MAX_COMMITS
) ? offset
: MAX_COMMITS
;
571 return (next
> MIN_COMMITS
) ? next
: MIN_COMMITS
;
574 static int date_compare(const void *_a
, const void *_b
)
576 struct commit
*a
= *(struct commit
**)_a
;
577 struct commit
*b
= *(struct commit
**)_b
;
578 return (long)b
->date
- (long)a
->date
;
581 void bitmap_writer_select_commits(struct commit
**indexed_commits
,
582 unsigned int indexed_commits_nr
,
585 unsigned int i
= 0, j
, next
;
587 QSORT(indexed_commits
, indexed_commits_nr
, date_compare
);
589 if (indexed_commits_nr
< 100) {
590 for (i
= 0; i
< indexed_commits_nr
; ++i
)
591 push_bitmapped_commit(indexed_commits
[i
]);
595 if (writer
.show_progress
)
596 writer
.progress
= start_progress("Selecting bitmap commits", 0);
599 struct commit
*chosen
= NULL
;
601 next
= next_commit_index(i
);
603 if (i
+ next
>= indexed_commits_nr
)
606 if (max_bitmaps
> 0 && writer
.selected_nr
>= max_bitmaps
) {
607 writer
.selected_nr
= max_bitmaps
;
612 chosen
= indexed_commits
[i
];
614 chosen
= indexed_commits
[i
+ next
];
616 for (j
= 0; j
<= next
; ++j
) {
617 struct commit
*cm
= indexed_commits
[i
+ j
];
619 if ((cm
->object
.flags
& NEEDS_BITMAP
) != 0) {
624 if (cm
->parents
&& cm
->parents
->next
)
629 push_bitmapped_commit(chosen
);
632 display_progress(writer
.progress
, i
);
635 stop_progress(&writer
.progress
);
639 static int hashwrite_ewah_helper(void *f
, const void *buf
, size_t len
)
641 /* hashwrite will die on error */
642 hashwrite(f
, buf
, len
);
647 * Write the bitmap index to disk
649 static inline void dump_bitmap(struct hashfile
*f
, struct ewah_bitmap
*bitmap
)
651 if (ewah_serialize_to(bitmap
, hashwrite_ewah_helper
, f
) < 0)
652 die("Failed to write bitmap index");
655 static const struct object_id
*oid_access(size_t pos
, const void *table
)
657 const struct pack_idx_entry
* const *index
= table
;
658 return &index
[pos
]->oid
;
661 static void write_selected_commits_v1(struct hashfile
*f
,
662 uint32_t *commit_positions
,
667 for (i
= 0; i
< writer
.selected_nr
; ++i
) {
668 struct bitmapped_commit
*stored
= &writer
.selected
[i
];
671 offsets
[i
] = hashfile_total(f
);
673 hashwrite_be32(f
, commit_positions
[i
]);
674 hashwrite_u8(f
, stored
->xor_offset
);
675 hashwrite_u8(f
, stored
->flags
);
677 dump_bitmap(f
, stored
->write_as
);
681 static int table_cmp(const void *_va
, const void *_vb
, void *_data
)
683 uint32_t *commit_positions
= _data
;
684 uint32_t a
= commit_positions
[*(uint32_t *)_va
];
685 uint32_t b
= commit_positions
[*(uint32_t *)_vb
];
695 static void write_lookup_table(struct hashfile
*f
,
696 uint32_t *commit_positions
,
700 uint32_t *table
, *table_inv
;
702 ALLOC_ARRAY(table
, writer
.selected_nr
);
703 ALLOC_ARRAY(table_inv
, writer
.selected_nr
);
705 for (i
= 0; i
< writer
.selected_nr
; i
++)
709 * At the end of this sort table[j] = i means that the i'th
710 * bitmap corresponds to j'th bitmapped commit (among the selected
711 * commits) in lex order of OIDs.
713 QSORT_S(table
, writer
.selected_nr
, table_cmp
, commit_positions
);
715 /* table_inv helps us discover that relationship (i'th bitmap
716 * to j'th commit by j = table_inv[i])
718 for (i
= 0; i
< writer
.selected_nr
; i
++)
719 table_inv
[table
[i
]] = i
;
721 trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository
);
722 for (i
= 0; i
< writer
.selected_nr
; i
++) {
723 struct bitmapped_commit
*selected
= &writer
.selected
[table
[i
]];
724 uint32_t xor_offset
= selected
->xor_offset
;
729 * xor_index stores the index (in the bitmap entries)
730 * of the corresponding xor bitmap. But we need to convert
731 * this index into lookup table's index. So, table_inv[xor_index]
732 * gives us the index position w.r.t. the lookup table.
734 * If "k = table[i] - xor_offset" then the xor base is the k'th
735 * bitmap. `table_inv[k]` gives us the position of that bitmap
736 * in the lookup table.
738 uint32_t xor_index
= table
[i
] - xor_offset
;
739 xor_row
= table_inv
[xor_index
];
741 xor_row
= 0xffffffff;
744 hashwrite_be32(f
, commit_positions
[table
[i
]]);
745 hashwrite_be64(f
, (uint64_t)offsets
[table
[i
]]);
746 hashwrite_be32(f
, xor_row
);
748 trace2_region_leave("pack-bitmap-write", "writing_lookup_table", the_repository
);
754 static void write_hash_cache(struct hashfile
*f
,
755 struct pack_idx_entry
**index
,
760 for (i
= 0; i
< index_nr
; ++i
) {
761 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
762 hashwrite_be32(f
, entry
->hash
);
766 void bitmap_writer_set_checksum(const unsigned char *sha1
)
768 hashcpy(writer
.pack_checksum
, sha1
);
771 void bitmap_writer_finish(struct pack_idx_entry
**index
,
773 const char *filename
,
776 static uint16_t default_version
= 1;
777 static uint16_t flags
= BITMAP_OPT_FULL_DAG
;
778 struct strbuf tmp_file
= STRBUF_INIT
;
780 uint32_t *commit_positions
= NULL
;
781 off_t
*offsets
= NULL
;
784 struct bitmap_disk_header header
;
786 int fd
= odb_mkstemp(&tmp_file
, "pack/tmp_bitmap_XXXXXX");
788 f
= hashfd(fd
, tmp_file
.buf
);
790 memcpy(header
.magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
));
791 header
.version
= htons(default_version
);
792 header
.options
= htons(flags
| options
);
793 header
.entry_count
= htonl(writer
.selected_nr
);
794 hashcpy(header
.checksum
, writer
.pack_checksum
);
796 hashwrite(f
, &header
, sizeof(header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
);
797 dump_bitmap(f
, writer
.commits
);
798 dump_bitmap(f
, writer
.trees
);
799 dump_bitmap(f
, writer
.blobs
);
800 dump_bitmap(f
, writer
.tags
);
802 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
803 CALLOC_ARRAY(offsets
, index_nr
);
805 ALLOC_ARRAY(commit_positions
, writer
.selected_nr
);
807 for (i
= 0; i
< writer
.selected_nr
; i
++) {
808 struct bitmapped_commit
*stored
= &writer
.selected
[i
];
809 int commit_pos
= oid_pos(&stored
->commit
->object
.oid
, index
, index_nr
, oid_access
);
812 BUG(_("trying to write commit not in index"));
814 commit_positions
[i
] = commit_pos
;
817 write_selected_commits_v1(f
, commit_positions
, offsets
);
819 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
820 write_lookup_table(f
, commit_positions
, offsets
);
822 if (options
& BITMAP_OPT_HASH_CACHE
)
823 write_hash_cache(f
, index
, index_nr
);
825 finalize_hashfile(f
, NULL
, FSYNC_COMPONENT_PACK_METADATA
,
826 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
828 if (adjust_shared_perm(tmp_file
.buf
))
829 die_errno("unable to make temporary bitmap file readable");
831 if (rename(tmp_file
.buf
, filename
))
832 die_errno("unable to rename temporary bitmap file to '%s'", filename
);
834 strbuf_release(&tmp_file
);
835 free(commit_positions
);