Browse files

command line option to set mbuf chunk size

  • Loading branch information...
1 parent 8753bc4 commit c9d0c173e520c3bc70df7fa0a6f7a4f437a76aa0 Manju Rajashekhar committed Jun 19, 2012
Showing with 55 additions and 19 deletions.
  1. +2 −1 README.md
  2. +29 −3 src/nc.c
  3. +1 −1 src/nc_core.c
  4. +2 −0 src/nc_core.h
  5. +18 −11 src/nc_mbuf.c
  6. +3 −3 src/nc_mbuf.h
View
3 README.md
@@ -43,7 +43,7 @@ To build nutcracker from source with _debug logs enabled_ and _assertions disabl
Usage: nutcracker [-?hVdt] [-v verbosity level] [-o output file]
[-c conf file] [-s stats port] [-i stats interval]
- [-p pid file]
+ [-p pid file] [-m mbuf size]
Options:
-h, --help : this help
@@ -56,6 +56,7 @@ To build nutcracker from source with _debug logs enabled_ and _assertions disabl
-s, --stats-port=N : set stats monitoring port (default: 22222)
-i, --stats-interval=N : set stats aggregation interval in msec (default: 30000 msec)
-p, --pid-file=S : set pid file (default: off)
+ -m, --mbuf-size=N : set size of mbuf chunk in bytes (default: 16384 bytes)
## Configuration ##
View
32 src/nc.c
@@ -39,6 +39,10 @@
#define NC_PID_FILE NULL
+#define NC_MBUF_SIZE MBUF_SIZE
+#define NC_MBUF_MIN_SIZE MBUF_MIN_SIZE
+#define NC_MBUF_MAX_SIZE MBUF_MAX_SIZE
+
static int show_help;
static int show_version;
static int test_conf;
@@ -55,10 +59,11 @@ static struct option long_options[] = {
{ "stats-port", required_argument, NULL, 's' },
{ "stats-interval", required_argument, NULL, 'i' },
{ "pid-file", required_argument, NULL, 'p' },
+ { "mbuf-size", required_argument, NULL, 'm' },
{ NULL, 0, NULL, 0 }
};
-static char short_options[] = "hVtdv:o:c:s:i:p:";
+static char short_options[] = "hVtdv:o:c:s:i:p:m:";
static rstatus_t
nc_daemonize(int dump_core)
@@ -184,7 +189,7 @@ nc_show_usage(void)
log_stderr(
"Usage: nutcracker [-?hVdt] [-v verbosity level] [-o output file]" CRLF
" [-c conf file] [-s stats port] [-i stats interval]" CRLF
- " [-p pid file]" CRLF
+ " [-p pid file] [-m mbuf size]" CRLF
"" CRLF
"Options:" CRLF
" -h, --help : this help" CRLF
@@ -197,11 +202,13 @@ nc_show_usage(void)
" -s, --stats-port=N : set stats monitoring port (default: %d)" CRLF
" -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
"",
NC_LOG_DEFAULT, NC_LOG_MIN, NC_LOG_MAX,
NC_LOG_PATH != NULL ? NC_LOG_PATH : "stderr",
NC_CONF_PATH, NC_STATS_PORT, NC_STATS_INTERVAL,
- NC_PID_FILE != NULL ? NC_PID_FILE : "off");
+ NC_PID_FILE != NULL ? NC_PID_FILE : "off",
+ NC_MBUF_SIZE);
}
static rstatus_t
@@ -267,6 +274,8 @@ nc_set_default_options(struct instance *nci)
}
nci->hostname[NC_MAXHOSTNAMELEN - 1] = '\0';
+ nci->mbuf_chunk_size = NC_MBUF_SIZE;
+
nci->pid = (pid_t)-1;
nci->pid_filename = NULL;
nci->pidfile = 0;
@@ -350,6 +359,22 @@ nc_get_options(int argc, char **argv, struct instance *nci)
nci->pid_filename = optarg;
break;
+ case 'm':
+ value = nc_atoi(optarg, strlen(optarg));
+ if (value <= 0) {
+ log_stderr("nutcracker: option -m requires a non-zero number");
+ return NC_ERROR;
+ }
+
+ if (value < NC_MBUF_MIN_SIZE || value > NC_MBUF_MAX_SIZE) {
+ log_stderr("nutcracker: mbuf chunk size must be between %zu and"
+ " %zu bytes", NC_MBUF_MIN_SIZE, NC_MBUF_MAX_SIZE);
+ return NC_ERROR;
+ }
+
+ nci->mbuf_chunk_size = (size_t)value;
+ break;
+
case '?':
switch (optopt) {
case 'o':
@@ -359,6 +384,7 @@ nc_get_options(int argc, char **argv, struct instance *nci)
optopt);
break;
+ case 'm':
case 'v':
case 's':
case 'i':
View
2 src/nc_core.c
@@ -130,7 +130,7 @@ core_start(struct instance *nci)
{
struct context *ctx;
- mbuf_init();
+ mbuf_init(nci);
msg_init();
conn_init();
View
2 src/nc_core.h
@@ -66,6 +66,7 @@ struct mhdr;
struct conf;
struct stats;
struct epoll_event;
+struct instance;
#include <stddef.h>
#include <stdint.h>
@@ -116,6 +117,7 @@ struct instance {
uint16_t stats_port; /* stats monitoring port */
int stats_interval; /* stats aggregation interval */
char hostname[NC_MAXHOSTNAMELEN]; /* hostname */
+ size_t mbuf_chunk_size; /* mbuf chunk size */
pid_t pid; /* process id */
char *pid_filename; /* pid filename */
unsigned pidfile:1; /* pid file created? */
View
29 src/nc_mbuf.c
@@ -30,6 +30,9 @@ static struct string mcopy_strings[] = {
static uint32_t nfree_mbufq; /* # free mbuf */
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 struct mbuf *
_mbuf_get(void)
{
@@ -47,7 +50,7 @@ _mbuf_get(void)
goto done;
}
- buf = nc_alloc(MBUF_SIZE);
+ buf = nc_alloc(mbuf_chunk_size);
if (buf == NULL) {
return NULL;
}
@@ -57,10 +60,10 @@ _mbuf_get(void)
* buffer overrun early by asserting on the magic value during get or
* put operations
*
- * <-------------- MBUF_SIZE ------------------>
+ * <------------- mbuf_chunk_size ------------->
* +-------------------------------------------+
* | mbuf data | mbuf header |
- * | (MBUF_LEN) | (struct mbuf) |
+ * | (mbuf_offset) | (struct mbuf) |
* +-------------------------------------------+
* ^ ^ ^ ^^
* | | | ||
@@ -71,7 +74,7 @@ _mbuf_get(void)
* mbuf->last (one byte past valid byte)
*
*/
- mbuf = (struct mbuf *)(buf + MBUF_OFFSET);
+ mbuf = (struct mbuf *)(buf + mbuf_offset);
mbuf->magic = MBUF_MAGIC;
done:
@@ -90,11 +93,11 @@ mbuf_get(void)
return NULL;
}
- buf = (uint8_t *)mbuf - MBUF_OFFSET;
+ buf = (uint8_t *)mbuf - mbuf_offset;
mbuf->start = buf;
- mbuf->end = buf + MBUF_OFFSET;
+ mbuf->end = buf + mbuf_offset;
- ASSERT(mbuf->end - mbuf->start == MBUF_LEN);
+ ASSERT(mbuf->end - mbuf->start == mbuf_offset);
ASSERT(mbuf->start < mbuf->end);
mbuf->pos = mbuf->start;
@@ -115,7 +118,7 @@ mbuf_free(struct mbuf *mbuf)
ASSERT(STAILQ_NEXT(mbuf, next) == NULL);
ASSERT(mbuf->magic == MBUF_MAGIC);
- buf = (uint8_t *)mbuf - MBUF_OFFSET;
+ buf = (uint8_t *)mbuf - mbuf_offset;
nc_free(buf);
}
@@ -263,12 +266,16 @@ mbuf_split(struct mhdr *h, uint8_t *pos, mcopy_type_t headcopy,
}
void
-mbuf_init(void)
+mbuf_init(struct instance *nci)
{
- log_debug(LOG_DEBUG, "mbuf hsize %d size %d offset %d length %d",
- MBUF_HSIZE, MBUF_SIZE, MBUF_OFFSET, MBUF_LEN);
nfree_mbufq = 0;
STAILQ_INIT(&free_mbufq);
+
+ mbuf_chunk_size = nci->mbuf_chunk_size;
+ mbuf_offset = mbuf_chunk_size - MBUF_HSIZE;
+
+ log_debug(LOG_DEBUG, "mbuf hsize %d chunk size %zu offset %zu length %d",
+ MBUF_HSIZE, mbuf_chunk_size, mbuf_offset, mbuf_offset);
}
void
View
6 src/nc_mbuf.h
@@ -45,10 +45,10 @@ struct mbuf {
STAILQ_HEAD(mhdr, mbuf);
#define MBUF_MAGIC 0xdeadbeef
+#define MBUF_MIN_SIZE 512
+#define MBUF_MAX_SIZE 65536
#define MBUF_SIZE 16384
#define MBUF_HSIZE sizeof(struct mbuf)
-#define MBUF_LEN (MBUF_SIZE - MBUF_HSIZE)
-#define MBUF_OFFSET MBUF_LEN
static inline bool
mbuf_empty(struct mbuf *mbuf)
@@ -62,7 +62,7 @@ mbuf_full(struct mbuf *mbuf)
return mbuf->last == mbuf->end ? true : false;
}
-void mbuf_init(void);
+void mbuf_init(struct instance *nci);
void mbuf_deinit(void);
struct mbuf *mbuf_get(void);
void mbuf_put(struct mbuf *mbuf);

0 comments on commit c9d0c17

Please sign in to comment.