Skip to content

Commit

Permalink
Add new metrics to monitor memory (#2650)
Browse files Browse the repository at this point in the history
* Add new metrocs to monitor memory

* Remove file

* Import page size constant and multiply integers

* convert to u64

* fix type
  • Loading branch information
lmuntaner authored Oct 15, 2024
1 parent f88237c commit 1f69be6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/archive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
use candid::{CandidType, Deserialize, Principal};
use ic_cdk::api::call::CallResult;
use ic_cdk::api::management_canister::main::{canister_status, CanisterIdRecord};
use ic_cdk::api::stable::stable_size;
use ic_cdk::api::stable::{stable_size, WASM_PAGE_SIZE_IN_BYTES};
use ic_cdk::api::time;
use ic_cdk::{call, caller, id, print, trap};
use ic_cdk_macros::{init, post_upgrade, query, update};
Expand Down Expand Up @@ -634,6 +634,23 @@ fn encode_metrics(w: &mut MetricsEncoder<Vec<u8>>) -> std::io::Result<()> {
stable_size() as f64,
"Number of stable memory pages used by this canister.",
)?;
w.encode_gauge(
"ii_archive_stable_memory_bytes",
(stable_size() * WASM_PAGE_SIZE_IN_BYTES) as f64,
"Size of the stable memory allocated by this canister.",
)?;
#[cfg(target_arch = "wasm32")]
w.encode_gauge(
"ii_archive_heap_pages",
core::arch::wasm32::memory_size::<0>() as f64,
"Number of heap memory pages used by this canister.",
)?;
#[cfg(target_arch = "wasm32")]
w.encode_gauge(
"ii_archive_heap_memory_bytes",
(core::arch::wasm32::memory_size::<0>() as u64 * WASM_PAGE_SIZE_IN_BYTES) as f64,
"Size of the heap memory allocated by this canister.",
)?;
with_call_info(|call_info| {
if let Some(successful_fetch) = &call_info.last_successful_fetch {
w.encode_gauge(
Expand Down
33 changes: 33 additions & 0 deletions src/archive/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ mod metrics_tests {
"ii_archive_virtual_memory_pages{kind=\"log_data\"}",
"ii_archive_virtual_memory_pages{kind=\"anchor_index\"}",
"ii_archive_stable_memory_pages",
"ii_archive_stable_memory_bytes",
"ii_archive_heap_pages",
"ii_archive_heap_memory_bytes",
// The metrics
// * ii_archive_last_successful_fetch_timestamp_seconds
// * ii_archive_last_successful_fetch_entries_count
Expand Down Expand Up @@ -669,6 +672,21 @@ mod metrics_tests {
"ii_archive_stable_memory_pages",
3074f64, // the memory_manager pre-allocates a lot of memory (1024 page buckets per virtual memory and some overhead)
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_stable_memory_bytes",
3074f64 * WASM_PAGE_SIZE as f64, // the memory_manager pre-allocates a lot of memory (1024 page buckets per virtual memory and some overhead)
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_heap_pages",
49f64,
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_heap_memory_bytes",
49f64 * WASM_PAGE_SIZE as f64,
);

api::add_entry(
&env,
Expand Down Expand Up @@ -699,6 +717,21 @@ mod metrics_tests {
"ii_archive_stable_memory_pages",
3074f64, // does not change due to pre-allocation
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_stable_memory_bytes",
3074f64 * WASM_PAGE_SIZE as f64, // does not change due to pre-allocation
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_heap_pages", // does not change due to pre-allocation
51f64,
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_heap_memory_bytes", // does not change due to pre-allocation
51f64 * WASM_PAGE_SIZE as f64,
);

Ok(())
}
Expand Down
13 changes: 12 additions & 1 deletion src/internet_identity/src/http/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::stats::activity_stats::ActivityStats;
use crate::stats::event_stats::AggregationWindow::{Day, Month};
use crate::stats::event_stats::{retrieve_aggregation, Aggregation, PD_COUNT, PD_SESS_SEC};
use crate::{state, IC0_APP_DOMAIN, INTERNETCOMPUTER_ORG_DOMAIN};
use ic_cdk::api::stable::stable_size;
use ic_cdk::api::stable::{stable_size, WASM_PAGE_SIZE_IN_BYTES};
use ic_cdk::api::time;
use ic_metrics_encoder::{LabeledMetricsBuilder, MetricsEncoder};
use std::time::Duration;
Expand Down Expand Up @@ -89,12 +89,23 @@ fn encode_metrics(w: &mut MetricsEncoder<Vec<u8>>) -> std::io::Result<()> {
stable_size() as f64,
"Number of stable memory pages used by this canister.",
)?;
w.encode_gauge(
"internet_identity_stable_memory_bytes",
(stable_size() * WASM_PAGE_SIZE_IN_BYTES) as f64,
"Size of the stable memory allocated by this canister.",
)?;
#[cfg(target_arch = "wasm32")]
w.encode_gauge(
"internet_identity_heap_pages",
core::arch::wasm32::memory_size::<0>() as f64,
"Number of heap memory pages used by this canister.",
)?;
#[cfg(target_arch = "wasm32")]
w.encode_gauge(
"internet_identity_heap_memory_bytes",
(core::arch::wasm32::memory_size::<0>() as u64 * WASM_PAGE_SIZE_IN_BYTES) as f64,
"Size of the heap memory allocated by this canister.",
)?;
w.encode_gauge(
"internet_identity_temp_keys_count",
state::with_temp_keys(|temp_keys| temp_keys.num_temp_keys()) as f64,
Expand Down
2 changes: 2 additions & 0 deletions src/internet_identity/tests/integration/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ fn ii_canister_serves_http_metrics() -> Result<(), CallError> {
"internet_identity_max_user_number",
"internet_identity_signature_count",
"internet_identity_stable_memory_pages",
"internet_identity_stable_memory_bytes",
"internet_identity_heap_pages",
"internet_identity_heap_memory_bytes",
"internet_identity_last_upgrade_timestamp",
"internet_identity_inflight_challenges",
"internet_identity_users_in_registration_mode",
Expand Down

0 comments on commit 1f69be6

Please sign in to comment.