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

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ wasmbin = "0.6"
webbrowser = "1.0.2"
windows-sys = "0.59"
xdg = "2.5"
tikv-jemallocator = { version = "0.6.0", features = ["profiling", "stats"] }
tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats"]}

# Vendor the openssl we rely on, rather than depend on a
# potentially very old system version.
Expand Down
4 changes: 4 additions & 0 deletions crates/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ tracing-subscriber.workspace = true
walkdir.workspace = true
itertools.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = { workspace = true }
tikv-jemalloc-ctl = { workspace = true }

[target.'cfg(target_os = "linux")'.dependencies]
# only try to build these on linux

Expand Down
9 changes: 8 additions & 1 deletion crates/bench/benches/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use criterion::{
Bencher, BenchmarkGroup, Criterion,
};
use lazy_static::lazy_static;
use mimalloc::MiMalloc;
use spacetimedb_bench::{
database::BenchDatabase,
schemas::{create_sequential, u32_u64_str, u32_u64_u64, BenchTable, IndexStrategy, RandomTable},
Expand All @@ -14,9 +13,17 @@ use spacetimedb_lib::sats::AlgebraicType;
use spacetimedb_primitives::ColId;
use spacetimedb_testing::modules::{Csharp, Rust};

#[cfg(target_env = "msvc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

lazy_static! {
static ref RUN_ONE_MILLION: bool = std::env::var("RUN_ONE_MILLION").is_ok();
}
Expand Down
9 changes: 8 additions & 1 deletion crates/bench/benches/special.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use criterion::async_executor::AsyncExecutor;
use criterion::{criterion_group, criterion_main, Criterion, SamplingMode};
use mimalloc::MiMalloc;
use spacetimedb_bench::{
database::BenchDatabase,
schemas::{create_sequential, u32_u64_str, u32_u64_u64, u64_u64_u32, BenchTable, RandomTable},
Expand All @@ -13,9 +12,17 @@ use spacetimedb_testing::modules::{Csharp, ModuleLanguage, Rust};
use std::sync::Arc;
use std::sync::OnceLock;

#[cfg(target_env = "msvc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

fn criterion_benchmark(c: &mut Criterion) {
serialize_benchmarks::<u32_u64_str>(c);
serialize_benchmarks::<u32_u64_u64>(c);
Expand Down
4 changes: 4 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ wasmtime.workspace = true
webbrowser.workspace = true
clap-markdown.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = { workspace = true }
tikv-jemalloc-ctl = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = ["Win32_System_Console"] }

Expand Down
14 changes: 13 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
use std::process::ExitCode;

use clap::{Arg, Command};
use mimalloc::MiMalloc;
use spacetimedb_cli::*;
use spacetimedb_paths::cli::CliTomlPath;
use spacetimedb_paths::{RootDir, SpacetimePaths};

// Note that the standalone server is invoked through standaline/src/main.rs, so you will
// also want to set the allocator there.
#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

#[cfg(target_env = "msvc")]
use mimalloc::MiMalloc;

#[cfg(target_env = "msvc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

Expand Down
4 changes: 4 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ jwks = { package = "spacetimedb-jwks", version = "0.1.3" }
async_cache = "0.3.1"
faststr = "0.2.23"

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = {workspace = true}
tikv-jemalloc-ctl = {workspace = true}

[features]
# Print a warning when doing an unindexed `iter_by_col_range` on a large table.
unindexed_iter_by_col_range_warn = []
Expand Down
52 changes: 52 additions & 0 deletions crates/core/src/worker_metrics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use crate::execution_context::WorkloadType;
use crate::hash::Hash;
use once_cell::sync::Lazy;
Expand All @@ -22,6 +24,21 @@ metrics_group!(
#[labels(replica_id: u64, protocol: str)]
pub websocket_request_msg_size: HistogramVec,

#[name = jemalloc_active_bytes]
#[help = "Number of bytes in jemallocs heap"]
#[labels(node_id: str)]
pub jemalloc_active_bytes: IntGaugeVec,

#[name = jemalloc_allocated_bytes]
#[help = "Number of bytes in use by the application"]
#[labels(node_id: str)]
pub jemalloc_allocated_bytes: IntGaugeVec,

#[name = jemalloc_resident_bytes]
#[help = "Total memory used by jemalloc"]
#[labels(node_id: str)]
pub jemalloc_resident_bytes: IntGaugeVec,

#[name = spacetime_websocket_sent_msg_size_bytes]
#[help = "The size of messages sent to connected sessions"]
#[labels(db: Identity, workload: WorkloadType)]
Expand Down Expand Up @@ -113,3 +130,38 @@ metrics_group!(
);

pub static WORKER_METRICS: Lazy<WorkerMetrics> = Lazy::new(WorkerMetrics::new);

#[cfg(not(target_env = "msvc"))]
use tikv_jemalloc_ctl::{epoch, stats};

use std::sync::Once;
use tokio::{spawn, time::sleep};
static SPAWN_JEMALLOC_GUARD: Once = Once::new();
pub fn spawn_jemalloc_stats(node_id: String) {
#[cfg(not(target_env = "msvc"))]
SPAWN_JEMALLOC_GUARD.call_once(|| {
spawn(async move {
let e = epoch::mib().unwrap();
loop {
e.advance().unwrap();
let allocated = stats::allocated::read().unwrap();
WORKER_METRICS
.jemalloc_allocated_bytes
.with_label_values(&node_id)
.set(allocated as i64);
let resident = stats::resident::read().unwrap();
WORKER_METRICS
.jemalloc_resident_bytes
.with_label_values(&node_id)
.set(resident as i64);
let active = stats::active::read().unwrap();
WORKER_METRICS
.jemalloc_active_bytes
.with_label_values(&node_id)
.set(active as i64);

sleep(Duration::from_secs(10)).await;
}
});
});
}
4 changes: 4 additions & 0 deletions crates/standalone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ tower-http.workspace = true
toml.workspace = true
tracing = { workspace = true, features = ["release_max_level_debug"] }

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = {workspace = true}
tikv-jemalloc-ctl = {workspace = true}

[dev-dependencies]
once_cell.workspace = true
tempfile.workspace = true
7 changes: 7 additions & 0 deletions crates/standalone/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ Example usage:
)
}

#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

fn main() -> anyhow::Result<()> {
// take_hook() returns the default hook in case when a custom one is not set
let orig_hook = panic::take_hook();
Expand Down
2 changes: 2 additions & 0 deletions crates/standalone/src/subcommands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use clap::{Arg, ArgMatches};
use spacetimedb::config::{CertificateAuthority, ConfigFile};
use spacetimedb::db::{Config, Storage};
use spacetimedb::startup::{self, TracingOptions};
use spacetimedb::worker_metrics;
use spacetimedb_client_api::routes::database::DatabaseRoutes;
use spacetimedb_client_api::routes::router;
use spacetimedb_paths::cli::{PrivKeyPath, PubKeyPath};
Expand Down Expand Up @@ -133,6 +134,7 @@ pub async fn exec(args: &ArgMatches) -> anyhow::Result<()> {

let data_dir = Arc::new(data_dir.clone());
let ctx = StandaloneEnv::init(db_config, &certs, data_dir).await?;
worker_metrics::spawn_jemalloc_stats(listen_addr.clone());

let mut db_routes = DatabaseRoutes::default();
db_routes.root_post = db_routes.root_post.layer(DefaultBodyLimit::disable());
Expand Down
Loading