git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: pclouds@gmail.com
Cc: git@vger.kernel.org, Eric Sunshine <sunshine@sunshineco.com>,
	Jeff Hostetler <git@jeffhostetler.com>,
	Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v4 3/7] gc: add --keep-largest-pack option
Date: Sat, 24 Mar 2018 08:42:30 +0100	[thread overview]
Message-ID: <20180324074308.18934-6-pclouds@gmail.com> (raw)
In-Reply-To: <20180324074308.18934-1-pclouds@gmail.com>

This adds a new repack mode that combines everything into a secondary
pack, leaving the largest pack alone.

This could help reduce memory pressure. On linux-2.6.git, valgrind
massif reports 1.6GB heap in "pack all" case, and 535MB in "pack
all except the base pack" case. We save roughly 1GB memory by
excluding the base pack.

This should also lower I/O because we don't have to rewrite a giant
pack every time (e.g. for linux-2.6.git that's a 1.4GB pack file)..

PS. The use of string_list here seems overkill, but we'll need it in
the next patch...

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-gc.txt |  6 ++++-
 builtin/gc.c             | 47 ++++++++++++++++++++++++++++++++++++----
 t/t6500-gc.sh            | 25 +++++++++++++++++++++
 3 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-gc.txt b/Documentation/git-gc.txt
index 571b5a7e3c..bf81b8de30 100644
--- a/Documentation/git-gc.txt
+++ b/Documentation/git-gc.txt
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
 SYNOPSIS
 --------
 [verse]
-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force] [--keep-largest-pack]
 
 DESCRIPTION
 -----------
@@ -78,6 +78,10 @@ automatic consolidation of packs.
 	Force `git gc` to run even if there may be another `git gc`
 	instance running on this repository.
 
+--keep-largest-pack::
+	All packs except the largest pack and those marked with a
+	`.keep` files are consolidated into a single pack.
+
 Configuration
 -------------
 
diff --git a/builtin/gc.c b/builtin/gc.c
index 77fa720bd0..9a09cf53b0 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -164,6 +164,24 @@ static int too_many_loose_objects(void)
 	return needed;
 }
 
+static void find_base_packs(struct string_list *packs)
+{
+	struct packed_git *p, *base = NULL;
+
+	prepare_packed_git();
+
+	for (p = packed_git; p; p = p->next) {
+		if (!p->pack_local)
+			continue;
+		if (!base || base->pack_size < p->pack_size) {
+			base = p;
+		}
+	}
+
+	if (base)
+		string_list_append(packs, base->pack_name);
+}
+
 static int too_many_packs(void)
 {
 	struct packed_git *p;
@@ -187,7 +205,13 @@ static int too_many_packs(void)
 	return gc_auto_pack_limit < cnt;
 }
 
-static void add_repack_all_option(void)
+static int keep_one_pack(struct string_list_item *item, void *data)
+{
+	argv_array_pushf(&repack, "--keep-pack=%s", basename(item->string));
+	return 0;
+}
+
+static void add_repack_all_option(struct string_list *keep_pack)
 {
 	if (prune_expire && !strcmp(prune_expire, "now"))
 		argv_array_push(&repack, "-a");
@@ -196,6 +220,9 @@ static void add_repack_all_option(void)
 		if (prune_expire)
 			argv_array_pushf(&repack, "--unpack-unreachable=%s", prune_expire);
 	}
+
+	if (keep_pack)
+		for_each_string_list(keep_pack, keep_one_pack, NULL);
 }
 
 static void add_repack_incremental_option(void)
@@ -219,7 +246,7 @@ static int need_to_gc(void)
 	 * there is no need.
 	 */
 	if (too_many_packs())
-		add_repack_all_option();
+		add_repack_all_option(NULL);
 	else if (too_many_loose_objects())
 		add_repack_incremental_option();
 	else
@@ -353,6 +380,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	const char *name;
 	pid_t pid;
 	int daemonized = 0;
+	int keep_base_pack = -1;
 
 	struct option builtin_gc_options[] = {
 		OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -362,6 +390,8 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 		OPT_BOOL(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
 		OPT_BOOL(0, "auto", &auto_gc, N_("enable auto-gc mode")),
 		OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
+		OPT_BOOL(0, "keep-largest-pack", &keep_base_pack,
+			 N_("repack all other packs except the largest pack")),
 		OPT_END()
 	};
 
@@ -427,8 +457,17 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 			 */
 			daemonized = !daemonize();
 		}
-	} else
-		add_repack_all_option();
+	} else {
+		struct string_list keep_pack = STRING_LIST_INIT_NODUP;
+
+		if (keep_base_pack != -1) {
+			if (keep_base_pack)
+				find_base_packs(&keep_pack);
+		}
+
+		add_repack_all_option(&keep_pack);
+		string_list_clear(&keep_pack, 0);
+	}
 
 	name = lock_repo_for_gc(force, &pid);
 	if (name) {
diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
index 41b0be575d..a95969af2a 100755
--- a/t/t6500-gc.sh
+++ b/t/t6500-gc.sh
@@ -43,6 +43,31 @@ test_expect_success 'gc is not aborted due to a stale symref' '
 	)
 '
 
