6 #include "object-store.h"
10 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
11 #define MIDX_VERSION 1
12 #define MIDX_BYTE_FILE_VERSION 4
13 #define MIDX_BYTE_HASH_VERSION 5
14 #define MIDX_BYTE_NUM_CHUNKS 6
15 #define MIDX_BYTE_NUM_PACKS 8
16 #define MIDX_HASH_VERSION 1
17 #define MIDX_HEADER_SIZE 12
18 #define MIDX_HASH_LEN 20
19 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN)
21 #define MIDX_MAX_CHUNKS 3
22 #define MIDX_CHUNK_ALIGNMENT 4
23 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
24 #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
25 #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
26 #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
27 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
29 static char *get_midx_filename(const char *object_dir)
31 return xstrfmt("%s/pack/multi-pack-index", object_dir);
34 struct multi_pack_index *load_multi_pack_index(const char *object_dir)
36 struct multi_pack_index *m = NULL;
40 void *midx_map = NULL;
41 uint32_t hash_version;
42 char *midx_name = get_midx_filename(object_dir);
44 const char *cur_pack_name;
46 fd = git_open(midx_name);
51 error_errno(_("failed to read %s"), midx_name);
55 midx_size = xsize_t(st.st_size);
57 if (midx_size < MIDX_MIN_SIZE) {
58 error(_("multi-pack-index file %s is too small"), midx_name);
62 FREE_AND_NULL(midx_name);
64 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
66 FLEX_ALLOC_MEM(m, object_dir, object_dir, strlen(object_dir));
69 m->data_len = midx_size;
71 m->signature = get_be32(m->data);
72 if (m->signature != MIDX_SIGNATURE) {
73 error(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
74 m->signature, MIDX_SIGNATURE);
78 m->version = m->data[MIDX_BYTE_FILE_VERSION];
79 if (m->version != MIDX_VERSION) {
80 error(_("multi-pack-index version %d not recognized"),
85 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
86 if (hash_version != MIDX_HASH_VERSION) {
87 error(_("hash version %u does not match"), hash_version);
90 m->hash_len = MIDX_HASH_LEN;
92 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
94 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
96 for (i = 0; i < m->num_chunks; i++) {
97 uint32_t chunk_id = get_be32(m->data + MIDX_HEADER_SIZE +
98 MIDX_CHUNKLOOKUP_WIDTH * i);
99 uint64_t chunk_offset = get_be64(m->data + MIDX_HEADER_SIZE + 4 +
100 MIDX_CHUNKLOOKUP_WIDTH * i);
103 case MIDX_CHUNKID_PACKNAMES:
104 m->chunk_pack_names = m->data + chunk_offset;
107 case MIDX_CHUNKID_OIDFANOUT:
108 m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset);
111 case MIDX_CHUNKID_OIDLOOKUP:
112 m->chunk_oid_lookup = m->data + chunk_offset;
116 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
121 * Do nothing on unrecognized chunks, allowing future
122 * extensions to add optional chunks.
128 if (!m->chunk_pack_names)
129 die(_("multi-pack-index missing required pack-name chunk"));
130 if (!m->chunk_oid_fanout)
131 die(_("multi-pack-index missing required OID fanout chunk"));
132 if (!m->chunk_oid_lookup)
133 die(_("multi-pack-index missing required OID lookup chunk"));
135 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
137 m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
139 cur_pack_name = (const char *)m->chunk_pack_names;
140 for (i = 0; i < m->num_packs; i++) {
141 m->pack_names[i] = cur_pack_name;
143 cur_pack_name += strlen(cur_pack_name) + 1;
145 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0) {
146 error(_("multi-pack-index pack names out of order: '%s' before '%s'"),
147 m->pack_names[i - 1],
159 munmap(midx_map, midx_size);
165 static size_t write_midx_header(struct hashfile *f,
166 unsigned char num_chunks,
169 unsigned char byte_values[4];
171 hashwrite_be32(f, MIDX_SIGNATURE);
172 byte_values[0] = MIDX_VERSION;
173 byte_values[1] = MIDX_HASH_VERSION;
174 byte_values[2] = num_chunks;
175 byte_values[3] = 0; /* unused */
176 hashwrite(f, byte_values, sizeof(byte_values));
177 hashwrite_be32(f, num_packs);
179 return MIDX_HEADER_SIZE;
183 struct packed_git **list;
187 uint32_t alloc_names;
188 size_t pack_name_concat_len;
191 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
192 const char *file_name, void *data)
194 struct pack_list *packs = (struct pack_list *)data;
196 if (ends_with(file_name, ".idx")) {
197 ALLOC_GROW(packs->list, packs->nr + 1, packs->alloc_list);
198 ALLOC_GROW(packs->names, packs->nr + 1, packs->alloc_names);
200 packs->list[packs->nr] = add_packed_git(full_path,
204 if (!packs->list[packs->nr]) {
205 warning(_("failed to add packfile '%s'"),
210 if (open_pack_index(packs->list[packs->nr])) {
211 warning(_("failed to open pack-index '%s'"),
213 close_pack(packs->list[packs->nr]);
214 FREE_AND_NULL(packs->list[packs->nr]);
218 packs->names[packs->nr] = xstrdup(file_name);
219 packs->pack_name_concat_len += strlen(file_name) + 1;
225 uint32_t pack_int_id;
229 static int pack_pair_compare(const void *_a, const void *_b)
231 struct pack_pair *a = (struct pack_pair *)_a;
232 struct pack_pair *b = (struct pack_pair *)_b;
233 return strcmp(a->pack_name, b->pack_name);
236 static void sort_packs_by_name(char **pack_names, uint32_t nr_packs, uint32_t *perm)
239 struct pack_pair *pairs;
241 ALLOC_ARRAY(pairs, nr_packs);
243 for (i = 0; i < nr_packs; i++) {
244 pairs[i].pack_int_id = i;
245 pairs[i].pack_name = pack_names[i];
248 QSORT(pairs, nr_packs, pack_pair_compare);
250 for (i = 0; i < nr_packs; i++) {
251 pack_names[i] = pairs[i].pack_name;
252 perm[pairs[i].pack_int_id] = i;
258 struct pack_midx_entry {
259 struct object_id oid;
260 uint32_t pack_int_id;
265 static int midx_oid_compare(const void *_a, const void *_b)
267 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
268 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
269 int cmp = oidcmp(&a->oid, &b->oid);
274 if (a->pack_mtime > b->pack_mtime)
276 else if (a->pack_mtime < b->pack_mtime)
279 return a->pack_int_id - b->pack_int_id;
282 static void fill_pack_entry(uint32_t pack_int_id,
283 struct packed_git *p,
285 struct pack_midx_entry *entry)
287 if (!nth_packed_object_oid(&entry->oid, p, cur_object))
288 die(_("failed to locate object %d in packfile"), cur_object);
290 entry->pack_int_id = pack_int_id;
291 entry->pack_mtime = p->mtime;
293 entry->offset = nth_packed_object_offset(p, cur_object);
297 * It is possible to artificially get into a state where there are many
298 * duplicate copies of objects. That can create high memory pressure if
299 * we are to create a list of all objects before de-duplication. To reduce
300 * this memory pressure without a significant performance drop, automatically
301 * group objects by the first byte of their object id. Use the IDX fanout
302 * tables to group the data, copy to a local array, then sort.
304 * Copy only the de-duplicated entries (selected by most-recent modified time
305 * of a packfile containing the object).
307 static struct pack_midx_entry *get_sorted_entries(struct packed_git **p,
310 uint32_t *nr_objects)
312 uint32_t cur_fanout, cur_pack, cur_object;
313 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
314 struct pack_midx_entry *entries_by_fanout = NULL;
315 struct pack_midx_entry *deduplicated_entries = NULL;
317 for (cur_pack = 0; cur_pack < nr_packs; cur_pack++)
318 total_objects += p[cur_pack]->num_objects;
321 * As we de-duplicate by fanout value, we expect the fanout
322 * slices to be evenly distributed, with some noise. Hence,
323 * allocate slightly more than one 256th.
325 alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
327 ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
328 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
331 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
332 uint32_t nr_fanout = 0;
334 for (cur_pack = 0; cur_pack < nr_packs; cur_pack++) {
335 uint32_t start = 0, end;
338 start = get_pack_fanout(p[cur_pack], cur_fanout - 1);
339 end = get_pack_fanout(p[cur_pack], cur_fanout);
341 for (cur_object = start; cur_object < end; cur_object++) {
342 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
343 fill_pack_entry(perm[cur_pack], p[cur_pack], cur_object, &entries_by_fanout[nr_fanout]);
348 QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
351 * The batch is now sorted by OID and then mtime (descending).
352 * Take only the first duplicate.
354 for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
355 if (cur_object && !oidcmp(&entries_by_fanout[cur_object - 1].oid,
356 &entries_by_fanout[cur_object].oid))
359 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
360 memcpy(&deduplicated_entries[*nr_objects],
361 &entries_by_fanout[cur_object],
362 sizeof(struct pack_midx_entry));
367 free(entries_by_fanout);
368 return deduplicated_entries;
371 static size_t write_midx_pack_names(struct hashfile *f,
376 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
379 for (i = 0; i < num_packs; i++) {
380 size_t writelen = strlen(pack_names[i]) + 1;
382 if (i && strcmp(pack_names[i], pack_names[i - 1]) <= 0)
383 BUG("incorrect pack-file order: %s before %s",
387 hashwrite(f, pack_names[i], writelen);
391 /* add padding to be aligned */
392 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
393 if (i < MIDX_CHUNK_ALIGNMENT) {
394 memset(padding, 0, sizeof(padding));
395 hashwrite(f, padding, i);
402 static size_t write_midx_oid_fanout(struct hashfile *f,
403 struct pack_midx_entry *objects,
406 struct pack_midx_entry *list = objects;
407 struct pack_midx_entry *last = objects + nr_objects;
412 * Write the first-level table (the list is sorted,
413 * but we use a 256-entry lookup to be able to avoid
414 * having to do eight extra binary search iterations).
416 for (i = 0; i < 256; i++) {
417 struct pack_midx_entry *next = list;
419 while (next < last && next->oid.hash[0] == i) {
424 hashwrite_be32(f, count);
428 return MIDX_CHUNK_FANOUT_SIZE;
431 static size_t write_midx_oid_lookup(struct hashfile *f, unsigned char hash_len,
432 struct pack_midx_entry *objects,
435 struct pack_midx_entry *list = objects;
439 for (i = 0; i < nr_objects; i++) {
440 struct pack_midx_entry *obj = list++;
442 if (i < nr_objects - 1) {
443 struct pack_midx_entry *next = list;
444 if (oidcmp(&obj->oid, &next->oid) >= 0)
445 BUG("OIDs not in order: %s >= %s",
446 oid_to_hex(&obj->oid),
447 oid_to_hex(&next->oid));
450 hashwrite(f, obj->oid.hash, (int)hash_len);
457 int write_midx_file(const char *object_dir)
459 unsigned char cur_chunk, num_chunks = 0;
462 struct hashfile *f = NULL;
464 struct pack_list packs;
465 uint32_t *pack_perm = NULL;
466 uint64_t written = 0;
467 uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
468 uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
470 struct pack_midx_entry *entries = NULL;
472 midx_name = get_midx_filename(object_dir);
473 if (safe_create_leading_directories(midx_name)) {
475 die_errno(_("unable to create leading directories of %s"),
480 packs.alloc_list = 16;
481 packs.alloc_names = 16;
483 packs.pack_name_concat_len = 0;
484 ALLOC_ARRAY(packs.list, packs.alloc_list);
485 ALLOC_ARRAY(packs.names, packs.alloc_names);
487 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs);
489 if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
490 packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
491 (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
493 ALLOC_ARRAY(pack_perm, packs.nr);
494 sort_packs_by_name(packs.names, packs.nr, pack_perm);
496 entries = get_sorted_entries(packs.list, pack_perm, packs.nr, &nr_entries);
498 hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
499 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
500 FREE_AND_NULL(midx_name);
505 written = write_midx_header(f, num_chunks, packs.nr);
507 chunk_ids[cur_chunk] = MIDX_CHUNKID_PACKNAMES;
508 chunk_offsets[cur_chunk] = written + (num_chunks + 1) * MIDX_CHUNKLOOKUP_WIDTH;
511 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
512 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + packs.pack_name_concat_len;
515 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;
516 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + MIDX_CHUNK_FANOUT_SIZE;
519 chunk_ids[cur_chunk] = 0;
520 chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * MIDX_HASH_LEN;
522 for (i = 0; i <= num_chunks; i++) {
523 if (i && chunk_offsets[i] < chunk_offsets[i - 1])
524 BUG("incorrect chunk offsets: %"PRIu64" before %"PRIu64,
525 chunk_offsets[i - 1],
528 if (chunk_offsets[i] % MIDX_CHUNK_ALIGNMENT)
529 BUG("chunk offset %"PRIu64" is not properly aligned",
532 hashwrite_be32(f, chunk_ids[i]);
533 hashwrite_be32(f, chunk_offsets[i] >> 32);
534 hashwrite_be32(f, chunk_offsets[i]);
536 written += MIDX_CHUNKLOOKUP_WIDTH;
539 for (i = 0; i < num_chunks; i++) {
540 if (written != chunk_offsets[i])
541 BUG("incorrect chunk offset (%"PRIu64" != %"PRIu64") for chunk id %"PRIx32,
546 switch (chunk_ids[i]) {
547 case MIDX_CHUNKID_PACKNAMES:
548 written += write_midx_pack_names(f, packs.names, packs.nr);
551 case MIDX_CHUNKID_OIDFANOUT:
552 written += write_midx_oid_fanout(f, entries, nr_entries);
555 case MIDX_CHUNKID_OIDLOOKUP:
556 written += write_midx_oid_lookup(f, MIDX_HASH_LEN, entries, nr_entries);
560 BUG("trying to write unknown chunk id %"PRIx32,
565 if (written != chunk_offsets[num_chunks])
566 BUG("incorrect final offset %"PRIu64" != %"PRIu64,
568 chunk_offsets[num_chunks]);
570 finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
571 commit_lock_file(&lk);
573 for (i = 0; i < packs.nr; i++) {
575 close_pack(packs.list[i]);
578 free(packs.names[i]);