| /* SPDX-License-Identifier: Apache-2.0 |
| * Copyright(c) 2022 Cisco Systems, Inc. |
| */ |
| |
| #include <vlib/vlib.h> |
| #include <vlib/unix/unix.h> |
| #include <vlib/stats/stats.h> |
| |
| static clib_mem_heap_t **memory_heaps_vec; |
| |
| enum |
| { |
| STAT_MEM_TOTAL = 0, |
| STAT_MEM_USED, |
| STAT_MEM_FREE, |
| STAT_MEM_USED_MMAP, |
| STAT_MEM_TOTAL_ALLOC, |
| STAT_MEM_FREE_CHUNKS, |
| STAT_MEM_RELEASABLE, |
| } stat_mem_usage_e; |
| |
| /* |
| * Called from the stats periodic process to update memory counters. |
| */ |
| static void |
| stat_provider_mem_usage_update_fn (vlib_stats_collector_data_t *d) |
| { |
| clib_mem_usage_t usage; |
| clib_mem_heap_t *heap; |
| counter_t **counters = d->entry->data; |
| counter_t *cb; |
| |
| heap = vec_elt (memory_heaps_vec, d->private_data); |
| clib_mem_get_heap_usage (heap, &usage); |
| cb = counters[0]; |
| cb[STAT_MEM_TOTAL] = usage.bytes_total; |
| cb[STAT_MEM_USED] = usage.bytes_used; |
| cb[STAT_MEM_FREE] = usage.bytes_free; |
| cb[STAT_MEM_USED_MMAP] = usage.bytes_used_mmap; |
| cb[STAT_MEM_TOTAL_ALLOC] = usage.bytes_max; |
| cb[STAT_MEM_FREE_CHUNKS] = usage.bytes_free_reclaimed; |
| cb[STAT_MEM_RELEASABLE] = usage.bytes_overhead; |
| } |
| |
| /* |
| * Provide memory heap counters. |
| * Two dimensional array of heap index and per-heap gauges. |
| */ |
| void |
| vlib_stats_register_mem_heap (clib_mem_heap_t *heap) |
| { |
| vlib_stats_collector_reg_t r = {}; |
| u32 idx; |
| |
| vec_add1 (memory_heaps_vec, heap); |
| |
| r.entry_index = idx = vlib_stats_add_counter_vector ("/mem/%s", heap->name); |
| vlib_stats_validate (idx, 0, STAT_MEM_RELEASABLE); |
| |
| /* Create symlink */ |
| vlib_stats_add_symlink (idx, STAT_MEM_USED, "/mem/%s/used", heap->name); |
| vlib_stats_add_symlink (idx, STAT_MEM_TOTAL, "/mem/%s/total", heap->name); |
| vlib_stats_add_symlink (idx, STAT_MEM_FREE, "/mem/%s/free", heap->name); |
| |
| r.private_data = vec_len (memory_heaps_vec) - 1; |
| r.collect_fn = stat_provider_mem_usage_update_fn; |
| vlib_stats_register_collector_fn (&r); |
| } |