Skip to content

Commit

Permalink
Added append_stat function to contain the common stat append stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Mar 24, 2009
1 parent f8770bf commit dd71386
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
8 changes: 3 additions & 5 deletions items.c
Expand Up @@ -430,7 +430,6 @@ char *do_item_stats_sizes(uint32_t (*add_stats)(char *buf,
int allocated = 2 * 1024 * 1024;
char *buf = (char *)malloc(allocated); /* 2MB max response size */
char *pos = buf;
int size;
int i;

if (histogram == 0 || buf == 0) {
Expand All @@ -455,14 +454,13 @@ char *do_item_stats_sizes(uint32_t (*add_stats)(char *buf,

/* write the buffer */
*buflen = 0;
char val_str[128];
int vlen = 0;

for (i = 0; i < num_buckets; i++) {
if (histogram[i] != 0) {
char key[8];
vlen = sprintf(key, "%d", i * 32);
assert(vlen < sizeof(key));
int klen = 0;
klen = sprintf(key, "%d", i * 32);
assert(klen < sizeof(key));
APPEND_STAT(key, "%u", histogram[i]);
}
}
Expand Down
55 changes: 43 additions & 12 deletions memcached.c
Expand Up @@ -21,6 +21,7 @@
#include <sys/resource.h>
#include <sys/uio.h>
#include <ctype.h>
#include <stdarg.h>

/* some POSIX systems need the following definition
* to get mlockall flags out of sys/mman.h. */
Expand Down Expand Up @@ -2095,6 +2096,39 @@ static inline void set_noreply_maybe(conn *c, token_t *tokens, size_t ntokens)
}
}

char *append_stat(const char *name, char *pos,
uint32_t (*add_stats)(char *buf, const char *key,
const uint16_t klen, const char *val,
const uint32_t vlen, void *cookie),
conn *c,
int allocated,
int *buflen,
const char *fmt, ...) {
char val_str[128];
int vlen = 0, size = 0;
va_list ap;

assert(name);
assert(pos);
assert(add_stats);
assert(c);
assert(fmt);
assert(buflen);
assert(*buflen < allocated);

va_start(ap, fmt);
vlen = vsnprintf(val_str, sizeof(val_str) - 1, fmt, ap);
va_end(ap);

size = add_stats(pos, name, strlen(name), val_str, vlen, c);
*buflen += size;
pos += size;

assert(*buflen < allocated);

return pos;
}

inline static void process_stats_detail(conn *c, const char *command) {
assert(c != NULL);

Expand Down Expand Up @@ -2123,11 +2157,8 @@ static char *server_stats(uint32_t (*add_stats)(char *buf, const char *key,
int *buflen) {
int allocated = 2048;
char temp[allocated];
char val_str[128];
char *buf = NULL;
char *pos = temp;
size_t size;
int vlen = 0;
pid_t pid = getpid();
rel_time_t now = current_time;
*buflen = 0;
Expand All @@ -2143,7 +2174,6 @@ static char *server_stats(uint32_t (*add_stats)(char *buf, const char *key,
#endif /* !WIN32 */

STATS_LOCK();
memset(val_str, 0, 128);

APPEND_STAT("pid", "%lu", (long)pid);
APPEND_STAT("uptime", "%u", now);
Expand All @@ -2152,12 +2182,14 @@ static char *server_stats(uint32_t (*add_stats)(char *buf, const char *key,
APPEND_STAT("pointer_size", "%d", (int)(8 * sizeof(void *)));

#ifndef WIN32
APPEND_STAT2("rusage_user", "%ld.%06ld",
(long)usage.ru_utime.tv_sec,
(long)usage.ru_utime.tv_usec);
APPEND_STAT2("rusage_system", "%ld.%06ld",
(long)usage.ru_stime.tv_sec,
(long)usage.ru_stime.tv_usec);
pos = append_stat("rusage_user", pos, add_stats, c, allocated,
buflen, "%ld.%06ld",
(long)usage.ru_utime.tv_sec,
(long)usage.ru_utime.tv_usec);
pos = append_stat("rusage_system", pos, add_stats, c, allocated,
buflen, "%ld.%06ld",
(long)usage.ru_stime.tv_sec,
(long)usage.ru_stime.tv_usec);
#endif /* !WIN32 */

APPEND_STAT("curr_connections", "%u", stats.curr_conns - 1);
Expand Down Expand Up @@ -2218,8 +2250,7 @@ static char *process_stat_settings(uint32_t (*add_stats)(char *buf,
void *cookie),
void *c, int *buflen) {
char *buf = NULL, *pos = NULL;
char val_str[128];
int size = 0, vlen = 0, allocated = 2048;
int allocated = 2048;
*buflen = 0;

assert(add_stats);
Expand Down
29 changes: 13 additions & 16 deletions memcached.h
Expand Up @@ -175,23 +175,10 @@ typedef struct _stritem {
+ (item)->nsuffix + (item)->nbytes \
+ (((item)->it_flags & ITEM_CAS) ? sizeof(uint64_t) : 0))

/* Stat processing macros */

/* Append a simple stat with a stat name, value format and value */
#define APPEND_STAT(name, fmt, val) \
vlen = sprintf(val_str, fmt, val); \
size = add_stats(pos, name, strlen(name), val_str, vlen, c); \
*buflen += size; \
pos += size; \
assert(*buflen < allocated);

/* Append a simple stat with a stat name, value format and two values */
#define APPEND_STAT2(name, fmt, val, val2) \
vlen = sprintf(val_str, fmt, val, val2); \
size = add_stats(pos, name, strlen(name), val_str, vlen, c); \
*buflen += size; \
pos += size; \
assert(*buflen < allocated);
#define APPEND_STAT(name, fmt, val) \
pos = append_stat(name, pos, add_stats, c, allocated, buflen, \
fmt, val);

/* Append an indexed stat with a stat name (with format), value format
and value */
Expand Down Expand Up @@ -413,6 +400,16 @@ void threadlocal_stats_reset(void);
void threadlocal_stats_aggregate(struct thread_stats *stats);
void slab_stats_aggregate(struct thread_stats *stats, struct slab_stats *out);

/* Stat processing functions */
char *append_stat(const char *name, char *pos,
uint32_t (*add_stats)(char *buf, const char *key,
const uint16_t klen, const char *val,
const uint32_t vlen, void *cookie),
conn *c,
int allocated,
int *buflen,
const char *fmt, ...);

enum store_item_type store_item(item *item, int comm, conn *c);

#if HAVE_DROP_PRIVILEGES
Expand Down
6 changes: 1 addition & 5 deletions slabs.c
Expand Up @@ -314,9 +314,7 @@ char *get_stats(const char *stat_type, int nkey,
if (!stat_type) {
int allocated = 512;
char *buf, *pos;
char val_str[128];
int size, vlen;
*buflen = size = vlen = 0;
*buflen = 0;

if ((buf = malloc(allocated)) == NULL) {
*buflen = -1;
Expand Down Expand Up @@ -405,8 +403,6 @@ static char *do_slabs_stats(uint32_t (*add_stats)(char *buf, const char *key,
}

/* add overall slab stats and append terminator */
char val_str[128];
int vlen = 0;

APPEND_STAT("active_slabs", "%d", total);
APPEND_STAT("total_malloced", "%llu", (unsigned long long)mem_malloced);
Expand Down

0 comments on commit dd71386

Please sign in to comment.