Merge branch 'rs/sequencer-oidset-insert-avoids-dups' into maint
[git.git] / pack-bitmap-write.c
blobd977e9bacb19ed766dd0a501b6fbcae800bb4ae0
1 #include "cache.h"
2 #include "object-store.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "list-objects.h"
8 #include "progress.h"
9 #include "pack-revindex.h"
10 #include "pack.h"
11 #include "pack-bitmap.h"
12 #include "sha1-lookup.h"
13 #include "pack-objects.h"
15 struct bitmapped_commit {
16 struct commit *commit;
17 struct ewah_bitmap *bitmap;
18 struct ewah_bitmap *write_as;
19 int flags;
20 int xor_offset;
21 uint32_t commit_pos;
24 struct bitmap_writer {
25 struct ewah_bitmap *commits;
26 struct ewah_bitmap *trees;
27 struct ewah_bitmap *blobs;
28 struct ewah_bitmap *tags;
30 khash_sha1 *bitmaps;
31 khash_sha1 *reused;
32 struct packing_data *to_pack;
34 struct bitmapped_commit *selected;
35 unsigned int selected_nr, selected_alloc;
37 struct progress *progress;
38 int show_progress;
39 unsigned char pack_checksum[20];
42 static struct bitmap_writer writer;
44 void bitmap_writer_show_progress(int show)
46 writer.show_progress = show;
49 /**
50 * Build the initial type index for the packfile
52 void bitmap_writer_build_type_index(struct packing_data *to_pack,
53 struct pack_idx_entry **index,
54 uint32_t index_nr)
56 uint32_t i;
58 writer.commits = ewah_new();
59 writer.trees = ewah_new();
60 writer.blobs = ewah_new();
61 writer.tags = ewah_new();
62 ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
64 for (i = 0; i < index_nr; ++i) {
65 struct object_entry *entry = (struct object_entry *)index[i];
66 enum object_type real_type;
68 oe_set_in_pack_pos(to_pack, entry, i);
70 switch (oe_type(entry)) {
71 case OBJ_COMMIT:
72 case OBJ_TREE:
73 case OBJ_BLOB:
74 case OBJ_TAG:
75 real_type = oe_type(entry);
76 break;
78 default:
79 real_type = oid_object_info(the_repository,
80 &entry->idx.oid, NULL);
81 break;
84 switch (real_type) {
85 case OBJ_COMMIT:
86 ewah_set(writer.commits, i);
87 break;
89 case OBJ_TREE:
90 ewah_set(writer.trees, i);
91 break;
93 case OBJ_BLOB:
94 ewah_set(writer.blobs, i);
95 break;
97 case OBJ_TAG:
98 ewah_set(writer.tags, i);
99 break;
101 default:
102 die("Missing type information for %s (%d/%d)",
103 oid_to_hex(&entry->idx.oid), real_type,
104 oe_type(entry));
110 * Compute the actual bitmaps
112 static struct object **seen_objects;
113 static unsigned int seen_objects_nr, seen_objects_alloc;
115 static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitmap *reused)
117 if (writer.selected_nr >= writer.selected_alloc) {
118 writer.selected_alloc = (writer.selected_alloc + 32) * 2;
119 REALLOC_ARRAY(writer.selected, writer.selected_alloc);
122 writer.selected[writer.selected_nr].commit = commit;
123 writer.selected[writer.selected_nr].bitmap = reused;
124 writer.selected[writer.selected_nr].flags = 0;
126 writer.selected_nr++;
129 static inline void mark_as_seen(struct object *object)
131 ALLOC_GROW(seen_objects, seen_objects_nr + 1, seen_objects_alloc);
132 seen_objects[seen_objects_nr++] = object;
135 static inline void reset_all_seen(void)
137 unsigned int i;
138 for (i = 0; i < seen_objects_nr; ++i) {
139 seen_objects[i]->flags &= ~(SEEN | ADDED | SHOWN);
141 seen_objects_nr = 0;
144 static uint32_t find_object_pos(const unsigned char *sha1)
146 struct object_entry *entry = packlist_find(writer.to_pack, sha1, NULL);
148 if (!entry) {
149 die("Failed to write bitmap index. Packfile doesn't have full closure "
150 "(object %s is missing)", sha1_to_hex(sha1));
153 return oe_in_pack_pos(writer.to_pack, entry);
156 static void show_object(struct object *object, const char *name, void *data)
158 struct bitmap *base = data;
159 bitmap_set(base, find_object_pos(object->oid.hash));
160 mark_as_seen(object);
163 static void show_commit(struct commit *commit, void *data)
165 mark_as_seen((struct object *)commit);
168 static int
169 add_to_include_set(struct bitmap *base, struct commit *commit)
171 khiter_t hash_pos;
172 uint32_t bitmap_pos = find_object_pos(commit->object.oid.hash);
174 if (bitmap_get(base, bitmap_pos))
175 return 0;
177 hash_pos = kh_get_sha1(writer.bitmaps, commit->object.oid.hash);
178 if (hash_pos < kh_end(writer.bitmaps)) {
179 struct bitmapped_commit *bc = kh_value(writer.bitmaps, hash_pos);
180 bitmap_or_ewah(base, bc->bitmap);
181 return 0;
184 bitmap_set(base, bitmap_pos);
185 return 1;
188 static int
189 should_include(struct commit *commit, void *_data)
191 struct bitmap *base = _data;
193 if (!add_to_include_set(base, commit)) {
194 struct commit_list *parent = commit->parents;
196 mark_as_seen((struct object *)commit);
198 while (parent) {
199 parent->item->object.flags |= SEEN;
200 mark_as_seen((struct object *)parent->item);
201 parent = parent->next;
204 return 0;
207 return 1;
210 static void compute_xor_offsets(void)
212 static const int MAX_XOR_OFFSET_SEARCH = 10;
214 int i, next = 0;
216 while (next < writer.selected_nr) {
217 struct bitmapped_commit *stored = &writer.selected[next];
219 int best_offset = 0;
220 struct ewah_bitmap *best_bitmap = stored->bitmap;
221 struct ewah_bitmap *test_xor;
223 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
224 int curr = next - i;
226 if (curr < 0)
227 break;
229 test_xor = ewah_pool_new();
230 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
232 if (test_xor->buffer_size < best_bitmap->buffer_size) {
233 if (best_bitmap != stored->bitmap)
234 ewah_pool_free(best_bitmap);
236 best_bitmap = test_xor;
237 best_offset = i;
238 } else {
239 ewah_pool_free(test_xor);
243 stored->xor_offset = best_offset;
244 stored->write_as = best_bitmap;
246 next++;
250 void bitmap_writer_build(struct packing_data *to_pack)
252 static const double REUSE_BITMAP_THRESHOLD = 0.2;
254 int i, reuse_after, need_reset;
255 struct bitmap *base = bitmap_new();
256 struct rev_info revs;
258 writer.bitmaps = kh_init_sha1();
259 writer.to_pack = to_pack;
261 if (writer.show_progress)
262 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
264 init_revisions(&revs, NULL);
265 revs.tag_objects = 1;
266 revs.tree_objects = 1;
267 revs.blob_objects = 1;
268 revs.no_walk = 0;
270 revs.include_check = should_include;
271 reset_revision_walk();
273 reuse_after = writer.selected_nr * REUSE_BITMAP_THRESHOLD;
274 need_reset = 0;
276 for (i = writer.selected_nr - 1; i >= 0; --i) {
277 struct bitmapped_commit *stored;
278 struct object *object;
280 khiter_t hash_pos;
281 int hash_ret;
283 stored = &writer.selected[i];
284 object = (struct object *)stored->commit;
286 if (stored->bitmap == NULL) {
287 if (i < writer.selected_nr - 1 &&
288 (need_reset ||
289 !in_merge_bases(writer.selected[i + 1].commit,
290 stored->commit))) {
291 bitmap_reset(base);
292 reset_all_seen();
295 add_pending_object(&revs, object, "");
296 revs.include_check_data = base;
298 if (prepare_revision_walk(&revs))
299 die("revision walk setup failed");
301 traverse_commit_list(&revs, show_commit, show_object, base);
303 object_array_clear(&revs.pending);
305 stored->bitmap = bitmap_to_ewah(base);
306 need_reset = 0;
307 } else
308 need_reset = 1;
310 if (i >= reuse_after)
311 stored->flags |= BITMAP_FLAG_REUSE;
313 hash_pos = kh_put_sha1(writer.bitmaps, object->oid.hash, &hash_ret);
314 if (hash_ret == 0)
315 die("Duplicate entry when writing index: %s",
316 oid_to_hex(&object->oid));
318 kh_value(writer.bitmaps, hash_pos) = stored;
319 display_progress(writer.progress, writer.selected_nr - i);
322 bitmap_free(base);
323 stop_progress(&writer.progress);
325 compute_xor_offsets();
329 * Select the commits that will be bitmapped
331 static inline unsigned int next_commit_index(unsigned int idx)
333 static const unsigned int MIN_COMMITS = 100;
334 static const unsigned int MAX_COMMITS = 5000;
336 static const unsigned int MUST_REGION = 100;
337 static const unsigned int MIN_REGION = 20000;
339 unsigned int offset, next;
341 if (idx <= MUST_REGION)
342 return 0;
344 if (idx <= MIN_REGION) {
345 offset = idx - MUST_REGION;
346 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
349 offset = idx - MIN_REGION;
350 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
352 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
355 static int date_compare(const void *_a, const void *_b)
357 struct commit *a = *(struct commit **)_a;
358 struct commit *b = *(struct commit **)_b;
359 return (long)b->date - (long)a->date;
362 void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
364 struct bitmap_index *bitmap_git;
365 if (!(bitmap_git = prepare_bitmap_git()))
366 return;
368 writer.reused = kh_init_sha1();
369 rebuild_existing_bitmaps(bitmap_git, to_pack, writer.reused,
370 writer.show_progress);
372 * NEEDSWORK: rebuild_existing_bitmaps() makes writer.reused reference
373 * some bitmaps in bitmap_git, so we can't free the latter.
377 static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
379 khiter_t hash_pos;
381 if (!writer.reused)
382 return NULL;
384 hash_pos = kh_get_sha1(writer.reused, sha1);
385 if (hash_pos >= kh_end(writer.reused))
386 return NULL;
388 return kh_value(writer.reused, hash_pos);
391 void bitmap_writer_select_commits(struct commit **indexed_commits,
392 unsigned int indexed_commits_nr,
393 int max_bitmaps)
395 unsigned int i = 0, j, next;
397 QSORT(indexed_commits, indexed_commits_nr, date_compare);
399 if (writer.show_progress)
400 writer.progress = start_progress("Selecting bitmap commits", 0);
402 if (indexed_commits_nr < 100) {
403 for (i = 0; i < indexed_commits_nr; ++i)
404 push_bitmapped_commit(indexed_commits[i], NULL);
405 return;
408 for (;;) {
409 struct ewah_bitmap *reused_bitmap = NULL;
410 struct commit *chosen = NULL;
412 next = next_commit_index(i);
414 if (i + next >= indexed_commits_nr)
415 break;
417 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
418 writer.selected_nr = max_bitmaps;
419 break;
422 if (next == 0) {
423 chosen = indexed_commits[i];
424 reused_bitmap = find_reused_bitmap(chosen->object.oid.hash);
425 } else {
426 chosen = indexed_commits[i + next];
428 for (j = 0; j <= next; ++j) {
429 struct commit *cm = indexed_commits[i + j];
431 reused_bitmap = find_reused_bitmap(cm->object.oid.hash);
432 if (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {
433 chosen = cm;
434 break;
437 if (cm->parents && cm->parents->next)
438 chosen = cm;
442 push_bitmapped_commit(chosen, reused_bitmap);
444 i += next + 1;
445 display_progress(writer.progress, i);
448 stop_progress(&writer.progress);
452 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
454 /* hashwrite will die on error */
455 hashwrite(f, buf, len);
456 return len;
460 * Write the bitmap index to disk
462 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
464 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
465 die("Failed to write bitmap index");
468 static const unsigned char *sha1_access(size_t pos, void *table)
470 struct pack_idx_entry **index = table;
471 return index[pos]->oid.hash;
474 static void write_selected_commits_v1(struct hashfile *f,
475 struct pack_idx_entry **index,
476 uint32_t index_nr)
478 int i;
480 for (i = 0; i < writer.selected_nr; ++i) {
481 struct bitmapped_commit *stored = &writer.selected[i];
483 int commit_pos =
484 sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
486 if (commit_pos < 0)
487 BUG("trying to write commit not in index");
489 hashwrite_be32(f, commit_pos);
490 hashwrite_u8(f, stored->xor_offset);
491 hashwrite_u8(f, stored->flags);
493 dump_bitmap(f, stored->write_as);
497 static void write_hash_cache(struct hashfile *f,
498 struct pack_idx_entry **index,
499 uint32_t index_nr)
501 uint32_t i;
503 for (i = 0; i < index_nr; ++i) {
504 struct object_entry *entry = (struct object_entry *)index[i];
505 uint32_t hash_value = htonl(entry->hash);
506 hashwrite(f, &hash_value, sizeof(hash_value));
510 void bitmap_writer_set_checksum(unsigned char *sha1)
512 hashcpy(writer.pack_checksum, sha1);
515 void bitmap_writer_finish(struct pack_idx_entry **index,
516 uint32_t index_nr,
517 const char *filename,
518 uint16_t options)
520 static uint16_t default_version = 1;
521 static uint16_t flags = BITMAP_OPT_FULL_DAG;
522 struct strbuf tmp_file = STRBUF_INIT;
523 struct hashfile *f;
525 struct bitmap_disk_header header;
527 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
529 f = hashfd(fd, tmp_file.buf);
531 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
532 header.version = htons(default_version);
533 header.options = htons(flags | options);
534 header.entry_count = htonl(writer.selected_nr);
535 hashcpy(header.checksum, writer.pack_checksum);
537 hashwrite(f, &header, sizeof(header));
538 dump_bitmap(f, writer.commits);
539 dump_bitmap(f, writer.trees);
540 dump_bitmap(f, writer.blobs);
541 dump_bitmap(f, writer.tags);
542 write_selected_commits_v1(f, index, index_nr);
544 if (options & BITMAP_OPT_HASH_CACHE)
545 write_hash_cache(f, index, index_nr);
547 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
549 if (adjust_shared_perm(tmp_file.buf))
550 die_errno("unable to make temporary bitmap file readable");
552 if (rename(tmp_file.buf, filename))
553 die_errno("unable to rename temporary bitmap file to '%s'", filename);
555 strbuf_release(&tmp_file);