Skip to content

Commit

Permalink
Apply malloc_size_of to most types to gather heap-allocated memory
Browse files Browse the repository at this point in the history
This is not yet complete.
It's applied for all metric types and part of the global Glean object.
However it doesn't expose any method yet to get the information about
the global Glean object.
Metrics are generated in user code anyway, so measuring their memory
usage needs codegen.

This also doesn't measure some other memory usage:

* Rkv
* delayed ping data
* event database
* any queues to the dispatcher
* maybe more?
  • Loading branch information
badboy committed Apr 10, 2024
1 parent dd7948b commit f6a81d4
Show file tree
Hide file tree
Showing 27 changed files with 384 additions and 36 deletions.
88 changes: 80 additions & 8 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions glean-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ thiserror = "1.0.4"
uniffi = { version = "0.27.0", default-features = false }
time = "0.1.40"
env_logger = { version = "0.10.0", default-features = false, optional = true }
# the version on crates.io depends on syn v1, but m-c uses a patched on with syn v2
malloc_size_of_derive = "0.1.2"
# crates.io has 0.1.0, m-c has 0.0.2, this makes things highly confusing.
malloc_size_of = { version = "< 1", package = "wr_malloc_size_of" }

[target.'cfg(target_os = "android")'.dependencies]
android_logger = { version = "0.12.0", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions glean-core/rlb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ inherent = "1"
log = "0.4.8"
once_cell = "1.18.0"
whatsys = "0.3.0"
# crates.io has 0.1.0, m-c has 0.0.2, this makes things highly confusing.
malloc_size_of = { version = "< 1", package = "wr_malloc_size_of" }

[dev-dependencies]
crossbeam-channel = "0.5"
Expand Down
20 changes: 19 additions & 1 deletion glean-core/rlb/src/private/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::sync::{Arc, Mutex};
use std::{
mem,
sync::{Arc, Mutex},
};

use malloc_size_of::MallocSizeOf;

type BoxedCallback = Box<dyn FnOnce(Option<&str>) + Send + 'static>;

Expand All @@ -19,6 +24,19 @@ pub struct PingType {
test_callback: Arc<Mutex<Option<BoxedCallback>>>,
}

impl MallocSizeOf for PingType {
fn size_of(&self, ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
self.inner.size_of(ops)
+ self
.test_callback
.lock()
.unwrap()
.as_ref()
.map(|cb| mem::size_of_val(cb))
.unwrap_or(0)
}
}

impl PingType {
/// Creates a new ping type.
///
Expand Down
9 changes: 6 additions & 3 deletions glean-core/src/common_metric_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use std::sync::atomic::{AtomicU8, Ordering};

use malloc_size_of_derive::MallocSizeOf;

use crate::error::{Error, ErrorKind};
use crate::metrics::labeled::validate_dynamic_label;
use crate::Glean;
Expand All @@ -12,7 +14,7 @@ use serde::{Deserialize, Serialize};
/// The supported metrics' lifetimes.
///
/// A metric's lifetime determines when its stored data gets reset.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default, MallocSizeOf)]
#[repr(i32)] // Use i32 to be compatible with our JNA definition
#[serde(rename_all = "lowercase")]
pub enum Lifetime {
Expand Down Expand Up @@ -50,7 +52,7 @@ impl TryFrom<i32> for Lifetime {
}

/// The common set of data shared across all different metric types.
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
#[derive(Default, Debug, Clone, Deserialize, Serialize, MallocSizeOf)]
pub struct CommonMetricData {
/// The metric's name.
pub name: String,
Expand All @@ -73,9 +75,10 @@ pub struct CommonMetricData {
pub dynamic_label: Option<String>,
}

#[derive(Default, Debug)]
#[derive(Default, Debug, MallocSizeOf)]
pub struct CommonMetricDataInternal {
pub inner: CommonMetricData,
#[ignore_malloc_size_of = "atomic not allocated"]
pub disabled: AtomicU8,
}

Expand Down
7 changes: 6 additions & 1 deletion glean-core/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, Mutex};

use chrono::{DateTime, FixedOffset};
use malloc_size_of_derive::MallocSizeOf;
use once_cell::sync::OnceCell;

use crate::database::Database;
Expand Down Expand Up @@ -142,7 +143,7 @@ where
///
/// In specific language bindings, this is usually wrapped in a singleton and all metric recording goes to a single instance of this object.
/// In the Rust core, it is possible to create multiple instances, which is used in testing.
#[derive(Debug)]
#[derive(Debug, MallocSizeOf)]
pub struct Glean {
upload_enabled: bool,
pub(crate) data_store: Option<Database>,
Expand All @@ -151,17 +152,21 @@ pub struct Glean {
pub(crate) additional_metrics: AdditionalMetrics,
pub(crate) database_metrics: DatabaseMetrics,
pub(crate) internal_pings: InternalPings,
#[ignore_malloc_size_of = "libstd type"]
data_path: PathBuf,
application_id: String,
ping_registry: HashMap<String, PingType>,
#[ignore_malloc_size_of = "non-allocating type"]
start_time: DateTime<FixedOffset>,
max_events: u32,
is_first_run: bool,
pub(crate) upload_manager: PingUploadManager,
debug: DebugOptions,
pub(crate) app_build: String,
pub(crate) schedule_metrics_pings: bool,
#[ignore_malloc_size_of = "atomic not allocated"]
pub(crate) remote_settings_epoch: AtomicU8,
#[ignore_malloc_size_of = "TODO"]
pub(crate) remote_settings_metrics_config: Arc<Mutex<MetricsEnabledConfig>>,
pub(crate) with_timestamps: bool,
}
Expand Down
4 changes: 3 additions & 1 deletion glean-core/src/core_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ use crate::metrics::{
};
use crate::{CommonMetricData, Lifetime};

use malloc_size_of_derive::MallocSizeOf;
use once_cell::sync::Lazy;

/// Metrics included in every ping as `client_info`.
#[derive(Debug, Default)]
#[derive(Debug, Default, MallocSizeOf)]
pub struct ClientInfoMetrics {
/// The build identifier generated by the CI system (e.g. "1234/A").
pub app_build: String,
/// The user visible version string (e.g. "1.0.3").
pub app_display_version: String,
/// The app's build date
#[ignore_malloc_size_of = "not an allocated value"]
pub app_build_date: Datetime,

/// The architecture of the device (e.g. "arm", "x86").
Expand Down
Loading

0 comments on commit f6a81d4

Please sign in to comment.