Skip to content

Commit

Permalink
Add "tracy" feature to enable Tracy profiler, with some basic annotat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
graydon committed Jul 15, 2023
1 parent 2941946 commit 801d8a3
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 14 deletions.
159 changes: 158 additions & 1 deletion Cargo.lock

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

10 changes: 9 additions & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ exclude = [
"num-derive",
"zeroize_derive",
# also a not upgraded to syn 2, but just a dev-dep anyway
"int-enum"
"int-enum",
# the dep specs of tracy-client are weird and include "loom" which it
# totally doesn't depend on but in any case it's compiled-out in real
# production builds we care about.
"tracy-client",
# Somehow the tracking machinery of two different dev-dep tracing
# subsystems also winds up pulling in conflicts, but again, just
# dev-deps or non-produciton configs.
"tracking-allocator"
]

# If true, metadata will be collected with `--all-features`. Note that this can't
Expand Down
4 changes: 4 additions & 0 deletions soroban-env-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ k256 = {version = "0.13.1", features=["ecdsa", "arithmetic"]}
getrandom = { version = "0.2", features=["js"] }
sha3 = "0.10.8"

[target.'cfg(not(target_family = "wasm"))'.dependencies]
tracy-client = { version = "=0.15.2", features = ["enable", "timer-fallback"], default-features = false, optional = true }

[dev-dependencies]
env_logger = "0.9.0"
itertools = "0.10.3"
Expand All @@ -55,6 +58,7 @@ linregress = "0.5.1"

[features]
testutils = ["soroban-env-common/testutils"]
tracy = ["dep:tracy-client"]

[target.'cfg(target_os = "linux")'.dev-dependencies]
perf-event = "0.4.7"
Expand Down
5 changes: 5 additions & 0 deletions soroban-env-host/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ impl AuthorizationManager {
address: AddressObject,
args: Vec<Val>,
) -> Result<(), HostError> {
let _span = tracy_span!("require auth");
let authorized_function = self
.try_borrow_call_stack(host)?
.last()
Expand Down Expand Up @@ -810,6 +811,7 @@ impl AuthorizationManager {

// Returns a snapshot of `AuthorizationManager` to use for rollback.
pub(crate) fn snapshot(&self, host: &Host) -> Result<AuthorizationManagerSnapshot, HostError> {
let _span = tracy_span!("snapshot auth");
let account_trackers_snapshot = match &self.mode {
AuthorizationMode::Enforcing => AccountTrackersSnapshot::Enforcing(
self.try_borrow_account_trackers(host)?
Expand Down Expand Up @@ -858,6 +860,7 @@ impl AuthorizationManager {
host: &Host,
snapshot: AuthorizationManagerSnapshot,
) -> Result<(), HostError> {
let _span = tracy_span!("rollback auth");
match snapshot.account_trackers_snapshot {
AccountTrackersSnapshot::Enforcing(trackers_snapshot) => {
let trackers = self.try_borrow_account_trackers(host)?;
Expand Down Expand Up @@ -942,6 +945,7 @@ impl AuthorizationManager {
// Records a new call stack frame.
// This should be called for every `Host` `push_frame`.
pub(crate) fn push_frame(&self, host: &Host, frame: &Frame) -> Result<(), HostError> {
let _span = tracy_span!("push auth frame");
let (contract_id, function_name) = match frame {
Frame::ContractVM { vm, fn_name, .. } => {
(vm.contract_id.metered_clone(host.budget_ref())?, *fn_name)
Expand Down Expand Up @@ -970,6 +974,7 @@ impl AuthorizationManager {
// Pops a call stack frame.
// This should be called for every `Host` `pop_frame`.
pub(crate) fn pop_frame(&self, host: &Host) -> Result<(), HostError> {
let _span = tracy_span!("pop auth frame");
{
let mut call_stack = self.try_borrow_call_stack_mut(host)?;
// Currently we don't push host function call frames, hence this may be
Expand Down
11 changes: 10 additions & 1 deletion soroban-env-host/src/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,18 @@ impl DepthLimiter for BudgetImpl {
}
}

#[derive(Default, Clone)]
#[derive(Clone)]
pub struct Budget(pub(crate) Rc<RefCell<BudgetImpl>>);

#[allow(clippy::derivable_impls)]
impl Default for Budget {
fn default() -> Self {
#[cfg(all(not(target_family = "wasm"), feature = "tracy"))]
let _client = tracy_client::Client::start();
Self(Default::default())
}
}

impl Debug for Budget {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{:?}", self.0.try_borrow().map_err(|_| std::fmt::Error)?)
Expand Down
13 changes: 12 additions & 1 deletion soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,18 @@ pub(crate) struct HostImpl {
previous_authorization_manager: RefCell<Option<AuthorizationManager>>,
}
// Host is a newtype on Rc<HostImpl> so we can impl Env for it below.
#[derive(Default, Clone)]
#[derive(Clone)]
pub struct Host(pub(crate) Rc<HostImpl>);

#[allow(clippy::derivable_impls)]
impl Default for Host {
fn default() -> Self {
#[cfg(all(not(target_family = "wasm"), feature = "tracy"))]
let _client = tracy_client::Client::start();
Self(Default::default())
}
}

macro_rules! impl_checked_borrow_helpers {
($field:ident, $t:ty, $borrow:ident, $borrow_mut:ident) => {
impl Host {
Expand Down Expand Up @@ -244,6 +253,8 @@ impl Host {
/// contract-data access functions such as
/// [`Env::get_contract_data`].
pub fn with_storage_and_budget(storage: Storage, budget: Budget) -> Self {
#[cfg(all(not(target_family = "wasm"), feature = "tracy"))]
let _client = tracy_client::Client::start();
Self(Rc::new(HostImpl {
source_account: RefCell::new(None),
ledger: RefCell::new(None),
Expand Down
Loading

0 comments on commit 801d8a3

Please sign in to comment.