Merge branch 'jt/remove-pack-bitmap-global'
[git/git.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
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;
22 };
23
24 struct bitmap_writer {
25 struct ewah_bitmap *commits;
26 struct ewah_bitmap *trees;
27 struct ewah_bitmap *blobs;
28 struct ewah_bitmap *tags;
29
30 khash_sha1 *bitmaps;
31 khash_sha1 *reused;
32 struct packing_data *to_pack;
33
34 struct bitmapped_commit *selected;
35 unsigned int selected_nr, selected_alloc;
36
37 struct progress *progress;
38 int show_progress;
39 unsigned char pack_checksum[20];
40 };
41
42 static struct bitmap_writer writer;
43
44 void bitmap_writer_show_progress(int show)
45 {
46 writer.show_progress = show;
47 }
48
49 /**
50 * Build the initial type index for the packfile
51 */
52 void bitmap_writer_build_type_index(struct packing_data *to_pack,
53 struct pack_idx_entry **index,
54 uint32_t index_nr)
55 {
56 uint32_t i;
57
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);
63
64 for (i = 0; i < index_nr; ++i) {
65 struct object_entry *entry = (struct object_entry *)index[i];
66 enum object_type real_type;
67
68 oe_set_in_pack_pos(to_pack, entry, i);
69
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;
77
78 default:
79 real_type = oid_object_info(the_repository,
80 &entry->idx.oid, NULL);
81 break;
82 }
83
84 switch (real_type) {
85 case OBJ_COMMIT:
86 ewah_set(writer.commits, i);
87 break;
88
89 case OBJ_TREE:
90 ewah_set(writer.trees, i);
91 break;
92
93 case OBJ_BLOB:
94 ewah_set(writer.blobs, i);
95 break;
96
97 case OBJ_TAG:
98 ewah_set(writer.tags, i);
99 break;
100
101 default:
102 die("Missing type information for %s (%d/%d)",
103 oid_to_hex(&entry->idx.oid), real_type,
104 oe_type(entry));
105 }
106 }
107 }
108
109 /**
110 * Compute the actual bitmaps
111 */
112 static struct object **seen_objects;
113 static unsigned int seen_objects_nr, seen_objects_alloc;
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 inline void mark_as_seen(struct object *object)
130 {
131 ALLOC_GROW(seen_objects, seen_objects_nr + 1, seen_objects_alloc);
132 seen_objects[seen_objects_nr++] = object;
133 }
134
135 static inline void reset_all_seen(void)
136 {
137 unsigned int i;
138 for (i = 0; i < seen_objects_nr; ++i) {
139 seen_objects[i]->flags &= ~(SEEN | ADDED | SHOWN);
140 }
141 seen_objects_nr = 0;
142 }
143
144 static uint32_t find_object_pos(const unsigned char *sha1)
145 {
146 struct object_entry *entry = packlist_find(writer.to_pack, sha1, NULL);
147
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));
151 }
152
153 return oe_in_pack_pos(writer.to_pack, entry);
154 }
155
156 static void show_object(struct object *object, const char *name, void *data)
157 {
158 struct bitmap *base = data;
159 bitmap_set(base, find_object_pos(object->oid.hash));
160 mark_as_seen(object);
161 }
162
163 static void show_commit(struct commit *commit, void *data)
164 {
165 mark_as_seen((struct object *)commit);
166 }
167
168 static int
169 add_to_include_set(struct bitmap *base, struct commit *commit)
170 {
171 khiter_t hash_pos;
172 uint32_t bitmap_pos = find_object_pos(commit->object.oid.hash);
173
174 if (bitmap_get(base, bitmap_pos))
175 return 0;
176
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;
182 }
183
184 bitmap_set(base, bitmap_pos);
185 return 1;
186 }
187
188 static int
189 should_include(struct commit *commit, void *_data)
190 {
191 struct bitmap *base = _data;
192
193 if (!add_to_include_set(base, commit)) {
194 struct commit_list *parent = commit->parents;
195
196 mark_as_seen((struct object *)commit);
197
198 while (parent) {
199 parent->item->object.flags |= SEEN;
200 mark_as_seen((struct object *)parent->item);
201 parent = parent->next;
202 }
203
204 return 0;
205 }
206
207 return 1;
208 }
209
210 static void compute_xor_offsets(void)
211 {
212 static const int MAX_XOR_OFFSET_SEARCH = 10;
213
214 int i, next = 0;
215
216 while (next < writer.selected_nr) {
217 struct bitmapped_commit *stored = &writer.selected[next];
218
219 int best_offset = 0;
220 struct ewah_bitmap *best_bitmap = stored->bitmap;
221 struct ewah_bitmap *test_xor;
222
223 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
224 int curr = next - i;
225
226 if (curr < 0)
227 break;
228
229 test_xor = ewah_pool_new();
230 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
231
232 if (test_xor->buffer_size < best_bitmap->buffer_size) {
233 if (best_bitmap != stored->bitmap)
234 ewah_pool_free(best_bitmap);
235
236 best_bitmap = test_xor;
237 best_offset = i;
238 } else {
239 ewah_pool_free(test_xor);
240 }
241 }
242
243 stored->xor_offset = best_offset;
244 stored->write_as = best_bitmap;
245
246 next++;
247 }
248 }
249
250 void bitmap_writer_build(struct packing_data *to_pack)
251 {
252 static const double REUSE_BITMAP_THRESHOLD = 0.2;
253
254 int i, reuse_after, need_reset;
255 struct bitmap *base = bitmap_new();
256 struct rev_info revs;
257
258 writer.bitmaps = kh_init_sha1();
259 writer.to_pack = to_pack;
260
261 if (writer.show_progress)
262 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
263
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;
269
270 revs.include_check = should_include;
271 reset_revision_walk();
272
273 reuse_after = writer.selected_nr * REUSE_BITMAP_THRESHOLD;
274 need_reset = 0;
275
276 for (i = writer.selected_nr - 1; i >= 0; --i) {
277 struct bitmapped_commit *stored;
278 struct object *object;
279
280 khiter_t hash_pos;
281 int hash_ret;
282
283 stored = &writer.selected[i];
284 object = (struct object *)stored->commit;
285
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();
293 }
294
295 add_pending_object(&revs, object, "");
296 revs.include_check_data = base;
297
298 if (prepare_revision_walk(&revs))
299 die("revision walk setup failed");
300
301 traverse_commit_list(&revs, show_commit, show_object, base);
302
303 object_array_clear(&revs.pending);
304
305 stored->bitmap = bitmap_to_ewah(base);
306 need_reset = 0;
307 } else
308 need_reset = 1;
309
310 if (i >= reuse_after)
311 stored->flags |= BITMAP_FLAG_REUSE;
312
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));
317
318 kh_value(writer.bitmaps, hash_pos) = stored;
319 display_progress(writer.progress, writer.selected_nr - i);
320 }
321
322 bitmap_free(base);
323 stop_progress(&writer.progress);
324
325 compute_xor_offsets();
326 }
327
328 /**
329 * Select the commits that will be bitmapped
330 */
331 static inline unsigned int next_commit_index(unsigned int idx)
332 {
333 static const unsigned int MIN_COMMITS = 100;
334 static const unsigned int MAX_COMMITS = 5000;
335
336 static const unsigned int MUST_REGION = 100;
337 static const unsigned int MIN_REGION = 20000;
338
339 unsigned int offset, next;
340
341 if (idx <= MUST_REGION)
342 return 0;
343
344 if (idx <= MIN_REGION) {
345 offset = idx - MUST_REGION;
346 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
347 }
348
349 offset = idx - MIN_REGION;
350 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
351
352 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
353 }
354
355 static int date_compare(const void *_a, const void *_b)
356 {
357 struct commit *a = *(struct commit **)_a;
358 struct commit *b = *(struct commit **)_b;
359 return (long)b->date - (long)a->date;
360 }
361
362 void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
363 {
364 struct bitmap_index *bitmap_git;
365 if (!(bitmap_git = prepare_bitmap_git()))
366 return;
367
368 writer.reused = kh_init_sha1();
369 rebuild_existing_bitmaps(bitmap_git, to_pack, writer.reused,
370 writer.show_progress);
371 /*
372 * NEEDSWORK: rebuild_existing_bitmaps() makes writer.reused reference
373 * some bitmaps in bitmap_git, so we can't free the latter.
374 */
375 }
376
377 static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
378 {
379 khiter_t hash_pos;
380
381 if (!writer.reused)
382 return NULL;
383
384 hash_pos = kh_get_sha1(writer.reused, sha1);
385 if (hash_pos >= kh_end(writer.reused))
386 return NULL;
387
388 return kh_value(writer.reused, hash_pos);
389 }
390
391 void bitmap_writer_select_commits(struct commit **indexed_commits,
392 unsigned int indexed_commits_nr,
393 int max_bitmaps)
394 {
395 unsigned int i = 0, j, next;
396
397 QSORT(indexed_commits, indexed_commits_nr, date_compare);
398
399 if (writer.show_progress)
400 writer.progress = start_progress("Selecting bitmap commits", 0);
401
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;
406 }
407
408 for (;;) {
409 struct ewah_bitmap *reused_bitmap = NULL;
410 struct commit *chosen = NULL;
411
412 next = next_commit_index(i);
413
414 if (i + next >= indexed_commits_nr)
415 break;
416
417 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
418 writer.selected_nr = max_bitmaps;
419 break;
420 }
421
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];
427
428 for (j = 0; j <= next; ++j) {
429 struct commit *cm = indexed_commits[i + j];
430
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;
435 }
436
437 if (cm->parents && cm->parents->next)
438 chosen = cm;
439 }
440 }
441
442 push_bitmapped_commit(chosen, reused_bitmap);
443
444 i += next + 1;
445 display_progress(writer.progress, i);
446 }
447
448 stop_progress(&writer.progress);
449 }
450
451
452 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
453 {
454 /* hashwrite will die on error */
455 hashwrite(f, buf, len);
456 return len;
457 }
458
459 /**
460 * Write the bitmap index to disk
461 */
462 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
463 {
464 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
465 die("Failed to write bitmap index");
466 }
467
468 static const unsigned char *sha1_access(size_t pos, void *table)
469 {
470 struct pack_idx_entry **index = table;
471 return index[pos]->oid.hash;
472 }
473
474 static void write_selected_commits_v1(struct hashfile *f,
475 struct pack_idx_entry **index,
476 uint32_t index_nr)
477 {
478 int i;
479
480 for (i = 0; i < writer.selected_nr; ++i) {
481 struct bitmapped_commit *stored = &writer.selected[i];
482
483 int commit_pos =
484 sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
485
486 if (commit_pos < 0)
487 BUG("trying to write commit not in index");
488
489 hashwrite_be32(f, commit_pos);
490 hashwrite_u8(f, stored->xor_offset);
491 hashwrite_u8(f, stored->flags);
492
493 dump_bitmap(f, stored->write_as);
494 }
495 }
496
497 static void write_hash_cache(struct hashfile *f,
498 struct pack_idx_entry **index,
499 uint32_t index_nr)
500 {
501 uint32_t i;
502
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));
507 }
508 }
509
510 void bitmap_writer_set_checksum(unsigned char *sha1)
511 {
512 hashcpy(writer.pack_checksum, sha1);
513 }
514
515 void bitmap_writer_finish(struct pack_idx_entry **index,
516 uint32_t index_nr,
517 const char *filename,
518 uint16_t options)
519 {
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;
524
525 struct bitmap_disk_header header;
526
527 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
528
529 f = hashfd(fd, tmp_file.buf);
530
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);
536
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);
543
544 if (options & BITMAP_OPT_HASH_CACHE)
545 write_hash_cache(f, index, index_nr);
546
547 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
548
549 if (adjust_shared_perm(tmp_file.buf))
550 die_errno("unable to make temporary bitmap file readable");
551
552 if (rename(tmp_file.buf, filename))
553 die_errno("unable to rename temporary bitmap file to '%s'", filename);
554
555 strbuf_release(&tmp_file);
556 }