FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
stat_segment_provider.c
Go to the documentation of this file.
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 
29 bool initialized = false;
30 
31 enum
32 {
41 
42 /*
43  * Called from the stats periodic process to update memory counters.
44  */
45 static void
47  u32 index)
48 {
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);
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 **
68 {
70  int i;
71  void *oldheap = clib_mem_set_heap (sm->heap);
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
85 {
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%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);
97  vec_free (s);
98  if (mem_vector_index == ~0)
99  ASSERT (0);
100 
104 
105  /* Create symlink */
106  void *oldheap = clib_mem_set_heap (sm->heap);
108  STAT_MEM_TOTAL, 0);
110  STAT_MEM_USED, 0);
112  STAT_MEM_FREE, 0);
114  clib_mem_set_heap (oldheap);
115  vec_free (s_used);
116  vec_free (s_total);
117  vec_free (s_free);
118 
120  heap_index, 10);
121 }
122 
123 static void
126 {
127  vlib_main_t *this_vlib_main;
128  int i;
129  ASSERT (e->data);
130  counter_t **counters = e->data;
131 
132  for (i = 0; i < vlib_get_n_threads (); i++)
133  {
134 
135  f64 this_vector_rate;
136 
137  this_vlib_main = vlib_get_main_by_index (i);
138 
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;
144  }
145 }
146 
147 static void
149  u32 index)
150 {
151  vlib_main_t *this_vlib_main;
152  int i;
153  f64 vector_rate = 0.0;
154  for (i = 0; i < vlib_get_n_threads (); i++)
155  {
156 
157  f64 this_vector_rate;
158 
159  this_vlib_main = vlib_get_main_by_index (i);
160 
161  this_vector_rate = vlib_internal_node_vector_rate (this_vlib_main);
162  vlib_clear_internal_node_vector_rate (this_vlib_main);
163 
164  vector_rate += this_vector_rate;
165  }
166 
167  /* And set the system average rate */
168  vector_rate /= (f64) (i > 1 ? i - 1 : 1);
169  e->value = vector_rate;
170 }
171 
172 void
174 {
175  int i;
176 
177  u8 *s = format (0, "/sys/vector_rate%c", 0);
178 
180  if (i == ~0)
181  ASSERT (0);
182  vec_free (s);
184 
185  s = format (0, "/sys/vector_rate_per_worker%c", 0);
187  if (i == ~0)
188  ASSERT (0);
189  vec_free (s);
191  10);
192 
196  ep->data = stat_validate_counter_vector3 (ep->data, num_workers, 0);
198 }
vlib.h
stat_segment_main_t::heap
void * heap
Definition: stat_segment.h:95
stat_segment_directory_entry_t::data
void * data
Definition: stat_segment_shared.h:42
stat_mem_usage_e
enum @587 stat_mem_usage_e
stat_segment_poll_add
void stat_segment_poll_add(u32 vector_index, stat_segment_update_fn update_fn, u32 caller_index, u32 interval)
Definition: stat_segment.c:884
clib_mem_usage_t
Definition: mem.h:389
stat_segment_main_t::directory_vector
stat_segment_directory_entry_t * directory_vector
Definition: stat_segment.h:80
usage
static void usage(void)
Definition: health_check.c:14
vlib_stats_register_symlink
void vlib_stats_register_symlink(void *oldheap, u8 *name, u32 index1, u32 index2, u8 lock)
Definition: stat_segment.c:249
STAT_MEM_FREE_CHUNKS
@ STAT_MEM_FREE_CHUNKS
Definition: stat_segment_provider.c:38
vlib_clear_internal_node_vector_rate
static void vlib_clear_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:414
STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE
@ STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE
Definition: stat_segment_shared.h:23
stat_segment_main_t
Definition: stat_segment.h:73
vlib_stat_segment_unlock
void vlib_stat_segment_unlock(void)
Definition: stat_segment.c:38
vec_elt
#define vec_elt(v, i)
Get vector value at index i.
Definition: vec_bootstrap.h:210
STAT_MEM_TOTAL
@ STAT_MEM_TOTAL
Definition: stat_segment_provider.c:33
initialized
bool initialized
Definition: stat_segment_provider.c:29
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
stat_provider_mem_usage_update_fn
static void stat_provider_mem_usage_update_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: stat_segment_provider.c:46
stat_provider_register_vector_rate
void stat_provider_register_vector_rate(u32 num_workers)
Definition: stat_segment_provider.c:173
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
stat_segment_directory_entry_t::value
uint64_t value
Definition: stat_segment_shared.h:41
vec_validate_aligned
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:534
counter_t
uint64_t counter_t
64bit counters
Definition: counter_types.h:22
vlib_stats_register_mem_heap
void vlib_stats_register_mem_heap(clib_mem_heap_t *heap)
Definition: stat_segment_provider.c:84
STAT_MEM_USED_MMAP
@ STAT_MEM_USED_MMAP
Definition: stat_segment_provider.c:36
STAT_MEM_TOTAL_ALLOC
@ STAT_MEM_TOTAL_ALLOC
Definition: stat_segment_provider.c:37
STAT_MEM_FREE
@ STAT_MEM_FREE
Definition: stat_segment_provider.c:35
stat_provider_vector_rate_per_thread_update_fn
static void stat_provider_vector_rate_per_thread_update_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: stat_segment_provider.c:124
f64
double f64
Definition: types.h:142
clib_mem_heap_t::name
char name[0]
Definition: mem.h:125
vlib_stat_segment_lock
void vlib_stat_segment_lock(void)
Definition: stat_segment.c:30
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:58
stat_segment.h
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
STAT_MEM_RELEASABLE
@ STAT_MEM_RELEASABLE
Definition: stat_segment_provider.c:39
stat_segment_directory_entry_t
Definition: stat_segment_shared.h:31
index
u32 index
Definition: flow_types.api:221
stat_validate_counter_vector3
static counter_t ** stat_validate_counter_vector3(counter_t **counters, u32 max1, u32 max2)
Definition: stat_segment_provider.c:67
mem_vector_index
u32 mem_vector_index
Definition: stat_segment_provider.c:28
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
memory_heaps_vec
clib_mem_heap_t ** memory_heaps_vec
Definition: stat_segment_provider.c:27
STAT_DIR_TYPE_SCALAR_INDEX
@ STAT_DIR_TYPE_SCALAR_INDEX
Definition: stat_segment_shared.h:22
vlib_get_main_by_index
static vlib_main_t * vlib_get_main_by_index(u32 thread_index)
Definition: global_funcs.h:29
vec.h
stat_provider_vector_rate_update_fn
static void stat_provider_vector_rate_update_fn(stat_segment_directory_entry_t *e, u32 index)
Definition: stat_segment_provider.c:148
vlib_internal_node_vector_rate
static f64 vlib_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:398
vlib_main_t
Definition: main.h:102
vlib_get_n_threads
static u32 vlib_get_n_threads()
Definition: global_funcs.h:23
u8
unsigned char u8
Definition: types.h:56
i
int i
Definition: flowhash_template.h:376
mem.h
counter.h
clib_mem_get_heap_usage
void clib_mem_get_heap_usage(clib_mem_heap_t *heap, clib_mem_usage_t *usage)
Definition: mem_dlmalloc.c:473
stat_segment_new_entry
u32 stat_segment_new_entry(u8 *name, stat_directory_type_t t)
Definition: stat_segment.c:393
clib_mem_set_heap
static clib_mem_heap_t * clib_mem_set_heap(clib_mem_heap_t *heap)
Definition: mem.h:368
clib_mem_heap_t
Definition: mem.h:107
stat_segment_main
stat_segment_main_t stat_segment_main
Definition: stat_segment.c:24
STAT_MEM_USED
@ STAT_MEM_USED
Definition: stat_segment_provider.c:34