Skip to content

Commit

Permalink
Add memory metrics to console api.
Browse files Browse the repository at this point in the history
  • Loading branch information
dtzxporter committed Jun 27, 2024
1 parent b4e8430 commit f470aaf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

### Fixed

# 0.1.29

### Added
- Added memory statistics to the console api.

# 0.1.28

### Changed
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sha2 = "0.10"
pin-project-lite = "0.2"
bincode = "2.0.0-rc.3"
pingora-timeout = "0.2"
memory-stats = "1.2"
smallvec = "1.13"
arc-swap = "1.7"
bytes = { version = "1.0", default-features = false }
Expand All @@ -32,8 +33,8 @@ tracing = { version = "0.1", default-features = false, features = ["std"] }
tracing-subscriber = "0.3"
dashmap = "6.0.1"

hydra-macros = { version = "0.1.28", path = "./hydra-macros" }
hydra = { version = "0.1.28", path = "./hydra", default-features = false }
hydra-macros = { version = "0.1.29", path = "./hydra-macros" }
hydra = { version = "0.1.29", path = "./hydra", default-features = false }

[profile.release]
lto = "fat"
Expand Down
2 changes: 1 addition & 1 deletion hydra-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hydra-macros"
version = "0.1.28"
version = "0.1.29"
edition = "2021"
license.workspace = true
repository.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion hydra-websockets/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hydra-websockets"
version = "0.1.28"
version = "0.1.29"
edition = "2021"
readme = "./README.md"
license.workspace = true
Expand Down
6 changes: 4 additions & 2 deletions hydra/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hydra"
version = "0.1.28"
version = "0.1.29"
edition = "2021"
readme.workspace = true
license.workspace = true
Expand All @@ -13,7 +13,7 @@ description.workspace = true
default = ["macros", "tracing"]
tracing = ["dep:tracing", "dep:tracing-subscriber"]
macros = ["dep:hydra-macros"]
console = []
console = ["dep:memory-stats"]

[dependencies]
flume.workspace = true
Expand All @@ -37,3 +37,5 @@ hydra-macros = { workspace = true, optional = true }

tracing = { workspace = true, optional = true }
tracing-subscriber = { workspace = true, optional = true }

memory-stats = { workspace = true, optional = true }
18 changes: 18 additions & 0 deletions hydra/src/runtime_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,38 @@ pub struct RuntimeInfo {
pub worker_threads: usize,
/// The number of processes currently running.
pub processes: usize,
/// The "physical" memory used by this process, in bytes.
/// This corresponds to the following
/// metric on each platform:
/// - **Linux, Android, MacOS, iOS**: Resident Set Size.
/// - **Windows**: Working Set.
pub physical_memory: usize,
/// The "virtual" memory used by this process, in bytes.
/// This corresponds to the following
/// metric on each platform:
/// - **Linux, Android, MacOS, iOS**: Virtual Size.
/// - **Windows**: Pagefile Usage.
pub virtual_memory: usize,
}

impl RuntimeInfo {
/// Constructs and loads [RuntimeInfo] data, must be called from within the runtime.
pub(super) fn load() -> Self {
let runtime = Handle::current();

let (physical_memory, virtual_memory) = memory_stats::memory_stats()
.map(|stats| (stats.physical_mem, stats.virtual_mem))
.unwrap_or_default();

Self {
system_version: String::from(env!("CARGO_PKG_VERSION")),
logical_cpus: std::thread::available_parallelism()
.map(|cpus| cpus.get())
.unwrap_or_default(),
worker_threads: runtime.metrics().num_workers(),
processes: process_len(),
physical_memory,
virtual_memory,
}
}
}

0 comments on commit f470aaf

Please sign in to comment.