From ab0edc889ca64009651c5e324e500b0e30171fc1 Mon Sep 17 00:00:00 2001 From: Yao Yue Date: Sat, 11 Feb 2017 19:26:03 -0800 Subject: [PATCH] add metrics to track buf_sock objects (#138) * add metrics to track buf_sock objects * bumping minor version, though I know I'm doing this wrong (breaking compatibility requires bumping major) --- CMakeLists.txt | 4 ++-- include/stream/cc_sockio.h | 18 +++++++++++++++++- src/stream/cc_sockio.c | 25 ++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8df6f9527..f378dec25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} ) diff --git a/include/stream/cc_sockio.h b/include/stream/cc_sockio.h index 89fe60471..9d2f9f570 100644 --- a/include/stream/cc_sockio.h +++ b/include/stream/cc_sockio.h @@ -48,6 +48,7 @@ extern "C" { #include #include +#include #include #include @@ -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; @@ -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 */ diff --git a/src/stream/cc_sockio.c b/src/stream/cc_sockio.c index 906fc463e..09c7af9a6 100644 --- a/src/stream/cc_sockio.c +++ b/src/stream/cc_sockio.c @@ -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) @@ -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; @@ -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; @@ -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 @@ -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); @@ -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