From 4251a36842477587d7fc5ef1fb7c60b437dd9587 Mon Sep 17 00:00:00 2001 From: Mike Voronov Date: Fri, 15 Oct 2021 14:26:46 +0300 Subject: [PATCH] decouple instruction tracker to a separate crate (#160) --- Cargo.lock | 5 + Cargo.toml | 1 + air/Cargo.toml | 1 + .../execution_context/context.rs | 4 +- .../execution_context/instructions_tracker.rs | 73 ----------- .../execution_step/execution_context/mod.rs | 2 - .../execution-info-collector/Cargo.toml | 14 +++ .../execution-info-collector/README.md | 3 + .../src/instructions_tracker.rs | 114 ++++++++++++++++++ .../execution-info-collector/src/lib.rs | 19 +++ 10 files changed, 159 insertions(+), 77 deletions(-) delete mode 100644 air/src/execution_step/execution_context/instructions_tracker.rs create mode 100644 crates/air-lib/execution-info-collector/Cargo.toml create mode 100644 crates/air-lib/execution-info-collector/README.md create mode 100644 crates/air-lib/execution-info-collector/src/instructions_tracker.rs create mode 100644 crates/air-lib/execution-info-collector/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 8c73442a..0ab7db60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,7 @@ dependencies = [ name = "air" version = "0.15.0" dependencies = [ + "air-execution-info-collector", "air-interpreter-data", "air-interpreter-interface", "air-log-targets", @@ -41,6 +42,10 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "air-execution-info-collector" +version = "0.1.0" + [[package]] name = "air-interpreter" version = "0.15.0" diff --git a/Cargo.toml b/Cargo.toml index ffea66e9..8ec56b39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "air-interpreter", "avm/server", "crates/air-lib/air-parser", + "crates/air-lib/execution-info-collector", "crates/air-lib/interpreter-data", "crates/air-lib/interpreter-interface", "crates/air-lib/log-targets", diff --git a/air/Cargo.toml b/air/Cargo.toml index b4b79e8c..20e12826 100644 --- a/air/Cargo.toml +++ b/air/Cargo.toml @@ -16,6 +16,7 @@ doctest = false [dependencies] air-parser = { path = "../crates/air-lib/air-parser" } +air-execution-info-collector = { path = "../crates/air-lib/execution-info-collector" } air-interpreter-data = { path = "../crates/air-lib/interpreter-data" } air-interpreter-interface = { path = "../crates/air-lib/interpreter-interface" } air-log-targets = { path = "../crates/air-lib/log-targets" } diff --git a/air/src/execution_step/execution_context/context.rs b/air/src/execution_step/execution_context/context.rs index 497c2185..fe61b95a 100644 --- a/air/src/execution_step/execution_context/context.rs +++ b/air/src/execution_step/execution_context/context.rs @@ -14,12 +14,12 @@ * limitations under the License. */ -use super::InstrTracker; use super::LastErrorDescriptor; use super::LastErrorWithTetraplet; use crate::execution_step::boxed_value::Scalar; use crate::execution_step::boxed_value::Stream; +use air_execution_info_collector::InstructionTracker; use air_interpreter_interface::*; use std::cell::RefCell; @@ -67,7 +67,7 @@ pub(crate) struct ExecutionCtx<'i> { pub met_folds: VecDeque<&'i str>, /// Tracker of all met instructions. - pub tracker: InstrTracker, + pub tracker: InstructionTracker, /// Last call request id that was used as an id for call request in outcome. pub last_call_request_id: u32, diff --git a/air/src/execution_step/execution_context/instructions_tracker.rs b/air/src/execution_step/execution_context/instructions_tracker.rs deleted file mode 100644 index 14684180..00000000 --- a/air/src/execution_step/execution_context/instructions_tracker.rs +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2021 Fluence Labs Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#[derive(Default)] -#[allow(dead_code)] -pub(crate) struct InstrTracker { - pub(crate) ap: ApTracker, - pub(crate) call: CallTracker, - pub(crate) fold: FoldTracker, - pub(crate) match_count: u32, - pub(crate) mismatch_count: u32, - pub(crate) next_count: u32, - pub(crate) null_count: u32, - pub(crate) par: ParTracker, - pub(crate) seq_count: u32, - pub(crate) xor_count: u32, -} - -#[derive(Default)] -#[allow(dead_code)] -pub(crate) struct ApTracker { - pub(crate) seen_count: u32, - pub(crate) executed_count: u32, -} - -#[derive(Default)] -#[allow(dead_code)] -pub(crate) struct CallTracker { - pub(crate) seen_count: u32, - pub(crate) executed_count: u32, -} - -#[derive(Default)] -#[allow(dead_code)] -pub(crate) struct FoldTracker { - pub(crate) seen_scalar_count: u32, - pub(crate) seen_stream_count: u32, -} - -#[derive(Default)] -#[allow(dead_code)] -pub(crate) struct ParTracker { - pub(crate) seen_count: u32, - pub(crate) executed_count: u32, -} - -impl InstrTracker { - pub(crate) fn meet_call(&mut self) { - self.call.seen_count += 1; - } - - #[allow(dead_code)] - pub(crate) fn meet_executed_call(&mut self) { - self.call.executed_count += 1; - } - - pub(crate) fn meet_fold_stream(&mut self) { - self.fold.seen_stream_count += 1; - } -} diff --git a/air/src/execution_step/execution_context/mod.rs b/air/src/execution_step/execution_context/mod.rs index c1dca9ab..118ac141 100644 --- a/air/src/execution_step/execution_context/mod.rs +++ b/air/src/execution_step/execution_context/mod.rs @@ -16,10 +16,8 @@ mod context; mod error_descriptor; -mod instructions_tracker; pub(crate) use context::*; pub use error_descriptor::LastError; pub(crate) use error_descriptor::LastErrorDescriptor; pub(crate) use error_descriptor::LastErrorWithTetraplet; -pub(crate) use instructions_tracker::*; diff --git a/crates/air-lib/execution-info-collector/Cargo.toml b/crates/air-lib/execution-info-collector/Cargo.toml new file mode 100644 index 00000000..e538bab9 --- /dev/null +++ b/crates/air-lib/execution-info-collector/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "air-execution-info-collector" +version = "0.1.0" +description = "Implementation of AIR execution info collector" +authors = ["Fluence Labs"] +edition = "2018" +license = "Apache-2.0" +publish = false +keywords = ["fluence", "air", "webassembly", "programming-language"] +categories = ["wasm"] + +[lib] +name = "air_execution_info_collector" +path = "src/lib.rs" diff --git a/crates/air-lib/execution-info-collector/README.md b/crates/air-lib/execution-info-collector/README.md new file mode 100644 index 00000000..16c8ba07 --- /dev/null +++ b/crates/air-lib/execution-info-collector/README.md @@ -0,0 +1,3 @@ +## AIR execution info collector + +It's important to capture some info about AIR scripts execution and then analyze it. This crate provides implementation of instruction info collector that is intended to collect various info about executed instructions. diff --git a/crates/air-lib/execution-info-collector/src/instructions_tracker.rs b/crates/air-lib/execution-info-collector/src/instructions_tracker.rs new file mode 100644 index 00000000..3e2e3a32 --- /dev/null +++ b/crates/air-lib/execution-info-collector/src/instructions_tracker.rs @@ -0,0 +1,114 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/// Intended to track a number of executed instruction of each type. For instructions that +/// have a corresponding state in data, it tracks number of executed instructions on +/// current peer (executed) and overall number (seen) of met instructions of such type. +#[derive(Default, Debug, PartialEq, Eq)] +pub struct InstructionTracker { + pub ap: ApTracker, + pub call: CallTracker, + pub fold: FoldTracker, + pub match_count: u32, + pub mismatch_count: u32, + pub next_count: u32, + pub null_count: u32, + pub par: ParTracker, + pub seq_count: u32, + pub xor_count: u32, +} + +#[derive(Default, Debug, PartialEq, Eq)] +pub struct ApTracker { + pub seen_count: u32, + pub executed_count: u32, +} + +#[derive(Default, Debug, PartialEq, Eq)] +pub struct CallTracker { + pub seen_count: u32, + pub executed_count: u32, +} + +#[derive(Default, Debug, PartialEq, Eq)] +pub struct FoldTracker { + pub seen_scalar_count: u32, + pub seen_stream_count: u32, +} + +#[derive(Default, Debug, PartialEq, Eq)] +pub struct ParTracker { + pub seen_count: u32, + pub executed_count: u32, +} + +impl InstructionTracker { + pub fn meet_ap(&mut self) { + self.ap.seen_count += 1; + } + + pub fn meet_executed_ap(&mut self) { + self.ap.executed_count += 1; + } + + pub fn meet_call(&mut self) { + self.call.seen_count += 1; + } + + pub fn meet_executed_call(&mut self) { + self.call.executed_count += 1; + } + + pub fn meet_fold_scalar(&mut self) { + self.fold.seen_scalar_count += 1; + } + + pub fn meet_fold_stream(&mut self) { + self.fold.seen_stream_count += 1; + } + + pub fn meet_match(&mut self) { + self.match_count += 1; + } + + pub fn meet_mismatch(&mut self) { + self.mismatch_count += 1; + } + + pub fn meet_next(&mut self) { + self.next_count += 1; + } + + pub fn meet_null(&mut self) { + self.null_count += 1; + } + + pub fn meet_par(&mut self) { + self.par.seen_count += 1; + } + + pub fn meet_executed_par(&mut self) { + self.par.executed_count += 1; + } + + pub fn meet_seq(&mut self) { + self.seq_count += 1; + } + + pub fn meet_xor(&mut self) { + self.xor_count += 1; + } +} diff --git a/crates/air-lib/execution-info-collector/src/lib.rs b/crates/air-lib/execution-info-collector/src/lib.rs new file mode 100644 index 00000000..0889aa5a --- /dev/null +++ b/crates/air-lib/execution-info-collector/src/lib.rs @@ -0,0 +1,19 @@ +/* + * Copyright 2021 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +mod instructions_tracker; + +pub use instructions_tracker::*;