Skip to content

Commit 6e565f7

Browse files
committed
Rework JobCores in order to pin v8 instance threads
1 parent f7a2201 commit 6e565f7

12 files changed

Lines changed: 537 additions & 535 deletions

File tree

crates/core/src/host/host_controller.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::db::persistence::PersistenceProvider;
88
use crate::db::relational_db::{self, spawn_view_cleanup_loop, DiskSizeFn, RelationalDB, Txdata};
99
use crate::db::{self, spawn_tx_metrics_recorder};
1010
use crate::energy::{EnergyMonitor, EnergyQuanta, NullEnergyMonitor};
11-
use crate::host::module_host::ModuleRuntime as _;
1211
use crate::host::v8::V8Runtime;
1312
use crate::host::ProcedureCallError;
1413
use crate::messages::control_db::{Database, HostType};
@@ -18,7 +17,7 @@ use crate::subscription::module_subscription_actor::ModuleSubscriptions;
1817
use crate::subscription::module_subscription_manager::{spawn_send_worker, SubscriptionManager, TransactionOffset};
1918
use crate::subscription::row_list_builder_pool::BsatnRowListBuilderPool;
2019
use crate::util::asyncify;
21-
use crate::util::jobs::{JobCores, SingleCoreExecutor};
20+
use crate::util::jobs::{AllocatedJobCore, JobCores};
2221
use crate::worker_metrics::WORKER_METRICS;
2322
use anyhow::{anyhow, bail, Context};
2423
use async_trait::async_trait;
@@ -702,39 +701,41 @@ async fn make_module_host(
702701
program: Program,
703702
energy_monitor: Arc<dyn EnergyMonitor>,
704703
unregister: impl Fn() + Send + Sync + 'static,
705-
executor: SingleCoreExecutor,
704+
core: AllocatedJobCore,
706705
) -> anyhow::Result<(Program, ModuleHost)> {
707706
// `make_actor` is blocking, as it needs to compile the wasm to native code,
708707
// which may be computationally expensive - sometimes up to 1s for a large module.
709708
// TODO: change back to using `spawn_rayon` here - asyncify runs on tokio blocking
710709
// threads, but those aren't for computation. Also, wasmtime uses rayon
711710
// to run compilation in parallel, so it'll need to run stuff in rayon anyway.
712-
asyncify(move || {
713-
let database_identity = replica_ctx.database_identity;
711+
let database_identity = replica_ctx.database_identity;
714712

715-
let mcc = ModuleCreationContext {
716-
replica_ctx,
717-
scheduler,
718-
program: &program,
719-
energy_monitor,
720-
};
713+
let mcc = ModuleCreationContext {
714+
replica_ctx,
715+
scheduler,
716+
program_hash: program.hash,
717+
energy_monitor,
718+
};
721719

722-
let start = Instant::now();
723-
let module_host = match host_type {
724-
HostType::Wasm => {
725-
let (actor, init_inst) = runtimes.wasmtime.make_actor(mcc)?;
720+
match host_type {
721+
HostType::Wasm => {
722+
asyncify(move || {
723+
let start = Instant::now();
724+
let module = runtimes.wasmtime.make_actor(mcc, &program.bytes, core)?;
726725
trace!("wasmtime::make_actor blocked for {:?}", start.elapsed());
727-
ModuleHost::new(actor, init_inst, unregister, executor, database_identity)
728-
}
729-
HostType::Js => {
730-
let (actor, init_inst) = runtimes.v8.make_actor(mcc)?;
731-
trace!("v8::make_actor blocked for {:?}", start.elapsed());
732-
ModuleHost::new(actor, init_inst, unregister, executor, database_identity)
733-
}
734-
};
735-
Ok((program, module_host))
736-
})
737-
.await
726+
let module_host = ModuleHost::new(module, unregister, database_identity);
727+
Ok((program, module_host))
728+
})
729+
.await
730+
}
731+
HostType::Js => {
732+
let start = Instant::now();
733+
let module = runtimes.v8.make_actor(mcc, &program.bytes, core).await?;
734+
trace!("v8::make_actor blocked for {:?}", start.elapsed());
735+
let module_host = ModuleHost::new(module, unregister, database_identity);
736+
Ok((program, module_host))
737+
}
738+
}
738739
}
739740

