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