Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 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, |
Ole Troan | 5010bbd | 2022-11-28 13:13:24 +0100 | [diff] [blame] | 24 | STAT_COUNTER_BOOTTIME, |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 25 | STAT_COUNTERS |
| 26 | } stat_segment_counter_t; |
| 27 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 28 | #define foreach_stat_segment_counter_name \ |
| 29 | _ (LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear, "/sys") \ |
Ole Troan | 5010bbd | 2022-11-28 13:13:24 +0100 | [diff] [blame] | 30 | _ (HEARTBEAT, SCALAR_INDEX, heartbeat, "/sys") \ |
| 31 | _ (BOOTTIME, SCALAR_INDEX, boottime, "/sys") |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 32 | |
| 33 | typedef struct |
| 34 | { |
| 35 | u32 entry_index; |
| 36 | u32 vector_index; |
| 37 | u64 private_data; |
| 38 | vlib_stats_entry_t *entry; |
| 39 | } vlib_stats_collector_data_t; |
| 40 | |
| 41 | typedef void (*vlib_stats_collector_fn_t) (vlib_stats_collector_data_t *); |
| 42 | |
| 43 | typedef struct |
| 44 | { |
| 45 | vlib_stats_collector_fn_t collect_fn; |
| 46 | u32 entry_index; |
| 47 | u32 vector_index; |
| 48 | u64 private_data; |
| 49 | } vlib_stats_collector_reg_t; |
| 50 | |
| 51 | typedef struct |
| 52 | { |
| 53 | vlib_stats_collector_fn_t fn; |
| 54 | u32 entry_index; |
| 55 | u32 vector_index; |
| 56 | u64 private_data; |
| 57 | } vlib_stats_collector_t; |
| 58 | |
| 59 | typedef struct |
| 60 | { |
| 61 | /* internal, does not point to shared memory */ |
| 62 | vlib_stats_collector_t *collectors; |
| 63 | |
| 64 | /* statistics segment */ |
| 65 | uword *directory_vector_by_name; |
| 66 | vlib_stats_entry_t *directory_vector; |
Damjan Marion | eecec8c | 2022-04-06 12:06:41 +0200 | [diff] [blame] | 67 | u32 dir_vector_first_free_elt; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 68 | |
| 69 | /* Update interval */ |
| 70 | f64 update_interval; |
| 71 | |
| 72 | clib_spinlock_t *stat_segment_lockp; |
Damjan Marion | d1bd9af | 2022-03-14 13:04:38 +0100 | [diff] [blame] | 73 | u32 locking_thread_index; |
| 74 | u32 n_locks; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 75 | clib_socket_t *socket; |
| 76 | u8 *socket_name; |
| 77 | ssize_t memory_size; |
| 78 | clib_mem_page_sz_t log2_page_sz; |
| 79 | u8 node_counters_enabled; |
| 80 | void *heap; |
| 81 | vlib_stats_shared_header_t |
| 82 | *shared_header; /* pointer to shared memory segment */ |
| 83 | int memfd; |
| 84 | |
| 85 | } vlib_stats_segment_t; |
| 86 | |
| 87 | typedef struct |
| 88 | { |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 89 | u32 entry_index; |
| 90 | } vlib_stats_header_t; |
| 91 | |
| 92 | typedef struct |
| 93 | { |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 94 | vlib_stats_segment_t segment; |
| 95 | } vlib_stats_main_t; |
| 96 | |
| 97 | extern vlib_stats_main_t vlib_stats_main; |
| 98 | |
| 99 | static_always_inline vlib_stats_segment_t * |
| 100 | vlib_stats_get_segment () |
| 101 | { |
| 102 | return &vlib_stats_main.segment; |
| 103 | } |
| 104 | |
| 105 | static_always_inline vlib_stats_entry_t * |
| 106 | vlib_stats_get_entry (vlib_stats_segment_t *sm, u32 entry_index) |
| 107 | { |
| 108 | vlib_stats_entry_t *e; |
| 109 | ASSERT (entry_index < vec_len (sm->directory_vector)); |
| 110 | e = sm->directory_vector + entry_index; |
| 111 | ASSERT (e->type != STAT_DIR_TYPE_EMPTY && e->type != STAT_DIR_TYPE_ILLEGAL); |
| 112 | return e; |
| 113 | } |
| 114 | |
Damjan Marion | 58fd481 | 2022-03-14 13:04:38 +0100 | [diff] [blame] | 115 | static_always_inline void * |
| 116 | vlib_stats_get_entry_data_pointer (u32 entry_index) |
| 117 | { |
| 118 | vlib_stats_segment_t *sm = vlib_stats_get_segment (); |
| 119 | vlib_stats_entry_t *e = vlib_stats_get_entry (sm, entry_index); |
| 120 | return e->data; |
| 121 | } |
| 122 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 123 | clib_error_t *vlib_stats_init (vlib_main_t *vm); |
| 124 | void *vlib_stats_set_heap (); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 125 | void vlib_stats_segment_lock (void); |
| 126 | void vlib_stats_segment_unlock (void); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 127 | void vlib_stats_register_mem_heap (clib_mem_heap_t *); |
| 128 | f64 vlib_stats_get_segment_update_rate (void); |
| 129 | |
| 130 | /* gauge */ |
| 131 | u32 vlib_stats_add_gauge (char *fmt, ...); |
| 132 | void vlib_stats_set_gauge (u32 entry_index, u64 value); |
| 133 | |
| 134 | /* timestamp */ |
| 135 | u32 vlib_stats_add_timestamp (char *fmt, ...); |
| 136 | void vlib_stats_set_timestamp (u32 entry_index, f64 value); |
| 137 | |
Damjan Marion | 58fd481 | 2022-03-14 13:04:38 +0100 | [diff] [blame] | 138 | /* counter vector */ |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 139 | u32 vlib_stats_add_counter_vector (char *fmt, ...); |
Damjan Marion | 58fd481 | 2022-03-14 13:04:38 +0100 | [diff] [blame] | 140 | |
| 141 | /* counter pair vector */ |
| 142 | u32 vlib_stats_add_counter_pair_vector (char *fmt, ...); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 143 | |
| 144 | /* string vector */ |
Damjan Marion | c5d81b9 | 2022-04-15 15:53:17 +0200 | [diff] [blame] | 145 | typedef u8 **vlib_stats_string_vector_t; |
| 146 | vlib_stats_string_vector_t vlib_stats_add_string_vector (char *fmt, ...); |
| 147 | void vlib_stats_set_string_vector (vlib_stats_string_vector_t *sv, u32 index, |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 148 | char *fmt, ...); |
Damjan Marion | 0530d09 | 2022-05-06 12:39:10 +0200 | [diff] [blame] | 149 | void vlib_stats_free_string_vector (vlib_stats_string_vector_t *sv); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 150 | |
| 151 | /* symlink */ |
| 152 | u32 vlib_stats_add_symlink (u32 entry_index, u32 vector_index, char *fmt, ...); |
| 153 | void vlib_stats_rename_symlink (u64 entry_index, char *fmt, ...); |
| 154 | |
| 155 | /* common to all types */ |
Damjan Marion | 58fd481 | 2022-03-14 13:04:38 +0100 | [diff] [blame] | 156 | void vlib_stats_validate (u32 entry_index, ...); |
| 157 | int vlib_stats_validate_will_expand (u32 entry_index, ...); |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 158 | void vlib_stats_remove_entry (u32 entry_index); |
| 159 | u32 vlib_stats_find_entry_index (char *fmt, ...); |
| 160 | void vlib_stats_register_collector_fn (vlib_stats_collector_reg_t *r); |
| 161 | |
| 162 | format_function_t format_vlib_stats_symlink; |
| 163 | |
| 164 | #endif |