Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright(c) 2022 Cisco Systems, Inc. |
| 3 | */ |
| 4 | |
| 5 | #include <vlib/vlib.h> |
| 6 | #include <vlib/unix/unix.h> |
| 7 | #include <vlib/stats/stats.h> |
| 8 | |
| 9 | static clib_mem_heap_t **memory_heaps_vec; |
| 10 | |
| 11 | enum |
| 12 | { |
| 13 | STAT_MEM_TOTAL = 0, |
| 14 | STAT_MEM_USED, |
| 15 | STAT_MEM_FREE, |
| 16 | STAT_MEM_USED_MMAP, |
| 17 | STAT_MEM_TOTAL_ALLOC, |
| 18 | STAT_MEM_FREE_CHUNKS, |
| 19 | STAT_MEM_RELEASABLE, |
| 20 | } stat_mem_usage_e; |
| 21 | |
| 22 | /* |
| 23 | * Called from the stats periodic process to update memory counters. |
| 24 | */ |
| 25 | static void |
| 26 | stat_provider_mem_usage_update_fn (vlib_stats_collector_data_t *d) |
| 27 | { |
| 28 | clib_mem_usage_t usage; |
| 29 | clib_mem_heap_t *heap; |
| 30 | counter_t **counters = d->entry->data; |
| 31 | counter_t *cb; |
| 32 | |
| 33 | heap = vec_elt (memory_heaps_vec, d->private_data); |
| 34 | clib_mem_get_heap_usage (heap, &usage); |
| 35 | cb = counters[0]; |
| 36 | cb[STAT_MEM_TOTAL] = usage.bytes_total; |
| 37 | cb[STAT_MEM_USED] = usage.bytes_used; |
| 38 | cb[STAT_MEM_FREE] = usage.bytes_free; |
| 39 | cb[STAT_MEM_USED_MMAP] = usage.bytes_used_mmap; |
| 40 | cb[STAT_MEM_TOTAL_ALLOC] = usage.bytes_max; |
| 41 | cb[STAT_MEM_FREE_CHUNKS] = usage.bytes_free_reclaimed; |
| 42 | cb[STAT_MEM_RELEASABLE] = usage.bytes_overhead; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Provide memory heap counters. |
| 47 | * Two dimensional array of heap index and per-heap gauges. |
| 48 | */ |
| 49 | void |
| 50 | vlib_stats_register_mem_heap (clib_mem_heap_t *heap) |
| 51 | { |
| 52 | vlib_stats_collector_reg_t r = {}; |
| 53 | u32 idx; |
| 54 | |
| 55 | vec_add1 (memory_heaps_vec, heap); |
| 56 | |
| 57 | r.entry_index = idx = vlib_stats_add_counter_vector ("/mem/%s", heap->name); |
Damjan Marion | 58fd481 | 2022-03-14 13:04:38 +0100 | [diff] [blame] | 58 | vlib_stats_validate (idx, 0, STAT_MEM_RELEASABLE); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 59 | |
| 60 | /* Create symlink */ |
| 61 | vlib_stats_add_symlink (idx, STAT_MEM_TOTAL, "/mem/%s/used", heap->name); |
| 62 | vlib_stats_add_symlink (idx, STAT_MEM_USED, "/mem/%s/total", heap->name); |
| 63 | vlib_stats_add_symlink (idx, STAT_MEM_FREE, "/mem/%s/free", heap->name); |
| 64 | |
| 65 | r.private_data = vec_len (memory_heaps_vec) - 1; |
| 66 | r.collect_fn = stat_provider_mem_usage_update_fn; |
| 67 | vlib_stats_register_collector_fn (&r); |
| 68 | } |