Skip to content

Commit

Permalink
pack-objects: output debug info about deltas
Browse files Browse the repository at this point in the history
In order to debug what is going on during delta calculations, add a
--debug-file=<file> option to 'git pack-objects'. This leads to sending
a JSON-formatted description of the delta information to that file.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
  • Loading branch information
derrickstolee committed Sep 9, 2024
1 parent 5dcb20a commit 460feef
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions builtin/pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
*/
static struct packing_data to_pack;

static FILE *delta_file;
static int delta_file_nr;

static inline struct object_entry *oe_delta(
const struct packing_data *pack,
const struct object_entry *e)
Expand Down Expand Up @@ -516,6 +519,14 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
hdrlen = encode_in_pack_object_header(header, sizeof(header),
type, size);

if (delta_file) {
if (delta_file_nr++)
fprintf(delta_file, ",\n");
fprintf(delta_file, "\t\t{\n");
fprintf(delta_file, "\t\t\t\"oid\" : \"%s\",\n", oid_to_hex(&entry->idx.oid));
fprintf(delta_file, "\t\t\t\"size\" : %"PRIuMAX",\n", datalen);

Check failure on line 527 in builtin/pack-objects.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

builtin/pack-objects.c:527:23: format '%llu' expects argument of type 'long long unsigned int', but argument 3 has type 'long unsigned int' [-Werror=format=]
}

if (type == OBJ_OFS_DELTA) {
/*
* Deltas with relative base contain an additional
Expand All @@ -536,6 +547,11 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
hashwrite(f, header, hdrlen);
hashwrite(f, dheader + pos, sizeof(dheader) - pos);
hdrlen += sizeof(dheader) - pos;
if (delta_file) {
fprintf(delta_file, "\t\t\t\"delta_type\" : \"OFS\",\n");
fprintf(delta_file, "\t\t\t\"offset\" : %"PRIuMAX",\n", ofs);
fprintf(delta_file, "\t\t\t\"delta_base\" : \"%s\",\n", oid_to_hex(&DELTA(entry)->idx.oid));
}
} else if (type == OBJ_REF_DELTA) {
/*
* Deltas with a base reference contain
Expand All @@ -550,6 +566,10 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
hashwrite(f, header, hdrlen);
hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
hdrlen += hashsz;
if (delta_file) {
fprintf(delta_file, "\t\t\t\"delta_type\" : \"REF\",\n");
fprintf(delta_file, "\t\t\t\"delta_base\" : \"%s\",\n", oid_to_hex(&DELTA(entry)->idx.oid));
}
} else {
if (limit && hdrlen + datalen + hashsz >= limit) {
if (st)
Expand All @@ -559,6 +579,10 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
}
hashwrite(f, header, hdrlen);
}

if (delta_file)
fprintf(delta_file, "\t\t\t\"reused\" : false\n\t\t}");

if (st) {
datalen = write_large_blob_data(st, f, &entry->idx.oid);
close_istream(st);
Expand Down Expand Up @@ -619,6 +643,14 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
return write_no_reuse_object(f, entry, limit, usable_delta);
}

if (delta_file) {
if (delta_file_nr++)
fprintf(delta_file, ",\n");
fprintf(delta_file, "\t\t{\n");
fprintf(delta_file, "\t\t\t\"oid\" : \"%s\",\n", oid_to_hex(&entry->idx.oid));
fprintf(delta_file, "\t\t\t\"size\" : %"PRIuMAX",\n", entry_size);

Check failure on line 651 in builtin/pack-objects.c

View workflow job for this annotation

GitHub Actions / linux32 (daald/ubuntu32:xenial)

builtin/pack-objects.c:651:23: format '%llu' expects argument of type 'long long unsigned int', but argument 3 has type 'long unsigned int' [-Werror=format=]
}

if (type == OBJ_OFS_DELTA) {
off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
unsigned pos = sizeof(dheader) - 1;
Expand All @@ -633,6 +665,12 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
hashwrite(f, dheader + pos, sizeof(dheader) - pos);
hdrlen += sizeof(dheader) - pos;
reused_delta++;

if (delta_file) {
fprintf(delta_file, "\t\t\t\"delta_type\" : \"OFS\",\n");
fprintf(delta_file, "\t\t\t\"offset\" : %"PRIuMAX",\n", ofs);
fprintf(delta_file, "\t\t\t\"delta_base\" : \"%s\",\n", oid_to_hex(&DELTA(entry)->idx.oid));
}
} else if (type == OBJ_REF_DELTA) {
if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
unuse_pack(&w_curs);
Expand All @@ -642,6 +680,10 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
hdrlen += hashsz;
reused_delta++;
if (delta_file) {
fprintf(delta_file, "\t\t\t\"delta_type\" : \"REF\",\n");
fprintf(delta_file, "\t\t\t\"delta_base\" : \"%s\",\n", oid_to_hex(&DELTA(entry)->idx.oid));
}
} else {
if (limit && hdrlen + datalen + hashsz >= limit) {
unuse_pack(&w_curs);
Expand All @@ -652,6 +694,10 @@ static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
copy_pack_data(f, p, &w_curs, offset, datalen);
unuse_pack(&w_curs);
reused++;

if (delta_file)
fprintf(delta_file, "\t\t\t\"reused\" : true\n\t\t}");

return hdrlen + datalen;
}

Expand Down Expand Up @@ -1264,6 +1310,11 @@ static void write_pack_file(void)
ALLOC_ARRAY(written_list, to_pack.nr_objects);
write_order = compute_write_order();

if (delta_file) {
fprintf(delta_file, "{\n\t\"num_objects\" : %"PRIu32",\n", to_pack.nr_objects);
fprintf(delta_file, "\t\"objects\" : [\n");
}

do {
unsigned char hash[GIT_MAX_RAWSZ];
char *pack_tmp_name = NULL;
Expand Down Expand Up @@ -1412,6 +1463,9 @@ static void write_pack_file(void)
written, nr_result);
trace2_data_intmax("pack-objects", the_repository,
"write_pack_file/wrote", nr_result);

if (delta_file)
fprintf(delta_file, "\n\t]\n}");
}

static int no_try_delta(const char *path)
Expand Down Expand Up @@ -4430,6 +4484,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
struct list_objects_filter_options filter_options =
LIST_OBJECTS_FILTER_INIT;
const char *delta_file_name = NULL;

struct option pack_objects_options[] = {
OPT_CALLBACK_F('q', "quiet", &progress, NULL,
Expand Down Expand Up @@ -4536,6 +4591,9 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
OPT_BOOL(0, "full-name-hash", &use_full_name_hash,
N_("optimize delta compression across identical path names over time")),
OPT_STRING(0, "delta-file", &delta_file_name,
N_("filename"),
N_("output delta compression details to the given file")),
OPT_END(),
};

Expand Down Expand Up @@ -4573,6 +4631,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
if (pack_to_stdout != !base_name || argc)
usage_with_options(pack_usage, pack_objects_options);

if (delta_file_name) {
delta_file = fopen(delta_file_name, "w");
if (!delta_file)
die_errno("failed to open '%s'", delta_file_name);
trace2_printf("opened '%s' for writing deltas", delta_file_name);
}
if (depth < 0)
depth = 0;
if (depth >= (1 << OE_DEPTH_BITS)) {
Expand Down Expand Up @@ -4796,5 +4860,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
list_objects_filter_release(&filter_options);
strvec_clear(&rp);

if (delta_file) {
fflush(delta_file);
fclose(delta_file);
}

return 0;
}

0 comments on commit 460feef

Please sign in to comment.