From e599375a82828fed0ae4f6be379f1c936b5a0293 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Sat, 7 Mar 2009 02:20:29 +0000 Subject: [PATCH] performance tuning git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12495 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/switch.c | 38 +++++++++++++++++++++++++++++++++++++- src/switch_core.c | 13 ++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/switch.c b/src/switch.c index ea4b66c646..dcabe92ef3 100644 --- a/src/switch.c +++ b/src/switch.c @@ -37,6 +37,12 @@ #define _XOPEN_SOURCE 600 #endif +#ifndef WIN32 +#ifdef HAVE_SETRLIMIT +#include +#endif +#endif + #include #include "private/switch_core_pvt.h" @@ -274,6 +280,10 @@ int main(int argc, char *argv[]) switch_status_t destroy_status; switch_file_t *fd; switch_memory_pool_t *pool = NULL; +#ifdef HAVE_SETRLIMIT + struct rlimit rlp; + int waste = 0; +#endif if (argv[0] && strstr(argv[0], "freeswitchd")) { nc++; @@ -291,6 +301,7 @@ int main(int argc, char *argv[]) #endif "\t-help -- this message\n" #ifdef HAVE_SETRLIMIT + "\t-waste -- allow memory waste\n" "\t-core -- dump cores\n" #endif "\t-hp -- enable high priority settings\n" @@ -374,6 +385,7 @@ int main(int argc, char *argv[]) exit(0); } } + if (argv[x] && !strcmp(argv[x], "-uninstall")) { x++; if (argv[x] && strlen(argv[x])) { @@ -429,13 +441,17 @@ int main(int argc, char *argv[]) #endif #ifdef HAVE_SETRLIMIT if (argv[x] && !strcmp(argv[x], "-core")) { - struct rlimit rlp; memset(&rlp, 0, sizeof(rlp)); rlp.rlim_cur = RLIM_INFINITY; rlp.rlim_max = RLIM_INFINITY; setrlimit(RLIMIT_CORE, &rlp); known_opt++; } + + if (argv[x] && !strcmp(argv[x], "-waste")) { + waste++; + known_opt++; + } #endif if (argv[x] && !strcmp(argv[x], "-hp")) { @@ -600,12 +616,32 @@ int main(int argc, char *argv[]) #endif } +#ifdef HAVE_SETRLIMIT + if (!waste) { + memset(&rlp, 0, sizeof(rlp)); + getrlimit(RLIMIT_STACK, &rlp); + if (rlp.rlim_max > SWITCH_THREAD_STACKSIZE) { + memset(&rlp, 0, sizeof(rlp)); + rlp.rlim_cur = SWITCH_THREAD_STACKSIZE; + rlp.rlim_max = SWITCH_THREAD_STACKSIZE; + setrlimit(RLIMIT_STACK, &rlp); + fprintf(stderr, "Error: stacksize %d is too large: run ulimit -s %d or run %s -waste.\nauto-adjusting stack size for optimal performance....\n", + SWITCH_THREAD_STACKSIZE / 1024, SWITCH_THREAD_STACKSIZE / 1024, argv[0]); + return (int)execv(argv[0], argv); + } + } +#endif + + + + if (high_prio) { set_high_priority(); } switch_core_setrlimits(); + #ifndef WIN32 if (runas_user || runas_group) { if (change_user_group(runas_user, runas_group) < 0) { diff --git a/src/switch_core.c b/src/switch_core.c index aba12e9734..535a4c9668 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -792,7 +792,7 @@ SWITCH_DECLARE(void) switch_core_setrlimits(void) #ifndef __FreeBSD__ memset(&rlp, 0, sizeof(rlp)); rlp.rlim_cur = SWITCH_THREAD_STACKSIZE; - rlp.rlim_max = SWITCH_SYSTEM_THREAD_STACKSIZE; + rlp.rlim_max = SWITCH_THREAD_STACKSIZE; setrlimit(RLIMIT_STACK, &rlp); #endif @@ -808,6 +808,8 @@ SWITCH_DECLARE(void) switch_core_setrlimits(void) setrlimit(RLIMIT_CPU, &rlp); setrlimit(RLIMIT_DATA, &rlp); setrlimit(RLIMIT_FSIZE, &rlp); + setrlimit(RLIMIT_NPROC, &rlp); + setrlimit(RLIMIT_RTPRIO, &rlp); #if !defined(__OpenBSD__) && !defined(__NetBSD__) setrlimit(RLIMIT_AS, &rlp); #endif @@ -1632,6 +1634,15 @@ SWITCH_DECLARE(int) switch_system(const char *cmd, switch_bool_t wait) switch_threadattr_detach_set(thd_attr, 1); switch_thread_create(&thread, thd_attr, system_thread, sth, sth->pool); + +#if defined(HAVE_SETRLIMIT) && !defined(__FreeBSD__) + rlim.rlim_cur = SWITCH_THREAD_STACKSIZE; + rlim.rlim_max = SWITCH_THREAD_STACKSIZE; + if (setrlimit(RLIMIT_STACK, &rlim) < 0) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Setting stack size failed!\n"); + } +#endif + if (wait) { switch_thread_cond_wait(sth->cond, sth->mutex); ret = sth->ret;