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

feat: add a tool to perf call stack #195

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions crates/specs/src/itable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ impl Into<OpcodeClassPlain> for Opcode {
#[derive(Clone, Debug, Serialize)]
pub struct InstructionTableEntry {
pub fid: u32,
pub function_name: String,
pub iid: u32,
pub opcode: Opcode,
}
Expand Down Expand Up @@ -651,7 +652,12 @@ impl InstructionTable {
opcodeclass
}

pub fn push(&mut self, fid: u32, iid: u32, opcode: Opcode) {
self.0.push(InstructionTableEntry { fid, iid, opcode })
pub fn push(&mut self, fid: u32, function_name: String, iid: u32, opcode: Opcode) {
self.0.push(InstructionTableEntry {
fid,
function_name,
iid,
opcode,
})
}
}
1 change: 1 addition & 0 deletions crates/specs/src/jtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct StaticFrameEntry {
pub struct JumpTableEntry {
// caller eid (unique)
pub eid: u32,
// caller's caller eid. It's used to maintain a caller chain.
pub last_jump_eid: u32,
pub callee_fid: u32,
pub inst: Box<InstructionTableEntry>,
Expand Down
2 changes: 2 additions & 0 deletions crates/specs/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@ pub enum StepInfo {

Call {
index: u32,
function_name: String,
},
CallIndirect {
table_index: u32,
type_index: u32,
offset: u32,
func_index: u32,
function_name: String,
},
CallHost {
plugin: HostPlugin,
Expand Down
2 changes: 1 addition & 1 deletion crates/zkwasm/src/circuits/etable/op_configure/op_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<F: FieldExt> EventTableOpcodeConfig<F> for CallConfig<F> {
entry: &EventTableEntryWithMemoryInfo,
) -> Result<(), Error> {
match &entry.eentry.step_info {
StepInfo::Call { index } => {
StepInfo::Call { index, .. } => {
self.index_cell.assign(ctx, F::from(*index as u64))?;
self.frame_table_lookup.0.assign(
ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl<F: FieldExt> EventTableOpcodeConfig<F> for CallIndirectConfig<F> {
type_index,
offset,
func_index,
..
} => {
self.table_index.assign(ctx, F::from(*table_index as u64))?;
self.type_index.assign(ctx, F::from(*type_index as u64))?;
Expand Down
4 changes: 3 additions & 1 deletion crates/zkwasm/src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
pub fn new(k: u32, image: Vec<u8>, phantom_functions: Vec<String>) -> Result<Self> {
set_zkwasm_k(k);

let module = wasmi::Module::from_buffer(&image)?;
let mut module = wasmi::Module::from_buffer(&image)?;
let parity_module = module.module().clone().parse_names().unwrap();
module.module = parity_module;

let loader = Self {
k,
Expand Down
2 changes: 1 addition & 1 deletion crates/zkwasm/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ pub fn memory_event_of_step(event: &EventTableEntry, emid: &mut u32) -> Vec<Memo

ops
}
StepInfo::Call { index: _ } => {
StepInfo::Call { index: _, .. } => {
vec![]
}
StepInfo::CallIndirect { offset, .. } => {
Expand Down
3 changes: 2 additions & 1 deletion crates/zkwasm/src/runtime/wasmi_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl WasmiRuntime {

let fid_of_entry = {
let idx_of_entry = instance.lookup_function_by_name(tracer.clone(), entry);
let idx_of_start_function = module.module().start_section();

tracer
.clone()
Expand All @@ -146,7 +147,7 @@ impl WasmiRuntime {
enable: true,
frame_id: 0,
next_frame_id: 0,
callee_fid: 0, // the fid of start function is always 0
callee_fid: idx_of_start_function.unwrap(),
fid: idx_of_entry,
iid: 0,
});
Expand Down
5 changes: 5 additions & 0 deletions scripts/perf/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generate flamegraph for etable

```
./perf_etable.sh <Path of etable> > <Path of svg output>
```
56 changes: 56 additions & 0 deletions scripts/perf/call_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import json
import copy
import sys

def analyze_etable(etable):
dict = {}
frame = []

for entry in etable:
if len(frame) == 0:
frame.append(entry["inst"]["function_name"])

if type(entry["inst"]["opcode"]) == str:
opcode = entry["inst"]["opcode"]
else:
if len(entry["inst"]["opcode"].items()) != 1:
print("Panic")
exit(1)

for key, value in entry["inst"]["opcode"].items():
opcode = key
break

if tuple(frame) not in dict:
dict[tuple(frame)] = 1
else:
dict[tuple(frame)] = dict[tuple(frame)] + 1

if opcode == "Call":
frame.append(entry["step_info"]["Call"]["function_name"])
elif opcode == "CallIndirect":
frame.append(entry["step_info"]["CallIndirect"]["function_name"])
elif opcode == "Return":
frame.pop()

return dict

def generate_log(dict):
for key, value in dict.items():
print(*key, sep=";", end="")
print(" ", end="")
print(value)

def main():
if len(sys.argv) != 2:
print("Usage: python call_stack.py <Path of etable.json>");
exit(1)

etable_path = sys.argv[1]
etable = open(etable_path)
etable = json.load(etable)

dict = analyze_etable(etable)
generate_log(dict)

main()
Loading