Skip to content

Commit cb01d50

Browse files
committed
clean up global stats code a little.
tons of stats were left in the global stats structure that're no longer used, and it looks like we kept accidentally adding new ones in there. There's also an unused mutex. Split global stats into `stats` and `stats_state`. initialize via memset, reset only `stats` via memset, removing several places where stats values get repeated. Looks much cleaner and should be less error prone.
1 parent 80ce010 commit cb01d50

File tree

7 files changed

+65
-84
lines changed

7 files changed

+65
-84
lines changed

assoc.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ void assoc_init(const int hashtable_init) {
7070
exit(EXIT_FAILURE);
7171
}
7272
STATS_LOCK();
73-
stats.hash_power_level = hashpower;
74-
stats.hash_bytes = hashsize(hashpower) * sizeof(void *);
73+
stats_state.hash_power_level = hashpower;
74+
stats_state.hash_bytes = hashsize(hashpower) * sizeof(void *);
7575
STATS_UNLOCK();
7676
}
7777

@@ -134,9 +134,9 @@ static void assoc_expand(void) {
134134
expanding = true;
135135
expand_bucket = 0;
136136
STATS_LOCK();
137-
stats.hash_power_level = hashpower;
138-
stats.hash_bytes += hashsize(hashpower) * sizeof(void *);
139-
stats.hash_is_expanding = 1;
137+
stats_state.hash_power_level = hashpower;
138+
stats_state.hash_bytes += hashsize(hashpower) * sizeof(void *);
139+
stats_state.hash_is_expanding = true;
140140
STATS_UNLOCK();
141141
} else {
142142
primary_hashtable = old_hashtable;
@@ -238,8 +238,8 @@ static void *assoc_maintenance_thread(void *arg) {
238238
expanding = false;
239239
free(old_hashtable);
240240
STATS_LOCK();
241-
stats.hash_bytes -= hashsize(hashpower - 1) * sizeof(void *);
242-
stats.hash_is_expanding = 0;
241+
stats_state.hash_bytes -= hashsize(hashpower - 1) * sizeof(void *);
242+
stats_state.hash_is_expanding = false;
243243
STATS_UNLOCK();
244244
if (settings.verbose > 1)
245245
fprintf(stderr, "Hash table expansion done\n");

globals.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ volatile rel_time_t current_time;
2020

2121
/** exported globals **/
2222
struct stats stats;
23+
struct stats_state stats_state;
2324
struct settings settings;
2425
struct slab_rebalance slab_rebal;
2526
volatile int slab_rebalance_signal;

items.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ typedef struct {
3333
uint64_t reclaimed;
3434
uint64_t outofmemory;
3535
uint64_t tailrepairs;
36-
uint64_t expired_unfetched;
37-
uint64_t evicted_unfetched;
36+
uint64_t expired_unfetched; /* items reclaimed but never touched */
37+
uint64_t evicted_unfetched; /* items evicted but never touched */
3838
uint64_t crawler_reclaimed;
3939
uint64_t crawler_items_checked;
4040
uint64_t lrutail_reflocked;
@@ -332,8 +332,8 @@ int do_item_link(item *it, const uint32_t hv) {
332332
it->time = current_time;
333333

334334
STATS_LOCK();
335-
stats.curr_bytes += ITEM_ntotal(it);
336-
stats.curr_items += 1;
335+
stats_state.curr_bytes += ITEM_ntotal(it);
336+
stats_state.curr_items += 1;
337337
stats.total_items += 1;
338338
STATS_UNLOCK();
339339

@@ -352,8 +352,8 @@ void do_item_unlink(item *it, const uint32_t hv) {
352352
if ((it->it_flags & ITEM_LINKED) != 0) {
353353
it->it_flags &= ~ITEM_LINKED;
354354
STATS_LOCK();
355-
stats.curr_bytes -= ITEM_ntotal(it);
356-
stats.curr_items -= 1;
355+
stats_state.curr_bytes -= ITEM_ntotal(it);
356+
stats_state.curr_items -= 1;
357357
STATS_UNLOCK();
358358
item_stats_sizes_remove(it);
359359
assoc_delete(ITEM_key(it), it->nkey, hv);
@@ -368,8 +368,8 @@ void do_item_unlink_nolock(item *it, const uint32_t hv) {
368368
if ((it->it_flags & ITEM_LINKED) != 0) {
369369
it->it_flags &= ~ITEM_LINKED;
370370
STATS_LOCK();
371-
stats.curr_bytes -= ITEM_ntotal(it);
372-
stats.curr_items -= 1;
371+
stats_state.curr_bytes -= ITEM_ntotal(it);
372+
stats_state.curr_items -= 1;
373373
STATS_UNLOCK();
374374
item_stats_sizes_remove(it);
375375
assoc_delete(ITEM_key(it), it->nkey, hv);
@@ -1377,7 +1377,7 @@ static void *item_crawler_thread(void *arg) {
13771377
if (settings.verbose > 2)
13781378
fprintf(stderr, "LRU crawler thread sleeping\n");
13791379
STATS_LOCK();
1380-
stats.lru_crawler_running = false;
1380+
stats_state.lru_crawler_running = false;
13811381
STATS_UNLOCK();
13821382
}
13831383
pthread_mutex_unlock(&lru_crawler_lock);
@@ -1469,7 +1469,7 @@ static int do_lru_crawler_start(uint32_t id, uint32_t remaining) {
14691469
}
14701470
if (starts) {
14711471
STATS_LOCK();
1472-
stats.lru_crawler_running = true;
1472+
stats_state.lru_crawler_running = true;
14731473
stats.lru_crawler_starts++;
14741474
STATS_UNLOCK();
14751475
pthread_mutex_lock(&lru_crawler_stats_lock);

memcached.c

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static void conn_free(conn *c);
108108

109109
/** exported globals **/
110110
struct stats stats;
111+
struct stats_state stats_state;
111112
struct settings settings;
112113
time_t process_started; /* when the process was started */
113114
conn **conns;
@@ -178,20 +179,9 @@ static rel_time_t realtime(const time_t exptime) {
178179
}
179180

180181
static void stats_init(void) {
181-
stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0;
182-
stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = stats.evictions = stats.reclaimed = 0;
183-
stats.touch_cmds = stats.touch_misses = stats.touch_hits = stats.rejected_conns = 0;
184-
stats.malloc_fails = 0;
185-
stats.curr_bytes = stats.listen_disabled_num = 0;
186-
stats.hash_power_level = stats.hash_bytes = stats.hash_is_expanding = 0;
187-
stats.expired_unfetched = stats.evicted_unfetched = 0;
188-
stats.slabs_moved = 0;
189-
stats.lru_maintainer_juggles = 0;
190-
stats.accepting_conns = true; /* assuming we start in this state. */
191-
stats.slab_reassign_running = false;
192-
stats.lru_crawler_running = false;
193-
stats.lru_crawler_starts = 0;
194-
stats.time_in_listen_disabled_us = 0;
182+
memset(&stats, 0, sizeof(struct stats));
183+
memset(&stats_state, 0, sizeof(struct stats_state));
184+
stats_state.accepting_conns = true; /* assuming we start in this state. */
195185

196186
/* make the time we started always be 2 seconds before we really
197187
did, so time(0) - time.started is never zero. if so, things
@@ -203,12 +193,7 @@ static void stats_init(void) {
203193

204194
static void stats_reset(void) {
205195
STATS_LOCK();
206-
stats.total_items = stats.total_conns = 0;
207-
stats.rejected_conns = 0;
208-
stats.malloc_fails = 0;
209-
stats.evictions = 0;
210-
stats.reclaimed = 0;
211-
stats.listen_disabled_num = 0;
196+
memset(&stats, 0, sizeof(struct stats));
212197
stats_prefix_clear();
213198
STATS_UNLOCK();
214199
threadlocal_stats_reset();
@@ -513,7 +498,7 @@ conn *conn_new(const int sfd, enum conn_states init_state,
513498
}
514499

515500
STATS_LOCK();
516-
stats.conn_structs++;
501+
stats_state.conn_structs++;
517502
STATS_UNLOCK();
518503

519504
c->sfd = sfd;
@@ -593,7 +578,7 @@ conn *conn_new(const int sfd, enum conn_states init_state,
593578
}
594579

595580
STATS_LOCK();
596-
stats.curr_conns++;
581+
stats_state.curr_conns++;
597582
stats.total_conns++;
598583
STATS_UNLOCK();
599584

@@ -697,7 +682,7 @@ static void conn_close(conn *c) {
697682
pthread_mutex_unlock(&conn_lock);
698683

699684
STATS_LOCK();
700-
stats.curr_conns--;
685+
stats_state.curr_conns--;
701686
STATS_UNLOCK();
702687

703688
return;
@@ -2714,13 +2699,13 @@ static void server_stats(ADD_STAT add_stats, conn *c) {
27142699
(long)usage.ru_stime.tv_usec);
27152700
#endif /* !WIN32 */
27162701

2717-
APPEND_STAT("curr_connections", "%llu", (unsigned long long)stats.curr_conns - 1);
2702+
APPEND_STAT("curr_connections", "%llu", (unsigned long long)stats_state.curr_conns - 1);
27182703
APPEND_STAT("total_connections", "%llu", (unsigned long long)stats.total_conns);
27192704
if (settings.maxconns_fast) {
27202705
APPEND_STAT("rejected_connections", "%llu", (unsigned long long)stats.rejected_conns);
27212706
}
2722-
APPEND_STAT("connection_structures", "%u", stats.conn_structs);
2723-
APPEND_STAT("reserved_fds", "%u", stats.reserved_fds);
2707+
APPEND_STAT("connection_structures", "%u", stats_state.conn_structs);
2708+
APPEND_STAT("reserved_fds", "%u", stats_state.reserved_fds);
27242709
APPEND_STAT("cmd_get", "%llu", (unsigned long long)thread_stats.get_cmds);
27252710
APPEND_STAT("cmd_set", "%llu", (unsigned long long)slab_stats.set_cmds);
27262711
APPEND_STAT("cmd_flush", "%llu", (unsigned long long)thread_stats.flush_cmds);
@@ -2748,24 +2733,24 @@ static void server_stats(ADD_STAT add_stats, conn *c) {
27482733
APPEND_STAT("bytes_read", "%llu", (unsigned long long)thread_stats.bytes_read);
27492734
APPEND_STAT("bytes_written", "%llu", (unsigned long long)thread_stats.bytes_written);
27502735
APPEND_STAT("limit_maxbytes", "%llu", (unsigned long long)settings.maxbytes);
2751-
APPEND_STAT("accepting_conns", "%u", stats.accepting_conns);
2736+
APPEND_STAT("accepting_conns", "%u", stats_state.accepting_conns);
27522737
APPEND_STAT("listen_disabled_num", "%llu", (unsigned long long)stats.listen_disabled_num);
27532738
APPEND_STAT("time_in_listen_disabled_us", "%llu", stats.time_in_listen_disabled_us);
27542739
APPEND_STAT("threads", "%d", settings.num_threads);
27552740
APPEND_STAT("conn_yields", "%llu", (unsigned long long)thread_stats.conn_yields);
2756-
APPEND_STAT("hash_power_level", "%u", stats.hash_power_level);
2757-
APPEND_STAT("hash_bytes", "%llu", (unsigned long long)stats.hash_bytes);
2758-
APPEND_STAT("hash_is_expanding", "%u", stats.hash_is_expanding);
2741+
APPEND_STAT("hash_power_level", "%u", stats_state.hash_power_level);
2742+
APPEND_STAT("hash_bytes", "%llu", (unsigned long long)stats_state.hash_bytes);
2743+
APPEND_STAT("hash_is_expanding", "%u", stats_state.hash_is_expanding);
27592744
if (settings.slab_reassign) {
27602745
APPEND_STAT("slab_reassign_rescues", "%llu", stats.slab_reassign_rescues);
27612746
APPEND_STAT("slab_reassign_evictions_nomem", "%llu", stats.slab_reassign_evictions_nomem);
27622747
APPEND_STAT("slab_reassign_inline_reclaim", "%llu", stats.slab_reassign_inline_reclaim);
27632748
APPEND_STAT("slab_reassign_busy_items", "%llu", stats.slab_reassign_busy_items);
2764-
APPEND_STAT("slab_reassign_running", "%u", stats.slab_reassign_running);
2749+
APPEND_STAT("slab_reassign_running", "%u", stats_state.slab_reassign_running);
27652750
APPEND_STAT("slabs_moved", "%llu", stats.slabs_moved);
27662751
}
27672752
if (settings.lru_crawler) {
2768-
APPEND_STAT("lru_crawler_running", "%u", stats.lru_crawler_running);
2753+
APPEND_STAT("lru_crawler_running", "%u", stats_state.lru_crawler_running);
27692754
APPEND_STAT("lru_crawler_starts", "%u", stats.lru_crawler_starts);
27702755
}
27712756
if (settings.lru_maintainer_thread) {
@@ -4180,11 +4165,11 @@ void do_accept_new_conns(const bool do_accept) {
41804165
(maxconns_exited.tv_sec - stats.maxconns_entered.tv_sec) * 1000000
41814166
+ (maxconns_exited.tv_usec - stats.maxconns_entered.tv_usec);
41824167
stats.time_in_listen_disabled_us += elapsed_us;
4183-
stats.accepting_conns = true;
4168+
stats_state.accepting_conns = true;
41844169
STATS_UNLOCK();
41854170
} else {
41864171
STATS_LOCK();
4187-
stats.accepting_conns = false;
4172+
stats_state.accepting_conns = false;
41884173
gettimeofday(&stats.maxconns_entered,NULL);
41894174
stats.listen_disabled_num++;
41904175
STATS_UNLOCK();
@@ -4319,7 +4304,7 @@ static void drive_machine(conn *c) {
43194304
}
43204305

43214306
if (settings.maxconns_fast &&
4322-
stats.curr_conns + stats.reserved_fds >= settings.maxconns - 1) {
4307+
stats_state.curr_conns + stats_state.reserved_fds >= settings.maxconns - 1) {
43234308
str = "ERROR Too many open connections\r\n";
43244309
res = write(sfd, str, strlen(str));
43254310
close(sfd);
@@ -6093,7 +6078,7 @@ int main (int argc, char **argv) {
60936078
* is only an advisory.
60946079
*/
60956080
usleep(1000);
6096-
if (stats.curr_conns + stats.reserved_fds >= settings.maxconns - 1) {
6081+
if (stats_state.curr_conns + stats_state.reserved_fds >= settings.maxconns - 1) {
60976082
fprintf(stderr, "Maxconns setting is too low, use -c to increase.\n");
60986083
exit(EXIT_FAILURE);
60996084
}

memcached.h

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -261,44 +261,20 @@ struct thread_stats {
261261
};
262262

263263
/**
264-
* Global stats.
264+
* Global stats. Only resettable stats should go into this structure.
265265
*/
266266
struct stats {
267-
pthread_mutex_t mutex;
268-
uint64_t curr_items;
269267
uint64_t total_items;
270-
uint64_t curr_bytes;
271-
uint64_t curr_conns;
272268
uint64_t total_conns;
273269
uint64_t rejected_conns;
274270
uint64_t malloc_fails;
275-
unsigned int reserved_fds;
276-
unsigned int conn_structs;
277-
uint64_t get_cmds;
278-
uint64_t set_cmds;
279-
uint64_t touch_cmds;
280-
uint64_t get_hits;
281-
uint64_t get_misses;
282-
uint64_t touch_hits;
283-
uint64_t touch_misses;
284-
uint64_t evictions;
285-
uint64_t reclaimed;
286-
time_t started; /* when the process was started */
287-
bool accepting_conns; /* whether we are currently accepting */
288271
uint64_t listen_disabled_num;
289-
unsigned int hash_power_level; /* Better hope it's not over 9000 */
290-
uint64_t hash_bytes; /* size used for hash tables */
291-
bool hash_is_expanding; /* If the hash table is being expanded */
292-
uint64_t expired_unfetched; /* items reclaimed but never touched */
293-
uint64_t evicted_unfetched; /* items evicted but never touched */
294-
bool slab_reassign_running; /* slab reassign in progress */
295272
uint64_t slabs_moved; /* times slabs were moved around */
296273
uint64_t slab_reassign_rescues; /* items rescued during slab move */
297274
uint64_t slab_reassign_evictions_nomem; /* valid items lost during slab move */
298275
uint64_t slab_reassign_inline_reclaim; /* valid items lost during slab move */
299276
uint64_t slab_reassign_busy_items; /* valid temporarily unmovable */
300277
uint64_t lru_crawler_starts; /* Number of item crawlers kicked off */
301-
bool lru_crawler_running; /* crawl in progress */
302278
uint64_t lru_maintainer_juggles; /* number of LRU bg pokes */
303279
uint64_t time_in_listen_disabled_us; /* elapsed time in microseconds while server unable to process new connections */
304280
uint64_t log_worker_dropped; /* logs dropped by worker threads */
@@ -308,6 +284,24 @@ struct stats {
308284
struct timeval maxconns_entered; /* last time maxconns entered */
309285
};
310286

287+
/**
288+
* Global "state" stats. Reflects state that shouldn't be wiped ever.
289+
* Ordered for some cache line locality for commonly updated counters.
290+
*/
291+
struct stats_state {
292+
uint64_t curr_items;
293+
uint64_t curr_bytes;
294+
uint64_t curr_conns;
295+
uint64_t hash_bytes; /* size used for hash tables */
296+
unsigned int conn_structs;
297+
unsigned int reserved_fds;
298+
unsigned int hash_power_level; /* Better hope it's not over 9000 */
299+
bool hash_is_expanding; /* If the hash table is being expanded */
300+
bool accepting_conns; /* whether we are currently accepting */
301+
bool slab_reassign_running; /* slab reassign in progress */
302+
bool lru_crawler_running; /* crawl in progress */
303+
};
304+
311305
#define MAX_VERBOSITY_LEVEL 2
312306

313307
/* When adding a setting, be sure to update process_stat_settings */
@@ -361,6 +355,7 @@ struct settings {
361355
};
362356

363357
extern struct stats stats;
358+
extern struct stats_state stats_state;
364359
extern time_t process_started;
365360
extern struct settings settings;
366361

slabs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ bool get_stats(const char *stat_type, int nkey, ADD_STAT add_stats, void *c) {
329329
if (!stat_type) {
330330
/* prepare general statistics for the engine */
331331
STATS_LOCK();
332-
APPEND_STAT("bytes", "%llu", (unsigned long long)stats.curr_bytes);
333-
APPEND_STAT("curr_items", "%llu", (unsigned long long)stats.curr_items);
332+
APPEND_STAT("bytes", "%llu", (unsigned long long)stats_state.curr_bytes);
333+
APPEND_STAT("curr_items", "%llu", (unsigned long long)stats_state.curr_items);
334334
APPEND_STAT("total_items", "%llu", (unsigned long long)stats.total_items);
335335
STATS_UNLOCK();
336336
if (settings.slab_automove > 0) {
@@ -588,7 +588,7 @@ static int slab_rebalance_start(void) {
588588
pthread_mutex_unlock(&slabs_lock);
589589

590590
STATS_LOCK();
591-
stats.slab_reassign_running = true;
591+
stats_state.slab_reassign_running = true;
592592
STATS_UNLOCK();
593593

594594
return 0;
@@ -870,11 +870,11 @@ static void slab_rebalance_finish(void) {
870870
pthread_mutex_unlock(&slabs_lock);
871871

872872
STATS_LOCK();
873-
stats.slab_reassign_running = false;
874873
stats.slabs_moved++;
875874
stats.slab_reassign_rescues += rescues;
876875
stats.slab_reassign_evictions_nomem += evictions_nomem;
877876
stats.slab_reassign_inline_reclaim += inline_reclaim;
877+
stats_state.slab_reassign_running = false;
878878
STATS_UNLOCK();
879879

880880
if (settings.verbose > 1) {

thread.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ void sidethread_conn_close(conn *c) {
516516
close(c->sfd);
517517

518518
STATS_LOCK();
519-
stats.curr_conns--;
519+
stats_state.curr_conns--;
520520
STATS_UNLOCK();
521521

522522
return;
@@ -851,7 +851,7 @@ void memcached_thread_init(int nthreads, struct event_base *main_base) {
851851

852852
setup_thread(&threads[i]);
853853
/* Reserve three fds for the libevent base, and two for the pipe */
854-
stats.reserved_fds += 5;
854+
stats_state.reserved_fds += 5;
855855
}
856856

857857
/* Create threads after we've done all the libevent setup. */

0 commit comments

Comments
 (0)