Skip to content

Commit

Permalink
hide pool create/destroy methods and invoke them from setup/teardown …
Browse files Browse the repository at this point in the history
…instead
  • Loading branch information
Yao Yue committed Mar 14, 2016
1 parent d56920d commit 66a8526
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 210 deletions.
7 changes: 2 additions & 5 deletions include/buffer/cc_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {
#include <cc_debug.h>
#include <cc_define.h>
#include <cc_metric.h>
#include <cc_option.h>
#include <cc_queue.h>
#include <cc_util.h>

Expand Down Expand Up @@ -93,13 +94,9 @@ extern buf_metrics_st *buf_metrics;
((BUF)->wpos == (BUF)->end)

/* Setup/teardown buf module */
void buf_setup(uint32_t size, buf_metrics_st *metrics);
void buf_setup(buf_options_st *options, buf_metrics_st *metrics);
void buf_teardown(void);

/* Create/destroy buffer pool */
void buf_pool_create(uint32_t max);
void buf_pool_destroy(void);

/* Obtain/return a buffer from the pool */
struct buf *buf_borrow(void);
void buf_return(struct buf **buf);
Expand Down
6 changes: 3 additions & 3 deletions include/buffer/cc_dbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ extern "C" {


/* name type default description */
#define DBUF_OPTION(ACTION) \
#define DBUF_OPTION(ACTION) \
ACTION( dbuf_max_power, OPTION_TYPE_UINT, DBUF_DEFAULT_MAX, "max number of doubling" )

typedef struct {
DBUF_OPTION(OPTION_DECLARE)
} dbuf_options_st;

#define DBUF_DEFAULT_MAX 6 /* with 16KiB default size, this gives us 1 MiB max */
#define DBUF_DEFAULT_MAX 8 /* with 16KiB default size, this gives us 4 MiB max */

/* Setup/teardown doubling buffer module */
void dbuf_setup(uint8_t power);
void dbuf_setup(dbuf_options_st *options);
void dbuf_teardown(void);

/* Buffer resizing functions */
Expand Down
2 changes: 1 addition & 1 deletion include/cc_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void array_sort(struct array *arr, array_compare_fn compare);
uint32_t array_each(struct array *arr, array_each_fn func, void *arg, err_i *err);

/* TODO(yao): refactor to use better arg names */
void array_setup(uint32_t nelem);
void array_setup(array_options_st *options);
void array_teardown(void);

#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions include/cc_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" {
#endif

#include <cc_define.h>
#include <cc_option.h>
#include <cc_signal.h>

#include <stdint.h>
Expand Down Expand Up @@ -83,8 +84,7 @@ typedef struct {

void debug_assert(const char *cond, const char *file, int line, int panic);

rstatus_i debug_setup(int level, char *log_file, uint32_t log_nbuf,
uint64_t log_intvl);
rstatus_i debug_setup(debug_options_st *options);
void debug_teardown(void);

/**
Expand Down
15 changes: 15 additions & 0 deletions include/cc_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ struct option {
char *description;
};

static inline bool
option_bool(struct option *opt) {
return opt->val.vbool;
}

static inline uintmax_t
option_uint(struct option *opt) {
return opt->val.vuint;
}

static inline char *
option_str(struct option *opt) {
return opt->val.vstr;
}

rstatus_i option_set(struct option *opt, char *val_str);
rstatus_i option_default(struct option *opt);
void option_print(struct option *opt);
Expand Down
9 changes: 0 additions & 9 deletions include/cc_ring_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,8 @@
#include <stddef.h>
#include <stdint.h>


#define RING_ARRAY_DEFAULT_CAP 1024

/* name type default description */
#define RING_ARRAY_OPTION(ACTION) \
ACTION( ring_array_cap, OPTION_TYPE_UINT, RING_ARRAY_DEFAULT_CAP, "default ring array capacity" )

typedef struct {
RING_ARRAY_OPTION(OPTION_DECLARE)
} ring_array_options_st;

struct ring_array {
size_t elem_size; /* element size */
uint32_t cap; /* total capacity (# items stored + 1) */
Expand Down
4 changes: 1 addition & 3 deletions include/channel/cc_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct pipe_conn {

STAILQ_HEAD(pipe_conn_sqh, pipe_conn); /* corresponding header type for the STAILQ */

void pipe_setup(pipe_metrics_st *metrics);
void pipe_setup(pipe_options_st *options, pipe_metrics_st *metrics);
void pipe_teardown(void);

/* functions for managing pipe connection structs */
Expand All @@ -101,8 +101,6 @@ void pipe_conn_destroy(struct pipe_conn **c);
void pipe_conn_reset(struct pipe_conn *c);

/* pool functions */
void pipe_conn_pool_create(uint32_t max);
void pipe_conn_pool_destroy(void);
struct pipe_conn *pipe_conn_borrow(void);
void pipe_conn_return(struct pipe_conn **c);

Expand Down
4 changes: 1 addition & 3 deletions include/channel/cc_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct tcp_conn {

STAILQ_HEAD(tcp_conn_sqh, tcp_conn); /* corresponding header type for the STAILQ */

void tcp_setup(int backlog, tcp_metrics_st *metrics);
void tcp_setup(tcp_options_st *options, tcp_metrics_st *metrics);
void tcp_teardown(void);

void tcp_conn_reset(struct tcp_conn *c);
Expand All @@ -110,8 +110,6 @@ void tcp_conn_reset(struct tcp_conn *c);
struct tcp_conn *tcp_conn_create(void); /* channel_get_fn, with allocation */
void tcp_conn_destroy(struct tcp_conn **c); /* channel_put_fn, with deallocation */

void tcp_conn_pool_create(uint32_t max);
void tcp_conn_pool_destroy(void);
struct tcp_conn *tcp_conn_borrow(void); /* channel_get_fn, with resource pool */
void tcp_conn_return(struct tcp_conn **c); /* channel_put_fn, with resource pool */

Expand Down
5 changes: 3 additions & 2 deletions include/stream/cc_sockio.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ 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_teardown(void);

struct buf_sock *buf_sock_create(void); /* stream_get_fn */
void buf_sock_destroy(struct buf_sock **); /* stream_put_fn */

void buf_sock_pool_create(uint32_t);
void buf_sock_pool_destroy(void);
struct buf_sock *buf_sock_borrow(void); /* stream_get_fn */
void buf_sock_return(struct buf_sock **); /* stream_put_fn */

Expand Down
94 changes: 49 additions & 45 deletions src/buffer/cc_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,32 @@ static bool bufp_init = false;
uint32_t buf_init_size = BUF_INIT_SIZE;
buf_metrics_st *buf_metrics = NULL;

void
buf_setup(uint32_t size, buf_metrics_st *metrics)
static void
buf_pool_destroy(void)
{
log_info("setting up the %s module", BUF_MODULE_NAME);
struct buf *buf, *nbuf;

buf_init_size = size;
buf_metrics = metrics;
if (metrics != NULL) {
BUF_METRIC_INIT(buf_metrics);
}
if (!bufp_init) {
log_warn("buf pool was never created, ignoring destroy");

if (buf_init) {
log_warn("%s was already setup, overwriting", BUF_MODULE_NAME);
return;
}

buf_init = true;

log_info("buf: size %zu", size);
}

void
buf_teardown(void)
{
log_info("tear down the %s module", BUF_MODULE_NAME);

if (!buf_init) {
log_warn("%s was not setup but is being torn down", BUF_MODULE_NAME);
}
log_info("destroying buf pool: free %"PRIu32, bufp.nfree);

buf_metrics = NULL;
buf_init = false;
FREEPOOL_DESTROY(buf, nbuf, &bufp, next, buf_destroy);
bufp_init = false;
}

void
static void
buf_pool_create(uint32_t max)
{
struct buf *buf;

if (bufp_init) {
log_warn("buf pool has already been created, ignoring");
log_warn("buf pool has already been created, re-creating");

return;
buf_pool_destroy();
}

log_info("creating buf pool: max %"PRIu32, max);
Expand All @@ -96,23 +80,6 @@ buf_pool_create(uint32_t max)
}
}

void
buf_pool_destroy(void)
{
struct buf *buf, *nbuf;

if (!bufp_init) {
log_warn("buf pool was never created, ignoring destroy");

return;
}

log_info("destroying buf pool: free %"PRIu32, bufp.nfree);

FREEPOOL_DESTROY(buf, nbuf, &bufp, next, buf_destroy);
bufp_init = false;
}

struct buf *
buf_borrow(void)
{
Expand Down Expand Up @@ -199,3 +166,40 @@ buf_destroy(struct buf **buf)
DECR(buf_metrics, buf_curr);
DECR_N(buf_metrics, buf_memory, cap);
}

void
buf_setup(buf_options_st *options, buf_metrics_st *metrics)
{
log_info("setting up the %s module", BUF_MODULE_NAME);

if (buf_init) {
log_warn("%s was already setup, overwriting", BUF_MODULE_NAME);
}

if (options != NULL) {
buf_init_size = option_uint(&options->buf_init_size);
buf_pool_create(option_uint(&options->buf_poolsize));
}

buf_metrics = metrics;
if (metrics != NULL) {
BUF_METRIC_INIT(buf_metrics);
}

buf_init = true;
}

void
buf_teardown(void)
{
log_info("tear down the %s module", BUF_MODULE_NAME);

if (!buf_init) {
log_warn("%s was not setup but is being torn down", BUF_MODULE_NAME);
}

buf_pool_destroy();
buf_metrics = NULL;

buf_init = false;
}
14 changes: 7 additions & 7 deletions src/buffer/cc_dbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ static uint8_t max_power = DBUF_DEFAULT_MAX;
static uint32_t max_size = BUF_INIT_SIZE << DBUF_DEFAULT_MAX;

void
dbuf_setup(uint8_t power)
dbuf_setup(dbuf_options_st *options)
{
log_info("set up the %s module", DBUF_MODULE_NAME);

/* TODO(yao): validate input */
max_power = power;
max_size = buf_init_size << power;

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

dbuf_init = true;
if (options != NULL) {
/* TODO(yao): validate input */
max_power = option_uint(&options->dbuf_max_power);
max_size = buf_init_size << max_power;
}

log_info("buffer/dbuf: max size %zu", max_size);
dbuf_init = true;
}

void
Expand Down
8 changes: 6 additions & 2 deletions src/cc_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,18 @@ array_each(struct array *arr, array_each_fn func, void *arg, err_i *err)
}

/* set the maximum number of elements allocated every time array expands */
void array_setup(uint32_t nelem)
void array_setup(array_options_st *options)
{
log_info("set up the %s module", ARRAY_MODULE_NAME);

max_nelem_delta = nelem;
if (array_init) {
log_warn("%s has already been setup, overwrite", ARRAY_MODULE_NAME);
}

if (options != NULL) {
max_nelem_delta = option_uint(&options->array_nelem_delta);
}

array_init = true;
}

Expand Down
17 changes: 13 additions & 4 deletions src/cc_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,35 @@ _debug_log_flush(void *arg)
}

rstatus_i
debug_setup(int log_level, char *log_file, uint32_t log_nbuf,
uint64_t log_intvl)
debug_setup(debug_options_st *options)
{
size_t log_nbuf;
uint64_t log_intvl;

/* since logs are not setup yet, we have to log to stderr */
log_stderr("Set up the %s module", DEBUG_MODULE_NAME);

if (debug_init) {
log_stderr("%s has already been setup, overwrite", DEBUG_MODULE_NAME);
}

if (options == NULL) { /* use default_logger */
return CC_OK;
}

if (dlog->logger != NULL) {
log_stderr("logger already exists, recreating it.");
log_destroy(&dlog->logger);
}

dlog->logger = log_create(log_file, log_nbuf);
log_nbuf = option_uint(&options->debug_log_nbuf);
log_intvl = option_uint(&options->debug_log_intvl);
dlog->logger = log_create(option_str(&options->debug_log_file), log_nbuf);
if (dlog->logger == NULL) {
log_stderr("Could not create logger!");
return CC_ERROR;
}
dlog->level = log_level;
dlog->level = option_uint(&options->debug_log_level);

/*
* 0 length buffer indicates that the logger will log directly to the file,
Expand Down
Loading

0 comments on commit 66a8526

Please sign in to comment.