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: git@vger.kernel.org
Cc: "Ramkumar Ramachandra" <artagnon@gmail.com>,
	"Junio C Hamano" <gitster@pobox.com>,
	"Johannes Sixt" <j6t@kdbg.org>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v3] gc: reject if another gc is running, unless --force is given
Date: Mon,  5 Aug 2013 21:19:14 +0700	[thread overview]
Message-ID: <1375712354-13171-1-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1375510890-4728-1-git-send-email-pclouds@gmail.com>

This may happen when `git gc --auto` is run automatically, then the
user, to avoid wait time, switches to a new terminal, keeps working
and `git gc --auto` is started again because the first gc instance has
not clean up the repository.

This patch tries to avoid multiple gc running, especially in --auto
mode. In the worst case, gc may be delayed 12 hours if a daemon reuses
the pid stored in gc-%s.pid.

uname() and kill(..,0) are added to MinGW compatibility layer so that
it'll work on Windows. Actually uname() is stub so it won't prevent
multiple gc running on a shared repo. But that's for Windows
contributors to step in.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 - new code is taken out in a separate function for clarity
 - file locking
 - implementing kill(pid, 0) on windows (but not tested)

 Documentation/git-gc.txt |  6 ++++-
 builtin/gc.c             | 62 ++++++++++++++++++++++++++++++++++++++++++++++++
 compat/mingw.c           |  6 +++++
 compat/mingw.h           |  6 +++++
 git-compat-util.h        |  1 +
 5 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-gc.txt b/Documentation/git-gc.txt
index 2402ed6..e158a3b 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]
+'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
 
 DESCRIPTION
 -----------
@@ -72,6 +72,10 @@ automatic consolidation of packs.
 --quiet::
 	Suppress all progress reports.
 
+--force::
+	Force `git gc` to run even if there may be another `git gc`
+	instance running on this repository.
+
 Configuration
 -------------
 
diff --git a/builtin/gc.c b/builtin/gc.c
index 6be6c8d..1f33908 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -167,11 +167,66 @@ static int need_to_gc(void)
 	return 1;
 }
 
+static int gc_running(int force)
+{
+	static struct lock_file lock;
+	struct utsname utsname;
+	struct stat st;
+	uintmax_t pid;
+	FILE *fp;
+	int fd, should_exit;
+
+	if (uname(&utsname))
+		strcpy(utsname.nodename, "unknown");
+
+	fd = hold_lock_file_for_update(&lock,
+			git_path("gc-%s.pid", utsname.nodename), 0);
+	if (!force) {
+		if (fd < 0)
+			return 1;
+
+		fp = fopen(git_path("gc-%s.pid", utsname.nodename), "r");
+		should_exit =
+			fp != NULL &&
+			!fstat(fileno(fp), &st) &&
+			/*
+			 * 12 hour limit is very generous as gc should
+			 * never take that long. On the other hand we
+			 * don't really need a strict limit here,
+			 * running gc --auto one day late is not a big
+			 * problem. --force can be used in manual gc
+			 * after the user verifies that no gc is
+			 * running.
+			 */
+			time(NULL) - st.st_mtime <= 12 * 3600 &&
+			fscanf(fp, "%"PRIuMAX, &pid) == 1 &&
+			!kill(pid, 0);
+		if (fp != NULL)
+			fclose(fp);
+		if (should_exit) {
+			if (fd >= 0)
+				rollback_lock_file(&lock);
+			return 1;
+		}
+	}
+
+	if (fd >= 0) {
+		struct strbuf sb = STRBUF_INIT;
+		strbuf_addf(&sb, "%"PRIuMAX"\n", (uintmax_t) getpid());
+		write_in_full(fd, sb.buf, sb.len);
+		strbuf_release(&sb);
+		commit_lock_file(&lock);
+	}
+
+	return 0;
+}
+
 int cmd_gc(int argc, const char **argv, const char *prefix)
 {
 	int aggressive = 0;
 	int auto_gc = 0;
 	int quiet = 0;
+	int force = 0;
 
 	struct option builtin_gc_options[] = {
 		OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -180,6 +235,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
 		OPT_BOOLEAN(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
 		OPT_BOOLEAN(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_END()
 	};
 
@@ -225,6 +281,12 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
 	} else
 		add_repack_all_option();
 
+	if (gc_running(force)) {
+		if (auto_gc)
+			return 0; /* be quiet on --auto */
+		die(_("gc is already running (use --force if not)"));
+	}
+
 	if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
 		return error(FAILED_RUN, pack_refs_cmd.argv[0]);
 
diff --git a/compat/mingw.c b/compat/mingw.c
index bb92c43..22ee9ef 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1086,6 +1086,12 @@ int mingw_kill(pid_t pid, int sig)
 		errno = err_win_to_posix(GetLastError());
 		CloseHandle(h);
 		return -1;
+	} else if (pid > 0 && sig == 0) {
+		HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+		if (h) {
+			CloseHandle(h);
+			return 0;
+		}
 	}
 
 	errno = EINVAL;
diff --git a/compat/mingw.h b/compat/mingw.h
index bd0a88b..2c25d2d 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -68,6 +68,10 @@ struct itimerval {
 };
 #define ITIMER_REAL 0
 
+struct utsname {
+	char nodename[128];
+};
+
 /*
  * sanitize preprocessor namespace polluted by Windows headers defining
  * macros which collide with git local versions
@@ -86,6 +90,8 @@ static inline int fchmod(int fildes, mode_t mode)
 { errno = ENOSYS; return -1; }
 static inline pid_t fork(void)
 { errno = ENOSYS; return -1; }
+static inline int uname(struct utsname *buf)
+{ errno = ENOSYS; return -1; }
 static inline unsigned int alarm(unsigned int seconds)
 { return 0; }
 static inline int fsync(int fd)
diff --git a/git-compat-util.h b/git-compat-util.h
index 115cb1d..c6a53cb 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -139,6 +139,7 @@
 #include <sys/resource.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
+#include <sys/utsname.h>
 #include <termios.h>
 #ifndef NO_SYS_SELECT_H
 #include <sys/select.h>
-- 
1.8.2.83.gc99314b

  parent reply	other threads:[~2013-08-05 14:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-03  4:20 [PATCH] gc: reject if another gc is running, unless --force is given Nguyễn Thái Ngọc Duy
2013-08-03  6:21 ` [PATCH v2] " Nguyễn Thái Ngọc Duy
2013-08-03  7:52   ` Ramkumar Ramachandra
2013-08-03 10:01     ` Duy Nguyen
2013-08-03  9:49   ` Johannes Sixt
2013-08-03 10:01     ` Duy Nguyen
2013-08-03 10:40       ` Johannes Sixt
2013-08-05 14:19   ` Nguyễn Thái Ngọc Duy [this message]
2013-08-05 18:17     ` [PATCH v3] " Junio C Hamano
2013-08-05 18:37       ` Ramkumar Ramachandra
2013-08-06  6:43         ` Junio C Hamano
2013-08-06  6:45           ` Ramkumar Ramachandra
2013-08-08 11:05     ` [PATCH v4] " Nguyễn Thái Ngọc Duy
2013-08-08 18:12       ` Junio C Hamano
2013-08-09 12:52         ` Duy Nguyen
2013-08-09 16:00           ` Junio C Hamano
2013-08-09 16:29       ` Andres Perera
2013-08-09 17:41         ` Junio C Hamano
2013-08-10  0:45         ` Duy Nguyen
2013-08-10  7:11           ` Andreas Schwab

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=1375712354-13171-1-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=artagnon@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    /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).