forked from cheekyguy/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.h
289 lines (255 loc) · 12.9 KB
/
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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#if !defined(_stats_h_)
#define _stats_h
#include <assert.h>
typedef enum prefix_stats_flags_e prefix_stats_flags_t;
enum prefix_stats_flags_e {
PREFIX_INCR_ITEM_COUNT = 0x1,
PREFIX_IS_OVERWRITE = 0x2,
};
/* stats */
extern void stats_prefix_init(void);
extern void stats_prefix_clear(void);
extern void stats_prefix_record_get(const char *key, const size_t nkey, const size_t nbytes, const bool is_hit);
extern void stats_prefix_record_delete(const char *key, const size_t nkey);
extern void stats_prefix_record_set(const char *key, const size_t nkey);
extern void stats_prefix_record_byte_total_change(const char *key, const size_t nkey, const long bytes, const int prefix_stats_flags);
extern void stats_prefix_record_removal(const char *key, const size_t nkey, const size_t bytes, const rel_time_t time, const long flags);
/*@null@*/
extern char *stats_prefix_dump(int *length);
#if defined(STATS_BUCKETS)
#define BUCKETS_RANGE(start, end, skip) uint64_t size_ ## start ## _ ## end [ ((end-start) / skip) ];
typedef struct _size_buckets SIZE_BUCKETS;
struct _size_buckets {
#include "buckets.h"
};
extern SIZE_BUCKETS set;
extern SIZE_BUCKETS hit;
extern SIZE_BUCKETS evict;
extern SIZE_BUCKETS delete;
extern SIZE_BUCKETS overwrite;
extern SIZE_BUCKETS expires;
#endif /* #if defined(STATS_BUCKETS) */
#if defined(COST_BENEFIT_STATS)
#define BUCKETS_RANGE(start, end, skip) \
uint64_t hits_ ## start ## _ ## end [ ((end-start) / skip) ]; \
uint64_t slot_seconds_ ## start ## _ ## end [ ((end-start) / skip) ]; \
rel_time_t last_update_ ## start ## _ ## end [ ((end-start) / skip) ]; \
uint32_t slots_ ## start ## _ ## end [ ((end-start) / skip) ];
typedef struct cost_benefit_buckets_s cost_benefit_buckets_t;
struct cost_benefit_buckets_s {
#include "buckets.h"
};
extern cost_benefit_buckets_t cb_buckets;
#endif /* #if defined(COST_BENEFIT_STATS) */
/* stats size buckets */
extern void stats_buckets_init(void);
extern void stats_cost_benefit_init(void);
static inline void stats_set(size_t sz, size_t overwritten_sz) {
#if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS)
GLOBAL_STATS_LOCK();
#if defined(STATS_BUCKETS)
if (overwritten_sz != 0) {
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (overwritten_sz >= start && overwritten_sz < end) { \
overwrite.size_ ## start ## _ ## end[ (overwritten_sz - start) / skip ] ++; \
break; \
} \
} while (0);
#include "buckets.h"
}
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
set.size_ ## start ## _ ## end[ (sz - start) / skip ] ++; \
break; \
} \
} while (0);
#include "buckets.h"
#endif /* #if defined(STATS_BUCKETS) */
#if defined(COST_BENEFIT_STATS)
{
uint64_t* from_slot_seconds_ptr = NULL, * to_slot_seconds_ptr = NULL;
rel_time_t* from_last_update_ptr = NULL, * to_last_update_ptr = NULL;
uint32_t* from_slots_ptr = NULL, * to_slots_ptr = NULL;
rel_time_t now = current_time;
if (overwritten_sz != 0) {
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (overwritten_sz >= start && overwritten_sz < end) { \
unsigned slot = (overwritten_sz - start) / skip; \
from_slot_seconds_ptr = &cb_buckets.slot_seconds_ ## start ## _ ## end[slot]; \
from_last_update_ptr = &cb_buckets.last_update_ ## start ## _ ## end[slot]; \
from_slots_ptr = &cb_buckets.slots_ ## start ## _ ## end[slot]; \
break; \
} \
} while (0);
#include "buckets.h"
}
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
unsigned slot = (sz - start) / skip; \
to_slot_seconds_ptr = &cb_buckets.slot_seconds_ ## start ## _ ## end[slot]; \
to_last_update_ptr = &cb_buckets.last_update_ ## start ## _ ## end[slot]; \
to_slots_ptr = &cb_buckets.slots_ ## start ## _ ## end[slot]; \
break; \
} \
} while (0);
#include "buckets.h"
assert( (from_slot_seconds_ptr == NULL && from_last_update_ptr == NULL && from_slots_ptr == NULL) ||
(from_slot_seconds_ptr != NULL && from_last_update_ptr != NULL && from_slots_ptr != NULL) );
assert(to_slot_seconds_ptr != NULL);
assert(to_last_update_ptr != NULL);
assert(to_slots_ptr != NULL);
if (from_slots_ptr != to_slots_ptr) {
/* only need to do an update if the item has changed slots. */
if (from_slots_ptr) {
/* we are doing an overwrite, so refresh the from slot. */
(*from_slot_seconds_ptr) += (*from_slots_ptr) * (now - *from_last_update_ptr);
assert((*from_slots_ptr) > 0);
(*from_slots_ptr) --;
(*from_last_update_ptr) = now;
}
/* the to_slot is always updated */
(*to_slot_seconds_ptr) += (*to_slots_ptr) * (now - *to_last_update_ptr);
(*to_slots_ptr) ++;
(*to_last_update_ptr) = now;
}
}
#endif /* #if defined(COST_BENEFIT_STATS) */
GLOBAL_STATS_UNLOCK();
#endif /* #if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS) */
}
static inline void stats_get(size_t sz) {
#if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS)
GLOBAL_STATS_LOCK();
#if defined(STATS_BUCKETS)
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
hit.size_ ## start ## _ ## end[ (sz - start) / skip ] ++; \
break; \
} \
} while (0);
#include "buckets.h"
#endif /* #if defined(STATS_BUCKETS) */
#if defined(COST_BENEFIT_STATS)
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
cb_buckets.hits_ ## start ## _ ## end[ (sz - start) / skip ] ++; \
break; \
} \
} while (0);
#include "buckets.h"
#endif /* #if defined(COST_BENEFIT_STATS) */
GLOBAL_STATS_UNLOCK();
#endif /* #if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS) */
}
static inline void stats_evict(size_t sz) {
#if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS)
GLOBAL_STATS_LOCK();
#if defined(STATS_BUCKETS)
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
evict.size_ ## start ## _ ## end[ (sz - start) / skip ] ++; \
break; \
} \
} while (0);
#include "buckets.h"
#endif /* #if defined(STATS_BUCKETS) */
#if defined(COST_BENEFIT_STATS)
{
rel_time_t now = current_time;
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
unsigned slot = (sz - start) / skip; \
cb_buckets.slot_seconds_ ## start ## _ ## end[slot] += \
(now - cb_buckets.last_update_ ## start ## _ ## end [slot]) * \
cb_buckets.slots_ ## start ## _ ## end [slot]; \
assert(cb_buckets.slots_ ## start ## _ ## end [slot] > 0); \
cb_buckets.slots_ ## start ## _ ## end [slot] --; \
break; \
} \
} while (0);
#include "buckets.h"
}
#endif /* #if defined(COST_BENEFIT_STATS) */
GLOBAL_STATS_UNLOCK();
#endif /* #if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS) */
}
static inline void stats_delete(size_t sz) {
#if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS)
GLOBAL_STATS_LOCK();
#if defined(STATS_BUCKETS)
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
delete.size_ ## start ## _ ## end[ (sz - start) / skip ] ++; \
break; \
} \
} while (0);
#include "buckets.h"
#endif /* #if defined(STATS_BUCKETS) */
#if defined(COST_BENEFIT_STATS)
{
rel_time_t now = current_time;
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
unsigned slot = (sz - start) / skip; \
cb_buckets.slot_seconds_ ## start ## _ ## end[slot] += \
(now - cb_buckets.last_update_ ## start ## _ ## end [slot]) * \
cb_buckets.slots_ ## start ## _ ## end [slot]; \
assert(cb_buckets.slots_ ## start ## _ ## end [slot] > 0); \
cb_buckets.slots_ ## start ## _ ## end [slot] --; \
break; \
} \
} while (0);
#include "buckets.h"
}
#endif /* #if defined(COST_BENEFIT_STATS) */
GLOBAL_STATS_UNLOCK();
#endif /* #if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS) */
}
static inline void stats_expire(size_t sz) {
#if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS)
GLOBAL_STATS_LOCK();
#if defined(STATS_BUCKETS)
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
expires.size_ ## start ## _ ## end[ (sz - start) / skip ] ++; \
break; \
} \
} while (0);
#include "buckets.h"
#endif /* #if defined(STATS_BUCKETS) */
#if defined(COST_BENEFIT_STATS)
{
rel_time_t now = current_time;
#define BUCKETS_RANGE(start, end, skip) \
do { \
if (sz >= start && sz < end) { \
unsigned slot = (sz - start) / skip; \
cb_buckets.slot_seconds_ ## start ## _ ## end[slot] += \
(now - cb_buckets.last_update_ ## start ## _ ## end [slot]) * \
cb_buckets.slots_ ## start ## _ ## end [slot]; \
assert(cb_buckets.slots_ ## start ## _ ## end [slot] > 0); \
cb_buckets.slots_ ## start ## _ ## end [slot] --; \
break; \
} \
} while (0);
#include "buckets.h"
}
#endif /* #if defined(COST_BENEFIT_STATS) */
GLOBAL_STATS_UNLOCK();
#endif /* #if defined(STATS_BUCKETS) || defined(COST_BENEFIT_STATS) */
}
extern char* item_stats_buckets(int *bytes);
extern char* cost_benefit_stats(int *bytes);
#endif /* #if !defined(_stats_h_) */