Mercurial > public > mercurial-scm > hg-stable
view rust/hg-core/src/lock.rs @ 48750:94e36b230990
status: prefer relative paths in Rust code
? when the repository root is under the current directory,
so the kernel needs to traverse fewer directory in every call
to `read_dir` or `symlink_metadata`.
Better yet would be to use libc functions like `openat` and `fstatat`
to remove such repeated traversals entirely, but the standard library
does not provide APIs based on those.
Maybe with a crate like https://crates.io/crates/openat instead?
Benchmarks of `rhg status` show that this patch is neutral in some configurations,
and makes the command up to ~20% faster in others.
Below is semi-arbitrary subset of results. The four numeric columns are:
time (in seconds) with this changeset?s parent, time with this changeset,
time difference (negative is better), time ratio (less than 1?is better).
```
mercurial-dirstate-v1 | default-plain-clean.no-iu.pbr | 0.0061 -> 0.0059: -0.0002 (0.97)
mercurial-dirstate-v2 | default-plain-clean.no-iu.pbr | 0.0029 -> 0.0028: -0.0001 (0.97)
mozilla-dirstate-v1 | default-plain-clean.no-iu.pbr | 0.2110 -> 0.2102: -0.0007 (1.00)
mozilla-dirstate-v2 | default-copies-clean.ignored.pbr | 0.0489 -> 0.0401: -0.0088 (0.82)
mozilla-dirstate-v2 | default-copies-clean.no-iu.pbr | 0.0479 -> 0.0393: -0.0085 (0.82)
mozilla-dirstate-v2 | default-copies-large.all.pbr | 0.1262 -> 0.1210: -0.0051 (0.96)
mozilla-dirstate-v2 | default-copies-small.ignored-unknown.pbr | 0.1262 -> 0.1200: -0.0062 (0.95)
mozilla-dirstate-v2 | default-copies-small.ignored.pbr | 0.0536 -> 0.0417: -0.0119 (0.78)
mozilla-dirstate-v2 | default-copies-small.no-iu.pbr | 0.0482 -> 0.0393: -0.0089 (0.81)
mozilla-dirstate-v2 | default-plain-clean.ignored.pbr | 0.0518 -> 0.0402: -0.0116 (0.78)
mozilla-dirstate-v2 | default-plain-clean.no-iu.pbr | 0.0481 -> 0.0392: -0.0088 (0.82)
mozilla-dirstate-v2 | default-plain-large.all.pbr | 0.1271 -> 0.1218: -0.0052 (0.96)
mozilla-dirstate-v2 | default-plain-small.ignored-unknown.pbr | 0.1225 -> 0.1202: -0.0022 (0.98)
mozilla-dirstate-v2 | default-plain-small.ignored.pbr | 0.0510 -> 0.0418: -0.0092 (0.82)
mozilla-dirstate-v2 | default-plain-small.no-iu.pbr | 0.0480 -> 0.0394: -0.0086 (0.82)
netbeans-dirstate-v1 | default-plain-clean.no-iu.pbr | 0.1442 -> 0.1422: -0.0020 (0.99)
netbeans-dirstate-v2 | default-plain-clean.no-iu.pbr | 0.0325 -> 0.0282: -0.0043 (0.87)
```
Differential Revision: https://phab.mercurial-scm.org/D12175
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 21 Jan 2022 17:54:03 +0100 |
parents | 5734b03ecf3e |
children | 59be65b7cdfd |
line wrap: on
line source
//! Filesystem-based locks for local repositories use crate::errors::HgError; use crate::errors::HgResultExt; use crate::utils::StrExt; use crate::vfs::Vfs; use std::io; use std::io::ErrorKind; #[derive(derive_more::From)] pub enum LockError { AlreadyHeld, #[from] Other(HgError), } /// Try to call `f` with the lock acquired, without waiting. /// /// If the lock is aready held, `f` is not called and `LockError::AlreadyHeld` /// is returned. `LockError::Io` is returned for any unexpected I/O error /// accessing the lock file, including for removing it after `f` was called. /// The return value of `f` is dropped in that case. If all is successful, the /// return value of `f` is forwarded. pub fn try_with_lock_no_wait<R>( hg_vfs: Vfs, lock_filename: &str, f: impl FnOnce() -> R, ) -> Result<R, LockError> { let our_lock_data = &*OUR_LOCK_DATA; for _retry in 0..5 { match make_lock(hg_vfs, lock_filename, our_lock_data) { Ok(()) => { let result = f(); unlock(hg_vfs, lock_filename)?; return Ok(result); } Err(HgError::IoError { error, .. }) if error.kind() == ErrorKind::AlreadyExists => { let lock_data = read_lock(hg_vfs, lock_filename)?; if lock_data.is_none() { // Lock was apparently just released, retry acquiring it continue; } if !lock_should_be_broken(&lock_data) { return Err(LockError::AlreadyHeld); } // The lock file is left over from a process not running // anymore. Break it, but with another lock to // avoid a race. break_lock(hg_vfs, lock_filename)?; // Retry acquiring } Err(error) => Err(error)?, } } Err(LockError::AlreadyHeld) } fn break_lock(hg_vfs: Vfs, lock_filename: &str) -> Result<(), LockError> { try_with_lock_no_wait(hg_vfs, &format!("{}.break", lock_filename), || { // Check again in case some other process broke and // acquired the lock in the meantime let lock_data = read_lock(hg_vfs, lock_filename)?; if !lock_should_be_broken(&lock_data) { return Err(LockError::AlreadyHeld); } Ok(hg_vfs.remove_file(lock_filename)?) })? } #[cfg(unix)] fn make_lock( hg_vfs: Vfs, lock_filename: &str, data: &str, ) -> Result<(), HgError> { // Use a symbolic link because creating it is atomic. // The link’s "target" contains data not representing any path. let fake_symlink_target = data; hg_vfs.create_symlink(lock_filename, fake_symlink_target) } fn read_lock( hg_vfs: Vfs, lock_filename: &str, ) -> Result<Option<String>, HgError> { let link_target = hg_vfs.read_link(lock_filename).io_not_found_as_none()?; if let Some(target) = link_target { let data = target .into_os_string() .into_string() .map_err(|_| HgError::corrupted("non-UTF-8 lock data"))?; Ok(Some(data)) } else { Ok(None) } } fn unlock(hg_vfs: Vfs, lock_filename: &str) -> Result<(), HgError> { hg_vfs.remove_file(lock_filename) } /// Return whether the process that is/was holding the lock is known not to be /// running anymore. fn lock_should_be_broken(data: &Option<String>) -> bool { (|| -> Option<bool> { let (prefix, pid) = data.as_ref()?.split_2(':')?; if prefix != &*LOCK_PREFIX { return Some(false); } let process_is_running; #[cfg(unix)] { let pid: libc::pid_t = pid.parse().ok()?; unsafe { let signal = 0; // Test if we could send a signal, without sending let result = libc::kill(pid, signal); if result == 0 { process_is_running = true } else { let errno = io::Error::last_os_error().raw_os_error().unwrap(); process_is_running = errno != libc::ESRCH } } } Some(!process_is_running) })() .unwrap_or(false) } lazy_static::lazy_static! { /// A string which is used to differentiate pid namespaces /// /// It's useful to detect "dead" processes and remove stale locks with /// confidence. Typically it's just hostname. On modern linux, we include an /// extra Linux-specific pid namespace identifier. static ref LOCK_PREFIX: String = { // Note: this must match the behavior of `_getlockprefix` in `mercurial/lock.py` /// Same as https://github.com/python/cpython/blob/v3.10.0/Modules/socketmodule.c#L5414 const BUFFER_SIZE: usize = 1024; let mut buffer = [0_i8; BUFFER_SIZE]; let hostname_bytes = unsafe { let result = libc::gethostname(buffer.as_mut_ptr(), BUFFER_SIZE); if result != 0 { panic!("gethostname: {}", io::Error::last_os_error()) } std::ffi::CStr::from_ptr(buffer.as_mut_ptr()).to_bytes() }; let hostname = std::str::from_utf8(hostname_bytes).expect("non-UTF-8 hostname"); #[cfg(target_os = "linux")] { use std::os::linux::fs::MetadataExt; match std::fs::metadata("/proc/self/ns/pid") { Ok(meta) => { return format!("{}/{:x}", hostname, meta.st_ino()) } Err(error) => { // TODO: match on `error.kind()` when `NotADirectory` // is available on all supported Rust versions: // https://github.com/rust-lang/rust/issues/86442 use libc::{ ENOENT, // ErrorKind::NotFound ENOTDIR, // ErrorKind::NotADirectory EACCES, // ErrorKind::PermissionDenied }; match error.raw_os_error() { Some(ENOENT) | Some(ENOTDIR) | Some(EACCES) => {} _ => panic!("stat /proc/self/ns/pid: {}", error), } } } } hostname.to_owned() }; static ref OUR_LOCK_DATA: String = format!("{}:{}", &*LOCK_PREFIX, std::process::id()); }