FD.io VPP  v21.06-1-gbb7418cf9
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);
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;
61  cb[STAT_MEM_TOTAL_ALLOC] = usage.bytes_max;
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", 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);
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");
178 
180  if (i == ~0)
181  ASSERT (0);
182  vec_free (s);
184 
185  s = format (0, "/sys/vector_rate_per_worker");
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 }
uword bytes_overhead
Definition: mem.h:397
uword bytes_total
Definition: mem.h:393
uword bytes_free
Definition: mem.h:393
void vlib_stats_register_mem_heap(clib_mem_heap_t *heap)
stat_segment_main_t stat_segment_main
Definition: stat_segment.c:24
void clib_mem_get_heap_usage(clib_mem_heap_t *heap, clib_mem_usage_t *usage)
Definition: mem_dlmalloc.c:473
uword bytes_free_reclaimed
Definition: mem.h:400
stat_segment_directory_entry_t * directory_vector
Definition: stat_segment.h:80
static clib_mem_heap_t * clib_mem_set_heap(clib_mem_heap_t *heap)
Definition: mem.h:365
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:607
static void usage(void)
Definition: health_check.c:14
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:535
void * data
char name[0]
Definition: mem.h:125
uword bytes_used
Definition: mem.h:393
unsigned char u8
Definition: types.h:56
void vlib_stats_register_symlink(void *oldheap, u8 *name, u32 index1, u32 index2, u8 lock)
Definition: stat_segment.c:249
double f64
Definition: types.h:142
unsigned int u32
Definition: types.h:88
void stat_segment_poll_add(u32 vector_index, stat_segment_update_fn update_fn, u32 caller_index, u32 interval)
Definition: stat_segment.c:884
uint64_t value
uint64_t counter_t
64bit counters
Definition: counter_types.h:22
static void stat_provider_vector_rate_per_thread_update_fn(stat_segment_directory_entry_t *e, u32 index)
description fragment has unexpected format
Definition: map.api:433
static counter_t ** stat_validate_counter_vector3(counter_t **counters, u32 max1, u32 max2)
void vlib_stat_segment_unlock(void)
Definition: stat_segment.c:38
clib_mem_heap_t ** memory_heaps_vec
uword bytes_used_mmap
Definition: mem.h:405
void stat_provider_register_vector_rate(u32 num_workers)
Optimized thread-safe counters.
uword bytes_max
Definition: mem.h:408
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
static void vlib_clear_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:410
u32 index
Definition: flow_types.api:221
void vlib_stat_segment_lock(void)
Definition: stat_segment.c:30
static void stat_provider_vector_rate_update_fn(stat_segment_directory_entry_t *e, u32 index)
#define ASSERT(truth)
static u32 vlib_get_n_threads()
Definition: global_funcs.h:23
#define vec_elt(v, i)
Get vector value at index i.
u32 stat_segment_new_entry(u8 *name, stat_directory_type_t t)
Definition: stat_segment.c:393
static vlib_main_t * vlib_get_main_by_index(u32 thread_index)
Definition: global_funcs.h:29
static void stat_provider_mem_usage_update_fn(stat_segment_directory_entry_t *e, u32 index)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 mem_vector_index
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static f64 vlib_internal_node_vector_rate(vlib_main_t *vm)
Definition: main.h:394
bool initialized
enum @581 stat_mem_usage_e
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".