+test_expect_success 'gc --keep-largest-pack' '
+	test_create_repo keep-pack &&
+	(
+		cd keep-pack &&
+		test_commit one &&
+		test_commit two &&
+		test_commit three &&
+		git gc &&
+		( cd .git/objects/pack && ls *.pack ) >pack-list &&
+		test_line_count = 1 pack-list &&
+		BASE_PACK=.git/objects/pack/pack-*.pack &&
+		test_commit four &&
+		git repack -d &&
+		test_commit five &&
+		git repack -d &&
+		( cd .git/objects/pack && ls *.pack ) >pack-list &&
+		test_line_count = 3 pack-list &&
+		git gc --keep-largest-pack &&
+		( cd .git/objects/pack && ls *.pack ) >pack-list &&
+		test_line_count = 2 pack-list &&
+		test_path_is_file $BASE_PACK &&
+		git fsck
+	)
+'
+
 test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' '
 	test_config gc.auto 3 &&
 	test_config gc.autodetach false &&
-- 
2.17.0.rc0.348.gd5a49e0b6f


  parent reply	other threads:[~2018-03-24  7:44 UTC|newest]

Thread overview: 111+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-17  7:53 [PATCH 00/36] Combine t/helper binaries into a single one Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 01/36] t/helper: add an empty test-tool program Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 02/36] t/helper: merge test-chmtime into test-tool Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 03/36] t/helper: merge test-sha1 " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 04/36] t/helper: merge test-lazy-init-name-hash " Nguyễn Thái Ngọc Duy
2018-03-18  2:11   ` Eric Sunshine
2018-03-18  8:25     ` Duy Nguyen
2018-03-18  8:47       ` Eric Sunshine
2018-03-19  9:40         ` Jeff Hostetler
2018-03-19 15:51           ` Duy Nguyen
2018-03-17  7:53 ` [PATCH 05/36] t/helper: merge test-config " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 06/36] t/helper: merge test-ctype " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 07/36] t/helper: merge test-date " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 08/36] t/helper: merge (unused) test-delta " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 09/36] t/helper: merge test-drop-caches " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 10/36] t/helper: merge test-dump-cache-tree " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 11/36] t/helper: merge test-dump-split-index " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 12/36] t/helper: merge test-example-decorate " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 13/36] t/helper: merge test-genrandom " Nguyễn Thái Ngọc Duy
2018-03-17  7:53 ` [PATCH 14/36] t/helper: merge test-hashmap " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 15/36] t/helper: merge test-index-version " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 16/36] t/helper: merge (unused) test-match-trees " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 17/36] t/helper: merge (unused) test-mergesort " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 18/36] t/helper: merge test-mktemp " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 19/36] t/helper: merge test-online-cpus " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 20/36] t/helper: merge test-path-utils " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 21/36] t/helper: merge test-prio-queue " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 22/36] t/helepr: merge test-read-cache " Nguyễn Thái Ngọc Duy
2018-03-18  2:21   ` Eric Sunshine
2018-03-17  7:54 ` [PATCH 23/36] t/helper: merge test-ref-store " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 24/36] t/helper: merge test-regex " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 25/36] t/helper: merge test-revision-walking " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 26/36] t/helper: merge test-run-command " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 27/36] t/helper: merge test-scrap-cache-tree " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 28/36] t/helper: merge test-sha1-array " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 29/36] t/helper: merge test-sigchain " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 30/36] t/helper: merge test-strcmp-offset " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 31/36] t/helper: merge test-string-list " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 32/36] t/helper: merge test-submodule-config " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 33/36] t/helper: merge test-subprocess " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 34/36] t/helper: merge test-urlmatch-.. " Nguyễn Thái Ngọc Duy
2018-03-18  2:32   ` Eric Sunshine
2018-03-17  7:54 ` [PATCH 35/36] t/helper: merge test-wildmatch " Nguyễn Thái Ngọc Duy
2018-03-17  7:54 ` [PATCH 36/36] t/helper: merge test-write-cache " Nguyễn Thái Ngọc Duy
2018-03-24  7:42 ` [PATCH v2 00/36] Combine t/helper binaries into a single one Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v4 1/7] t7700: have closing quote of a test at the beginning of line Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v2 01/36] t/helper: add an empty test-tool program Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v4 2/7] repack: add --keep-pack option Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v2 02/36] t/helper: merge test-chmtime into test-tool Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` Nguyễn Thái Ngọc Duy [this message]
2018-03-24  9:36     ` [PATCH v4 3/7] gc: add --keep-largest-pack option Ævar Arnfjörð Bjarmason
2018-03-24 12:13       ` Duy Nguyen
2018-03-26  2:05         ` Junio C Hamano
2018-03-24  7:42   ` [PATCH v2 03/36] t/helper: merge test-sha1 into test-tool Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v4 4/7] gc: add gc.bigPackThreshold config Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v2 04/36] t/helper: merge test-lazy-init-name-hash into test-tool Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v4 5/7] gc: handle a corner case in gc.bigPackThreshold Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v2 05/36] t/helper: merge test-config into test-tool Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v4 6/7] gc --auto: exclude base pack if not enough mem to "repack -ad" Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v2 06/36] t/helper: merge test-ctype into test-tool Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v4 7/7] pack-objects: show some progress when counting kept objects Nguyễn Thái Ngọc Duy
2018-03-24  7:42   ` [PATCH v2 07/36] t/helper: merge test-date into test-tool Nguyễn Thái Ngọc Duy
2018-03-24  7:44 ` [PATCH v2 00/36] Combine t/helper binaries into a single one Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 01/36] t/helper: add an empty test-tool program Nguyễn Thái Ngọc Duy
2018-03-26 15:27     ` Johannes Schindelin
2018-03-26 17:09       ` Duy Nguyen
2018-03-26 22:14         ` Johannes Schindelin
2018-03-26 23:07           ` SZEDER Gábor
2018-03-27 13:57             ` Johannes Schindelin
2018-03-24  7:44   ` [PATCH v2 02/36] t/helper: merge test-chmtime into test-tool Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 03/36] t/helper: merge test-sha1 " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 04/36] t/helper: merge test-lazy-init-name-hash " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 05/36] t/helper: merge test-config " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 06/36] t/helper: merge test-ctype " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 07/36] t/helper: merge test-date " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 08/36] t/helper: merge (unused) test-delta " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 09/36] t/helper: merge test-drop-caches " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 10/36] t/helper: merge test-dump-cache-tree " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 11/36] t/helper: merge test-dump-split-index " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 12/36] t/helper: merge test-example-decorate " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 13/36] t/helper: merge test-genrandom " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 14/36] t/helper: merge test-hashmap " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 15/36] t/helper: merge test-index-version " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 16/36] t/helper: merge (unused) test-match-trees " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 17/36] t/helper: merge (unused) test-mergesort " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 18/36] t/helper: merge test-mktemp " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 19/36] t/helper: merge test-online-cpus " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 20/36] t/helper: merge test-path-utils " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 21/36] t/helper: merge test-prio-queue " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 22/36] t/helper: merge test-read-cache " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 23/36] t/helper: merge test-ref-store " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 24/36] t/helper: merge test-regex " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 25/36] t/helper: merge test-revision-walking " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 26/36] t/helper: merge test-run-command " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 27/36] t/helper: merge test-scrap-cache-tree " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 28/36] t/helper: merge test-sha1-array " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 29/36] t/helper: merge test-sigchain " Nguyễn Thái Ngọc Duy
2018-03-24  7:44   ` [PATCH v2 30/36] t/helper: merge test-strcmp-offset " Nguyễn Thái Ngọc Duy
2018-03-24  7:45   ` [PATCH v2 31/36] t/helper: merge test-string-list " Nguyễn Thái Ngọc Duy
2018-03-24  7:45   ` [PATCH v2 32/36] t/helper: merge test-submodule-config " Nguyễn Thái Ngọc Duy
2018-03-24  7:45   ` [PATCH v2 33/36] t/helper: merge test-subprocess " Nguyễn Thái Ngọc Duy
2018-03-24  7:45   ` [PATCH v2 34/36] t/helper: merge test-urlmatch-normalization " Nguyễn Thái Ngọc Duy
2018-03-24  7:45   ` [PATCH v2 35/36] t/helper: merge test-wildmatch " Nguyễn Thái Ngọc Duy
2018-03-24  7:45   ` [PATCH v2 36/36] t/helper: merge test-write-cache " Nguyễn Thái Ngọc Duy
2018-03-24 12:50   ` [PATCH v2 00/36] Combine t/helper binaries into a single one Ævar Arnfjörð Bjarmason
2018-03-24 12:57     ` Duy Nguyen
2018-03-26  2:16   ` Junio C Hamano
2018-03-27 14:00   ` Johannes Schindelin
2018-03-27 15:42     ` Junio C Hamano
2018-03-27 16:12       ` Johannes Schindelin
  -- strict thread matches above, loose matches on Subject: below --
2018-03-16 19:27 [PATCH v3 0/7] nd/repack-keep-pack updates Nguyễn Thái Ngọc Duy
2018-03-24  7:25 ` [PATCH v4 " Nguyễn Thái Ngọc Duy
2018-03-24  7:25   ` [PATCH v4 3/7] gc: add --keep-largest-pack option Nguyễn Thái Ngọc Duy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180324074308.18934-6-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@jeffhostetler.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).