Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

MOD: add stats command for fatcache #29

Merged
merged 1 commit into from Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.am
Expand Up @@ -30,5 +30,6 @@ fatcache_SOURCES = \
fc_string.c fc_string.h \
fc_array.c fc_array.h \
fc_util.c fc_util.h \
fc_stats.c fc_stats.h \
fc_queue.h \
fc.c
21 changes: 21 additions & 0 deletions src/fc_connection.c
Expand Up @@ -21,6 +21,7 @@

extern struct settings settings;

static uint32_t nalloc_conn; /* total conn num */
static uint32_t nfree_connq; /* # free conn q */
static struct conn_tqh free_connq; /* free conn q */

Expand All @@ -31,6 +32,7 @@ conn_init(void)
{
log_debug(LOG_DEBUG, "conn size %d", sizeof(struct conn));
nfree_connq = 0;
nalloc_conn = 0;
TAILQ_INIT(&free_connq);
}

Expand Down Expand Up @@ -181,6 +183,7 @@ _conn_get(void)
if (conn == NULL) {
return NULL;
}
++nalloc_conn;
}

/* extra stuff */
Expand Down Expand Up @@ -240,3 +243,21 @@ conn_get(int sd, bool client)

return c;
}

uint32_t
conn_total(void)
{
return nalloc_conn - 1;
}

uint32_t
conn_nused(void)
{
return nalloc_conn - nfree_connq - 1;
}

uint32_t
conn_nfree(void)
{
return nfree_connq;
}
4 changes: 4 additions & 0 deletions src/fc_connection.h
Expand Up @@ -67,4 +67,8 @@ ssize_t conn_sendv(struct conn *conn, struct array *sendv, size_t nsend);
struct conn *conn_get(int sd, bool client);
void conn_put(struct conn *c);

uint32_t conn_total(void);
uint32_t conn_nused(void);
uint32_t conn_nfree(void);

#endif
16 changes: 16 additions & 0 deletions src/fc_itemx.c
Expand Up @@ -26,6 +26,7 @@ static uint64_t nitx; /* # item index */
static uint64_t nitx_table; /* # item index table entries */
static struct itemx_tqh *itx_table; /* item index table */

static uint64_t nalloc_itemx; /* # nalloc itemx */
static uint64_t nfree_itemxq; /* # free itemx q */
static struct itemx_tqh free_itemxq; /* free itemx q */

Expand Down Expand Up @@ -140,6 +141,7 @@ itemx_init(void)
for (itx = istart; itx < iend; itx++) {
itemx_put(itx);
}
nalloc_itemx = n;

return FC_OK;
}
Expand Down Expand Up @@ -217,6 +219,7 @@ itemx_putx(uint32_t hash, uint8_t *md, uint32_t sid, uint32_t offset,
bucket = itemx_bucket(hash);
nitx++;
STAILQ_INSERT_HEAD(bucket, itx, tqe);
slab_incr_chunks_by_sid(itx->sid, 1);
}

bool
Expand All @@ -233,8 +236,21 @@ itemx_removex(uint32_t hash, uint8_t *md)
bucket = itemx_bucket(hash);
nitx--;
STAILQ_REMOVE(bucket, itx, itemx, tqe);
slab_incr_chunks_by_sid(itx->sid, -1);

itemx_put(itx);

return true;
}

uint64_t
itemx_nalloc(void)
{
return nalloc_itemx;
}

uint64_t
itemx_nfree(void)
{
return nfree_itemxq;
}
2 changes: 2 additions & 0 deletions src/fc_itemx.h
Expand Up @@ -40,4 +40,6 @@ struct itemx *itemx_getx(uint32_t hash, uint8_t *md);
void itemx_putx(uint32_t hash, uint8_t *md, uint32_t sid, uint32_t ioff, rel_time_t expiry, uint64_t cas);
bool itemx_removex(uint32_t hash, uint8_t *md);

