blob: c43375bd344f22473adcd7cac09c9f1572e27079 [file] [log] [blame]
Vicent Marti7cc8f972013-12-21 09:00:16 -05001#include "cache.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07002#include "object-store.h"
Vicent Marti7cc8f972013-12-21 09:00:16 -05003#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"
Martin Ågrenbc626922020-12-31 12:56:23 +010012#include "hash-lookup.h"
Vicent Marti7cc8f972013-12-21 09:00:16 -050013#include "pack-objects.h"
Derrick Stolee64043552018-07-20 16:33:04 +000014#include "commit-reach.h"
Derrick Stolee6dc5ef72020-12-08 17:04:03 -050015#include "prio-queue.h"
Vicent Marti7cc8f972013-12-21 09:00:16 -050016
17struct 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
26struct bitmap_writer {
27 struct ewah_bitmap *commits;
28 struct ewah_bitmap *trees;
29 struct ewah_bitmap *blobs;
30 struct ewah_bitmap *tags;
31
Jeff Kingd2bc62b2019-06-20 03:41:35 -040032 kh_oid_map_t *bitmaps;
Vicent Marti7cc8f972013-12-21 09:00:16 -050033 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;
brian m. carlson825544a2018-10-15 00:01:49 +000040 unsigned char pack_checksum[GIT_MAX_RAWSZ];
Vicent Marti7cc8f972013-12-21 09:00:16 -050041};
42
43static struct bitmap_writer writer;
44
45void bitmap_writer_show_progress(int show)
46{
47 writer.show_progress = show;
48}
49
50/**
Taylor Blau0f533c72021-08-31 16:52:21 -040051 * Build the initial type index for the packfile or multi-pack-index
Vicent Marti7cc8f972013-12-21 09:00:16 -050052 */
Nguyễn Thái Ngọc Duy06af3bb2018-04-14 17:35:04 +020053void bitmap_writer_build_type_index(struct packing_data *to_pack,
54 struct pack_idx_entry **index,
Vicent Marti7cc8f972013-12-21 09:00:16 -050055 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();
Nguyễn Thái Ngọc Duy06af3bb2018-04-14 17:35:04 +020063 ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
Vicent Marti7cc8f972013-12-21 09:00:16 -050064
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
Nguyễn Thái Ngọc Duy06af3bb2018-04-14 17:35:04 +020069 oe_set_in_pack_pos(to_pack, entry, i);
Vicent Marti7cc8f972013-12-21 09:00:16 -050070
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +020071 switch (oe_type(entry)) {
Vicent Marti7cc8f972013-12-21 09:00:16 -050072 case OBJ_COMMIT:
73 case OBJ_TREE:
74 case OBJ_BLOB:
75 case OBJ_TAG:
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +020076 real_type = oe_type(entry);
Vicent Marti7cc8f972013-12-21 09:00:16 -050077 break;
78
79 default:
Nguyễn Thái Ngọc Duy7c141122018-11-10 06:49:08 +010080 real_type = oid_object_info(to_pack->repo,
Stefan Beller0df8e962018-04-25 11:20:59 -070081 &entry->idx.oid, NULL);
Vicent Marti7cc8f972013-12-21 09:00:16 -050082 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)",
brian m. carlsone6a492b2017-05-06 22:10:11 +0000104 oid_to_hex(&entry->idx.oid), real_type,
Nguyễn Thái Ngọc Duyfd9b1ba2018-04-14 17:35:01 +0200105 oe_type(entry));
Vicent Marti7cc8f972013-12-21 09:00:16 -0500106 }
107 }
108}
109
110/**
111 * Compute the actual bitmaps
112 */
Vicent Marti7cc8f972013-12-21 09:00:16 -0500113
Jeff King449fa5e2020-12-08 17:04:34 -0500114static inline void push_bitmapped_commit(struct commit *commit)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500115{
116 if (writer.selected_nr >= writer.selected_alloc) {
117 writer.selected_alloc = (writer.selected_alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200118 REALLOC_ARRAY(writer.selected, writer.selected_alloc);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500119 }
120
121 writer.selected[writer.selected_nr].commit = commit;
Jeff King449fa5e2020-12-08 17:04:34 -0500122 writer.selected[writer.selected_nr].bitmap = NULL;
Vicent Marti7cc8f972013-12-21 09:00:16 -0500123 writer.selected[writer.selected_nr].flags = 0;
124
125 writer.selected_nr++;
126}
127
Taylor Blau3ba3d062021-08-24 12:15:54 -0400128static uint32_t find_object_pos(const struct object_id *oid, int *found)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500129{
Jeff King3a378762019-09-05 21:36:05 -0400130 struct object_entry *entry = packlist_find(writer.to_pack, oid);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500131
132 if (!entry) {
Taylor Blau3ba3d062021-08-24 12:15:54 -0400133 if (found)
134 *found = 0;
135 warning("Failed to write bitmap index. Packfile doesn't have full closure "
Jeff King05805d72019-06-20 03:40:59 -0400136 "(object %s is missing)", oid_to_hex(oid));
Taylor Blau3ba3d062021-08-24 12:15:54 -0400137 return 0;
Vicent Marti7cc8f972013-12-21 09:00:16 -0500138 }
139
Taylor Blau3ba3d062021-08-24 12:15:54 -0400140 if (found)
141 *found = 1;
Nguyễn Thái Ngọc Duy06af3bb2018-04-14 17:35:04 +0200142 return oe_in_pack_pos(writer.to_pack, entry);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500143}
144
Vicent Marti7cc8f972013-12-21 09:00:16 -0500145static void compute_xor_offsets(void)
146{
147 static const int MAX_XOR_OFFSET_SEARCH = 10;
148
149 int i, next = 0;
150
151 while (next < writer.selected_nr) {
152 struct bitmapped_commit *stored = &writer.selected[next];
153
154 int best_offset = 0;
155 struct ewah_bitmap *best_bitmap = stored->bitmap;
156 struct ewah_bitmap *test_xor;
157
158 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
159 int curr = next - i;
160
161 if (curr < 0)
162 break;
163
164 test_xor = ewah_pool_new();
165 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
166
167 if (test_xor->buffer_size < best_bitmap->buffer_size) {
168 if (best_bitmap != stored->bitmap)
169 ewah_pool_free(best_bitmap);
170
171 best_bitmap = test_xor;
172 best_offset = i;
173 } else {
174 ewah_pool_free(test_xor);
175 }
176 }
177
178 stored->xor_offset = best_offset;
179 stored->write_as = best_bitmap;
180
181 next++;
182 }
183}
184
Jeff King4a9c5812020-12-08 17:03:55 -0500185struct bb_commit {
Derrick Stolee928e3f42020-12-08 17:04:22 -0500186 struct commit_list *reverse_edges;
Derrick Stolee089f7512020-12-08 17:04:30 -0500187 struct bitmap *commit_mask;
Jeff King4a9c5812020-12-08 17:03:55 -0500188 struct bitmap *bitmap;
Derrick Stolee089f7512020-12-08 17:04:30 -0500189 unsigned selected:1,
190 maximal:1;
Jeff King4a9c5812020-12-08 17:03:55 -0500191 unsigned idx; /* within selected array */
192};
193
194define_commit_slab(bb_data, struct bb_commit);
195
196struct bitmap_builder {
197 struct bb_data data;
198 struct commit **commits;
199 size_t commits_nr, commits_alloc;
200};
201
202static void bitmap_builder_init(struct bitmap_builder *bb,
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500203 struct bitmap_writer *writer,
204 struct bitmap_index *old_bitmap)
Jeff King4a9c5812020-12-08 17:03:55 -0500205{
206 struct rev_info revs;
207 struct commit *commit;
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500208 struct commit_list *reusable = NULL;
209 struct commit_list *r;
Derrick Stolee45f4eeb2020-12-08 17:05:26 -0500210 unsigned int i, num_maximal = 0;
Jeff King4a9c5812020-12-08 17:03:55 -0500211
212 memset(bb, 0, sizeof(*bb));
213 init_bb_data(&bb->data);
214
215 reset_revision_walk();
216 repo_init_revisions(writer->to_pack->repo, &revs, NULL);
217 revs.topo_order = 1;
Derrick Stolee45f4eeb2020-12-08 17:05:26 -0500218 revs.first_parent_only = 1;
Jeff King4a9c5812020-12-08 17:03:55 -0500219
220 for (i = 0; i < writer->selected_nr; i++) {
221 struct commit *c = writer->selected[i].commit;
222 struct bb_commit *ent = bb_data_at(&bb->data, c);
Derrick Stolee089f7512020-12-08 17:04:30 -0500223
Jeff King4a9c5812020-12-08 17:03:55 -0500224 ent->selected = 1;
Derrick Stolee089f7512020-12-08 17:04:30 -0500225 ent->maximal = 1;
Jeff King4a9c5812020-12-08 17:03:55 -0500226 ent->idx = i;
Derrick Stolee089f7512020-12-08 17:04:30 -0500227
228 ent->commit_mask = bitmap_new();
229 bitmap_set(ent->commit_mask, i);
230
Jeff King4a9c5812020-12-08 17:03:55 -0500231 add_pending_object(&revs, &c->object, "");
232 }
233
234 if (prepare_revision_walk(&revs))
235 die("revision walk setup failed");
236
237 while ((commit = get_revision(&revs))) {
Derrick Stolee45f4eeb2020-12-08 17:05:26 -0500238 struct commit_list *p = commit->parents;
Derrick Stolee089f7512020-12-08 17:04:30 -0500239 struct bb_commit *c_ent;
Jeff King4a9c5812020-12-08 17:03:55 -0500240
241 parse_commit_or_die(commit);
242
Derrick Stolee089f7512020-12-08 17:04:30 -0500243 c_ent = bb_data_at(&bb->data, commit);
244
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500245 /*
246 * If there is no commit_mask, there is no reason to iterate
247 * over this commit; it is not selected (if it were, it would
248 * not have a blank commit mask) and all its children have
249 * existing bitmaps (see the comment starting with "This commit
250 * has an existing bitmap" below), so it does not contribute
251 * anything to the final bitmap file or its descendants.
252 */
253 if (!c_ent->commit_mask)
254 continue;
255
256 if (old_bitmap && bitmap_for_commit(old_bitmap, commit)) {
257 /*
258 * This commit has an existing bitmap, so we can
259 * get its bits immediately without an object
260 * walk. That is, it is reusable as-is and there is no
261 * need to continue walking beyond it.
262 *
263 * Mark it as such and add it to bb->commits separately
264 * to avoid allocating a position in the commit mask.
265 */
266 commit_list_insert(commit, &reusable);
267 goto next;
268 }
269
Derrick Stolee089f7512020-12-08 17:04:30 -0500270 if (c_ent->maximal) {
Derrick Stolee45f4eeb2020-12-08 17:05:26 -0500271 num_maximal++;
Derrick Stolee089f7512020-12-08 17:04:30 -0500272 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
273 bb->commits[bb->commits_nr++] = commit;
274 }
Jeff King4a9c5812020-12-08 17:03:55 -0500275
Derrick Stolee45f4eeb2020-12-08 17:05:26 -0500276 if (p) {
Derrick Stolee089f7512020-12-08 17:04:30 -0500277 struct bb_commit *p_ent = bb_data_at(&bb->data, p->item);
278 int c_not_p, p_not_c;
279
280 if (!p_ent->commit_mask) {
281 p_ent->commit_mask = bitmap_new();
282 c_not_p = 1;
283 p_not_c = 0;
284 } else {
285 c_not_p = bitmap_is_subset(c_ent->commit_mask, p_ent->commit_mask);
286 p_not_c = bitmap_is_subset(p_ent->commit_mask, c_ent->commit_mask);
287 }
288
289 if (!c_not_p)
290 continue;
291
292 bitmap_or(p_ent->commit_mask, c_ent->commit_mask);
293
294 if (p_not_c)
295 p_ent->maximal = 1;
296 else {
297 p_ent->maximal = 0;
298 free_commit_list(p_ent->reverse_edges);
299 p_ent->reverse_edges = NULL;
300 }
301
302 if (c_ent->maximal) {
303 commit_list_insert(commit, &p_ent->reverse_edges);
304 } else {
305 struct commit_list *cc = c_ent->reverse_edges;
306
307 for (; cc; cc = cc->next) {
308 if (!commit_list_contains(cc->item, p_ent->reverse_edges))
309 commit_list_insert(cc->item, &p_ent->reverse_edges);
310 }
311 }
Jeff King4a9c5812020-12-08 17:03:55 -0500312 }
Derrick Stolee089f7512020-12-08 17:04:30 -0500313
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500314next:
Derrick Stolee089f7512020-12-08 17:04:30 -0500315 bitmap_free(c_ent->commit_mask);
316 c_ent->commit_mask = NULL;
Jeff King4a9c5812020-12-08 17:03:55 -0500317 }
Derrick Stolee089f7512020-12-08 17:04:30 -0500318
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500319 for (r = reusable; r; r = r->next) {
320 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
321 bb->commits[bb->commits_nr++] = r->item;
322 }
323
Derrick Stolee089f7512020-12-08 17:04:30 -0500324 trace2_data_intmax("pack-bitmap-write", the_repository,
325 "num_selected_commits", writer->selected_nr);
326 trace2_data_intmax("pack-bitmap-write", the_repository,
327 "num_maximal_commits", num_maximal);
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500328
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +0200329 release_revisions(&revs);
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500330 free_commit_list(reusable);
Jeff King4a9c5812020-12-08 17:03:55 -0500331}
332
333static void bitmap_builder_clear(struct bitmap_builder *bb)
334{
335 clear_bb_data(&bb->data);
336 free(bb->commits);
337 bb->commits_nr = bb->commits_alloc = 0;
338}
339
Taylor Blau3ba3d062021-08-24 12:15:54 -0400340static int fill_bitmap_tree(struct bitmap *bitmap,
341 struct tree *tree)
Jeff King4a9c5812020-12-08 17:03:55 -0500342{
Taylor Blau3ba3d062021-08-24 12:15:54 -0400343 int found;
Jeff King4a9c5812020-12-08 17:03:55 -0500344 uint32_t pos;
345 struct tree_desc desc;
346 struct name_entry entry;
347
348 /*
349 * If our bit is already set, then there is nothing to do. Both this
350 * tree and all of its children will be set.
351 */
Taylor Blau3ba3d062021-08-24 12:15:54 -0400352 pos = find_object_pos(&tree->object.oid, &found);
353 if (!found)
354 return -1;
Jeff King4a9c5812020-12-08 17:03:55 -0500355 if (bitmap_get(bitmap, pos))
Taylor Blau3ba3d062021-08-24 12:15:54 -0400356 return 0;
Jeff King4a9c5812020-12-08 17:03:55 -0500357 bitmap_set(bitmap, pos);
358
359 if (parse_tree(tree) < 0)
360 die("unable to load tree object %s",
361 oid_to_hex(&tree->object.oid));
362 init_tree_desc(&desc, tree->buffer, tree->size);
363
364 while (tree_entry(&desc, &entry)) {
365 switch (object_type(entry.mode)) {
366 case OBJ_TREE:
Taylor Blau3ba3d062021-08-24 12:15:54 -0400367 if (fill_bitmap_tree(bitmap,
368 lookup_tree(the_repository, &entry.oid)) < 0)
369 return -1;
Jeff King4a9c5812020-12-08 17:03:55 -0500370 break;
371 case OBJ_BLOB:
Taylor Blau3ba3d062021-08-24 12:15:54 -0400372 pos = find_object_pos(&entry.oid, &found);
373 if (!found)
374 return -1;
375 bitmap_set(bitmap, pos);
Jeff King4a9c5812020-12-08 17:03:55 -0500376 break;
377 default:
378 /* Gitlink, etc; not reachable */
379 break;
380 }
381 }
382
383 free_tree_buffer(tree);
Taylor Blau3ba3d062021-08-24 12:15:54 -0400384 return 0;
Jeff King4a9c5812020-12-08 17:03:55 -0500385}
386
Taylor Blau3ba3d062021-08-24 12:15:54 -0400387static int fill_bitmap_commit(struct bb_commit *ent,
388 struct commit *commit,
389 struct prio_queue *queue,
390 struct prio_queue *tree_queue,
391 struct bitmap_index *old_bitmap,
392 const uint32_t *mapping)
Jeff King4a9c5812020-12-08 17:03:55 -0500393{
Taylor Blau3ba3d062021-08-24 12:15:54 -0400394 int found;
395 uint32_t pos;
Jeff King4a9c5812020-12-08 17:03:55 -0500396 if (!ent->bitmap)
397 ent->bitmap = bitmap_new();
398
Derrick Stolee6dc5ef72020-12-08 17:04:03 -0500399 prio_queue_put(queue, commit);
400
401 while (queue->nr) {
402 struct commit_list *p;
403 struct commit *c = prio_queue_get(queue);
404
Derrick Stolee341fa342020-12-08 17:05:21 -0500405 if (old_bitmap && mapping) {
406 struct ewah_bitmap *old = bitmap_for_commit(old_bitmap, c);
407 /*
408 * If this commit has an old bitmap, then translate that
409 * bitmap and add its bits to this one. No need to walk
410 * parents or the tree for this commit.
411 */
412 if (old && !rebuild_bitmap(mapping, old, ent->bitmap))
413 continue;
414 }
415
416 /*
417 * Mark ourselves and queue our tree. The commit
418 * walk ensures we cover all parents.
419 */
Taylor Blau3ba3d062021-08-24 12:15:54 -0400420 pos = find_object_pos(&c->object.oid, &found);
421 if (!found)
422 return -1;
423 bitmap_set(ent->bitmap, pos);
Derrick Stolee341fa342020-12-08 17:05:21 -0500424 prio_queue_put(tree_queue, get_commit_tree(c));
Derrick Stolee6dc5ef72020-12-08 17:04:03 -0500425
426 for (p = c->parents; p; p = p->next) {
Taylor Blau3ba3d062021-08-24 12:15:54 -0400427 pos = find_object_pos(&p->item->object.oid, &found);
428 if (!found)
429 return -1;
Derrick Stolee6dc5ef72020-12-08 17:04:03 -0500430 if (!bitmap_get(ent->bitmap, pos)) {
431 bitmap_set(ent->bitmap, pos);
432 prio_queue_put(queue, p->item);
433 }
434 }
435 }
Derrick Stolee341fa342020-12-08 17:05:21 -0500436
Taylor Blau3ba3d062021-08-24 12:15:54 -0400437 while (tree_queue->nr) {
438 if (fill_bitmap_tree(ent->bitmap,
439 prio_queue_get(tree_queue)) < 0)
440 return -1;
441 }
442 return 0;
Jeff King4a9c5812020-12-08 17:03:55 -0500443}
444
445static void store_selected(struct bb_commit *ent, struct commit *commit)
446{
447 struct bitmapped_commit *stored = &writer.selected[ent->idx];
448 khiter_t hash_pos;
449 int hash_ret;
450
Jeff King4a9c5812020-12-08 17:03:55 -0500451 stored->bitmap = bitmap_to_ewah(ent->bitmap);
452
453 hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret);
454 if (hash_ret == 0)
455 die("Duplicate entry when writing index: %s",
456 oid_to_hex(&commit->object.oid));
457 kh_value(writer.bitmaps, hash_pos) = stored;
458}
459
Taylor Blau3ba3d062021-08-24 12:15:54 -0400460int bitmap_writer_build(struct packing_data *to_pack)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500461{
Jeff King4a9c5812020-12-08 17:03:55 -0500462 struct bitmap_builder bb;
463 size_t i;
464 int nr_stored = 0; /* for progress */
Derrick Stolee6dc5ef72020-12-08 17:04:03 -0500465 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
Derrick Stolee341fa342020-12-08 17:05:21 -0500466 struct prio_queue tree_queue = { NULL };
467 struct bitmap_index *old_bitmap;
468 uint32_t *mapping;
Taylor Blau3ba3d062021-08-24 12:15:54 -0400469 int closed = 1; /* until proven otherwise */
Vicent Marti7cc8f972013-12-21 09:00:16 -0500470
Jeff Kingd2bc62b2019-06-20 03:41:35 -0400471 writer.bitmaps = kh_init_oid_map();
Vicent Marti7cc8f972013-12-21 09:00:16 -0500472 writer.to_pack = to_pack;
473
474 if (writer.show_progress)
475 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
Jeff King4a9c5812020-12-08 17:03:55 -0500476 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
477 the_repository);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500478
Derrick Stolee341fa342020-12-08 17:05:21 -0500479 old_bitmap = prepare_bitmap_git(to_pack->repo);
480 if (old_bitmap)
481 mapping = create_bitmap_mapping(old_bitmap, to_pack);
482 else
483 mapping = NULL;
484
Derrick Stoleef077b0a2020-12-08 17:05:30 -0500485 bitmap_builder_init(&bb, &writer, old_bitmap);
Jeff King4a9c5812020-12-08 17:03:55 -0500486 for (i = bb.commits_nr; i > 0; i--) {
487 struct commit *commit = bb.commits[i-1];
488 struct bb_commit *ent = bb_data_at(&bb.data, commit);
489 struct commit *child;
Jeff King010e5ea2020-12-08 17:03:59 -0500490 int reused = 0;
Vicent Marti7cc8f972013-12-21 09:00:16 -0500491
Taylor Blau3ba3d062021-08-24 12:15:54 -0400492 if (fill_bitmap_commit(ent, commit, &queue, &tree_queue,
493 old_bitmap, mapping) < 0) {
494 closed = 0;
495 break;
496 }
Vicent Marti7cc8f972013-12-21 09:00:16 -0500497
Jeff King4a9c5812020-12-08 17:03:55 -0500498 if (ent->selected) {
499 store_selected(ent, commit);
500 nr_stored++;
501 display_progress(writer.progress, nr_stored);
502 }
Vicent Marti7cc8f972013-12-21 09:00:16 -0500503
Derrick Stolee928e3f42020-12-08 17:04:22 -0500504 while ((child = pop_commit(&ent->reverse_edges))) {
Jeff King4a9c5812020-12-08 17:03:55 -0500505 struct bb_commit *child_ent =
506 bb_data_at(&bb.data, child);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500507
Jeff King4a9c5812020-12-08 17:03:55 -0500508 if (child_ent->bitmap)
509 bitmap_or(child_ent->bitmap, ent->bitmap);
Jeff King010e5ea2020-12-08 17:03:59 -0500510 else if (reused)
Jeff King4a9c5812020-12-08 17:03:55 -0500511 child_ent->bitmap = bitmap_dup(ent->bitmap);
Jeff King010e5ea2020-12-08 17:03:59 -0500512 else {
513 child_ent->bitmap = ent->bitmap;
514 reused = 1;
515 }
Jeff King4a9c5812020-12-08 17:03:55 -0500516 }
Jeff King010e5ea2020-12-08 17:03:59 -0500517 if (!reused)
518 bitmap_free(ent->bitmap);
Jeff King4a9c5812020-12-08 17:03:55 -0500519 ent->bitmap = NULL;
Vicent Marti7cc8f972013-12-21 09:00:16 -0500520 }
Derrick Stolee6dc5ef72020-12-08 17:04:03 -0500521 clear_prio_queue(&queue);
Derrick Stolee341fa342020-12-08 17:05:21 -0500522 clear_prio_queue(&tree_queue);
Jeff King4a9c5812020-12-08 17:03:55 -0500523 bitmap_builder_clear(&bb);
Taylor Blau1d7f7f22021-08-24 12:15:56 -0400524 free_bitmap_index(old_bitmap);
Derrick Stolee341fa342020-12-08 17:05:21 -0500525 free(mapping);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500526
Jeff King4a9c5812020-12-08 17:03:55 -0500527 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
528 the_repository);
529
Vicent Marti7cc8f972013-12-21 09:00:16 -0500530 stop_progress(&writer.progress);
531
Taylor Blau3ba3d062021-08-24 12:15:54 -0400532 if (closed)
533 compute_xor_offsets();
534 return closed ? 0 : -1;
Vicent Marti7cc8f972013-12-21 09:00:16 -0500535}
536
537/**
538 * Select the commits that will be bitmapped
539 */
540static inline unsigned int next_commit_index(unsigned int idx)
541{
542 static const unsigned int MIN_COMMITS = 100;
543 static const unsigned int MAX_COMMITS = 5000;
544
545 static const unsigned int MUST_REGION = 100;
546 static const unsigned int MIN_REGION = 20000;
547
548 unsigned int offset, next;
549
550 if (idx <= MUST_REGION)
551 return 0;
552
553 if (idx <= MIN_REGION) {
554 offset = idx - MUST_REGION;
555 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
556 }
557
558 offset = idx - MIN_REGION;
559 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
560
561 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
562}
563
564static int date_compare(const void *_a, const void *_b)
565{
566 struct commit *a = *(struct commit **)_a;
567 struct commit *b = *(struct commit **)_b;
568 return (long)b->date - (long)a->date;
569}
570
Vicent Marti7cc8f972013-12-21 09:00:16 -0500571void bitmap_writer_select_commits(struct commit **indexed_commits,
572 unsigned int indexed_commits_nr,
573 int max_bitmaps)
574{
575 unsigned int i = 0, j, next;
576
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200577 QSORT(indexed_commits, indexed_commits_nr, date_compare);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500578
Vicent Marti7cc8f972013-12-21 09:00:16 -0500579 if (indexed_commits_nr < 100) {
580 for (i = 0; i < indexed_commits_nr; ++i)
Jeff King449fa5e2020-12-08 17:04:34 -0500581 push_bitmapped_commit(indexed_commits[i]);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500582 return;
583 }
584
Ævar Arnfjörð Bjarmasonb3118a52022-02-03 22:40:19 +0100585 if (writer.show_progress)
586 writer.progress = start_progress("Selecting bitmap commits", 0);
587
Vicent Marti7cc8f972013-12-21 09:00:16 -0500588 for (;;) {
Vicent Marti7cc8f972013-12-21 09:00:16 -0500589 struct commit *chosen = NULL;
590
591 next = next_commit_index(i);
592
593 if (i + next >= indexed_commits_nr)
594 break;
595
596 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
597 writer.selected_nr = max_bitmaps;
598 break;
599 }
600
601 if (next == 0) {
602 chosen = indexed_commits[i];
Vicent Marti7cc8f972013-12-21 09:00:16 -0500603 } else {
604 chosen = indexed_commits[i + next];
605
606 for (j = 0; j <= next; ++j) {
607 struct commit *cm = indexed_commits[i + j];
608
Jeff King449fa5e2020-12-08 17:04:34 -0500609 if ((cm->object.flags & NEEDS_BITMAP) != 0) {
Vicent Marti7cc8f972013-12-21 09:00:16 -0500610 chosen = cm;
611 break;
612 }
613
614 if (cm->parents && cm->parents->next)
615 chosen = cm;
616 }
617 }
618
Jeff King449fa5e2020-12-08 17:04:34 -0500619 push_bitmapped_commit(chosen);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500620
621 i += next + 1;
622 display_progress(writer.progress, i);
623 }
624
625 stop_progress(&writer.progress);
626}
627
628
brian m. carlson98a3bea2018-02-01 02:18:46 +0000629static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500630{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000631 /* hashwrite will die on error */
632 hashwrite(f, buf, len);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500633 return len;
634}
635
636/**
637 * Write the bitmap index to disk
638 */
brian m. carlson98a3bea2018-02-01 02:18:46 +0000639static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500640{
brian m. carlson98a3bea2018-02-01 02:18:46 +0000641 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500642 die("Failed to write bitmap index");
643}
644
Jeff King8380dcd2021-01-28 01:20:23 -0500645static const struct object_id *oid_access(size_t pos, const void *table)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500646{
Jeff King8380dcd2021-01-28 01:20:23 -0500647 const struct pack_idx_entry * const *index = table;
Jeff King45ee13b2021-01-28 01:19:42 -0500648 return &index[pos]->oid;
Vicent Marti7cc8f972013-12-21 09:00:16 -0500649}
650
brian m. carlson98a3bea2018-02-01 02:18:46 +0000651static void write_selected_commits_v1(struct hashfile *f,
Vicent Marti7cc8f972013-12-21 09:00:16 -0500652 struct pack_idx_entry **index,
653 uint32_t index_nr)
654{
655 int i;
656
657 for (i = 0; i < writer.selected_nr; ++i) {
658 struct bitmapped_commit *stored = &writer.selected[i];
Vicent Marti7cc8f972013-12-21 09:00:16 -0500659
660 int commit_pos =
Jeff King45ee13b2021-01-28 01:19:42 -0500661 oid_pos(&stored->commit->object.oid, index, index_nr, oid_access);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500662
663 if (commit_pos < 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200664 BUG("trying to write commit not in index");
Vicent Marti7cc8f972013-12-21 09:00:16 -0500665
brian m. carlson98a3bea2018-02-01 02:18:46 +0000666 hashwrite_be32(f, commit_pos);
667 hashwrite_u8(f, stored->xor_offset);
668 hashwrite_u8(f, stored->flags);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500669
Vicent Marti7cc8f972013-12-21 09:00:16 -0500670 dump_bitmap(f, stored->write_as);
671 }
672}
673
brian m. carlson98a3bea2018-02-01 02:18:46 +0000674static void write_hash_cache(struct hashfile *f,
Vicent Martiae4f07f2013-12-21 09:00:45 -0500675 struct pack_idx_entry **index,
676 uint32_t index_nr)
677{
678 uint32_t i;
679
680 for (i = 0; i < index_nr; ++i) {
681 struct object_entry *entry = (struct object_entry *)index[i];
René Scharfe7744a5d2020-09-06 10:59:06 +0200682 hashwrite_be32(f, entry->hash);
Vicent Martiae4f07f2013-12-21 09:00:45 -0500683 }
684}
685
Vicent Marti7cc8f972013-12-21 09:00:16 -0500686void bitmap_writer_set_checksum(unsigned char *sha1)
687{
688 hashcpy(writer.pack_checksum, sha1);
689}
690
691void bitmap_writer_finish(struct pack_idx_entry **index,
692 uint32_t index_nr,
Vicent Martiae4f07f2013-12-21 09:00:45 -0500693 const char *filename,
694 uint16_t options)
Vicent Marti7cc8f972013-12-21 09:00:16 -0500695{
Vicent Marti7cc8f972013-12-21 09:00:16 -0500696 static uint16_t default_version = 1;
697 static uint16_t flags = BITMAP_OPT_FULL_DAG;
Jeff King594fa992017-03-28 15:45:43 -0400698 struct strbuf tmp_file = STRBUF_INIT;
brian m. carlson98a3bea2018-02-01 02:18:46 +0000699 struct hashfile *f;
Vicent Marti7cc8f972013-12-21 09:00:16 -0500700
701 struct bitmap_disk_header header;
702
Jeff King594fa992017-03-28 15:45:43 -0400703 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
Vicent Marti7cc8f972013-12-21 09:00:16 -0500704
brian m. carlson98a3bea2018-02-01 02:18:46 +0000705 f = hashfd(fd, tmp_file.buf);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500706
707 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
708 header.version = htons(default_version);
Vicent Martiae4f07f2013-12-21 09:00:45 -0500709 header.options = htons(flags | options);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500710 header.entry_count = htonl(writer.selected_nr);
Sun He50546b12014-03-03 17:39:59 +0800711 hashcpy(header.checksum, writer.pack_checksum);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500712
brian m. carlson0f4d6ca2019-02-19 00:04:54 +0000713 hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500714 dump_bitmap(f, writer.commits);
715 dump_bitmap(f, writer.trees);
716 dump_bitmap(f, writer.blobs);
717 dump_bitmap(f, writer.tags);
718 write_selected_commits_v1(f, index, index_nr);
719
Vicent Martiae4f07f2013-12-21 09:00:45 -0500720 if (options & BITMAP_OPT_HASH_CACHE)
721 write_hash_cache(f, index, index_nr);
722
Neeraj Singh020406e2022-03-10 22:43:21 +0000723 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
724 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500725
Jeff King594fa992017-03-28 15:45:43 -0400726 if (adjust_shared_perm(tmp_file.buf))
Vicent Marti7cc8f972013-12-21 09:00:16 -0500727 die_errno("unable to make temporary bitmap file readable");
728
Jeff King594fa992017-03-28 15:45:43 -0400729 if (rename(tmp_file.buf, filename))
Vicent Marti7cc8f972013-12-21 09:00:16 -0500730 die_errno("unable to rename temporary bitmap file to '%s'", filename);
Jeff King594fa992017-03-28 15:45:43 -0400731
732 strbuf_release(&tmp_file);
Vicent Marti7cc8f972013-12-21 09:00:16 -0500733}