blob: faaa37a7ec2de8990894445ba26e2d16edb5a56a [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
122clib_error_t *vlib_stats_init (vlib_main_t *vm);
123void *vlib_stats_set_heap ();
124void vlib_stats_update_counter (void *, u32, stat_directory_type_t);
125void vlib_stats_register_error_index (u64 *em_vec, u64 index, char *fmt, ...);
126void vlib_stats_update_error_vector (u64 *error_vector, u32 thread_index,
127 int lock);
128void vlib_stats_segment_lock (void);
129void vlib_stats_segment_unlock (void);
130void vlib_stats_delete_cm (void *);
131void vlib_stats_register_mem_heap (clib_mem_heap_t *);
132f64 vlib_stats_get_segment_update_rate (void);
133
134/* gauge */
135u32 vlib_stats_add_gauge (char *fmt, ...);
136void vlib_stats_set_gauge (u32 entry_index, u64 value);
137
138/* timestamp */
139u32 vlib_stats_add_timestamp (char *fmt, ...);
140void vlib_stats_set_timestamp (u32 entry_index, f64 value);
141
142/* vector */
143u32 vlib_stats_add_counter_vector (char *fmt, ...);
144void vlib_stats_validate_counter_vector (u32 entry_index, u32 vector_index);
145
146/* string vector */
147u32 vlib_stats_add_string_vector (char *fmt, ...);
148void vlib_stats_set_string_vector (u32 entry_index, u32 vector_index,
149 char *fmt, ...);
150
151/* symlink */
152u32 vlib_stats_add_symlink (u32 entry_index, u32 vector_index, char *fmt, ...);
153void vlib_stats_rename_symlink (u64 entry_index, char *fmt, ...);
154
155/* common to all types */
156void vlib_stats_remove_entry (u32 entry_index);
157u32 vlib_stats_find_entry_index (char *fmt, ...);
158void vlib_stats_register_collector_fn (vlib_stats_collector_reg_t *r);
159
160format_function_t format_vlib_stats_symlink;
161
162#endif