Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ic-deterministic-heap-bytes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rust_library(
deps = [
# Keep sorted.
"@crate_index//:candid",
"@crate_index//:tempfile",
],
)

Expand Down
1 change: 1 addition & 0 deletions packages/ic-deterministic-heap-bytes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ path = "src/lib.rs"
candid = { workspace = true }
ic-deterministic-heap-bytes-derive = { path = "../ic-deterministic-heap-bytes-derive" }
paste = { workspace = true }
tempfile = { workspace = true }
17 changes: 17 additions & 0 deletions packages/ic-deterministic-heap-bytes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub trait DeterministicHeapBytes {
}
}

////////////////////////////////////////////////////////////////////////
// Scalar types.

impl DeterministicHeapBytes for u8 {}
impl DeterministicHeapBytes for u16 {}
impl DeterministicHeapBytes for u32 {}
Expand All @@ -46,6 +49,11 @@ impl DeterministicHeapBytes for f64 {}
impl DeterministicHeapBytes for bool {}
impl DeterministicHeapBytes for char {}

////////////////////////////////////////////////////////////////////////
// Standard library types.

impl DeterministicHeapBytes for std::sync::atomic::AtomicU64 {}

impl DeterministicHeapBytes for std::time::Duration {}

impl DeterministicHeapBytes for String {
Expand Down Expand Up @@ -139,7 +147,16 @@ impl<T: DeterministicHeapBytes, E: DeterministicHeapBytes> DeterministicHeapByte
}
}

////////////////////////////////////////////////////////////////////////
// External types.

impl DeterministicHeapBytes for candid::Principal {}
impl DeterministicHeapBytes for tempfile::TempDir {
fn deterministic_heap_bytes(&self) -> usize {
// TempDir allocates a string for the path.
self.path().as_os_str().len()
}
}

#[cfg(test)]
mod tests;
6 changes: 5 additions & 1 deletion rs/embedders/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package(default_visibility = ["//visibility:public"])

DEPENDENCIES = [
# Keep sorted.
"//packages/ic-deterministic-heap-bytes",
"//packages/ic-error-types",
"//rs/config",
"//rs/cycles_account_manager",
Expand Down Expand Up @@ -50,7 +51,10 @@ DEPENDENCIES = [
"@crate_index//:wasmtime-environ",
]

MACRO_DEPENDENCIES = []
MACRO_DEPENDENCIES = [
# Keep sorted.
"//packages/ic-deterministic-heap-bytes-derive",
]

DEV_DEPENDENCIES = [
# Keep sorted.
Expand Down
1 change: 1 addition & 0 deletions rs/embedders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ clap = { workspace = true }
ic-btc-interface = { workspace = true }
ic-config = { path = "../config" }
ic-cycles-account-manager = { path = "../cycles_account_manager" }
ic-deterministic-heap-bytes = { path = "../../packages/ic-deterministic-heap-bytes" }
ic-error-types = { path = "../../packages/ic-error-types" }
ic-interfaces = { path = "../interfaces" }
ic-logger = { path = "../monitoring/logger" }
Expand Down
6 changes: 5 additions & 1 deletion rs/embedders/src/compilation_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
use tempfile::TempDir;

use crate::{OnDiskSerializedModule, SerializedModule};
use ic_deterministic_heap_bytes::DeterministicHeapBytes;
use ic_interfaces::execution_environment::{HypervisorError, HypervisorResult};
use ic_types::{MemoryDiskBytes, NumBytes};
use ic_utils_lru_cache::LruCache;
Expand All @@ -29,11 +30,14 @@ const DEFAULT_MEMORY_CAPACITY: NumBytes = NumBytes::new(10 * GB);

/// Stores the serialized modules of wasm code that has already been compiled so
/// that it can be used again without recompiling.
#[derive(DeterministicHeapBytes)]
pub struct CompilationCache {
/// Directory holding all the temporary files. It will be deleted on
/// drop.
dir: TempDir,
/// Map from wasm hash to an open fd with the serialized Module result.
#[deterministic_heap_bytes(with = |c: &Mutex<LruCache<WasmHash, HypervisorResult<Arc<OnDiskSerializedModule>>>>|
c.lock().unwrap().memory_bytes())]
cache: Mutex<LruCache<WasmHash, HypervisorResult<Arc<OnDiskSerializedModule>>>>,
/// Atomic counter to deduplicate files in the case of concurrent compilations of the same module.
counter: AtomicU64,
Expand All @@ -43,7 +47,7 @@ pub struct CompilationCache {

impl MemoryDiskBytes for CompilationCache {
fn memory_bytes(&self) -> usize {
self.cache.lock().unwrap().memory_bytes()
unreachable!("To be removed in favor of DeterministicHeapBytes")
}

fn disk_bytes(&self) -> usize {
Expand Down
5 changes: 3 additions & 2 deletions rs/execution_environment/src/hypervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ic_canister_sandbox_backend_lib::replica_controller::sandboxed_execution_con
use ic_config::execution_environment::{Config, MAX_COMPILATION_CACHE_SIZE};
use ic_config::flag_status::FlagStatus;
use ic_cycles_account_manager::CyclesAccountManager;
use ic_deterministic_heap_bytes::DeterministicHeapBytes;
use ic_embedders::{
wasm_executor::{WasmExecutionResult, WasmExecutor, WasmExecutorImpl},
wasm_utils::decoding::decoded_wasm_size,
Expand Down Expand Up @@ -166,7 +167,7 @@ impl Hypervisor {
if let Some(compilation_result) = compilation_result {
self.metrics.observe_compilation_metrics(
&compilation_result,
self.compilation_cache.memory_bytes(),
self.compilation_cache.deterministic_heap_bytes(),
self.compilation_cache.disk_bytes(),
);
}
Expand Down Expand Up @@ -409,7 +410,7 @@ impl Hypervisor {
if let Some(compilation_result) = compilation_result {
self.metrics.observe_compilation_metrics(
&compilation_result,
self.compilation_cache.memory_bytes(),
self.compilation_cache.deterministic_heap_bytes(),
self.compilation_cache.disk_bytes(),
);
}
Expand Down
Loading