##// END OF EJS Templates
bisect: avoid adding irrelevant revisions to bisect state...
bisect: avoid adding irrelevant revisions to bisect state When adding new revisions to the bisect state, it only makes sense to add information about revisions that are under consideration (i.e., those that are topologically between the known good and bad revisions). However, if the user passes in a revset (e.g., '!merge()' to exclude merge commits), hg will resolve the revset first and add all matching revisions to the bisect state (which in this case would likely be the majority of revisions in the repo). To avoid this, revisions should only be added to the bisect state if they are between the good and bad revisions (and therefore relevant to the bisection). -- Here are the results of some performance tests using the `mozilla-central` repo (since it is one of the largest freely-available hg repositories in the wild). These tests compare the performance of a locally-built `hg` before and after application of this series. Note that `--noupdate` is passed to avoid including update time (which should not vary across cases). Setup (run between each test): $ hg bisect --reset $ hg bisect --noupdate --bad 56c3ad4bde5c70714b784ccf15d099e0df0f5bde $ hg bisect --noupdate --good 57426696adaf08298af3027fa77486fee0633b13 Test using a revset that returns a very large number of revisions: $ time hg bisect --noupdate --skip '!merge()' > /dev/null Before: real 0m9.398s user 0m9.233s sys 0m0.120s After: real 0m1.513s user 0m1.425s sys 0m0.052s Test using a revset that is expensive to compute: $ time hg bisect --noupdate --skip 'desc("Bug")' > /dev/null Before: real 0m49.853s user 0m49.580s sys 0m0.243s After: real 0m4.120s user 0m4.036s sys 0m0.048s

File last commit:

r49943:86c49b00 default
r50337:81623652 default
Show More
blackbox.rs
172 lines | 5.3 KiB | application/rls-services+xml | RustLexer
//! Logging for repository events, including commands run in the repository.
use crate::CliInvocation;
use format_bytes::format_bytes;
use hg::errors::HgError;
use hg::repo::Repo;
use hg::utils::{files::get_bytes_from_os_str, shell_quote};
use std::ffi::OsString;
const ONE_MEBIBYTE: u64 = 1 << 20;
// TODO: somehow keep defaults in sync with `configitem` in `hgext/blackbox.py`
const DEFAULT_MAX_SIZE: u64 = ONE_MEBIBYTE;
const DEFAULT_MAX_FILES: u32 = 7;
// Python does not support %.3f, only %f
const DEFAULT_DATE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.3f";
type DateTime = chrono::DateTime<chrono::Local>;
pub struct ProcessStartTime {
/// For measuring duration
monotonic_clock: std::time::Instant,
/// For formatting with year, month, day, etc.
calendar_based: DateTime,
}
impl ProcessStartTime {
pub fn now() -> Self {
Self {
monotonic_clock: std::time::Instant::now(),
calendar_based: chrono::Local::now(),
}
}
}
pub struct Blackbox<'a> {
process_start_time: &'a ProcessStartTime,
/// Do nothing if this is `None`
configured: Option<ConfiguredBlackbox<'a>>,
}
struct ConfiguredBlackbox<'a> {
repo: &'a Repo,
max_size: u64,
max_files: u32,
date_format: &'a str,
}
impl<'a> Blackbox<'a> {
pub fn new(
invocation: &'a CliInvocation<'a>,
process_start_time: &'a ProcessStartTime,
) -> Result<Self, HgError> {
let configured = if let Ok(repo) = invocation.repo {
if invocation.config.get(b"extensions", b"blackbox").is_none() {
// The extension is not enabled
None
} else {
Some(ConfiguredBlackbox {
repo,
max_size: invocation
.config
.get_byte_size(b"blackbox", b"maxsize")?
.unwrap_or(DEFAULT_MAX_SIZE),
max_files: invocation
.config
.get_u32(b"blackbox", b"maxfiles")?
.unwrap_or(DEFAULT_MAX_FILES),
date_format: invocation
.config
.get_str(b"blackbox", b"date-format")?
.unwrap_or(DEFAULT_DATE_FORMAT),
})
}
} else {
// Without a local repository there’s no `.hg/blackbox.log` to
// write to.
None
};
Ok(Self {
process_start_time,
configured,
})
}
pub fn log_command_start<'arg>(
&self,
argv: impl Iterator<Item = &'arg OsString>,
) {
if let Some(configured) = &self.configured {
let message = format_bytes!(b"(rust) {}", format_cli_args(argv));
configured.log(&self.process_start_time.calendar_based, &message);
}
}
pub fn log_command_end<'arg>(
&self,
argv: impl Iterator<Item = &'arg OsString>,
exit_code: i32,
) {
if let Some(configured) = &self.configured {
let now = chrono::Local::now();
let duration = self
.process_start_time
.monotonic_clock
.elapsed()
.as_secs_f64();
let message = format_bytes!(
b"(rust) {} exited {} after {} seconds",
format_cli_args(argv),
exit_code,
format_bytes::Utf8(format_args!("{:.03}", duration))
);
configured.log(&now, &message);
}
}
}
impl ConfiguredBlackbox<'_> {
fn log(&self, date_time: &DateTime, message: &[u8]) {
let date = format_bytes::Utf8(date_time.format(self.date_format));
let user = users::get_current_username().map(get_bytes_from_os_str);
let user = user.as_deref().unwrap_or(b"???");
let rev = format_bytes::Utf8(match self.repo.dirstate_parents() {
Ok(parents) if parents.p2 == hg::revlog::node::NULL_NODE => {
format!("{:x}", parents.p1)
}
Ok(parents) => format!("{:x}+{:x}", parents.p1, parents.p2),
Err(_dirstate_corruption_error) => {
// TODO: log a non-fatal warning to stderr
"???".to_owned()
}
});
let pid = std::process::id();
let line = format_bytes!(
b"{} {} @{} ({})> {}\n",
date,
user,
rev,
pid,
message
);
let result =
hg::logging::LogFile::new(self.repo.hg_vfs(), "blackbox.log")
.max_size(Some(self.max_size))
.max_files(self.max_files)
.write(&line);
match result {
Ok(()) => {}
Err(_io_error) => {
// TODO: log a non-fatal warning to stderr
}
}
}
}
fn format_cli_args<'a>(
mut args: impl Iterator<Item = &'a OsString>,
) -> Vec<u8> {
let _ = args.next(); // Skip the first (or zeroth) arg, the name of the `rhg` executable
let mut args = args.map(|arg| shell_quote(&get_bytes_from_os_str(arg)));
let mut formatted = Vec::new();
if let Some(arg) = args.next() {
formatted.extend(arg)
}
for arg in args {
formatted.push(b' ');
formatted.extend(arg)
}
formatted
}