Skip to content

Commit

Permalink
add a cmd_flush stat
Browse files Browse the repository at this point in the history
shouldn't add much lock contention for just this.
I want to add this one stat (mayb a few more?) since it's happened more than
once that folks think memcached is broken when a cron or something is calling
'flush_all' once a minute.
  • Loading branch information
dormando authored and dustin committed Mar 30, 2009
1 parent b8d997e commit 534466e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
8 changes: 7 additions & 1 deletion memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static rel_time_t realtime(const time_t exptime) {

static void stats_init(void) {
stats.curr_items = stats.total_items = stats.curr_conns = stats.total_conns = stats.conn_structs = 0;
stats.evictions = 0;
stats.get_cmds = stats.set_cmds = stats.get_hits = stats.get_misses = stats.evictions = 0;
stats.curr_bytes = 0;

/* make the time we started always be 2 seconds before we really
Expand Down Expand Up @@ -2209,6 +2209,7 @@ static char *server_stats(uint32_t (*add_stats)(char *buf, const char *key,
APPEND_STAT("connection_structures", "%u", stats.conn_structs);
APPEND_STAT("cmd_get", "%llu", (unsigned long long)thread_stats.get_cmds);
APPEND_STAT("cmd_set", "%llu", (unsigned long long)slab_stats.set_cmds);
APPEND_STAT("cmd_flush", "%llu", (unsigned long long)thread_stats.flush_cmds);
APPEND_STAT("get_hits", "%llu", (unsigned long long)slab_stats.get_hits);
APPEND_STAT("get_misses", "%llu", (unsigned long long)thread_stats.get_misses);
APPEND_STAT("delete_misses", "%llu", (unsigned long long)thread_stats.delete_misses);
Expand All @@ -2223,6 +2224,7 @@ static char *server_stats(uint32_t (*add_stats)(char *buf, const char *key,
APPEND_STAT("bytes_read", "%llu", (unsigned long long)thread_stats.bytes_read);
APPEND_STAT("bytes_written", "%llu", (unsigned long long)thread_stats.bytes_written);
APPEND_STAT("limit_maxbytes", "%llu", (unsigned long long)settings.maxbytes);
APPEND_STAT("bytes_written", "%llu", (unsigned long long)thread_stats.bytes_written);
APPEND_STAT("threads", "%d", settings.num_threads);

if(*buflen > 0 && (buf = malloc(*buflen)) == NULL) {
Expand Down Expand Up @@ -2916,6 +2918,10 @@ static void process_command(conn *c, char *command) {

set_noreply_maybe(c, tokens, ntokens);

STATS_LOCK();
c->thread->stats.flush_cmds++;
STATS_UNLOCK();

if(ntokens == (c->noreply ? 3 : 2)) {
settings.oldest_live = current_time - 1;
item_flush_expired();
Expand Down
5 changes: 5 additions & 0 deletions memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct thread_stats {
uint64_t cas_misses;
uint64_t bytes_read;
uint64_t bytes_written;
uint64_t flush_cmds;
struct slab_stats slab_stats[MAX_NUMBER_OF_SLAB_CLASSES];
};

Expand All @@ -102,6 +103,10 @@ struct stats {
unsigned int curr_conns;
unsigned int total_conns;
unsigned int conn_structs;
uint64_t get_cmds;
uint64_t set_cmds;
uint64_t get_hits;
uint64_t get_misses;
uint64_t evictions;
};

Expand Down
15 changes: 12 additions & 3 deletions t/stats.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/perl

use strict;
use Test::More tests => 89;
use Test::More tests => 91;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;
Expand All @@ -25,6 +25,7 @@ my $sock = $server->sock;
## STAT curr_connections 1
## STAT total_connections 2
## STAT connection_structures 2
## STAT cmd_flush 0
## STAT cmd_get 0
## STAT cmd_set 0
## STAT get_hits 0
Expand All @@ -46,10 +47,12 @@ my $sock = $server->sock;
my $stats = mem_stats($sock);

# Test number of keys
is(scalar(keys(%$stats)), 31, "31 stats values");
is(scalar(keys(%$stats)), 32, "32 stats values");

# Test initial state
foreach my $key (qw(curr_items total_items bytes cmd_get cmd_set get_hits evictions get_misses bytes_written delete_hits delete_misses incr_hits incr_misses decr_hits decr_misses)) {
foreach my $key (qw(curr_items total_items bytes cmd_get cmd_set get_hits evictions get_misses
bytes_written delete_hits delete_misses incr_hits incr_misses decr_hits
decr_misses)) {
is($stats->{$key}, 0, "initial $key is zero");
}

Expand Down Expand Up @@ -171,3 +174,9 @@ is(0, $stats->{'cas_misses'});
is(0, $stats->{'cas_hits'});
is(0, $stats->{'cas_badval'});
is(0, $stats->{'evictions'});

print $sock "flush_all\r\n";
is(scalar <$sock>, "OK\r\n", "flushed");

my $stats = mem_stats($sock);
is($stats->{cmd_flush}, 1, "after one flush cmd_flush is 1");
3 changes: 3 additions & 0 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ void threadlocal_stats_reset(void) {
threads[ii].stats.cas_misses = 0;
threads[ii].stats.bytes_read = 0;
threads[ii].stats.bytes_written = 0;
threads[ii].stats.flush_cmds = 0;

for(sid = 0; sid < MAX_NUMBER_OF_SLAB_CLASSES; sid++) {
threads[ii].stats.slab_stats[sid].set_cmds = 0;
Expand All @@ -502,6 +503,7 @@ void threadlocal_stats_aggregate(struct thread_stats *stats) {
stats->cas_misses = 0;
stats->bytes_written = 0;
stats->bytes_read = 0;
stats->flush_cmds = 0;

memset(stats->slab_stats, 0,
sizeof(struct slab_stats) * MAX_NUMBER_OF_SLAB_CLASSES);
Expand All @@ -517,6 +519,7 @@ void threadlocal_stats_aggregate(struct thread_stats *stats) {
stats->cas_misses += threads[ii].stats.cas_misses;
stats->bytes_read += threads[ii].stats.bytes_read;
stats->bytes_written += threads[ii].stats.bytes_written;
stats->flush_cmds += threads[ii].stats.flush_cmds;

for (sid = 0; sid < MAX_NUMBER_OF_SLAB_CLASSES; sid++) {
stats->slab_stats[sid].set_cmds +=
Expand Down

0 comments on commit 534466e

Please sign in to comment.