stats: Add interface name to sw_if_index as /if/names
[vpp.git] / src / vpp / stats / stat_segment.h
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef included_stat_segment_h
17 #define included_stat_segment_h
18
19 #include <stdatomic.h>
20 #include <vlib/vlib.h>
21 #include <vppinfra/socket.h>
22 #include <vpp-api/client/stat_client.h>
23
24 typedef enum
25 {
26  STAT_COUNTER_VECTOR_RATE = 0,
27  STAT_COUNTER_INPUT_RATE,
28  STAT_COUNTER_LAST_UPDATE,
29  STAT_COUNTER_LAST_STATS_CLEAR,
30  STAT_COUNTER_HEARTBEAT,
31  STAT_COUNTER_NODE_CLOCKS,
32  STAT_COUNTER_NODE_VECTORS,
33  STAT_COUNTER_NODE_CALLS,
34  STAT_COUNTER_NODE_SUSPENDS,
35  STAT_COUNTER_INTERFACE_NAMES,
36  STAT_COUNTERS
37 } stat_segment_counter_t;
38
39 #define foreach_stat_segment_counter_name                       \
40   _(VECTOR_RATE, SCALAR_INDEX, vector_rate, /sys)               \
41   _(INPUT_RATE, SCALAR_INDEX, input_rate, /sys)                 \
42   _(LAST_UPDATE, SCALAR_INDEX, last_update, /sys)               \
43   _(LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear, /sys)     \
44   _(HEARTBEAT, SCALAR_INDEX, heartbeat, /sys)                   \
45   _(NODE_CLOCKS, COUNTER_VECTOR_SIMPLE, clocks, /sys/node)      \
46   _(NODE_VECTORS, COUNTER_VECTOR_SIMPLE, vectors, /sys/node)    \
47   _(NODE_CALLS, COUNTER_VECTOR_SIMPLE, calls, /sys/node)        \
48   _(NODE_SUSPENDS, COUNTER_VECTOR_SIMPLE, suspends, /sys/node)  \
49   _(INTERFACE_NAMES, NAME_VECTOR, names, /if)
50
51 typedef struct
52 {
53   stat_directory_type_t type;
54   union {
55     uint64_t offset;
56     uint64_t index;
57     uint64_t value;
58   };
59   uint64_t offset_vector;
60   char name[128]; // TODO change this to pointer to "somewhere"
61 } stat_segment_directory_entry_t;
62
63 /* Default stat segment 32m */
64 #define STAT_SEGMENT_DEFAULT_SIZE       (32<<20)
65
66 /*
67  * Shared header first in the shared memory segment.
68  */
69 typedef struct
70 {
71   atomic_int_fast64_t epoch;
72   atomic_int_fast64_t in_progress;
73   atomic_int_fast64_t directory_offset;
74   atomic_int_fast64_t error_offset;
75   atomic_int_fast64_t stats_offset;
76 } stat_segment_shared_header_t;
77
78 static inline uint64_t
79 stat_segment_offset (void *start, void *data)
80 {
81   return (char *) data - (char *) start;
82 }
83
84 static inline void *
85 stat_segment_pointer (void *start, uint64_t offset)
86 {
87   return ((char *) start + offset);
88 }
89
90 typedef void (*stat_segment_update_fn)(stat_segment_directory_entry_t * e, u32 i);
91
92 typedef struct {
93   u32 directory_index;
94   stat_segment_update_fn fn;
95   u32 caller_index;
96 } stat_segment_gauges_pool_t;
97
98 typedef struct
99 {
100   /* internal, does not point to shared memory */
101   stat_segment_gauges_pool_t *gauges;
102
103   /* statistics segment */
104   uword *directory_vector_by_name;
105   stat_segment_directory_entry_t *directory_vector;
106   u8 **interfaces;
107
108   clib_spinlock_t *stat_segment_lockp;
109   clib_socket_t *socket;
110   u8 *socket_name;
111   ssize_t memory_size;
112   u8 node_counters_enabled;
113   void *heap;
114   stat_segment_shared_header_t *shared_header;  /* pointer to shared memory segment */
115   int memfd;
116
117   u64 last_input_packets; // OLE REMOVE?
118 } stat_segment_main_t;
119
120 extern stat_segment_main_t stat_segment_main;
121
122 clib_error_t *
123 stat_segment_register_gauge (u8 *names, stat_segment_update_fn update_fn, u32 index);
124
125 #endif