{{ message }}
Repository navigation
Expand file tree
/
Copy pathcounters.c
More file actions
258 lines (229 loc) · 8.45 KB
/
Copy pathcounters.c
File metadata and controls
258 lines (229 loc) · 8.45 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* objects counters management
*
* Copyright 2025 HAProxy Technologies
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <haproxy/atomic.h>
#include <haproxy/clock.h>
#include <haproxy/counters.h>
#include <haproxy/global.h>
#include <haproxy/guid.h>
#include <haproxy/stats-file.h>
#include <haproxy/time.h>
#include <haproxy/tools.h>
static void _counters_shared_drop(void *counters)
{
struct counters_shared *shared = counters;
int it = 0;
if (!shared)
return;
while (it < global.nbtgroups && shared->tg && shared->tg[it]) {
if (shared->flags & COUNTERS_SHARED_F_LOCAL) {
/* memory was allocated using calloc(), simply free it */
free(shared->tg[it]);
}
else {
struct shm_stats_file_object *obj;
/* inside shared memory, retrieve associated object and remove
* ourselves from its users
*/
obj = container_of(shared->tg[it], struct shm_stats_file_object, data);
HA_ATOMIC_OR(&obj->users, (1 << shm_stats_file_slot));
}
it += 1;
}
free(shared->tg);
}
/* release a shared fe counters struct */
void counters_fe_shared_drop(struct fe_counters_shared *counters)
{
_counters_shared_drop(counters);
}
/* release a shared be counters struct */
void counters_be_shared_drop(struct be_counters_shared *counters)
{
_counters_shared_drop(counters);
}
/* prepare shared counters pointers for a given <shared> parent
* pointer and for <guid> object
* <is_be> hint is expected to be set to 1 when the guid refers to be_shared
* struct, else fe_shared stuct is assumed.
*
* if <guid> is not set, then sharing is disabled
* Returns the pointer on success or NULL on failure, in which case
* <errmsg> may contain additional hints about the error and must be freed accordingly
*/
static int _counters_shared_prepare(struct counters_shared *shared,
const struct guid_node *guid, int is_be, char **errmsg)
{
struct fe_counters_shared *fe_shared;
struct be_counters_shared *be_shared;
int it = 0;
if (!guid->key || !shm_stats_file_hdr)
shared->flags |= COUNTERS_SHARED_F_LOCAL;
if (!shared->tg) {
shared->tg = calloc(global.nbtgroups, sizeof(*shared->tg));
if (!shared->tg) {
memprintf(errmsg, "couldn't allocate memory for shared counters");
return 0;
}
}
while (it < global.nbtgroups) {
if (shared->flags & COUNTERS_SHARED_F_LOCAL) {
size_t tg_size;
tg_size = (is_be) ? sizeof(*be_shared->tg[0]) : sizeof(*fe_shared->tg[0]);
shared->tg[it] = calloc(1, tg_size);
if (!shared->tg[it])
memprintf(errmsg, "memory error, calloc failed");
}
else if (!shared->tg[it]) {
struct shm_stats_file_object *shm_obj;
shm_obj = shm_stats_file_add_object(errmsg);
if (shm_obj) {
snprintf(shm_obj->guid, sizeof(shm_obj->guid), "%s", guid_get(guid));
if (is_be) {
shm_obj->type = SHM_STATS_FILE_OBJECT_TYPE_BE;
be_shared = (struct be_counters_shared *)shared;
be_shared->tg[it] = &shm_obj->data.be;
}
else {
shm_obj->type = SHM_STATS_FILE_OBJECT_TYPE_FE;
fe_shared = (struct fe_counters_shared *)shared;
fe_shared->tg[it] = &shm_obj->data.fe;
}
/* we use atomic op to make the object visible by setting valid tgid value */
HA_ATOMIC_STORE(&shm_obj->tgid, it + 1);
}
}
if (!shared->tg[it]) {
_counters_shared_drop(shared);
return 0;
}
it += 1;
}
/* initial values:
* only set one group, only latest value is considered
*/
HA_ATOMIC_STORE(&shared->tg[0]->last_state_change, ns_to_sec(now_ns));
return 1;
}
/* prepare shared fe counters pointer for a given <guid> object */
int counters_fe_shared_prepare(struct fe_counters_shared *shared, const struct guid_node *guid, char **errmsg)
{
return _counters_shared_prepare((struct counters_shared *)shared, guid, 0, errmsg);
}
/* prepare shared be counters pointer for a given <guid> object */
int counters_be_shared_prepare(struct be_counters_shared *shared, const struct guid_node *guid, char **errmsg)
{
return _counters_shared_prepare((struct counters_shared *)shared, guid, 1, errmsg);
}
/* Reset the contents of the per-thread-group shared counters attached to
* <shared>, while preserving the shared.tg pointer array and the per-tgroup
* allocations themselves. This is the safe way to reset counters at runtime:
* the hot path dereferences shared.tg[tgid-1] with no NULL check, so nulling
* that pointer (as a blanket memset on the outer struct would do) segfaults on
* the next request. The shared.flags field is also preserved because
* COUNTERS_SHARED_F_LOCAL is set once at init and reflects allocation
* ownership, not counter state.
*
* In shared-memory (SHM stats file) mode, zeroing the per-tgroup structs
* affects every process attached to the same object. This is the intended
* "reset everywhere" semantic of clear counters.
*/
static void _counters_shared_reset(struct counters_shared *shared, size_t tg_size)
{
int it;
if (!shared || !shared->tg)
return;
for (it = 0; it < global.nbtgroups; it++) {
if (shared->tg[it])
memset(shared->tg[it], 0, tg_size);
}
}
/* Reset all counter fields of <counters> while preserving the shared.tg
* pointer array and the per-tgroup allocations. See _counters_shared_reset()
* for the pointer-lifetime rationale. Both the per-tgroup accumulated
* counters (bytes, sessions, requests, errors, response codes, ...) and the
* local max/rate counters are cleared.
*/
void counters_fe_reset(struct fe_counters *counters)
{
if (!counters)
return;
_counters_shared_reset((struct counters_shared *)&counters->shared,
sizeof(struct fe_counters_shared_tg));
/* Zero all local (non-shared) fields. Note this deliberately is NOT a
* plain memset() over the whole struct: shared is the first member and
* holds the shared.tg pointer array that the hot path dereferences
* without a NULL check, so blanket-zeroing it would segfault the next
* request. We skip exactly the shared sub-struct and clear the rest;
* every field after it is numeric, so any field added later is reset
* automatically without touching this code.
*/
memset((char *)counters + sizeof(counters->shared), 0,
sizeof(*counters) - sizeof(counters->shared));
}
/* Reset all counter fields of <counters>. See counters_fe_reset(). */
void counters_be_reset(struct be_counters *counters)
{
if (!counters)
return;
_counters_shared_reset((struct counters_shared *)&counters->shared,
sizeof(struct be_counters_shared_tg));
memset((char *)counters + sizeof(counters->shared), 0,
sizeof(*counters) - sizeof(counters->shared));
}
/* Reset only the max/peak gauges of a frontend/listener <counters> struct,
* leaving cumulative counters intact. Used by the plain "clear counters"
* command (as opposed to "clear counters all" which resets everything via
* counters_fe_reset()).
*/
void counters_fe_reset_max(struct fe_counters *counters)
{
if (!counters)
return;
counters->conn_max = 0;
counters->cps_max = 0;
counters->sps_max = 0;
counters->p.http.rps_max = 0;
}
/* Reset only the max/peak gauges of a backend/server <counters> struct,
* leaving cumulative counters intact. See counters_fe_reset_max().
*
* Note: this clears the union of all max gauges present in the struct. The
* previous inline code cleared slightly different subsets for backends and
* servers (backends left cur_sess_max untouched, servers left conn_max /
* cps_max / rps_max untouched); those were latent gaps, so plain "clear
* counters" now consistently resets every peak gauge for both.
*/
void counters_be_reset_max(struct be_counters *counters)
{
if (!counters)
return;
counters->conn_max = 0;
counters->cps_max = 0;
counters->sps_max = 0;
counters->nbpend_max = 0;
counters->cur_sess_max = 0;
counters->qtime_max = 0;
counters->ctime_max = 0;
counters->dtime_max = 0;
counters->ttime_max = 0;
counters->p.http.rps_max = 0;
}
You can’t perform that action at this time.
