pack-bitmap: add support for bitmap indexes

A bitmap index is a `.bitmap` file that can be found inside `$GIT_DIR/objects/pack/`, next to its corresponding packfile, and contains precalculated reachability information for selected commits. The full specification of the format for these bitmap indexes can be found in `Documentation/technical/bitmap-format.txt`. For a given commit SHA1, if it happens to be available in the bitmap index, its bitmap will represent every single object that is reachable from the commit itself. The nth bit in the bitmap is the nth object in the packfile; if it's set to 1, the object is reachable. By using the bitmaps available in the index, this commit implements several new functions: - `prepare_bitmap_git` - `prepare_bitmap_walk` - `traverse_bitmap_commit_list` - `reuse_partial_packfile_from_bitmap` The `prepare_bitmap_walk` function tries to build a bitmap of all the objects that can be reached from the commit roots of a given `rev_info` struct by using the following algorithm: - If all the interesting commits for a revision walk are available in the index, the resulting reachability bitmap is the bitwise OR of all the individual bitmaps. - When the full set of WANTs is not available in the index, we perform a partial revision walk using the commits that don't have bitmaps as roots, and limiting the revision walk as soon as we reach a commit that has a corresponding bitmap. The earlier OR'ed bitmap with all the indexed commits can now be completed as this walk progresses, so the end result is the full reachability list. - For revision walks with a HAVEs set (a set of commits that are deemed uninteresting), first we perform the same method as for the WANTs, but using our HAVEs as roots, in order to obtain a full reachability bitmap of all the uninteresting commits. This bitmap then can be used to: a) limit the subsequent walk when building the WANTs bitmap b) finding the final set of interesting commits by performing an AND-NOT of the WANTs and the HAVEs. If `prepare_bitmap_walk` runs successfully, the resulting bitmap is stored and the equivalent of a `traverse_commit_list` call can be performed by using `traverse_bitmap_commit_list`; the bitmap version of this call yields the objects straight from the packfile index (without having to look them up or parse them) and hence is several orders of magnitude faster. As an extra optimization, when `prepare_bitmap_walk` succeeds, the `reuse_partial_packfile_from_bitmap` call can be attempted: it will find the amount of objects at the beginning of the on-disk packfile that can be reused as-is, and return an offset into the packfile. The source packfile can then be loaded and the bytes up to `offset` can be written directly to the result without having to consider the entires inside the packfile individually. If the `prepare_bitmap_walk` call fails (e.g. because no bitmap files are available), the `rev_info` struct is left untouched, and can be used to perform a manual rev-walk using `traverse_commit_list`. Hence, this new set of functions are a generic API that allows to perform the equivalent of git rev-list --objects [roots...] [^uninteresting...] for any set of commits, even if they don't have specific bitmaps generated for them. In further patches, we'll use this bitmap traversal optimization to speed up the `pack-objects` and `rev-list` commands. Signed-off-by: Vicent Marti <tanoku@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Vicent Marti committed Dec 21, 2013 at 09:00 UTC fff42755efc7585378ca562e54828532922371be
4 files changed +1353
Makefile
+2
@@ -699,6 +699,7 @@ LIB_H += object.h
699 LIB_H += pack-objects.h
700 LIB_H += pack-revindex.h
701 LIB_H += pack.h
702 +LIB_H += pack-bitmap.h
703 LIB_H += parse-options.h
704 LIB_H += patch-ids.h
705 LIB_H += pathspec.h
@@ -837,6 +838,7 @@ LIB_OBJS += notes-cache.o
838 LIB_OBJS += notes-merge.o
839 LIB_OBJS += notes-utils.o
840 LIB_OBJS += object.o
841 +LIB_OBJS += pack-bitmap.o
842 LIB_OBJS += pack-check.o
843 LIB_OBJS += pack-objects.o
844 LIB_OBJS += pack-revindex.o
khash.h new
+338
@@ -0,0 +1,338 @@
1 +/* The MIT License
2 +
3 + Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk>
4 +
5 + Permission is hereby granted, free of charge, to any person obtaining
6 + a copy of this software and associated documentation files (the
7 + "Software"), to deal in the Software without restriction, including
8 + without limitation the rights to use, copy, modify, merge, publish,
9 + distribute, sublicense, and/or sell copies of the Software, and to
10 + permit persons to whom the Software is furnished to do so, subject to
11 + the following conditions:
12 +
13 + The above copyright notice and this permission notice shall be
14 + included in all copies or substantial portions of the Software.
15 +
16 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 + SOFTWARE.
24 +*/
25 +
26 +#ifndef __AC_KHASH_H
27 +#define __AC_KHASH_H
28 +
29 +#define AC_VERSION_KHASH_H "0.2.8"
30 +
31 +typedef uint32_t khint32_t;
32 +typedef uint64_t khint64_t;
33 +
34 +typedef khint32_t khint_t;
35 +typedef khint_t khiter_t;
36 +
37 +#define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)
38 +#define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)
39 +#define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)
40 +#define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))
41 +#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))
42 +#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))
43 +#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))
44 +
45 +#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4)
46 +
47 +#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
48 +
49 +static inline khint_t __ac_X31_hash_string(const char *s)
50 +{
51 + khint_t h = (khint_t)*s;
52 + if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;
53 + return h;
54 +}
55 +
56 +#define kh_str_hash_func(key) __ac_X31_hash_string(key)
57 +#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)
58 +
59 +static const double __ac_HASH_UPPER = 0.77;
60 +
61 +#define __KHASH_TYPE(name, khkey_t, khval_t) \
62 + typedef struct { \
63 + khint_t n_buckets, size, n_occupied, upper_bound; \
64 + khint32_t *flags; \
65 + khkey_t *keys; \
66 + khval_t *vals; \
67 + } kh_##name##_t;
68 +
69 +#define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \
70 + extern kh_##name##_t *kh_init_##name(void); \
71 + extern void kh_destroy_##name(kh_##name##_t *h); \
72 + extern void kh_clear_##name(kh_##name##_t *h); \
73 + extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
74 + extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
75 + extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
76 + extern void kh_del_##name(kh_##name##_t *h, khint_t x);
77 +
78 +#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
79 + SCOPE kh_##name##_t *kh_init_##name(void) { \
80 + return (kh_##name##_t*)xcalloc(1, sizeof(kh_##name##_t)); \
81 + } \
82 + SCOPE void kh_destroy_##name(kh_##name##_t *h) \
83 + { \
84 + if (h) { \
85 + free((void *)h->keys); free(h->flags); \
86 + free((void *)h->vals); \
87 + free(h); \
88 + } \
89 + } \
90 + SCOPE void kh_clear_##name(kh_##name##_t *h) \
91 + { \
92 + if (h && h->flags) { \
93 + memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \
94 + h->size = h->n_occupied = 0; \
95 + } \
96 + } \
97 + SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \
98 + { \
99 + if (h->n_buckets) { \
100 + khint_t k, i, last, mask, step = 0; \
101 + mask = h->n_buckets - 1; \
102 + k = __hash_func(key); i = k & mask; \
103 + last = i; \
104 + while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
105 + i = (i + (++step)) & mask; \
106 + if (i == last) return h->n_buckets; \
107 + } \
108 + return __ac_iseither(h->flags, i)? h->n_buckets : i; \
109 + } else return 0; \
110 + } \
111 + SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
112 + { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
113 + khint32_t *new_flags = NULL; \
114 + khint_t j = 1; \
115 + { \
116 + kroundup32(new_n_buckets); \
117 + if (new_n_buckets < 4) new_n_buckets = 4; \
118 + if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
119 + else { /* hash table size to be changed (shrink or expand); rehash */ \
120 + new_flags = (khint32_t*)xmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
121 + if (!new_flags) return -1; \
122 + memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
123 + if (h->n_buckets < new_n_buckets) { /* expand */ \
124 + khkey_t *new_keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
125 + if (!new_keys) return -1; \
126 + h->keys = new_keys; \
127 + if (kh_is_map) { \
128 + khval_t *new_vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
129 + if (!new_vals) return -1; \
130 + h->vals = new_vals; \
131 + } \
132 + } /* otherwise shrink */ \
133 + } \
134 + } \
135 + if (j) { /* rehashing is needed */ \
136 + for (j = 0; j != h->n_buckets; ++j) { \
137 + if (__ac_iseither(h->flags, j) == 0) { \
138 + khkey_t key = h->keys[j]; \
139 + khval_t val; \
140 + khint_t new_mask; \
141 + new_mask = new_n_buckets - 1; \
142 + if (kh_is_map) val = h->vals[j]; \
143 + __ac_set_isdel_true(h->flags, j); \
144 + while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \
145 + khint_t k, i, step = 0; \
146 + k = __hash_func(key); \
147 + i = k & new_mask; \
148 + while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \
149 + __ac_set_isempty_false(new_flags, i); \
150 + if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \
151 + { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \
152 + if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \
153 + __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \
154 + } else { /* write the element and jump out of the loop */ \
155 + h->keys[i] = key; \
156 + if (kh_is_map) h->vals[i] = val; \
157 + break; \
158 + } \
159 + } \
160 + } \
161 + } \
162 + if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
163 + h->keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
164 + if (kh_is_map) h->vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
165 + } \
166 + free(h->flags); /* free the working space */ \
167 + h->flags = new_flags; \
168 + h->n_buckets = new_n_buckets; \
169 + h->n_occupied = h->size; \
170 + h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
171 + } \
172 + return 0; \
173 + } \
174 + SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
175 + { \
176 + khint_t x; \
177 + if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
178 + if (h->n_buckets > (h->size<<1)) { \
179 + if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
180 + *ret = -1; return h->n_buckets; \
181 + } \
182 + } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
183 + *ret = -1; return h->n_buckets; \
184 + } \
185 + } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
186 + { \
187 + khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \
188 + x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \
189 + if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \
190 + else { \
191 + last = i; \
192 + while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \
193 + if (__ac_isdel(h->flags, i)) site = i; \
194 + i = (i + (++step)) & mask; \
195 + if (i == last) { x = site; break; } \
196 + } \
197 + if (x == h->n_buckets) { \
198 + if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \
199 + else x = i; \
200 + } \
201 + } \
202 + } \
203 + if (__ac_isempty(h->flags, x)) { /* not present at all */ \
204 + h->keys[x] = key; \
205 + __ac_set_isboth_false(h->flags, x); \
206 + ++h->size; ++h->n_occupied; \
207 + *ret = 1; \
208 + } else if (__ac_isdel(h->flags, x)) { /* deleted */ \
209 + h->keys[x] = key; \
210 + __ac_set_isboth_false(h->flags, x); \
211 + ++h->size; \
212 + *ret = 2; \
213 + } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \
214 + return x; \
215 + } \
216 + SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \
217 + { \
218 + if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \
219 + __ac_set_isdel_true(h->flags, x); \
220 + --h->size; \
221 + } \
222 + }
223 +
224 +#define KHASH_DECLARE(name, khkey_t, khval_t) \
225 + __KHASH_TYPE(name, khkey_t, khval_t) \
226 + __KHASH_PROTOTYPES(name, khkey_t, khval_t)
227 +
228 +#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
229 + __KHASH_TYPE(name, khkey_t, khval_t) \
230 + __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
231 +
232 +#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
233 + KHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)
234 +
235 +/* Other convenient macros... */
236 +
237 +/*! @function
238 + @abstract Test whether a bucket contains data.
239 + @param h Pointer to the hash table [khash_t(name)*]
240 + @param x Iterator to the bucket [khint_t]
241 + @return 1 if containing data; 0 otherwise [int]
242 + */
243 +#define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))
244 +
245 +/*! @function
246 + @abstract Get key given an iterator
247 + @param h Pointer to the hash table [khash_t(name)*]
248 + @param x Iterator to the bucket [khint_t]
249 + @return Key [type of keys]
250 + */
251 +#define kh_key(h, x) ((h)->keys[x])
252 +
253 +/*! @function
254 + @abstract Get value given an iterator
255 + @param h Pointer to the hash table [khash_t(name)*]
256 + @param x Iterator to the bucket [khint_t]
257 + @return Value [type of values]
258 + @discussion For hash sets, calling this results in segfault.
259 + */
260 +#define kh_val(h, x) ((h)->vals[x])
261 +
262 +/*! @function
263 + @abstract Alias of kh_val()
264 + */
265 +#define kh_value(h, x) ((h)->vals[x])
266 +
267 +/*! @function
268 + @abstract Get the start iterator
269 + @param h Pointer to the hash table [khash_t(name)*]
270 + @return The start iterator [khint_t]
271 + */
272 +#define kh_begin(h) (khint_t)(0)
273 +
274 +/*! @function
275 + @abstract Get the end iterator
276 + @param h Pointer to the hash table [khash_t(name)*]
277 + @return The end iterator [khint_t]
278 + */
279 +#define kh_end(h) ((h)->n_buckets)
280 +
281 +/*! @function
282 + @abstract Get the number of elements in the hash table
283 + @param h Pointer to the hash table [khash_t(name)*]
284 + @return Number of elements in the hash table [khint_t]
285 + */
286 +#define kh_size(h) ((h)->size)
287 +
288 +/*! @function
289 + @abstract Get the number of buckets in the hash table
290 + @param h Pointer to the hash table [khash_t(name)*]
291 + @return Number of buckets in the hash table [khint_t]
292 + */
293 +#define kh_n_buckets(h) ((h)->n_buckets)
294 +
295 +/*! @function
296 + @abstract Iterate over the entries in the hash table
297 + @param h Pointer to the hash table [khash_t(name)*]
298 + @param kvar Variable to which key will be assigned
299 + @param vvar Variable to which value will be assigned
300 + @param code Block of code to execute
301 + */
302 +#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \
303 + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
304 + if (!kh_exist(h,__i)) continue; \
305 + (kvar) = kh_key(h,__i); \
306 + (vvar) = kh_val(h,__i); \
307 + code; \
308 + } }
309 +
310 +/*! @function
311 + @abstract Iterate over the values in the hash table
312 + @param h Pointer to the hash table [khash_t(name)*]
313 + @param vvar Variable to which value will be assigned
314 + @param code Block of code to execute
315 + */
316 +#define kh_foreach_value(h, vvar, code) { khint_t __i; \
317 + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \
318 + if (!kh_exist(h,__i)) continue; \
319 + (vvar) = kh_val(h,__i); \
320 + code; \
321 + } }
322 +
323 +static inline khint_t __kh_oid_hash(const unsigned char *oid)
324 +{
325 + khint_t hash;
326 + memcpy(&hash, oid, sizeof(hash));
327 + return hash;
328 +}
329 +
330 +#define __kh_oid_cmp(a, b) (hashcmp(a, b) == 0)
331 +
332 +KHASH_INIT(sha1, const unsigned char *, void *, 1, __kh_oid_hash, __kh_oid_cmp)
333 +typedef kh_sha1_t khash_sha1;
334 +
335 +KHASH_INIT(sha1_pos, const unsigned char *, int, 1, __kh_oid_hash, __kh_oid_cmp)
336 +typedef kh_sha1_pos_t khash_sha1_pos;
337 +
338 +#endif /* __AC_KHASH_H */
pack-bitmap.c new
+970
@@ -0,0 +1,970 @@
1 +#include "cache.h"
2 +#include "commit.h"
3 +#include "tag.h"
4 +#include "diff.h"
5 +#include "revision.h"
6 +#include "progress.h"
7 +#include "list-objects.h"
8 +#include "pack.h"
9 +#include "pack-bitmap.h"
10 +#include "pack-revindex.h"
11 +#include "pack-objects.h"
12 +
13 +/*
14 + * An entry on the bitmap index, representing the bitmap for a given
15 + * commit.
16 + */
17 +struct stored_bitmap {
18 + unsigned char sha1[20];
19 + struct ewah_bitmap *root;
20 + struct stored_bitmap *xor;
21 + int flags;
22 +};
23 +
24 +/*
25 + * The currently active bitmap index. By design, repositories only have
26 + * a single bitmap index available (the index for the biggest packfile in
27 + * the repository), since bitmap indexes need full closure.
28 + *
29 + * If there is more than one bitmap index available (e.g. because of alternates),
30 + * the active bitmap index is the largest one.
31 + */
32 +static struct bitmap_index {
33 + /* Packfile to which this bitmap index belongs to */
34 + struct packed_git *pack;
35 +
36 + /* reverse index for the packfile */
37 + struct pack_revindex *reverse_index;
38 +
39 + /*
40 + * Mark the first `reuse_objects` in the packfile as reused:
41 + * they will be sent as-is without using them for repacking
42 + * calculations
43 + */
44 + uint32_t reuse_objects;
45 +
46 + /* mmapped buffer of the whole bitmap index */
47 + unsigned char *map;
48 + size_t map_size; /* size of the mmaped buffer */
49 + size_t map_pos; /* current position when loading the index */
50 +
51 + /*
52 + * Type indexes.
53 + *
54 + * Each bitmap marks which objects in the packfile are of the given
55 + * type. This provides type information when yielding the objects from
56 + * the packfile during a walk, which allows for better delta bases.
57 + */
58 + struct ewah_bitmap *commits;
59 + struct ewah_bitmap *trees;
60 + struct ewah_bitmap *blobs;
61 + struct ewah_bitmap *tags;
62 +
63 + /* Map from SHA1 -> `stored_bitmap` for all the bitmapped comits */
64 + khash_sha1 *bitmaps;
65 +
66 + /* Number of bitmapped commits */
67 + uint32_t entry_count;
68 +
69 + /*
70 + * Extended index.
71 + *
72 + * When trying to perform bitmap operations with objects that are not
73 + * packed in `pack`, these objects are added to this "fake index" and
74 + * are assumed to appear at the end of the packfile for all operations
75 + */
76 + struct eindex {
77 + struct object **objects;
78 + uint32_t *hashes;
79 + uint32_t count, alloc;
80 + khash_sha1_pos *positions;
81 + } ext_index;
82 +
83 + /* Bitmap result of the last performed walk */
84 + struct bitmap *result;
85 +
86 + /* Version of the bitmap index */
87 + unsigned int version;
88 +
89 + unsigned loaded : 1;
90 +
91 +} bitmap_git;
92 +
93 +static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
94 +{
95 + struct ewah_bitmap *parent;
96 + struct ewah_bitmap *composed;
97 +
98 + if (st->xor == NULL)
99 + return st->root;
100 +
101 + composed = ewah_pool_new();
102 + parent = lookup_stored_bitmap(st->xor);
103 + ewah_xor(st->root, parent, composed);
104 +
105 + ewah_pool_free(st->root);
106 + st->root = composed;
107 + st->xor = NULL;
108 +
109 + return composed;
110 +}
111 +
112 +/*
113 + * Read a bitmap from the current read position on the mmaped
114 + * index, and increase the read position accordingly
115 + */
116 +static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
117 +{
118 + struct ewah_bitmap *b = ewah_pool_new();
119 +
120 + int bitmap_size = ewah_read_mmap(b,
121 + index->map + index->map_pos,
122 + index->map_size - index->map_pos);
123 +
124 + if (bitmap_size < 0) {
125 + error("Failed to load bitmap index (corrupted?)");
126 + ewah_pool_free(b);
127 + return NULL;
128 + }
129 +
130 + index->map_pos += bitmap_size;
131 + return b;
132 +}
133 +
134 +static int load_bitmap_header(struct bitmap_index *index)
135 +{
136 + struct bitmap_disk_header *header = (void *)index->map;
137 +
138 + if (index->map_size < sizeof(*header) + 20)
139 + return error("Corrupted bitmap index (missing header data)");
140 +
141 + if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
142 + return error("Corrupted bitmap index file (wrong header)");
143 +
144 + index->version = ntohs(header->version);
145 + if (index->version != 1)
146 + return error("Unsupported version for bitmap index file (%d)", index->version);
147 +
148 + /* Parse known bitmap format options */
149 + {
150 + uint32_t flags = ntohs(header->options);
151 +
152 + if ((flags & BITMAP_OPT_FULL_DAG) == 0)
153 + return error("Unsupported options for bitmap index file "
154 + "(Git requires BITMAP_OPT_FULL_DAG)");
155 + }
156 +
157 + index->entry_count = ntohl(header->entry_count);
158 + index->map_pos += sizeof(*header);
159 + return 0;
160 +}
161 +
162 +static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
163 + struct ewah_bitmap *root,
164 + const unsigned char *sha1,
165 + struct stored_bitmap *xor_with,
166 + int flags)
167 +{
168 + struct stored_bitmap *stored;
169 + khiter_t hash_pos;
170 + int ret;
171 +
172 + stored = xmalloc(sizeof(struct stored_bitmap));
173 + stored->root = root;
174 + stored->xor = xor_with;
175 + stored->flags = flags;
176 + hashcpy(stored->sha1, sha1);
177 +
178 + hash_pos = kh_put_sha1(index->bitmaps, stored->sha1, &ret);
179 +
180 + /* a 0 return code means the insertion succeeded with no changes,
181 + * because the SHA1 already existed on the map. this is bad, there
182 + * shouldn't be duplicated commits in the index */
183 + if (ret == 0) {
184 + error("Duplicate entry in bitmap index: %s", sha1_to_hex(sha1));
185 + return NULL;
186 + }
187 +
188 + kh_value(index->bitmaps, hash_pos) = stored;
189 + return stored;
190 +}
191 +
192 +static int load_bitmap_entries_v1(struct bitmap_index *index)
193 +{
194 + static const size_t MAX_XOR_OFFSET = 160;
195 +
196 + uint32_t i;
197 + struct stored_bitmap **recent_bitmaps;
198 + struct bitmap_disk_entry *entry;
199 +
200 + recent_bitmaps = xcalloc(MAX_XOR_OFFSET, sizeof(struct stored_bitmap));
201 +
202 + for (i = 0; i < index->entry_count; ++i) {
203 + int xor_offset, flags;
204 + struct ewah_bitmap *bitmap = NULL;
205 + struct stored_bitmap *xor_bitmap = NULL;
206 + uint32_t commit_idx_pos;
207 + const unsigned char *sha1;
208 +
209 + entry = (struct bitmap_disk_entry *)(index->map + index->map_pos);
210 + index->map_pos += sizeof(struct bitmap_disk_entry);
211 +
212 + commit_idx_pos = ntohl(entry->object_pos);
213 + sha1 = nth_packed_object_sha1(index->pack, commit_idx_pos);
214 +
215 + xor_offset = (int)entry->xor_offset;
216 + flags = (int)entry->flags;
217 +
218 + bitmap = read_bitmap_1(index);
219 + if (!bitmap)
220 + return -1;
221 +
222 + if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
223 + return error("Corrupted bitmap pack index");
224 +
225 + if (xor_offset > 0) {
226 + xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
227 +
228 + if (xor_bitmap == NULL)
229 + return error("Invalid XOR offset in bitmap pack index");
230 + }
231 +
232 + recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
233 + index, bitmap, sha1, xor_bitmap, flags);
234 + }
235 +
236 + return 0;
237 +}
238 +
239 +static int open_pack_bitmap_1(struct packed_git *packfile)
240 +{
241 + int fd;
242 + struct stat st;
243 + char *idx_name;
244 +
245 + if (open_pack_index(packfile))
246 + return -1;
247 +
248 + idx_name = pack_bitmap_filename(packfile);
249 + fd = git_open_noatime(idx_name);
250 + free(idx_name);
251 +
252 + if (fd < 0)
253 + return -1;
254 +
255 + if (fstat(fd, &st)) {
256 + close(fd);
257 + return -1;
258 + }
259 +
260 + if (bitmap_git.pack) {
261 + warning("ignoring extra bitmap file: %s", packfile->pack_name);
262 + close(fd);
263 + return -1;
264 + }
265 +
266 + bitmap_git.pack = packfile;
267 + bitmap_git.map_size = xsize_t(st.st_size);
268 + bitmap_git.map = xmmap(NULL, bitmap_git.map_size, PROT_READ, MAP_PRIVATE, fd, 0);
269 + bitmap_git.map_pos = 0;
270 + close(fd);
271 +
272 + if (load_bitmap_header(&bitmap_git) < 0) {
273 + munmap(bitmap_git.map, bitmap_git.map_size);
274 + bitmap_git.map = NULL;
275 + bitmap_git.map_size = 0;
276 + return -1;
277 + }
278 +
279 + return 0;
280 +}
281 +
282 +static int load_pack_bitmap(void)
283 +{
284 + assert(bitmap_git.map && !bitmap_git.loaded);
285 +
286 + bitmap_git.bitmaps = kh_init_sha1();
287 + bitmap_git.ext_index.positions = kh_init_sha1_pos();
288 + bitmap_git.reverse_index = revindex_for_pack(bitmap_git.pack);
289 +
290 + if (!(bitmap_git.commits = read_bitmap_1(&bitmap_git)) ||
291 + !(bitmap_git.trees = read_bitmap_1(&bitmap_git)) ||
292 + !(bitmap_git.blobs = read_bitmap_1(&bitmap_git)) ||
293 + !(bitmap_git.tags = read_bitmap_1(&bitmap_git)))
294 + goto failed;
295 +
296 + if (load_bitmap_entries_v1(&bitmap_git) < 0)
297 + goto failed;
298 +
299 + bitmap_git.loaded = 1;
300 + return 0;
301 +
302 +failed:
303 + munmap(bitmap_git.map, bitmap_git.map_size);
304 + bitmap_git.map = NULL;
305 + bitmap_git.map_size = 0;
306 + return -1;
307 +}
308 +
309 +char *pack_bitmap_filename(struct packed_git *p)
310 +{
311 + char *idx_name;
312 + int len;
313 +
314 + len = strlen(p->pack_name) - strlen(".pack");
315 + idx_name = xmalloc(len + strlen(".bitmap") + 1);
316 +
317 + memcpy(idx_name, p->pack_name, len);
318 + memcpy(idx_name + len, ".bitmap", strlen(".bitmap") + 1);
319 +
320 + return idx_name;
321 +}
322 +
323 +static int open_pack_bitmap(void)
324 +{
325 + struct packed_git *p;
326 + int ret = -1;
327 +
328 + assert(!bitmap_git.map && !bitmap_git.loaded);
329 +
330 + prepare_packed_git();
331 + for (p = packed_git; p; p = p->next) {
332 + if (open_pack_bitmap_1(p) == 0)
333 + ret = 0;
334 + }
335 +
336 + return ret;
337 +}
338 +
339 +int prepare_bitmap_git(void)
340 +{
341 + if (bitmap_git.loaded)
342 + return 0;
343 +
344 + if (!open_pack_bitmap())
345 + return load_pack_bitmap();
346 +
347 + return -1;
348 +}
349 +
350 +struct include_data {
351 + struct bitmap *base;
352 + struct bitmap *seen;
353 +};
354 +
355 +static inline int bitmap_position_extended(const unsigned char *sha1)
356 +{
357 + khash_sha1_pos *positions = bitmap_git.ext_index.positions;
358 + khiter_t pos = kh_get_sha1_pos(positions, sha1);
359 +
360 + if (pos < kh_end(positions)) {
361 + int bitmap_pos = kh_value(positions, pos);
362 + return bitmap_pos + bitmap_git.pack->num_objects;
363 + }
364 +
365 + return -1;
366 +}
367 +
368 +static inline int bitmap_position_packfile(const unsigned char *sha1)
369 +{
370 + off_t offset = find_pack_entry_one(sha1, bitmap_git.pack);
371 + if (!offset)
372 + return -1;
373 +
374 + return find_revindex_position(bitmap_git.reverse_index, offset);
375 +}
376 +
377 +static int bitmap_position(const unsigned char *sha1)
378 +{
379 + int pos = bitmap_position_packfile(sha1);
380 + return (pos >= 0) ? pos : bitmap_position_extended(sha1);
381 +}
382 +
383 +static int ext_index_add_object(struct object *object, const char *name)
384 +{
385 + struct eindex *eindex = &bitmap_git.ext_index;
386 +
387 + khiter_t hash_pos;
388 + int hash_ret;
389 + int bitmap_pos;
390 +
391 + hash_pos = kh_put_sha1_pos(eindex->positions, object->sha1, &hash_ret);
392 + if (hash_ret > 0) {
393 + if (eindex->count >= eindex->alloc) {
394 + eindex->alloc = (eindex->alloc + 16) * 3 / 2;
395 + eindex->objects = xrealloc(eindex->objects,
396 + eindex->alloc * sizeof(struct object *));
397 + eindex->hashes = xrealloc(eindex->hashes,
398 + eindex->alloc * sizeof(uint32_t));
399 + }
400 +
401 + bitmap_pos = eindex->count;
402 + eindex->objects[eindex->count] = object;
403 + eindex->hashes[eindex->count] = pack_name_hash(name);
404 + kh_value(eindex->positions, hash_pos) = bitmap_pos;
405 + eindex->count++;
406 + } else {
407 + bitmap_pos = kh_value(eindex->positions, hash_pos);
408 + }
409 +
410 + return bitmap_pos + bitmap_git.pack->num_objects;
411 +}
412 +
413 +static void show_object(struct object *object, const struct name_path *path,
414 + const char *last, void *data)
415 +{
416 + struct bitmap *base = data;
417 + int bitmap_pos;
418 +
419 + bitmap_pos = bitmap_position(object->sha1);
420 +
421 + if (bitmap_pos < 0) {
422 + char *name = path_name(path, last);
423 + bitmap_pos = ext_index_add_object(object, name);
424 + free(name);
425 + }
426 +
427 + bitmap_set(base, bitmap_pos);
428 +}
429 +
430 +static void show_commit(struct commit *commit, void *data)
431 +{
432 +}
433 +
434 +static int add_to_include_set(struct include_data *data,
435 + const unsigned char *sha1,
436 + int bitmap_pos)
437 +{
438 + khiter_t hash_pos;
439 +
440 + if (data->seen && bitmap_get(data->seen, bitmap_pos))
441 + return 0;
442 +
443 + if (bitmap_get(data->base, bitmap_pos))
444 + return 0;
445 +
446 + hash_pos = kh_get_sha1(bitmap_git.bitmaps, sha1);
447 + if (hash_pos < kh_end(bitmap_git.bitmaps)) {
448 + struct stored_bitmap *st = kh_value(bitmap_git.bitmaps, hash_pos);
449 + bitmap_or_ewah(data->base, lookup_stored_bitmap(st));
450 + return 0;
451 + }
452 +
453 + bitmap_set(data->base, bitmap_pos);
454 + return 1;
455 +}
456 +
457 +static int should_include(struct commit *commit, void *_data)
458 +{
459 + struct include_data *data = _data;
460 + int bitmap_pos;
461 +
462 + bitmap_pos = bitmap_position(commit->object.sha1);
463 + if (bitmap_pos < 0)
464 + bitmap_pos = ext_index_add_object((struct object *)commit, NULL);
465 +
466 + if (!add_to_include_set(data, commit->object.sha1, bitmap_pos)) {
467 + struct commit_list *parent = commit->parents;
468 +
469 + while (parent) {
470 + parent->item->object.flags |= SEEN;
471 + parent = parent->next;
472 + }
473 +
474 + return 0;
475 + }
476 +
477 + return 1;
478 +}
479 +
480 +static struct bitmap *find_objects(struct rev_info *revs,
481 + struct object_list *roots,
482 + struct bitmap *seen)
483 +{
484 + struct bitmap *base = NULL;
485 + int needs_walk = 0;
486 +
487 + struct object_list *not_mapped = NULL;
488 +
489 + /*
490 + * Go through all the roots for the walk. The ones that have bitmaps
491 + * on the bitmap index will be `or`ed together to form an initial
492 + * global reachability analysis.
493 + *
494 + * The ones without bitmaps in the index will be stored in the
495 + * `not_mapped_list` for further processing.
496 + */
497 + while (roots) {
498 + struct object *object = roots->item;
499 + roots = roots->next;
500 +
501 + if (object->type == OBJ_COMMIT) {
502 + khiter_t pos = kh_get_sha1(bitmap_git.bitmaps, object->sha1);
503 +
504 + if (pos < kh_end(bitmap_git.bitmaps)) {
505 + struct stored_bitmap *st = kh_value(bitmap_git.bitmaps, pos);
506 + struct ewah_bitmap *or_with = lookup_stored_bitmap(st);
507 +
508 + if (base == NULL)
509 + base = ewah_to_bitmap(or_with);
510 + else
511 + bitmap_or_ewah(base, or_with);
512 +
513 + object->flags |= SEEN;
514 + continue;
515 + }
516 + }
517 +
518 + object_list_insert(object, &not_mapped);
519 + }
520 +
521 + /*
522 + * Best case scenario: We found bitmaps for all the roots,
523 + * so the resulting `or` bitmap has the full reachability analysis
524 + */
525 + if (not_mapped == NULL)
526 + return base;
527 +
528 + roots = not_mapped;
529 +
530 + /*
531 + * Let's iterate through all the roots that don't have bitmaps to
532 + * check if we can determine them to be reachable from the existing
533 + * global bitmap.
534 + *
535 + * If we cannot find them in the existing global bitmap, we'll need
536 + * to push them to an actual walk and run it until we can confirm
537 + * they are reachable
538 + */
539 + while (roots) {
540 + struct object *object = roots->item;
541 + int pos;
542 +
543 + roots = roots->next;
544 + pos = bitmap_position(object->sha1);
545 +
546 + if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
547 + object->flags &= ~UNINTERESTING;
548 + add_pending_object(revs, object, "");
549 + needs_walk = 1;
550 + } else {
551 + object->flags |= SEEN;
552 + }
553 + }
554 +
555 + if (needs_walk) {
556 + struct include_data incdata;
557 +
558 + if (base == NULL)
559 + base = bitmap_new();
560 +
561 + incdata.base = base;
562 + incdata.seen = seen;
563 +
564 + revs->include_check = should_include;
565 + revs->include_check_data = &incdata;
566 +
567 + if (prepare_revision_walk(revs))
568 + die("revision walk setup failed");
569 +
570 + traverse_commit_list(revs, show_commit, show_object, base);
571 + }
572 +
573 + return base;
574 +}
575 +
576 +static void show_extended_objects(struct bitmap *objects,
577 + show_reachable_fn show_reach)
578 +{
579 + struct eindex *eindex = &bitmap_git.ext_index;
580 + uint32_t i;
581 +
582 + for (i = 0; i < eindex->count; ++i) {
583 + struct object *obj;
584 +
585 + if (!bitmap_get(objects, bitmap_git.pack->num_objects + i))
586 + continue;
587 +
588 + obj = eindex->objects[i];
589 + show_reach(obj->sha1, obj->type, 0, eindex->hashes[i], NULL, 0);
590 + }
591 +}
592 +
593 +static void show_objects_for_type(
594 + struct bitmap *objects,
595 + struct ewah_bitmap *type_filter,
596 + enum object_type object_type,
597 + show_reachable_fn show_reach)
598 +{
599 + size_t pos = 0, i = 0;
600 + uint32_t offset;
601 +
602 + struct ewah_iterator it;
603 + eword_t filter;
604 +
605 + if (bitmap_git.reuse_objects == bitmap_git.pack->num_objects)
606 + return;
607 +
608 + ewah_iterator_init(&it, type_filter);
609 +
610 + while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
611 + eword_t word = objects->words[i] & filter;
612 +
613 + for (offset = 0; offset < BITS_IN_WORD; ++offset) {
614 + const unsigned char *sha1;
615 + struct revindex_entry *entry;
616 + uint32_t hash = 0;
617 +
618 + if ((word >> offset) == 0)
619 + break;
620 +
621 + offset += ewah_bit_ctz64(word >> offset);
622 +
623 + if (pos + offset < bitmap_git.reuse_objects)
624 + continue;
625 +
626 + entry = &bitmap_git.reverse_index->revindex[pos + offset];
627 + sha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);
628 +
629 + show_reach(sha1, object_type, 0, hash, bitmap_git.pack, entry->offset);
630 + }
631 +
632 + pos += BITS_IN_WORD;
633 + i++;
634 + }
635 +}
636 +
637 +static int in_bitmapped_pack(struct object_list *roots)
638 +{
639 + while (roots) {
640 + struct object *object = roots->item;
641 + roots = roots->next;
642 +
643 + if (find_pack_entry_one(object->sha1, bitmap_git.pack) > 0)
644 + return 1;
645 + }
646 +
647 + return 0;
648 +}
649 +
650 +int prepare_bitmap_walk(struct rev_info *revs)
651 +{
652 + unsigned int i;
653 + unsigned int pending_nr = revs->pending.nr;
654 + struct object_array_entry *pending_e = revs->pending.objects;
655 +
656 + struct object_list *wants = NULL;
657 + struct object_list *haves = NULL;
658 +
659 + struct bitmap *wants_bitmap = NULL;
660 + struct bitmap *haves_bitmap = NULL;
661 +
662 + if (!bitmap_git.loaded) {
663 + /* try to open a bitmapped pack, but don't parse it yet
664 + * because we may not need to use it */
665 + if (open_pack_bitmap() < 0)
666 + return -1;
667 + }
668 +
669 + for (i = 0; i < pending_nr; ++i) {
670 + struct object *object = pending_e[i].item;
671 +
672 + if (object->type == OBJ_NONE)
673 + parse_object_or_die(object->sha1, NULL);
674 +
675 + while (object->type == OBJ_TAG) {
676 + struct tag *tag = (struct tag *) object;
677 +
678 + if (object->flags & UNINTERESTING)
679 + object_list_insert(object, &haves);
680 + else
681 + object_list_insert(object, &wants);
682 +
683 + if (!tag->tagged)
684 + die("bad tag");
685 + object = parse_object_or_die(tag->tagged->sha1, NULL);
686 + }
687 +
688 + if (object->flags & UNINTERESTING)
689 + object_list_insert(object, &haves);
690 + else
691 + object_list_insert(object, &wants);
692 + }
693 +
694 + /*
695 + * if we have a HAVES list, but none of those haves is contained
696 + * in the packfile that has a bitmap, we don't have anything to
697 + * optimize here
698 + */
699 + if (haves && !in_bitmapped_pack(haves))
700 + return -1;
701 +
702 + /* if we don't want anything, we're done here */
703 + if (!wants)
704 + return -1;
705 +
706 + /*
707 + * now we're going to use bitmaps, so load the actual bitmap entries
708 + * from disk. this is the point of no return; after this the rev_list
709 + * becomes invalidated and we must perform the revwalk through bitmaps
710 + */
711 + if (!bitmap_git.loaded && load_pack_bitmap() < 0)
712 + return -1;
713 +
714 + revs->pending.nr = 0;
715 + revs->pending.alloc = 0;
716 + revs->pending.objects = NULL;
717 +
718 + if (haves) {
719 + haves_bitmap = find_objects(revs, haves, NULL);
720 + reset_revision_walk();
721 +
722 + if (haves_bitmap == NULL)
723 + die("BUG: failed to perform bitmap walk");
724 + }
725 +
726 + wants_bitmap = find_objects(revs, wants, haves_bitmap);
727 +
728 + if (!wants_bitmap)
729 + die("BUG: failed to perform bitmap walk");
730 +
731 + if (haves_bitmap)
732 + bitmap_and_not(wants_bitmap, haves_bitmap);
733 +
734 + bitmap_git.result = wants_bitmap;
735 +
736 + bitmap_free(haves_bitmap);
737 + return 0;
738 +}
739 +
740 +int reuse_partial_packfile_from_bitmap(struct packed_git **packfile,
741 + uint32_t *entries,
742 + off_t *up_to)
743 +{
744 + /*
745 + * Reuse the packfile content if we need more than
746 + * 90% of its objects
747 + */
748 + static const double REUSE_PERCENT = 0.9;
749 +
750 + struct bitmap *result = bitmap_git.result;
751 + uint32_t reuse_threshold;
752 + uint32_t i, reuse_objects = 0;
753 +
754 + assert(result);
755 +
756 + for (i = 0; i < result->word_alloc; ++i) {
757 + if (result->words[i] != (eword_t)~0) {
758 + reuse_objects += ewah_bit_ctz64(~result->words[i]);
759 + break;
760 + }
761 +
762 + reuse_objects += BITS_IN_WORD;
763 + }
764 +
765 +#ifdef GIT_BITMAP_DEBUG
766 + {
767 + const unsigned char *sha1;
768 + struct revindex_entry *entry;
769 +
770 + entry = &bitmap_git.reverse_index->revindex[reuse_objects];
771 + sha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);
772 +
773 + fprintf(stderr, "Failed to reuse at %d (%016llx)\n",
774 + reuse_objects, result->words[i]);
775 + fprintf(stderr, " %s\n", sha1_to_hex(sha1));
776 + }
777 +#endif
778 +
779 + if (!reuse_objects)
780 + return -1;
781 +
782 + if (reuse_objects >= bitmap_git.pack->num_objects) {
783 + bitmap_git.reuse_objects = *entries = bitmap_git.pack->num_objects;
784 + *up_to = -1; /* reuse the full pack */
785 + *packfile = bitmap_git.pack;
786 + return 0;
787 + }
788 +
789 + reuse_threshold = bitmap_popcount(bitmap_git.result) * REUSE_PERCENT;
790 +
791 + if (reuse_objects < reuse_threshold)
792 + return -1;
793 +
794 + bitmap_git.reuse_objects = *entries = reuse_objects;
795 + *up_to = bitmap_git.reverse_index->revindex[reuse_objects].offset;
796 + *packfile = bitmap_git.pack;
797 +
798 + return 0;
799 +}
800 +
801 +void traverse_bitmap_commit_list(show_reachable_fn show_reachable)
802 +{
803 + assert(bitmap_git.result);
804 +
805 + show_objects_for_type(bitmap_git.result, bitmap_git.commits,
806 + OBJ_COMMIT, show_reachable);
807 + show_objects_for_type(bitmap_git.result, bitmap_git.trees,
808 + OBJ_TREE, show_reachable);
809 + show_objects_for_type(bitmap_git.result, bitmap_git.blobs,
810 + OBJ_BLOB, show_reachable);
811 + show_objects_for_type(bitmap_git.result, bitmap_git.tags,
812 + OBJ_TAG, show_reachable);
813 +
814 + show_extended_objects(bitmap_git.result, show_reachable);
815 +
816 + bitmap_free(bitmap_git.result);
817 + bitmap_git.result = NULL;
818 +}
819 +
820 +static uint32_t count_object_type(struct bitmap *objects,
821 + enum object_type type)
822 +{
823 + struct eindex *eindex = &bitmap_git.ext_index;
824 +
825 + uint32_t i = 0, count = 0;
826 + struct ewah_iterator it;
827 + eword_t filter;
828 +
829 + switch (type) {
830 + case OBJ_COMMIT:
831 + ewah_iterator_init(&it, bitmap_git.commits);
832 + break;
833 +
834 + case OBJ_TREE:
835 + ewah_iterator_init(&it, bitmap_git.trees);
836 + break;
837 +
838 + case OBJ_BLOB:
839 + ewah_iterator_init(&it, bitmap_git.blobs);
840 + break;
841 +
842 + case OBJ_TAG:
843 + ewah_iterator_init(&it, bitmap_git.tags);
844 + break;
845 +
846 + default:
847 + return 0;
848 + }
849 +
850 + while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
851 + eword_t word = objects->words[i++] & filter;
852 + count += ewah_bit_popcount64(word);
853 + }
854 +
855 + for (i = 0; i < eindex->count; ++i) {
856 + if (eindex->objects[i]->type == type &&
857 + bitmap_get(objects, bitmap_git.pack->num_objects + i))
858 + count++;
859 + }
860 +
861 + return count;
862 +}
863 +
864 +void count_bitmap_commit_list(uint32_t *commits, uint32_t *trees,
865 + uint32_t *blobs, uint32_t *tags)
866 +{
867 + assert(bitmap_git.result);
868 +
869 + if (commits)
870 + *commits = count_object_type(bitmap_git.result, OBJ_COMMIT);
871 +
872 + if (trees)
873 + *trees = count_object_type(bitmap_git.result, OBJ_TREE);
874 +
875 + if (blobs)
876 + *blobs = count_object_type(bitmap_git.result, OBJ_BLOB);
877 +
878 + if (tags)
879 + *tags = count_object_type(bitmap_git.result, OBJ_TAG);
880 +}
881 +
882 +struct bitmap_test_data {
883 + struct bitmap *base;
884 + struct progress *prg;
885 + size_t seen;
886 +};
887 +
888 +static void test_show_object(struct object *object,
889 + const struct name_path *path,
890 + const char *last, void *data)
891 +{
892 + struct bitmap_test_data *tdata = data;
893 + int bitmap_pos;
894 +
895 + bitmap_pos = bitmap_position(object->sha1);
896 + if (bitmap_pos < 0)
897 + die("Object not in bitmap: %s\n", sha1_to_hex(object->sha1));
898 +
899 + bitmap_set(tdata->base, bitmap_pos);
900 + display_progress(tdata->prg, ++tdata->seen);
901 +}
902 +
903 +static void test_show_commit(struct commit *commit, void *data)
904 +{
905 + struct bitmap_test_data *tdata = data;
906 + int bitmap_pos;
907 +
908 + bitmap_pos = bitmap_position(commit->object.sha1);
909 + if (bitmap_pos < 0)
910 + die("Object not in bitmap: %s\n", sha1_to_hex(commit->object.sha1));
911 +
912 + bitmap_set(tdata->base, bitmap_pos);
913 + display_progress(tdata->prg, ++tdata->seen);
914 +}
915 +
916 +void test_bitmap_walk(struct rev_info *revs)
917 +{
918 + struct object *root;
919 + struct bitmap *result = NULL;
920 + khiter_t pos;
921 + size_t result_popcnt;
922 + struct bitmap_test_data tdata;
923 +
924 + if (prepare_bitmap_git())
925 + die("failed to load bitmap indexes");
926 +
927 + if (revs->pending.nr != 1)
928 + die("you must specify exactly one commit to test");
929 +
930 + fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
931 + bitmap_git.version, bitmap_git.entry_count);
932 +
933 + root = revs->pending.objects[0].item;
934 + pos = kh_get_sha1(bitmap_git.bitmaps, root->sha1);
935 +
936 + if (pos < kh_end(bitmap_git.bitmaps)) {
937 + struct stored_bitmap *st = kh_value(bitmap_git.bitmaps, pos);
938 + struct ewah_bitmap *bm = lookup_stored_bitmap(st);
939 +
940 + fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
941 + sha1_to_hex(root->sha1), (int)bm->bit_size, ewah_checksum(bm));
942 +
943 + result = ewah_to_bitmap(bm);
944 + }
945 +
946 + if (result == NULL)
947 + die("Commit %s doesn't have an indexed bitmap", sha1_to_hex(root->sha1));
948 +
949 + revs->tag_objects = 1;
950 + revs->tree_objects = 1;
951 + revs->blob_objects = 1;
952 +
953 + result_popcnt = bitmap_popcount(result);
954 +
955 + if (prepare_revision_walk(revs))
956 + die("revision walk setup failed");
957 +
958 + tdata.base = bitmap_new();
959 + tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
960 + tdata.seen = 0;
961 +
962 + traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
963 +
964 + stop_progress(&tdata.prg);
965 +
966 + if (bitmap_equals(result, tdata.base))
967 + fprintf(stderr, "OK!\n");
968 + else
969 + fprintf(stderr, "Mismatch!\n");
970 +}
pack-bitmap.h new
+43
@@ -0,0 +1,43 @@
1 +#ifndef PACK_BITMAP_H
2 +#define PACK_BITMAP_H
3 +
4 +#include "ewah/ewok.h"
5 +#include "khash.h"
6 +
7 +struct bitmap_disk_entry {
8 + uint32_t object_pos;
9 + uint8_t xor_offset;
10 + uint8_t flags;
11 +} __attribute__((packed));
12 +
13 +struct bitmap_disk_header {
14 + char magic[4];
15 + uint16_t version;
16 + uint16_t options;
17 + uint32_t entry_count;
18 + unsigned char checksum[20];
19 +};
20 +
21 +static const char BITMAP_IDX_SIGNATURE[] = {'B', 'I', 'T', 'M'};
22 +
23 +enum pack_bitmap_opts {
24 + BITMAP_OPT_FULL_DAG = 1
25 +};
26 +
27 +typedef int (*show_reachable_fn)(
28 + const unsigned char *sha1,
29 + enum object_type type,
30 + int flags,
31 + uint32_t hash,
32 + struct packed_git *found_pack,
33 + off_t found_offset);
34 +
35 +int prepare_bitmap_git(void);
36 +void count_bitmap_commit_list(uint32_t *commits, uint32_t *trees, uint32_t *blobs, uint32_t *tags);
37 +void traverse_bitmap_commit_list(show_reachable_fn show_reachable);
38 +void test_bitmap_walk(struct rev_info *revs);
39 +char *pack_bitmap_filename(struct packed_git *p);
40 +int prepare_bitmap_walk(struct rev_info *revs);
41 +int reuse_partial_packfile_from_bitmap(struct packed_git **packfile, uint32_t *entries, off_t *up_to);
42 +
43 +#endif