Skip to content

Commit

Permalink
Merge pull request #8 from twitter/swaisbrot/option-default-type
Browse files Browse the repository at this point in the history
Consistent default_val option type
  • Loading branch information
Yao Yue committed Oct 31, 2015
2 parents c9a068b + e14545a commit 33c843e
Show file tree
Hide file tree
Showing 27 changed files with 138 additions and 116 deletions.
9 changes: 3 additions & 6 deletions client/src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ static struct setting setting = {
SETTING(OPTION_INIT)
};

#define PRINT_DEFAULT(_name, _type, _default, _description) \
log_stdout(" %-31s ( default: %s )", #_name, _default);

static const unsigned int nopt = OPTION_CARDINALITY(struct setting);

static void
Expand All @@ -41,7 +38,7 @@ show_usage(void)
"./pelikan_client ../template/client.conf" CRLF
);
log_stdout("Setting & Default Values:");
SETTING(PRINT_DEFAULT)
option_printall_default((struct option *)&setting, nopt);
}

static rstatus_t
Expand Down Expand Up @@ -138,10 +135,10 @@ main(int argc, char *argv[])
exit(EX_DATAERR);
}

option_printall((struct option *)&setting, nopt);

setup();

option_printall((struct option *)&setting, nopt);

client_core_run();