740741
async fn load_program(storage: &ProgramStorage, hash: Hash) -> anyhow::Result<Program> {
@@ -762,7 +763,7 @@ async fn launch_module(
762763
energy_monitor: Arc<dyn EnergyMonitor>,
763764
module_logs: Option<ModuleLogsDir>,
764765
runtimes: Arc<HostRuntimes>,
765-
executor: SingleCoreExecutor,
766+
core: AllocatedJobCore,
766767
bsatn_rlb_pool: BsatnRowListBuilderPool,
767768
) -> anyhow::Result<(Program, LaunchedModule)> {
768769
let db_identity = database.database_identity;
@@ -780,7 +781,7 @@ async fn launch_module(
780781
program,
781782
energy_monitor.clone(),
782783
on_panic,
783-
executor,
784+
core,
784785
)
785786
.await?;
786787

@@ -1018,7 +1019,7 @@ impl Host {
10181019
page_pool: PagePool,
10191020
database: Database,
10201021
program: Program,
1021-
executor: SingleCoreExecutor,
1022+
core: AllocatedJobCore,
10221023
bsatn_rlb_pool: BsatnRowListBuilderPool,
10231024
) -> anyhow::Result<Arc<ModuleInfo>> {
10241025
let (db, _connected_clients) = RelationalDB::open(
@@ -1042,7 +1043,7 @@ impl Host {
10421043
Arc::new(NullEnergyMonitor),
10431044
None,
10441045
runtimes.clone(),
1045-
executor,
1046+
core,
10461047
bsatn_rlb_pool,
10471048
)
10481049
.await?;
@@ -1076,7 +1077,7 @@ impl Host {
10761077
policy: MigrationPolicy,
10771078
energy_monitor: Arc<dyn EnergyMonitor>,
10781079
on_panic: impl Fn() + Send + Sync + 'static,
1079-
executor: SingleCoreExecutor,
1080+
core: AllocatedJobCore,
10801081
) -> anyhow::Result<UpdateDatabaseResult> {
10811082
let replica_ctx = &self.replica_ctx;
10821083
let (scheduler, scheduler_starter) = Scheduler::open(self.replica_ctx.relational_db.clone());
@@ -1089,7 +1090,7 @@ impl Host {
10891090
program,
10901091
energy_monitor,
10911092
on_panic,
1092-
executor,
1093+
core,
10931094
)
10941095
.await?;
10951096

@@ -1253,7 +1254,7 @@ pub(crate) async fn extract_schema_with_pools(
12531254
initial_program: program.hash,
12541255
};
12551256

1256-
let core = SingleCoreExecutor::in_current_tokio_runtime();
1257+
let core = AllocatedJobCore::default();
12571258
let module_info =
12581259
Host::try_init_in_memory_to_check(runtimes, page_pool, database, program, core, bsatn_rlb_pool).await?;
12591260
// this should always succeed, but sometimes it doesn't

crates/core/src/host/module_common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
wasm_common::{module_host_actor::DescribeError, DESCRIBE_MODULE_DUNDER},
99
Scheduler,
1010
},
11-
module_host_context::ModuleCreationContextLimited,
11+
module_host_context::ModuleCreationContext,
1212
replica_context::ReplicaContext,
1313
};
1414
use spacetimedb_lib::{Identity, RawModuleDef};
@@ -17,7 +17,7 @@ use std::sync::Arc;
1717

1818
/// Builds a [`ModuleCommon`] from a [`RawModuleDef`].
1919
pub fn build_common_module_from_raw(
20-
mcc: ModuleCreationContextLimited,
20+
mcc: ModuleCreationContext,
2121
raw_def: RawModuleDef,
2222
) -> Result<ModuleCommon, ValidationErrors> {
2323
// Perform a bunch of validation on the raw definition.

0 commit comments

Comments
 (0)