2 #include "object-store.h"
7 #include "list-objects.h"
9 #include "pack-revindex.h"
11 #include "pack-bitmap.h"
12 #include "hash-lookup.h"
13 #include "pack-objects.h"
14 #include "commit-reach.h"
15 #include "prio-queue.h"
17 struct bitmapped_commit
{
18 struct commit
*commit
;
19 struct ewah_bitmap
*bitmap
;
20 struct ewah_bitmap
*write_as
;
26 struct bitmap_writer
{
27 struct ewah_bitmap
*commits
;
28 struct ewah_bitmap
*trees
;
29 struct ewah_bitmap
*blobs
;
30 struct ewah_bitmap
*tags
;
32 kh_oid_map_t
*bitmaps
;
33 struct packing_data
*to_pack
;
35 struct bitmapped_commit
*selected
;
36 unsigned int selected_nr
, selected_alloc
;
38 struct progress
*progress
;
40 unsigned char pack_checksum
[GIT_MAX_RAWSZ
];
43 static struct bitmap_writer writer
;
45 void bitmap_writer_show_progress(int show
)
47 writer
.show_progress
= show
;
51 * Build the initial type index for the packfile or multi-pack-index
53 void bitmap_writer_build_type_index(struct packing_data
*to_pack
,
54 struct pack_idx_entry
**index
,
59 writer
.commits
= ewah_new();
60 writer
.trees
= ewah_new();
61 writer
.blobs
= ewah_new();
62 writer
.tags
= ewah_new();
63 ALLOC_ARRAY(to_pack
->in_pack_pos
, to_pack
->nr_objects
);
65 for (i
= 0; i
< index_nr
; ++i
) {
66 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
67 enum object_type real_type
;
69 oe_set_in_pack_pos(to_pack
, entry
, i
);
71 switch (oe_type(entry
)) {
76 real_type
= oe_type(entry
);
80 real_type
= oid_object_info(to_pack
->repo
,
81 &entry
->idx
.oid
, NULL
);
87 ewah_set(writer
.commits
, i
);
91 ewah_set(writer
.trees
, i
);
95 ewah_set(writer
.blobs
, i
);
99 ewah_set(writer
.tags
, i
);
103 die("Missing type information for %s (%d/%d)",
104 oid_to_hex(&entry
->idx
.oid
), real_type
,
111 * Compute the actual bitmaps
114 static inline void push_bitmapped_commit(struct commit
*commit
)
116 if (writer
.selected_nr
>= writer
.selected_alloc
) {
117 writer
.selected_alloc
= (writer
.selected_alloc
+ 32) * 2;
118 REALLOC_ARRAY(writer
.selected
, writer
.selected_alloc
);
121 writer
.selected
[writer
.selected_nr
].commit
= commit
;
122 writer
.selected
[writer
.selected_nr
].bitmap
= NULL
;
123 writer
.selected
[writer
.selected_nr
].flags
= 0;
125 writer
.selected_nr
++;
128 static uint32_t find_object_pos(const struct object_id
*oid
, int *found
)
130 struct object_entry
*entry
= packlist_find(writer
.to_pack
, oid
);
135 warning("Failed to write bitmap index. Packfile doesn't have full closure "
136 "(object %s is missing)", oid_to_hex(oid
));
142 return oe_in_pack_pos(writer
.to_pack
, entry
);
145 static void compute_xor_offsets(void)
147 static const int MAX_XOR_OFFSET_SEARCH
= 10;
151 while (next
< writer
.selected_nr
) {
152 struct bitmapped_commit
*stored
= &writer
.selected
[next
];
155 struct ewah_bitmap
*best_bitmap
= stored
->bitmap
;
156 struct ewah_bitmap
*test_xor
;
158 for (i
= 1; i
<= MAX_XOR_OFFSET_SEARCH
; ++i
) {
164 test_xor
= ewah_pool_new();
165 ewah_xor(writer
.selected
[curr
].bitmap
, stored
->bitmap
, test_xor
);
167 if (test_xor
->buffer_size
< best_bitmap
->buffer_size
) {
168 if (best_bitmap
!= stored
->bitmap
)
169 ewah_pool_free(best_bitmap
);
171 best_bitmap
= test_xor
;
174 ewah_pool_free(test_xor
);
178 stored
->xor_offset
= best_offset
;
179 stored
->write_as
= best_bitmap
;
186 struct commit_list
*reverse_edges
;
187 struct bitmap
*commit_mask
;
188 struct bitmap
*bitmap
;
191 unsigned idx
; /* within selected array */
194 define_commit_slab(bb_data
, struct bb_commit
);
196 struct bitmap_builder
{
198 struct commit
**commits
;
199 size_t commits_nr
, commits_alloc
;
202 static void bitmap_builder_init(struct bitmap_builder
*bb
,
203 struct bitmap_writer
*writer
,
204 struct bitmap_index
*old_bitmap
)
206 struct rev_info revs
;
207 struct commit
*commit
;
208 struct commit_list
*reusable
= NULL
;
209 struct commit_list
*r
;
210 unsigned int i
, num_maximal
= 0;
212 memset(bb
, 0, sizeof(*bb
));
213 init_bb_data(&bb
->data
);
215 reset_revision_walk();
216 repo_init_revisions(writer
->to_pack
->repo
, &revs
, NULL
);
218 revs
.first_parent_only
= 1;
220 for (i
= 0; i
< writer
->selected_nr
; i
++) {
221 struct commit
*c
= writer
->selected
[i
].commit
;
222 struct bb_commit
*ent
= bb_data_at(&bb
->data
, c
);
228 ent
->commit_mask
= bitmap_new();
229 bitmap_set(ent
->commit_mask
, i
);
231 add_pending_object(&revs
, &c
->object
, "");
234 if (prepare_revision_walk(&revs
))
235 die("revision walk setup failed");
237 while ((commit
= get_revision(&revs
))) {
238 struct commit_list
*p
= commit
->parents
;
239 struct bb_commit
*c_ent
;
241 parse_commit_or_die(commit
);
243 c_ent
= bb_data_at(&bb
->data
, commit
);
246 * If there is no commit_mask, there is no reason to iterate
247 * over this commit; it is not selected (if it were, it would
248 * not have a blank commit mask) and all its children have
249 * existing bitmaps (see the comment starting with "This commit
250 * has an existing bitmap" below), so it does not contribute
251 * anything to the final bitmap file or its descendants.
253 if (!c_ent
->commit_mask
)
256 if (old_bitmap
&& bitmap_for_commit(old_bitmap
, commit
)) {
258 * This commit has an existing bitmap, so we can
259 * get its bits immediately without an object
260 * walk. That is, it is reusable as-is and there is no
261 * need to continue walking beyond it.
263 * Mark it as such and add it to bb->commits separately
264 * to avoid allocating a position in the commit mask.
266 commit_list_insert(commit
, &reusable
);
270 if (c_ent
->maximal
) {
272 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
273 bb
->commits
[bb
->commits_nr
++] = commit
;
277 struct bb_commit
*p_ent
= bb_data_at(&bb
->data
, p
->item
);
278 int c_not_p
, p_not_c
;
280 if (!p_ent
->commit_mask
) {
281 p_ent
->commit_mask
= bitmap_new();
285 c_not_p
= bitmap_is_subset(c_ent
->commit_mask
, p_ent
->commit_mask
);
286 p_not_c
= bitmap_is_subset(p_ent
->commit_mask
, c_ent
->commit_mask
);
292 bitmap_or(p_ent
->commit_mask
, c_ent
->commit_mask
);
298 free_commit_list(p_ent
->reverse_edges
);
299 p_ent
->reverse_edges
= NULL
;
302 if (c_ent
->maximal
) {
303 commit_list_insert(commit
, &p_ent
->reverse_edges
);
305 struct commit_list
*cc
= c_ent
->reverse_edges
;
307 for (; cc
; cc
= cc
->next
) {
308 if (!commit_list_contains(cc
->item
, p_ent
->reverse_edges
))
309 commit_list_insert(cc
->item
, &p_ent
->reverse_edges
);
315 bitmap_free(c_ent
->commit_mask
);
316 c_ent
->commit_mask
= NULL
;
319 for (r
= reusable
; r
; r
= r
->next
) {
320 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
321 bb
->commits
[bb
->commits_nr
++] = r
->item
;
324 trace2_data_intmax("pack-bitmap-write", the_repository
,
325 "num_selected_commits", writer
->selected_nr
);
326 trace2_data_intmax("pack-bitmap-write", the_repository
,
327 "num_maximal_commits", num_maximal
);
329 release_revisions(&revs
);
330 free_commit_list(reusable
);
333 static void bitmap_builder_clear(struct bitmap_builder
*bb
)
335 clear_bb_data(&bb
->data
);
337 bb
->commits_nr
= bb
->commits_alloc
= 0;
340 static int fill_bitmap_tree(struct bitmap
*bitmap
,
345 struct tree_desc desc
;
346 struct name_entry entry
;
349 * If our bit is already set, then there is nothing to do. Both this
350 * tree and all of its children will be set.
352 pos
= find_object_pos(&tree
->object
.oid
, &found
);
355 if (bitmap_get(bitmap
, pos
))
357 bitmap_set(bitmap
, pos
);
359 if (parse_tree(tree
) < 0)
360 die("unable to load tree object %s",
361 oid_to_hex(&tree
->object
.oid
));
362 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
364 while (tree_entry(&desc
, &entry
)) {
365 switch (object_type(entry
.mode
)) {
367 if (fill_bitmap_tree(bitmap
,
368 lookup_tree(the_repository
, &entry
.oid
)) < 0)
372 pos
= find_object_pos(&entry
.oid
, &found
);
375 bitmap_set(bitmap
, pos
);
378 /* Gitlink, etc; not reachable */
383 free_tree_buffer(tree
);
387 static int reused_bitmaps_nr
;
389 static int fill_bitmap_commit(struct bb_commit
*ent
,
390 struct commit
*commit
,
391 struct prio_queue
*queue
,
392 struct prio_queue
*tree_queue
,
393 struct bitmap_index
*old_bitmap
,
394 const uint32_t *mapping
)
399 ent
->bitmap
= bitmap_new();
401 prio_queue_put(queue
, commit
);
404 struct commit_list
*p
;
405 struct commit
*c
= prio_queue_get(queue
);
407 if (old_bitmap
&& mapping
) {
408 struct ewah_bitmap
*old
= bitmap_for_commit(old_bitmap
, c
);
410 * If this commit has an old bitmap, then translate that
411 * bitmap and add its bits to this one. No need to walk
412 * parents or the tree for this commit.
414 if (old
&& !rebuild_bitmap(mapping
, old
, ent
->bitmap
)) {
421 * Mark ourselves and queue our tree. The commit
422 * walk ensures we cover all parents.
424 pos
= find_object_pos(&c
->object
.oid
, &found
);
427 bitmap_set(ent
->bitmap
, pos
);
428 prio_queue_put(tree_queue
, get_commit_tree(c
));
430 for (p
= c
->parents
; p
; p
= p
->next
) {
431 pos
= find_object_pos(&p
->item
->object
.oid
, &found
);
434 if (!bitmap_get(ent
->bitmap
, pos
)) {
435 bitmap_set(ent
->bitmap
, pos
);
436 prio_queue_put(queue
, p
->item
);
441 while (tree_queue
->nr
) {
442 if (fill_bitmap_tree(ent
->bitmap
,
443 prio_queue_get(tree_queue
)) < 0)
449 static void store_selected(struct bb_commit
*ent
, struct commit
*commit
)
451 struct bitmapped_commit
*stored
= &writer
.selected
[ent
->idx
];
455 stored
->bitmap
= bitmap_to_ewah(ent
->bitmap
);
457 hash_pos
= kh_put_oid_map(writer
.bitmaps
, commit
->object
.oid
, &hash_ret
);
459 die("Duplicate entry when writing index: %s",
460 oid_to_hex(&commit
->object
.oid
));
461 kh_value(writer
.bitmaps
, hash_pos
) = stored
;
464 int bitmap_writer_build(struct packing_data
*to_pack
)
466 struct bitmap_builder bb
;
468 int nr_stored
= 0; /* for progress */
469 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
470 struct prio_queue tree_queue
= { NULL
};
471 struct bitmap_index
*old_bitmap
;
473 int closed
= 1; /* until proven otherwise */
475 writer
.bitmaps
= kh_init_oid_map();
476 writer
.to_pack
= to_pack
;
478 if (writer
.show_progress
)
479 writer
.progress
= start_progress("Building bitmaps", writer
.selected_nr
);
480 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
483 old_bitmap
= prepare_bitmap_git(to_pack
->repo
);
485 mapping
= create_bitmap_mapping(old_bitmap
, to_pack
);
489 bitmap_builder_init(&bb
, &writer
, old_bitmap
);
490 for (i
= bb
.commits_nr
; i
> 0; i
--) {
491 struct commit
*commit
= bb
.commits
[i
-1];
492 struct bb_commit
*ent
= bb_data_at(&bb
.data
, commit
);
493 struct commit
*child
;
496 if (fill_bitmap_commit(ent
, commit
, &queue
, &tree_queue
,
497 old_bitmap
, mapping
) < 0) {
503 store_selected(ent
, commit
);
505 display_progress(writer
.progress
, nr_stored
);
508 while ((child
= pop_commit(&ent
->reverse_edges
))) {
509 struct bb_commit
*child_ent
=
510 bb_data_at(&bb
.data
, child
);
512 if (child_ent
->bitmap
)
513 bitmap_or(child_ent
->bitmap
, ent
->bitmap
);
515 child_ent
->bitmap
= bitmap_dup(ent
->bitmap
);
517 child_ent
->bitmap
= ent
->bitmap
;
522 bitmap_free(ent
->bitmap
);
525 clear_prio_queue(&queue
);
526 clear_prio_queue(&tree_queue
);
527 bitmap_builder_clear(&bb
);
528 free_bitmap_index(old_bitmap
);
531 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
533 trace2_data_intmax("pack-bitmap-write", the_repository
,
534 "building_bitmaps_reused", reused_bitmaps_nr
);
536 stop_progress(&writer
.progress
);
539 compute_xor_offsets();
540 return closed
? 0 : -1;
544 * Select the commits that will be bitmapped
546 static inline unsigned int next_commit_index(unsigned int idx
)
548 static const unsigned int MIN_COMMITS
= 100;
549 static const unsigned int MAX_COMMITS
= 5000;
551 static const unsigned int MUST_REGION
= 100;
552 static const unsigned int MIN_REGION
= 20000;
554 unsigned int offset
, next
;
556 if (idx
<= MUST_REGION
)
559 if (idx
<= MIN_REGION
) {
560 offset
= idx
- MUST_REGION
;
561 return (offset
< MIN_COMMITS
) ? offset
: MIN_COMMITS
;
564 offset
= idx
- MIN_REGION
;
565 next
= (offset
< MAX_COMMITS
) ? offset
: MAX_COMMITS
;
567 return (next
> MIN_COMMITS
) ? next
: MIN_COMMITS
;
570 static int date_compare(const void *_a
, const void *_b
)
572 struct commit
*a
= *(struct commit
**)_a
;
573 struct commit
*b
= *(struct commit
**)_b
;
574 return (long)b
->date
- (long)a
->date
;
577 void bitmap_writer_select_commits(struct commit
**indexed_commits
,
578 unsigned int indexed_commits_nr
,
581 unsigned int i
= 0, j
, next
;
583 QSORT(indexed_commits
, indexed_commits_nr
, date_compare
);
585 if (indexed_commits_nr
< 100) {
586 for (i
= 0; i
< indexed_commits_nr
; ++i
)
587 push_bitmapped_commit(indexed_commits
[i
]);
591 if (writer
.show_progress
)
592 writer
.progress
= start_progress("Selecting bitmap commits", 0);
595 struct commit
*chosen
= NULL
;
597 next
= next_commit_index(i
);
599 if (i
+ next
>= indexed_commits_nr
)
602 if (max_bitmaps
> 0 && writer
.selected_nr
>= max_bitmaps
) {
603 writer
.selected_nr
= max_bitmaps
;
608 chosen
= indexed_commits
[i
];
610 chosen
= indexed_commits
[i
+ next
];
612 for (j
= 0; j
<= next
; ++j
) {
613 struct commit
*cm
= indexed_commits
[i
+ j
];
615 if ((cm
->object
.flags
& NEEDS_BITMAP
) != 0) {
620 if (cm
->parents
&& cm
->parents
->next
)
625 push_bitmapped_commit(chosen
);
628 display_progress(writer
.progress
, i
);
631 stop_progress(&writer
.progress
);
635 static int hashwrite_ewah_helper(void *f
, const void *buf
, size_t len
)
637 /* hashwrite will die on error */
638 hashwrite(f
, buf
, len
);
643 * Write the bitmap index to disk
645 static inline void dump_bitmap(struct hashfile
*f
, struct ewah_bitmap
*bitmap
)
647 if (ewah_serialize_to(bitmap
, hashwrite_ewah_helper
, f
) < 0)
648 die("Failed to write bitmap index");
651 static const struct object_id
*oid_access(size_t pos
, const void *table
)
653 const struct pack_idx_entry
* const *index
= table
;
654 return &index
[pos
]->oid
;
657 static void write_selected_commits_v1(struct hashfile
*f
,
658 uint32_t *commit_positions
,
663 for (i
= 0; i
< writer
.selected_nr
; ++i
) {
664 struct bitmapped_commit
*stored
= &writer
.selected
[i
];
667 offsets
[i
] = hashfile_total(f
);
669 hashwrite_be32(f
, commit_positions
[i
]);
670 hashwrite_u8(f
, stored
->xor_offset
);
671 hashwrite_u8(f
, stored
->flags
);
673 dump_bitmap(f
, stored
->write_as
);
677 static int table_cmp(const void *_va
, const void *_vb
, void *_data
)
679 uint32_t *commit_positions
= _data
;
680 uint32_t a
= commit_positions
[*(uint32_t *)_va
];
681 uint32_t b
= commit_positions
[*(uint32_t *)_vb
];
691 static void write_lookup_table(struct hashfile
*f
,
692 uint32_t *commit_positions
,
696 uint32_t *table
, *table_inv
;
698 ALLOC_ARRAY(table
, writer
.selected_nr
);
699 ALLOC_ARRAY(table_inv
, writer
.selected_nr
);
701 for (i
= 0; i
< writer
.selected_nr
; i
++)
705 * At the end of this sort table[j] = i means that the i'th
706 * bitmap corresponds to j'th bitmapped commit (among the selected
707 * commits) in lex order of OIDs.
709 QSORT_S(table
, writer
.selected_nr
, table_cmp
, commit_positions
);
711 /* table_inv helps us discover that relationship (i'th bitmap
712 * to j'th commit by j = table_inv[i])
714 for (i
= 0; i
< writer
.selected_nr
; i
++)
715 table_inv
[table
[i
]] = i
;
717 trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository
);
718 for (i
= 0; i
< writer
.selected_nr
; i
++) {
719 struct bitmapped_commit
*selected
= &writer
.selected
[table
[i
]];
720 uint32_t xor_offset
= selected
->xor_offset
;
725 * xor_index stores the index (in the bitmap entries)
726 * of the corresponding xor bitmap. But we need to convert
727 * this index into lookup table's index. So, table_inv[xor_index]
728 * gives us the index position w.r.t. the lookup table.
730 * If "k = table[i] - xor_offset" then the xor base is the k'th
731 * bitmap. `table_inv[k]` gives us the position of that bitmap
732 * in the lookup table.
734 uint32_t xor_index
= table
[i
] - xor_offset
;
735 xor_row
= table_inv
[xor_index
];
737 xor_row
= 0xffffffff;
740 hashwrite_be32(f
, commit_positions
[table
[i
]]);
741 hashwrite_be64(f
, (uint64_t)offsets
[table
[i
]]);
742 hashwrite_be32(f
, xor_row
);
744 trace2_region_leave("pack-bitmap-write", "writing_lookup_table", the_repository
);
750 static void write_hash_cache(struct hashfile
*f
,
751 struct pack_idx_entry
**index
,
756 for (i
= 0; i
< index_nr
; ++i
) {
757 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
758 hashwrite_be32(f
, entry
->hash
);
762 void bitmap_writer_set_checksum(const unsigned char *sha1
)
764 hashcpy(writer
.pack_checksum
, sha1
);
767 void bitmap_writer_finish(struct pack_idx_entry
**index
,
769 const char *filename
,
772 static uint16_t default_version
= 1;
773 static uint16_t flags
= BITMAP_OPT_FULL_DAG
;
774 struct strbuf tmp_file
= STRBUF_INIT
;
776 uint32_t *commit_positions
= NULL
;
777 off_t
*offsets
= NULL
;
780 struct bitmap_disk_header header
;
782 int fd
= odb_mkstemp(&tmp_file
, "pack/tmp_bitmap_XXXXXX");
784 f
= hashfd(fd
, tmp_file
.buf
);
786 memcpy(header
.magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
));
787 header
.version
= htons(default_version
);
788 header
.options
= htons(flags
| options
);
789 header
.entry_count
= htonl(writer
.selected_nr
);
790 hashcpy(header
.checksum
, writer
.pack_checksum
);
792 hashwrite(f
, &header
, sizeof(header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
);
793 dump_bitmap(f
, writer
.commits
);
794 dump_bitmap(f
, writer
.trees
);
795 dump_bitmap(f
, writer
.blobs
);
796 dump_bitmap(f
, writer
.tags
);
798 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
799 CALLOC_ARRAY(offsets
, index_nr
);
801 ALLOC_ARRAY(commit_positions
, writer
.selected_nr
);
803 for (i
= 0; i
< writer
.selected_nr
; i
++) {
804 struct bitmapped_commit
*stored
= &writer
.selected
[i
];
805 int commit_pos
= oid_pos(&stored
->commit
->object
.oid
, index
, index_nr
, oid_access
);
808 BUG(_("trying to write commit not in index"));
810 commit_positions
[i
] = commit_pos
;
813 write_selected_commits_v1(f
, commit_positions
, offsets
);
815 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
816 write_lookup_table(f
, commit_positions
, offsets
);
818 if (options
& BITMAP_OPT_HASH_CACHE
)
819 write_hash_cache(f
, index
, index_nr
);
821 finalize_hashfile(f
, NULL
, FSYNC_COMPONENT_PACK_METADATA
,
822 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
824 if (adjust_shared_perm(tmp_file
.buf
))
825 die_errno("unable to make temporary bitmap file readable");
827 if (rename(tmp_file
.buf
, filename
))
828 die_errno("unable to rename temporary bitmap file to '%s'", filename
);
830 strbuf_release(&tmp_file
);
831 free(commit_positions
);