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