uint64_t itemx_nalloc(void);
uint64_t itemx_nfree(void);
#endif
20 changes: 20 additions & 0 deletions src/fc_memcache.c
Expand Up @@ -177,6 +177,12 @@ memcache_version(struct msg *r)
return false;
}

static bool
memcache_stats(struct msg *r)
{
return r->type == MSG_REQ_STATS;
}

void
memcache_parse_req(struct msg *r)
{
Expand Down Expand Up @@ -297,6 +303,12 @@ memcache_parse_req(struct msg *r)

break;

case 5:
if (str5cmp(m, 's', 't', 'a', 't', 's')) {
r->type = MSG_REQ_STATS;
break;
}
break;
case 6:
if (str6cmp(m, 'a', 'p', 'p', 'e', 'n', 'd')) {
r->type = MSG_REQ_APPEND;
Expand Down Expand Up @@ -337,6 +349,14 @@ memcache_parse_req(struct msg *r)
} else if (memcache_quit(r) || memcache_version(r)) {
p = p - 1; /* go back by 1 byte */
state = SW_CRLF;
} else if(memcache_stats(r)) {
while(*p == ' ') p++; /* trim the space */
if (*p == '\r') {
state = SW_CRLF;
} else {
state = SW_SPACES_BEFORE_KEY;
}
p = p - 1;
} else {
goto error;
}
Expand Down
2 changes: 2 additions & 0 deletions src/fc_message.h
Expand Up @@ -35,6 +35,7 @@ typedef void (*msg_parse_t)(struct msg *);
ACTION( REQ_PREPEND, "prepend " ) \
ACTION( REQ_INCR, "incr " ) \
ACTION( REQ_DECR, "decr " ) \
ACTION( REQ_STATS, "stats " ) \
ACTION( REQ_VERSION, "version " ) \
ACTION( REQ_QUIT, "quit " ) \
ACTION( RSP_NUM, "" /* na */ ) \
Expand Down Expand Up @@ -149,5 +150,6 @@ void rsp_send_status(struct context *ctx, struct conn *conn, struct msg *msg, ms
void rsp_send_error(struct context *ctx, struct conn *conn, struct msg *msg, msg_type_t rsp_type, int err);
void rsp_send_value(struct context *ctx, struct conn *conn, struct msg *msg, struct item *it, uint64_t cas);
void rsp_send_num(struct context *ctx, struct conn *conn, struct msg *msg, struct item *it);
void rsp_send_string(struct context *ctx, struct conn *conn, struct msg *msg, struct string *str);

#endif
53 changes: 49 additions & 4 deletions src/fc_request.c
Expand Up @@ -16,9 +16,12 @@
*/

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#include <fc_core.h>
#include <fc_event.h>
#include <fc_stats.h>

extern struct string msg_strings[];

Expand Down Expand Up @@ -204,20 +207,27 @@ req_process_get(struct context *ctx, struct conn *conn, struct msg *msg)
return;
}

STATS_HIT_INCR(msg->type);
SC_STATS_INCR(it->cid, msg->type);
rsp_send_value(ctx, conn, msg, it, itx->cas);
}

static void
req_process_delete(struct context *ctx, struct conn *conn, struct msg *msg)
{
bool found;
uint8_t cid;
struct itemx *itx;

found = itemx_removex(msg->hash, msg->md);
if (!found) {
itx = itemx_getx(msg->hash, msg->md);
if (itx == NULL) {
rsp_send_status(ctx, conn, msg, MSG_RSP_NOT_FOUND);
return;
}
cid = slab_get_cid(itx->sid);
itemx_removex(msg->hash, msg->md);

STATS_HIT_INCR(msg->type);
SC_STATS_INCR(cid, msg->type);
rsp_send_status(ctx, conn, msg, MSG_RSP_DELETED);
}

Expand Down Expand Up @@ -247,6 +257,7 @@ req_process_set(struct context *ctx, struct conn *conn, struct msg *msg)

mbuf_copy_to(&msg->mhdr, msg->value, item_data(it), msg->vlen);

SC_STATS_INCR(cid, msg->type);
rsp_send_status(ctx, conn, msg, MSG_RSP_STORED);
}

