Line data Source code
1 : #ifndef HEADER_fd_src_disco_metrics_fd_metrics_h
2 : #define HEADER_fd_src_disco_metrics_fd_metrics_h
3 :
4 : #include "fd_metrics_base.h"
5 :
6 : #include "generated/fd_metrics_all.h"
7 :
8 : #include "../../tango/tempo/fd_tempo.h"
9 :
10 : /* fd_metrics mostly defines way of laying out metrics in shared
11 : memory so that a producer and consumer can agree on where they
12 : are, and can read and wite them quickly and with little to no
13 : boilerplate.
14 :
15 : At initialization time, a thread can call fd_metrics_register
16 : which saves a thread local base pointer. Then, macros are provided
17 : which given a macro "name", maps it to an offset from that base
18 : pointer and does a write of the corresponding ulong.
19 :
20 : For low-frequency metrics like incrementing rarely hit error
21 : counters, it is OK to use the macros inline. For high frequency
22 : metrics in core loops, it may be preferable to accumulate local
23 : metric values in the tile and drain them to the metrics shared
24 : memory periodically, eg, via. a housekeeping step.
25 :
26 : The metrics area is minimal and contains no metadata itself. For
27 : example, histograms in the metrics shared memory are just the bucket
28 : values, and there is no metadata about the edges. The consumer will
29 : determine the edges by looking at the statically compiled metadata.
30 :
31 : This is to reduce cache traffic and keep the metrics area small, so
32 : it can be copied to produce a snapshot quickly. When updating
33 : metrics, the producer should do atomic writes so that these snapshots
34 : will see consistent values. In particular, the producer should not
35 : do a memcpy into the metrics region. */
36 :
37 : /* The metrics region is laid out like
38 :
39 : [ in_link_N ulong ]
40 : [ out_link_N ulong]
41 : [ in_link_0_metrics ... in_link_N_metrics ]
42 : [ out_link_0_metrics ... out_link_N_metrics ]
43 : [ tile_metrics ]
44 :
45 : where every value is a ulong. Tile metrics come after link metrics,
46 : so this base pointer points at the very start of the layout. You
47 : shouldn't need to use this directly, instead it's used by the mux
48 : tile when it's computing the metrics for specific links. */
49 : extern FD_TL ulong * fd_metrics_base_tl;
50 :
51 : /* All metrics in the application are ulongs, and are laid out
52 : sequentially, so this thread local is a pointer to the first tile
53 : specific metric in the layout, or the "tile_metrics" start as defined
54 : above. All tile metrics are defined as an offset from this metrics
55 : pointer. You shouldn't need to use this directly, instead it is used
56 : by the macros below like FD_MCNT_SET etc. The thread local should be
57 : set by calling fd_metrics_register. */
58 : extern FD_TL volatile ulong * fd_metrics_tl;
59 :
60 51 : #define FD_METRICS_ALIGN (128UL)
61 : #define FD_METRICS_FOOTPRINT(in_link_cnt, out_link_reliable_consumer_cnt) \
62 57 : FD_LAYOUT_FINI( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND( FD_LAYOUT_APPEND ( FD_LAYOUT_APPEND ( FD_LAYOUT_INIT, \
63 57 : 8UL, 16UL ), \
64 57 : 8UL, (in_link_cnt)*FD_METRICS_ALL_LINK_IN_TOTAL*sizeof(ulong) ), \
65 57 : 8UL, (out_link_reliable_consumer_cnt)*FD_METRICS_ALL_LINK_OUT_TOTAL*sizeof(ulong) ), \
66 57 : 8UL, FD_METRICS_TOTAL_SZ ), \
67 57 : FD_METRICS_ALIGN )
68 :
69 : /* The following macros are convenience helpers for updating tile metric
70 : values in shared memory, and can be used like
71 :
72 : FD_MGAUGE_SET( QUIC, CONNECTIONS_CREATED_COUNT, conn_cnt );
73 :
74 : This compiles to a single write to an offset of the metrics pointer
75 : above. */
76 :
77 2217816 : #define FD_MGAUGE_SET( group, measurement, value ) do { \
78 2217816 : fd_metrics_tl[ MIDX(GAUGE, group, measurement) ] = (value); \
79 2217816 : } while(0)
80 :
81 0 : #define FD_MGAUGE_GET( group, measurement ) (fd_metrics_tl[ MIDX(GAUGE, group, measurement) ])
82 :
83 0 : #define FD_MCNT_GET( group, measurement ) (fd_metrics_tl[ MIDX(COUNTER, group, measurement) ])
84 :
85 0 : #define FD_MCNT_SET( group, measurement, value ) do { \
86 0 : fd_metrics_tl[ MIDX(COUNTER, group, measurement) ] = (value); \
87 0 : } while(0)
88 :
89 4559091 : #define FD_MCNT_INC( group, measurement, value ) do { \
90 4559091 : fd_metrics_tl[ MIDX(COUNTER, group, measurement) ] += (value); \
91 4559091 : } while(0)
92 :
93 2610 : #define FD_MHIST_MIN( group, measurement ) (FD_METRICS_HISTOGRAM_##group##_##measurement##_MIN)
94 0 : #define FD_MHIST_SECONDS_MIN( group, measurement ) (fd_metrics_convert_seconds_to_ticks(FD_METRICS_HISTOGRAM_##group##_##measurement##_MIN))
95 2610 : #define FD_MHIST_MAX( group, measurement ) (FD_METRICS_HISTOGRAM_##group##_##measurement##_MAX)
96 0 : #define FD_MHIST_SECONDS_MAX( group, measurement ) (fd_metrics_convert_seconds_to_ticks(FD_METRICS_HISTOGRAM_##group##_##measurement##_MAX))
97 :
98 13230 : #define FD_MHIST_COPY( group, measurement, hist ) do { \
99 13230 : ulong __fd_metrics_off = MIDX(HISTOGRAM, group, measurement); \
100 224910 : for( ulong i=0; i<FD_HISTF_BUCKET_CNT; i++ ) { \
101 211680 : fd_metrics_tl[ __fd_metrics_off + i ] = hist->counts[ i ]; \
102 211680 : } \
103 13230 : fd_metrics_tl[ __fd_metrics_off + FD_HISTF_BUCKET_CNT ] = hist->sum; \
104 13230 : } while(0)
105 :
106 : #define FD_MHIST_SUM( group, measurement ) (fd_metrics_tl[ MIDX(HISTOGRAM, group, measurement) + FD_HISTF_BUCKET_CNT ])
107 :
108 0 : #define FD_MCNT_ENUM_COPY( group, measurement, values ) do { \
109 0 : ulong __fd_metrics_off = MIDX(COUNTER, group, measurement); \
110 0 : for( ulong i=0; i<FD_METRICS_COUNTER_##group##_##measurement##_CNT; i++ ) { \
111 0 : fd_metrics_tl[ __fd_metrics_off + i ] = values[ i ]; \
112 0 : } \
113 0 : } while(0)
114 :
115 : #define FD_MGAUGE_ENUM_COPY( group, measurement, values ) do { \
116 : ulong __fd_metrics_off = MIDX(GAUGE, group, measurement); \
117 : for( ulong i=0; i<FD_METRICS_GAUGE_##group##_##measurement##_CNT; i++ ) { \
118 : fd_metrics_tl[ __fd_metrics_off + i ] = values[ i ]; \
119 : } \
120 : } while(0)
121 :
122 : FD_PROTOTYPES_BEGIN
123 :
124 : /* fd_metrics_tile returns a pointer to the tile-specific metrics area
125 : for the given metrics object. */
126 : static inline volatile ulong *
127 6 : fd_metrics_tile( ulong * metrics ) { return metrics + 2UL + FD_METRICS_ALL_LINK_IN_TOTAL*metrics[ 0 ] + FD_METRICS_ALL_LINK_OUT_TOTAL*metrics[ 1 ]; }
128 :
129 : /* fd_metrics_link_in returns a pointer the in-link metrics area for the
130 : given in link index of this metrics object. */
131 : static inline volatile ulong *
132 0 : fd_metrics_link_in( ulong * metrics, ulong in_idx ) { return metrics + 2UL + FD_METRICS_ALL_LINK_IN_TOTAL*in_idx; }
133 :
134 : /* fd_metrics_link_in returns a pointer the in-link metrics area for the
135 : given out link index of this metrics object. */
136 : static inline volatile ulong *
137 0 : fd_metrics_link_out( ulong * metrics, ulong out_idx ) { return metrics + 2UL + FD_METRICS_ALL_LINK_IN_TOTAL*metrics[0] + FD_METRICS_ALL_LINK_OUT_TOTAL*out_idx; }
138 :
139 : /* fd_metrics_new formats an unused memory region for use as a metrics.
140 : Assumes shmem is a non-NULL pointer to this region in the local
141 : address space with the required footprint and alignment. All of the
142 : mtrics will be initialized to zero. Returns shmem (and the memory
143 : region it points to will be formatted as a metrics, caller is not
144 : joined). */
145 :
146 : static inline void *
147 : fd_metrics_new( void * shmem,
148 : ulong in_link_cnt,
149 6 : ulong out_link_consumer_cnt ) {
150 6 : fd_memset( shmem, 0, FD_METRICS_FOOTPRINT(in_link_cnt, out_link_consumer_cnt) );
151 6 : ulong * metrics = shmem;
152 6 : metrics[0] = in_link_cnt;
153 6 : metrics[1] = out_link_consumer_cnt;
154 6 : return shmem;
155 6 : }
156 :
157 : /* fd_metrics_register sets the thread local values used by the macros
158 : like FD_MCNT_SET to point to the provided metrics object. */
159 : static inline ulong *
160 6 : fd_metrics_register( ulong * metrics ) {
161 6 : if( FD_UNLIKELY( !metrics ) ) FD_LOG_ERR(( "NULL metrics" ));
162 :
163 6 : fd_metrics_base_tl = metrics;
164 6 : fd_metrics_tl = fd_metrics_tile( metrics );
165 6 : return metrics;
166 6 : }
167 :
168 : static inline ulong
169 0 : fd_metrics_convert_seconds_to_ticks( double seconds ) {
170 : /* The tick_per_ns() value needs to be the same across the tile doing
171 : the sampling and the tile doing the reporting so that they compute
172 : the same bucket edges for histograms. */
173 0 : double tick_per_ns = fd_tempo_tick_per_ns( NULL );
174 0 : return (ulong)(seconds * tick_per_ns * 1e9);
175 0 : }
176 :
177 : static inline double
178 0 : fd_metrics_convert_ticks_to_seconds( ulong ticks ) {
179 0 : double tick_per_ns = fd_tempo_tick_per_ns( NULL );
180 0 : return (double)ticks / (tick_per_ns * 1e9);
181 0 : }
182 :
183 : static inline ulong
184 0 : fd_metrics_convert_ticks_to_nanoseconds( ulong ticks ) {
185 0 : double tick_per_ns = fd_tempo_tick_per_ns( NULL );
186 0 : return (ulong)((double)ticks / tick_per_ns);
187 0 : }
188 :
189 0 : static inline ulong * fd_metrics_join ( void * mem ) { return mem; }
190 0 : static inline void * fd_metrics_leave ( void * mem ) { return (void *)mem; }
191 0 : static inline void * fd_metrics_delete( void * mem ) { return (void *)mem; }
192 :
193 : FD_PROTOTYPES_END
194 :
195 : #endif /* HEADER_fd_src_disco_metrics_fd_metrics_h */
|