Skip to content

Commit

Permalink
Support of stats sizes subcommand via the binary protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
Toru Maesaka authored and dustin committed Jan 3, 2009
1 parent 199216a commit e2df488
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
32 changes: 29 additions & 3 deletions items.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,10 @@ char *do_item_stats(int *bytes, uint32_t (*add_stats)(char *buf,

/** dumps out a list of objects of each size, with granularity of 32 bytes */
/*@null@*/
char* do_item_stats_sizes(int *bytes) {
char *do_item_stats_sizes(int *bytes, uint32_t (*add_stats)(char *buf,
const char *key, const char *val, const uint16_t klen,
const uint32_t vlen), bool bin_prot) {

const int num_buckets = 32768; /* max 1MB object, divided into 32 bytes size buckets */
unsigned int *histogram = (unsigned int *)malloc((size_t)num_buckets * sizeof(int));
char *buf = (char *)malloc(2 * 1024 * 1024); /* 2MB max response size */
Expand All @@ -447,12 +450,35 @@ char* do_item_stats_sizes(int *bytes) {

/* write the buffer */
*bytes = 0;

/* binary protocol variables */
uint32_t nbytes, linelen = 0;
char *ptr = buf;
char key[128];
char val[128];

for (i = 0; i < num_buckets; i++) {
if (histogram[i] != 0) {
*bytes += sprintf(&buf[*bytes], "%d %u\r\n", i * 32, histogram[i]);
if (bin_prot) {
sprintf(key, "%d", i * 32);
sprintf(val, "%u", histogram[i]);
nbytes = add_stats(ptr, key, val, strlen(key), strlen(val));
linelen += nbytes;
ptr += nbytes;
} else {
*bytes += sprintf(&buf[*bytes], "%d %u\r\n", i * 32,
histogram[i]);
}
}
}
*bytes += sprintf(&buf[*bytes], "END\r\n");

if (bin_prot) {
nbytes = add_stats(ptr, NULL, NULL, 0, 0);
*bytes = linelen + nbytes;
} else {
*bytes += sprintf(&buf[*bytes], "END\r\n");
}

free(histogram);
return buf;
}
Expand Down
5 changes: 4 additions & 1 deletion items.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ char *do_item_stats(int *bytes, uint32_t (*add_stats)(char *buf,
const char *key, const char *val, const uint16_t klen,
const uint32_t vlen), bool bin_prot);
/*@null@*/
char *do_item_stats_sizes(int *bytes);
char *do_item_stats_sizes(int *bytes, uint32_t (*add_stats)(char *buf,
const char *key, const char *val, const uint16_t klen,
const uint32_t vlen), bool bin_prot);

void do_item_flush_expired(void);

item *do_item_get(const char *key, const size_t nkey);
Expand Down
2 changes: 1 addition & 1 deletion memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ static void process_stat(conn *c, token_t *tokens, const size_t ntokens) {

if (strcmp(subcommand, "sizes") == 0) {
int bytes = 0;
char *buf = item_stats_sizes(&bytes);
char *buf = get_stats(false, "sizes", &append_bin_stats, &bytes);
write_and_free(c, buf, bytes);
return;
}
Expand Down
4 changes: 3 additions & 1 deletion memcached.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ int item_replace(item *it, item *new_it);
char *item_stats(int *bytes, uint32_t (*add_stats)(char *buf,
const char *key, const char *val, const uint16_t klen,
const uint32_t vlen), bool bin_prot);
char *item_stats_sizes(int *bytes);
char *item_stats_sizes(int *bytes, uint32_t (*add_stats)(char *buf,
const char *key, const char *val, const uint16_t klen,
const uint32_t vlen), bool bin_prot);
void item_unlink(item *it);
void item_update(item *it);
void *slabs_alloc(size_t size, unsigned int id);
Expand Down
4 changes: 4 additions & 0 deletions slabs.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ char *get_stats(const bool bin_prot, const char *stat_type,
buf = slabs_stats(&size, add_stats, bin_prot);
*buflen = size;
return buf;
} else if (strcmp(stat_type, "sizes") == 0) {
buf = item_stats_sizes(&size, add_stats, bin_prot);
*buflen = size;
return buf;
}

#ifdef HAVE_MALLOC_H
Expand Down
6 changes: 4 additions & 2 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,13 @@ char *item_stats(int *bytes, uint32_t (*add_stats)(char *buf,
/*
* Dumps a list of objects of each size in 32-byte increments
*/
char *item_stats_sizes(int *bytes) {
char *item_stats_sizes(int *bytes, uint32_t (*add_stats)(char *buf,
const char *key, const char *val, const uint16_t klen,
const uint32_t vlen), bool bin_prot) {
char *ret;

pthread_mutex_lock(&cache_lock);
ret = do_item_stats_sizes(bytes);
ret = do_item_stats_sizes(bytes, add_stats, bin_prot);
pthread_mutex_unlock(&cache_lock);
return ret;
}
Expand Down

0 comments on commit e2df488

Please sign in to comment.