-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c80b055
commit 005fd0b
Showing
3 changed files
with
139 additions
and
111 deletions.
There are no files selected for viewing
This file contains 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
120 changes: 73 additions & 47 deletions
120
crates/cairo-profiler/src/trace_reader/function_trace_builder/function_stack_trace.rs
This file contains 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 |
---|---|---|
@@ -1,67 +1,93 @@ | ||
use crate::trace_reader::function_name::FunctionName; | ||
|
||
struct CallStackElement { | ||
pub function_name: FunctionName, | ||
/// Consecutive recursive calls to this function that are currently on the stack. | ||
recursive_calls_count: usize, | ||
} | ||
use crate::trace_reader::sample::{FunctionCall, InternalFunctionCall}; | ||
|
||
/// The function call stack of the current function, excluding the current function call. | ||
pub(super) struct CallStack { | ||
stack: Vec<CallStackElement>, | ||
/// Tracks the depth of the function call stack, without limit. This is usually equal to | ||
/// `stack.len()`, but if the actual stack is deeper than `max_function_trace_depth`, | ||
/// this remains reliable while `stack` does not. | ||
real_function_stack_depth: usize, | ||
/// Constant through existence of the object. | ||
max_function_stack_trace_depth: usize, | ||
/// This stack is guaranteed to have maximum of `max_function_stack_trace_depth` elements. | ||
stack: VecWithLimitedCapacity<FunctionCall>, | ||
/// The last element of this vector is always a number of elements of the stack before the last | ||
/// function call. | ||
previous_stack_lengths: Vec<usize>, | ||
} | ||
|
||
impl CallStack { | ||
pub fn new(max_function_stack_trace_depth: usize) -> Self { | ||
Self { | ||
stack: vec![], | ||
real_function_stack_depth: 0, | ||
max_function_stack_trace_depth, | ||
stack: VecWithLimitedCapacity::new(max_function_stack_trace_depth), | ||
previous_stack_lengths: vec![], | ||
} | ||
} | ||
|
||
pub fn enter_function_call(&mut self, function_name: FunctionName) { | ||
if let Some(stack_element) = self.stack.last_mut() { | ||
if function_name == stack_element.function_name { | ||
stack_element.recursive_calls_count += 1; | ||
return; | ||
} | ||
pub fn enter_function_call(&mut self, new_call_stack: VecWithLimitedCapacity<FunctionCall>) { | ||
self.previous_stack_lengths.push(self.stack.len()); | ||
|
||
self.stack = new_call_stack; | ||
} | ||
|
||
pub fn exit_function_call(&mut self) -> Option<()> { | ||
let previous_stack_len = self.previous_stack_lengths.pop()?; | ||
self.stack.truncate(previous_stack_len); | ||
Some(()) | ||
} | ||
|
||
/// Returns current call stack truncated to `max_function_stack_trace_depth`. | ||
pub fn current_call_stack( | ||
&self, | ||
current_function_name: FunctionName, | ||
) -> VecWithLimitedCapacity<FunctionCall> { | ||
let mut current_call_stack = self.stack.clone(); | ||
|
||
current_call_stack.push(FunctionCall::InternalFunctionCall( | ||
InternalFunctionCall::NonInlined(current_function_name), | ||
)); | ||
|
||
current_call_stack | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct VecWithLimitedCapacity<T> { | ||
vector: Vec<T>, | ||
max_capacity: usize, | ||
} | ||
|
||
impl<T> VecWithLimitedCapacity<T> { | ||
pub fn new(max_capacity: usize) -> Self { | ||
Self { | ||
vector: vec![], | ||
max_capacity, | ||
} | ||
} | ||
|
||
if self.real_function_stack_depth < self.max_function_stack_trace_depth { | ||
self.stack.push(CallStackElement { | ||
function_name, | ||
recursive_calls_count: 0, | ||
}); | ||
pub fn from(mut vector: Vec<T>, max_capacity: usize) -> Self { | ||
vector.truncate(max_capacity); | ||
Self { | ||
vector, | ||
max_capacity, | ||
} | ||
self.real_function_stack_depth += 1; | ||
} | ||
|
||
pub fn exit_function_call(&mut self) -> Option<()> { | ||
if self.real_function_stack_depth <= self.max_function_stack_trace_depth { | ||
let mut stack_element = self.stack.pop()?; | ||
|
||
if stack_element.recursive_calls_count > 0 { | ||
// Recursive function exited. | ||
stack_element.recursive_calls_count -= 1; | ||
self.stack.push(stack_element); | ||
|
||
Some(()) | ||
} else { | ||
// Regular function exited. | ||
self.real_function_stack_depth -= 1; | ||
Some(()) | ||
} | ||
} else { | ||
// Hidden function exited. | ||
self.real_function_stack_depth -= 1; | ||
Some(()) | ||
pub fn push(&mut self, el: T) { | ||
if self.vector.len() < self.max_capacity { | ||
self.vector.push(el); | ||
} | ||
} | ||
|
||
pub fn truncate(&mut self, len: usize) { | ||
self.vector.truncate(len); | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.vector.len() | ||
} | ||
|
||
pub fn deconstruct(self) -> (Vec<T>, usize) { | ||
(self.vector, self.max_capacity) | ||
} | ||
} | ||
|
||
impl<T> From<VecWithLimitedCapacity<T>> for Vec<T> { | ||
fn from(value: VecWithLimitedCapacity<T>) -> Self { | ||
value.vector | ||
} | ||
} |
This file contains 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