Skip to content

Commit

Permalink
feat: experimental deadlock detection
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsoledad authored and doitian committed Apr 10, 2019
1 parent b34a0c0 commit f55cec5
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions src/helper.rs
@@ -0,0 +1,38 @@
use ckb_util::{parking_lot::deadlock, Condvar, Mutex};
use log::warn;
use std::sync::Arc;
use std::thread;
use std::time::Duration;

pub fn wait_for_exit() {
let exit = Arc::new((Mutex::new(()), Condvar::new()));

// Handle possible exits
let e = Arc::<(Mutex<()>, Condvar)>::clone(&exit);
let _ = ctrlc::set_handler(move || {
e.1.notify_all();
});

// Wait for signal
let mut l = exit.0.lock();
exit.1.wait(&mut l);
}

pub fn deadlock_detection() {
thread::spawn(move || loop {
thread::sleep(Duration::from_secs(10));
let deadlocks = deadlock::check_deadlock();
if deadlocks.is_empty() {
continue;
}

warn!("{} deadlocks detected", deadlocks.len());
for (i, threads) in deadlocks.iter().enumerate() {
warn!("Deadlock #{}", i);
for t in threads {
warn!("Thread Id {:#?}", t.thread_id());
warn!("{:#?}", t.backtrace());
}
}
});
}
2 changes: 1 addition & 1 deletion src/main.rs
@@ -1,6 +1,6 @@
mod helper;
mod setup;
mod subcommand;
mod system;

use setup::{cli, ExitCode, Setup};

Expand Down
4 changes: 3 additions & 1 deletion src/subcommand/run.rs
@@ -1,5 +1,5 @@
use crate::helper::{deadlock_detection, wait_for_exit};
use crate::setup::{ExitCode, RunArgs};
use crate::system::wait_for_exit;
use ckb_chain::chain::{ChainBuilder, ChainController};
use ckb_db::diskdb::RocksDB;
use ckb_miner::BlockAssembler;
Expand All @@ -15,6 +15,8 @@ use log::info;
use std::sync::Arc;

pub fn run(args: RunArgs) -> Result<(), ExitCode> {
deadlock_detection();

let shared = SharedBuilder::<CacheDB<RocksDB>>::default()
.consensus(args.consensus)
.db(&args.config.db)
Expand Down
16 changes: 0 additions & 16 deletions src/system.rs

This file was deleted.

2 changes: 1 addition & 1 deletion util/Cargo.toml
Expand Up @@ -6,4 +6,4 @@ authors = ["Nervos Core Dev <dev@nervos.org>"]
edition = "2018"

[dependencies]
parking_lot = "0.7"
parking_lot = {version = "0.7", features = ["deadlock_detection"]}
4 changes: 3 additions & 1 deletion util/src/lib.rs
@@ -1,7 +1,9 @@
mod unstable;

pub use crate::unstable::{TryFrom, TryInto};
pub use parking_lot::{Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub use parking_lot::{
self, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
};

/// Helper macro for reducing boilerplate code for matching `Option` together
/// with early return.
Expand Down

0 comments on commit f55cec5

Please sign in to comment.