blob: b07f21724a07a7f1fcdd91f539265adc569bb049 [file] [log] [blame]
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2022 Cisco Systems, Inc.
*/
#ifndef included_stats_stats_h
#define included_stats_stats_h
#include <vppinfra/socket.h>
#include <vppinfra/lock.h>
#include <vlib/stats/shared.h>
/* Default stat segment 32m */
#define STAT_SEGMENT_DEFAULT_SIZE (32 << 20)
/* Shared segment memory layout version */
#define STAT_SEGMENT_VERSION 2
#define STAT_SEGMENT_INDEX_INVALID UINT32_MAX
typedef enum
{
STAT_COUNTER_HEARTBEAT = 0,
STAT_COUNTER_LAST_STATS_CLEAR,
STAT_COUNTERS
} stat_segment_counter_t;
#define foreach_stat_segment_counter_name \
_ (LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear, "/sys") \
_ (HEARTBEAT, SCALAR_INDEX, heartbeat, "/sys")
typedef struct
{
u32 entry_index;
u32 vector_index;
u64 private_data;
vlib_stats_entry_t *entry;
} vlib_stats_collector_data_t;
typedef void (*vlib_stats_collector_fn_t) (vlib_stats_collector_data_t *);
typedef struct
{
vlib_stats_collector_fn_t collect_fn;
u32 entry_index;
u32 vector_index;
u64 private_data;
} vlib_stats_collector_reg_t;
typedef struct
{
vlib_stats_collector_fn_t fn;
u32 entry_index;
u32 vector_index;
u64 private_data;
} vlib_stats_collector_t;
typedef struct
{
/* internal, does not point to shared memory */
vlib_stats_collector_t *collectors;
/* statistics segment */
uword *directory_vector_by_name;
vlib_stats_entry_t *directory_vector;
u32 dir_vector_first_free_elt;
/* Update interval */
f64 update_interval;
clib_spinlock_t *stat_segment_lockp;
u32 locking_thread_index;
u32 n_locks;
clib_socket_t *socket;
u8 *socket_name;
ssize_t memory_size;
clib_mem_page_sz_t log2_page_sz;
u8 node_counters_enabled;
void *heap;
vlib_stats_shared_header_t
*shared_header; /* pointer to shared memory segment */
int memfd;
} vlib_stats_segment_t;
typedef struct
{
u32 entry_index;
} vlib_stats_header_t;
typedef struct
{
vlib_stats_segment_t segment;
} vlib_stats_main_t;
extern vlib_stats_main_t vlib_stats_main;
static_always_inline vlib_stats_segment_t *
vlib_stats_get_segment ()
{
return &vlib_stats_main.segment;
}
static_always_inline vlib_stats_entry_t *
vlib_stats_get_entry (vlib_stats_segment_t *sm, u32 entry_index)
{
vlib_stats_entry_t *e;
ASSERT (entry_index < vec_len (sm->directory_vector));
e = sm->directory_vector + entry_index;
ASSERT (e->type != STAT_DIR_TYPE_EMPTY && e->type != STAT_DIR_TYPE_ILLEGAL);
return e;
}
static_always_inline void *
vlib_stats_get_entry_data_pointer (u32 entry_index)
{
vlib_stats_segment_t *sm = vlib_stats_get_segment ();
vlib_stats_entry_t *e = vlib_stats_get_entry (sm, entry_index);
return e->data;
}
clib_error_t *vlib_stats_init (vlib_main_t *vm);
void *vlib_stats_set_heap ();
void vlib_stats_segment_lock (void);
void vlib_stats_segment_unlock (void);
void vlib_stats_register_mem_heap (clib_mem_heap_t *);
f64 vlib_stats_get_segment_update_rate (void);
/* gauge */
u32 vlib_stats_add_gauge (char *fmt, ...);
void vlib_stats_set_gauge (u32 entry_index, u64 value);
/* timestamp */
u32 vlib_stats_add_timestamp (char *fmt, ...);
void vlib_stats_set_timestamp (u32 entry_index, f64 value);
/* counter vector */
u32 vlib_stats_add_counter_vector (char *fmt, ...);
/* counter pair vector */
u32 vlib_stats_add_counter_pair_vector (char *fmt, ...);
/* string vector */
typedef u8 **vlib_stats_string_vector_t;
vlib_stats_string_vector_t vlib_stats_add_string_vector (char *fmt, ...);
void vlib_stats_set_string_vector (vlib_stats_string_vector_t *sv, u32 index,
char *fmt, ...);
void vlib_stats_free_string_vector (vlib_stats_string_vector_t *sv);
/* symlink */
u32 vlib_stats_add_symlink (u32 entry_index, u32 vector_index, char *fmt, ...);
void vlib_stats_rename_symlink (u64 entry_index, char *fmt, ...);
/* common to all types */
void vlib_stats_validate (u32 entry_index, ...);
int vlib_stats_validate_will_expand (u32 entry_index, ...);
void vlib_stats_remove_entry (u32 entry_index);
u32 vlib_stats_find_entry_index (char *fmt, ...);
void vlib_stats_register_collector_fn (vlib_stats_collector_reg_t *r);
format_function_t format_vlib_stats_symlink;
#endif