pack-bitmap-write: use existing bitmaps
[git] / pack-bitmap-write.c
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"
14 #include "commit-reach.h"
15 #include "prio-queue.h"
16
17 struct bitmapped_commit {
18         struct commit *commit;
19         struct ewah_bitmap *bitmap;
20         struct ewah_bitmap *write_as;
21         int flags;
22         int xor_offset;
23         uint32_t commit_pos;
24 };
25
26 struct bitmap_writer {
27         struct ewah_bitmap *commits;
28         struct ewah_bitmap *trees;
29         struct ewah_bitmap *blobs;
30         struct ewah_bitmap *tags;
31
32         kh_oid_map_t *bitmaps;
33         struct packing_data *to_pack;
34
35         struct bitmapped_commit *selected;
36         unsigned int selected_nr, selected_alloc;
37
38         struct progress *progress;
39         int show_progress;
40         unsigned char pack_checksum[GIT_MAX_RAWSZ];
41 };
42
43 static struct bitmap_writer writer;
44
45 void bitmap_writer_show_progress(int show)
46 {
47         writer.show_progress = show;
48 }
49
50 /**
51  * Build the initial type index for the packfile
52  */
53 void bitmap_writer_build_type_index(struct packing_data *to_pack,
54                                     struct pack_idx_entry **index,
55                                     uint32_t index_nr)
56 {
57         uint32_t i;
58
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);
64
65         for (i = 0; i < index_nr; ++i) {
66                 struct object_entry *entry = (struct object_entry *)index[i];
67                 enum object_type real_type;
68
69                 oe_set_in_pack_pos(to_pack, entry, i);
70
71                 switch (oe_type(entry)) {
72                 case OBJ_COMMIT:
73                 case OBJ_TREE:
74                 case OBJ_BLOB:
75                 case OBJ_TAG:
76                         real_type = oe_type(entry);
77                         break;
78
79                 default:
80                         real_type = oid_object_info(to_pack->repo,
81                                                     &entry->idx.oid, NULL);
82                         break;
83                 }
84
85                 switch (real_type) {
86                 case OBJ_COMMIT:
87                         ewah_set(writer.commits, i);
88                         break;
89
90                 case OBJ_TREE:
91                         ewah_set(writer.trees, i);
92                         break;
93
94                 case OBJ_BLOB:
95                         ewah_set(writer.blobs, i);
96                         break;
97
98                 case OBJ_TAG:
99                         ewah_set(writer.tags, i);
100                         break;
101
102                 default:
103                         die("Missing type information for %s (%d/%d)",
104                             oid_to_hex(&entry->idx.oid), real_type,
105                             oe_type(entry));
106                 }
107         }
108 }
109
110 /**
111  * Compute the actual bitmaps
112  */
113
114 static inline void push_bitmapped_commit(struct commit *commit)
115 {
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);
119         }
120
121         writer.selected[writer.selected_nr].commit = commit;
122         writer.selected[writer.selected_nr].bitmap = NULL;
123         writer.selected[writer.selected_nr].flags = 0;
124
125         writer.selected_nr++;
126 }
127
128 static uint32_t find_object_pos(const struct object_id *oid)
129 {
130         struct object_entry *entry = packlist_find(writer.to_pack, oid);
131
132         if (!entry) {
133                 die("Failed to write bitmap index. Packfile doesn't have full closure "
134                         "(object %s is missing)", oid_to_hex(oid));
135         }
136
137         return oe_in_pack_pos(writer.to_pack, entry);
138 }
139
140 static void compute_xor_offsets(void)
141 {
142         static const int MAX_XOR_OFFSET_SEARCH = 10;
143
144         int i, next = 0;
145
146         while (next < writer.selected_nr) {
147                 struct bitmapped_commit *stored = &writer.selected[next];
148
149                 int best_offset = 0;
150                 struct ewah_bitmap *best_bitmap = stored->bitmap;
151                 struct ewah_bitmap *test_xor;
152
153                 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
154                         int curr = next - i;
155
156                         if (curr < 0)
157                                 break;
158
159                         test_xor = ewah_pool_new();
160                         ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
161
162                         if (test_xor->buffer_size < best_bitmap->buffer_size) {
163                                 if (best_bitmap != stored->bitmap)
164                                         ewah_pool_free(best_bitmap);
165
166                                 best_bitmap = test_xor;
167                                 best_offset = i;
168                         } else {
169                                 ewah_pool_free(test_xor);
170                         }
171                 }
172
173                 stored->xor_offset = best_offset;
174                 stored->write_as = best_bitmap;
175
176                 next++;
177         }
178 }
179
180 struct bb_commit {
181         struct commit_list *reverse_edges;
182         struct bitmap *commit_mask;
183         struct bitmap *bitmap;
184         unsigned selected:1,
185                  maximal:1;
186         unsigned idx; /* within selected array */
187 };
188
189 define_commit_slab(bb_data, struct bb_commit);
190
191 struct bitmap_builder {
192         struct bb_data data;
193         struct commit **commits;
194         size_t commits_nr, commits_alloc;
195 };
196
197 static void bitmap_builder_init(struct bitmap_builder *bb,
198                                 struct bitmap_writer *writer)
199 {
200         struct rev_info revs;
201         struct commit *commit;
202         unsigned int i, num_maximal;
203
204         memset(bb, 0, sizeof(*bb));
205         init_bb_data(&bb->data);
206
207         reset_revision_walk();
208         repo_init_revisions(writer->to_pack->repo, &revs, NULL);
209         revs.topo_order = 1;
210
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);
214
215                 ent->selected = 1;
216                 ent->maximal = 1;
217                 ent->idx = i;
218
219                 ent->commit_mask = bitmap_new();
220                 bitmap_set(ent->commit_mask, i);
221
222                 add_pending_object(&revs, &c->object, "");
223         }
224         num_maximal = writer->selected_nr;
225
226         if (prepare_revision_walk(&revs))
227                 die("revision walk setup failed");
228
229         while ((commit = get_revision(&revs))) {
230                 struct commit_list *p;
231                 struct bb_commit *c_ent;
232
233                 parse_commit_or_die(commit);
234
235                 c_ent = bb_data_at(&bb->data, commit);
236
237                 if (c_ent->maximal) {
238                         if (!c_ent->selected) {
239                                 bitmap_set(c_ent->commit_mask, num_maximal);
240                                 num_maximal++;
241                         }
242
243                         ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
244                         bb->commits[bb->commits_nr++] = commit;
245                 }
246
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;
250
251                         if (!p_ent->commit_mask) {
252                                 p_ent->commit_mask = bitmap_new();
253                                 c_not_p = 1;
254                                 p_not_c = 0;
255                         } else {
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);
258                         }
259
260                         if (!c_not_p)
261                                 continue;
262
263                         bitmap_or(p_ent->commit_mask, c_ent->commit_mask);
264
265                         if (p_not_c)
266                                 p_ent->maximal = 1;
267                         else {
268                                 p_ent->maximal = 0;
269                                 free_commit_list(p_ent->reverse_edges);
270                                 p_ent->reverse_edges = NULL;
271                         }
272
273                         if (c_ent->maximal) {
274                                 commit_list_insert(commit, &p_ent->reverse_edges);
275                         } else {
276                                 struct commit_list *cc = c_ent->reverse_edges;
277
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);
281                                 }
282                         }
283                 }
284
285                 bitmap_free(c_ent->commit_mask);
286                 c_ent->commit_mask = NULL;
287         }
288
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);
293 }
294
295 static void bitmap_builder_clear(struct bitmap_builder *bb)
296 {
297         clear_bb_data(&bb->data);
298         free(bb->commits);
299         bb->commits_nr = bb->commits_alloc = 0;
300 }
301
302 static void fill_bitmap_tree(struct bitmap *bitmap,
303                              struct tree *tree)
304 {
305         uint32_t pos;
306         struct tree_desc desc;
307         struct name_entry entry;
308
309         /*
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.
312          */
313         pos = find_object_pos(&tree->object.oid);
314         if (bitmap_get(bitmap, pos))
315                 return;
316         bitmap_set(bitmap, pos);
317
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);
322
323         while (tree_entry(&desc, &entry)) {
324                 switch (object_type(entry.mode)) {
325                 case OBJ_TREE:
326                         fill_bitmap_tree(bitmap,
327                                          lookup_tree(the_repository, &entry.oid));
328                         break;
329                 case OBJ_BLOB:
330                         bitmap_set(bitmap, find_object_pos(&entry.oid));
331                         break;
332                 default:
333                         /* Gitlink, etc; not reachable */
334                         break;
335                 }
336         }
337
338         free_tree_buffer(tree);
339 }
340
341 static void fill_bitmap_commit(struct bb_commit *ent,
342                                struct commit *commit,
343                                struct prio_queue *queue,
344                                struct prio_queue *tree_queue,
345                                struct bitmap_index *old_bitmap,
346                                const uint32_t *mapping)
347 {
348         if (!ent->bitmap)
349                 ent->bitmap = bitmap_new();
350
351         prio_queue_put(queue, commit);
352
353         while (queue->nr) {
354                 struct commit_list *p;
355                 struct commit *c = prio_queue_get(queue);
356
357                 if (old_bitmap && mapping) {
358                         struct ewah_bitmap *old = bitmap_for_commit(old_bitmap, c);
359                         /*
360                          * If this commit has an old bitmap, then translate that
361                          * bitmap and add its bits to this one. No need to walk
362                          * parents or the tree for this commit.
363                          */
364                         if (old && !rebuild_bitmap(mapping, old, ent->bitmap))
365                                 continue;
366                 }
367
368                 /*
369                  * Mark ourselves and queue our tree. The commit
370                  * walk ensures we cover all parents.
371                  */
372                 bitmap_set(ent->bitmap, find_object_pos(&c->object.oid));
373                 prio_queue_put(tree_queue, get_commit_tree(c));
374
375                 for (p = c->parents; p; p = p->next) {
376                         int pos = find_object_pos(&p->item->object.oid);
377                         if (!bitmap_get(ent->bitmap, pos)) {
378                                 bitmap_set(ent->bitmap, pos);
379                                 prio_queue_put(queue, p->item);
380                         }
381                 }
382         }
383
384         while (tree_queue->nr)
385                 fill_bitmap_tree(ent->bitmap, prio_queue_get(tree_queue));
386 }
387
388 static void store_selected(struct bb_commit *ent, struct commit *commit)
389 {
390         struct bitmapped_commit *stored = &writer.selected[ent->idx];
391         khiter_t hash_pos;
392         int hash_ret;
393
394         stored->bitmap = bitmap_to_ewah(ent->bitmap);
395
396         hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret);
397         if (hash_ret == 0)
398                 die("Duplicate entry when writing index: %s",
399                     oid_to_hex(&commit->object.oid));
400         kh_value(writer.bitmaps, hash_pos) = stored;
401 }
402
403 void bitmap_writer_build(struct packing_data *to_pack)
404 {
405         struct bitmap_builder bb;
406         size_t i;
407         int nr_stored = 0; /* for progress */
408         struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
409         struct prio_queue tree_queue = { NULL };
410         struct bitmap_index *old_bitmap;
411         uint32_t *mapping;
412
413         writer.bitmaps = kh_init_oid_map();
414         writer.to_pack = to_pack;
415
416         if (writer.show_progress)
417                 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
418         trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
419                             the_repository);
420
421         old_bitmap = prepare_bitmap_git(to_pack->repo);
422         if (old_bitmap)
423                 mapping = create_bitmap_mapping(old_bitmap, to_pack);
424         else
425                 mapping = NULL;
426
427         bitmap_builder_init(&bb, &writer);
428         for (i = bb.commits_nr; i > 0; i--) {
429                 struct commit *commit = bb.commits[i-1];
430                 struct bb_commit *ent = bb_data_at(&bb.data, commit);
431                 struct commit *child;
432                 int reused = 0;
433
434                 fill_bitmap_commit(ent, commit, &queue, &tree_queue,
435                                    old_bitmap, mapping);
436
437                 if (ent->selected) {
438                         store_selected(ent, commit);
439                         nr_stored++;
440                         display_progress(writer.progress, nr_stored);
441                 }
442
443                 while ((child = pop_commit(&ent->reverse_edges))) {
444                         struct bb_commit *child_ent =
445                                 bb_data_at(&bb.data, child);
446
447                         if (child_ent->bitmap)
448                                 bitmap_or(child_ent->bitmap, ent->bitmap);
449                         else if (reused)
450                                 child_ent->bitmap = bitmap_dup(ent->bitmap);
451                         else {
452                                 child_ent->bitmap = ent->bitmap;
453                                 reused = 1;
454                         }
455                 }
456                 if (!reused)
457                         bitmap_free(ent->bitmap);
458                 ent->bitmap = NULL;
459         }
460         clear_prio_queue(&queue);
461         clear_prio_queue(&tree_queue);
462         bitmap_builder_clear(&bb);
463         free(mapping);
464
465         trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
466                             the_repository);
467
468         stop_progress(&writer.progress);
469
470         compute_xor_offsets();
471 }
472
473 /**
474  * Select the commits that will be bitmapped
475  */
476 static inline unsigned int next_commit_index(unsigned int idx)
477 {
478         static const unsigned int MIN_COMMITS = 100;
479         static const unsigned int MAX_COMMITS = 5000;
480
481         static const unsigned int MUST_REGION = 100;
482         static const unsigned int MIN_REGION = 20000;
483
484         unsigned int offset, next;
485
486         if (idx <= MUST_REGION)
487                 return 0;
488
489         if (idx <= MIN_REGION) {
490                 offset = idx - MUST_REGION;
491                 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
492         }
493
494         offset = idx - MIN_REGION;
495         next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
496
497         return (next > MIN_COMMITS) ? next : MIN_COMMITS;
498 }
499
500 static int date_compare(const void *_a, const void *_b)
501 {
502         struct commit *a = *(struct commit **)_a;
503         struct commit *b = *(struct commit **)_b;
504         return (long)b->date - (long)a->date;
505 }
506
507 void bitmap_writer_select_commits(struct commit **indexed_commits,
508                                   unsigned int indexed_commits_nr,
509                                   int max_bitmaps)
510 {
511         unsigned int i = 0, j, next;
512
513         QSORT(indexed_commits, indexed_commits_nr, date_compare);
514
515         if (writer.show_progress)
516                 writer.progress = start_progress("Selecting bitmap commits", 0);
517
518         if (indexed_commits_nr < 100) {
519                 for (i = 0; i < indexed_commits_nr; ++i)
520                         push_bitmapped_commit(indexed_commits[i]);
521                 return;
522         }
523
524         for (;;) {
525                 struct commit *chosen = NULL;
526
527                 next = next_commit_index(i);
528
529                 if (i + next >= indexed_commits_nr)
530                         break;
531
532                 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
533                         writer.selected_nr = max_bitmaps;
534                         break;
535                 }
536
537                 if (next == 0) {
538                         chosen = indexed_commits[i];
539                 } else {
540                         chosen = indexed_commits[i + next];
541
542                         for (j = 0; j <= next; ++j) {
543                                 struct commit *cm = indexed_commits[i + j];
544
545                                 if ((cm->object.flags & NEEDS_BITMAP) != 0) {
546                                         chosen = cm;
547                                         break;
548                                 }
549
550                                 if (cm->parents && cm->parents->next)
551                                         chosen = cm;
552                         }
553                 }
554
555                 push_bitmapped_commit(chosen);
556
557                 i += next + 1;
558                 display_progress(writer.progress, i);
559         }
560
561         stop_progress(&writer.progress);
562 }
563
564
565 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
566 {
567         /* hashwrite will die on error */
568         hashwrite(f, buf, len);
569         return len;
570 }
571
572 /**
573  * Write the bitmap index to disk
574  */
575 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
576 {
577         if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
578                 die("Failed to write bitmap index");
579 }
580
581 static const unsigned char *sha1_access(size_t pos, void *table)
582 {
583         struct pack_idx_entry **index = table;
584         return index[pos]->oid.hash;
585 }
586
587 static void write_selected_commits_v1(struct hashfile *f,
588                                       struct pack_idx_entry **index,
589                                       uint32_t index_nr)
590 {
591         int i;
592
593         for (i = 0; i < writer.selected_nr; ++i) {
594                 struct bitmapped_commit *stored = &writer.selected[i];
595
596                 int commit_pos =
597                         sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
598
599                 if (commit_pos < 0)
600                         BUG("trying to write commit not in index");
601
602                 hashwrite_be32(f, commit_pos);
603                 hashwrite_u8(f, stored->xor_offset);
604                 hashwrite_u8(f, stored->flags);
605
606                 dump_bitmap(f, stored->write_as);
607         }
608 }
609
610 static void write_hash_cache(struct hashfile *f,
611                              struct pack_idx_entry **index,
612                              uint32_t index_nr)
613 {
614         uint32_t i;
615
616         for (i = 0; i < index_nr; ++i) {
617                 struct object_entry *entry = (struct object_entry *)index[i];
618                 hashwrite_be32(f, entry->hash);
619         }
620 }
621
622 void bitmap_writer_set_checksum(unsigned char *sha1)
623 {
624         hashcpy(writer.pack_checksum, sha1);
625 }
626
627 void bitmap_writer_finish(struct pack_idx_entry **index,
628                           uint32_t index_nr,
629                           const char *filename,
630                           uint16_t options)
631 {
632         static uint16_t default_version = 1;
633         static uint16_t flags = BITMAP_OPT_FULL_DAG;
634         struct strbuf tmp_file = STRBUF_INIT;
635         struct hashfile *f;
636
637         struct bitmap_disk_header header;
638
639         int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
640
641         f = hashfd(fd, tmp_file.buf);
642
643         memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
644         header.version = htons(default_version);
645         header.options = htons(flags | options);
646         header.entry_count = htonl(writer.selected_nr);
647         hashcpy(header.checksum, writer.pack_checksum);
648
649         hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
650         dump_bitmap(f, writer.commits);
651         dump_bitmap(f, writer.trees);
652         dump_bitmap(f, writer.blobs);
653         dump_bitmap(f, writer.tags);
654         write_selected_commits_v1(f, index, index_nr);
655
656         if (options & BITMAP_OPT_HASH_CACHE)
657                 write_hash_cache(f, index, index_nr);
658
659         finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
660
661         if (adjust_shared_perm(tmp_file.buf))
662                 die_errno("unable to make temporary bitmap file readable");
663
664         if (rename(tmp_file.buf, filename))
665                 die_errno("unable to rename temporary bitmap file to '%s'", filename);
666
667         strbuf_release(&tmp_file);
668 }