STATS: Dynamically mapped shared memory segment
[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
21 /* Default socket to exchange segment fd */
22 #define STAT_SEGMENT_SOCKET_FILE "/run/vpp/stats.sock"
23
24 typedef enum
25 {
26   STAT_DIR_TYPE_ILLEGAL = 0,
27   STAT_DIR_TYPE_SCALAR_INDEX,
28   STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE,
29   STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED,
30   STAT_DIR_TYPE_ERROR_INDEX,
31 } stat_directory_type_t;
32
33 typedef enum
34 {
35  STAT_COUNTER_VECTOR_RATE = 0,
36  STAT_COUNTER_INPUT_RATE,
37  STAT_COUNTER_LAST_UPDATE,
38  STAT_COUNTER_LAST_STATS_CLEAR,
39  STAT_COUNTER_HEARTBEAT,
40  STAT_COUNTER_NODE_CLOCKS,
41  STAT_COUNTER_NODE_VECTORS,
42  STAT_COUNTER_NODE_CALLS,
43  STAT_COUNTER_NODE_SUSPENDS,
44  STAT_COUNTERS
45 } stat_segment_counter_t;
46
47 #define foreach_stat_segment_counter_name                       \
48   _(VECTOR_RATE, SCALAR_INDEX, vector_rate,)                    \
49   _(INPUT_RATE, SCALAR_INDEX, input_rate,)                      \
50   _(LAST_UPDATE, SCALAR_INDEX, last_update,)                    \
51   _(LAST_STATS_CLEAR, SCALAR_INDEX, last_stats_clear,)          \
52   _(HEARTBEAT, SCALAR_INDEX, heartbeat,)                        \
53   _(NODE_CLOCKS, COUNTER_VECTOR_SIMPLE, clocks, /node)          \
54   _(NODE_VECTORS, COUNTER_VECTOR_SIMPLE, vectors, /node)        \
55   _(NODE_CALLS, COUNTER_VECTOR_SIMPLE, calls, /node)            \
56   _(NODE_SUSPENDS, COUNTER_VECTOR_SIMPLE, suspends, /node)
57
58 typedef struct
59 {
60   stat_directory_type_t type;
61   union {
62     uint64_t offset;
63     uint64_t index;
64     uint64_t value;
65   };
66   uint64_t offset_vector;
67   char name[128]; // TODO change this to pointer to "somewhere"
68 } stat_segment_directory_entry_t;
69
70 /* Default stat segment 32m */
71 #define STAT_SEGMENT_DEFAULT_SIZE       (32<<20)
72
73 /*
74  * Shared header first in the shared memory segment.
75  */
76 typedef struct
77 {
78   atomic_int_fast64_t epoch;
79   atomic_int_fast64_t in_progress;
80   atomic_int_fast64_t directory_offset;
81   atomic_int_fast64_t error_offset;
82   atomic_int_fast64_t stats_offset;
83 } stat_segment_shared_header_t;
84
85 static inline uint64_t
86 stat_segment_offset (void *start, void *data)
87 {
88   return (char *) data - (char *) start;
89 }
90
91 static inline void *
92 stat_segment_pointer (void *start, uint64_t offset)
93 {
94   return ((char *) start + offset);
95 }
96
97 #endif