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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Counters handled by the stats module directly.
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"
27 clib_mem_heap_t **memory_heaps_vec;
29 bool initialized = false;
43 * Called from the stats periodic process to update memory counters.
46 stat_provider_mem_usage_update_fn (stat_segment_directory_entry_t *e,
49 clib_mem_usage_t usage;
50 clib_mem_heap_t *heap;
51 counter_t **counters = e->data;
54 heap = vec_elt (memory_heaps_vec, index);
55 clib_mem_get_heap_usage (heap, &usage);
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;
67 stat_validate_counter_vector3 (counter_t **counters, u32 max1, u32 max2)
69 stat_segment_main_t *sm = &stat_segment_main;
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);
80 * Provide memory heap counters.
81 * Two dimensional array of heap index and per-heap gauges.
84 vlib_stats_register_mem_heap (clib_mem_heap_t *heap)
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;
90 /* Memory counters provider */
91 u8 *s = format (0, "/mem/%s%c", heap->name, 0);
92 u8 *s_used = format (0, "/mem/%s/used%c", heap->name, 0);
93 u8 *s_total = format (0, "/mem/%s/total%c", heap->name, 0);
94 u8 *s_free = format (0, "/mem/%s/free%c", heap->name, 0);
96 stat_segment_new_entry (s, STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE);
98 if (mem_vector_index == ~0)
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);
106 void *oldheap = clib_mem_set_heap (sm->heap);
107 vlib_stats_register_symlink (oldheap, s_total, mem_vector_index,
109 vlib_stats_register_symlink (oldheap, s_used, mem_vector_index,
111 vlib_stats_register_symlink (oldheap, s_free, mem_vector_index,
113 vlib_stat_segment_unlock ();
114 clib_mem_set_heap (oldheap);
119 stat_segment_poll_add (mem_vector_index, stat_provider_mem_usage_update_fn,
124 stat_provider_vector_rate_per_thread_update_fn (
125 stat_segment_directory_entry_t *e, u32 index)
127 vlib_main_t *this_vlib_main;
130 counter_t **counters = e->data;
132 for (i = 0; i < vlib_get_n_threads (); i++)
135 f64 this_vector_rate;
137 this_vlib_main = vlib_get_main_by_index (i);
139 this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
140 vlib_clear_internal_node_vector_rate (this_vlib_main);
141 /* Set the per-worker rate */
142 counter_t *cb = counters[i];
143 cb[0] = this_vector_rate;
148 stat_provider_vector_rate_update_fn (stat_segment_directory_entry_t *e,
151 vlib_main_t *this_vlib_main;
153 f64 vector_rate = 0.0;
154 for (i = 0; i < vlib_get_n_threads (); i++)
157 f64 this_vector_rate;
159 this_vlib_main = vlib_get_main_by_index (i);
161 this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
162 vlib_clear_internal_node_vector_rate (this_vlib_main);
164 vector_rate += this_vector_rate;
167 /* And set the system average rate */
168 vector_rate /= (f64) (i > 1 ? i - 1 : 1);
169 e->value = vector_rate;
173 stat_provider_register_vector_rate (u32 num_workers)
177 u8 *s = format (0, "/sys/vector_rate%c", 0);
179 i = stat_segment_new_entry (s, STAT_DIR_TYPE_SCALAR_INDEX);
183 stat_segment_poll_add (i, stat_provider_vector_rate_update_fn, ~0, 10);
185 s = format (0, "/sys/vector_rate_per_worker%c", 0);
186 i = stat_segment_new_entry (s, STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE);
190 stat_segment_poll_add (i, stat_provider_vector_rate_per_thread_update_fn, ~0,
193 stat_segment_main_t *sm = &stat_segment_main;
194 vlib_stat_segment_lock ();
195 stat_segment_directory_entry_t *ep = &sm->directory_vector[i];
196 ep->data = stat_validate_counter_vector3 (ep->data, num_workers, 0);
197 vlib_stat_segment_unlock ();