pack-bitmap-write.con commit diff-lib: allow ita entries treated as "not yet exist in index" (425a28e)
   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
  14struct 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
  23struct 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
  41static struct bitmap_writer writer;
  42
  43void 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 */
  51void 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 = sha1_object_info(entry->idx.sha1, 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                            sha1_to_hex(entry->idx.sha1), real_type, entry->type);
 100                }
 101        }
 102}
 103
 104/**
 105 * Compute the actual bitmaps
 106 */
 107static struct object **seen_objects;
 108static unsigned int seen_objects_nr, seen_objects_alloc;
 109
 110static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitmap *reused)
 111{
 112        if (writer.selected_nr >= writer.selected_alloc) {
 113                writer.selected_alloc = (writer.selected_alloc + 32) * 2;
 114                REALLOC_ARRAY(writer.selected, writer.selected_alloc);
 115        }
 116
 117        writer.selected[writer.selected_nr].commit = commit;
 118        writer.selected[writer.selected_nr].bitmap = reused;
 119        writer.selected[writer.selected_nr].flags = 0;
 120
 121        writer.selected_nr++;
 122}
 123
 124static inline void mark_as_seen(struct object *object)
 125{
 126        ALLOC_GROW(seen_objects, seen_objects_nr + 1, seen_objects_alloc);
 127        seen_objects[seen_objects_nr++] = object;
 128}
 129
 130static inline void reset_all_seen(void)
 131{
 132        unsigned int i;
 133        for (i = 0; i < seen_objects_nr; ++i) {
 134                seen_objects[i]->flags &= ~(SEEN | ADDED | SHOWN);
 135        }
 136        seen_objects_nr = 0;
 137}
 138
 139static uint32_t find_object_pos(const unsigned char *sha1)
 140{
 141        struct object_entry *entry = packlist_find(writer.to_pack, sha1, NULL);
 142
 143        if (!entry) {
 144                die("Failed to write bitmap index. Packfile doesn't have full closure "
 145                        "(object %s is missing)", sha1_to_hex(sha1));
 146        }
 147
 148        return entry->in_pack_pos;
 149}
 150
 151static void show_object(struct object *object, const char *name, void *data)
 152{
 153        struct bitmap *base = data;
 154        bitmap_set(base, find_object_pos(object->oid.hash));
 155        mark_as_seen(object);
 156}
 157
 158static void show_commit(struct commit *commit, void *data)
 159{
 160        mark_as_seen((struct object *)commit);
 161}
 162
 163static int
 164add_to_include_set(struct bitmap *base, struct commit *commit)
 165{
 166        khiter_t hash_pos;
 167        uint32_t bitmap_pos = find_object_pos(commit->object.oid.hash);
 168
 169        if (bitmap_get(base, bitmap_pos))
 170                return 0;
 171
 172        hash_pos = kh_get_sha1(writer.bitmaps, commit->object.oid.hash);
 173        if (hash_pos < kh_end(writer.bitmaps)) {
 174                struct bitmapped_commit *bc = kh_value(writer.bitmaps, hash_pos);
 175                bitmap_or_ewah(base, bc->bitmap);
 176                return 0;
 177        }
 178
 179        bitmap_set(base, bitmap_pos);
 180        return 1;
 181}
 182
 183static int
 184should_include(struct commit *commit, void *_data)
 185{
 186        struct bitmap *base = _data;
 187
 188        if (!add_to_include_set(base, commit)) {
 189                struct commit_list *parent = commit->parents;
 190
 191                mark_as_seen((struct object *)commit);
 192
 193                while (parent) {
 194                        parent->item->object.flags |= SEEN;
 195                        mark_as_seen((struct object *)parent->item);
 196                        parent = parent->next;
 197                }
 198
 199                return 0;
 200        }
 201
 202        return 1;
 203}
 204
 205static void compute_xor_offsets(void)
 206{
 207        static const int MAX_XOR_OFFSET_SEARCH = 10;
 208
 209        int i, next = 0;
 210
 211        while (next < writer.selected_nr) {
 212                struct bitmapped_commit *stored = &writer.selected[next];
 213
 214                int best_offset = 0;
 215                struct ewah_bitmap *best_bitmap = stored->bitmap;
 216                struct ewah_bitmap *test_xor;
 217
 218                for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
 219                        int curr = next - i;
 220
 221                        if (curr < 0)
 222                                break;
 223
 224                        test_xor = ewah_pool_new();
 225                        ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
 226
 227                        if (test_xor->buffer_size < best_bitmap->buffer_size) {
 228                                if (best_bitmap != stored->bitmap)
 229                                        ewah_pool_free(best_bitmap);
 230
 231                                best_bitmap = test_xor;
 232                                best_offset = i;
 233                        } else {
 234                                ewah_pool_free(test_xor);
 235                        }
 236                }
 237
 238                stored->xor_offset = best_offset;
 239                stored->write_as = best_bitmap;
 240
 241                next++;
 242        }
 243}
 244
 245void bitmap_writer_build(struct packing_data *to_pack)
 246{
 247        static const double REUSE_BITMAP_THRESHOLD = 0.2;
 248
 249        int i, reuse_after, need_reset;
 250        struct bitmap *base = bitmap_new();
 251        struct rev_info revs;
 252
 253        writer.bitmaps = kh_init_sha1();
 254        writer.to_pack = to_pack;
 255
 256        if (writer.show_progress)
 257                writer.progress = start_progress("Building bitmaps", writer.selected_nr);
 258
 259        init_revisions(&revs, NULL);
 260        revs.tag_objects = 1;
 261        revs.tree_objects = 1;
 262        revs.blob_objects = 1;
 263        revs.no_walk = 0;
 264
 265        revs.include_check = should_include;
 266        reset_revision_walk();
 267
 268        reuse_after = writer.selected_nr * REUSE_BITMAP_THRESHOLD;
 269        need_reset = 0;
 270
 271        for (i = writer.selected_nr - 1; i >= 0; --i) {
 272                struct bitmapped_commit *stored;
 273                struct object *object;
 274
 275                khiter_t hash_pos;
 276                int hash_ret;
 277
 278                stored = &writer.selected[i];
 279                object = (struct object *)stored->commit;
 280
 281                if (stored->bitmap == NULL) {
 282                        if (i < writer.selected_nr - 1 &&
 283                            (need_reset ||
 284                             !in_merge_bases(writer.selected[i + 1].commit,
 285                                             stored->commit))) {
 286                            bitmap_reset(base);
 287                            reset_all_seen();
 288                        }
 289
 290                        add_pending_object(&revs, object, "");
 291                        revs.include_check_data = base;
 292
 293                        if (prepare_revision_walk(&revs))
 294                                die("revision walk setup failed");
 295
 296                        traverse_commit_list(&revs, show_commit, show_object, base);
 297
 298                        revs.pending.nr = 0;
 299                        revs.pending.alloc = 0;
 300                        revs.pending.objects = NULL;
 301
 302                        stored->bitmap = bitmap_to_ewah(base);
 303                        need_reset = 0;
 304                } else
 305                        need_reset = 1;
 306
 307                if (i >= reuse_after)
 308                        stored->flags |= BITMAP_FLAG_REUSE;
 309
 310                hash_pos = kh_put_sha1(writer.bitmaps, object->oid.hash, &hash_ret);
 311                if (hash_ret == 0)
 312                        die("Duplicate entry when writing index: %s",
 313                            oid_to_hex(&object->oid));
 314
 315                kh_value(writer.bitmaps, hash_pos) = stored;
 316                display_progress(writer.progress, writer.selected_nr - i);
 317        }
 318
 319        bitmap_free(base);
 320        stop_progress(&writer.progress);
 321
 322        compute_xor_offsets();
 323}
 324
 325/**
 326 * Select the commits that will be bitmapped
 327 */
 328static inline unsigned int next_commit_index(unsigned int idx)
 329{
 330        static const unsigned int MIN_COMMITS = 100;
 331        static const unsigned int MAX_COMMITS = 5000;
 332
 333        static const unsigned int MUST_REGION = 100;
 334        static const unsigned int MIN_REGION = 20000;
 335
 336        unsigned int offset, next;
 337
 338        if (idx <= MUST_REGION)
 339                return 0;
 340
 341        if (idx <= MIN_REGION) {
 342                offset = idx - MUST_REGION;
 343                return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
 344        }
 345
 346        offset = idx - MIN_REGION;
 347        next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
 348
 349        return (next > MIN_COMMITS) ? next : MIN_COMMITS;
 350}
 351
 352static int date_compare(const void *_a, const void *_b)
 353{
 354        struct commit *a = *(struct commit **)_a;
 355        struct commit *b = *(struct commit **)_b;
 356        return (long)b->date - (long)a->date;
 357}
 358
 359void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
 360{
 361        if (prepare_bitmap_git() < 0)
 362                return;
 363
 364        writer.reused = kh_init_sha1();
 365        rebuild_existing_bitmaps(to_pack, writer.reused, writer.show_progress);
 366}
 367
 368static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
 369{
 370        khiter_t hash_pos;
 371
 372        if (!writer.reused)
 373                return NULL;
 374
 375        hash_pos = kh_get_sha1(writer.reused, sha1);
 376        if (hash_pos >= kh_end(writer.reused))
 377                return NULL;
 378
 379        return kh_value(writer.reused, hash_pos);
 380}
 381
 382void bitmap_writer_select_commits(struct commit **indexed_commits,
 383                                  unsigned int indexed_commits_nr,
 384                                  int max_bitmaps)
 385{
 386        unsigned int i = 0, j, next;
 387
 388        qsort(indexed_commits, indexed_commits_nr, sizeof(indexed_commits[0]),
 389              date_compare);
 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];
 416                        reused_bitmap = find_reused_bitmap(chosen->object.oid.hash);
 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
 423                                reused_bitmap = find_reused_bitmap(cm->object.oid.hash);
 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
 444static int sha1write_ewah_helper(void *f, const void *buf, size_t len)
 445{
 446        /* sha1write will die on error */
 447        sha1write(f, buf, len);
 448        return len;
 449}
 450
 451/**
 452 * Write the bitmap index to disk
 453 */
 454static inline void dump_bitmap(struct sha1file *f, struct ewah_bitmap *bitmap)
 455{
 456        if (ewah_serialize_to(bitmap, sha1write_ewah_helper, f) < 0)
 457                die("Failed to write bitmap index");
 458}
 459
 460static const unsigned char *sha1_access(size_t pos, void *table)
 461{
 462        struct pack_idx_entry **index = table;
 463        return index[pos]->sha1;
 464}
 465
 466static void write_selected_commits_v1(struct sha1file *f,
 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];
 474
 475                int commit_pos =
 476                        sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
 477
 478                if (commit_pos < 0)
 479                        die("BUG: trying to write commit not in index");
 480
 481                sha1write_be32(f, commit_pos);
 482                sha1write_u8(f, stored->xor_offset);
 483                sha1write_u8(f, stored->flags);
 484
 485                dump_bitmap(f, stored->write_as);
 486        }
 487}
 488
 489static void write_hash_cache(struct sha1file *f,
 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);
 498                sha1write(f, &hash_value, sizeof(hash_value));
 499        }
 500}
 501
 502void bitmap_writer_set_checksum(unsigned char *sha1)
 503{
 504        hashcpy(writer.pack_checksum, sha1);
 505}
 506
 507void bitmap_writer_finish(struct pack_idx_entry **index,
 508                          uint32_t index_nr,
 509                          const char *filename,
 510                          uint16_t options)
 511{
 512        static char tmp_file[PATH_MAX];
 513        static uint16_t default_version = 1;
 514        static uint16_t flags = BITMAP_OPT_FULL_DAG;
 515        struct sha1file *f;
 516
 517        struct bitmap_disk_header header;
 518
 519        int fd = odb_mkstemp(tmp_file, sizeof(tmp_file), "pack/tmp_bitmap_XXXXXX");
 520
 521        if (fd < 0)
 522                die_errno("unable to create '%s'", tmp_file);
 523        f = sha1fd(fd, tmp_file);
 524
 525        memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
 526        header.version = htons(default_version);
 527        header.options = htons(flags | options);
 528        header.entry_count = htonl(writer.selected_nr);
 529        hashcpy(header.checksum, writer.pack_checksum);
 530
 531        sha1write(f, &header, sizeof(header));
 532        dump_bitmap(f, writer.commits);
 533        dump_bitmap(f, writer.trees);
 534        dump_bitmap(f, writer.blobs);
 535        dump_bitmap(f, writer.tags);
 536        write_selected_commits_v1(f, index, index_nr);
 537
 538        if (options & BITMAP_OPT_HASH_CACHE)
 539                write_hash_cache(f, index, index_nr);
 540
 541        sha1close(f, NULL, CSUM_FSYNC);
 542
 543        if (adjust_shared_perm(tmp_file))
 544                die_errno("unable to make temporary bitmap file readable");
 545
 546        if (rename(tmp_file, filename))
 547                die_errno("unable to rename temporary bitmap file to '%s'", filename);
 548}