return 0;
Expand Down
7 changes: 4 additions & 3 deletions deps/ccommon/include/buffer/cc_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ extern "C" {
#include <stdbool.h>
#include <sys/param.h>

#define BUF_OPTION(ACTION) \
ACTION( buf_init_size, OPTION_TYPE_UINT, str(BUF_DEFAULT_SIZE), "default size when buf is created" )\
ACTION( buf_poolsize, OPTION_TYPE_UINT, str(BUF_POOLSIZE), "buf pool size" )
/* name type default description */
#define BUF_OPTION(ACTION) \
ACTION( buf_init_size, OPTION_TYPE_UINT, BUF_DEFAULT_SIZE, "default size when buf is created" )\
ACTION( buf_poolsize, OPTION_TYPE_UINT, BUF_POOLSIZE, "buf pool size" )

/* name type description */
#define BUF_METRIC(ACTION) \
Expand Down
3 changes: 2 additions & 1 deletion deps/ccommon/include/buffer/cc_dbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ extern "C" {

#include <stdbool.h>

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

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

Expand Down
6 changes: 3 additions & 3 deletions deps/ccommon/include/cc_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ extern "C" {

#define NELEM_DELTA 16

/* name type default description */
#define ARRAY_OPTION(ACTION) \
ACTION( array_nelem_delta, OPTION_TYPE_UINT, str(NELEM_DELTA), "max nelem delta during expansion" )
/* name type default description */
#define ARRAY_OPTION(ACTION) \
ACTION( array_nelem_delta, OPTION_TYPE_UINT, NELEM_DELTA, "max nelem delta during expansion" )


typedef int (*array_compare_t)(const void *, const void *);
Expand Down
12 changes: 6 additions & 6 deletions deps/ccommon/include/cc_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ extern "C" {
#define DEBUG_LOG_NBUF 4 * MiB /* default log buf size */
#define DEBUG_LOG_INTVL 100000 /* flush every 100 milliseconds */

/* name type default description */
#define DEBUG_OPTION(ACTION) \
ACTION( debug_log_level, OPTION_TYPE_UINT, str(DEBUG_LOG_LEVEL), "debug log level" )\
ACTION( debug_log_file, OPTION_TYPE_STR, NULL, "debug log file" )\
ACTION( debug_log_nbuf, OPTION_TYPE_UINT, str(DEBUG_LOG_NBUF), "debug log buf size" )\
ACTION( debug_log_intvl, OPTION_TYPE_UINT, str(DEBUG_LOG_INTVL), "debug log flush interval")
/* name type default description */
#define DEBUG_OPTION(ACTION) \
ACTION( debug_log_level, OPTION_TYPE_UINT, DEBUG_LOG_LEVEL, "debug log level" )\
ACTION( debug_log_file, OPTION_TYPE_STR, NULL, "debug log file" )\
ACTION( debug_log_nbuf, OPTION_TYPE_UINT, DEBUG_LOG_NBUF, "debug log buf size" )\
ACTION( debug_log_intvl, OPTION_TYPE_UINT, DEBUG_LOG_INTVL, "debug log flush interval")

/**
* the debug module override the following signal handlers:
Expand Down
8 changes: 6 additions & 2 deletions deps/ccommon/include/cc_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ extern "C" {

/* TODO(yao): add an (optional) callback that can sanity-check input values */

#define OPTION_TYPE_BOOL_VAR vbool
#define OPTION_TYPE_UINT_VAR vuint
#define OPTION_TYPE_STR_VAR vstr

#define OPTION_DECLARE(_name, _type, _default, _description) \
struct option _name;

/* Initialize option */
#define OPTION_INIT(_name, _type, _default, _description) \
._name = {.name = #_name, .set = false, .type = _type, \
.default_val_str = _default, .description = _description},
.default_val._type ## _VAR = _default, .description = _description},

#define OPTION_CARDINALITY(_o) sizeof(_o)/sizeof(struct option)

Expand All @@ -76,7 +79,7 @@ struct option {
char *name;
bool set;
option_type_t type;
char *default_val_str;
option_val_u default_val;
option_val_u val;
char *description;
};
Expand All @@ -85,6 +88,7 @@ rstatus_t option_set(struct option *opt, char *val_str);
rstatus_t option_parse(char *line, char *name, char *val);
void option_print(struct option *opt);
void option_printall(struct option options[], unsigned int nopt);
void option_printall_default(struct option options[], unsigned int nopt);
rstatus_t option_load_default(struct option options[], unsigned int nopt);
rstatus_t option_load_file(FILE *fp, struct option options[], unsigned int nopt);
void option_free(struct option options[], unsigned int nopt);
Expand Down
6 changes: 3 additions & 3 deletions deps/ccommon/include/cc_ring_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

#define RING_ARRAY_DEFAULT_CAP 1024

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

struct ring_array {
size_t elem_size; /* element size */
Expand Down
3 changes: 2 additions & 1 deletion deps/ccommon/include/channel/cc_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ typedef struct {
*(_metrics) = (pipe_metrics_st) { PIPE_METRIC(METRIC_INIT) }; \
} while (0)

/* name type default description */
#define PIPE_OPTION(ACTION) \
ACTION( pipe_poolsize, OPTION_TYPE_UINT, str(PIPE_POOLSIZE), "pipe conn pool size" )
ACTION( pipe_poolsize, OPTION_TYPE_UINT, PIPE_POOLSIZE, "pipe conn pool size" )

struct pipe_conn {
STAILQ_ENTRY(pipe_conn) next; /* for pool */
Expand Down
8 changes: 4 additions & 4 deletions deps/ccommon/include/channel/cc_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ typedef struct {
*(_metrics) = (tcp_metrics_st) { TCP_METRIC(METRIC_INIT) }; \
} while(0)

/* name type default description */
#define TCP_OPTION(ACTION) \
ACTION( tcp_backlog, OPTION_TYPE_UINT, str(TCP_BACKLOG), "tcp conn backlog limit" )\
ACTION( tcp_poolsize, OPTION_TYPE_UINT, str(TCP_POOLSIZE), "tcp conn pool size" )
/* name type default description */
#define TCP_OPTION(ACTION) \
ACTION( tcp_backlog, OPTION_TYPE_UINT, TCP_BACKLOG, "tcp conn backlog limit" )\
ACTION( tcp_poolsize, OPTION_TYPE_UINT, TCP_POOLSIZE, "tcp conn pool size" )

struct tcp_conn {
STAILQ_ENTRY(tcp_conn) next; /* for conn pool */
Expand Down
6 changes: 3 additions & 3 deletions deps/ccommon/include/stream/cc_sockio.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ extern "C" {

#define BUFSOCK_POOLSIZE 0 /* unlimited */

/* name type default description */
#define SOCKIO_OPTION(ACTION) \
ACTION( buf_sock_poolsize, OPTION_TYPE_UINT, str(BUFSOCK_POOLSIZE), "buf_sock limit" )
/* name type default description */
#define SOCKIO_OPTION(ACTION) \
ACTION( buf_sock_poolsize, OPTION_TYPE_UINT, BUFSOCK_POOLSIZE, "buf_sock limit" )

struct buf_sock {
/* these fields are useful for resource managmenet */
Expand Down
62 changes: 44 additions & 18 deletions deps/ccommon/src/cc_option.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,25 +446,22 @@ option_parse(char *line, char name[OPTNAME_MAXLEN+1], char val[OPTVAL_MAXLEN+1])
return CC_OK;
}

void option_print(struct option *opt)
static void
option_print_val(char *s, size_t len, option_type_t type, option_val_u val)
{
loga("name: %s, type: %s, set? %s, default: %s, description: %s",
opt->name, option_type_str[opt->type], opt->set ? "yes" : "no",
opt->default_val_str, opt->description);

switch (opt->type) {
switch (type) {
case OPTION_TYPE_BOOL:
loga("current value: %d", opt->val.vbool);
snprintf(s, len, "%s", val.vbool ? "yes" : "no");

break;

case OPTION_TYPE_UINT:
loga("current value: %ju", opt->val.vuint);
snprintf(s, len, "%ju", val.vuint);

break;

case OPTION_TYPE_STR:
loga("current value: %s", opt->val.vstr == NULL ? "NULL" : opt->val.vstr);
snprintf(s, len, "%s", val.vstr == NULL ? "NULL" : val.vstr);

break;

Expand All @@ -473,7 +470,22 @@ void option_print(struct option *opt)
}
}

void option_printall(struct option options[], unsigned int nopt)
void
option_print(struct option *opt)
{
#define MAX_LEN 40
char default_s[MAX_LEN];
char current_s[MAX_LEN];
option_print_val(default_s, MAX_LEN, opt->type, opt->default_val);
option_print_val(current_s, MAX_LEN, opt->type, opt->val);
loga("name: %s, type: %s, set? %s, default: %s, description: %s, current: %s",
opt->name, option_type_str[opt->type], opt->set ? "yes" : "no",
default_s, opt->description, current_s);
#undef MAX_LEN
}

void
option_printall(struct option options[], unsigned int nopt)
{
unsigned int i;
struct option *opt = options;
Expand All @@ -484,21 +496,35 @@ void option_printall(struct option options[], unsigned int nopt)

}

static void
_option_print_default(struct option *opt)
{
#define MAX_LEN (10 + PATH_MAX)
char default_s[MAX_LEN];
option_print_val(default_s, MAX_LEN, opt->type, opt->default_val);
log_stdout(" %-31s ( default: %s )", opt->name, default_s);
#undef MAX_LEN
}

void
option_printall_default(struct option options[], unsigned int nopt)
{
unsigned int i;
struct option *opt = options;

for (i = 0; i < nopt; i++, opt++) {
_option_print_default(opt);
}
}

rstatus_t
option_load_default(struct option options[], unsigned int nopt)
{
unsigned int i;
rstatus_t status;
struct option *opt = options;

for (i = 0; i < nopt; i++, opt++) {
status = option_set(opt, opt->default_val_str);
if (status != CC_OK) {
loga("error loading default value %s into option type %d",
opt->default_val_str, opt->type);

return CC_ERROR;
}
opt->val = opt->default_val;
}

return CC_OK;
Expand Down
9 changes: 3 additions & 6 deletions src/pingserver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ static struct setting setting = {
SETTING(OPTION_INIT)
};

#define PRINT_DEFAULT(_name, _type, _default, _description) \
log_stdout(" %-31s ( default: %s )", #_name, _default);

static const unsigned int nopt = OPTION_CARDINALITY(struct setting);

static void
Expand All @@ -42,7 +39,7 @@ show_usage(void)
" ./pelikan_pingserver" CRLF
);
log_stdout("Setting & Default Values:");
SETTING(PRINT_DEFAULT)
option_printall_default((struct option *)&setting, nopt);
}

static void
Expand Down Expand Up @@ -164,9 +161,9 @@ main(int argc, char **argv)
exit(EX_DATAERR);
}

option_printall((struct option *)&setting, nopt);

setup();

option_printall((struct option *)&setting, nopt);

exit(EX_OK);
}
2 changes: 1 addition & 1 deletion src/pingserver/setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* option related */
/* name type default description */
#define SERVER_OPTION(ACTION) \
ACTION( daemonize, OPTION_TYPE_BOOL, "no", "daemonize the process" )\
ACTION( daemonize, OPTION_TYPE_BOOL, false, "daemonize the process" )\
ACTION( pid_filename, OPTION_TYPE_STR, NULL, "file storing the pid" )\
ACTION( server_host, OPTION_TYPE_STR, NULL, "interfaces listening on" )\
ACTION( server_port, OPTION_TYPE_STR, "54321", "port listening on" )
Expand Down
12 changes: 6 additions & 6 deletions src/protocol/memcache/klog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#define KLOG_INTVL 100000 /* flush every 100 milliseconds */
#define KLOG_SAMPLE 100 /* log one in every 100 commands */

/* name type default description */
#define KLOG_OPTION(ACTION) \
ACTION( klog_file, OPTION_TYPE_STR, NULL, "command log file" )\
ACTION( klog_nbuf, OPTION_TYPE_UINT, str(KLOG_NBUF), "command log buf size" )\
ACTION( klog_intvl, OPTION_TYPE_UINT, str(KLOG_INTVL), "command log flush interval" )\
ACTION( klog_sample, OPTION_TYPE_UINT, str(KLOG_SAMPLE), "command log sample ratio" )
/* name type default description */
#define KLOG_OPTION(ACTION) \
ACTION( klog_file, OPTION_TYPE_STR, NULL, "command log file" )\
ACTION( klog_nbuf, OPTION_TYPE_UINT, KLOG_NBUF, "command log buf size" )\
ACTION( klog_intvl, OPTION_TYPE_UINT, KLOG_INTVL, "command log flush interval" )\
ACTION( klog_sample, OPTION_TYPE_UINT, KLOG_SAMPLE, "command log sample ratio" )

/* name type description */
#define KLOG_METRIC(ACTION) \
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/memcache/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#define REQ_POOLSIZE 0

/* name type default description */
#define REQUEST_OPTION(ACTION) \
ACTION( request_poolsize, OPTION_TYPE_UINT, str(REQ_POOLSIZE), "request pool size")
#define REQUEST_OPTION(ACTION) \
ACTION( request_poolsize, OPTION_TYPE_UINT, REQ_POOLSIZE, "request pool size")

/* name type description */
#define REQUEST_METRIC(ACTION) \
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/memcache/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#define RSP_POOLSIZE 0

/* name type default description */
#define RESPONSE_OPTION(ACTION) \
ACTION( response_poolsize, OPTION_TYPE_UINT, str(RSP_POOLSIZE), "response pool size" )
/* name type default description */
#define RESPONSE_OPTION(ACTION) \
ACTION( response_poolsize, OPTION_TYPE_UINT, RSP_POOLSIZE, "response pool size" )

/* name type description */
#define RESPONSE_METRIC(ACTION) \
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/ping/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <cc_bstring.h>
#include <cc_metric.h>

/* name type default description */
#define REQUEST_OPTION(ACTION) \
ACTION( request_poolsize, OPTION_TYPE_UINT, str(REQ_POOLSIZE), "request pool size")
/* name type default description */
#define REQUEST_OPTION(ACTION) \
ACTION( request_poolsize, OPTION_TYPE_UINT, REQ_POOLSIZE, "request pool size")

/* name type description */
#define REQUEST_METRIC(ACTION) \
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/ping/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <cc_bstring.h>
#include <cc_metric.h>

/* name type default description */
#define RESPONSE_OPTION(ACTION) \
ACTION( response_poolsize, OPTION_TYPE_UINT, str(RSP_POOLSIZE), "response pool size" )
/* name type default description */
#define RESPONSE_OPTION(ACTION) \
ACTION( response_poolsize, OPTION_TYPE_UINT, RSP_POOLSIZE, "response pool size" )

/* name type description */
#define RESPONSE_METRIC(ACTION) \
Expand Down
Loading

0 comments on commit 33c843e

Please sign in to comment.