Compare commits

...
9 changed files with 153 additions and 68 deletions

No files matched your search

Generated
+27 -7
View File
@@ -991,7 +991,6 @@ dependencies = [
"figment",
"futures",
"governor",
"hardened_malloc-rs",
"http",
"http-body-util",
"hyper-util",
@@ -999,8 +998,10 @@ dependencies = [
"lettre",
"libc",
"libloading 0.9.1",
"libmimalloc-sys",
"lock_api 0.5.0",
"maplit",
"mimalloc",
"nix",
"num-traits",
"openidconnect",
@@ -1552,6 +1553,12 @@ dependencies = [
"subtle",
]
[[package]]
name = "cty"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]]
name = "curve25519-dalek"
version = "5.0.0"
@@ -2395,12 +2402,6 @@ dependencies = [
"tokio-util",
]
[[package]]
name = "hardened_malloc-rs"
version = "0.1.2+12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "647deb1583b14d160f85f3ff626f20b6edd366e3852c9843b06077388f794cb6"
[[package]]
name = "hashbrown"
version = "0.12.3"
@@ -3256,6 +3257,16 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "libmimalloc-sys"
version = "0.1.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9"
dependencies = [
"cc",
"cty",
]
[[package]]
name = "libz-sys"
version = "1.1.32"
@@ -3429,6 +3440,15 @@ dependencies = [
"walkdir",
]
[[package]]
name = "mimalloc"
version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d4139bb28d14ad1facf21d5eb8825051b326e172d216b39f6d31df53cc97862"
dependencies = [
"libmimalloc-sys",
]
[[package]]
name = "mime"
version = "0.3.18"
+8 -9
View File
@@ -522,15 +522,6 @@ features = ["resource"]
version = "0.5.0"
default-features = false
[workspace.dependencies.hardened_malloc-rs]
version = "0.1.2"
default-features = false
features = [
"static",
"gcc",
"light",
]
[workspace.dependencies.rustyline-async]
version = "0.4.9"
default-features = false
@@ -610,6 +601,14 @@ features = ["unstable-msc4188"]
[workspace.dependencies.serde_urlencoded]
version = "0.7.1"
[workspace.dependencies.mimalloc]
version = "0.1.52"
features = ["extended"]
[workspace.dependencies.libmimalloc-sys]
version = "0.1.49"
features = ["extended"]
#
# Patches
#
+8 -7
View File
@@ -26,14 +26,15 @@ conduwuit_mods = [
gzip_compression = [
"reqwest/gzip",
]
hardened_malloc = [
"dep:hardened_malloc-rs"
]
jemalloc = [
"dep:tikv-jemalloc-sys",
"dep:tikv-jemalloc-ctl",
"dep:tikv-jemallocator",
]
mimalloc = [
"dep:libmimalloc-sys",
"dep:mimalloc",
]
jemalloc_conf = []
jemalloc_prof = [
"tikv-jemalloc-sys/profiling",
@@ -118,14 +119,14 @@ lock_api.workspace = true
hyper-util.workspace = true
resolvematrix.workspace = true
openidconnect.workspace = true
libmimalloc-sys.optional = true
libmimalloc-sys.workspace = true
mimalloc.optional = true
mimalloc.workspace = true
[target.'cfg(unix)'.dependencies]
nix.workspace = true
[target.'cfg(all(not(target_env = "msvc"), target_os = "linux"))'.dependencies]
hardened_malloc-rs.workspace = true
hardened_malloc-rs.optional = true
[dev-dependencies]
maplit.workspace = true
opentelemetry_sdk.workspace = true
-18
View File
@@ -1,18 +0,0 @@
//! hmalloc allocator
#[global_allocator]
static HMALLOC: hardened_malloc_rs::HardenedMalloc = hardened_malloc_rs::HardenedMalloc;
pub fn trim<I: Into<Option<usize>>>(_: I) -> crate::Result { Ok(()) }
#[must_use]
//TODO: get usage
pub fn memory_usage() -> Option<String> { None }
#[must_use]
pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { None }
#[must_use]
pub fn memory_stats(_opts: &str) -> Option<String> {
Some("Extended statistics are not available from hardened_malloc.".to_owned())
}
+87
View File
@@ -0,0 +1,87 @@
//! mimalloc allocator
use std::ptr;
use libmimalloc_sys as ffi;
use mimalloc::MiMalloc;
#[global_allocator]
static MIMALLOC: MiMalloc = MiMalloc;
pub fn trim<I: Into<Option<usize>>>(_: I) -> crate::Result {
// SAFETY: `mi_collect` is thread-safe and takes a single boolean flag.
unsafe {
ffi::mi_collect(true);
}
Ok(())
}
#[must_use]
pub fn memory_usage() -> Option<String> {
let mut current_rss = 0;
let mut peak_rss = 0;
let mut current_commit = 0;
let mut peak_commit = 0;
// SAFETY: All non-null out-pointers are valid writable `usize` stack slots;
// null pointers are explicitly permitted by `mi_process_info`.
unsafe {
ffi::mi_process_info(
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
&raw mut current_rss,
&raw mut peak_rss,
&raw mut current_commit,
&raw mut peak_commit,
ptr::null_mut(),
);
}
let mibs = |input: usize| {
let whole = u32::try_from(input / (1024 * 1024)).unwrap_or(u32::MAX);
let rem_kibs = u32::try_from((input / 1024) % 1024).unwrap_or_default();
f64::from(whole) + f64::from(rem_kibs) / 1024.0
};
Some(format!(
"current_rss: {:.2} MiB\npeak_rss: {:.2} MiB\ncurrent_commit: {:.2} MiB\npeak_commit: \
{:.2} MiB\n",
mibs(current_rss),
mibs(peak_rss),
mibs(current_commit),
mibs(peak_commit),
))
}
#[must_use]
pub fn memory_usage_stats() -> Option<[(&'static str, usize); 6]> { None }
#[must_use]
pub fn memory_stats(_opts: &str) -> Option<String> {
MiMalloc::stats_json()
.ok()
.and_then(|stats| stats.to_str().ok().map(str::to_owned))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn trim_succeeds() { trim(None).expect("trim(None)"); }
#[test]
fn memory_usage_returns_rss_and_commit() {
let usage = memory_usage().expect("memory_usage");
assert!(usage.contains("current_rss:"), "unexpected usage: {usage}");
assert!(usage.contains("current_commit:"), "unexpected usage: {usage}");
}
#[test]
fn memory_stats_returns_json() {
let stats = memory_stats("").expect("memory_stats");
assert!(stats.starts_with('{'), "expected JSON object, got: {stats}");
}
}
+8 -16
View File
@@ -6,22 +6,14 @@ pub mod je;
#[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))]
pub use je::{memory_stats, memory_usage, memory_usage_stats, trim};
#[cfg(all(not(target_env = "msvc"), feature = "hardened_malloc", not(feature = "jemalloc")))]
pub mod hardened;
#[cfg(all(
not(target_env = "msvc"),
feature = "hardened_malloc",
not(feature = "jemalloc")
))]
pub use hardened::{memory_stats, memory_usage, memory_usage_stats, trim};
// mimalloc
#[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))]
pub mod mimalloc;
#[cfg(all(feature = "mimalloc", any(target_env = "msvc", not(feature = "jemalloc"))))]
pub use mimalloc::{memory_stats, memory_usage, memory_usage_stats, trim};
#[cfg(any(
target_env = "msvc",
all(not(feature = "hardened_malloc"), not(feature = "jemalloc"))
))]
// default
#[cfg(all(not(feature = "mimalloc"), any(target_env = "msvc", not(feature = "jemalloc"))))]
pub mod default;
#[cfg(any(
target_env = "msvc",
all(not(feature = "hardened_malloc"), not(feature = "jemalloc"))
))]
#[cfg(all(not(feature = "mimalloc"), any(target_env = "msvc", not(feature = "jemalloc"))))]
pub use default::{memory_stats, memory_usage, memory_usage_stats, trim};
+5 -5
View File
@@ -5,7 +5,7 @@ use figment::Figment;
use ruma::{OwnedServerName, events::room::server_acl::RoomServerAclEventContent};
use super::DEPRECATED_KEYS;
use crate::{Config, Err, Result, Server, debug, debug_info, debug_warn, error, warn};
use crate::{Config, Err, Result, Server, debug, debug_info, error, warn};
/// Performs check() with additional checks specific to reloading old config
/// with new config.
@@ -43,10 +43,10 @@ pub fn check(config: &Config) -> Result {
));
}
if cfg!(all(feature = "hardened_malloc", feature = "jemalloc", not(target_env = "msvc"))) {
debug_warn!(
"hardened_malloc and jemalloc compile-time features are both enabled, this causes \
jemalloc to be used."
if cfg!(all(feature = "mimalloc", feature = "jemalloc", not(target_env = "msvc"))) {
warn!(
"mimalloc and jemalloc compile-time features are both enabled, this causes jemalloc \
to be used."
);
}
+7 -4
View File
@@ -70,7 +70,6 @@ standard = [
]
full = [
"standard",
# "hardened_malloc", # Conflicts with jemalloc
"jemalloc_prof",
"perf_measurements",
"tokio_console",
@@ -96,9 +95,6 @@ gzip_compression = [
"conduwuit-router/gzip_compression",
"conduwuit-service/gzip_compression",
]
hardened_malloc = [
"conduwuit-core/hardened_malloc",
]
http3 = [
"conduwuit-service/http3"
]
@@ -116,6 +112,13 @@ jemalloc = [
"conduwuit-core/jemalloc",
"conduwuit-service/jemalloc",
]
# Sets Rust's #[global_allocator] to mimalloc (C/C++ libraries such as RocksDB
# still use system libc malloc). On non-MSVC targets, `jemalloc` takes
# precedence if both are enabled, so build with `--no-default-features` and
# omit `jemalloc`/`standard` to use mimalloc.
mimalloc = [
"conduwuit-core/mimalloc",
]
jemalloc_prof = [
"conduwuit-core/jemalloc_prof",
]
+3 -2
View File
@@ -1,6 +1,7 @@
#[cfg(feature = "console")]
use std::io::IsTerminal;
use conduwuit::{Err, Result, debug, debug_info, error, info, warn};
use conduwuit::{Err, Result, debug, debug_info, error, info};
use ruma::events::room::message::RoomMessageEventContent;
use tokio::time::{Duration, sleep};
@@ -15,7 +16,7 @@ impl super::Service {
#[cfg(feature = "console")]
if self.services.server.config.admin_console_automatic {
if !std::io::stdin().is_terminal() {
warn!("Console enabled without a tty available; Not enabling console");
conduwuit::warn!("Console enabled without a tty available; Not enabling console");
return;
}