Hi This drops Monkey's RAM use from 56.6mb to 4.2mb.
On a 64-bit system, I measured the max stack use at 132kb. Setting it to 512kb leaves plenty of headroom, and 32-bit systems will use even less. Linux defaults to 8mb stacks (one per thread), and we can quite comfortably drop 7.5mb off each of them. Note that this RAM is not measured by valgrind (it measures heap only), you can count it with "pmap -d PID". - Lauri
>From 89a1e7c210bf15625fa3144eaaf022cb23746929 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen <[email protected]> Date: Sun, 6 May 2012 16:39:17 +0300 Subject: [PATCH] monkey: Limit stack size to 512kb This drops Monkey's RAM use from 56.6mb to 4.2mb. On a 64-bit system, I measured the max stack use at 132kb. Setting it to 512kb leaves plenty of headroom, and 32-bit systems will use even less. Signed-off-by: Lauri Kasanen <[email protected]> --- src/include/monkey.h | 3 +++ src/mk_scheduler.c | 1 + src/mk_utils.c | 1 + src/monkey.c | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 0 deletions(-) diff --git a/src/include/monkey.h b/src/include/monkey.h index 25f56bf..d776616 100644 --- a/src/include/monkey.h +++ b/src/include/monkey.h @@ -27,6 +27,9 @@ /* Max Path lenth */ #define MAX_PATH 1024 +/* Stack size, bytes */ +#define MK_STACKSIZE 512*1024 + /* Send_Header(...,int cgi) */ #define SH_NOCGI 0 #define SH_CGI 1 diff --git a/src/mk_scheduler.c b/src/mk_scheduler.c index 48e0f5e..508acb6 100644 --- a/src/mk_scheduler.c +++ b/src/mk_scheduler.c @@ -277,6 +277,7 @@ int mk_sched_launch_thread(int max_events) pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstacksize(&attr, MK_STACKSIZE); if (pthread_create(&tid, &attr, mk_sched_launch_worker_loop, (void *) thconf) != 0) { perror("pthread_create"); diff --git a/src/mk_utils.c b/src/mk_utils.c index d62b06b..39fbb85 100644 --- a/src/mk_utils.c +++ b/src/mk_utils.c @@ -537,6 +537,7 @@ pthread_t mk_utils_worker_spawn(void (*func) (void *)) pthread_attr_init(&thread_attr); pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstacksize(&thread_attr, MK_STACKSIZE); if (pthread_create(&tid, &thread_attr, (void *) func, NULL) < 0) { perror("pthread_create"); exit(EXIT_FAILURE); diff --git a/src/monkey.c b/src/monkey.c index 630d23d..e2ebb39 100644 --- a/src/monkey.c +++ b/src/monkey.c @@ -24,6 +24,7 @@ #include <unistd.h> #include <getopt.h> #include <pthread.h> +#include <sys/resource.h> #include "monkey.h" #include "mk_socket.h" @@ -57,6 +58,22 @@ static void mk_thread_keys_init(void) pthread_key_create(&mk_plugin_event_k, NULL); } +static void mk_set_stacksize(void) +{ + struct rlimit limit; + getrlimit(RLIMIT_STACK, &limit); + +// printf("Stack size of %lld kb\n", (long long) limit.rlim_cur/1024); + + if (limit.rlim_cur > MK_STACKSIZE) { + limit.rlim_cur = MK_STACKSIZE; + limit.rlim_max = MK_STACKSIZE; + + setrlimit(RLIMIT_STACK, &limit); +// printf("Set stack size to %d kb\n", MK_STACKSIZE/1024); + } +} + static void mk_details(void) { printf("* Process ID is %i", getpid()); @@ -127,6 +144,8 @@ int main(int argc, char **argv) } } + mk_set_stacksize(); + /* setup basic configurations */ config = mk_mem_malloc_z(sizeof(struct server_config)); -- 1.7.2.1
_______________________________________________ Monkey mailing list [email protected] http://lists.monkey-project.com/listinfo/monkey
