Merge branch 'ld/p423'
[alt-git.git] / pack-bitmap-write.c
blob953c5dd84dff2d87b639d29b71d844b29f925618
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 if (prepare_bitmap_git() < 0)
365 return;
367 writer.reused = kh_init_sha1();
368 rebuild_existing_bitmaps(to_pack, writer.reused, writer.show_progress);
371 static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
373 khiter_t hash_pos;
375 if (!writer.reused)
376 return NULL;
378 hash_pos = kh_get_sha1(writer.reused, sha1);
379 if (hash_pos >= kh_end(writer.reused))
380 return NULL;
382 return kh_value(writer.reused, hash_pos);
385 void bitmap_writer_select_commits(struct commit **indexed_commits,
386 unsigned int indexed_commits_nr,
387 int max_bitmaps)
389 unsigned int i = 0, j, next;
391 QSORT(indexed_commits, indexed_commits_nr, date_compare);
393 if (writer.show_progress)
394 writer.progress = start_progress("Selecting bitmap commits", 0);
396 if (indexed_commits_nr < 100) {
397 for (i = 0; i < indexed_commits_nr; ++i)
398 push_bitmapped_commit(indexed_commits[i], NULL);
399 return;
402 for (;;) {
403 struct ewah_bitmap *reused_bitmap = NULL;
404 struct commit *chosen = NULL;
406 next = next_commit_index(i);
408 if (i + next >= indexed_commits_nr)
409 break;
411 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
412 writer.selected_nr = max_bitmaps;
413 break;
416 if (next == 0) {
417 chosen = indexed_commits[i];
418 reused_bitmap = find_reused_bitmap(chosen->object.oid.hash);
419 } else {
420 chosen = indexed_commits[i + next];
422 for (j = 0; j <= next; ++j) {
423 struct commit *cm = indexed_commits[i + j];
425 reused_bitmap = find_reused_bitmap(cm->object.oid.hash);
426 if (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {
427 chosen = cm;
428 break;
431 if (cm->parents && cm->parents->next)
432 chosen = cm;
436 push_bitmapped_commit(chosen, reused_bitmap);
438 i += next + 1;
439 display_progress(writer.progress, i);
442 stop_progress(&writer.progress);
446 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
448 /* hashwrite will die on error */
449 hashwrite(f, buf, len);
450 return len;
454 * Write the bitmap index to disk
456 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
458 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
459 die("Failed to write bitmap index");
462 static const unsigned char *sha1_access(size_t pos, void *table)
464 struct pack_idx_entry **index = table;
465 return index[pos]->oid.hash;
468 static void write_selected_commits_v1(struct hashfile *f,
469 struct pack_idx_entry **index,
470 uint32_t index_nr)
472 int i;
474 for (i = 0; i < writer.selected_nr; ++i) {
475 struct bitmapped_commit *stored = &writer.selected[i];
477 int commit_pos =
478 sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
480 if (commit_pos < 0)
481 BUG("trying to write commit not in index");
483 hashwrite_be32(f, commit_pos);
484 hashwrite_u8(f, stored->xor_offset);
485 hashwrite_u8(f, stored->flags);
487 dump_bitmap(f, stored->write_as);
491 static void write_hash_cache(struct hashfile *f,
492 struct pack_idx_entry **index,
493 uint32_t index_nr)
495 uint32_t i;
497 for (i = 0; i < index_nr; ++i) {
498 struct object_entry *entry = (struct object_entry *)index[i];
499 uint32_t hash_value = htonl(entry->hash);
500 hashwrite(f, &hash_value, sizeof(hash_value));
504 void bitmap_writer_set_checksum(unsigned char *sha1)
506 hashcpy(writer.pack_checksum, sha1);
509 void bitmap_writer_finish(struct pack_idx_entry **index,
510 uint32_t index_nr,
511 const char *filename,
512 uint16_t options)
514 static uint16_t default_version = 1;
515 static uint16_t flags = BITMAP_OPT_FULL_DAG;
516 struct strbuf tmp_file = STRBUF_INIT;
517 struct hashfile *f;
519 struct bitmap_disk_header header;
521 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
523 f = hashfd(fd, tmp_file.buf);
525 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
526 header.version = htons(default_version);
527 header.options = htons(flags | options);
528 header.entry_count = htonl(writer.selected_nr);
529 hashcpy(header.checksum, writer.pack_checksum);
531 hashwrite(f, &header, sizeof(header));
532 dump_bitmap(f, writer.commits);
533 dump_bitmap(f, writer.trees);
534 dump_bitmap(f, writer.blobs);
535 dump_bitmap(f, writer.tags);
536 write_selected_commits_v1(f, index, index_nr);
538 if (options & BITMAP_OPT_HASH_CACHE)
539 write_hash_cache(f, index, index_nr);
541 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
543 if (adjust_shared_perm(tmp_file.buf))
544 die_errno("unable to make temporary bitmap file readable");
546 if (rename(tmp_file.buf, filename))
547 die_errno("unable to rename temporary bitmap file to '%s'", filename);
549 strbuf_release(&tmp_file);