Expand Down Expand Up @@ -310,6 +321,7 @@ req_process_cas(struct context *ctx, struct conn *conn, struct msg *msg)
return;
}

STATS_HIT_INCR(msg->type);
req_process_set(ctx, conn, msg);
}

Expand Down Expand Up @@ -442,9 +454,37 @@ req_process_num(struct context *ctx, struct conn *conn, struct msg *msg)
/* 7). copy numstr to it */
fc_memcpy(item_data(it), numstr, n);

STATS_HIT_INCR(msg->type);
rsp_send_num(ctx, conn, msg, it);
}

static void req_process_stats(struct context *ctx, struct conn *conn, struct msg *msg)
{
int nkey;
uint8_t *key;
struct string str;
buffer *buf = NULL;

key = msg->key_start;
nkey = (uint8_t)(msg->key_end - msg->key_start);

if (fc_strlen("settings") == nkey && !fc_strncmp("settings", key, nkey)) {
buf = stats_settings();
} else if (fc_strlen("slabs") == nkey && !fc_strncmp("slabs", key, nkey)) {
buf = stats_slabs();
} else {
buf = stats_server();
}
if (buf == NULL) {
rsp_send_error(ctx, conn, msg, MSG_RSP_SERVER_ERROR, ENOMEM);
} else {
str.data = buf->data;
str.len = buf->nused;
rsp_send_string(ctx, conn, msg, &str);
stats_dealloc_buffer(buf);
}
}

void
req_process_error(struct context *ctx, struct conn *conn, struct msg *msg,
int err)
Expand Down Expand Up @@ -488,7 +528,7 @@ req_process(struct context *ctx, struct conn *conn, struct msg *msg)
req_enqueue_omsgq(ctx, conn, msg);
}

ASSERT(msg->key_end > msg->key_start);
ASSERT((msg->key_end > msg->key_start) || msg->type >= MSG_REQ_STATS);
key = msg->key_start;
keylen = msg->key_end - msg->key_start;

Expand All @@ -501,6 +541,7 @@ req_process(struct context *ctx, struct conn *conn, struct msg *msg)
sha1(key, keylen, msg->md);
msg->hash = sha1_hash(msg->md);

STATS_INCR(msg->type);
switch (msg->type) {
case MSG_REQ_GET:
case MSG_REQ_GETS:
Expand Down Expand Up @@ -541,6 +582,10 @@ req_process(struct context *ctx, struct conn *conn, struct msg *msg)
req_process_num(ctx, conn, msg);
break;

case MSG_REQ_STATS:
req_process_stats(ctx, conn, msg);
break;

default:
NOT_REACHED();
}
Expand Down
38 changes: 38 additions & 0 deletions src/fc_response.c
Expand Up @@ -107,6 +107,44 @@ rsp_send_done(struct context *ctx, struct conn *conn, struct msg *msg)
req_put(pmsg);
}

void rsp_send_string(struct context *ctx, struct conn *conn, struct msg *msg,
struct string *str)
{
rstatus_t status; /* return status */
struct msg *pmsg; /* peer response */

if(!str) return;

pmsg = rsp_get(conn);
if (pmsg == NULL) {
req_process_error(ctx, conn, msg, ENOMEM);
return;
}

ASSERT(msg->request);
ASSERT(msg->peer == NULL);
ASSERT(!msg->done);
ASSERT(!pmsg->request);

status = mbuf_copy_from(&pmsg->mhdr, str->data, str->len);
if (status != FC_OK) {
req_process_error(ctx, conn, msg, errno);
return;
}
pmsg->mlen += str->len;

/* mark response as done */
msg->done = 1;
msg->peer = pmsg;
pmsg->peer = msg;

status = event_add_out(ctx->ep, conn);
if (status != FC_OK) {
req_process_error(ctx, conn, msg, errno);
return;
}
}

void
rsp_send_status(struct context *ctx, struct conn *conn, struct msg *msg,
msg_type_t rsp_type)
Expand Down