glean_ffi/
memory_distribution.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::os::raw::c_char;
6
7use ffi_support::FfiStr;
8
9use crate::{
10    define_metric, from_raw_int64_array, handlemap_ext::HandleMapExtension, with_glean_value,
11    Lifetime, MemoryUnit, RawInt64Array,
12};
13
14define_metric!(MemoryDistributionMetric => MEMORY_DISTRIBUTION_METRICS {
15    new           -> glean_new_memory_distribution_metric(memory_unit: MemoryUnit),
16    test_get_num_recorded_errors -> glean_memory_distribution_test_get_num_recorded_errors,
17    destroy       -> glean_destroy_memory_distribution_metric,
18    accumulate    -> glean_memory_distribution_accumulate(sample: u64),
19});
20
21#[no_mangle]
22pub extern "C" fn glean_memory_distribution_accumulate_samples(
23    metric_id: u64,
24    raw_samples: RawInt64Array,
25    num_samples: i32,
26) {
27    with_glean_value(|glean| {
28        MEMORY_DISTRIBUTION_METRICS.call_infallible_mut(metric_id, |metric| {
29            // The Kotlin code is sending Long(s), which are 64 bits, as there's
30            // currently no stable UInt type. The positive part of [Int] would not
31            // be enough to represent the values coming in:.
32            // Here Long(s) are handled as i64 and then casted in `accumulate_samples_signed`
33            // to u32.
34            let samples = from_raw_int64_array(raw_samples, num_samples);
35            metric.accumulate_samples_signed(glean, samples);
36        })
37    })
38}
39
40#[no_mangle]
41pub extern "C" fn glean_memory_distribution_test_has_value(
42    metric_id: u64,
43    storage_name: FfiStr,
44) -> u8 {
45    with_glean_value(|glean| {
46        MEMORY_DISTRIBUTION_METRICS.call_infallible(metric_id, |metric| {
47            metric
48                .test_get_value(glean, storage_name.as_str())
49                .is_some()
50        })
51    })
52}
53
54#[no_mangle]
55pub extern "C" fn glean_memory_distribution_test_get_value_as_json_string(
56    metric_id: u64,
57    storage_name: FfiStr,
58) -> *mut c_char {
59    with_glean_value(|glean| {
60        MEMORY_DISTRIBUTION_METRICS.call_infallible(metric_id, |metric| {
61            metric
62                .test_get_value_as_json_string(glean, storage_name.as_str())
63                .unwrap()
64        })
65    })
66}