PERFORCE change 122017 for review
Fredrik Lindberg
fli at FreeBSD.org
Wed Jun 20 10:19:09 UTC 2007
http://perforce.freebsd.org/chv.cgi?CH=122017
Change 122017 by fli at fli_genesis on 2007/06/20 10:18:28
- Revisit the buffer management code. We now keep two different
sized chunks, one huge (9000 bytes, maximum mdns packet length)
and one "MTU-sized".
- Add some debugging printouts.
Affected files ...
.. //depot/projects/soc2007/fli-mdns_sd/mdnsd/stack_buf.c#7 edit
.. //depot/projects/soc2007/fli-mdns_sd/mdnsd/stack_buf.h#5 edit
Differences ...
==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/stack_buf.c#7 (text+ko) ====
@@ -29,7 +29,9 @@
#include <string.h>
#include <strings.h>
+#include "stack_mdns.h"
#include "stack_buf.h"
+#include "log.h"
#include "debug.h"
struct mdns_bufpool * mdns_bufpool_init(size_t, int,
@@ -48,6 +50,8 @@
assert((lock == NULL && unlock == NULL) || (lock != unlock));
bp = malloc(sizeof(struct mdns_bufpool));
+ if (bp == NULL)
+ return (NULL);
bzero(bp, sizeof(struct mdns_bufpool));
bp->bp_defsz = defsz;
bp->bp_lim = lim;
@@ -55,34 +59,41 @@
bp->bp_unlock = unlock;
bp->bp_lockarg = arg;
TAILQ_INIT(&bp->bp_list);
+ TAILQ_INIT(&bp->bp_huge);
MDNS_INIT_SET(bp, bp_magic);
+ dprintf(DEBUG_BUF, "Bufferpool initialized defsz=%d, bp=%x", defsz, bp);
return (bp);
}
int
mdns_bufpool_destroy(struct mdns_bufpool *bp)
{
- struct mdns_buf *buf;
- int ret;
+ struct mdns_buf *buf, *buf2;
MDNS_INIT_ASSERT(bp, bp_magic);
MDNS_BP_LOCK(bp);
- while (!TAILQ_EMPTY(&bp->bp_list)) {
- buf = TAILQ_FIRST(&bp->bp_list);
- TAILQ_REMOVE(&bp->bp_list, buf, b_list);
+ TAILQ_FOREACH_SAFE(buf, &bp->bp_list, b_next, buf2) {
+ MDNS_INIT_ASSERT(buf, b_magic);
+ MDNS_INIT_UNSET(buf, b_magic);
+ TAILQ_REMOVE(&bp->bp_list, buf, b_next);
+ free(buf->b_buf);
+ free(buf);
+ }
+ TAILQ_FOREACH_SAFE(buf, &bp->bp_huge, b_next, buf2) {
+ MDNS_INIT_ASSERT(buf, b_magic);
+ MDNS_INIT_UNSET(buf, b_magic);
+ TAILQ_REMOVE(&bp->bp_huge, buf, b_next);
free(buf->b_buf);
free(buf);
}
- ret = TAILQ_EMPTY(&bp->bp_list);
+ MDNS_INIT_UNSET(bp, bp_magic);
MDNS_BP_UNLOCK(bp);
+ free(bp);
- if (ret == 1) {
- MDNS_INIT_UNSET(bp, bp_magic);
- free(bp);
- }
- return (ret);
+ dprintf(DEBUG_BUF, "Bufferpool destroyed bp=%x", bp);
+ return (0);
}
void
@@ -93,6 +104,7 @@
MDNS_BP_LOCK(bp);
bp->bp_defsz = len;
MDNS_BP_UNLOCK(bp);
+ dprintf(DEBUG_BUF, "Bufferpool size set to %d bytes, bp=%x", len, bp);
}
size_t
@@ -109,53 +121,73 @@
}
struct mdns_buf *
-mdns_buf_alloc(struct mdns_bufpool *bp, struct mdns_bufhead *bh,
- size_t len, int locked)
+mdns_buf_alloc(struct mdns_bufpool *bp, struct mdns_bufhead *bh, size_t len,
+ int flags)
{
struct mdns_buf *buf;
MDNS_INIT_ASSERT(bp, bp_magic);
- if (!locked)
+ if (!(flags & MDNS_BP_LOCKED))
MDNS_BP_LOCK(bp);
buf = NULL;
- do {
+ if (flags & MDNS_BP_HUGE) {
+ if (!TAILQ_EMPTY(&bp->bp_huge)) {
+ buf = TAILQ_FIRST(&bp->bp_huge);
+ TAILQ_REMOVE(&bp->bp_huge, buf, b_next);
+ bp->bp_buffree[1]--;
+ }
+ }
+ else {
if (!TAILQ_EMPTY(&bp->bp_list)) {
buf = TAILQ_FIRST(&bp->bp_list);
- TAILQ_REMOVE(&bp->bp_list, buf, b_list);
- bp->bp_buffree--;
- if (buf->b_sz < bp->bp_defsz) {
- mdns_buf_free(bp, bh, buf, 1);
- buf = NULL;
- }
- else if (buf->b_sz > bp->bp_defsz) {
- buf->b_flags |= MDNS_BUF_TEMP;
- }
+ TAILQ_REMOVE(&bp->bp_list, buf, b_next);
+ bp->bp_buffree[0]--;
+ }
+ }
+
+ if (buf == NULL) {
+ buf = malloc(sizeof(struct mdns_buf));
+ if (buf == NULL)
+ goto out;
+
+ buf->b_flags = 0;
+ buf->b_sz = (len == 0) ?
+ ((flags & MDNS_BP_HUGE) ? MDNS_PKG_MAX_LEN : bp->bp_defsz) : len;
+ buf->b_buf = malloc(buf->b_sz);
+ if (buf->b_buf == NULL) {
+ free(buf);
+ buf = NULL;
+ goto out;
+ }
+
+ if (flags & MDNS_BP_HUGE) {
+ buf->b_flags |= MDNS_BUF_HUGE;
+ bp->bp_buftotal[1]++;
+ }
+ else if (buf->b_sz > bp->bp_defsz) {
+ buf->b_flags |= MDNS_BUF_TEMP;
+ bp->bp_buftotal[0]++;
}
else {
- buf = malloc(sizeof(struct mdns_buf));
- buf->b_flags = 0;
- if (len > 0) {
- buf->b_sz = len;
- buf->b_flags |= MDNS_BUF_TEMP;
- }
- else {
- buf->b_sz = bp->bp_defsz;
- }
- buf->b_buf = malloc(buf->b_sz);
- bp->bp_buftotal++;
+ bp->bp_buftotal[0]++;
}
- } while (buf == NULL);
+ }
+ MDNS_INIT_SET(buf, b_magic);
+ bp->bp_allocs++;
buf->b_len = 0;
if (bh->bh_size == 0)
TAILQ_INIT(&bh->bh_list);
- TAILQ_INSERT_TAIL(&bh->bh_list, buf, b_list);
+ TAILQ_INSERT_TAIL(&bh->bh_list, buf, b_next);
bh->bh_size++;
- MDNS_INIT_SET(buf, b_magic);
+
+ dprintf(DEBUG_BUF, "Blocks in use %d, free normal=%d, huge=%d",
+ bp->bp_allocs, bp->bp_buffree[0], bp->bp_buffree[1]);
- if (!locked)
+out:
+ if (!(flags & MDNS_BP_LOCKED))
MDNS_BP_UNLOCK(bp);
return (buf);
}
@@ -171,31 +203,53 @@
if (!locked)
MDNS_BP_LOCK(bp);
- MDNS_INIT_UNSET(buf, b_magic);
- TAILQ_REMOVE(&bh->bh_list, buf, b_list);
+ TAILQ_REMOVE(&bh->bh_list, buf, b_next);
bh->bh_size--;
- if (!(buf->b_flags & MDNS_BUF_TEMP)) {
- TAILQ_INSERT_TAIL(&bp->bp_list, buf, b_list);
- bp->bp_buffree++;
+ bp->bp_allocs--;
+
+ dprintf(DEBUG_BUF, "Free buf=%x, size=%d, flags=%x", buf,
+ buf->b_sz, buf->b_flags);
+ if (!(buf->b_flags & MDNS_BUF_TEMP) || buf->b_sz >= bp->bp_defsz) {
+ if (buf->b_flags & MDNS_BUF_HUGE) {
+ TAILQ_INSERT_TAIL(&bp->bp_huge, buf, b_next);
+ bp->bp_buffree[1]++;
+ }
+ else {
+ TAILQ_INSERT_TAIL(&bp->bp_list, buf, b_next);
+ bp->bp_buffree[0]++;
+ }
}
else {
+ MDNS_INIT_UNSET(buf, b_magic);
free(buf->b_buf);
free(buf);
goto out;
}
- i = (bp->bp_buffree - 1) / 4;
- for (; i > 0; i--) {
+ i = ((bp->bp_allocs + 1) * 1.5);
+ while (bp->bp_buffree[0] > i) {
+ bp->bp_buffree[0]--;
buf = TAILQ_FIRST(&bp->bp_list);
if (buf == NULL)
break;
- TAILQ_REMOVE(&bp->bp_list, buf, b_list);
+ TAILQ_REMOVE(&bp->bp_list, buf, b_next);
+ free(buf->b_buf);
+ free(buf);
+ }
+
+ while (bp->bp_buffree[1] > i) {
+ bp->bp_buffree[1]--;
+ buf = TAILQ_FIRST(&bp->bp_huge);
+ if (buf == NULL)
+ break;
+ TAILQ_REMOVE(&bp->bp_huge, buf, b_next);
free(buf->b_buf);
free(buf);
- bp->bp_buftotal--;
- bp->bp_buffree--;
}
+ dprintf(DEBUG_BUF, "Blocks in use %d, free normal=%d, huge=%d",
+ bp->bp_allocs, bp->bp_buffree[0], bp->bp_buffree[1]);
+
out:
if (!locked)
MDNS_BP_UNLOCK(bp);
@@ -241,7 +295,7 @@
MDNS_BP_LOCK(bp);
len = 0;
- TAILQ_FOREACH(buf, &bh->bh_list, b_list) {
+ TAILQ_FOREACH(buf, &bh->bh_list, b_next) {
len += MDNS_BUFLEN(buf);
}
@@ -252,7 +306,7 @@
while (!TAILQ_EMPTY(&bh->bh_list)) {
buf2 = TAILQ_FIRST(&bh->bh_list);
- TAILQ_REMOVE(&bh->bh_list, buf2, b_list);
+ TAILQ_REMOVE(&bh->bh_list, buf2, b_next);
memcpy(MDNS_BUFPOS(buf), MDNS_BUF(buf2), MDNS_BUFLEN(buf2));
MDNS_BUFLEN(buf) += MDNS_BUFLEN(buf2);
mdns_buf_free(bp, bh, buf2, 1);
==== //depot/projects/soc2007/fli-mdns_sd/mdnsd/stack_buf.h#5 (text+ko) ====
@@ -37,12 +37,13 @@
*/
struct mdns_buf {
MAGIC(b_magic);
- TAILQ_ENTRY(mdns_buf) b_list;
+ TAILQ_ENTRY(mdns_buf) b_next;
char *b_buf; /* raw byte buffer */
size_t b_sz; /* buffer size */
size_t b_len;
int b_flags;
#define MDNS_BUF_TEMP 0x01 /* do not put back on free list after use */
+#define MDNS_BUF_HUGE 0x02
};
/*
@@ -72,15 +73,20 @@
struct mdns_bufpool {
MAGIC(bp_magic);
TAILQ_HEAD(, mdns_buf) bp_list; /* free list */
+ TAILQ_HEAD(, mdns_buf) bp_huge; /* huge free list */
size_t bp_defsz; /* default segment size */
int bp_lim; /* max number of segments */
bp_lock bp_lock; /* external lock function */
bp_unlock bp_unlock; /* external lock function */
void *bp_lockarg; /* lock argument */
- int bp_buffree;
- int bp_buftotal;
+ int bp_buffree[2];
+ int bp_buftotal[2];
+ uint32_t bp_allocs;
};
+#define MDNS_BP_LOCKED 0x01
+#define MDNS_BP_HUGE 0x02
+
/*
* Buffer consumer
*/
More information about the p4-projects
mailing list