Skip to content

Commit

Permalink
add metrics to track buf_sock objects (twitter#138)
Browse files Browse the repository at this point in the history
* add metrics to track buf_sock objects

* bumping minor version, though I know I'm doing this wrong (breaking compatibility requires bumping major)
  • Loading branch information
thinkingfish authored Feb 12, 2017
1 parent ae02038 commit ab0edc8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ endif()

# version info
set(${PROJECT_NAME}_VERSION_MAJOR 1)
set(${PROJECT_NAME}_VERSION_MINOR 0)
set(${PROJECT_NAME}_VERSION_PATCH 2)
set(${PROJECT_NAME}_VERSION_MINOR 1)
set(${PROJECT_NAME}_VERSION_PATCH 0)
set(${PROJECT_NAME}_VERSION
${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}
)
Expand Down
18 changes: 17 additions & 1 deletion include/stream/cc_sockio.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extern "C" {
#include <cc_stream.h>

#include <cc_define.h>
#include <cc_metric.h>

#include <inttypes.h>
#include <stdlib.h>
Expand All @@ -62,6 +63,21 @@ typedef struct {
SOCKIO_OPTION(OPTION_DECLARE)
} sockio_options_st;

/* name type description */
#define SOCKIO_METRIC(ACTION) \
ACTION( buf_sock_create, METRIC_COUNTER, "# buf sock created" )\
ACTION( buf_sock_create_ex, METRIC_COUNTER, "# buf sock create exceptions" )\
ACTION( buf_sock_destroy, METRIC_COUNTER, "# buf sock destroyed" )\
ACTION( buf_sock_curr, METRIC_GAUGE, "# buf sock allocated" )\
ACTION( buf_sock_borrow, METRIC_COUNTER, "# buf sock borrowed" )\
ACTION( buf_sock_borrow_ex, METRIC_COUNTER, "# buf sock borrow exceptions" )\
ACTION( buf_sock_return, METRIC_COUNTER, "# buf sock returned" )\
ACTION( buf_sock_active, METRIC_GAUGE, "# buf sock being borrowed" )

typedef struct {
SOCKIO_METRIC(METRIC_DECLARE)
} sockio_metrics_st;

struct buf_sock {
/* these fields are useful for resource managmenet */
STAILQ_ENTRY(buf_sock) next;
Expand All @@ -79,7 +95,7 @@ struct buf_sock {

STAILQ_HEAD(buf_sock_sqh, buf_sock); /* corresponding header type for the STAILQ */

void sockio_setup(sockio_options_st *options);
void sockio_setup(sockio_options_st *options, sockio_metrics_st *metrics);
void sockio_teardown(void);

struct buf_sock *buf_sock_create(void); /* stream_get_fn */
Expand Down
25 changes: 24 additions & 1 deletion src/stream/cc_sockio.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
FREEPOOL(buf_sock_pool, buf_sockq, buf_sock);
struct buf_sock_pool bsp;

static bool sockio_init = false;
static bool bsp_init = false;
static sockio_metrics_st *sockio_metrics = NULL;

rstatus_i
buf_tcp_read(struct buf_sock *s)
Expand Down Expand Up @@ -212,6 +214,7 @@ buf_sock_create(void)

s = (struct buf_sock *)cc_alloc(sizeof(struct buf_sock));
if (s == NULL) {
INCR(sockio_metrics, buf_sock_create_ex);
return NULL;
}
STAILQ_NEXT(s, next) = NULL;
Expand All @@ -235,12 +238,16 @@ buf_sock_create(void)
goto error;
}

INCR(sockio_metrics, buf_sock_create);
INCR(sockio_metrics, buf_sock_curr);

log_verb("created buffered socket %p", s);

return s;

error:
log_info("buffered socket creation failed");
INCR(sockio_metrics, buf_sock_create_ex);
buf_sock_destroy(&s);

return NULL;
Expand All @@ -261,6 +268,8 @@ buf_sock_destroy(struct buf_sock **s)
cc_free(*s);

*s = NULL;
INCR(sockio_metrics, buf_sock_destroy);
DECR(sockio_metrics, buf_sock_curr);
}

static void
Expand Down Expand Up @@ -331,10 +340,13 @@ buf_sock_borrow(void)
FREEPOOL_BORROW(s, &bsp, next, buf_sock_create);
if (s == NULL) {
log_debug("borrow buffered socket failed: OOM or over limit");
INCR(sockio_metrics, buf_sock_borrow_ex);

return NULL;
}
buf_sock_reset(s);
INCR(sockio_metrics, buf_sock_borrow);
INCR(sockio_metrics, buf_sock_active);

log_verb("borrowed buffered socket %p", s);

Expand All @@ -354,18 +366,29 @@ buf_sock_return(struct buf_sock **s)
FREEPOOL_RETURN(*s, &bsp, next);

*s = NULL;
INCR(sockio_metrics, buf_sock_return);
DECR(sockio_metrics, buf_sock_active);
}

void
sockio_setup(sockio_options_st *options)
sockio_setup(sockio_options_st *options, sockio_metrics_st *metrics)
{
uint32_t max = BUFSOCK_POOLSIZE;

log_info("set up the %s module", SOCKIO_MODULE_NAME);

if (sockio_init) {
log_warn("%s has already been setup, overwrite", SOCKIO_MODULE_NAME);
}

sockio_metrics = metrics;

if (options != NULL) {
max = option_uint(&options->buf_sock_poolsize);
}

buf_sock_pool_create(max);
sockio_init = true;
}

void
Expand Down

0 comments on commit ab0edc8

Please sign in to comment.