1 /* SPDX-License-Identifier: Apache-2.0
2 * Copyright(c) 2022 Cisco Systems, Inc.
5 #ifndef included_stats_stats_h
6 #define included_stats_stats_h
8 #include <vppinfra/socket.h>
9 #include <vppinfra/lock.h>
10 #include <vlib/stats/shared.h>
12 /* Default stat segment 32m */
13 #define STAT_SEGMENT_DEFAULT_SIZE (32 << 20)
15 /* Shared segment memory layout version */
16 #define STAT_SEGMENT_VERSION 2
18 #define STAT_SEGMENT_INDEX_INVALID UINT32_MAX
22 STAT_COUNTER_HEARTBEAT = 0,
23 STAT_COUNTER_LAST_STATS_CLEAR,
25 } stat_segment_counter_t;
27 #define foreach_stat_segment_counter_name \
28 _ (LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear, "/sys") \
29 _ (HEARTBEAT, SCALAR_INDEX, heartbeat, "/sys")
36 vlib_stats_entry_t *entry;
37 } vlib_stats_collector_data_t;
39 typedef void (*vlib_stats_collector_fn_t) (vlib_stats_collector_data_t *);
43 vlib_stats_collector_fn_t collect_fn;
47 } vlib_stats_collector_reg_t;
51 vlib_stats_collector_fn_t fn;
55 } vlib_stats_collector_t;
59 /* internal, does not point to shared memory */
60 vlib_stats_collector_t *collectors;
62 /* statistics segment */
63 uword *directory_vector_by_name;
64 vlib_stats_entry_t *directory_vector;
65 u32 dir_vector_first_free_elt;
70 clib_spinlock_t *stat_segment_lockp;
71 u32 locking_thread_index;
73 clib_socket_t *socket;
76 clib_mem_page_sz_t log2_page_sz;
77 u8 node_counters_enabled;
79 vlib_stats_shared_header_t
80 *shared_header; /* pointer to shared memory segment */
83 } vlib_stats_segment_t;
88 } vlib_stats_header_t;
92 vlib_stats_segment_t segment;
95 extern vlib_stats_main_t vlib_stats_main;
97 static_always_inline vlib_stats_segment_t *
98 vlib_stats_get_segment ()
100 return &vlib_stats_main.segment;
103 static_always_inline vlib_stats_entry_t *
104 vlib_stats_get_entry (vlib_stats_segment_t *sm, u32 entry_index)
106 vlib_stats_entry_t *e;
107 ASSERT (entry_index < vec_len (sm->directory_vector));
108 e = sm->directory_vector + entry_index;
109 ASSERT (e->type != STAT_DIR_TYPE_EMPTY && e->type != STAT_DIR_TYPE_ILLEGAL);
113 static_always_inline void *
114 vlib_stats_get_entry_data_pointer (u32 entry_index)
116 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
117 vlib_stats_entry_t *e = vlib_stats_get_entry (sm, entry_index);
121 clib_error_t *vlib_stats_init (vlib_main_t *vm);
122 void *vlib_stats_set_heap ();
123 void vlib_stats_segment_lock (void);
124 void vlib_stats_segment_unlock (void);
125 void vlib_stats_register_mem_heap (clib_mem_heap_t *);
126 f64 vlib_stats_get_segment_update_rate (void);
129 u32 vlib_stats_add_gauge (char *fmt, ...);
130 void vlib_stats_set_gauge (u32 entry_index, u64 value);
133 u32 vlib_stats_add_timestamp (char *fmt, ...);
134 void vlib_stats_set_timestamp (u32 entry_index, f64 value);
137 u32 vlib_stats_add_counter_vector (char *fmt, ...);
139 /* counter pair vector */
140 u32 vlib_stats_add_counter_pair_vector (char *fmt, ...);
143 typedef u8 **vlib_stats_string_vector_t;
144 vlib_stats_string_vector_t vlib_stats_add_string_vector (char *fmt, ...);
145 void vlib_stats_set_string_vector (vlib_stats_string_vector_t *sv, u32 index,
147 void vlib_stats_free_string_vector (vlib_stats_string_vector_t *sv);
150 u32 vlib_stats_add_symlink (u32 entry_index, u32 vector_index, char *fmt, ...);
151 void vlib_stats_rename_symlink (u64 entry_index, char *fmt, ...);
153 /* common to all types */
154 void vlib_stats_validate (u32 entry_index, ...);
155 int vlib_stats_validate_will_expand (u32 entry_index, ...);
156 void vlib_stats_remove_entry (u32 entry_index);
157 u32 vlib_stats_find_entry_index (char *fmt, ...);
158 void vlib_stats_register_collector_fn (vlib_stats_collector_reg_t *r);
160 format_function_t format_vlib_stats_symlink;