-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log memory usage of the agent and the mapper #2693
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,22 @@ | |
#![deny(clippy::mem_forget)] | ||
|
||
use anyhow::Context; | ||
use cap::Cap; | ||
use clap::Parser; | ||
use std::alloc; | ||
use std::future::Future; | ||
use std::path::PathBuf; | ||
use std::time::Duration; | ||
use tedge::command::BuildCommand; | ||
use tedge::command::BuildContext; | ||
use tedge::Component; | ||
use tedge::TEdgeOptMulticall; | ||
use tedge_apt_plugin::AptCli; | ||
use tedge_config::system_services::set_log_level; | ||
use tracing::log; | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::MAX); | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let executable_name = executable_name(); | ||
|
@@ -22,10 +29,20 @@ fn main() -> anyhow::Result<()> { | |
|
||
let opt = parse_multicall_if_known(&executable_name); | ||
match opt { | ||
TEdgeOptMulticall::Component(Component::TedgeMapper(mapper_opt)) => { | ||
block_on(tedge_mapper::run(mapper_opt)) | ||
TEdgeOptMulticall::Component(Component::TedgeMapper(opt)) => { | ||
let tedge_config = tedge_config::load_tedge_config(&opt.config_dir)?; | ||
block_on_with( | ||
tedge_config.run.log_memory_interval.duration(), | ||
tedge_mapper::run(opt), | ||
) | ||
Comment on lines
+33
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not very happy with that, i.e. loading the config without passing it to Doing so is not straightforward as each of the actors used by the mapper and the agent have specific way to load the config, with or without a reference to the config-dir and other weirdness including duplicated paths to the same setting. It would be good to simplify all that in a follow up PR. |
||
} | ||
TEdgeOptMulticall::Component(Component::TedgeAgent(opt)) => { | ||
let tedge_config = tedge_config::load_tedge_config(&opt.config_dir)?; | ||
block_on_with( | ||
tedge_config.run.log_memory_interval.duration(), | ||
tedge_agent::run(opt), | ||
) | ||
} | ||
TEdgeOptMulticall::Component(Component::TedgeAgent(opt)) => block_on(tedge_agent::run(opt)), | ||
TEdgeOptMulticall::Component(Component::C8yFirmwarePlugin(fp_opt)) => { | ||
block_on(c8y_firmware_plugin::run(fp_opt)) | ||
} | ||
|
@@ -63,6 +80,23 @@ fn block_on<T>(future: impl Future<Output = T>) -> T { | |
tokio::runtime::Runtime::new().unwrap().block_on(future) | ||
} | ||
|
||
fn block_on_with<T>(log_memory_interval: Duration, future: impl Future<Output = T>) -> T { | ||
if log_memory_interval.is_zero() { | ||
block_on(future) | ||
} else { | ||
block_on(async move { | ||
tokio::spawn(async move { | ||
loop { | ||
log::info!("Allocated memory: {} Bytes", ALLOCATOR.allocated()); | ||
tokio::time::sleep(log_memory_interval).await; | ||
} | ||
}); | ||
|
||
future.await | ||
}) | ||
} | ||
} | ||
|
||
fn executable_name() -> Option<String> { | ||
Some( | ||
PathBuf::from(std::env::args_os().next()?) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
use cap::Cap; | ||
use clap::Parser; | ||
use std::alloc; | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::MAX); | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), anyhow::Error> { | ||
async fn main() -> anyhow::Result<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have to consider deprecating this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is a ticket for that: #2696 |
||
let agent_opt = tedge_agent::AgentOpt::parse(); | ||
let tedge_config = tedge_config::load_tedge_config(&agent_opt.config_dir)?; | ||
let log_memory_interval = tedge_config.run.log_memory_interval.duration(); | ||
if !log_memory_interval.is_zero() { | ||
tokio::spawn(async move { | ||
loop { | ||
log::info!("Allocated memory: {} Bytes", ALLOCATOR.allocated()); | ||
tokio::time::sleep(log_memory_interval).await; | ||
} | ||
}); | ||
} | ||
|
||
tedge_agent::run(agent_opt).await | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,24 @@ | ||
use cap::Cap; | ||
use clap::Parser; | ||
use std::alloc; | ||
use tracing::log; | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::MAX); | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
let mapper_opt = tedge_mapper::MapperOpt::parse(); | ||
let tedge_config = tedge_config::load_tedge_config(&mapper_opt.config_dir)?; | ||
let log_memory_interval = tedge_config.run.log_memory_interval.duration(); | ||
if !log_memory_interval.is_zero() { | ||
tokio::spawn(async move { | ||
loop { | ||
log::info!("Allocated memory: {} Bytes", ALLOCATOR.allocated()); | ||
tokio::time::sleep(log_memory_interval).await; | ||
} | ||
}); | ||
} | ||
|
||
tedge_mapper::run(mapper_opt).await | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side commentary: Came across this sysinfo crate that covers a lot more parameters like CPU usage. Something we can consider in the future, for advanced monitoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose is different. Sysinfo observes the operating system, while here the point is to monitor the amount of memory actually allocated by tedge on the heap.