Skip to content

Instantly share code, notes, and snippets.

@Mark-Simulacrum
Created November 27, 2019 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mark-Simulacrum/6dfa7678f2d449175aa1f3d8856340f7 to your computer and use it in GitHub Desktop.
Save Mark-Simulacrum/6dfa7678f2d449175aa1f3d8856340f7 to your computer and use it in GitHub Desktop.
contention patch
diff --git a/src/libstd/sys/unix/mutex.rs b/src/libstd/sys/unix/mutex.rs
index b43af8fdcaa..26c90cf6f31 100644
--- a/src/libstd/sys/unix/mutex.rs
+++ b/src/libstd/sys/unix/mutex.rs
@@ -1,6 +1,29 @@
use crate::cell::UnsafeCell;
use crate::mem::MaybeUninit;
+
+fn time_and_wrap(s: &str, f: impl FnOnce()) {
+ use crate::cell::Cell;
+ thread_local!(static SHOULD_CAPTURE: Cell<bool> = Cell::new(true););
+ SHOULD_CAPTURE.with(|capture| {
+ if capture.get() {
+ capture.set(false);
+ let start = crate::time::Instant::now();
+ f();
+ let elapsed = start.elapsed();
+ if elapsed.as_millis() > 300 {
+ use crate::io::Write;
+ let bt = crate::backtrace::Backtrace::force_capture();
+ let mut f = crate::fs::OpenOptions::new().create(true).append(true).open(&format!("/tmp/timings-{}", s)).unwrap();
+ let _ = f.write_all(format!("{} {:?}: {}", elapsed.as_nanos(), elapsed, bt).as_bytes());
+ }
+ capture.set(true);
+ } else {
+ f();
+ }
+ });
+}
+
pub struct Mutex { inner: UnsafeCell<libc::pthread_mutex_t> }
#[inline]
@@ -52,8 +75,10 @@ impl Mutex {
}
#[inline]
pub unsafe fn lock(&self) {
- let r = libc::pthread_mutex_lock(self.inner.get());
- debug_assert_eq!(r, 0);
+ time_and_wrap("mutex", || {
+ let result = libc::pthread_mutex_lock(self.inner.get());
+ debug_assert_eq!(result, 0);
+ });
}
#[inline]
pub unsafe fn unlock(&self) {
@@ -106,8 +131,10 @@ impl ReentrantMutex {
}
pub unsafe fn lock(&self) {
- let result = libc::pthread_mutex_lock(self.inner.get());
- debug_assert_eq!(result, 0);
+ time_and_wrap("reentrant", || {
+ let result = libc::pthread_mutex_lock(self.inner.get());
+ debug_assert_eq!(result, 0);
+ });
}
#[inline]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment