stats: avoid linear search for empty entry
[vpp.git] / src / vlib / stats / stats.h
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2022 Cisco Systems, Inc.
3  */
4
5 #ifndef included_stats_stats_h
6 #define included_stats_stats_h
7
8 #include <vppinfra/socket.h>
9 #include <vppinfra/lock.h>
10 #include <vlib/stats/shared.h>
11
12 /* Default stat segment 32m */
13 #define STAT_SEGMENT_DEFAULT_SIZE (32 << 20)
14
15 /* Shared segment memory layout version */
16 #define STAT_SEGMENT_VERSION 2
17
18 #define STAT_SEGMENT_INDEX_INVALID UINT32_MAX
19
20 typedef enum
21 {
22   STAT_COUNTER_HEARTBEAT = 0,
23   STAT_COUNTER_LAST_STATS_CLEAR,
24   STAT_COUNTER_NODE_CLOCKS,
25   STAT_COUNTER_NODE_VECTORS,
26   STAT_COUNTER_NODE_CALLS,
27   STAT_COUNTER_NODE_SUSPENDS,
28   STAT_COUNTER_NODE_NAMES,
29   STAT_COUNTERS
30 } stat_segment_counter_t;
31
32 #define foreach_stat_segment_node_counter_name                                \
33   _ (NODE_CLOCKS, COUNTER_VECTOR_SIMPLE, clocks, "/sys/node")                 \
34   _ (NODE_VECTORS, COUNTER_VECTOR_SIMPLE, vectors, "/sys/node")               \
35   _ (NODE_CALLS, COUNTER_VECTOR_SIMPLE, calls, "/sys/node")                   \
36   _ (NODE_SUSPENDS, COUNTER_VECTOR_SIMPLE, suspends, "/sys/node")
37
38 #define foreach_stat_segment_counter_name                                     \
39   _ (LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear, "/sys")                \
40   _ (HEARTBEAT, SCALAR_INDEX, heartbeat, "/sys")                              \
41   _ (NODE_NAMES, NAME_VECTOR, names, "/sys/node")                             \
42   foreach_stat_segment_node_counter_name
43
44 typedef struct
45 {
46   u32 entry_index;
47   u32 vector_index;
48   u64 private_data;
49   vlib_stats_entry_t *entry;
50 } vlib_stats_collector_data_t;
51
52 typedef void (*vlib_stats_collector_fn_t) (vlib_stats_collector_data_t *);
53
54 typedef struct
55 {
56   vlib_stats_collector_fn_t collect_fn;
57   u32 entry_index;
58   u32 vector_index;
59   u64 private_data;
60 } vlib_stats_collector_reg_t;
61
62 typedef struct
63 {
64   vlib_stats_collector_fn_t fn;
65   u32 entry_index;
66   u32 vector_index;
67   u64 private_data;
68 } vlib_stats_collector_t;
69
70 typedef struct
71 {
72   /* internal, does not point to shared memory */
73   vlib_stats_collector_t *collectors;
74
75   /* statistics segment */
76   uword *directory_vector_by_name;
77   vlib_stats_entry_t *directory_vector;
78   u32 dir_vector_first_free_elt;
79   u8 **nodes;
80
81   /* Update interval */
82   f64 update_interval;
83
84   clib_spinlock_t *stat_segment_lockp;
85   u32 locking_thread_index;
86   u32 n_locks;
87   clib_socket_t *socket;
88   u8 *socket_name;
89   ssize_t memory_size;
90   clib_mem_page_sz_t log2_page_sz;
91   u8 node_counters_enabled;
92   void *heap;
93   vlib_stats_shared_header_t
94     *shared_header; /* pointer to shared memory segment */
95   int memfd;
96
97 } vlib_stats_segment_t;
98
99 typedef struct
100 {
101   vlib_stats_segment_t segment;
102 } vlib_stats_main_t;
103
104 extern vlib_stats_main_t vlib_stats_main;
105
106 static_always_inline vlib_stats_segment_t *
107 vlib_stats_get_segment ()
108 {
109   return &vlib_stats_main.segment;
110 }
111
112 static_always_inline vlib_stats_entry_t *
113 vlib_stats_get_entry (vlib_stats_segment_t *sm, u32 entry_index)
114 {
115   vlib_stats_entry_t *e;
116   ASSERT (entry_index < vec_len (sm->directory_vector));
117   e = sm->directory_vector + entry_index;
118   ASSERT (e->type != STAT_DIR_TYPE_EMPTY && e->type != STAT_DIR_TYPE_ILLEGAL);
119   return e;
120 }
121
122 static_always_inline void *
123 vlib_stats_get_entry_data_pointer (u32 entry_index)
124 {
125   vlib_stats_segment_t *sm = vlib_stats_get_segment ();
126   vlib_stats_entry_t *e = vlib_stats_get_entry (sm, entry_index);
127   return e->data;
128 }
129
130 clib_error_t *vlib_stats_init (vlib_main_t *vm);
131 void *vlib_stats_set_heap ();
132 void vlib_stats_segment_lock (void);
133 void vlib_stats_segment_unlock (void);
134 void vlib_stats_register_mem_heap (clib_mem_heap_t *);
135 f64 vlib_stats_get_segment_update_rate (void);
136
137 /* gauge */
138 u32 vlib_stats_add_gauge (char *fmt, ...);
139 void vlib_stats_set_gauge (u32 entry_index, u64 value);
140
141 /* timestamp */
142 u32 vlib_stats_add_timestamp (char *fmt, ...);
143 void vlib_stats_set_timestamp (u32 entry_index, f64 value);
144
145 /* counter vector */
146 u32 vlib_stats_add_counter_vector (char *fmt, ...);
147
148 /* counter pair vector */
149 u32 vlib_stats_add_counter_pair_vector (char *fmt, ...);
150
151 /* string vector */
152 u32 vlib_stats_add_string_vector (char *fmt, ...);
153 void vlib_stats_set_string_vector (u32 entry_index, u32 vector_index,
154                                    char *fmt, ...);
155
156 /* symlink */
157 u32 vlib_stats_add_symlink (u32 entry_index, u32 vector_index, char *fmt, ...);
158 void vlib_stats_rename_symlink (u64 entry_index, char *fmt, ...);
159
160 /* common to all types */
161 void vlib_stats_validate (u32 entry_index, ...);
162 int vlib_stats_validate_will_expand (u32 entry_index, ...);
163 void vlib_stats_remove_entry (u32 entry_index);
164 u32 vlib_stats_find_entry_index (char *fmt, ...);
165 void vlib_stats_register_collector_fn (vlib_stats_collector_reg_t *r);
166
167 format_function_t format_vlib_stats_symlink;
168
169 #endif