-
Notifications
You must be signed in to change notification settings - Fork 83
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
Gas used and execution time measurement for the Move function #2031
Open
steelgeek091
wants to merge
9
commits into
rooch-network:main
Choose a base branch
from
steelgeek091:execution_measurement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b92edce
insert the generated move into the head and tail of the function
steelgeek091 5f4aa81
add measurement lib
steelgeek091 90792d9
to determine the return stmt by the return types of function
steelgeek091 8fefe53
add a command line option to enable the gas profiling
steelgeek091 4bb15df
add the license information for file
steelgeek091 c1cef1c
save the calling depth while call the inject function
steelgeek091 beaded5
use a feature to control the code
steelgeek091 f2f1e83
add current balance info
steelgeek091 315e080
Merge branch 'main' into execution_measurement
steelgeek091 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
<a name="0x2_measurements"></a> | ||
|
||
# Module `0x2::measurements` | ||
|
||
|
||
|
||
- [Function `inject_parameter`](#0x2_measurements_inject_parameter) | ||
|
||
|
||
<pre><code></code></pre> | ||
|
||
|
||
|
||
<a name="0x2_measurements_inject_parameter"></a> | ||
|
||
## Function `inject_parameter` | ||
|
||
|
||
|
||
<pre><code><b>public</b> <b>fun</b> <a href="measurements.md#0x2_measurements_inject_parameter">inject_parameter</a>(arg1: u64, arg2: u64, arg3: <a href="">vector</a><u8>): (u64, u64) | ||
</code></pre> |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module moveos_std::measurements { | ||
public native fun inject_parameter(arg1: u64, arg2: u64, arg3: vector<u8>): (u64, u64); | ||
} |
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
134 changes: 134 additions & 0 deletions
134
frameworks/moveos-stdlib/src/natives/moveos_stdlib/measurement.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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright (c) RoochNetwork | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::natives::helpers::{make_module_natives, make_native}; | ||
use move_binary_format::errors::PartialVMResult; | ||
use move_core_types::gas_algebra::InternalGas; | ||
use move_vm_runtime::native_functions::{NativeContext, NativeFunction}; | ||
use move_vm_types::loaded_data::runtime_types::Type; | ||
use move_vm_types::natives::function::NativeResult; | ||
use move_vm_types::pop_arg; | ||
use move_vm_types::values::Value; | ||
use moveos_verifier::execution_measurement::NativeExecutionTracing; | ||
use petgraph::matrix_graph::Zero; | ||
use smallvec::smallvec; | ||
use std::collections::VecDeque; | ||
use std::time::UNIX_EPOCH; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct MeasurementGas { | ||
pub base: InternalGas, | ||
} | ||
|
||
impl MeasurementGas { | ||
pub fn zero() -> Self { | ||
Self { base: 0.into() } | ||
} | ||
} | ||
|
||
fn inject_parameter( | ||
_gas_params: &MeasurementGas, | ||
context: &mut NativeContext, | ||
_ty_args: Vec<Type>, | ||
mut args: VecDeque<Value>, | ||
) -> PartialVMResult<NativeResult> { | ||
use std::time::SystemTime; | ||
|
||
let current_gas_balance = context.gas_balance(); | ||
let execution_tracing = context.extensions_mut().get_mut::<NativeExecutionTracing>(); | ||
|
||
let generate_print_space = |space_count: u64| -> String { | ||
if space_count.is_zero() { | ||
"".to_string() | ||
} else { | ||
"| ".repeat(space_count as usize) | ||
} | ||
}; | ||
|
||
let start = SystemTime::now(); | ||
let since_the_epoch = start | ||
.duration_since(UNIX_EPOCH) | ||
.expect("Time went backwards"); | ||
|
||
let full_func_name_bytes = pop_arg!(args, Vec<u8>); | ||
let full_func_name = String::from_utf8_lossy(full_func_name_bytes.as_slice()).to_string(); | ||
let timestamp = pop_arg!(args, u64); | ||
let gas_balance = pop_arg!(args, u64); | ||
|
||
let current_timestamp_millis = since_the_epoch.as_millis() as u64; | ||
if timestamp > 0 { | ||
let gas_used_p1 = InternalGas::from(gas_balance) | ||
.checked_sub(current_gas_balance) | ||
.unwrap(); | ||
|
||
if !gas_used_p1.is_zero() { | ||
if gas_used_p1 > InternalGas::new(31892) { | ||
if let Some(gas_used_p2) = gas_used_p1.checked_sub(31892.into()) { | ||
let time_used: u64 = current_timestamp_millis.saturating_sub(timestamp); | ||
execution_tracing.log.push(format!( | ||
"{}{}.gas_used: {:}, time_used {:?} -> {}, balance {:}", | ||
generate_print_space(execution_tracing.calling_depth), | ||
execution_tracing.calling_depth, | ||
gas_used_p2, | ||
time_used, | ||
full_func_name, | ||
current_gas_balance | ||
)); | ||
} | ||
} else { | ||
let time_used: u64 = current_timestamp_millis.saturating_sub(timestamp); | ||
execution_tracing.log.push(format!( | ||
"{}{}.gas_used: {:}, time_used {:?} -> {}, balance {:}", | ||
generate_print_space(execution_tracing.calling_depth), | ||
execution_tracing.calling_depth, | ||
gas_used_p1, | ||
time_used, | ||
full_func_name, | ||
current_gas_balance | ||
)); | ||
} | ||
} else { | ||
execution_tracing.log.push(format!( | ||
"{}{}.gas_used: {:}, time_used {:?} -> {}, balance {:}", | ||
generate_print_space(execution_tracing.calling_depth), | ||
execution_tracing.calling_depth, | ||
0, | ||
timestamp, | ||
full_func_name, | ||
current_gas_balance | ||
)); | ||
} | ||
|
||
execution_tracing.calling_depth -= 1; | ||
|
||
Ok(NativeResult::Success { | ||
cost: InternalGas::zero(), | ||
ret_vals: smallvec![Value::u64(0), Value::u64(0)], | ||
}) | ||
} else { | ||
execution_tracing.calling_depth += 1; | ||
execution_tracing.log.push(format!( | ||
"{}{}.call {:} balance {:}", | ||
generate_print_space(execution_tracing.calling_depth), | ||
execution_tracing.calling_depth, | ||
full_func_name, | ||
current_gas_balance | ||
)); | ||
Ok(NativeResult::Success { | ||
cost: InternalGas::zero(), | ||
ret_vals: smallvec![ | ||
Value::u64(current_gas_balance.into()), | ||
Value::u64(current_timestamp_millis) | ||
], | ||
}) | ||
} | ||
} | ||
|
||
pub fn make_all(gas_params: MeasurementGas) -> impl Iterator<Item = (String, NativeFunction)> { | ||
let natives = [( | ||
"inject_parameter", | ||
make_native(gas_params, inject_parameter), | ||
)]; | ||
|
||
make_module_natives(natives) | ||
} |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
The NativeContext is initialized each time a native function is called, a mechanism is needed to maintain gas statistics throughout the entire transaction execution process.
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.
Custom a Native context like
NativeEventContext
orObjectRuntimeContext
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.
OK, got it.