2 #include "object-store.h"
7 #include "list-objects.h"
9 #include "pack-revindex.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"
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
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)
130 struct object_entry *entry = packlist_find(writer.to_pack, oid);
133 die("Failed to write bitmap index. Packfile doesn't have full closure "
134 "(object %s is missing)", oid_to_hex(oid));
137 return oe_in_pack_pos(writer.to_pack, entry);
140 static void compute_xor_offsets(void)
142 static const int MAX_XOR_OFFSET_SEARCH = 10;
146 while (next < writer.selected_nr) {
147 struct bitmapped_commit *stored = &writer.selected[next];
150 struct ewah_bitmap *best_bitmap = stored->bitmap;
151 struct ewah_bitmap *test_xor;
153 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
159 test_xor = ewah_pool_new();
160 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
162 if (test_xor->buffer_size < best_bitmap->buffer_size) {
163 if (best_bitmap != stored->bitmap)
164 ewah_pool_free(best_bitmap);
166 best_bitmap = test_xor;
169 ewah_pool_free(test_xor);
173 stored->xor_offset = best_offset;
174 stored->write_as = best_bitmap;
181 struct commit_list *reverse_edges;
182 struct bitmap *commit_mask;
183 struct bitmap *bitmap;
186 unsigned idx; /* within selected array */
189 define_commit_slab(bb_data, struct bb_commit);
191 struct bitmap_builder {
193 struct commit **commits;
194 size_t commits_nr, commits_alloc;
197 static void bitmap_builder_init(struct bitmap_builder *bb,
198 struct bitmap_writer *writer)
200 struct rev_info revs;
201 struct commit *commit;
202 unsigned int i, num_maximal;
204 memset(bb, 0, sizeof(*bb));
205 init_bb_data(&bb->data);
207 reset_revision_walk();
208 repo_init_revisions(writer->to_pack->repo, &revs, NULL);
211 for (i = 0; i < writer->selected_nr; i++) {
212 struct commit *c = writer->selected[i].commit;
213 struct bb_commit *ent = bb_data_at(&bb->data, c);
219 ent->commit_mask = bitmap_new();
220 bitmap_set(ent->commit_mask, i);
222 add_pending_object(&revs, &c->object, "");
224 num_maximal = writer->selected_nr;
226 if (prepare_revision_walk(&revs))
227 die("revision walk setup failed");
229 while ((commit = get_revision(&revs))) {
230 struct commit_list *p;
231 struct bb_commit *c_ent;
233 parse_commit_or_die(commit);
235 c_ent = bb_data_at(&bb->data, commit);
237 if (c_ent->maximal) {
238 if (!c_ent->selected) {
239 bitmap_set(c_ent->commit_mask, num_maximal);
243 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
244 bb->commits[bb->commits_nr++] = commit;
247 for (p = commit->parents; p; p = p->next) {
248 struct bb_commit *p_ent = bb_data_at(&bb->data, p->item);
249 int c_not_p, p_not_c;
251 if (!p_ent->commit_mask) {
252 p_ent->commit_mask = bitmap_new();
256 c_not_p = bitmap_is_subset(c_ent->commit_mask, p_ent->commit_mask);
257 p_not_c = bitmap_is_subset(p_ent->commit_mask, c_ent->commit_mask);
263 bitmap_or(p_ent->commit_mask, c_ent->commit_mask);
269 free_commit_list(p_ent->reverse_edges);
270 p_ent->reverse_edges = NULL;
273 if (c_ent->maximal) {
274 commit_list_insert(commit, &p_ent->reverse_edges);
276 struct commit_list *cc = c_ent->reverse_edges;
278 for (; cc; cc = cc->next) {
279 if (!commit_list_contains(cc->item, p_ent->reverse_edges))
280 commit_list_insert(cc->item, &p_ent->reverse_edges);
285 bitmap_free(c_ent->commit_mask);
286 c_ent->commit_mask = NULL;
289 trace2_data_intmax("pack-bitmap-write", the_repository,
290 "num_selected_commits", writer->selected_nr);
291 trace2_data_intmax("pack-bitmap-write", the_repository,
292 "num_maximal_commits", num_maximal);
295 static void bitmap_builder_clear(struct bitmap_builder *bb)
297 clear_bb_data(&bb->data);
299 bb->commits_nr = bb->commits_alloc = 0;
302 static void fill_bitmap_tree(struct bitmap *bitmap,
306 struct tree_desc desc;
307 struct name_entry entry;
310 * If our bit is already set, then there is nothing to do. Both this
311 * tree and all of its children will be set.
313 pos = find_object_pos(&tree->object.oid);
314 if (bitmap_get(bitmap, pos))
316 bitmap_set(bitmap, pos);
318 if (parse_tree(tree) < 0)
319 die("unable to load tree object %s",
320 oid_to_hex(&tree->object.oid));
321 init_tree_desc(&desc, tree->buffer, tree->size);
323 while (tree_entry(&desc, &entry)) {
324 switch (object_type(entry.mode)) {
326 fill_bitmap_tree(bitmap,
327 lookup_tree(the_repository, &entry.oid));
330 bitmap_set(bitmap, find_object_pos(&entry.oid));
333 /* Gitlink, etc; not reachable */
338 free_tree_buffer(tree);
341 static void fill_bitmap_commit(struct bb_commit *ent,
342 struct commit *commit,
343 struct prio_queue *queue)
346 ent->bitmap = bitmap_new();
348 bitmap_set(ent->bitmap, find_object_pos(&commit->object.oid));
349 prio_queue_put(queue, commit);
352 struct commit_list *p;
353 struct commit *c = prio_queue_get(queue);
355 bitmap_set(ent->bitmap, find_object_pos(&c->object.oid));
356 fill_bitmap_tree(ent->bitmap, get_commit_tree(c));
358 for (p = c->parents; p; p = p->next) {
359 int pos = find_object_pos(&p->item->object.oid);
360 if (!bitmap_get(ent->bitmap, pos)) {
361 bitmap_set(ent->bitmap, pos);
362 prio_queue_put(queue, p->item);
368 static void store_selected(struct bb_commit *ent, struct commit *commit)
370 struct bitmapped_commit *stored = &writer.selected[ent->idx];
374 stored->bitmap = bitmap_to_ewah(ent->bitmap);
376 hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret);
378 die("Duplicate entry when writing index: %s",
379 oid_to_hex(&commit->object.oid));
380 kh_value(writer.bitmaps, hash_pos) = stored;
383 void bitmap_writer_build(struct packing_data *to_pack)
385 struct bitmap_builder bb;
387 int nr_stored = 0; /* for progress */
388 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
390 writer.bitmaps = kh_init_oid_map();
391 writer.to_pack = to_pack;
393 if (writer.show_progress)
394 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
395 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
398 bitmap_builder_init(&bb, &writer);
399 for (i = bb.commits_nr; i > 0; i--) {
400 struct commit *commit = bb.commits[i-1];
401 struct bb_commit *ent = bb_data_at(&bb.data, commit);
402 struct commit *child;
405 fill_bitmap_commit(ent, commit, &queue);
408 store_selected(ent, commit);
410 display_progress(writer.progress, nr_stored);
413 while ((child = pop_commit(&ent->reverse_edges))) {
414 struct bb_commit *child_ent =
415 bb_data_at(&bb.data, child);
417 if (child_ent->bitmap)
418 bitmap_or(child_ent->bitmap, ent->bitmap);
420 child_ent->bitmap = bitmap_dup(ent->bitmap);
422 child_ent->bitmap = ent->bitmap;
427 bitmap_free(ent->bitmap);
430 clear_prio_queue(&queue);
431 bitmap_builder_clear(&bb);
433 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
436 stop_progress(&writer.progress);
438 compute_xor_offsets();
442 * Select the commits that will be bitmapped
444 static inline unsigned int next_commit_index(unsigned int idx)
446 static const unsigned int MIN_COMMITS = 100;
447 static const unsigned int MAX_COMMITS = 5000;
449 static const unsigned int MUST_REGION = 100;
450 static const unsigned int MIN_REGION = 20000;
452 unsigned int offset, next;
454 if (idx <= MUST_REGION)
457 if (idx <= MIN_REGION) {
458 offset = idx - MUST_REGION;
459 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
462 offset = idx - MIN_REGION;
463 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
465 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
468 static int date_compare(const void *_a, const void *_b)
470 struct commit *a = *(struct commit **)_a;
471 struct commit *b = *(struct commit **)_b;
472 return (long)b->date - (long)a->date;
475 void bitmap_writer_select_commits(struct commit **indexed_commits,
476 unsigned int indexed_commits_nr,
479 unsigned int i = 0, j, next;
481 QSORT(indexed_commits, indexed_commits_nr, date_compare);
483 if (writer.show_progress)
484 writer.progress = start_progress("Selecting bitmap commits", 0);
486 if (indexed_commits_nr < 100) {
487 for (i = 0; i < indexed_commits_nr; ++i)
488 push_bitmapped_commit(indexed_commits[i]);
493 struct commit *chosen = NULL;
495 next = next_commit_index(i);
497 if (i + next >= indexed_commits_nr)
500 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
501 writer.selected_nr = max_bitmaps;
506 chosen = indexed_commits[i];
508 chosen = indexed_commits[i + next];
510 for (j = 0; j <= next; ++j) {
511 struct commit *cm = indexed_commits[i + j];
513 if ((cm->object.flags & NEEDS_BITMAP) != 0) {
518 if (cm->parents && cm->parents->next)
523 push_bitmapped_commit(chosen);
526 display_progress(writer.progress, i);
529 stop_progress(&writer.progress);
533 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
535 /* hashwrite will die on error */
536 hashwrite(f, buf, len);
541 * Write the bitmap index to disk
543 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
545 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
546 die("Failed to write bitmap index");
549 static const unsigned char *sha1_access(size_t pos, void *table)
551 struct pack_idx_entry **index = table;
552 return index[pos]->oid.hash;
555 static void write_selected_commits_v1(struct hashfile *f,
556 struct pack_idx_entry **index,
561 for (i = 0; i < writer.selected_nr; ++i) {
562 struct bitmapped_commit *stored = &writer.selected[i];
565 sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
568 BUG("trying to write commit not in index");
570 hashwrite_be32(f, commit_pos);
571 hashwrite_u8(f, stored->xor_offset);
572 hashwrite_u8(f, stored->flags);
574 dump_bitmap(f, stored->write_as);
578 static void write_hash_cache(struct hashfile *f,
579 struct pack_idx_entry **index,
584 for (i = 0; i < index_nr; ++i) {
585 struct object_entry *entry = (struct object_entry *)index[i];
586 hashwrite_be32(f, entry->hash);
590 void bitmap_writer_set_checksum(unsigned char *sha1)
592 hashcpy(writer.pack_checksum, sha1);
595 void bitmap_writer_finish(struct pack_idx_entry **index,
597 const char *filename,
600 static uint16_t default_version = 1;
601 static uint16_t flags = BITMAP_OPT_FULL_DAG;
602 struct strbuf tmp_file = STRBUF_INIT;
605 struct bitmap_disk_header header;
607 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
609 f = hashfd(fd, tmp_file.buf);
611 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
612 header.version = htons(default_version);
613 header.options = htons(flags | options);
614 header.entry_count = htonl(writer.selected_nr);
615 hashcpy(header.checksum, writer.pack_checksum);
617 hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
618 dump_bitmap(f, writer.commits);
619 dump_bitmap(f, writer.trees);
620 dump_bitmap(f, writer.blobs);
621 dump_bitmap(f, writer.tags);
622 write_selected_commits_v1(f, index, index_nr);
624 if (options & BITMAP_OPT_HASH_CACHE)
625 write_hash_cache(f, index, index_nr);
627 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
629 if (adjust_shared_perm(tmp_file.buf))
630 die_errno("unable to make temporary bitmap file readable");
632 if (rename(tmp_file.buf, filename))
633 die_errno("unable to rename temporary bitmap file to '%s'", filename);
635 strbuf_release(&tmp_file);