Merge branch 'ds/commit-graph'
[git/git.git] / pack-bitmap-write.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "list-objects.h"
7 #include "progress.h"
8 #include "pack-revindex.h"
9 #include "pack.h"
10 #include "pack-bitmap.h"
11 #include "sha1-lookup.h"
12 #include "pack-objects.h"
13
14 struct bitmapped_commit {
15 struct commit *commit;
16 struct ewah_bitmap *bitmap;
17 struct ewah_bitmap *write_as;
18 int flags;
19 int xor_offset;
20 uint32_t commit_pos;
21 };
22
23 struct bitmap_writer {
24 struct ewah_bitmap *commits;
25 struct ewah_bitmap *trees;
26 struct ewah_bitmap *blobs;
27 struct ewah_bitmap *tags;
28
29 khash_sha1 *bitmaps;
30 khash_sha1 *reused;
31 struct packing_data *to_pack;
32
33 struct bitmapped_commit *selected;
34 unsigned int selected_nr, selected_alloc;
35
36 struct progress *progress;
37 int show_progress;
38 unsigned char pack_checksum[20];
39 };
40
41 static struct bitmap_writer writer;
42
43 void bitmap_writer_show_progress(int show)
44 {
45 writer.show_progress = show;
46 }
47
48 /**
49 * Build the initial type index for the packfile
50 */
51 void bitmap_writer_build_type_index(struct pack_idx_entry **index,
52 uint32_t index_nr)
53 {
54 uint32_t i;
55
56 writer.commits = ewah_new();
57 writer.trees = ewah_new();
58 writer.blobs = ewah_new();
59 writer.tags = ewah_new();
60
61 for (i = 0; i < index_nr; ++i) {
62 struct object_entry *entry = (struct object_entry *)index[i];
63 enum object_type real_type;
64
65 entry->in_pack_pos = i;
66
67 switch (entry->type) {
68 case OBJ_COMMIT:
69 case OBJ_TREE:
70 case OBJ_BLOB:
71 case OBJ_TAG:
72 real_type = entry->type;
73 break;
74
75 default:
76 real_type = oid_object_info(&entry->idx.oid, NULL);
77 break;
78 }
79
80 switch (real_type) {
81 case OBJ_COMMIT:
82 ewah_set(writer.commits, i);
83 break;
84
85 case OBJ_TREE:
86 ewah_set(writer.trees, i);
87 break;
88
89 case OBJ_BLOB:
90 ewah_set(writer.blobs, i);
91 break;
92
93 case OBJ_TAG:
94 ewah_set(writer.tags, i);
95 break;
96
97 default:
98 die("Missing type information for %s (%d/%d)",
99 oid_to_hex(&entry->idx.oid), real_type,
100 entry->type);
101 }
102 }
103 }
104
105 /**
106 * Compute the actual bitmaps
107 */
108 static struct object **seen_objects;
109 static unsigned int seen_objects_nr, seen_objects_alloc;
110
111 static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitmap *reused)
112 {
113 if (writer.selected_nr >= writer.selected_alloc) {
114 writer.selected_alloc = (writer.selected_alloc + 32) * 2;
115 REALLOC_ARRAY(writer.selected, writer.selected_alloc);
116 }
117
118 writer.selected[writer.selected_nr].commit = commit;
119 writer.selected[writer.selected_nr].bitmap = reused;
120 writer.selected[writer.selected_nr].flags = 0;
121
122 writer.selected_nr++;
123 }
124
125 static inline void mark_as_seen(struct object *object)
126 {
127 ALLOC_GROW(seen_objects, seen_objects_nr + 1, seen_objects_alloc);
128 seen_objects[seen_objects_nr++] = object;
129 }
130
131 static inline void reset_all_seen(void)
132 {
133 unsigned int i;
134 for (i = 0; i < seen_objects_nr; ++i) {
135 seen_objects[i]->flags &= ~(SEEN | ADDED | SHOWN);
136 }
137 seen_objects_nr = 0;
138 }
139
140 static uint32_t find_object_pos(const unsigned char *sha1)
141 {
142 struct object_entry *entry = packlist_find(writer.to_pack, sha1, NULL);
143
144 if (!entry) {
145 die("Failed to write bitmap index. Packfile doesn't have full closure "
146 "(object %s is missing)", sha1_to_hex(sha1));
147 }
148
149 return entry->in_pack_pos;
150 }
151
152 static void show_object(struct object *object, const char *name, void *data)
153 {
154 struct bitmap *base = data;
155 bitmap_set(base, find_object_pos(object->oid.hash));
156 mark_as_seen(object);
157 }
158
159 static void show_commit(struct commit *commit, void *data)
160 {
161 mark_as_seen((struct object *)commit);
162 }
163
164 static int
165 add_to_include_set(struct bitmap *base, struct commit *commit)
166 {
167 khiter_t hash_pos;
168 uint32_t bitmap_pos = find_object_pos(commit->object.oid.hash);
169
170 if (bitmap_get(base, bitmap_pos))
171 return 0;
172
173 hash_pos = kh_get_sha1(writer.bitmaps, commit->object.oid.hash);
174 if (hash_pos < kh_end(writer.bitmaps)) {
175 struct bitmapped_commit *bc = kh_value(writer.bitmaps, hash_pos);
176 bitmap_or_ewah(base, bc->bitmap);
177 return 0;
178 }
179
180 bitmap_set(base, bitmap_pos);
181 return 1;
182 }
183
184 static int
185 should_include(struct commit *commit, void *_data)
186 {
187 struct bitmap *base = _data;
188
189 if (!add_to_include_set(base, commit)) {
190 struct commit_list *parent = commit->parents;
191
192 mark_as_seen((struct object *)commit);
193
194 while (parent) {
195 parent->item->object.flags |= SEEN;
196 mark_as_seen((struct object *)parent->item);
197 parent = parent->next;
198 }
199
200 return 0;
201 }
202
203 return 1;
204 }
205
206 static void compute_xor_offsets(void)
207 {
208 static const int MAX_XOR_OFFSET_SEARCH = 10;
209
210 int i, next = 0;
211
212 while (next < writer.selected_nr) {
213 struct bitmapped_commit *stored = &writer.selected[next];
214
215 int best_offset = 0;
216 struct ewah_bitmap *best_bitmap = stored->bitmap;
217 struct ewah_bitmap *test_xor;
218
219 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
220 int curr = next - i;
221
222 if (curr < 0)
223 break;
224
225 test_xor = ewah_pool_new();
226 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
227
228 if (test_xor->buffer_size < best_bitmap->buffer_size) {
229 if (best_bitmap != stored->bitmap)
230 ewah_pool_free(best_bitmap);
231
232 best_bitmap = test_xor;
233 best_offset = i;
234 } else {
235 ewah_pool_free(test_xor);
236 }
237 }
238
239 stored->xor_offset = best_offset;
240 stored->write_as = best_bitmap;
241
242 next++;
243 }
244 }
245
246 void bitmap_writer_build(struct packing_data *to_pack)
247 {
248 static const double REUSE_BITMAP_THRESHOLD = 0.2;
249
250 int i, reuse_after, need_reset;
251 struct bitmap *base = bitmap_new();
252 struct rev_info revs;
253
254 writer.bitmaps = kh_init_sha1();
255 writer.to_pack = to_pack;
256
257 if (writer.show_progress)
258 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
259
260 init_revisions(&revs, NULL);
261 revs.tag_objects = 1;
262 revs.tree_objects = 1;
263 revs.blob_objects = 1;
264 revs.no_walk = 0;
265
266 revs.include_check = should_include;
267 reset_revision_walk();
268
269 reuse_after = writer.selected_nr * REUSE_BITMAP_THRESHOLD;
270 need_reset = 0;
271
272 for (i = writer.selected_nr - 1; i >= 0; --i) {
273 struct bitmapped_commit *stored;
274 struct object *object;
275
276 khiter_t hash_pos;
277 int hash_ret;
278
279 stored = &writer.selected[i];
280 object = (struct object *)stored->commit;
281
282 if (stored->bitmap == NULL) {
283 if (i < writer.selected_nr - 1 &&
284 (need_reset ||
285 !in_merge_bases(writer.selected[i + 1].commit,
286 stored->commit))) {
287 bitmap_reset(base);
288 reset_all_seen();
289 }
290
291 add_pending_object(&revs, object, "");
292 revs.include_check_data = base;
293
294 if (prepare_revision_walk(&revs))
295 die("revision walk setup failed");
296
297 traverse_commit_list(&revs, show_commit, show_object, base);
298
299 object_array_clear(&revs.pending);
300
301 stored->bitmap = bitmap_to_ewah(base);
302 need_reset = 0;
303 } else
304 need_reset = 1;
305
306 if (i >= reuse_after)
307 stored->flags |= BITMAP_FLAG_REUSE;
308
309 hash_pos = kh_put_sha1(writer.bitmaps, object->oid.hash, &hash_ret);
310 if (hash_ret == 0)
311 die("Duplicate entry when writing index: %s",
312 oid_to_hex(&object->oid));
313
314 kh_value(writer.bitmaps, hash_pos) = stored;
315 display_progress(writer.progress, writer.selected_nr - i);
316 }
317
318 bitmap_free(base);
319 stop_progress(&writer.progress);
320
321 compute_xor_offsets();
322 }
323
324 /**
325 * Select the commits that will be bitmapped
326 */
327 static inline unsigned int next_commit_index(unsigned int idx)
328 {
329 static const unsigned int MIN_COMMITS = 100;
330 static const unsigned int MAX_COMMITS = 5000;
331
332 static const unsigned int MUST_REGION = 100;
333 static const unsigned int MIN_REGION = 20000;
334
335 unsigned int offset, next;
336
337 if (idx <= MUST_REGION)
338 return 0;
339
340 if (idx <= MIN_REGION) {
341 offset = idx - MUST_REGION;
342 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
343 }
344
345 offset = idx - MIN_REGION;
346 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
347
348 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
349 }
350
351 static int date_compare(const void *_a, const void *_b)
352 {
353 struct commit *a = *(struct commit **)_a;
354 struct commit *b = *(struct commit **)_b;
355 return (long)b->date - (long)a->date;
356 }
357
358 void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
359 {
360 if (prepare_bitmap_git() < 0)
361 return;
362
363 writer.reused = kh_init_sha1();
364 rebuild_existing_bitmaps(to_pack, writer.reused, writer.show_progress);
365 }
366
367 static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
368 {
369 khiter_t hash_pos;
370
371 if (!writer.reused)
372 return NULL;
373
374 hash_pos = kh_get_sha1(writer.reused, sha1);
375 if (hash_pos >= kh_end(writer.reused))
376 return NULL;
377
378 return kh_value(writer.reused, hash_pos);
379 }
380
381 void bitmap_writer_select_commits(struct commit **indexed_commits,
382 unsigned int indexed_commits_nr,
383 int max_bitmaps)
384 {
385 unsigned int i = 0, j, next;
386
387 QSORT(indexed_commits, indexed_commits_nr, date_compare);
388
389 if (writer.show_progress)
390 writer.progress = start_progress("Selecting bitmap commits", 0);
391
392 if (indexed_commits_nr < 100) {
393 for (i = 0; i < indexed_commits_nr; ++i)
394 push_bitmapped_commit(indexed_commits[i], NULL);
395 return;
396 }
397
398 for (;;) {
399 struct ewah_bitmap *reused_bitmap = NULL;
400 struct commit *chosen = NULL;
401
402 next = next_commit_index(i);
403
404 if (i + next >= indexed_commits_nr)
405 break;
406
407 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
408 writer.selected_nr = max_bitmaps;
409 break;
410 }
411
412 if (next == 0) {
413 chosen = indexed_commits[i];
414 reused_bitmap = find_reused_bitmap(chosen->object.oid.hash);
415 } else {
416 chosen = indexed_commits[i + next];
417
418 for (j = 0; j <= next; ++j) {
419 struct commit *cm = indexed_commits[i + j];
420
421 reused_bitmap = find_reused_bitmap(cm->object.oid.hash);
422 if (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {
423 chosen = cm;
424 break;
425 }
426
427 if (cm->parents && cm->parents->next)
428 chosen = cm;
429 }
430 }
431
432 push_bitmapped_commit(chosen, reused_bitmap);
433
434 i += next + 1;
435 display_progress(writer.progress, i);
436 }
437
438 stop_progress(&writer.progress);
439 }
440
441
442 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
443 {
444 /* hashwrite will die on error */
445 hashwrite(f, buf, len);
446 return len;
447 }
448
449 /**
450 * Write the bitmap index to disk
451 */
452 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
453 {
454 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
455 die("Failed to write bitmap index");
456 }
457
458 static const unsigned char *sha1_access(size_t pos, void *table)
459 {
460 struct pack_idx_entry **index = table;
461 return index[pos]->oid.hash;
462 }
463
464 static void write_selected_commits_v1(struct hashfile *f,
465 struct pack_idx_entry **index,
466 uint32_t index_nr)
467 {
468 int i;
469
470 for (i = 0; i < writer.selected_nr; ++i) {
471 struct bitmapped_commit *stored = &writer.selected[i];
472
473 int commit_pos =
474 sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
475
476 if (commit_pos < 0)
477 die("BUG: trying to write commit not in index");
478
479 hashwrite_be32(f, commit_pos);
480 hashwrite_u8(f, stored->xor_offset);
481 hashwrite_u8(f, stored->flags);
482
483 dump_bitmap(f, stored->write_as);
484 }
485 }
486
487 static void write_hash_cache(struct hashfile *f,
488 struct pack_idx_entry **index,
489 uint32_t index_nr)
490 {
491 uint32_t i;
492
493 for (i = 0; i < index_nr; ++i) {
494 struct object_entry *entry = (struct object_entry *)index[i];
495 uint32_t hash_value = htonl(entry->hash);
496 hashwrite(f, &hash_value, sizeof(hash_value));
497 }
498 }
499
500 void bitmap_writer_set_checksum(unsigned char *sha1)
501 {
502 hashcpy(writer.pack_checksum, sha1);
503 }
504
505 void bitmap_writer_finish(struct pack_idx_entry **index,
506 uint32_t index_nr,
507 const char *filename,
508 uint16_t options)
509 {
510 static uint16_t default_version = 1;
511 static uint16_t flags = BITMAP_OPT_FULL_DAG;
512 struct strbuf tmp_file = STRBUF_INIT;
513 struct hashfile *f;
514
515 struct bitmap_disk_header header;
516
517 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
518
519 f = hashfd(fd, tmp_file.buf);
520
521 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
522 header.version = htons(default_version);
523 header.options = htons(flags | options);
524 header.entry_count = htonl(writer.selected_nr);
525 hashcpy(header.checksum, writer.pack_checksum);
526
527 hashwrite(f, &header, sizeof(header));
528 dump_bitmap(f, writer.commits);
529 dump_bitmap(f, writer.trees);
530 dump_bitmap(f, writer.blobs);
531 dump_bitmap(f, writer.tags);
532 write_selected_commits_v1(f, index, index_nr);
533
534 if (options & BITMAP_OPT_HASH_CACHE)
535 write_hash_cache(f, index, index_nr);
536
537 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
538
539 if (adjust_shared_perm(tmp_file.buf))
540 die_errno("unable to make temporary bitmap file readable");
541
542 if (rename(tmp_file.buf, filename))
543 die_errno("unable to rename temporary bitmap file to '%s'", filename);
544
545 strbuf_release(&tmp_file);
546 }