Skip to content
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

Fix memory leak when getting stylus cache metrics #2734

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions arbitrator/stylus/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,28 +275,21 @@ impl InitCache {
cache.long_term_size_bytes = 0;
}

pub fn get_metrics() -> CacheMetrics {
pub fn get_metrics(output: &mut CacheMetrics) {
let mut cache = cache!();

let lru_count = cache.lru.len();
let lru_metrics = LruCacheMetrics {
// adds 1 to each entry to account that we subtracted 1 in the weight calculation
size_bytes: (cache.lru.weight() + lru_count).try_into().unwrap(),

count: lru_count.try_into().unwrap(),

hits: cache.lru_counters.hits,
misses: cache.lru_counters.misses,
does_not_fit: cache.lru_counters.does_not_fit,
};

let long_term_metrics = LongTermCacheMetrics {
size_bytes: cache.long_term_size_bytes.try_into().unwrap(),
count: cache.long_term.len().try_into().unwrap(),

hits: cache.long_term_counters.hits,
misses: cache.long_term_counters.misses,
};
// adds 1 to each entry to account that we subtracted 1 in the weight calculation
output.lru.size_bytes = (cache.lru.weight() + lru_count).try_into().unwrap();
output.lru.count = lru_count.try_into().unwrap();
output.lru.hits = cache.lru_counters.hits;
output.lru.misses = cache.lru_counters.misses;
output.lru.does_not_fit = cache.lru_counters.does_not_fit;

output.long_term.size_bytes = cache.long_term_size_bytes.try_into().unwrap();
output.long_term.count = cache.long_term.len().try_into().unwrap();
output.long_term.hits = cache.long_term_counters.hits;
output.long_term.misses = cache.long_term_counters.misses;

// Empty counters.
// go side, which is the only consumer of this function besides tests,
Expand All @@ -307,11 +300,6 @@ impl InitCache {
does_not_fit: 0,
};
cache.long_term_counters = LongTermCounters { hits: 0, misses: 0 };

CacheMetrics {
lru: lru_metrics,
long_term: long_term_metrics,
}
}

// only used for testing
Expand Down
9 changes: 7 additions & 2 deletions arbitrator/stylus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,14 @@ pub unsafe extern "C" fn stylus_drop_vec(vec: RustBytes) {
}

/// Gets cache metrics.
///
/// # Safety
///
/// `output` must not be null.
#[no_mangle]
pub extern "C" fn stylus_get_cache_metrics() -> CacheMetrics {
InitCache::get_metrics()
pub unsafe extern "C" fn stylus_get_cache_metrics(output: *mut CacheMetrics) {
let output = &mut *output;
InitCache::get_metrics(output);
}

/// Clears lru cache.
Expand Down
6 changes: 4 additions & 2 deletions arbos/programs/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ func SetWasmLruCacheCapacity(capacityBytes uint64) {
}

func UpdateWasmCacheMetrics() {
metrics := C.stylus_get_cache_metrics()
metrics := &C.CacheMetrics{}
C.stylus_get_cache_metrics(metrics)

stylusLRUCacheSizeBytesGauge.Update(int64(metrics.lru.size_bytes))
stylusLRUCacheCountGauge.Update(int64(metrics.lru.count))
Expand Down Expand Up @@ -373,7 +374,8 @@ type WasmCacheMetrics struct {

// Used for testing
func GetWasmCacheMetrics() *WasmCacheMetrics {
metrics := C.stylus_get_cache_metrics()
metrics := &C.CacheMetrics{}
C.stylus_get_cache_metrics(metrics)

return &WasmCacheMetrics{
Lru: WasmLruCacheMetrics{
Expand Down
Loading