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 2
22 #define MIDX_CHUNK_ALIGNMENT 4
23 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
24 #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
25 #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
27 static char *get_midx_filename(const char *object_dir
)
29 return xstrfmt("%s/pack/multi-pack-index", object_dir
);
32 struct multi_pack_index
*load_multi_pack_index(const char *object_dir
)
34 struct multi_pack_index
*m
= NULL
;
38 void *midx_map
= NULL
;
39 uint32_t hash_version
;
40 char *midx_name
= get_midx_filename(object_dir
);
42 const char *cur_pack_name
;
44 fd
= git_open(midx_name
);
49 error_errno(_("failed to read %s"), midx_name
);
53 midx_size
= xsize_t(st
.st_size
);
55 if (midx_size
< MIDX_MIN_SIZE
) {
56 error(_("multi-pack-index file %s is too small"), midx_name
);
60 FREE_AND_NULL(midx_name
);
62 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
64 FLEX_ALLOC_MEM(m
, object_dir
, object_dir
, strlen(object_dir
));
67 m
->data_len
= midx_size
;
69 m
->signature
= get_be32(m
->data
);
70 if (m
->signature
!= MIDX_SIGNATURE
) {
71 error(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
72 m
->signature
, MIDX_SIGNATURE
);
76 m
->version
= m
->data
[MIDX_BYTE_FILE_VERSION
];
77 if (m
->version
!= MIDX_VERSION
) {
78 error(_("multi-pack-index version %d not recognized"),
83 hash_version
= m
->data
[MIDX_BYTE_HASH_VERSION
];
84 if (hash_version
!= MIDX_HASH_VERSION
) {
85 error(_("hash version %u does not match"), hash_version
);
88 m
->hash_len
= MIDX_HASH_LEN
;
90 m
->num_chunks
= m
->data
[MIDX_BYTE_NUM_CHUNKS
];
92 m
->num_packs
= get_be32(m
->data
+ MIDX_BYTE_NUM_PACKS
);
94 for (i
= 0; i
< m
->num_chunks
; i
++) {
95 uint32_t chunk_id
= get_be32(m
->data
+ MIDX_HEADER_SIZE
+
96 MIDX_CHUNKLOOKUP_WIDTH
* i
);
97 uint64_t chunk_offset
= get_be64(m
->data
+ MIDX_HEADER_SIZE
+ 4 +
98 MIDX_CHUNKLOOKUP_WIDTH
* i
);
101 case MIDX_CHUNKID_PACKNAMES
:
102 m
->chunk_pack_names
= m
->data
+ chunk_offset
;
105 case MIDX_CHUNKID_OIDLOOKUP
:
106 m
->chunk_oid_lookup
= m
->data
+ chunk_offset
;
110 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
115 * Do nothing on unrecognized chunks, allowing future
116 * extensions to add optional chunks.
122 if (!m
->chunk_pack_names
)
123 die(_("multi-pack-index missing required pack-name chunk"));
124 if (!m
->chunk_oid_lookup
)
125 die(_("multi-pack-index missing required OID lookup chunk"));
127 m
->pack_names
= xcalloc(m
->num_packs
, sizeof(*m
->pack_names
));
129 cur_pack_name
= (const char *)m
->chunk_pack_names
;
130 for (i
= 0; i
< m
->num_packs
; i
++) {
131 m
->pack_names
[i
] = cur_pack_name
;
133 cur_pack_name
+= strlen(cur_pack_name
) + 1;
135 if (i
&& strcmp(m
->pack_names
[i
], m
->pack_names
[i
- 1]) <= 0) {
136 error(_("multi-pack-index pack names out of order: '%s' before '%s'"),
137 m
->pack_names
[i
- 1],
149 munmap(midx_map
, midx_size
);
155 static size_t write_midx_header(struct hashfile
*f
,
156 unsigned char num_chunks
,
159 unsigned char byte_values
[4];
161 hashwrite_be32(f
, MIDX_SIGNATURE
);
162 byte_values
[0] = MIDX_VERSION
;
163 byte_values
[1] = MIDX_HASH_VERSION
;
164 byte_values
[2] = num_chunks
;
165 byte_values
[3] = 0; /* unused */
166 hashwrite(f
, byte_values
, sizeof(byte_values
));
167 hashwrite_be32(f
, num_packs
);
169 return MIDX_HEADER_SIZE
;
173 struct packed_git
**list
;
177 uint32_t alloc_names
;
178 size_t pack_name_concat_len
;
181 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
182 const char *file_name
, void *data
)
184 struct pack_list
*packs
= (struct pack_list
*)data
;
186 if (ends_with(file_name
, ".idx")) {
187 ALLOC_GROW(packs
->list
, packs
->nr
+ 1, packs
->alloc_list
);
188 ALLOC_GROW(packs
->names
, packs
->nr
+ 1, packs
->alloc_names
);
190 packs
->list
[packs
->nr
] = add_packed_git(full_path
,
194 if (!packs
->list
[packs
->nr
]) {
195 warning(_("failed to add packfile '%s'"),
200 if (open_pack_index(packs
->list
[packs
->nr
])) {
201 warning(_("failed to open pack-index '%s'"),
203 close_pack(packs
->list
[packs
->nr
]);
204 FREE_AND_NULL(packs
->list
[packs
->nr
]);
208 packs
->names
[packs
->nr
] = xstrdup(file_name
);
209 packs
->pack_name_concat_len
+= strlen(file_name
) + 1;
215 uint32_t pack_int_id
;
219 static int pack_pair_compare(const void *_a
, const void *_b
)
221 struct pack_pair
*a
= (struct pack_pair
*)_a
;
222 struct pack_pair
*b
= (struct pack_pair
*)_b
;
223 return strcmp(a
->pack_name
, b
->pack_name
);
226 static void sort_packs_by_name(char **pack_names
, uint32_t nr_packs
, uint32_t *perm
)
229 struct pack_pair
*pairs
;
231 ALLOC_ARRAY(pairs
, nr_packs
);
233 for (i
= 0; i
< nr_packs
; i
++) {
234 pairs
[i
].pack_int_id
= i
;
235 pairs
[i
].pack_name
= pack_names
[i
];
238 QSORT(pairs
, nr_packs
, pack_pair_compare
);
240 for (i
= 0; i
< nr_packs
; i
++) {
241 pack_names
[i
] = pairs
[i
].pack_name
;
242 perm
[pairs
[i
].pack_int_id
] = i
;
248 struct pack_midx_entry
{
249 struct object_id oid
;
250 uint32_t pack_int_id
;
255 static int midx_oid_compare(const void *_a
, const void *_b
)
257 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
258 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
259 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
264 if (a
->pack_mtime
> b
->pack_mtime
)
266 else if (a
->pack_mtime
< b
->pack_mtime
)
269 return a
->pack_int_id
- b
->pack_int_id
;
272 static void fill_pack_entry(uint32_t pack_int_id
,
273 struct packed_git
*p
,
275 struct pack_midx_entry
*entry
)
277 if (!nth_packed_object_oid(&entry
->oid
, p
, cur_object
))
278 die(_("failed to locate object %d in packfile"), cur_object
);
280 entry
->pack_int_id
= pack_int_id
;
281 entry
->pack_mtime
= p
->mtime
;
283 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
287 * It is possible to artificially get into a state where there are many
288 * duplicate copies of objects. That can create high memory pressure if
289 * we are to create a list of all objects before de-duplication. To reduce
290 * this memory pressure without a significant performance drop, automatically
291 * group objects by the first byte of their object id. Use the IDX fanout
292 * tables to group the data, copy to a local array, then sort.
294 * Copy only the de-duplicated entries (selected by most-recent modified time
295 * of a packfile containing the object).
297 static struct pack_midx_entry
*get_sorted_entries(struct packed_git
**p
,
300 uint32_t *nr_objects
)
302 uint32_t cur_fanout
, cur_pack
, cur_object
;
303 uint32_t alloc_fanout
, alloc_objects
, total_objects
= 0;
304 struct pack_midx_entry
*entries_by_fanout
= NULL
;
305 struct pack_midx_entry
*deduplicated_entries
= NULL
;
307 for (cur_pack
= 0; cur_pack
< nr_packs
; cur_pack
++)
308 total_objects
+= p
[cur_pack
]->num_objects
;
311 * As we de-duplicate by fanout value, we expect the fanout
312 * slices to be evenly distributed, with some noise. Hence,
313 * allocate slightly more than one 256th.
315 alloc_objects
= alloc_fanout
= total_objects
> 3200 ? total_objects
/ 200 : 16;
317 ALLOC_ARRAY(entries_by_fanout
, alloc_fanout
);
318 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
321 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
322 uint32_t nr_fanout
= 0;
324 for (cur_pack
= 0; cur_pack
< nr_packs
; cur_pack
++) {
325 uint32_t start
= 0, end
;
328 start
= get_pack_fanout(p
[cur_pack
], cur_fanout
- 1);
329 end
= get_pack_fanout(p
[cur_pack
], cur_fanout
);
331 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
332 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
333 fill_pack_entry(perm
[cur_pack
], p
[cur_pack
], cur_object
, &entries_by_fanout
[nr_fanout
]);
338 QSORT(entries_by_fanout
, nr_fanout
, midx_oid_compare
);
341 * The batch is now sorted by OID and then mtime (descending).
342 * Take only the first duplicate.
344 for (cur_object
= 0; cur_object
< nr_fanout
; cur_object
++) {
345 if (cur_object
&& !oidcmp(&entries_by_fanout
[cur_object
- 1].oid
,
346 &entries_by_fanout
[cur_object
].oid
))
349 ALLOC_GROW(deduplicated_entries
, *nr_objects
+ 1, alloc_objects
);
350 memcpy(&deduplicated_entries
[*nr_objects
],
351 &entries_by_fanout
[cur_object
],
352 sizeof(struct pack_midx_entry
));
357 free(entries_by_fanout
);
358 return deduplicated_entries
;
361 static size_t write_midx_pack_names(struct hashfile
*f
,
366 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
369 for (i
= 0; i
< num_packs
; i
++) {
370 size_t writelen
= strlen(pack_names
[i
]) + 1;
372 if (i
&& strcmp(pack_names
[i
], pack_names
[i
- 1]) <= 0)
373 BUG("incorrect pack-file order: %s before %s",
377 hashwrite(f
, pack_names
[i
], writelen
);
381 /* add padding to be aligned */
382 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
383 if (i
< MIDX_CHUNK_ALIGNMENT
) {
384 memset(padding
, 0, sizeof(padding
));
385 hashwrite(f
, padding
, i
);
392 static size_t write_midx_oid_lookup(struct hashfile
*f
, unsigned char hash_len
,
393 struct pack_midx_entry
*objects
,
396 struct pack_midx_entry
*list
= objects
;
400 for (i
= 0; i
< nr_objects
; i
++) {
401 struct pack_midx_entry
*obj
= list
++;
403 if (i
< nr_objects
- 1) {
404 struct pack_midx_entry
*next
= list
;
405 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
406 BUG("OIDs not in order: %s >= %s",
407 oid_to_hex(&obj
->oid
),
408 oid_to_hex(&next
->oid
));
411 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
418 int write_midx_file(const char *object_dir
)
420 unsigned char cur_chunk
, num_chunks
= 0;
423 struct hashfile
*f
= NULL
;
425 struct pack_list packs
;
426 uint32_t *pack_perm
= NULL
;
427 uint64_t written
= 0;
428 uint32_t chunk_ids
[MIDX_MAX_CHUNKS
+ 1];
429 uint64_t chunk_offsets
[MIDX_MAX_CHUNKS
+ 1];
431 struct pack_midx_entry
*entries
= NULL
;
433 midx_name
= get_midx_filename(object_dir
);
434 if (safe_create_leading_directories(midx_name
)) {
436 die_errno(_("unable to create leading directories of %s"),
441 packs
.alloc_list
= 16;
442 packs
.alloc_names
= 16;
444 packs
.pack_name_concat_len
= 0;
445 ALLOC_ARRAY(packs
.list
, packs
.alloc_list
);
446 ALLOC_ARRAY(packs
.names
, packs
.alloc_names
);
448 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &packs
);
450 if (packs
.pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
451 packs
.pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
452 (packs
.pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
454 ALLOC_ARRAY(pack_perm
, packs
.nr
);
455 sort_packs_by_name(packs
.names
, packs
.nr
, pack_perm
);
457 entries
= get_sorted_entries(packs
.list
, pack_perm
, packs
.nr
, &nr_entries
);
459 hold_lock_file_for_update(&lk
, midx_name
, LOCK_DIE_ON_ERROR
);
460 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
461 FREE_AND_NULL(midx_name
);
466 written
= write_midx_header(f
, num_chunks
, packs
.nr
);
468 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_PACKNAMES
;
469 chunk_offsets
[cur_chunk
] = written
+ (num_chunks
+ 1) * MIDX_CHUNKLOOKUP_WIDTH
;
472 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDLOOKUP
;
473 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + packs
.pack_name_concat_len
;
476 chunk_ids
[cur_chunk
] = 0;
477 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + nr_entries
* MIDX_HASH_LEN
;
479 for (i
= 0; i
<= num_chunks
; i
++) {
480 if (i
&& chunk_offsets
[i
] < chunk_offsets
[i
- 1])
481 BUG("incorrect chunk offsets: %"PRIu64
" before %"PRIu64
,
482 chunk_offsets
[i
- 1],
485 if (chunk_offsets
[i
] % MIDX_CHUNK_ALIGNMENT
)
486 BUG("chunk offset %"PRIu64
" is not properly aligned",
489 hashwrite_be32(f
, chunk_ids
[i
]);
490 hashwrite_be32(f
, chunk_offsets
[i
] >> 32);
491 hashwrite_be32(f
, chunk_offsets
[i
]);
493 written
+= MIDX_CHUNKLOOKUP_WIDTH
;
496 for (i
= 0; i
< num_chunks
; i
++) {
497 if (written
!= chunk_offsets
[i
])
498 BUG("incorrect chunk offset (%"PRIu64
" != %"PRIu64
") for chunk id %"PRIx32
,
503 switch (chunk_ids
[i
]) {
504 case MIDX_CHUNKID_PACKNAMES
:
505 written
+= write_midx_pack_names(f
, packs
.names
, packs
.nr
);
508 case MIDX_CHUNKID_OIDLOOKUP
:
509 written
+= write_midx_oid_lookup(f
, MIDX_HASH_LEN
, entries
, nr_entries
);
513 BUG("trying to write unknown chunk id %"PRIx32
,
518 if (written
!= chunk_offsets
[num_chunks
])
519 BUG("incorrect final offset %"PRIu64
" != %"PRIu64
,
521 chunk_offsets
[num_chunks
]);
523 finalize_hashfile(f
, NULL
, CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
524 commit_lock_file(&lk
);
526 for (i
= 0; i
< packs
.nr
; i
++) {
528 close_pack(packs
.list
[i
]);
531 free(packs
.names
[i
]);