This repository was archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Preliminary trace recording. #3
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,45 @@ | |
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use ::cell::RefCell; | ||
use ::fmt; | ||
|
||
#[allow(missing_docs)] | ||
/// A block location in the Rust MIR. | ||
pub struct MirLoc { | ||
pub crate_hash: u64, | ||
pub def_idx: u32, | ||
pub bb_idx: u32, | ||
} | ||
|
||
impl fmt::Debug for MirLoc { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth this manual code vs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I did a custom one was because the derived one is quite long, with the names of the fields etc. I wanted a shorter representation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, but let's only do this sparingly, just because it's that bit more code to read and maintain. |
||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "loc<{}, {}, {}>", self.crate_hash, self.def_idx, self.bb_idx) | ||
} | ||
} | ||
|
||
thread_local! { | ||
/// The software trace currently being collected (if any). | ||
/// When `Some`, a tracing is enabled, otherwise tracing is disabled. | ||
pub static TRACE: RefCell<Option<Vec<MirLoc>>> = RefCell::new(None); | ||
} | ||
|
||
/// Start software tracing. | ||
#[cfg_attr(not(stage0), no_trace)] | ||
pub fn start_tracing() { | ||
TRACE.with(|rc| { | ||
let mut trace_o = rc.borrow_mut(); | ||
match *trace_o { | ||
Some(_) => panic!("tracing was already started for this thread!"), | ||
None => *trace_o = Some(Vec::new()), | ||
} | ||
}); | ||
} | ||
|
||
// FIXME Anything used in `rec_loc` below cannot itself be traced, or we get infinite recursion. To | ||
// work around this, many crates are ignored by the software tracing MIR pass (see | ||
// librustc_mir/transform/add_yk_swt_calls.rs). Consider re-implementing the trace recorder in C? | ||
|
||
/// The software trace recorder function. | ||
/// The `AddYkSWTCalls` MIR pass injects a call this for every MIR block. The call is done | ||
/// indirectly via a wrapper in libcore. | ||
|
@@ -15,5 +54,19 @@ | |
#[cfg_attr(not(stage0), no_trace)] | ||
#[cfg(not(test))] | ||
fn rec_loc(crate_hash: u64, def_idx: u32, bb_idx: u32) { | ||
// Not implemented. | ||
TRACE.with(|rc| { | ||
let mut trace_o = rc.borrow_mut(); | ||
match trace_o.as_mut() { | ||
Some(trace) => trace.push(MirLoc{crate_hash, def_idx, bb_idx}), | ||
None => (), // We are not currently tracing, do nothing. | ||
} | ||
}); | ||
} | ||
|
||
/// Stop tracing and return the trace. | ||
#[cfg_attr(not(stage0), no_trace)] | ||
pub fn stop_tracing() -> Vec<MirLoc> { | ||
TRACE.with(|rc| { | ||
rc.borrow_mut().take().expect("tracing not started on this thread") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2019 King's College London. | ||
// Created by the Software Development Team <http://soft-dev.org/>. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// error-pattern: thread 'main' panicked at 'tracing was already started for this thread!' | ||
|
||
#![feature(yk_swt)] | ||
|
||
use std::yk_swt::{start_tracing, stop_tracing}; | ||
|
||
pub fn main() { | ||
start_tracing(); | ||
start_tracing(); // Can't call a second time without a stop_tracing() first. | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2019 King's College London. | ||
// Created by the Software Development Team <http://soft-dev.org/>. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// error-pattern: thread 'main' panicked at 'tracing not started on this thread' | ||
|
||
#![feature(yk_swt)] | ||
|
||
use std::yk_swt::{start_tracing, stop_tracing}; | ||
|
||
pub fn main() { | ||
// Missing start_tracing(); | ||
let _ = stop_tracing(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2019 King's College London. | ||
// Created by the Software Development Team <http://soft-dev.org/>. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(yk_swt)] | ||
|
||
use std::yk_swt::{start_tracing, stop_tracing}; | ||
|
||
pub fn main() { | ||
start_tracing(); | ||
let _ = work(); | ||
let trace = stop_tracing(); | ||
assert!(!trace.is_empty()); | ||
} | ||
|
||
#[inline(never)] | ||
fn work() -> u64{ | ||
let mut res = 100; | ||
for i in 0..10 { | ||
res += res / 2 + i; | ||
} | ||
res | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this comment (it just says what the line of code below does, and the line of code is very clear IMHO).