mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-07-07 10:15:45 +00:00
tracer: add safety comments
Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
parent
ca02a69fdf
commit
93883681be
@ -30,6 +30,7 @@ impl Tracer {
|
|||||||
|
|
||||||
fn end(&self) {
|
fn end(&self) {
|
||||||
let end = Instant::now();
|
let end = Instant::now();
|
||||||
|
// SAFETY: FFI call
|
||||||
let path = format!("cloud-hypervisor-{}.trace", unsafe { libc::getpid() });
|
let path = format!("cloud-hypervisor-{}.trace", unsafe { libc::getpid() });
|
||||||
let mut file = File::create(&path).unwrap();
|
let mut file = File::create(&path).unwrap();
|
||||||
|
|
||||||
@ -105,11 +106,14 @@ struct TraceEvent {
|
|||||||
|
|
||||||
pub fn trace_point_log(event: &'static str) {
|
pub fn trace_point_log(event: &'static str) {
|
||||||
let trace_event = TraceEvent {
|
let trace_event = TraceEvent {
|
||||||
|
// SAFETY: start has been initialised as part of initialising the value of TRACER
|
||||||
timestamp: Instant::now().duration_since(unsafe { TRACER.get().unwrap().start }),
|
timestamp: Instant::now().duration_since(unsafe { TRACER.get().unwrap().start }),
|
||||||
event,
|
event,
|
||||||
end_timestamp: None,
|
end_timestamp: None,
|
||||||
|
// SAFETY: thread_depth accesses current thread only specific data
|
||||||
depth: unsafe { TRACER.get().unwrap().thread_depth() },
|
depth: unsafe { TRACER.get().unwrap().thread_depth() },
|
||||||
};
|
};
|
||||||
|
// SAFETY: add_event accesses current thread only specific data
|
||||||
unsafe {
|
unsafe {
|
||||||
TRACER.get_mut().unwrap().add_event(trace_event);
|
TRACER.get_mut().unwrap().add_event(trace_event);
|
||||||
}
|
}
|
||||||
@ -122,6 +126,7 @@ pub struct TraceBlock {
|
|||||||
|
|
||||||
impl TraceBlock {
|
impl TraceBlock {
|
||||||
pub fn new(event: &'static str) -> Self {
|
pub fn new(event: &'static str) -> Self {
|
||||||
|
// SAFETY: increase_thread_depth accesses current thread only specific data
|
||||||
unsafe {
|
unsafe {
|
||||||
TRACER.get_mut().unwrap().increase_thread_depth();
|
TRACER.get_mut().unwrap().increase_thread_depth();
|
||||||
}
|
}
|
||||||
@ -134,16 +139,16 @@ impl TraceBlock {
|
|||||||
|
|
||||||
impl Drop for TraceBlock {
|
impl Drop for TraceBlock {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
// SAFETY: start has been initialised as part of initialising the value of TRACER
|
||||||
|
let start = unsafe { TRACER.get().unwrap().start };
|
||||||
let trace_event = TraceEvent {
|
let trace_event = TraceEvent {
|
||||||
timestamp: self
|
timestamp: self.start.duration_since(start),
|
||||||
.start
|
|
||||||
.duration_since(unsafe { TRACER.get().unwrap().start }),
|
|
||||||
event: self.event,
|
event: self.event,
|
||||||
end_timestamp: Some(
|
end_timestamp: Some(Instant::now().duration_since(start)),
|
||||||
Instant::now().duration_since(unsafe { TRACER.get().unwrap().start }),
|
// SAFETY: thread_depth() returns a number local to the current thread
|
||||||
),
|
|
||||||
depth: unsafe { TRACER.get().unwrap().thread_depth() },
|
depth: unsafe { TRACER.get().unwrap().thread_depth() },
|
||||||
};
|
};
|
||||||
|
// SAFETY: add_event and decrease_thread_depth access current thread only specific data
|
||||||
unsafe {
|
unsafe {
|
||||||
TRACER.get_mut().unwrap().add_event(trace_event);
|
TRACER.get_mut().unwrap().add_event(trace_event);
|
||||||
TRACER.get_mut().unwrap().decrease_thread_depth();
|
TRACER.get_mut().unwrap().decrease_thread_depth();
|
||||||
@ -166,9 +171,11 @@ macro_rules! trace_scoped {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn end() {
|
pub fn end() {
|
||||||
|
// SAFETY: this is called after all other threads end
|
||||||
unsafe { TRACER.get().unwrap().end() }
|
unsafe { TRACER.get().unwrap().end() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start() {
|
pub fn start() {
|
||||||
|
// SAFETY: this is called before other threads start
|
||||||
unsafe { TRACER.set(Tracer::new()).unwrap() }
|
unsafe { TRACER.set(Tracer::new()).unwrap() }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user