stats: memory heap counters
[vpp.git] / src / vpp / stats / stat_segment_provider.c
1 /*
2  * Copyright (c) 2021 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /*
17  * Counters handled by the stats module directly.
18  */
19
20 #include <stdbool.h>
21 #include <vppinfra/mem.h>
22 #include <vppinfra/vec.h>
23 #include <vlib/vlib.h>
24 #include <vlib/counter.h>
25 #include "stat_segment.h"
26
27 clib_mem_heap_t **memory_heaps_vec;
28 u32 mem_vector_index;
29 bool initialized = false;
30
31 enum
32 {
33   STAT_MEM_TOTAL = 0,
34   STAT_MEM_USED,
35   STAT_MEM_FREE,
36   STAT_MEM_USED_MMAP,
37   STAT_MEM_TOTAL_ALLOC,
38   STAT_MEM_FREE_CHUNKS,
39   STAT_MEM_RELEASABLE,
40 } stat_mem_usage_e;
41
42 /*
43  * Called from the stats periodic process to update memory counters.
44  */
45 static void
46 stat_provider_mem_usage_update_fn (stat_segment_directory_entry_t *e,
47                                    u32 index)
48 {
49   clib_mem_usage_t usage;
50   clib_mem_heap_t *heap;
51   counter_t **counters = e->data;
52   counter_t *cb;
53
54   heap = vec_elt (memory_heaps_vec, index);
55   clib_mem_get_heap_usage (heap, &usage);
56   cb = counters[0];
57   cb[STAT_MEM_TOTAL] = usage.bytes_total;
58   cb[STAT_MEM_USED] = usage.bytes_used;
59   cb[STAT_MEM_FREE] = usage.bytes_free;
60   cb[STAT_MEM_USED_MMAP] = usage.bytes_used_mmap;
61   cb[STAT_MEM_TOTAL_ALLOC] = usage.bytes_max;
62   cb[STAT_MEM_FREE_CHUNKS] = usage.bytes_free_reclaimed;
63   cb[STAT_MEM_RELEASABLE] = usage.bytes_overhead;
64 }
65
66 static counter_t **
67 stat_validate_counter_vector3 (counter_t **counters, u32 max1, u32 max2)
68 {
69   stat_segment_main_t *sm = &stat_segment_main;
70   int i;
71   void *oldheap = clib_mem_set_heap (sm->heap);
72   vec_validate_aligned (counters, max1, CLIB_CACHE_LINE_BYTES);
73   for (i = 0; i <= max1; i++)
74     vec_validate_aligned (counters[i], max2, CLIB_CACHE_LINE_BYTES);
75   clib_mem_set_heap (oldheap);
76   return counters;
77 }
78
79 /*
80  * Provide memory heap counters.
81  * Two dimensional array of heap index and per-heap gauges.
82  */
83 void
84 vlib_stats_register_mem_heap (clib_mem_heap_t *heap)
85 {
86   stat_segment_main_t *sm = &stat_segment_main;
87   vec_add1 (memory_heaps_vec, heap);
88   u32 heap_index = vec_len (memory_heaps_vec) - 1;
89
90   /* Memory counters provider */
91   u8 *s = format (0, "/mem/%s", heap->name);
92   u8 *s_used = format (0, "/mem/%s/used", heap->name);
93   u8 *s_total = format (0, "/mem/%s/total", heap->name);
94   u8 *s_free = format (0, "/mem/%s/free", heap->name);
95   mem_vector_index =
96     stat_segment_new_entry (s, STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE);
97   vec_free (s);
98   if (mem_vector_index == ~0)
99     ASSERT (0);
100
101   vlib_stat_segment_lock ();
102   stat_segment_directory_entry_t *ep = &sm->directory_vector[mem_vector_index];
103   ep->data = stat_validate_counter_vector3 (ep->data, 0, STAT_MEM_RELEASABLE);
104
105   /* Create symlink */
106   void *oldheap = clib_mem_set_heap (sm->heap);
107   vlib_stats_register_symlink (oldheap, s_total, mem_vector_index,
108                                STAT_MEM_TOTAL, 0);
109   vlib_stats_register_symlink (oldheap, s_used, mem_vector_index,
110                                STAT_MEM_USED, 0);
111   vlib_stats_register_symlink (oldheap, s_free, mem_vector_index,
112                                STAT_MEM_FREE, 0);
113   vlib_stat_segment_unlock ();
114   clib_mem_set_heap (oldheap);
115   vec_free (s_used);
116   vec_free (s_total);
117   vec_free (s_free);
118
119   stat_segment_poll_add (mem_vector_index, stat_provider_mem_usage_update_fn,
120                          heap_index, 10);
121 }