Skip to content

Commit

Permalink
use std::sync::atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Sep 23, 2018
1 parent c9885dd commit 4976bef
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 36 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion components/msg/Cargo.toml
Expand Up @@ -12,7 +12,6 @@ test = false
doctest = false

[dependencies]
atomic = {version = "0.4", features = ["nightly"]}
backtrace = "0.3"
bitflags = "1.0"
ipc-channel = "0.11"
Expand Down
29 changes: 14 additions & 15 deletions components/msg/background_hang_monitor.rs
Expand Up @@ -2,41 +2,40 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use atomic::{Atomic, Ordering};
use backtrace::Backtrace;
use constellation_msg::{HangAlert, HangAnnotation};
use constellation_msg::{MonitoredComponentId, MonitoredComponentMsg};
use ipc_channel::ipc::IpcSender;
use libc;
use servo_channel::{Receiver, Sender, base_channel, channel};
use servo_channel::{Receiver, base_channel};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::{Duration, Instant};


/// The means of communication between monitored and monitor, inside of a "trace transaction".
pub static mut BACKTRACE: Option<(libc::pthread_t, Backtrace)> = None;

lazy_static! {
/// The means of communication between monitored and monitoree.
pub static ref BACKTRACE: Atomic<Option<(libc::pthread_t, [u8; 400])>> = Atomic::new(None);
/// A flag used to create a "trace transaction" around the workflow of accessing the backtrace,
/// from a monitored thread inside a SIGPROF handler, and the background hang monitor.
pub static ref BACKTRACE_READY: AtomicBool = AtomicBool::new(false);
}

#[allow(unsafe_code)]
unsafe fn get_backtrace_from_monitored_component(monitored: &MonitoredComponent) -> String {
//assert!(Atomic::<Option<(libc::pthread_t, [char; 200])>>::is_lock_free());
unsafe fn get_backtrace_from_monitored_component(monitored: &MonitoredComponent) -> Backtrace {
libc::pthread_kill(monitored.thread_id, libc::SIGPROF);
loop {
if BACKTRACE.load(Ordering::SeqCst).is_some() {
if BACKTRACE_READY.load(Ordering::SeqCst) {
break;
}
thread::yield_now();
}
let (thread_id, trace) = BACKTRACE.load(Ordering::SeqCst).unwrap();
let (thread_id, trace) = BACKTRACE.take().unwrap();
assert_eq!(thread_id, monitored.thread_id);
let mut trace_vec = Vec::new();
for x in trace.iter() {
trace_vec.push(*x);
}
BACKTRACE.store(None, Ordering::SeqCst);
String::from_utf8(trace_vec).unwrap()
BACKTRACE_READY.store(false, Ordering::SeqCst);
trace
}

struct MonitoredComponent {
Expand Down Expand Up @@ -160,7 +159,7 @@ impl BackgroundHangMonitor {
HangAlert::Permanent(
component_id.clone(),
last_annotation,
trace
format!("{:?}", trace)
)
);
monitored.sent_permanent_alert = true;
Expand Down
15 changes: 4 additions & 11 deletions components/msg/constellation_msg.rs
Expand Up @@ -5,17 +5,16 @@
//! The high-level interface from script to constellation. Using this abstract interface helps
//! reduce coupling between these two components.

use atomic::Ordering;
use background_hang_monitor::{BackgroundHangMonitor, BACKTRACE};
use background_hang_monitor::{BackgroundHangMonitor, BACKTRACE, BACKTRACE_READY};
use backtrace::Backtrace;
use ipc_channel::ipc::IpcSender;
use libc;
use servo_channel::{Sender, channel};
use sig::ffi::Sig;
use std::cell::Cell;
use std::fmt;
use std::mem;
use std::num::NonZeroU32;
use std::sync::atomic::Ordering;
use std::thread;
use std::time::Duration;
use webrender_api;
Expand Down Expand Up @@ -607,16 +606,10 @@ pub fn start_monitoring_component(
#[allow(unsafe_code)]
fn install_sigprof_handler() {
fn handler(_sig: i32) {
let trace = Backtrace::new();
let trace_string = format!("{:?}", trace);
let trace_vec = trace_string.into_bytes();
let trace_slice = trace_vec.as_slice();
let mut trace_array: [u8; 400] = unsafe { mem::uninitialized() };
let bytes = &trace_slice[..trace_array.len()];
trace_array.copy_from_slice(bytes);
unsafe {
BACKTRACE.store(Some((libc::pthread_self(), trace_array)), Ordering::SeqCst);
BACKTRACE = Some((libc::pthread_self(), Backtrace::new()));
}
BACKTRACE_READY.store(true, Ordering::SeqCst);
}
signal!(Sig::PROF, handler);
}
1 change: 0 additions & 1 deletion components/msg/lib.rs
Expand Up @@ -4,7 +4,6 @@

#![deny(unsafe_code)]

extern crate atomic;
extern crate backtrace;
#[macro_use]
extern crate bitflags;
Expand Down
1 change: 0 additions & 1 deletion components/msg/tests/hang_monitor_tests.rs
Expand Up @@ -10,7 +10,6 @@ use msg::constellation_msg::{HangAnnotation, HangAlert, ScriptHangAnnotation};
use msg::constellation_msg::{init_background_hang_monitor, start_monitoring_component};
use msg::constellation_msg::{MonitoredComponentType, MonitoredComponentMsg, MonitoredComponentId};
use msg::constellation_msg::TEST_PIPELINE_ID;
use std::sync::{Arc, Barrier};
use std::thread;
use std::time::Duration;

Expand Down

0 comments on commit 4976bef

Please sign in to comment.