blob: 5f5581ca62b4393d12ea91880e0b9b5f4d00789e [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,
Damjan Marion8973b072022-03-01 15:51:18 +010024 STAT_COUNTERS
25} stat_segment_counter_t;
26
Damjan Marion8973b072022-03-01 15:51:18 +010027#define foreach_stat_segment_counter_name \
28 _ (LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear, "/sys") \
Damjan Marionc5d81b92022-04-15 15:53:17 +020029 _ (HEARTBEAT, SCALAR_INDEX, heartbeat, "/sys")
Damjan Marion8973b072022-03-01 15:51:18 +010030
31typedef struct
32{
33 u32 entry_index;
34 u32 vector_index;
35 u64 private_data;
36 vlib_stats_entry_t *entry;
37} vlib_stats_collector_data_t;
38
39typedef void (*vlib_stats_collector_fn_t) (vlib_stats_collector_data_t *);
40
41typedef struct
42{
43 vlib_stats_collector_fn_t collect_fn;
44 u32 entry_index;
45 u32 vector_index;
46 u64 private_data;
47} vlib_stats_collector_reg_t;
48
49typedef struct
50{
51 vlib_stats_collector_fn_t fn;
52 u32 entry_index;
53 u32 vector_index;
54 u64 private_data;
55} vlib_stats_collector_t;
56
57typedef struct
58{
59 /* internal, does not point to shared memory */
60 vlib_stats_collector_t *collectors;
61
62 /* statistics segment */
63 uword *directory_vector_by_name;
64 vlib_stats_entry_t *directory_vector;
Damjan Marioneecec8c2022-04-06 12:06:41 +020065 u32 dir_vector_first_free_elt;
Damjan Marion8973b072022-03-01 15:51:18 +010066
67 /* Update interval */
68 f64 update_interval;
69
70 clib_spinlock_t *stat_segment_lockp;
Damjan Mariond1bd9af2022-03-14 13:04:38 +010071 u32 locking_thread_index;
72 u32 n_locks;
Damjan Marion8973b072022-03-01 15:51:18 +010073 clib_socket_t *socket;
74 u8 *socket_name;
75 ssize_t memory_size;
76 clib_mem_page_sz_t log2_page_sz;
77 u8 node_counters_enabled;
78 void *heap;
79 vlib_stats_shared_header_t
80 *shared_header; /* pointer to shared memory segment */
81 int memfd;
82
83} vlib_stats_segment_t;
84
85typedef struct
86{
Damjan Marionc5d81b92022-04-15 15:53:17 +020087 u32 entry_index;
88} vlib_stats_header_t;
89
90typedef struct
91{
Damjan Marion8973b072022-03-01 15:51:18 +010092 vlib_stats_segment_t segment;
93} vlib_stats_main_t;
94
95extern vlib_stats_main_t vlib_stats_main;
96
97static_always_inline vlib_stats_segment_t *
98vlib_stats_get_segment ()
99{
100 return &vlib_stats_main.segment;
101}
102
103static_always_inline vlib_stats_entry_t *
104vlib_stats_get_entry (vlib_stats_segment_t *sm, u32 entry_index)
105{
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);
110 return e;
111}
112
Damjan Marion58fd4812022-03-14 13:04:38 +0100113static_always_inline void *
114vlib_stats_get_entry_data_pointer (u32 entry_index)
115{
116 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
117 vlib_stats_entry_t *e = vlib_stats_get_entry (sm, entry_index);
118 return e->data;
119}
120
Damjan Marion8973b072022-03-01 15:51:18 +0100121clib_error_t *vlib_stats_init (vlib_main_t *vm);
122void *vlib_stats_set_heap ();
Damjan Marion8973b072022-03-01 15:51:18 +0100123void vlib_stats_segment_lock (void);
124void vlib_stats_segment_unlock (void);
Damjan Marion8973b072022-03-01 15:51:18 +0100125void vlib_stats_register_mem_heap (clib_mem_heap_t *);
126f64 vlib_stats_get_segment_update_rate (void);
127
128/* gauge */
129u32 vlib_stats_add_gauge (char *fmt, ...);
130void vlib_stats_set_gauge (u32 entry_index, u64 value);
131
132/* timestamp */
133u32 vlib_stats_add_timestamp (char *fmt, ...);
134void vlib_stats_set_timestamp (u32 entry_index, f64 value);
135
Damjan Marion58fd4812022-03-14 13:04:38 +0100136/* counter vector */
Damjan Marion8973b072022-03-01 15:51:18 +0100137u32 vlib_stats_add_counter_vector (char *fmt, ...);
Damjan Marion58fd4812022-03-14 13:04:38 +0100138
139/* counter pair vector */
140u32 vlib_stats_add_counter_pair_vector (char *fmt, ...);
Damjan Marion8973b072022-03-01 15:51:18 +0100141
142/* string vector */
Damjan Marionc5d81b92022-04-15 15:53:17 +0200143typedef u8 **vlib_stats_string_vector_t;
144vlib_stats_string_vector_t vlib_stats_add_string_vector (char *fmt, ...);
145void vlib_stats_set_string_vector (vlib_stats_string_vector_t *sv, u32 index,
Damjan Marion8973b072022-03-01 15:51:18 +0100146 char *fmt, ...);
147
148/* symlink */
149u32 vlib_stats_add_symlink (u32 entry_index, u32 vector_index, char *fmt, ...);
150void vlib_stats_rename_symlink (u64 entry_index, char *fmt, ...);
151
152/* common to all types */
Damjan Marion58fd4812022-03-14 13:04:38 +0100153void vlib_stats_validate (u32 entry_index, ...);
154int vlib_stats_validate_will_expand (u32 entry_index, ...);
Damjan Marion8973b072022-03-01 15:51:18 +0100155void vlib_stats_remove_entry (u32 entry_index);
156u32 vlib_stats_find_entry_index (char *fmt, ...);
157void vlib_stats_register_collector_fn (vlib_stats_collector_reg_t *r);
158
159format_function_t format_vlib_stats_symlink;
160
161#endif