This repository was archived by the owner on Mar 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnc_stats.h
230 lines (182 loc) · 10.3 KB
/
nc_stats.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* twemproxy - A fast and lightweight proxy for memcached protocol.
* Copyright (C) 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _NC_STATS_H_
#define _NC_STATS_H_
#include <nc_core.h>
#define STATS_POOL_CODEC(ACTION) \
/* client behavior */ \
ACTION( client_eof, STATS_COUNTER, "# eof on client connections") \
ACTION( client_err, STATS_COUNTER, "# errors on client connections") \
ACTION( client_connections, STATS_GAUGE, "# active client connections") \
/* pool behavior */ \
ACTION( server_ejects, STATS_COUNTER, "# times backend server was ejected") \
/* forwarder behavior */ \
ACTION( forward_error, STATS_COUNTER, "# times we encountered a forwarding error") \
ACTION( fragments, STATS_COUNTER, "# fragments created from a multi-vector request") \
#define STATS_SERVER_CODEC(ACTION) \
/* server behavior */ \
ACTION( server_eof, STATS_COUNTER, "# eof on server connections") \
ACTION( server_err, STATS_COUNTER, "# errors on server connections") \
ACTION( server_timedout, STATS_COUNTER, "# timeouts on server connections") \
ACTION( server_connections, STATS_GAUGE, "# active server connections") \
ACTION( server_ejected_at, STATS_TIMESTAMP, "timestamp when server was ejected in usec since epoch") \
/* data behavior */ \
ACTION( requests, STATS_COUNTER, "# requests") \
ACTION( request_bytes, STATS_COUNTER, "total request bytes") \
ACTION( responses, STATS_COUNTER, "# responses") \
ACTION( response_bytes, STATS_COUNTER, "total response bytes") \
ACTION( in_queue, STATS_GAUGE, "# requests in incoming queue") \
ACTION( in_queue_bytes, STATS_GAUGE, "current request bytes in incoming queue") \
ACTION( out_queue, STATS_GAUGE, "# requests in outgoing queue") \
ACTION( out_queue_bytes, STATS_GAUGE, "current request bytes in outgoing queue") \
#define STATS_ADDR "0.0.0.0"
#define STATS_PORT 22222
#define STATS_INTERVAL (30 * 1000) /* in msec */
typedef enum stats_type {
STATS_INVALID,
STATS_COUNTER, /* monotonic accumulator */
STATS_GAUGE, /* non-monotonic accumulator */
STATS_TIMESTAMP, /* monotonic timestamp (in nsec) */
STATS_SENTINEL
} stats_type_t;
struct stats_metric {
stats_type_t type; /* type */
struct string name; /* name (ref) */
union {
int64_t counter; /* accumulating counter */
int64_t timestamp; /* monotonic timestamp */
} value;
};
struct stats_server {
struct string name; /* server name (ref) */
struct array metric; /* stats_metric[] for server codec */
};
struct stats_pool {
struct string name; /* pool name (ref) */
struct array metric; /* stats_metric[] for pool codec */
struct array server; /* stats_server[] */
};
struct stats_buffer {
size_t len; /* buffer length */
uint8_t *data; /* buffer data */
size_t size; /* buffer alloc size */
};
struct stats {
struct context *ctx;
uint16_t port; /* stats monitoring port */
int interval; /* stats aggregation interval */
struct string addr; /* stats monitoring address */
int64_t start_ts; /* start timestamp of nutcracker */
struct stats_buffer buf; /* output buffer */
struct array current; /* stats_pool[] (a) */
struct array shadow; /* stats_pool[] (b) */
struct array sum; /* stats_pool[] (c = a + b) */
pthread_t tid; /* stats aggregator thread */
int sd; /* stats descriptor */
struct string service_str; /* service string */
struct string service; /* service */
struct string source_str; /* source string */
struct string source; /* source */
struct string version_str; /* version string */
struct string version; /* version */
struct string uptime_str; /* uptime string */
struct string timestamp_str; /* timestamp string */
struct string ntotal_conn_str; /* total connections string */
struct string ncurr_conn_str; /* curr connections string */
volatile int aggregate; /* shadow (b) aggregate? */
volatile int updated; /* current (a) updated? */
};
#define DEFINE_ACTION(_name, _type, _desc) STATS_POOL_##_name,
typedef enum stats_pool_field {
STATS_POOL_CODEC(DEFINE_ACTION)
STATS_POOL_NFIELD
} stats_pool_field_t;
#undef DEFINE_ACTION
#define DEFINE_ACTION(_name, _type, _desc) STATS_SERVER_##_name,
typedef enum stats_server_field {
STATS_SERVER_CODEC(DEFINE_ACTION)
STATS_SERVER_NFIELD
} stats_server_field_t;
#undef DEFINE_ACTION
#if defined NC_STATS && NC_STATS == 1
#define stats_pool_incr(_ctx, _pool, _name) do { \
_stats_pool_incr(_ctx, _pool, STATS_POOL_##_name); \
} while (0)
#define stats_pool_decr(_ctx, _pool, _name) do { \
_stats_pool_decr(_ctx, _pool, STATS_POOL_##_name); \
} while (0)
#define stats_pool_incr_by(_ctx, _pool, _name, _val) do { \
_stats_pool_incr_by(_ctx, _pool, STATS_POOL_##_name, _val); \
} while (0)
#define stats_pool_decr_by(_ctx, _pool, _name, _val) do { \
_stats_pool_decr_by(_ctx, _pool, STATS_POOL_##_name, _val); \
} while (0)
#define stats_pool_set_ts(_ctx, _pool, _name, _val) do { \
_stats_pool_set_ts(_ctx, _pool, STATS_POOL_##_name, _val); \
} while (0)
#define stats_server_incr(_ctx, _server, _name) do { \
_stats_server_incr(_ctx, _server, STATS_SERVER_##_name); \
} while (0)
#define stats_server_decr(_ctx, _server, _name) do { \
_stats_server_decr(_ctx, _server, STATS_SERVER_##_name); \
} while (0)
#define stats_server_incr_by(_ctx, _server, _name, _val) do { \
_stats_server_incr_by(_ctx, _server, STATS_SERVER_##_name, _val); \
} while (0)
#define stats_server_decr_by(_ctx, _server, _name, _val) do { \
_stats_server_decr_by(_ctx, _server, STATS_SERVER_##_name, _val); \
} while (0)
#define stats_server_set_ts(_ctx, _server, _name, _val) do { \
_stats_server_set_ts(_ctx, _server, STATS_SERVER_##_name, _val); \
} while (0)
#else
#define stats_pool_incr(_ctx, _pool, _name)
#define stats_pool_decr(_ctx, _pool, _name)
#define stats_pool_incr_by(_ctx, _pool, _name, _val)
#define stats_pool_decr_by(_ctx, _pool, _name, _val)
#define stats_server_incr(_ctx, _server, _name)
#define stats_server_decr(_ctx, _server, _name)
#define stats_server_incr_by(_ctx, _server, _name, _val)
#define stats_server_decr_by(_ctx, _server, _name, _val)
#define stats_server_set_ts(_ctx, _server, _name, _val)
#endif
#define stats_enabled NC_STATS
void stats_describe(void);
void _stats_pool_incr(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx);
void _stats_pool_decr(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx);
void _stats_pool_incr_by(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx, int64_t val);
void _stats_pool_decr_by(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx, int64_t val);
void _stats_pool_set_ts(struct context *ctx, struct server_pool *pool, stats_pool_field_t fidx, int64_t val);
void _stats_server_incr(struct context *ctx, struct server *server, stats_server_field_t fidx);
void _stats_server_decr(struct context *ctx, struct server *server, stats_server_field_t fidx);
void _stats_server_incr_by(struct context *ctx, struct server *server, stats_server_field_t fidx, int64_t val);
void _stats_server_decr_by(struct context *ctx, struct server *server, stats_server_field_t fidx, int64_t val);
void _stats_server_set_ts(struct context *ctx, struct server *server, stats_server_field_t fidx, int64_t val);
struct stats *stats_create(struct context *ctx, char *source);
void stats_destroy(struct stats *stats);
void stats_swap(struct stats *stats);
void stats_aggregate(struct context *ctx);
rstatus_t stats_make_rsp(struct stats *st);
int STATS_LOCK_INIT(void);
int STATS_LOCK_DESTROY(void);
void STATS_LOCK(void);
void STATS_UNLOCK(void);
int thread_stats_lock_init(struct context *ctx);
int thread_stats_lock_destroy(struct context *ctx);
void thread_stats_lock(struct context *ctx);
void thread_stats_unlock(struct context *ctx);
#endif