summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDustin Sallings <dustin@spy.net>2009-03-24 11:49:52 -0700
committerDustin Sallings <dustin@spy.net>2009-03-24 13:27:56 -0700
commitdd713869ffac9412b142485b568debbf1df4d386 (patch)
treec2305c9fbc353136007c6c629efe7f2dbc5211a4
parentf8770bf2eeeb29e6b7d25eb43782e03367301e6d (diff)
downloadmemcached-dd713869ffac9412b142485b568debbf1df4d386.tar.gz
Added append_stat function to contain the common stat append stuff.
-rw-r--r--items.c8
-rw-r--r--memcached.c55
-rw-r--r--memcached.h29
-rw-r--r--slabs.c6
4 files changed, 60 insertions, 38 deletions
diff --git a/items.c b/items.c
index 075c3bf..b5e3e37 100644
--- a/items.c
+++ b/items.c
@@ -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) {
@@ -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]);
}
}
diff --git a/memcached.c b/memcached.c
index fe8a1a4..bb8e2b1 100644
--- a/memcached.c
+++ b/memcached.c
@@ -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. */
@@ -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);
@@ -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;
@@ -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);
@@ -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);
@@ -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);
diff --git a/memcached.h b/memcached.h
index 84e36c7..49e462b 100644
--- a/memcached.h
+++ b/memcached.h
@@ -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 */
@@ -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
diff --git a/slabs.c b/slabs.c
index c717984..15ebc47 100644
--- a/slabs.c
+++ b/slabs.c
@@ -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;
@@ -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);