view rust/chg/src/sighandlers.c @ 49491:c6a1beba27e9

bisect: avoid copying ancestor list for non-merge commits During a bisection, hg needs to compute a list of all ancestors for every candidate commit. This is accomplished via a bottom-up traversal of the set of candidates, during which each revision's ancestor list is populated using the ancestor list of its parent(s). Previously, this involved copying the entire list, which could be very long in if the bisection range was large. To help improve this, we can observe that each candidate commit is visited exactly once, at which point its ancestor list is copied into its children's lists and then dropped. In the case of non-merge commits, a commit's ancestor list consists exactly of its parent's list plus itself. This means that we can trivially reuse the parent's existing list for one of its non-merge children, which avoids copying entirely if that commit is the parent's only child. This makes bisections over linear ranges of commits much faster. During some informal testing in the large publicly-available `mozilla-central` repository, this noticeably sped up bisections over large ranges of history: Setup: $ cd mozilla-central $ hg bisect --reset $ hg bisect --good 0 $ hg log -r tip -T '{rev}\n' 628417 Test: $ time hg bisect --bad tip --noupdate Before: real 3m35.927s user 3m35.553s sys 0m0.319s After: real 1m41.142s user 1m40.810s sys 0m0.285s
author Arun Kulshreshtha <akulshreshtha@janestreet.com>
date Tue, 30 Aug 2022 15:29:55 -0400
parents 763b45bc4483
children
line wrap: on
line source

/*
 * Signal handlers for cHg
 *
 * Copyright 2011, 2018 Yuya Nishihara <yuya@tcha.org>
 *
 * This software may be used and distributed according to the terms of the
 * GNU General Public License version 2 or any later version.
 */

#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

static pid_t peerpgid = 0;
static pid_t peerpid = 0;

static void forwardsignal(int sig)
{
	assert(peerpid > 0);
	(void)kill(peerpid, sig);
}

static void forwardsignaltogroup(int sig)
{
	/* prefer kill(-pgid, sig), fallback to pid if pgid is invalid */
	pid_t killpid = peerpgid > 1 ? -peerpgid : peerpid;
	(void)kill(killpid, sig);
}

static void handlestopsignal(int sig)
{
	sigset_t unblockset, oldset;
	struct sigaction sa, oldsa;
	if (sigemptyset(&unblockset) < 0) {
		return;
	}
	if (sigaddset(&unblockset, sig) < 0) {
		return;
	}
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = SIG_DFL;
	sa.sa_flags = SA_RESTART;
	if (sigemptyset(&sa.sa_mask) < 0) {
		return;
	}

	forwardsignal(sig);
	if (raise(sig) < 0) { /* resend to self */
		return;
	}
	if (sigaction(sig, &sa, &oldsa) < 0) {
		return;
	}
	if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) {
		return;
	}
	/* resent signal will be handled before sigprocmask() returns */
	if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) {
		return;
	}
	if (sigaction(sig, &oldsa, NULL) < 0) {
		return;
	}
}

/*
 * Installs signal handlers.
 *
 * Returns 0 on success, -1 on error and errno is set appropriately.
 * Installed handlers wouldn't be cleaned up on error.
 */
int setupsignalhandler(pid_t pid, pid_t pgid)
{
	if (pid <= 0) {
		errno = EINVAL;
		return -1;
	}
	peerpid = pid;
	peerpgid = (pgid <= 1 ? 0 : pgid);

	struct sigaction sa;
	memset(&sa, 0, sizeof(sa));

	/* deadly signals meant to be sent to a process group:
	 * - SIGHUP: usually generated by the kernel, when termination of a
	 *   process causes that process group to become orphaned
	 * - SIGINT: usually generated by the terminal */
	sa.sa_handler = forwardsignaltogroup;
	sa.sa_flags = SA_RESTART;
	if (sigemptyset(&sa.sa_mask) < 0) {
		return -1;
	}
	if (sigaction(SIGHUP, &sa, NULL) < 0) {
		return -1;
	}
	if (sigaction(SIGINT, &sa, NULL) < 0) {
		return -1;
	}

	/* terminate frontend by double SIGTERM in case of server freeze */
	sa.sa_handler = forwardsignal;
	sa.sa_flags |= SA_RESETHAND;
	if (sigaction(SIGTERM, &sa, NULL) < 0) {
		return -1;
	}

	/* notify the worker about window resize events */
	sa.sa_flags = SA_RESTART;
	if (sigaction(SIGWINCH, &sa, NULL) < 0) {
		return -1;
	}
	/* forward user-defined signals */
	if (sigaction(SIGUSR1, &sa, NULL) < 0) {
		return -1;
	}
	if (sigaction(SIGUSR2, &sa, NULL) < 0) {
		return -1;
	}
	/* propagate job control requests to worker */
	sa.sa_handler = forwardsignal;
	sa.sa_flags = SA_RESTART;
	if (sigaction(SIGCONT, &sa, NULL) < 0) {
		return -1;
	}
	sa.sa_handler = handlestopsignal;
	sa.sa_flags = SA_RESTART;
	if (sigaction(SIGTSTP, &sa, NULL) < 0) {
		return -1;
	}

	return 0;
}

/*
 * Restores signal handlers to the default, and masks SIGINT.
 *
 * Returns 0 on success, -1 on error and errno is set appropriately.
 */
int restoresignalhandler(void)
{
	struct sigaction sa;
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = SIG_DFL;
	sa.sa_flags = SA_RESTART;
	if (sigemptyset(&sa.sa_mask) < 0) {
		return -1;
	}

	if (sigaction(SIGHUP, &sa, NULL) < 0) {
		return -1;
	}
	if (sigaction(SIGTERM, &sa, NULL) < 0) {
		return -1;
	}
	if (sigaction(SIGWINCH, &sa, NULL) < 0) {
		return -1;
	}
	if (sigaction(SIGCONT, &sa, NULL) < 0) {
		return -1;
	}
	if (sigaction(SIGTSTP, &sa, NULL) < 0) {
		return -1;
	}

	/* ignore Ctrl+C while shutting down to make pager exits cleanly */
	sa.sa_handler = SIG_IGN;
	if (sigaction(SIGINT, &sa, NULL) < 0) {
		return -1;
	}

	peerpid = 0;
	return 0;
}