6 #include "list-objects.h"
8 #include "pack-revindex.h"
10 #include "pack-bitmap.h"
11 #include "sha1-lookup.h"
12 #include "pack-objects.h"
14 struct bitmapped_commit
{
15 struct commit
*commit
;
16 struct ewah_bitmap
*bitmap
;
17 struct ewah_bitmap
*write_as
;
23 struct bitmap_writer
{
24 struct ewah_bitmap
*commits
;
25 struct ewah_bitmap
*trees
;
26 struct ewah_bitmap
*blobs
;
27 struct ewah_bitmap
*tags
;
31 struct packing_data
*to_pack
;
33 struct bitmapped_commit
*selected
;
34 unsigned int selected_nr
, selected_alloc
;
36 struct progress
*progress
;
38 unsigned char pack_checksum
[20];
41 static struct bitmap_writer writer
;
43 void bitmap_writer_show_progress(int show
)
45 writer
.show_progress
= show
;
49 * Build the initial type index for the packfile
51 void bitmap_writer_build_type_index(struct pack_idx_entry
**index
,
56 writer
.commits
= ewah_new();
57 writer
.trees
= ewah_new();
58 writer
.blobs
= ewah_new();
59 writer
.tags
= ewah_new();
61 for (i
= 0; i
< index_nr
; ++i
) {
62 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
63 enum object_type real_type
;
65 entry
->in_pack_pos
= i
;
67 switch (entry
->type
) {
72 real_type
= entry
->type
;
76 real_type
= sha1_object_info(entry
->idx
.oid
.hash
,
83 ewah_set(writer
.commits
, i
);
87 ewah_set(writer
.trees
, i
);
91 ewah_set(writer
.blobs
, i
);
95 ewah_set(writer
.tags
, i
);
99 die("Missing type information for %s (%d/%d)",
100 oid_to_hex(&entry
->idx
.oid
), real_type
,
107 * Compute the actual bitmaps
109 static struct object
**seen_objects
;
110 static unsigned int seen_objects_nr
, seen_objects_alloc
;
112 static inline void push_bitmapped_commit(struct commit
*commit
, struct ewah_bitmap
*reused
)
114 if (writer
.selected_nr
>= writer
.selected_alloc
) {
115 writer
.selected_alloc
= (writer
.selected_alloc
+ 32) * 2;
116 REALLOC_ARRAY(writer
.selected
, writer
.selected_alloc
);
119 writer
.selected
[writer
.selected_nr
].commit
= commit
;
120 writer
.selected
[writer
.selected_nr
].bitmap
= reused
;
121 writer
.selected
[writer
.selected_nr
].flags
= 0;
123 writer
.selected_nr
++;
126 static inline void mark_as_seen(struct object
*object
)
128 ALLOC_GROW(seen_objects
, seen_objects_nr
+ 1, seen_objects_alloc
);
129 seen_objects
[seen_objects_nr
++] = object
;
132 static inline void reset_all_seen(void)
135 for (i
= 0; i
< seen_objects_nr
; ++i
) {
136 seen_objects
[i
]->flags
&= ~(SEEN
| ADDED
| SHOWN
);
141 static uint32_t find_object_pos(const unsigned char *sha1
)
143 struct object_entry
*entry
= packlist_find(writer
.to_pack
, sha1
, NULL
);
146 die("Failed to write bitmap index. Packfile doesn't have full closure "
147 "(object %s is missing)", sha1_to_hex(sha1
));
150 return entry
->in_pack_pos
;
153 static void show_object(struct object
*object
, const char *name
, void *data
)
155 struct bitmap
*base
= data
;
156 bitmap_set(base
, find_object_pos(object
->oid
.hash
));
157 mark_as_seen(object
);
160 static void show_commit(struct commit
*commit
, void *data
)
162 mark_as_seen((struct object
*)commit
);
166 add_to_include_set(struct bitmap
*base
, struct commit
*commit
)
169 uint32_t bitmap_pos
= find_object_pos(commit
->object
.oid
.hash
);
171 if (bitmap_get(base
, bitmap_pos
))
174 hash_pos
= kh_get_sha1(writer
.bitmaps
, commit
->object
.oid
.hash
);
175 if (hash_pos
< kh_end(writer
.bitmaps
)) {
176 struct bitmapped_commit
*bc
= kh_value(writer
.bitmaps
, hash_pos
);
177 bitmap_or_ewah(base
, bc
->bitmap
);
181 bitmap_set(base
, bitmap_pos
);
186 should_include(struct commit
*commit
, void *_data
)
188 struct bitmap
*base
= _data
;
190 if (!add_to_include_set(base
, commit
)) {
191 struct commit_list
*parent
= commit
->parents
;
193 mark_as_seen((struct object
*)commit
);
196 parent
->item
->object
.flags
|= SEEN
;
197 mark_as_seen((struct object
*)parent
->item
);
198 parent
= parent
->next
;
207 static void compute_xor_offsets(void)
209 static const int MAX_XOR_OFFSET_SEARCH
= 10;
213 while (next
< writer
.selected_nr
) {
214 struct bitmapped_commit
*stored
= &writer
.selected
[next
];
217 struct ewah_bitmap
*best_bitmap
= stored
->bitmap
;
218 struct ewah_bitmap
*test_xor
;
220 for (i
= 1; i
<= MAX_XOR_OFFSET_SEARCH
; ++i
) {
226 test_xor
= ewah_pool_new();
227 ewah_xor(writer
.selected
[curr
].bitmap
, stored
->bitmap
, test_xor
);
229 if (test_xor
->buffer_size
< best_bitmap
->buffer_size
) {
230 if (best_bitmap
!= stored
->bitmap
)
231 ewah_pool_free(best_bitmap
);
233 best_bitmap
= test_xor
;
236 ewah_pool_free(test_xor
);
240 stored
->xor_offset
= best_offset
;
241 stored
->write_as
= best_bitmap
;
247 void bitmap_writer_build(struct packing_data
*to_pack
)
249 static const double REUSE_BITMAP_THRESHOLD
= 0.2;
251 int i
, reuse_after
, need_reset
;
252 struct bitmap
*base
= bitmap_new();
253 struct rev_info revs
;
255 writer
.bitmaps
= kh_init_sha1();
256 writer
.to_pack
= to_pack
;
258 if (writer
.show_progress
)
259 writer
.progress
= start_progress("Building bitmaps", writer
.selected_nr
);
261 init_revisions(&revs
, NULL
);
262 revs
.tag_objects
= 1;
263 revs
.tree_objects
= 1;
264 revs
.blob_objects
= 1;
267 revs
.include_check
= should_include
;
268 reset_revision_walk();
270 reuse_after
= writer
.selected_nr
* REUSE_BITMAP_THRESHOLD
;
273 for (i
= writer
.selected_nr
- 1; i
>= 0; --i
) {
274 struct bitmapped_commit
*stored
;
275 struct object
*object
;
280 stored
= &writer
.selected
[i
];
281 object
= (struct object
*)stored
->commit
;
283 if (stored
->bitmap
== NULL
) {
284 if (i
< writer
.selected_nr
- 1 &&
286 !in_merge_bases(writer
.selected
[i
+ 1].commit
,
292 add_pending_object(&revs
, object
, "");
293 revs
.include_check_data
= base
;
295 if (prepare_revision_walk(&revs
))
296 die("revision walk setup failed");
298 traverse_commit_list(&revs
, show_commit
, show_object
, base
);
300 object_array_clear(&revs
.pending
);
302 stored
->bitmap
= bitmap_to_ewah(base
);
307 if (i
>= reuse_after
)
308 stored
->flags
|= BITMAP_FLAG_REUSE
;
310 hash_pos
= kh_put_sha1(writer
.bitmaps
, object
->oid
.hash
, &hash_ret
);
312 die("Duplicate entry when writing index: %s",
313 oid_to_hex(&object
->oid
));
315 kh_value(writer
.bitmaps
, hash_pos
) = stored
;
316 display_progress(writer
.progress
, writer
.selected_nr
- i
);
320 stop_progress(&writer
.progress
);
322 compute_xor_offsets();
326 * Select the commits that will be bitmapped
328 static inline unsigned int next_commit_index(unsigned int idx
)
330 static const unsigned int MIN_COMMITS
= 100;
331 static const unsigned int MAX_COMMITS
= 5000;
333 static const unsigned int MUST_REGION
= 100;
334 static const unsigned int MIN_REGION
= 20000;
336 unsigned int offset
, next
;
338 if (idx
<= MUST_REGION
)
341 if (idx
<= MIN_REGION
) {
342 offset
= idx
- MUST_REGION
;
343 return (offset
< MIN_COMMITS
) ? offset
: MIN_COMMITS
;
346 offset
= idx
- MIN_REGION
;
347 next
= (offset
< MAX_COMMITS
) ? offset
: MAX_COMMITS
;
349 return (next
> MIN_COMMITS
) ? next
: MIN_COMMITS
;
352 static int date_compare(const void *_a
, const void *_b
)
354 struct commit
*a
= *(struct commit
**)_a
;
355 struct commit
*b
= *(struct commit
**)_b
;
356 return (long)b
->date
- (long)a
->date
;
359 void bitmap_writer_reuse_bitmaps(struct packing_data
*to_pack
)
361 if (prepare_bitmap_git() < 0)
364 writer
.reused
= kh_init_sha1();
365 rebuild_existing_bitmaps(to_pack
, writer
.reused
, writer
.show_progress
);
368 static struct ewah_bitmap
*find_reused_bitmap(const unsigned char *sha1
)
375 hash_pos
= kh_get_sha1(writer
.reused
, sha1
);
376 if (hash_pos
>= kh_end(writer
.reused
))
379 return kh_value(writer
.reused
, hash_pos
);
382 void bitmap_writer_select_commits(struct commit
**indexed_commits
,
383 unsigned int indexed_commits_nr
,
386 unsigned int i
= 0, j
, next
;
388 QSORT(indexed_commits
, indexed_commits_nr
, date_compare
);
390 if (writer
.show_progress
)
391 writer
.progress
= start_progress("Selecting bitmap commits", 0);
393 if (indexed_commits_nr
< 100) {
394 for (i
= 0; i
< indexed_commits_nr
; ++i
)
395 push_bitmapped_commit(indexed_commits
[i
], NULL
);
400 struct ewah_bitmap
*reused_bitmap
= NULL
;
401 struct commit
*chosen
= NULL
;
403 next
= next_commit_index(i
);
405 if (i
+ next
>= indexed_commits_nr
)
408 if (max_bitmaps
> 0 && writer
.selected_nr
>= max_bitmaps
) {
409 writer
.selected_nr
= max_bitmaps
;
414 chosen
= indexed_commits
[i
];
415 reused_bitmap
= find_reused_bitmap(chosen
->object
.oid
.hash
);
417 chosen
= indexed_commits
[i
+ next
];
419 for (j
= 0; j
<= next
; ++j
) {
420 struct commit
*cm
= indexed_commits
[i
+ j
];
422 reused_bitmap
= find_reused_bitmap(cm
->object
.oid
.hash
);
423 if (reused_bitmap
|| (cm
->object
.flags
& NEEDS_BITMAP
) != 0) {
428 if (cm
->parents
&& cm
->parents
->next
)
433 push_bitmapped_commit(chosen
, reused_bitmap
);
436 display_progress(writer
.progress
, i
);
439 stop_progress(&writer
.progress
);
443 static int hashwrite_ewah_helper(void *f
, const void *buf
, size_t len
)
445 /* hashwrite will die on error */
446 hashwrite(f
, buf
, len
);
451 * Write the bitmap index to disk
453 static inline void dump_bitmap(struct hashfile
*f
, struct ewah_bitmap
*bitmap
)
455 if (ewah_serialize_to(bitmap
, hashwrite_ewah_helper
, f
) < 0)
456 die("Failed to write bitmap index");
459 static const unsigned char *sha1_access(size_t pos
, void *table
)
461 struct pack_idx_entry
**index
= table
;
462 return index
[pos
]->oid
.hash
;
465 static void write_selected_commits_v1(struct hashfile
*f
,
466 struct pack_idx_entry
**index
,
471 for (i
= 0; i
< writer
.selected_nr
; ++i
) {
472 struct bitmapped_commit
*stored
= &writer
.selected
[i
];
475 sha1_pos(stored
->commit
->object
.oid
.hash
, index
, index_nr
, sha1_access
);
478 die("BUG: trying to write commit not in index");
480 hashwrite_be32(f
, commit_pos
);
481 hashwrite_u8(f
, stored
->xor_offset
);
482 hashwrite_u8(f
, stored
->flags
);
484 dump_bitmap(f
, stored
->write_as
);
488 static void write_hash_cache(struct hashfile
*f
,
489 struct pack_idx_entry
**index
,
494 for (i
= 0; i
< index_nr
; ++i
) {
495 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
496 uint32_t hash_value
= htonl(entry
->hash
);
497 hashwrite(f
, &hash_value
, sizeof(hash_value
));
501 void bitmap_writer_set_checksum(unsigned char *sha1
)
503 hashcpy(writer
.pack_checksum
, sha1
);
506 void bitmap_writer_finish(struct pack_idx_entry
**index
,
508 const char *filename
,
511 static uint16_t default_version
= 1;
512 static uint16_t flags
= BITMAP_OPT_FULL_DAG
;
513 struct strbuf tmp_file
= STRBUF_INIT
;
516 struct bitmap_disk_header header
;
518 int fd
= odb_mkstemp(&tmp_file
, "pack/tmp_bitmap_XXXXXX");
520 f
= hashfd(fd
, tmp_file
.buf
);
522 memcpy(header
.magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
));
523 header
.version
= htons(default_version
);
524 header
.options
= htons(flags
| options
);
525 header
.entry_count
= htonl(writer
.selected_nr
);
526 hashcpy(header
.checksum
, writer
.pack_checksum
);
528 hashwrite(f
, &header
, sizeof(header
));
529 dump_bitmap(f
, writer
.commits
);
530 dump_bitmap(f
, writer
.trees
);
531 dump_bitmap(f
, writer
.blobs
);
532 dump_bitmap(f
, writer
.tags
);
533 write_selected_commits_v1(f
, index
, index_nr
);
535 if (options
& BITMAP_OPT_HASH_CACHE
)
536 write_hash_cache(f
, index
, index_nr
);
538 finalize_hashfile(f
, NULL
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
540 if (adjust_shared_perm(tmp_file
.buf
))
541 die_errno("unable to make temporary bitmap file readable");
543 if (rename(tmp_file
.buf
, filename
))
544 die_errno("unable to rename temporary bitmap file to '%s'", filename
);
546 strbuf_release(&tmp_file
);