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

Code coverge profiling #58

Merged
merged 3 commits into from
Jan 4, 2022
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --features random,profile-gas
args: --verbose --features random,profile-gas,profile-coverage

- name: Run debug tests
uses: actions-rs/cargo@v1
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rand = "0.8"
[features]
debug = []
profile-gas = [ "profile-any" ]
profile-coverage = [ "profile-any" ]
profile-any = [ "dyn-clone" ] # All profiling features should depend on this
random = [ "fuel-types/random", "fuel-tx/random" ]
serde-types = [ "fuel-asm/serde-types", "fuel-types/serde-types", "fuel-tx/serde-types", "serde" ]
Expand All @@ -53,6 +54,11 @@ name = "test-profile-gas"
path = "tests/profile_gas.rs"
required-features = [ "random", "profile-gas" ]

[[test]]
name = "test-code-coverage"
path = "tests/code_coverage.rs"
required-features = [ "random", "profile-coverage" ]

[[test]]
name = "test-encoding"
path = "tests/encoding.rs"
Expand Down
6 changes: 6 additions & 0 deletions src/interpreter/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ impl<S> Interpreter<S> {
pub(crate) fn gas_charge(&mut self, gas: Word) -> Result<(), RuntimeError> {
let gas = !self.is_predicate() as Word * gas;

#[cfg(feature = "profile-coverage")]
{
let location = self.current_location();
self.profiler.data_mut().coverage_mut().set(location);
}

#[cfg(feature = "profile-gas")]
{
let gas_use = gas.min(self.registers[REG_CGAS]);
Expand Down
46 changes: 46 additions & 0 deletions src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ impl fmt::Debug for Profiler {
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ProfilingData {
#[cfg(feature = "profile-coverage")]
coverage: CoverageProfilingData,
#[cfg(feature = "profile-gas")]
gas: GasProfilingData,
}
Expand All @@ -170,6 +172,50 @@ impl ProfilingData {
pub fn gas_mut(&mut self) -> &mut GasProfilingData {
&mut self.gas
}

/// Coverage profiling info, immutable
#[cfg(feature = "profile-coverage")]
pub fn coverage(&self) -> &CoverageProfilingData {
&self.coverage
}

/// Coverage profiling info, mutable
#[cfg(feature = "profile-coverage")]
pub fn coverage_mut(&mut self) -> &mut CoverageProfilingData {
&mut self.coverage
}
}

/// Excuted memory addresses
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CoverageProfilingData {
executed: PerLocation<()>,
}

impl<'a> CoverageProfilingData {
/// Get total gas used at location
pub fn get(&self, location: &InstructionLocation) -> bool {
self.executed.contains_key(location)
}

/// Increase gas used at location
pub fn set(&mut self, location: InstructionLocation) {
self.executed.insert(location, ());
}

/// Iterate through locations
pub fn iter(&'a self) -> PerLocationKeys<'a, ()> {
PerLocationKeys(self.executed.keys())
}
}

impl fmt::Display for CoverageProfilingData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut items: Vec<_> = self.iter().collect();
items.sort();
writeln!(f, "{:?}", items)
}
}

/// Used gas per memory address
Expand Down
94 changes: 94 additions & 0 deletions tests/code_coverage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::sync::{Arc, Mutex};

use fuel_vm::consts::*;
use fuel_vm::prelude::*;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};

use fuel_vm::profiler::{InstructionLocation, ProfileReceiver, ProfilingData};

const HALF_WORD_SIZE: u64 = 4;

#[test]
fn code_coverage() {
let rng = &mut StdRng::seed_from_u64(2322u64);
let salt: Salt = rng.gen();

let gas_price = 1;
let gas_limit = 1_000;
let maturity = 0;

// Deploy contract with loops
let reg_a = 0x20;

let contract_code: Vec<Opcode> = vec![
Opcode::JNEI(REG_ZERO, REG_ONE, 2), // Skip next
Opcode::XOR(reg_a, reg_a, reg_a), // Skipped
Opcode::JNEI(REG_ZERO, REG_ZERO, 2), // Do not skip
Opcode::XOR(reg_a, reg_a, reg_a), // Executed
Opcode::RET(REG_ONE),
];

let program: Witness = contract_code.clone().into_iter().collect::<Vec<u8>>().into();
let contract = Contract::from(program.as_ref());
let contract_root = contract.root();
let contract_id = contract.id(&salt, &contract_root);

let input = Input::contract(rng.gen(), rng.gen(), rng.gen(), contract_id);
let output = Output::contract(0, rng.gen(), rng.gen());

let tx_deploy = Transaction::script(
gas_price,
gas_limit,
maturity,
contract_code.clone().into_iter().collect(),
vec![],
vec![input],
vec![output],
vec![],
);

#[derive(Clone, Default)]
struct ProfilingOutput {
data: Arc<Mutex<Option<ProfilingData>>>,
}

impl ProfileReceiver for ProfilingOutput {
fn on_transaction(&mut self, _state: &Result<ProgramState, InterpreterError>, data: &ProfilingData) {
let mut guard = self.data.lock().unwrap();
*guard = Some(data.clone());
}
}

let output = ProfilingOutput::default();

let mut client = MemoryClient::from_txtor(
Interpreter::with_memory_storage()
.with_profiling(Box::new(output.clone()))
.into(),
);

let receipts = client.transact(tx_deploy);

if let Some(Receipt::ScriptResult { result, .. }) = receipts.last() {
assert!(result.is_success());
} else {
panic!("Missing result receipt");
}

let guard = output.data.lock().unwrap();
let case = guard.as_ref().unwrap().clone();

let mut items: Vec<_> = case.coverage().iter().collect();
items.sort();

let expect = vec![0, 2, 3, 4];

assert_eq!(items.len(), expect.len());

println!("{:?}", items);

for (item, expect) in items.into_iter().zip(expect.into_iter()) {
assert_eq!(*item, InstructionLocation::new(None, expect * HALF_WORD_SIZE));
}
}