pack-objects: merge read_lock and lock in packing_data struct
[git] / pack-objects.h
1 #ifndef PACK_OBJECTS_H
2 #define PACK_OBJECTS_H
3
4 #include "object-store.h"
5 #include "thread-utils.h"
6 #include "pack.h"
7
8 #define DEFAULT_DELTA_CACHE_SIZE (256 * 1024 * 1024)
9
10 #define OE_DFS_STATE_BITS       2
11 #define OE_DEPTH_BITS           12
12 #define OE_IN_PACK_BITS         10
13 #define OE_Z_DELTA_BITS         20
14 /*
15  * Note that oe_set_size() becomes expensive when the given size is
16  * above this limit. Don't lower it too much.
17  */
18 #define OE_SIZE_BITS            31
19 #define OE_DELTA_SIZE_BITS      23
20
21 /*
22  * State flags for depth-first search used for analyzing delta cycles.
23  *
24  * The depth is measured in delta-links to the base (so if A is a delta
25  * against B, then A has a depth of 1, and B a depth of 0).
26  */
27 enum dfs_state {
28         DFS_NONE = 0,
29         DFS_ACTIVE,
30         DFS_DONE,
31         DFS_NUM_STATES
32 };
33
34 /*
35  * The size of struct nearly determines pack-objects's memory
36  * consumption. This struct is packed tight for that reason. When you
37  * add or reorder something in this struct, think a bit about this.
38  *
39  * basic object info
40  * -----------------
41  * idx.oid is filled up before delta searching starts. idx.crc32 is
42  * only valid after the object is written out and will be used for
43  * generating the index. idx.offset will be both gradually set and
44  * used in writing phase (base objects get offset first, then deltas
45  * refer to them)
46  *
47  * "size" is the uncompressed object size. Compressed size of the raw
48  * data for an object in a pack is not stored anywhere but is computed
49  * and made available when reverse .idx is made. Note that when a
50  * delta is reused, "size" is the uncompressed _delta_ size, not the
51  * canonical one after the delta has been applied.
52  *
53  * "hash" contains a path name hash which is used for sorting the
54  * delta list and also during delta searching. Once prepare_pack()
55  * returns it's no longer needed.
56  *
57  * source pack info
58  * ----------------
59  * The (in_pack, in_pack_offset) tuple contains the location of the
60  * object in the source pack. in_pack_header_size allows quickly
61  * skipping the header and going straight to the zlib stream.
62  *
63  * "type" and "in_pack_type" both describe object type. in_pack_type
64  * may contain a delta type, while type is always the canonical type.
65  *
66  * deltas
67  * ------
68  * Delta links (delta, delta_child and delta_sibling) are created to
69  * reflect that delta graph from the source pack then updated or added
70  * during delta searching phase when we find better deltas.
71  *
72  * delta_child and delta_sibling are last needed in
73  * compute_write_order(). "delta" and "delta_size" must remain valid
74  * at object writing phase in case the delta is not cached.
75  *
76  * If a delta is cached in memory and is compressed, delta_data points
77  * to the data and z_delta_size contains the compressed size. If it's
78  * uncompressed [1], z_delta_size must be zero. delta_size is always
79  * the uncompressed size and must be valid even if the delta is not
80  * cached.
81  *
82  * [1] during try_delta phase we don't bother with compressing because
83  * the delta could be quickly replaced with a better one.
84  */
85 struct object_entry {
86         struct pack_idx_entry idx;
87         void *delta_data;       /* cached delta (uncompressed) */
88         off_t in_pack_offset;
89         uint32_t hash;                  /* name hint hash */
90         unsigned size_:OE_SIZE_BITS;
91         unsigned size_valid:1;
92         uint32_t delta_idx;     /* delta base object */
93         uint32_t delta_child_idx; /* deltified objects who bases me */
94         uint32_t delta_sibling_idx; /* other deltified objects who
95                                      * uses the same base as me
96                                      */
97         unsigned delta_size_:OE_DELTA_SIZE_BITS; /* delta data size (uncompressed) */
98         unsigned delta_size_valid:1;
99         unsigned char in_pack_header_size;
100         unsigned in_pack_idx:OE_IN_PACK_BITS;   /* already in pack */
101         unsigned z_delta_size:OE_Z_DELTA_BITS;
102         unsigned type_valid:1;
103         unsigned no_try_delta:1;
104         unsigned type_:TYPE_BITS;
105         unsigned in_pack_type:TYPE_BITS; /* could be delta */
106
107         unsigned preferred_base:1; /*
108                                     * we do not pack this, but is available
109                                     * to be used as the base object to delta
110                                     * objects against.
111                                     */
112         unsigned tagged:1; /* near the very tip of refs */
113         unsigned filled:1; /* assigned write-order */
114         unsigned dfs_state:OE_DFS_STATE_BITS;
115         unsigned depth:OE_DEPTH_BITS;
116         unsigned ext_base:1; /* delta_idx points outside packlist */
117
118         /*
119          * pahole results on 64-bit linux (gcc and clang)
120          *
121          *   size: 80, bit_padding: 9 bits
122          *
123          * and on 32-bit (gcc)
124          *
125          *   size: 76, bit_padding: 9 bits
126          */
127 };
128
129 struct packing_data {
130         struct object_entry *objects;
131         uint32_t nr_objects, nr_alloc;
132
133         int32_t *index;
134         uint32_t index_size;
135
136         unsigned int *in_pack_pos;
137         unsigned long *delta_size;
138
139         /*
140          * Only one of these can be non-NULL and they have different
141          * sizes. if in_pack_by_idx is allocated, oe_in_pack() returns
142          * the pack of an object using in_pack_idx field. If not,
143          * in_pack[] array is used the same way as in_pack_pos[]
144          */
145         struct packed_git **in_pack_by_idx;
146         struct packed_git **in_pack;
147
148         /*
149          * During packing with multiple threads, protect the in-core
150          * object database from concurrent accesses.
151          */
152         pthread_mutex_t odb_lock;
153
154         /*
155          * This list contains entries for bases which we know the other side
156          * has (e.g., via reachability bitmaps), but which aren't in our
157          * "objects" list.
158          */
159         struct object_entry *ext_bases;
160         uint32_t nr_ext, alloc_ext;
161
162         uintmax_t oe_size_limit;
163         uintmax_t oe_delta_size_limit;
164
165         /* delta islands */
166         unsigned int *tree_depth;
167         unsigned char *layer;
168 };
169
170 void prepare_packing_data(struct packing_data *pdata);
171
172 /* Protect access to object database */
173 static inline void packing_data_lock(struct packing_data *pdata)
174 {
175         pthread_mutex_lock(&pdata->odb_lock);
176 }
177 static inline void packing_data_unlock(struct packing_data *pdata)
178 {
179         pthread_mutex_unlock(&pdata->odb_lock);
180 }
181
182 struct object_entry *packlist_alloc(struct packing_data *pdata,
183                                     const unsigned char *sha1,
184                                     uint32_t index_pos);
185
186 struct object_entry *packlist_find(struct packing_data *pdata,
187                                    const unsigned char *sha1,
188                                    uint32_t *index_pos);
189
190 static inline uint32_t pack_name_hash(const char *name)
191 {
192         uint32_t c, hash = 0;
193
194         if (!name)
195                 return 0;
196
197         /*
198          * This effectively just creates a sortable number from the
199          * last sixteen non-whitespace characters. Last characters
200          * count "most", so things that end in ".c" sort together.
201          */
202         while ((c = *name++) != 0) {
203                 if (isspace(c))
204                         continue;
205                 hash = (hash >> 2) + (c << 24);
206         }
207         return hash;
208 }
209
210 static inline enum object_type oe_type(const struct object_entry *e)
211 {
212         return e->type_valid ? e->type_ : OBJ_BAD;
213 }
214
215 static inline void oe_set_type(struct object_entry *e,
216                                enum object_type type)
217 {
218         if (type >= OBJ_ANY)
219                 BUG("OBJ_ANY cannot be set in pack-objects code");
220
221         e->type_valid = type >= OBJ_NONE;
222         e->type_ = (unsigned)type;
223 }
224
225 static inline unsigned int oe_in_pack_pos(const struct packing_data *pack,
226                                           const struct object_entry *e)
227 {
228         return pack->in_pack_pos[e - pack->objects];
229 }
230
231 static inline void oe_set_in_pack_pos(const struct packing_data *pack,
232                                       const struct object_entry *e,
233                                       unsigned int pos)
234 {
235         pack->in_pack_pos[e - pack->objects] = pos;
236 }
237
238 static inline struct packed_git *oe_in_pack(const struct packing_data *pack,
239                                             const struct object_entry *e)
240 {
241         if (pack->in_pack_by_idx)
242                 return pack->in_pack_by_idx[e->in_pack_idx];
243         else
244                 return pack->in_pack[e - pack->objects];
245 }
246
247 void oe_map_new_pack(struct packing_data *pack,
248                      struct packed_git *p);
249 static inline void oe_set_in_pack(struct packing_data *pack,
250                                   struct object_entry *e,
251                                   struct packed_git *p)
252 {
253         if (!p->index)
254                 oe_map_new_pack(pack, p);
255         if (pack->in_pack_by_idx)
256                 e->in_pack_idx = p->index;
257         else
258                 pack->in_pack[e - pack->objects] = p;
259 }
260
261 static inline struct object_entry *oe_delta(
262                 const struct packing_data *pack,
263                 const struct object_entry *e)
264 {
265         if (!e->delta_idx)
266                 return NULL;
267         if (e->ext_base)
268                 return &pack->ext_bases[e->delta_idx - 1];
269         else
270                 return &pack->objects[e->delta_idx - 1];
271 }
272
273 static inline void oe_set_delta(struct packing_data *pack,
274                                 struct object_entry *e,
275                                 struct object_entry *delta)
276 {
277         if (delta)
278                 e->delta_idx = (delta - pack->objects) + 1;
279         else
280                 e->delta_idx = 0;
281 }
282
283 void oe_set_delta_ext(struct packing_data *pack,
284                       struct object_entry *e,
285                       const unsigned char *sha1);
286
287 static inline struct object_entry *oe_delta_child(
288                 const struct packing_data *pack,
289                 const struct object_entry *e)
290 {
291         if (e->delta_child_idx)
292                 return &pack->objects[e->delta_child_idx - 1];
293         return NULL;
294 }
295
296 static inline void oe_set_delta_child(struct packing_data *pack,
297                                       struct object_entry *e,
298                                       struct object_entry *delta)
299 {
300         if (delta)
301                 e->delta_child_idx = (delta - pack->objects) + 1;
302         else
303                 e->delta_child_idx = 0;
304 }
305
306 static inline struct object_entry *oe_delta_sibling(
307                 const struct packing_data *pack,
308                 const struct object_entry *e)
309 {
310         if (e->delta_sibling_idx)
311                 return &pack->objects[e->delta_sibling_idx - 1];
312         return NULL;
313 }
314
315 static inline void oe_set_delta_sibling(struct packing_data *pack,
316                                         struct object_entry *e,
317                                         struct object_entry *delta)
318 {
319         if (delta)
320                 e->delta_sibling_idx = (delta - pack->objects) + 1;
321         else
322                 e->delta_sibling_idx = 0;
323 }
324
325 unsigned long oe_get_size_slow(struct packing_data *pack,
326                                const struct object_entry *e);
327 static inline unsigned long oe_size(struct packing_data *pack,
328                                     const struct object_entry *e)
329 {
330         if (e->size_valid)
331                 return e->size_;
332
333         return oe_get_size_slow(pack, e);
334 }
335
336 static inline int oe_size_less_than(struct packing_data *pack,
337                                     const struct object_entry *lhs,
338                                     unsigned long rhs)
339 {
340         if (lhs->size_valid)
341                 return lhs->size_ < rhs;
342         if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
343                 return 0;
344         return oe_get_size_slow(pack, lhs) < rhs;
345 }
346
347 static inline int oe_size_greater_than(struct packing_data *pack,
348                                        const struct object_entry *lhs,
349                                        unsigned long rhs)
350 {
351         if (lhs->size_valid)
352                 return lhs->size_ > rhs;
353         if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
354                 return 1;
355         return oe_get_size_slow(pack, lhs) > rhs;
356 }
357
358 static inline void oe_set_size(struct packing_data *pack,
359                                struct object_entry *e,
360                                unsigned long size)
361 {
362         if (size < pack->oe_size_limit) {
363                 e->size_ = size;
364                 e->size_valid = 1;
365         } else {
366                 e->size_valid = 0;
367                 if (oe_get_size_slow(pack, e) != size)
368                         BUG("'size' is supposed to be the object size!");
369         }
370 }
371
372 static inline unsigned long oe_delta_size(struct packing_data *pack,
373                                           const struct object_entry *e)
374 {
375         if (e->delta_size_valid)
376                 return e->delta_size_;
377
378         /*
379          * pack->delta_size[] can't be NULL because oe_set_delta_size()
380          * must have been called when a new delta is saved with
381          * oe_set_delta().
382          * If oe_delta() returns NULL (i.e. default state, which means
383          * delta_size_valid is also false), then the caller must never
384          * call oe_delta_size().
385          */
386         return pack->delta_size[e - pack->objects];
387 }
388
389 static inline void oe_set_delta_size(struct packing_data *pack,
390                                      struct object_entry *e,
391                                      unsigned long size)
392 {
393         if (size < pack->oe_delta_size_limit) {
394                 e->delta_size_ = size;
395                 e->delta_size_valid = 1;
396         } else {
397                 packing_data_lock(pack);
398                 if (!pack->delta_size)
399                         ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
400                 packing_data_unlock(pack);
401
402                 pack->delta_size[e - pack->objects] = size;
403                 e->delta_size_valid = 0;
404         }
405 }
406
407 static inline unsigned int oe_tree_depth(struct packing_data *pack,
408                                          struct object_entry *e)
409 {
410         if (!pack->tree_depth)
411                 return 0;
412         return pack->tree_depth[e - pack->objects];
413 }
414
415 static inline void oe_set_tree_depth(struct packing_data *pack,
416                                      struct object_entry *e,
417                                      unsigned int tree_depth)
418 {
419         if (!pack->tree_depth)
420                 CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
421         pack->tree_depth[e - pack->objects] = tree_depth;
422 }
423
424 static inline unsigned char oe_layer(struct packing_data *pack,
425                                      struct object_entry *e)
426 {
427         if (!pack->layer)
428                 return 0;
429         return pack->layer[e - pack->objects];
430 }
431
432 static inline void oe_set_layer(struct packing_data *pack,
433                                 struct object_entry *e,
434                                 unsigned char layer)
435 {
436         if (!pack->layer)
437                 CALLOC_ARRAY(pack->layer, pack->nr_alloc);
438         pack->layer[e - pack->objects] = layer;
439 }
440
441 #endif