Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat syscalls as nodes #112

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

THenry14
Copy link
Member

@THenry14 THenry14 commented Oct 14, 2024

Closes #88

Introduced changes

  • Remove syscalls from samples list, and treat them as separate nodes
  • Allow to pass custom resource map with resources cost different to default one

Checklist

  • Linked relevant issue
  • Updated relevant documentation (README.md)
  • Added relevant tests
  • Performed self-review of the code
  • Added changes to CHANGELOG.md

Minimal example:
obraz

crates/cairo-profiler/src/main.rs Show resolved Hide resolved
crates/cairo-profiler/src/main.rs Outdated Show resolved Hide resolved
crates/trace-data/src/lib.rs Outdated Show resolved Hide resolved
crates/cairo-profiler/src/trace_reader/syscall.rs Outdated Show resolved Hide resolved
crates/cairo-profiler/src/main.rs Outdated Show resolved Hide resolved
crates/cairo-profiler/src/trace_reader/syscall.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@piotmag769 piotmag769 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check if it doesn't mess up the inlining? It shouldn't since the syscalls are always leaves in the tree, but it's worth verifying

crates/cairo-profiler/src/main.rs Outdated Show resolved Hide resolved
crates/cairo-profiler/src/versioned_constants_reader.rs Outdated Show resolved Hide resolved
crates/trace-data/src/lib.rs Show resolved Hide resolved
crates/cairo-profiler/src/versioned_constants_reader.rs Outdated Show resolved Hide resolved
function_name,
))) = call_stack.last()
{
let Ok(syscall) = map_syscall_name_to_selector(function_name.0.as_str()) else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo we should expect here, if the TryInto fails it means we have broken the conversion implementation (and names of these libfuncs in sierra shouldn't change, so we don't have to worry about that)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about other libfuncs? at this level they are indistinguishable from syscalls. I guess expecting here would mean that each time we run at a libfunc that is not in artifacts file, we panic.

Copy link
Collaborator

@piotmag769 piotmag769 Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this level they are, but you explicitly add only syscalls there during trace building. Good point though, having the type checked during creation is better, we could do:

pub enum InternalFunctionCall {
    Inlined(FunctionName),
    NonInlined(FunctionName),
    Syscall(DeprecatedSyscallSelector),
}

so it is typesafe

crates/cairo-profiler/src/versioned_constants_reader.rs Outdated Show resolved Hide resolved
crates/cairo-profiler/src/versioned_constants_reader.rs Outdated Show resolved Hide resolved
.execute_syscalls
.get(&syscall)
.unwrap_or_else(|| {
panic!("Missing syscall {syscall:?} from versioned constants file")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should validate if all syscalls' costs are present in the versioned constants file when reading it so we can unwrap safely here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the upside of this approach? I get users might fiddle with the file so technically this might be viewed as user error, but generally speaking if it's missing (and they do not pass a custom map), it's our fault right?

Copy link
Collaborator

@piotmag769 piotmag769 Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they pass a custom map with missing syscalls: it is a user error and we should report it as early as possible (during reading of the file)

If it is missing at this point in the code it is our fault, that's why unwrapping here is "safe" (if it fails i means we don't handle a certain syscall)

This comment may be in a wrong place, I just thought about it here (since it is the only way to make this unwrap make sense), sorry xD

@THenry14 THenry14 force-pushed the szymczyk/88-syscalls-as-nodes branch from c690afe to 1086f92 Compare October 25, 2024 13:43
Copy link
Member Author

@THenry14 THenry14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check if it doesn't mess up the inlining? It shouldn't since the syscalls are always leaves in the tree, but it's worth verifying

Yup, when I was testing I did it for both inlining and non-inlining, and compared to the same artifacts using code from main, nothing looked out of place or suspicious

crates/cairo-profiler/src/main.rs Outdated Show resolved Hide resolved
Ok(CoreConcreteLibfunc::StarkNet(_)) => {
if invocation.libfunc_id.debug_name.is_none() {
// this libfunc is not included in the artifact file
// it is likely to be a libfunc from the test itself
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some libfuncs seem to not be included in the sierra file, even though they are executed and can be catched here.

An example of such libfuncs can be, for example, a ContractAddressTryFromFelt252 or just Testing(Cheatcode) that is being called by the test (snforge)


if !in_syscall {
in_syscall = true;
let mut new_current_call_stack = current_call_stack.clone();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this just means that we need to mark current_call_stack as mut, and I wasn't sure it's the best way as call stack is typically not very large.

If you prefer it to be mut I can change, do not have strong opinion on this tbh

function_name,
))) = call_stack.last()
{
let Ok(syscall) = map_syscall_name_to_selector(function_name.0.as_str()) else {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about other libfuncs? at this level they are indistinguishable from syscalls. I guess expecting here would mean that each time we run at a libfunc that is not in artifacts file, we panic.

.execute_syscalls
.get(&syscall)
.unwrap_or_else(|| {
panic!("Missing syscall {syscall:?} from versioned constants file")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the upside of this approach? I get users might fiddle with the file so technically this might be viewed as user error, but generally speaking if it's missing (and they do not pass a custom map), it's our fault right?

crates/trace-data/src/lib.rs Show resolved Hide resolved
@@ -220,3 +257,84 @@ fn build_current_call_stack(
current_call_stack
}
}

fn stack_trace_to_samples(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls extract this function and the one below to separate module in crates/cairo-profiler/src/trace_reader/function_trace_builder/, this file is getting big

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Syscalls as nodes in trace tree
2 participants