[PATCH v2 0/7] packfile: some lifecycle improvements
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Dec 2, 2026, 13:19 UTC
- Message-ID
- <20261202-pks-packfile-global-statistics-v2-0-bb8be4aa552d@pks.im>
- In-Reply-To
- <20261120-pks-packfile-global-statistics-v1-0-19beb83b145f@pks.im>
Hi,
this small patch series polishes our lifecycle handling of packfiles a bit. The original motivation of this patch series is really only the last patch, which fixes how we handle process-wide limitations. But I took the chance to also polish this subsystem a bit, as a lot of its parts feel rather dated nowadays.
There's a minor semantic merge conflict with "seen" that can be sovled like this:
diff --git a/odb/source-packed.c b/odb/source-packed.c index 77f8227ae52..e24f6cc7ef9 100644 --- a/odb/source-packed.c +++ b/odb/source-packed.c @@ -898,7 +898,7 @@ static int odb_source_packed_map_oid(struct odb_source *source, &hash_algos[src->algo] : repo->hash_algo; - if (open_pack_index(p) || !p->num_objects) + if (packfile_open_index(p) || !p->num_objects) continue; /* Only v3 indices store object IDs in both algorithms. */ if (p->index_version < 3) Changes in v2: - The usual commit message tweaks - Drop a useless forward declaration. - Add another patch that fixes accounting of open file descriptors when fstat(3p) fails. - Make `packfile_free()` NULL-safe. - Rename `unuse_one_window()` and `close_one_pack()` to `unuse_lru_window()` and `close_lru_pack()`. - Link to v1: https://patch.msgid.link/20261120-pks-packfile-global-statistics-v1-0-19beb83b145f@pks.im Thanks! Patrick --- Patrick Steinhardt (7): builtin/fast-import: stop reporting packfile statistics packfile: rename lifecycle functions packfile: inline `open_packed_git_1()` packfile: fix open file descriptor counter when fstat(3p) fails packfile: introduce function to free a packfile packfile: maintain process-wide limits globally packfile: skip closing packs marked as do-not-close Documentation/git-fast-import.adoc | 5 +- builtin/count-objects.c | 2 +- builtin/fast-import.c | 10 +- builtin/index-pack.c | 16 +-- builtin/pack-objects.c | 2 +- builtin/pack-redundant.c | 2 +- commit-graph.c | 12 +- connected.c | 9 +- http-push.c | 2 +- http-walker.c | 8 +- http.c | 4 +- midx-write.c | 22 ++-- midx.c | 6 +- odb/source-packed.c | 14 +-- pack-check.c | 2 +- pack-mtimes.c | 2 +- pack-revindex.c | 4 +- packfile.c | 240 ++++++++++++++++++------------------- packfile.h | 28 ++--- repack-geometry.c | 2 +- repack-midx.c | 2 +- t/helper/test-bitmap.c | 2 +- 22 files changed, 186 insertions(+), 210 deletions(-) Range-diff versus v1: 1: 6a5412030f2 ! 1: 7602ff8a1d4 builtin/fast-import: stop reporting packfile statistics @@ Commit message objects have been allocated, the number of refs processed, or the amount of memory that was allocated. - Reporting this kind information is highly questionable, doubly so as it - is done by default, as a user really shoudln't have to worry about such - internal implementation details. For some of the above metrics it could - be argued though that it's indeed interesting to learn about the number - of objects processed to get a rough sense of what git-fast-import(1) has - done. But for other kind of information that's not the case, as it is - without a doubt purely internal implementation details. + Reporting this kind of information is highly questionable, doubly so as + it is done by default, as a user really shouldn't have to worry about + such internal implementation details. For some of the above metrics it + could be argued though that it's indeed interesting to learn about the + number of objects processed to get a sense of what git-fast-import(1) + has done. But for other kinds of information that's not the case, as it + is without a doubt purely internal implementation details. One of these reported implementation details is our packfile statistics. - These include information on the system's page size, the number of packs - that were opened, the number of calls to mmap(3) and how many pack - windows were created. Reporting that kind of information, and that even - by default, is just completely uncalled for. This information may be - useful to a developer, but a user really shouldn't have to care about - any of this. + These include information like for example the system's page size, the + number of open pack windows or the number of calls to mmap(3). Reporting + that kind of information, and that even by default, is just completely + uncalled for. This information may be useful to a developer, but a user + really shouldn't have to care about any of this. More importantly though, with pluggable object databases we shouldn't assume that we're using packfiles in the first place anymore. The @@ Documentation/git-fast-import.adoc: OPTIONS - created, the packfiles they were stored into, and the - memory used by fast-import during this run. Showing this output - is currently the default, but can be disabled with --quiet. -+ created, and the memory used by fast-import during this run. Showing ++ created and the memory used by fast-import during this run. Showing + this output is currently the default, but can be disabled with --quiet. --allow-unsafe-features:: @@ builtin/fast-import.c: static int option_parse_early_allow_unsafe(const struct o { struct fast_import_state state; +@@ builtin/fast-import.c: int cmd_fast_import(int argc, + N_("format of the commit/tag dates"), + PARSE_OPT_NONEG, option_parse_date_format), + OPT_BOOL_F(0, "stats", &show_stats, +- N_("display some basic statistics (objects, packfiles and memory)"), ++ N_("display some basic statistics (objects and memory)"), + PARSE_OPT_NONEG), + OPT_CALLBACK_F(0, "quiet", NULL, NULL, + N_("disable the output shown by --stats"), @@ builtin/fast-import.c: int cmd_fast_import(int argc, fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024)); fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024); 2: 08c545da5ff ! 2: 4cb666102f6 packfile: rename lifecycle functions @@ Commit message The packfile subsystem is kind of the wild west when it comes to function naming. None of the functions have proper subsystem prefixes - and the naming of `struct packed_git` feels like a relict from the past. + and the naming of `struct packed_git` feels like a relic from the past. Ideally we'd modernize all of that, but that would lead to a huge amount of churn. Instead, let's improve the subsystem one step at a time and @@ packfile.h: int for_each_object_in_pack(struct packed_git *p, +uint32_t get_pack_fanout(struct packed_git *p, uint32_t value); + -+struct object_database; -+ +struct packed_git *packfile_from_idx(struct repository *r, const char *path, + size_t path_len, int local); + 3: 906522ae361 ! 3: 4bdddd04d4f packfile: inline `open_packed_git_1()` @@ Commit message again. Fix this design smell by introducing a common exit path that knows to - close the file descriptor on error. This allows us to inline the call to - `open_packed_git_1()`. + close the file descriptor on error. This allows us to merge the `_1()` + function into `open_packed_git()`. While at it, rename the function to `packfile_open_pack()` to match modern best practices. Besides that, it also gives the caller a better -: ----------- > 4: 9b051f945e6 packfile: fix open file descriptor counter when fstat(3p) fails 4: 759ee1e33f5 ! 5: 8fc7c6ffd87 packfile: introduce function to free a packfile @@ connected.c: int check_connected(struct repository *repo, - packfile_close(new_pack); - free(new_pack); - } -+ if (new_pack) -+ packfile_free(new_pack); ++ packfile_free(new_pack); return finish_command(&rev_list) || err; } @@ midx-write.c: static int write_midx_internal(struct write_midx_opts *opts) - packfile_close(ctx.info[i].p); - free(ctx.info[i].p); - } -+ if (ctx.info[i].p) -+ packfile_free(ctx.info[i].p); ++ packfile_free(ctx.info[i].p); free(ctx.info[i].pack_name); } @@ packfile.c: struct packed_git *packfile_from_idx(struct repository *r, const cha +void packfile_free(struct packed_git *p) +{ ++ if (!p) ++ return; + packfile_close(p); + free(p); +} @@ packfile.c: struct packed_git *packfile_from_idx(struct repository *r, const cha { ## packfile.h ## -@@ packfile.h: struct object_database; +@@ packfile.h: uint32_t get_pack_fanout(struct packed_git *p, uint32_t value); struct packed_git *packfile_from_idx(struct repository *r, const char *path, size_t path_len, int local); 5: 4e510170a22 ! 6: 3e0f87b14ef packfile: maintain process-wide limits globally @@ Commit message packfiles. When we exceed that limit, we close least-recently-used packs until we're below it again. - - "core.packedGitLimit" is the maximum number of packfile data that + - "core.packedGitLimit" is the maximum number of packfile bytes that may be mapped into memory. When we exceed that limit, we close the least-recently-used windows into our packfiles. @@ Commit message agnostic of the actual object database backend that's in use. Use it to maintain the above two limits. + While at it, rename the functions that close the least-recently-used + window and packfile to better indicate what they do. + Signed-off-by: Patrick Steinhardt <ps@pks.im> ## packfile.c ## @@ packfile.c: static void scan_windows(struct packed_git *p, } -static int unuse_one_window(struct object_database *odb) -+static int unuse_one_window(void) ++static int unuse_lru_window(void) { - struct odb_source_files *files = odb_source_files_downcast(odb->source); struct packfile_list_entry *e; @@ packfile.c: static void find_lru_pack(struct packed_git *p, struct packed_git ** } -static int close_one_pack(struct repository *r) -+static int close_one_pack(void) ++static int close_lru_pack(void) { - struct odb_source_files *files = odb_source_files_downcast(r->objects->source); struct packfile_list_entry *e; @@ packfile.c: static int packfile_open_pack(struct packed_git *p) } - while (pack_max_fds <= pack_open_fds && close_one_pack(p->repo)) -+ while (pack_max_fds <= pack_open_fds && close_one_pack()) ++ while (pack_max_fds <= pack_open_fds && close_lru_pack()) ; /* nothing */ p->pack_fd = git_open(p->pack_name); @@ packfile.c: unsigned char *use_pack(struct packed_git *p, while (settings->packed_git_limit < pack_mapped && - unuse_one_window(p->repo->objects)) -+ unuse_one_window()) ++ unuse_lru_window()) ; /* nothing */ win->base = xmmap_gently(NULL, win->len, PROT_READ, MAP_PRIVATE, -@@ packfile.c: struct packed_git *packfile_from_idx(struct repository *r, const char *path, - - void packfile_free(struct packed_git *p) +@@ packfile.c: void packfile_free(struct packed_git *p) { + if (!p) + return; + packfile_list_remove(&all_packfiles, p); packfile_close(p); free(p); 6: 5e7c7c97901 ! 7: ec89598a42e packfile: skip closing packs marked as do-not-close @@ Commit message Skip over packfiles marked as do-not-close to reduce the fragility. This should not result in a user-visible change in behaviour. + An alternative would have been to just drop the bit completely, as + git-fast-import(1) is the only user thereof. But we can't because it + calls `use_pack()` indirectly via `unpack_entry()` on its written packs, + and that function will close a small packfile in case the mmap window + happens to cover the full pack. But we also call this function on the + pack that we're currently writing to, and closing that pack is of course + counterproductive. + Signed-off-by: Patrick Steinhardt <ps@pks.im> ## packfile.c ## -@@ packfile.c: static int close_one_pack(void) +@@ packfile.c: static int close_lru_pack(void) int accept_windows_inuse = 1; for (e = all_packfiles.head; e; e = e->next) { --- base-commit: f7de81456940dd6f3274e461149bba12965de609 change-id: 20261120-pks-packfile-global-statistics-92a9be97a11f