blob: 6db93719a0c03ac187f7227bdbb7e6321ec3f280 [file] [log] [blame]
Damjan Marion8973b072022-03-01 15:51:18 +01001/* 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
20typedef 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
44typedef 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
52typedef void (*vlib_stats_collector_fn_t) (vlib_stats_collector_data_t *);
53
54typedef 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
62typedef 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
70typedef 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 volatile u64 **error_vector;
79 u8 **nodes;
80
81 /* Update interval */
82 f64 update_interval;
83
84 clib_spinlock_t *stat_segment_lockp;
Damjan Mariond1bd9af2022-03-14 13:04:38 +010085 u32 locking_thread_index;
86 u32 n_locks;
Damjan Marion8973b072022-03-01 15:51:18 +010087 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
99typedef struct
100{
101 vlib_stats_segment_t segment;
102} vlib_stats_main_t;
103
104extern vlib_stats_main_t vlib_stats_main;
105
106static_always_inline vlib_stats_segment_t *
107vlib_stats_get_segment ()
108{
109 return &vlib_stats_main.segment;
110}
111
112static_always_inline vlib_stats_entry_t *
113vlib_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
Damjan Marion58fd4812022-03-14 13:04:38 +0100122static_always_inline void *
123vlib_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
Damjan Marion8973b072022-03-01 15:51:18 +0100130clib_error_t *vlib_stats_init (vlib_main_t *vm);
131void *vlib_stats_set_heap ();
Damjan Marion8973b072022-03-01 15:51:18 +0100132void vlib_stats_register_error_index (u64 *em_vec, u64 index, char *fmt, ...);
133void vlib_stats_update_error_vector (u64 *error_vector, u32 thread_index,
134 int lock);
135void vlib_stats_segment_lock (void);
136void vlib_stats_segment_unlock (void);
Damjan Marion8973b072022-03-01 15:51:18 +0100137void vlib_stats_register_mem_heap (clib_mem_heap_t *);
138f64 vlib_stats_get_segment_update_rate (void);
139
140/* gauge */
141u32 vlib_stats_add_gauge (char *fmt, ...);
142void vlib_stats_set_gauge (u32 entry_index, u64 value);
143
144/* timestamp */
145u32 vlib_stats_add_timestamp (char *fmt, ...);
146void vlib_stats_set_timestamp (u32 entry_index, f64 value);
147
Damjan Marion58fd4812022-03-14 13:04:38 +0100148/* counter vector */
Damjan Marion8973b072022-03-01 15:51:18 +0100149u32 vlib_stats_add_counter_vector (char *fmt, ...);
Damjan Marion58fd4812022-03-14 13:04:38 +0100150
151/* counter pair vector */
152u32 vlib_stats_add_counter_pair_vector (char *fmt, ...);
Damjan Marion8973b072022-03-01 15:51:18 +0100153
154/* string vector */
155u32 vlib_stats_add_string_vector (char *fmt, ...);
156void vlib_stats_set_string_vector (u32 entry_index, u32 vector_index,
157 char *fmt, ...);
158
159/* symlink */
160u32 vlib_stats_add_symlink (u32 entry_index, u32 vector_index, char *fmt, ...);
161void vlib_stats_rename_symlink (u64 entry_index, char *fmt, ...);
162
163/* common to all types */
Damjan Marion58fd4812022-03-14 13:04:38 +0100164void vlib_stats_validate (u32 entry_index, ...);
165int vlib_stats_validate_will_expand (u32 entry_index, ...);
Damjan Marion8973b072022-03-01 15:51:18 +0100166void vlib_stats_remove_entry (u32 entry_index);
167u32 vlib_stats_find_entry_index (char *fmt, ...);
168void vlib_stats_register_collector_fn (vlib_stats_collector_reg_t *r);
169
170format_function_t format_vlib_stats_symlink;
171
172#endif