Skip to content

Commit

Permalink
remove 2 version from lazy-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Aug 17, 2023
1 parent cd9311d commit 63e806d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 74 deletions.
13 changes: 0 additions & 13 deletions lazy-pages/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,13 @@ pub(crate) type GlobalNames = Vec<LimitedStr<'static>>;

#[derive(Debug)]
pub(crate) struct LazyPagesRuntimeContext {
pub version: LazyPagesVersion,
pub page_sizes: PageSizes,
pub global_names: GlobalNames,
pub pages_storage_prefix: Vec<u8>,
}

#[derive(Debug)]
pub(crate) struct LazyPagesExecutionContext {
/// Lazy-pages impl version.
pub version: LazyPagesVersion,
/// Lazy-pages page size.
pub page_sizes: PageSizes,
/// Lazy-pages accesses weights.
Expand Down Expand Up @@ -150,16 +147,6 @@ pub(crate) struct LazyPagesExecutionContext {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LazyPagesVersion {
Version1,
Version2,
}

impl LazyPagesVersion {
pub const fn globals_count(&self) -> usize {
match self {
Self::Version1 => 2,
Self::Version2 => 1,
}
}
}

impl SizeManager for LazyPagesExecutionContext {
Expand Down
19 changes: 4 additions & 15 deletions lazy-pages/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

//! `GlobalsAccessor` realizations for native and wasm runtimes.

use crate::{
common::{Error, GlobalNames},
LazyPagesVersion,
};
use crate::common::{Error, GlobalNames};
use core::any::Any;
use gear_backend_common::{
lazy_pages::{GlobalsAccessError, GlobalsAccessMod, GlobalsAccessor},
Expand All @@ -33,15 +30,8 @@ use sp_wasm_interface::Value;

#[derive(Debug, Clone, Copy)]
pub(crate) enum GlobalNo {
Gas,
}

impl GlobalNo {
pub(crate) fn into_idx(self, version: LazyPagesVersion) -> usize {
match self {
GlobalNo::Gas => 0,
}
}
Gas = 0,
Amount = 1,
}

#[derive(Debug)]
Expand Down Expand Up @@ -118,12 +108,11 @@ fn apply_for_global_internal(
}

pub(crate) unsafe fn apply_for_global(
version: LazyPagesVersion,
globals_ctx: &GlobalsContext,
global_no: GlobalNo,
f: impl FnMut(u64) -> Result<Option<u64>, Error>,
) -> Result<u64, Error> {
let name = globals_ctx.names[global_no.into_idx(version)].as_str();
let name = globals_ctx.names[global_no as usize].as_str();
match globals_ctx.access_mod {
GlobalsAccessMod::WasmRuntime => {
let instance = (globals_ctx.access_ptr as *mut SandboxInstance)
Expand Down
18 changes: 8 additions & 10 deletions lazy-pages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod utils;

use crate::{
common::{ContextError, LazyPagesContext, PagePrefix, PageSizes, WeightNo, Weights},
globals::GlobalsContext,
globals::{GlobalNo, GlobalsContext},
init_flag::InitializationFlag,
};

Expand Down Expand Up @@ -152,7 +152,6 @@ pub fn initialize_for_program(
})?;

let execution_ctx = LazyPagesExecutionContext {
version: runtime_ctx.version,
page_sizes: runtime_ctx.page_sizes,
weights,
wasm_mem_addr,
Expand Down Expand Up @@ -385,7 +384,7 @@ pub(crate) fn reset_init_flag() {

/// Initialize lazy-pages for current thread.
fn init_with_handler<H: UserSignalHandler>(
version: LazyPagesVersion,
_version: LazyPagesVersion,
page_sizes: Vec<u32>,
global_names: Vec<LimitedStr<'static>>,
pages_storage_prefix: Vec<u8>,
Expand Down Expand Up @@ -417,18 +416,17 @@ fn init_with_handler<H: UserSignalHandler>(
return Err(NotSuitablePageSizes);
}

if global_names.len() != version.globals_count() {
return Err(WrongGlobalNamesAmount(
global_names.len(),
version.globals_count(),
));
}
// if global_names.len() != GlobalNo::Amount as usize {
// return Err(WrongGlobalNamesAmount(
// global_names.len(),
// GlobalNo::Amount as usize,
// ));
// }

// Set version even if it has been already set, because it can be changed after runtime upgrade.
LAZY_PAGES_CONTEXT.with(|ctx| {
ctx.borrow_mut()
.set_runtime_context(LazyPagesRuntimeContext {
version,
page_sizes,
global_names,
pages_storage_prefix,
Expand Down
26 changes: 5 additions & 21 deletions lazy-pages/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
common::{Error, GasCharger, LazyPagesExecutionContext, WeightNo},
globals::{self, GlobalNo},
process::{self, AccessHandler},
LazyPagesVersion, LAZY_PAGES_CONTEXT,
LAZY_PAGES_CONTEXT,
};
use gear_backend_common::lazy_pages::Status;
use gear_core::pages::{GearPage, PageDynSize};
Expand Down Expand Up @@ -84,14 +84,7 @@ unsafe fn user_signal_handler_internal(
load_data_cost: ctx.weight(WeightNo::LoadPageDataFromStorage),
};

let gas_counter = match ctx.version {
LazyPagesVersion::Version1 => {
globals::apply_for_global(ctx.version, globals_config, GlobalNo::Gas, |_| Ok(None))?
}
LazyPagesVersion::Version2 => {
globals::apply_for_global(ctx.version, globals_config, GlobalNo::Gas, |_| Ok(None))?
}
};
let gas_counter = globals::apply_for_global(globals_config, GlobalNo::Gas, |_| Ok(None))?;

Some((gas_counter, gas_charger))
} else {
Expand Down Expand Up @@ -183,18 +176,9 @@ impl AccessHandler for SignalAccessHandler {
if let (Some((gas_counter, _)), Some(globals_config)) =
(self.gas_ctx, ctx.globals_context.as_ref())
{
match ctx.version {
LazyPagesVersion::Version1 => unsafe {
globals::apply_for_global(ctx.version, globals_config, GlobalNo::Gas, |_| {
Ok(Some(gas_counter))
})?;
},
LazyPagesVersion::Version2 => unsafe {
globals::apply_for_global(ctx.version, globals_config, GlobalNo::Gas, |_| {
Ok(Some(gas_counter))
})?;
},
}
unsafe {
globals::apply_for_global(globals_config, GlobalNo::Gas, |_| Ok(Some(gas_counter)))?
};
}
Ok(())
}
Expand Down
16 changes: 1 addition & 15 deletions runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,13 @@ pub trait GearRI {
.unwrap_or_else(|err| unreachable!("Cannot get lazy-pages status: {err}")),)
}

fn init_lazy_pages(ctx: LazyPagesRuntimeContext) -> bool {
use lazy_pages::LazyPagesVersion;

lazy_pages::init(
LazyPagesVersion::Version1,
ctx.page_sizes,
ctx.global_names,
ctx.pages_storage_prefix,
)
.map_err(|err| log::error!("Cannot initialize lazy-pages: {}", err))
.is_ok()
}

/// Init lazy-pages.
/// Returns whether initialization was successful.
#[version(2)]
fn init_lazy_pages(ctx: LazyPagesRuntimeContext) -> bool {
use lazy_pages::LazyPagesVersion;

lazy_pages::init(
LazyPagesVersion::Version2,
LazyPagesVersion::Version1,
ctx.page_sizes,
ctx.global_names,
ctx.pages_storage_prefix,
Expand Down

0 comments on commit 63e806d

Please sign in to comment.