--------------------- PatchSet 10339 Date: 2007/12/28 08:03:34 Author: adri Branch: s27_adri Tag: (none) Log: Modify stmem to actually use buf_t's as the underlying buffer mechanism. Now, this is a bit crap: * the pages are fixed at 4k, but I've tried to cope with the pages growing a little bit * the malloc overheads now are just stupendous because each new stmem mem_node now malloc's three times, instead of one. Aiee! * data is still being copied in! However, this work lets us do a few things, namely: * there's now no reason why stmem entries must be 4k. Well, besides the rest of the codebase doing data copies assuming a 4k buffer. This obviously must be fixed, and quickly! * next, the stmem mem_node's shouldn't be buf_t's; they should be buffer region references. This shouldn't be that difficult to implement now that I've implement a buf_region_t type. Then.. * Once thats done, a "stmemAppendRef" can be written to append a read-only reference to a buf_t to the list, hopefully eliminating most of the overheads involved in getting data into the store in the first place. (stmemAppend() has to stay there for things like the storeAppendPrintf() type functions.) Members: src/defines.h:1.43.8.3->1.43.8.4 src/stmem.c:1.10.2.4.4.4->1.10.2.4.4.5 src/store_swapout.c:1.22.24.2->1.22.24.3 src/structs.h:1.158.2.5.4.8->1.158.2.5.4.9 Index: squid/src/defines.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/defines.h,v retrieving revision 1.43.8.3 retrieving revision 1.43.8.4 diff -u -r1.43.8.3 -r1.43.8.4 --- squid/src/defines.h 28 Dec 2007 04:05:40 -0000 1.43.8.3 +++ squid/src/defines.h 28 Dec 2007 08:03:34 -0000 1.43.8.4 @@ -1,6 +1,6 @@ /* - * $Id: defines.h,v 1.43.8.3 2007/12/28 04:05:40 adri Exp $ + * $Id: defines.h,v 1.43.8.4 2007/12/28 08:03:34 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -356,8 +356,8 @@ #define LOGFILE_SEQNO(n) ( (n)->sequence_number ) -#define MEMREF_BUF(r) ( (r).node->data + (r).offset ) -#define MEMREF_LEN(r) ( (r).node->len - (r).offset ) -#define MEMREF_ISNULL(r) ( (r).node == NULL ) +#define MEMREF_BUF(r) ( buf_buf((r).node->buf) + (r).offset ) +#define MEMREF_LEN(r) ( buf_len((r).node->buf) - (r).offset ) +#define MEMREF_ISNULL(r) ( (r).node == NULL && (r).node->buf == NULL ) #endif /* SQUID_DEFINES_H */ Index: squid/src/stmem.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/stmem.c,v retrieving revision 1.10.2.4.4.4 retrieving revision 1.10.2.4.4.5 diff -u -r1.10.2.4.4.4 -r1.10.2.4.4.5 --- squid/src/stmem.c 27 Dec 2007 13:47:07 -0000 1.10.2.4.4.4 +++ squid/src/stmem.c 28 Dec 2007 08:03:34 -0000 1.10.2.4.4.5 @@ -1,6 +1,6 @@ /* - * $Id: stmem.c,v 1.10.2.4.4.4 2007/12/27 13:47:07 adri Exp $ + * $Id: stmem.c,v 1.10.2.4.4.5 2007/12/28 08:03:34 adri Exp $ * * DEBUG: section 19 Store Memory Primitives * AUTHOR: Harvest Derived @@ -39,7 +39,7 @@ stmemNodeFree(mem_node *p) { if (!p->uses) { - memFree(p->data, MEM_4K_BUF); + p->buf = buf_deref(p->buf); memFree(p, MEM_MEM_NODE); } else @@ -52,7 +52,7 @@ mem_node *p; while ((p = mem->head)) { mem->head = p->next; - store_mem_size -= SM_PAGE_SIZE; + store_mem_size -= buf_capacity(p->buf); stmemNodeFree(p); } mem->head = mem->tail = NULL; @@ -64,7 +64,7 @@ { squid_off_t current_offset = mem->origin_offset; mem_node *p = mem->head; - while (p && ((current_offset + p->len) <= target_offset)) { + while (p && ((current_offset + buf_len(p->buf)) <= target_offset)) { if (p == mem->tail) { /* keep the last one to avoid change to other part of code */ mem->head = mem->tail; @@ -73,8 +73,8 @@ } else { mem_node *lastp = p; p = p->next; - current_offset += lastp->len; - store_mem_size -= SM_PAGE_SIZE; + current_offset += buf_len(lastp->buf); + store_mem_size -= buf_capacity(lastp->buf); stmemNodeFree(lastp); } } @@ -99,25 +99,25 @@ /* Does the last block still contain empty space? * If so, fill out the block before dropping into the * allocation loop */ - if (mem->head && mem->tail && (mem->tail->len < SM_PAGE_SIZE)) { - avail_len = SM_PAGE_SIZE - (mem->tail->len); + if (mem->head && mem->tail && buf_len(mem->tail->buf) < SM_PAGE_SIZE) { + avail_len = SM_PAGE_SIZE - buf_len(mem->tail->buf); len_to_copy = XMIN(avail_len, len); - xmemcpy((mem->tail->data + mem->tail->len), data, len_to_copy); + /* Just in case the buffer suddenly "becomes bigger" for some reason! */ + store_mem_size -= buf_capacity(mem->tail->buf); + buf_append(mem->tail->buf, data, len_to_copy, BF_NONE); + store_mem_size += buf_capacity(mem->tail->buf); /* Adjust the ptr and len according to what was deposited in the page */ data += len_to_copy; len -= len_to_copy; - mem->tail->len += len_to_copy; } while (len > 0) { len_to_copy = XMIN(len, SM_PAGE_SIZE); p = memAllocate(MEM_MEM_NODE); /* This is a non-zero'ed buffer; make sure you fully initialise it */ - p->data = memAllocate(MEM_4K_BUF); + p->buf = buf_create_size(SM_PAGE_SIZE); p->next = NULL; - p->len = len_to_copy; - p->size = 4096; p->uses = 0; - store_mem_size += SM_PAGE_SIZE; - xmemcpy(p->data, data, len_to_copy); + buf_append(p->buf, data, len_to_copy, BF_NONE); + store_mem_size += buf_capacity(p->buf); if (!mem->head) { /* The chain is empty */ mem->head = mem->tail = p; @@ -157,8 +157,8 @@ if (p == NULL) return 0; /* Seek our way into store */ - while ((t_off + p->len) <= offset) { - t_off += p->len; + while ((t_off + buf_len(p->buf)) <= offset) { + t_off += buf_len(p->buf); if (!p->next) { debug(19, 1) ("memRef: p->next == NULL\n"); return 0; @@ -172,9 +172,9 @@ r->offset = offset - t_off; assert(r->offset >= 0); assert(r->offset >= 0); - assert(p->len + t_off - offset > 0); - debug(19, 3) ("memRef: returning node %p, offset %d, %d bytes\n", p, (int) r->offset, (int) (p->len + t_off - offset)); - return p->len + t_off - offset; + assert(buf_len(p->buf) + t_off - offset > 0); + debug(19, 3) ("memRef: returning node %p, offset %d, %d bytes\n", p, (int) r->offset, (int) (buf_len(p->buf) + t_off - offset)); + return buf_len(p->buf) + t_off - offset; } void @@ -184,7 +184,7 @@ r->node = memAllocate(MEM_MEM_NODE); r->node->uses = 0; r->node->next = NULL; - r->node->len = 4096; + r->node->buf = buf_create_size(SM_PAGE_SIZE); r->offset = 0; } @@ -210,44 +210,6 @@ ssize_t stmemCopy(const mem_hdr * mem, squid_off_t offset, char *buf, size_t size) { - mem_node *p = mem->head; - squid_off_t t_off = mem->origin_offset; - size_t bytes_to_go = size; - char *ptr_to_buf = NULL; - int bytes_from_this_packet = 0; - int bytes_into_this_packet = 0; - debug(19, 6) ("memCopy: offset %" PRINTF_OFF_T ": size %d\n", offset, (int) size); - if (p == NULL) - return 0; - assert(size > 0); - /* Seek our way into store */ - while ((t_off + p->len) < offset) { - t_off += p->len; - if (!p->next) { - debug(19, 1) ("memCopy: p->next == NULL\n"); - return 0; - } - assert(p->next); - p = p->next; - } - /* Start copying begining with this block until - * we're satiated */ - bytes_into_this_packet = offset - t_off; - bytes_from_this_packet = XMIN(bytes_to_go, p->len - bytes_into_this_packet); - xmemcpy(buf, p->data + bytes_into_this_packet, bytes_from_this_packet); - bytes_to_go -= bytes_from_this_packet; - ptr_to_buf = buf + bytes_from_this_packet; - p = p->next; - while (p && bytes_to_go > 0) { - if (bytes_to_go > p->len) { - xmemcpy(ptr_to_buf, p->data, p->len); - ptr_to_buf += p->len; - bytes_to_go -= p->len; - } else { - xmemcpy(ptr_to_buf, p->data, bytes_to_go); - bytes_to_go -= bytes_to_go; - } - p = p->next; - } - return size - bytes_to_go; + assert(1==0); + return -1; } Index: squid/src/store_swapout.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/store_swapout.c,v retrieving revision 1.22.24.2 retrieving revision 1.22.24.3 diff -u -r1.22.24.2 -r1.22.24.3 --- squid/src/store_swapout.c 27 Dec 2007 13:47:07 -0000 1.22.24.2 +++ squid/src/store_swapout.c 28 Dec 2007 08:03:34 -0000 1.22.24.3 @@ -1,6 +1,6 @@ /* - * $Id: store_swapout.c,v 1.22.24.2 2007/12/27 13:47:07 adri Exp $ + * $Id: store_swapout.c,v 1.22.24.3 2007/12/28 08:03:34 adri Exp $ * * DEBUG: section 20 Storage Manager Swapout Functions * AUTHOR: Duane Wessels @@ -277,7 +277,7 @@ * but we can look at this at a later date or whenever the code results * in bad swapouts, whichever happens first. :-) */ - swap_buf_len = mem->swapout.memnode->len; + swap_buf_len = buf_len(mem->swapout.memnode->buf); debug(20, 3) ("storeSwapOut: swap_buf_len = %d\n", (int) swap_buf_len); assert(swap_buf_len > 0); Index: squid/src/structs.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/structs.h,v retrieving revision 1.158.2.5.4.8 retrieving revision 1.158.2.5.4.9 diff -u -r1.158.2.5.4.8 -r1.158.2.5.4.9 --- squid/src/structs.h 27 Dec 2007 13:39:08 -0000 1.158.2.5.4.8 +++ squid/src/structs.h 28 Dec 2007 08:03:34 -0000 1.158.2.5.4.9 @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.158.2.5.4.8 2007/12/27 13:39:08 adri Exp $ + * $Id: structs.h,v 1.158.2.5.4.9 2007/12/28 08:03:34 adri Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -1627,9 +1627,8 @@ }; struct _mem_node { - char *data; - int len; - int size; + /* This is a bit pointless considering that buffers already have their own reference counting; worry about that later */ + buf_t *buf; int uses; mem_node *next; };