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