Skip to content

Commit

Permalink
add the maximum number of mbuf chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqiang committed Feb 23, 2019
1 parent 0785822 commit 2b2a0d0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/nc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#define NC_MBUF_MIN_SIZE MBUF_MIN_SIZE
#define NC_MBUF_MAX_SIZE MBUF_MAX_SIZE

#define NC_MBUF_MIN_NUM MBUF_MIN_NUM
#define NC_MBUF_MAX_NUM MBUF_MAX_NUM

static int show_help;
static int show_version;
static int test_conf;
Expand All @@ -65,10 +68,11 @@ static struct option long_options[] = {
{ "stats-addr", required_argument, NULL, 'a' },
{ "pid-file", required_argument, NULL, 'p' },
{ "mbuf-size", required_argument, NULL, 'm' },
{ "mbuf-number", required_argument, NULL, 'n' },
{ NULL, 0, NULL, 0 }
};

static char short_options[] = "hVtdDv:o:c:s:i:a:p:m:";
static char short_options[] = "hVtdDv:o:c:s:i:a:p:m:n:";

static rstatus_t
nc_daemonize(int dump_core)
Expand Down Expand Up @@ -205,6 +209,7 @@ nc_show_usage(void)
"Usage: nutcracker [-?hVdDt] [-v verbosity level] [-o output file]" CRLF
" [-c conf file] [-s stats port] [-a stats addr]" CRLF
" [-i stats interval] [-p pid file] [-m mbuf size]" CRLF
" [-n mbuf number]" CRLF
"");
log_stderr(
"Options:" CRLF
Expand All @@ -222,6 +227,7 @@ nc_show_usage(void)
" -i, --stats-interval=N : set stats aggregation interval in msec (default: %d msec)" CRLF
" -p, --pid-file=S : set pid file (default: %s)" CRLF
" -m, --mbuf-size=N : set size of mbuf chunk in bytes (default: %d bytes)" CRLF
" -n, --mbuf-number=N : set maximum number of mbuf chunks (default: 0, means no limit)" CRLF
"",
NC_LOG_DEFAULT, NC_LOG_MIN, NC_LOG_MAX,
NC_LOG_PATH != NULL ? NC_LOG_PATH : "stderr",
Expand Down Expand Up @@ -296,6 +302,7 @@ nc_set_default_options(struct instance *nci)
nci->hostname[NC_MAXHOSTNAMELEN - 1] = '\0';

nci->mbuf_chunk_size = NC_MBUF_SIZE;
nci->mbuf_chunk_max = 0;

nci->pid = (pid_t)-1;
nci->pid_filename = NULL;
Expand Down Expand Up @@ -405,6 +412,24 @@ nc_get_options(int argc, char **argv, struct instance *nci)
nci->mbuf_chunk_size = (size_t)value;
break;

case 'n':
value = nc_atoi(optarg, strlen(optarg));
if (value < 0) {
log_stderr("nutcracker: option -n requires a number");
return NC_ERROR;
}

if ((value != 0 && value < NC_MBUF_MIN_NUM) ||
value > NC_MBUF_MAX_NUM) {
log_stderr("nutcracker: maximum number of mbuf chunks must be "
"zero or between %zu and %zu", NC_MBUF_MIN_NUM,
NC_MBUF_MAX_NUM);
return NC_ERROR;
}

nci->mbuf_chunk_max = (size_t)value;
break;

case '?':
switch (optopt) {
case 'o':
Expand Down
1 change: 1 addition & 0 deletions src/nc_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ struct instance {
char *stats_addr; /* stats monitoring addr */
char hostname[NC_MAXHOSTNAMELEN]; /* hostname */
size_t mbuf_chunk_size; /* mbuf chunk size */
size_t mbuf_chunk_max; /* maximum number of mbuf chunks */
pid_t pid; /* process id */
char *pid_filename; /* pid filename */
unsigned pidfile:1; /* pid file created? */
Expand Down
7 changes: 7 additions & 0 deletions src/nc_mbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static struct mhdr free_mbufq; /* free mbuf q */

static size_t mbuf_chunk_size; /* mbuf chunk size - header + data (const) */
static size_t mbuf_offset; /* mbuf offset in chunk (const) */
static size_t mbuf_chunk_max; /* maximum number of mbuf chunks */

static struct mbuf *
_mbuf_get(void)
Expand Down Expand Up @@ -123,6 +124,11 @@ mbuf_put(struct mbuf *mbuf)
ASSERT(STAILQ_NEXT(mbuf, next) == NULL);
ASSERT(mbuf->magic == MBUF_MAGIC);

if (mbuf_chunk_max != 0 && nfree_mbufq >= mbuf_chunk_max) {
mbuf_free(mbuf);
return;
}

nfree_mbufq++;
STAILQ_INSERT_HEAD(&free_mbufq, mbuf, next);
}
Expand Down Expand Up @@ -267,6 +273,7 @@ mbuf_init(struct instance *nci)

mbuf_chunk_size = nci->mbuf_chunk_size;
mbuf_offset = mbuf_chunk_size - MBUF_HSIZE;
mbuf_chunk_max = nci->mbuf_chunk_max;

log_debug(LOG_DEBUG, "mbuf hsize %d chunk size %zu offset %zu length %zu",
MBUF_HSIZE, mbuf_chunk_size, mbuf_offset, mbuf_offset);
Expand Down
2 changes: 2 additions & 0 deletions src/nc_mbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ STAILQ_HEAD(mhdr, mbuf);
#define MBUF_MAX_SIZE 16777216
#define MBUF_SIZE 16384
#define MBUF_HSIZE sizeof(struct mbuf)
#define MBUF_MIN_NUM 524288
#define MBUF_MAX_NUM 134217728

static inline bool
mbuf_empty(struct mbuf *mbuf)
Expand Down

0 comments on commit 2b2a0d0

Please sign in to comment.