pack-bitmap-write.con commit object.c: allow parse_object to handle arbitrary repositories (8e4b0b6)
   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
  15struct 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
  24struct 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
  42static struct bitmap_writer writer;
  43
  44void 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 */
  52void 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 */
 112static struct object **seen_objects;
 113static unsigned int seen_objects_nr, seen_objects_alloc;
 114
 115static 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
 129static 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
 135static 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
 144static 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
 156static 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
 163static void show_commit(struct commit *commit, void *data)
 164{
 165        mark_as_seen((struct object *)commit);
 166}
 167
 168static int
 169add_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
 188static int
 189should_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
 210static 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
 250void 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 */
 331static 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
 355static 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
 362void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)
 363{
 364        if (prepare_bitmap_git() < 0)
 365                return;
 366
 367        writer.reused = kh_init_sha1();
 368        rebuild_existing_bitmaps(to_pack, writer.reused, writer.show_progress);
 369}
 370
 371static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)
 372{
 373        khiter_t hash_pos;
 374
 375        if (!writer.reused)
 376                return NULL;
 377
 378        hash_pos = kh_get_sha1(writer.reused, sha1);
 379        if (hash_pos >= kh_end(writer.reused))
 380                return NULL;
 381
 382        return kh_value(writer.reused, hash_pos);
 383}
 384
 385void bitmap_writer_select_commits(struct commit **indexed_commits,
 386                                  unsigned int indexed_commits_nr,
 387                                  int max_bitmaps)
 388{
 389        unsigned int i = 0, j, next;
 390
 391        QSORT(indexed_commits, indexed_commits_nr, date_compare);
 392
 393        if (writer.show_progress)
 394                writer.progress = start_progress("Selecting bitmap commits", 0);
 395
 396        if (indexed_commits_nr < 100) {
 397                for (i = 0; i < indexed_commits_nr; ++i)
 398                        push_bitmapped_commit(indexed_commits[i], NULL);
 399                return;
 400        }
 401
 402        for (;;) {
 403                struct ewah_bitmap *reused_bitmap = NULL;
 404                struct commit *chosen = NULL;
 405
 406                next = next_commit_index(i);
 407
 408                if (i + next >= indexed_commits_nr)
 409                        break;
 410
 411                if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
 412                        writer.selected_nr = max_bitmaps;
 413                        break;
 414                }
 415
 416                if (next == 0) {
 417                        chosen = indexed_commits[i];
 418                        reused_bitmap = find_reused_bitmap(chosen->object.oid.hash);
 419                } else {
 420                        chosen = indexed_commits[i + next];
 421
 422                        for (j = 0; j <= next; ++j) {
 423                                struct commit *cm = indexed_commits[i + j];
 424
 425                                reused_bitmap = find_reused_bitmap(cm->object.oid.hash);
 426                                if (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {
 427                                        chosen = cm;
 428                                        break;
 429                                }
 430
 431                                if (cm->parents && cm->parents->next)
 432                                        chosen = cm;
 433                        }
 434                }
 435
 436                push_bitmapped_commit(chosen, reused_bitmap);
 437
 438                i += next + 1;
 439                display_progress(writer.progress, i);
 440        }
 441
 442        stop_progress(&writer.progress);
 443}
 444
 445
 446static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
 447{
 448        /* hashwrite will die on error */
 449        hashwrite(f, buf, len);
 450        return len;
 451}
 452
 453/**
 454 * Write the bitmap index to disk
 455 */
 456static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
 457{
 458        if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
 459                die("Failed to write bitmap index");
 460}
 461
 462static const unsigned char *sha1_access(size_t pos, void *table)
 463{
 464        struct pack_idx_entry **index = table;
 465        return index[pos]->oid.hash;
 466}
 467
 468static void write_selected_commits_v1(struct hashfile *f,
 469                                      struct pack_idx_entry **index,
 470                                      uint32_t index_nr)
 471{
 472        int i;
 473
 474        for (i = 0; i < writer.selected_nr; ++i) {
 475                struct bitmapped_commit *stored = &writer.selected[i];
 476
 477                int commit_pos =
 478                        sha1_pos(stored->commit->object.oid.hash, index, index_nr, sha1_access);
 479
 480                if (commit_pos < 0)
 481                        BUG("trying to write commit not in index");
 482
 483                hashwrite_be32(f, commit_pos);
 484                hashwrite_u8(f, stored->xor_offset);
 485                hashwrite_u8(f, stored->flags);
 486
 487                dump_bitmap(f, stored->write_as);
 488        }
 489}
 490
 491static void write_hash_cache(struct hashfile *f,
 492                             struct pack_idx_entry **index,
 493                             uint32_t index_nr)
 494{
 495        uint32_t i;
 496
 497        for (i = 0; i < index_nr; ++i) {
 498                struct object_entry *entry = (struct object_entry *)index[i];
 499                uint32_t hash_value = htonl(entry->hash);
 500                hashwrite(f, &hash_value, sizeof(hash_value));
 501        }
 502}
 503
 504void bitmap_writer_set_checksum(unsigned char *sha1)
 505{
 506        hashcpy(writer.pack_checksum, sha1);
 507}
 508
 509void bitmap_writer_finish(struct pack_idx_entry **index,
 510                          uint32_t index_nr,
 511                          const char *filename,
 512                          uint16_t options)
 513{
 514        static uint16_t default_version = 1;
 515        static uint16_t flags = BITMAP_OPT_FULL_DAG;
 516        struct strbuf tmp_file = STRBUF_INIT;
 517        struct hashfile *f;
 518
 519        struct bitmap_disk_header header;
 520
 521        int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
 522
 523        f = hashfd(fd, tmp_file.buf);
 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        hashwrite(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        finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
 542
 543        if (adjust_shared_perm(tmp_file.buf))
 544                die_errno("unable to make temporary bitmap file readable");
 545
 546        if (rename(tmp_file.buf, filename))
 547                die_errno("unable to rename temporary bitmap file to '%s'", filename);
 548
 549        strbuf_release(&tmp_file);
 550}