From 64b0cc8703122437fb46e890ea0c0b4fda1d77da Mon Sep 17 00:00:00 2001 From: Dmitry Kozlov Date: Wed, 26 Jan 2011 00:50:30 +0300 Subject: futher memory usage optimization --- accel-pppd/CMakeLists.txt | 14 +++++------ accel-pppd/radius/packet.c | 14 ++++++++--- accel-pppd/triton/mempool.c | 59 ++++++++++++++++++--------------------------- accel-pppd/triton/triton.c | 6 ++--- 4 files changed, 44 insertions(+), 49 deletions(-) (limited to 'accel-pppd') diff --git a/accel-pppd/CMakeLists.txt b/accel-pppd/CMakeLists.txt index 526ad89..8e0af47 100644 --- a/accel-pppd/CMakeLists.txt +++ b/accel-pppd/CMakeLists.txt @@ -31,6 +31,13 @@ ADD_DEFINITIONS(-DACCEL_PPP_VERSION="${ACCEL_PPP_VERSION}") INCLUDE_DIRECTORIES(include) +IF (MEMDEBUG) + ADD_DEFINITIONS(-DMEMDEBUG) + IF (VALGRIND) + ADD_DEFINITIONS(-DVALGRIND) + ENDIF (VALGRIND) +ENDIF (MEMDEBUG) + IF (NOT DEFINED RADIUS) SET(RADIUS TRUE) ENDIF (NOT DEFINED RADIUS) @@ -40,13 +47,6 @@ IF (RADIUS) ADD_SUBDIRECTORY(radius) ENDIF (RADIUS) -IF (MEMDEBUG) - ADD_DEFINITIONS(-DMEMDEBUG) - IF (VALGRIND) - ADD_DEFINITIONS(-DVALGRIND) - ENDIF (VALGRIND) -ENDIF (MEMDEBUG) - ADD_SUBDIRECTORY(triton) ADD_SUBDIRECTORY(ctrl) ADD_SUBDIRECTORY(auth) diff --git a/accel-pppd/radius/packet.c b/accel-pppd/radius/packet.c index e8d2af9..dc355d5 100644 --- a/accel-pppd/radius/packet.c +++ b/accel-pppd/radius/packet.c @@ -17,6 +17,7 @@ static mempool_t packet_pool; static mempool_t attr_pool; +static mempool_t buf_pool; struct rad_packet_t *rad_packet_alloc(int code) { @@ -51,9 +52,11 @@ int rad_packet_build(struct rad_packet_t *pack, uint8_t *RA) uint8_t *ptr; if (!pack->buf) { - ptr = mmap(NULL, REQ_LENGTH_MAX, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + //ptr = mmap(NULL, REQ_LENGTH_MAX, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + ptr = mempool_alloc(buf_pool); - if (ptr == MAP_FAILED) { + //if (ptr == MAP_FAILED) { + if (!ptr) { log_emerg("radius:packet: out of memory\n"); return -1; } @@ -116,7 +119,8 @@ int rad_packet_recv(int fd, struct rad_packet_t **p, struct sockaddr_in *addr) if (!pack) return 0; - ptr = mmap(NULL, REQ_LENGTH_MAX, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + //ptr = mmap(NULL, REQ_LENGTH_MAX, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + ptr = mempool_alloc(buf_pool); if (ptr == MAP_FAILED) { log_emerg("radius:packet: out of memory\n"); goto out_err; @@ -241,7 +245,8 @@ void rad_packet_free(struct rad_packet_t *pack) struct rad_attr_t *attr; if (pack->buf) - munmap(pack->buf, REQ_LENGTH_MAX); + mempool_free(pack->buf); + //munmap(pack->buf, REQ_LENGTH_MAX); while(!list_empty(&pack->attrs)) { attr = list_entry(pack->attrs.next, typeof(*attr), entry); @@ -644,4 +649,5 @@ static void __init init(void) { attr_pool = mempool_create(sizeof(struct rad_attr_t)); packet_pool = mempool_create(sizeof(struct rad_packet_t)); + buf_pool = mempool_create(REQ_LENGTH_MAX); } diff --git a/accel-pppd/triton/mempool.c b/accel-pppd/triton/mempool.c index e3950dc..eb33e42 100644 --- a/accel-pppd/triton/mempool.c +++ b/accel-pppd/triton/mempool.c @@ -20,6 +20,8 @@ #define MAGIC1 0x2233445566778899llu #define PAGE_ORDER 5 +static int conf_mempool_min = 128; + struct _mempool_t { struct list_head entry; @@ -31,6 +33,7 @@ struct _mempool_t spinlock_t lock; uint64_t magic; int mmap:1; + int objects; }; struct _item_t @@ -56,6 +59,7 @@ static void *mmap_ptr; static void *mmap_endptr; static int mmap_grow(void); +static void mempool_clean(void); mempool_t __export *mempool_create(int size) { @@ -69,7 +73,6 @@ mempool_t __export *mempool_create(int size) spinlock_init(&p->lock); p->size = size; p->magic = (uint64_t)random() * (uint64_t)random(); - p->mmap = 1; spin_lock(&pools_lock); list_add_tail(&p->entry, &pools); @@ -100,6 +103,7 @@ void __export *mempool_alloc(mempool_t *pool) list_del(&it->entry); spin_unlock(&p->lock); + --p->objects; __sync_sub_and_fetch(&triton_stat.mempool_available, size); it->magic1 = MAGIC1; @@ -160,6 +164,7 @@ void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line) it->fname = fname; it->line = line; + --p->objects; __sync_sub_and_fetch(&triton_stat.mempool_available, size); it->magic1 = MAGIC1; @@ -208,6 +213,7 @@ void __export mempool_free(void *ptr) struct _item_t *it = container_of(ptr, typeof(*it), ptr); struct _mempool_t *p = it->owner; uint32_t size = sizeof(*it) + it->owner->size + 8; + int need_free = 0; #ifdef MEMDEBUG if (it->magic1 != MAGIC1) { @@ -233,7 +239,11 @@ void __export mempool_free(void *ptr) list_del(&it->entry); #endif #ifndef MEMPOOL_DISABLE - list_add_tail(&it->entry,&it->owner->items); + if (p->objects < conf_mempool_min) { + ++p->objects; + list_add_tail(&it->entry,&it->owner->items); + } else + need_free = 1; #endif #ifdef VALGRIND time(&it->timestamp); @@ -242,41 +252,15 @@ void __export mempool_free(void *ptr) spin_unlock(&p->lock); #ifdef MEMPOOL_DISABLE - if (it->owner->mmap) - munmap(it, size); - else + _free(it); +#else + if (need_free) { _free(it); -#endif - - __sync_add_and_fetch(&triton_stat.mempool_available, size); -} - -void __export mempool_clean(mempool_t *pool) -{ - struct _mempool_t *p = (struct _mempool_t *)pool; - struct _item_t *it; - uint32_t size = sizeof(*it) + p->size + 8; - - spin_lock(&p->lock); - while (!list_empty(&p->items)) { - it = list_entry(p->items.next, typeof(*it), entry); -#ifdef VALGRIND - if (it->timestamp + DELAY < time(NULL)) { - VALGRIND_MAKE_MEM_DEFINED(&it->owner, size - sizeof(it->entry) - sizeof(it->timestamp)); -#endif - list_del(&it->entry); - if (p->mmap) - munmap(it, size); - else - _free(it); __sync_sub_and_fetch(&triton_stat.mempool_allocated, size); - __sync_sub_and_fetch(&triton_stat.mempool_available, size); -#ifdef VALGRIND - } else - break; + } else + __sync_add_and_fetch(&triton_stat.mempool_available, size); #endif - } - spin_unlock(&p->lock); + } #ifdef MEMDEBUG @@ -292,7 +276,7 @@ void __export mempool_show(mempool_t *pool) } #endif -void sigclean(int num) +static void mempool_clean(void) { struct _mempool_t *p; struct _item_t *it; @@ -326,6 +310,11 @@ void sigclean(int num) spin_unlock(&pools_lock); } +static void sigclean(int num) +{ + mempool_clean(); +} + static int mmap_grow(void) { int size = sysconf(_SC_PAGE_SIZE) * (1 << PAGE_ORDER); diff --git a/accel-pppd/triton/triton.c b/accel-pppd/triton/triton.c index 00dfcf6..9236a00 100644 --- a/accel-pppd/triton/triton.c +++ b/accel-pppd/triton/triton.c @@ -90,7 +90,7 @@ static void* triton_thread(struct _triton_thread_t *thread) while (1) { spin_lock(&threads_lock); - if (!list_empty(&ctx_queue) && !need_config_reload) { + if (!list_empty(&ctx_queue) && !need_config_reload && triton_stat.thread_active <= thread_count) { thread->ctx = list_entry(ctx_queue.next, typeof(*thread->ctx), entry2); log_debug2("thread: %p: dequeued ctx %p\n", thread, thread->ctx); list_del(&thread->ctx->entry2); @@ -252,7 +252,7 @@ int triton_queue_ctx(struct _triton_context_t *ctx) return 0; spin_lock(&threads_lock); - if (list_empty(&sleep_threads) || need_config_reload) { + if (list_empty(&sleep_threads) || need_config_reload || triton_stat.thread_active > thread_count) { if (ctx->priority) list_add(&ctx->entry2, &ctx_queue); else @@ -513,7 +513,7 @@ static void ru_update(struct triton_timer_t *t) int __export triton_init(const char *conf_file) { - ctx_pool = mempool_create2(sizeof(struct _triton_context_t)); + ctx_pool = mempool_create(sizeof(struct _triton_context_t)); call_pool = mempool_create(sizeof(struct _triton_ctx_call_t)); if (conf_load(conf_file)) -- cgit v1.2.3