blob: f3a3f5d3ed48c75ec82a88788d3dc3b505dafa6f [file] [log] [blame]
Damjan Marion8973b072022-03-01 15:51:18 +01001/* 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
9static clib_mem_heap_t **memory_heaps_vec;
10
11enum
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 */
25static void
26stat_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 */
49void
50vlib_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 Marion58fd4812022-03-14 13:04:38 +010058 vlib_stats_validate (idx, 0, STAT_MEM_RELEASABLE);
Damjan Marion8973b072022-03-01 15:51:18 +010059
60 /* Create symlink */
Leland Krych082dc142022-05-26 08:37:54 -070061 vlib_stats_add_symlink (idx, STAT_MEM_USED, "/mem/%s/used", heap->name);
62 vlib_stats_add_symlink (idx, STAT_MEM_TOTAL, "/mem/%s/total", heap->name);
Damjan Marion8973b072022-03-01 15:51:18 +010063 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}