[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r10149: Periodically clean the freelist of buffer memory chunks. (in tor/trunk: . src/or)



Author: nickm
Date: 2007-05-09 17:43:41 -0400 (Wed, 09 May 2007)
New Revision: 10149

Modified:
   tor/trunk/
   tor/trunk/ChangeLog
   tor/trunk/src/or/buffers.c
   tor/trunk/src/or/main.c
   tor/trunk/src/or/or.h
Log:
 r12704@catbus:  nickm | 2007-05-09 17:43:34 -0400
 Periodically clean the freelist of buffer memory chunks.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r12704] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2007-05-09 21:22:56 UTC (rev 10148)
+++ tor/trunk/ChangeLog	2007-05-09 21:43:41 UTC (rev 10149)
@@ -40,6 +40,8 @@
       the server), and avoids the nasty Windows socketpair() workaround.
     - Keep unused 4k buffers on a free list, rather than wasting 8k for every
       single inactive connection_t.
+    - Free items from the 4k-buffer free list when they haven't been used
+      for a while.
 
   o Minor features (build):
     - Make autoconf search for libevent, openssl, and zlib consistently.

Modified: tor/trunk/src/or/buffers.c
===================================================================
--- tor/trunk/src/or/buffers.c	2007-05-09 21:22:56 UTC (rev 10148)
+++ tor/trunk/src/or/buffers.c	2007-05-09 21:43:41 UTC (rev 10149)
@@ -161,7 +161,7 @@
 /** DOCDOC */
 static char *free_mem_list = NULL;
 static int free_mem_list_len = 0;
-/*XXXX020 Actually remove stuff from freelist when it gets too big */
+static int free_mem_list_lowwater = 0;
 
 /** DOCDOC */
 static void
@@ -194,7 +194,8 @@
   if (free_mem_list) {
     mem = free_mem_list;
     free_mem_list = *(char**)mem;
-    --free_mem_list_len;
+    if (--free_mem_list_len < free_mem_list_lowwater)
+      free_mem_list_lowwater = free_mem_list_len;
     log_info(LD_GENERAL, "Got buf mem from freelist. Freelist has %d entries.",
              free_mem_list_len);
   } else {
@@ -209,6 +210,30 @@
   buf->cur = buf->mem;
 }
 
+/** DOCDOC 64k of 4k buffers. */
+#define BUF_FREELIST_SLACK 16
+
+/** DOCDOC */
+void
+buf_shrink_freelist(void)
+{
+  if (free_mem_list_lowwater > BUF_FREELIST_SLACK) {
+    int i;
+    log_info(LD_GENERAL, "We haven't used %d/%d allocated buffer memory "
+             "chunks since the last call(); freeing all but %d of them",
+             free_mem_list_lowwater, free_mem_list_len,
+             BUF_FREELIST_SLACK);
+    for (i = BUF_FREELIST_SLACK; i < free_mem_list_lowwater; ++i) {
+      char *mem = free_mem_list;
+      tor_assert(mem);
+      free_mem_list = *(char**)mem;
+      tor_free(mem);
+      --free_mem_list_len;
+    }
+  }
+  free_mem_list_lowwater = free_mem_list_len;
+}
+
 /** Change a buffer's capacity. <b>new_capacity</b> must be \>=
  * buf->datalen. */
 static void

Modified: tor/trunk/src/or/main.c
===================================================================
--- tor/trunk/src/or/main.c	2007-05-09 21:22:56 UTC (rev 10148)
+++ tor/trunk/src/or/main.c	2007-05-09 21:43:41 UTC (rev 10149)
@@ -1021,6 +1021,7 @@
         buf_shrink(conn->inbuf);
     }
     clean_cell_pool();
+    buf_shrink_freelist();
     time_to_shrink_memory = now + MEM_SHRINK_INTERVAL;
   }
 

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-05-09 21:22:56 UTC (rev 10148)
+++ tor/trunk/src/or/or.h	2007-05-09 21:43:41 UTC (rev 10149)
@@ -2020,6 +2020,7 @@
 void buf_free(buf_t *buf);
 void buf_clear(buf_t *buf);
 void buf_shrink(buf_t *buf);
+void buf_shrink_freelist(void);
 
 size_t buf_datalen(const buf_t *buf);
 size_t buf_capacity(const buf_t *buf);