Skip to content

Commit

Permalink
Log memory usage of the agent and the mapper
Browse files Browse the repository at this point in the history
The default interval is of 60 seconds.

This can be changed using the --log-memory-interval flag
which sets the logging interval in seconds.

```
$ target/debug/tedge-mapper --log-memory-interval 5 c8y
$ sudo target/debug/tedge-agent --log-memory-interval 5
```

Signed-off-by: Didier Wenzek <[email protected]>
  • Loading branch information
didier-wenzek committed Feb 8, 2024
1 parent 4823d86 commit 549d6ec
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ c8y_http_proxy = { path = "crates/extensions/c8y_http_proxy" }
c8y_log_manager = { path = "crates/extensions/c8y_log_manager" }
c8y_mapper_ext = { path = "crates/extensions/c8y_mapper_ext" }
camino = "1.1"
cap = "0.1"
capture-logger = "0.1"
certificate = { path = "crates/common/certificate" }
clap = { version = "4.4", features = ["cargo", "derive"] }
Expand Down
1 change: 1 addition & 0 deletions crates/core/tedge_agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ axum = { workspace = true }
axum-server = { workspace = true }
axum_tls = { workspace = true }
camino = { workspace = true }
cap = { workspace = true }
clap = { workspace = true }
flockfile = { workspace = true }
futures = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/core/tedge_agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ pub struct AgentOpt {
/// MQTT root prefix
#[clap(long)]
pub mqtt_topic_root: Option<Arc<str>>,

/// Interval at which the memory usage is logged (in seconds)
#[clap(long = "log-memory-interval", default_value = "60")]
pub log_memory_interval: u64,
}

pub async fn run(agent_opt: AgentOpt) -> Result<(), anyhow::Error> {
Expand Down
20 changes: 19 additions & 1 deletion crates/core/tedge_agent/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
use cap::Cap;
use clap::Parser;
use std::alloc;
use std::time::Duration;

#[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<()> {
let agent_opt = tedge_agent::AgentOpt::parse();

let log_memory_interval = agent_opt.log_memory_interval;
if log_memory_interval > 0 {
tokio::spawn(async move {
let interval = Duration::from_secs(log_memory_interval);
loop {
log::info!("Allocated memory: {} Bytes", ALLOCATOR.allocated());
tokio::time::sleep(interval).await;
}
});
}

tedge_agent::run(agent_opt).await
}
1 change: 1 addition & 0 deletions crates/core/tedge_mapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ c8y_api = { workspace = true }
c8y_auth_proxy = { workspace = true }
c8y_http_proxy = { workspace = true }
c8y_mapper_ext = { workspace = true }
cap = { workspace = true }
clap = { workspace = true }
clock = { workspace = true }
collectd_ext = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/core/tedge_mapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub struct MapperOpt {
/// WARNING: This is mostly used in testing.
#[clap(long = "config-dir", default_value = DEFAULT_TEDGE_CONFIG_PATH)]
pub config_dir: PathBuf,

/// Interval at which the memory usage is logged (in seconds)
#[clap(long = "log-memory-interval", default_value = "60")]
pub log_memory_interval: u64,
}

#[derive(Debug, clap::Subcommand)]
Expand Down
19 changes: 19 additions & 0 deletions crates/core/tedge_mapper/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
use cap::Cap;
use clap::Parser;
use std::alloc;
use std::time::Duration;
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 log_memory_interval = mapper_opt.log_memory_interval;
if log_memory_interval > 0 {
tokio::spawn(async move {
let interval = Duration::from_secs(log_memory_interval);
loop {
log::info!("Allocated memory: {} Bytes", ALLOCATOR.allocated());
tokio::time::sleep(interval).await;
}
});
}

tedge_mapper::run(mapper_opt).await
}

0 comments on commit 549d6ec

Please sign in to comment.