1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* Copyright (c) 2021 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Counters handled by the stats module directly.
*/
#include <stdbool.h>
#include <vppinfra/mem.h>
#include <vppinfra/vec.h>
#include <vlib/vlib.h>
#include <vlib/counter.h>
#include "stat_segment.h"
clib_mem_heap_t **memory_heaps_vec;
u32 mem_vector_index;
bool initialized = false;
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 (stat_segment_directory_entry_t *e,
u32 index)
{
clib_mem_usage_t usage;
clib_mem_heap_t *heap;
counter_t **counters = e->data;
counter_t *cb;
heap = vec_elt (memory_heaps_vec, index);
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;
}
static counter_t **
stat_validate_counter_vector3 (counter_t **counters, u32 max1, u32 max2)
{
stat_segment_main_t *sm = &stat_segment_main;
int i;
void *oldheap = clib_mem_set_heap (sm->heap);
vec_validate_aligned (counters, max1, CLIB_CACHE_LINE_BYTES);
for (i = 0; i <= max1; i++)
vec_validate_aligned (counters[i], max2, CLIB_CACHE_LINE_BYTES);
clib_mem_set_heap (oldheap);
return counters;
}
/*
* 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)
{
stat_segment_main_t *sm = &stat_segment_main;
vec_add1 (memory_heaps_vec, heap);
u32 heap_index = vec_len (memory_heaps_vec) - 1;
/* Memory counters provider */
u8 *s = format (0, "/mem/%s", heap->name);
u8 *s_used = format (0, "/mem/%s/used", heap->name);
u8 *s_total = format (0, "/mem/%s/total", heap->name);
u8 *s_free = format (0, "/mem/%s/free", heap->name);
mem_vector_index =
stat_segment_new_entry (s, STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE);
vec_free (s);
if (mem_vector_index == ~0)
ASSERT (0);
vlib_stat_segment_lock ();
stat_segment_directory_entry_t *ep = &sm->directory_vector[mem_vector_index];
ep->data = stat_validate_counter_vector3 (ep->data, 0, STAT_MEM_RELEASABLE);
/* Create symlink */
void *oldheap = clib_mem_set_heap (sm->heap);
vlib_stats_register_symlink (oldheap, s_total, mem_vector_index,
STAT_MEM_TOTAL, 0);
vlib_stats_register_symlink (oldheap, s_used, mem_vector_index,
STAT_MEM_USED, 0);
vlib_stats_register_symlink (oldheap, s_free, mem_vector_index,
STAT_MEM_FREE, 0);
vlib_stat_segment_unlock ();
clib_mem_set_heap (oldheap);
vec_free (s_used);
vec_free (s_total);
vec_free (s_free);
stat_segment_poll_add (mem_vector_index, stat_provider_mem_usage_update_fn,
heap_index, 10);
}
static void
stat_provider_vector_rate_per_thread_update_fn (
stat_segment_directory_entry_t *e, u32 index)
{
vlib_main_t *this_vlib_main;
int i;
ASSERT (e->data);
counter_t **counters = e->data;
for (i = 0; i < vlib_get_n_threads (); i++)
{
f64 this_vector_rate;
this_vlib_main = vlib_get_main_by_index (i);
this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
vlib_clear_internal_node_vector_rate (this_vlib_main);
/* Set the per-worker rate */
counter_t *cb = counters[i];
cb[0] = this_vector_rate;
}
}
static void
stat_provider_vector_rate_update_fn (stat_segment_directory_entry_t *e,
u32 index)
{
vlib_main_t *this_vlib_main;
int i;
f64 vector_rate = 0.0;
for (i = 0; i < vlib_get_n_threads (); i++)
{
f64 this_vector_rate;
this_vlib_main = vlib_get_main_by_index (i);
this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
vlib_clear_internal_node_vector_rate (this_vlib_main);
vector_rate += this_vector_rate;
}
/* And set the system average rate */
vector_rate /= (f64) (i > 1 ? i - 1 : 1);
e->value = vector_rate;
}
void
stat_provider_register_vector_rate (u32 num_workers)
{
int i;
u8 *s = format (0, "/sys/vector_rate");
i = stat_segment_new_entry (s, STAT_DIR_TYPE_SCALAR_INDEX);
if (i == ~0)
ASSERT (0);
vec_free (s);
stat_segment_poll_add (i, stat_provider_vector_rate_update_fn, ~0, 10);
s = format (0, "/sys/vector_rate_per_worker");
i = stat_segment_new_entry (s, STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE);
if (i == ~0)
ASSERT (0);
vec_free (s);
stat_segment_poll_add (i, stat_provider_vector_rate_per_thread_update_fn, ~0,
10);
stat_segment_main_t *sm = &stat_segment_main;
vlib_stat_segment_lock ();
stat_segment_directory_entry_t *ep = &sm->directory_vector[i];
ep->data = stat_validate_counter_vector3 (ep->data, num_workers, 0);
vlib_stat_segment_unlock ();
}
|