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"
22 struct bitmapped_commit
{
23 struct commit
*commit
;
24 struct ewah_bitmap
*bitmap
;
25 struct ewah_bitmap
*write_as
;
31 struct bitmap_writer
{
32 struct ewah_bitmap
*commits
;
33 struct ewah_bitmap
*trees
;
34 struct ewah_bitmap
*blobs
;
35 struct ewah_bitmap
*tags
;
37 kh_oid_map_t
*bitmaps
;
38 struct packing_data
*to_pack
;
40 struct bitmapped_commit
*selected
;
41 unsigned int selected_nr
, selected_alloc
;
43 struct progress
*progress
;
45 unsigned char pack_checksum
[GIT_MAX_RAWSZ
];
48 static struct bitmap_writer writer
;
50 void bitmap_writer_show_progress(int show
)
52 writer
.show_progress
= show
;
56 * Build the initial type index for the packfile or multi-pack-index
58 void bitmap_writer_build_type_index(struct packing_data
*to_pack
,
59 struct pack_idx_entry
**index
,
64 writer
.commits
= ewah_new();
65 writer
.trees
= ewah_new();
66 writer
.blobs
= ewah_new();
67 writer
.tags
= ewah_new();
68 ALLOC_ARRAY(to_pack
->in_pack_pos
, to_pack
->nr_objects
);
70 for (i
= 0; i
< index_nr
; ++i
) {
71 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
72 enum object_type real_type
;
74 oe_set_in_pack_pos(to_pack
, entry
, i
);
76 switch (oe_type(entry
)) {
81 real_type
= oe_type(entry
);
85 real_type
= oid_object_info(to_pack
->repo
,
86 &entry
->idx
.oid
, NULL
);
92 ewah_set(writer
.commits
, i
);
96 ewah_set(writer
.trees
, i
);
100 ewah_set(writer
.blobs
, i
);
104 ewah_set(writer
.tags
, i
);
108 die("Missing type information for %s (%d/%d)",
109 oid_to_hex(&entry
->idx
.oid
), real_type
,
116 * Compute the actual bitmaps
119 static inline void push_bitmapped_commit(struct commit
*commit
)
121 if (writer
.selected_nr
>= writer
.selected_alloc
) {
122 writer
.selected_alloc
= (writer
.selected_alloc
+ 32) * 2;
123 REALLOC_ARRAY(writer
.selected
, writer
.selected_alloc
);
126 writer
.selected
[writer
.selected_nr
].commit
= commit
;
127 writer
.selected
[writer
.selected_nr
].bitmap
= NULL
;
128 writer
.selected
[writer
.selected_nr
].flags
= 0;
130 writer
.selected_nr
++;
133 static uint32_t find_object_pos(const struct object_id
*oid
, int *found
)
135 struct object_entry
*entry
= packlist_find(writer
.to_pack
, oid
);
140 warning("Failed to write bitmap index. Packfile doesn't have full closure "
141 "(object %s is missing)", oid_to_hex(oid
));
147 return oe_in_pack_pos(writer
.to_pack
, entry
);
150 static void compute_xor_offsets(void)
152 static const int MAX_XOR_OFFSET_SEARCH
= 10;
156 while (next
< writer
.selected_nr
) {
157 struct bitmapped_commit
*stored
= &writer
.selected
[next
];
160 struct ewah_bitmap
*best_bitmap
= stored
->bitmap
;
161 struct ewah_bitmap
*test_xor
;
163 for (i
= 1; i
<= MAX_XOR_OFFSET_SEARCH
; ++i
) {
169 test_xor
= ewah_pool_new();
170 ewah_xor(writer
.selected
[curr
].bitmap
, stored
->bitmap
, test_xor
);
172 if (test_xor
->buffer_size
< best_bitmap
->buffer_size
) {
173 if (best_bitmap
!= stored
->bitmap
)
174 ewah_pool_free(best_bitmap
);
176 best_bitmap
= test_xor
;
179 ewah_pool_free(test_xor
);
183 stored
->xor_offset
= best_offset
;
184 stored
->write_as
= best_bitmap
;
191 struct commit_list
*reverse_edges
;
192 struct bitmap
*commit_mask
;
193 struct bitmap
*bitmap
;
196 unsigned idx
; /* within selected array */
199 define_commit_slab(bb_data
, struct bb_commit
);
201 struct bitmap_builder
{
203 struct commit
**commits
;
204 size_t commits_nr
, commits_alloc
;
207 static void bitmap_builder_init(struct bitmap_builder
*bb
,
208 struct bitmap_writer
*writer
,
209 struct bitmap_index
*old_bitmap
)
211 struct rev_info revs
;
212 struct commit
*commit
;
213 struct commit_list
*reusable
= NULL
;
214 struct commit_list
*r
;
215 unsigned int i
, num_maximal
= 0;
217 memset(bb
, 0, sizeof(*bb
));
218 init_bb_data(&bb
->data
);
220 reset_revision_walk();
221 repo_init_revisions(writer
->to_pack
->repo
, &revs
, NULL
);
223 revs
.first_parent_only
= 1;
225 for (i
= 0; i
< writer
->selected_nr
; i
++) {
226 struct commit
*c
= writer
->selected
[i
].commit
;
227 struct bb_commit
*ent
= bb_data_at(&bb
->data
, c
);
233 ent
->commit_mask
= bitmap_new();
234 bitmap_set(ent
->commit_mask
, i
);
236 add_pending_object(&revs
, &c
->object
, "");
239 if (prepare_revision_walk(&revs
))
240 die("revision walk setup failed");
242 while ((commit
= get_revision(&revs
))) {
243 struct commit_list
*p
= commit
->parents
;
244 struct bb_commit
*c_ent
;
246 parse_commit_or_die(commit
);
248 c_ent
= bb_data_at(&bb
->data
, commit
);
251 * If there is no commit_mask, there is no reason to iterate
252 * over this commit; it is not selected (if it were, it would
253 * not have a blank commit mask) and all its children have
254 * existing bitmaps (see the comment starting with "This commit
255 * has an existing bitmap" below), so it does not contribute
256 * anything to the final bitmap file or its descendants.
258 if (!c_ent
->commit_mask
)
261 if (old_bitmap
&& bitmap_for_commit(old_bitmap
, commit
)) {
263 * This commit has an existing bitmap, so we can
264 * get its bits immediately without an object
265 * walk. That is, it is reusable as-is and there is no
266 * need to continue walking beyond it.
268 * Mark it as such and add it to bb->commits separately
269 * to avoid allocating a position in the commit mask.
271 commit_list_insert(commit
, &reusable
);
275 if (c_ent
->maximal
) {
277 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
278 bb
->commits
[bb
->commits_nr
++] = commit
;
282 struct bb_commit
*p_ent
= bb_data_at(&bb
->data
, p
->item
);
283 int c_not_p
, p_not_c
;
285 if (!p_ent
->commit_mask
) {
286 p_ent
->commit_mask
= bitmap_new();
290 c_not_p
= bitmap_is_subset(c_ent
->commit_mask
, p_ent
->commit_mask
);
291 p_not_c
= bitmap_is_subset(p_ent
->commit_mask
, c_ent
->commit_mask
);
297 bitmap_or(p_ent
->commit_mask
, c_ent
->commit_mask
);
303 free_commit_list(p_ent
->reverse_edges
);
304 p_ent
->reverse_edges
= NULL
;
307 if (c_ent
->maximal
) {
308 commit_list_insert(commit
, &p_ent
->reverse_edges
);
310 struct commit_list
*cc
= c_ent
->reverse_edges
;
312 for (; cc
; cc
= cc
->next
) {
313 if (!commit_list_contains(cc
->item
, p_ent
->reverse_edges
))
314 commit_list_insert(cc
->item
, &p_ent
->reverse_edges
);
320 bitmap_free(c_ent
->commit_mask
);
321 c_ent
->commit_mask
= NULL
;
324 for (r
= reusable
; r
; r
= r
->next
) {
325 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
326 bb
->commits
[bb
->commits_nr
++] = r
->item
;
329 trace2_data_intmax("pack-bitmap-write", the_repository
,
330 "num_selected_commits", writer
->selected_nr
);
331 trace2_data_intmax("pack-bitmap-write", the_repository
,
332 "num_maximal_commits", num_maximal
);
334 release_revisions(&revs
);
335 free_commit_list(reusable
);
338 static void bitmap_builder_clear(struct bitmap_builder
*bb
)
340 clear_bb_data(&bb
->data
);
342 bb
->commits_nr
= bb
->commits_alloc
= 0;
345 static int fill_bitmap_tree(struct bitmap
*bitmap
,
350 struct tree_desc desc
;
351 struct name_entry entry
;
354 * If our bit is already set, then there is nothing to do. Both this
355 * tree and all of its children will be set.
357 pos
= find_object_pos(&tree
->object
.oid
, &found
);
360 if (bitmap_get(bitmap
, pos
))
362 bitmap_set(bitmap
, pos
);
364 if (parse_tree(tree
) < 0)
365 die("unable to load tree object %s",
366 oid_to_hex(&tree
->object
.oid
));
367 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
369 while (tree_entry(&desc
, &entry
)) {
370 switch (object_type(entry
.mode
)) {
372 if (fill_bitmap_tree(bitmap
,
373 lookup_tree(the_repository
, &entry
.oid
)) < 0)
377 pos
= find_object_pos(&entry
.oid
, &found
);
380 bitmap_set(bitmap
, pos
);
383 /* Gitlink, etc; not reachable */
388 free_tree_buffer(tree
);
392 static int reused_bitmaps_nr
;
394 static int fill_bitmap_commit(struct bb_commit
*ent
,
395 struct commit
*commit
,
396 struct prio_queue
*queue
,
397 struct prio_queue
*tree_queue
,
398 struct bitmap_index
*old_bitmap
,
399 const uint32_t *mapping
)
404 ent
->bitmap
= bitmap_new();
406 prio_queue_put(queue
, commit
);
409 struct commit_list
*p
;
410 struct commit
*c
= prio_queue_get(queue
);
412 if (old_bitmap
&& mapping
) {
413 struct ewah_bitmap
*old
= bitmap_for_commit(old_bitmap
, c
);
415 * If this commit has an old bitmap, then translate that
416 * bitmap and add its bits to this one. No need to walk
417 * parents or the tree for this commit.
419 if (old
&& !rebuild_bitmap(mapping
, old
, ent
->bitmap
)) {
426 * Mark ourselves and queue our tree. The commit
427 * walk ensures we cover all parents.
429 pos
= find_object_pos(&c
->object
.oid
, &found
);
432 bitmap_set(ent
->bitmap
, pos
);
433 prio_queue_put(tree_queue
,
434 repo_get_commit_tree(the_repository
, c
));
436 for (p
= c
->parents
; p
; p
= p
->next
) {
437 pos
= find_object_pos(&p
->item
->object
.oid
, &found
);
440 if (!bitmap_get(ent
->bitmap
, pos
)) {
441 bitmap_set(ent
->bitmap
, pos
);
442 prio_queue_put(queue
, p
->item
);
447 while (tree_queue
->nr
) {
448 if (fill_bitmap_tree(ent
->bitmap
,
449 prio_queue_get(tree_queue
)) < 0)
455 static void store_selected(struct bb_commit
*ent
, struct commit
*commit
)
457 struct bitmapped_commit
*stored
= &writer
.selected
[ent
->idx
];
461 stored
->bitmap
= bitmap_to_ewah(ent
->bitmap
);
463 hash_pos
= kh_put_oid_map(writer
.bitmaps
, commit
->object
.oid
, &hash_ret
);
465 die("Duplicate entry when writing index: %s",
466 oid_to_hex(&commit
->object
.oid
));
467 kh_value(writer
.bitmaps
, hash_pos
) = stored
;
470 int bitmap_writer_build(struct packing_data
*to_pack
)
472 struct bitmap_builder bb
;
474 int nr_stored
= 0; /* for progress */
475 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
476 struct prio_queue tree_queue
= { NULL
};
477 struct bitmap_index
*old_bitmap
;
479 int closed
= 1; /* until proven otherwise */
481 writer
.bitmaps
= kh_init_oid_map();
482 writer
.to_pack
= to_pack
;
484 if (writer
.show_progress
)
485 writer
.progress
= start_progress("Building bitmaps", writer
.selected_nr
);
486 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
489 old_bitmap
= prepare_bitmap_git(to_pack
->repo
);
491 mapping
= create_bitmap_mapping(old_bitmap
, to_pack
);
495 bitmap_builder_init(&bb
, &writer
, old_bitmap
);
496 for (i
= bb
.commits_nr
; i
> 0; i
--) {
497 struct commit
*commit
= bb
.commits
[i
-1];
498 struct bb_commit
*ent
= bb_data_at(&bb
.data
, commit
);
499 struct commit
*child
;
502 if (fill_bitmap_commit(ent
, commit
, &queue
, &tree_queue
,
503 old_bitmap
, mapping
) < 0) {
509 store_selected(ent
, commit
);
511 display_progress(writer
.progress
, nr_stored
);
514 while ((child
= pop_commit(&ent
->reverse_edges
))) {
515 struct bb_commit
*child_ent
=
516 bb_data_at(&bb
.data
, child
);
518 if (child_ent
->bitmap
)
519 bitmap_or(child_ent
->bitmap
, ent
->bitmap
);
521 child_ent
->bitmap
= bitmap_dup(ent
->bitmap
);
523 child_ent
->bitmap
= ent
->bitmap
;
528 bitmap_free(ent
->bitmap
);
531 clear_prio_queue(&queue
);
532 clear_prio_queue(&tree_queue
);
533 bitmap_builder_clear(&bb
);
534 free_bitmap_index(old_bitmap
);
537 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
539 trace2_data_intmax("pack-bitmap-write", the_repository
,
540 "building_bitmaps_reused", reused_bitmaps_nr
);
542 stop_progress(&writer
.progress
);
545 compute_xor_offsets();
546 return closed
? 0 : -1;
550 * Select the commits that will be bitmapped
552 static inline unsigned int next_commit_index(unsigned int idx
)
554 static const unsigned int MIN_COMMITS
= 100;
555 static const unsigned int MAX_COMMITS
= 5000;
557 static const unsigned int MUST_REGION
= 100;
558 static const unsigned int MIN_REGION
= 20000;
560 unsigned int offset
, next
;
562 if (idx
<= MUST_REGION
)
565 if (idx
<= MIN_REGION
) {
566 offset
= idx
- MUST_REGION
;
567 return (offset
< MIN_COMMITS
) ? offset
: MIN_COMMITS
;
570 offset
= idx
- MIN_REGION
;
571 next
= (offset
< MAX_COMMITS
) ? offset
: MAX_COMMITS
;
573 return (next
> MIN_COMMITS
) ? next
: MIN_COMMITS
;
576 static int date_compare(const void *_a
, const void *_b
)
578 struct commit
*a
= *(struct commit
**)_a
;
579 struct commit
*b
= *(struct commit
**)_b
;
580 return (long)b
->date
- (long)a
->date
;
583 void bitmap_writer_select_commits(struct commit
**indexed_commits
,
584 unsigned int indexed_commits_nr
,
587 unsigned int i
= 0, j
, next
;
589 QSORT(indexed_commits
, indexed_commits_nr
, date_compare
);
591 if (indexed_commits_nr
< 100) {
592 for (i
= 0; i
< indexed_commits_nr
; ++i
)
593 push_bitmapped_commit(indexed_commits
[i
]);
597 if (writer
.show_progress
)
598 writer
.progress
= start_progress("Selecting bitmap commits", 0);
601 struct commit
*chosen
= NULL
;
603 next
= next_commit_index(i
);
605 if (i
+ next
>= indexed_commits_nr
)
608 if (max_bitmaps
> 0 && writer
.selected_nr
>= max_bitmaps
) {
609 writer
.selected_nr
= max_bitmaps
;
614 chosen
= indexed_commits
[i
];
616 chosen
= indexed_commits
[i
+ next
];
618 for (j
= 0; j
<= next
; ++j
) {
619 struct commit
*cm
= indexed_commits
[i
+ j
];
621 if ((cm
->object
.flags
& NEEDS_BITMAP
) != 0) {
626 if (cm
->parents
&& cm
->parents
->next
)
631 push_bitmapped_commit(chosen
);
634 display_progress(writer
.progress
, i
);
637 stop_progress(&writer
.progress
);
641 static int hashwrite_ewah_helper(void *f
, const void *buf
, size_t len
)
643 /* hashwrite will die on error */
644 hashwrite(f
, buf
, len
);
649 * Write the bitmap index to disk
651 static inline void dump_bitmap(struct hashfile
*f
, struct ewah_bitmap
*bitmap
)
653 if (ewah_serialize_to(bitmap
, hashwrite_ewah_helper
, f
) < 0)
654 die("Failed to write bitmap index");
657 static const struct object_id
*oid_access(size_t pos
, const void *table
)
659 const struct pack_idx_entry
* const *index
= table
;
660 return &index
[pos
]->oid
;
663 static void write_selected_commits_v1(struct hashfile
*f
,
664 uint32_t *commit_positions
,
669 for (i
= 0; i
< writer
.selected_nr
; ++i
) {
670 struct bitmapped_commit
*stored
= &writer
.selected
[i
];
673 offsets
[i
] = hashfile_total(f
);
675 hashwrite_be32(f
, commit_positions
[i
]);
676 hashwrite_u8(f
, stored
->xor_offset
);
677 hashwrite_u8(f
, stored
->flags
);
679 dump_bitmap(f
, stored
->write_as
);
683 static int table_cmp(const void *_va
, const void *_vb
, void *_data
)
685 uint32_t *commit_positions
= _data
;
686 uint32_t a
= commit_positions
[*(uint32_t *)_va
];
687 uint32_t b
= commit_positions
[*(uint32_t *)_vb
];
697 static void write_lookup_table(struct hashfile
*f
,
698 uint32_t *commit_positions
,
702 uint32_t *table
, *table_inv
;
704 ALLOC_ARRAY(table
, writer
.selected_nr
);
705 ALLOC_ARRAY(table_inv
, writer
.selected_nr
);
707 for (i
= 0; i
< writer
.selected_nr
; i
++)
711 * At the end of this sort table[j] = i means that the i'th
712 * bitmap corresponds to j'th bitmapped commit (among the selected
713 * commits) in lex order of OIDs.
715 QSORT_S(table
, writer
.selected_nr
, table_cmp
, commit_positions
);
717 /* table_inv helps us discover that relationship (i'th bitmap
718 * to j'th commit by j = table_inv[i])
720 for (i
= 0; i
< writer
.selected_nr
; i
++)
721 table_inv
[table
[i
]] = i
;
723 trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository
);
724 for (i
= 0; i
< writer
.selected_nr
; i
++) {
725 struct bitmapped_commit
*selected
= &writer
.selected
[table
[i
]];
726 uint32_t xor_offset
= selected
->xor_offset
;
731 * xor_index stores the index (in the bitmap entries)
732 * of the corresponding xor bitmap. But we need to convert
733 * this index into lookup table's index. So, table_inv[xor_index]
734 * gives us the index position w.r.t. the lookup table.
736 * If "k = table[i] - xor_offset" then the xor base is the k'th
737 * bitmap. `table_inv[k]` gives us the position of that bitmap
738 * in the lookup table.
740 uint32_t xor_index
= table
[i
] - xor_offset
;
741 xor_row
= table_inv
[xor_index
];
743 xor_row
= 0xffffffff;
746 hashwrite_be32(f
, commit_positions
[table
[i
]]);
747 hashwrite_be64(f
, (uint64_t)offsets
[table
[i
]]);
748 hashwrite_be32(f
, xor_row
);
750 trace2_region_leave("pack-bitmap-write", "writing_lookup_table", the_repository
);
756 static void write_hash_cache(struct hashfile
*f
,
757 struct pack_idx_entry
**index
,
762 for (i
= 0; i
< index_nr
; ++i
) {
763 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
764 hashwrite_be32(f
, entry
->hash
);
768 void bitmap_writer_set_checksum(const unsigned char *sha1
)
770 hashcpy(writer
.pack_checksum
, sha1
);
773 void bitmap_writer_finish(struct pack_idx_entry
**index
,
775 const char *filename
,
778 static uint16_t default_version
= 1;
779 static uint16_t flags
= BITMAP_OPT_FULL_DAG
;
780 struct strbuf tmp_file
= STRBUF_INIT
;
782 uint32_t *commit_positions
= NULL
;
783 off_t
*offsets
= NULL
;
786 struct bitmap_disk_header header
;
788 int fd
= odb_mkstemp(&tmp_file
, "pack/tmp_bitmap_XXXXXX");
790 f
= hashfd(fd
, tmp_file
.buf
);
792 memcpy(header
.magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
));
793 header
.version
= htons(default_version
);
794 header
.options
= htons(flags
| options
);
795 header
.entry_count
= htonl(writer
.selected_nr
);
796 hashcpy(header
.checksum
, writer
.pack_checksum
);
798 hashwrite(f
, &header
, sizeof(header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
);
799 dump_bitmap(f
, writer
.commits
);
800 dump_bitmap(f
, writer
.trees
);
801 dump_bitmap(f
, writer
.blobs
);
802 dump_bitmap(f
, writer
.tags
);
804 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
805 CALLOC_ARRAY(offsets
, index_nr
);
807 ALLOC_ARRAY(commit_positions
, writer
.selected_nr
);
809 for (i
= 0; i
< writer
.selected_nr
; i
++) {
810 struct bitmapped_commit
*stored
= &writer
.selected
[i
];
811 int commit_pos
= oid_pos(&stored
->commit
->object
.oid
, index
, index_nr
, oid_access
);
814 BUG(_("trying to write commit not in index"));
816 commit_positions
[i
] = commit_pos
;
819 write_selected_commits_v1(f
, commit_positions
, offsets
);
821 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
822 write_lookup_table(f
, commit_positions
, offsets
);
824 if (options
& BITMAP_OPT_HASH_CACHE
)
825 write_hash_cache(f
, index
, index_nr
);
827 finalize_hashfile(f
, NULL
, FSYNC_COMPONENT_PACK_METADATA
,
828 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
830 if (adjust_shared_perm(tmp_file
.buf
))
831 die_errno("unable to make temporary bitmap file readable");
833 if (rename(tmp_file
.buf
, filename
))
834 die_errno("unable to rename temporary bitmap file to '%s'", filename
);
836 strbuf_release(&tmp_file
);
837 free(commit_positions
);