From c1dd008b1dd5a5020763476306f365dc18f7b4b1 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Mon, 12 Sep 2011 16:13:19 -0500
Subject: [PATCH] change -hp to -rp, add -lp and -np, no priority flags means
 auto which will do -rp if you have > 1 cpu

---
 src/include/private/switch_core_pvt.h |  1 +
 src/include/switch_core.h             |  5 ++-
 src/switch.c                          | 38 +++++++++++++++-----
 src/switch_core.c                     | 50 ++++++++-------------------
 4 files changed, 48 insertions(+), 46 deletions(-)

diff --git a/src/include/private/switch_core_pvt.h b/src/include/private/switch_core_pvt.h
index af82f1e88d..31da5a9bf1 100644
--- a/src/include/private/switch_core_pvt.h
+++ b/src/include/private/switch_core_pvt.h
@@ -255,6 +255,7 @@ struct switch_runtime {
 	uint32_t db_handle_timeout;
 	int curl_count;
 	int ssl_count;
+	int cpu_count;
 };
 
 extern struct switch_runtime runtime;
diff --git a/src/include/switch_core.h b/src/include/switch_core.h
index 562262c82d..da6a2e6ada 100644
--- a/src/include/switch_core.h
+++ b/src/include/switch_core.h
@@ -1978,8 +1978,11 @@ SWITCH_DECLARE(switch_status_t) switch_core_management_exec(char *relative_oid,
   \brief Set the maximum priority the process can obtain
   \return 0 on success
 */
-SWITCH_DECLARE(int32_t) set_high_priority(void);
+
 SWITCH_DECLARE(int32_t) set_normal_priority(void);
+SWITCH_DECLARE(int32_t) set_auto_priority(void);
+SWITCH_DECLARE(int32_t) set_realtime_priority(void);
+SWITCH_DECLARE(int32_t) set_low_priority(void);
 
 /*! 
   \brief Change user and/or group of the running process
diff --git a/src/switch.c b/src/switch.c
index 4ef38111f2..4d4208ebdb 100644
--- a/src/switch.c
+++ b/src/switch.c
@@ -358,7 +358,7 @@ int main(int argc, char *argv[])
 	char *usageDesc;
 	int alt_dirs = 0, log_set = 0, run_set = 0, do_kill = 0;
 	int known_opt;
-	int high_prio = 0;
+	int priority = 0;
 #ifndef WIN32
 	int do_wait = 0;
 #endif
@@ -408,7 +408,9 @@ int main(int argc, char *argv[])
 		"\t-waste                 -- allow memory waste\n" 
 		"\t-core                  -- dump cores\n"
 #endif
-		"\t-hp                    -- enable high priority settings\n"
+		"\t-rp                    -- enable high(realtime) priority settings\n"
+		"\t-lp                    -- enable low priority settings\n"
+		"\t-np                    -- enable normal priority settings (system defaults)\n"
 		"\t-vg                    -- run under valgrind\n"
 		"\t-nosql                 -- disable internal sql scoreboard\n"
 		"\t-heavy-timer           -- Heavy Timer, possibly more accurate but at a cost\n"
@@ -567,8 +569,18 @@ int main(int argc, char *argv[])
 		}
 #endif
 
-		if (local_argv[x] && !strcmp(local_argv[x], "-hp")) {
-			high_prio++;
+		if (local_argv[x] && (!strcmp(local_argv[x], "-hp") || !strcmp(local_argv[x], "-rp"))) {
+			priority = 2;
+			known_opt++;
+		}
+
+		if (local_argv[x] && !strcmp(local_argv[x], "-lp")) {
+			priority = -1;
+			known_opt++;
+		}
+
+		if (local_argv[x] && !strcmp(local_argv[x], "-np")) {
+			priority = 1;
 			known_opt++;
 		}
 
@@ -828,13 +840,21 @@ int main(int argc, char *argv[])
 	}
 
 
-
-	if (high_prio) {
-		set_high_priority();
-	} else {
+	switch(priority) {
+	case 2:
+		set_realtime_priority();
+		break;
+	case 1:
 		set_normal_priority();
+		break;
+	case -1:
+		set_low_priority();
+		break;
+	default:
+		set_auto_priority();
+		break;
 	}
-
+	
 	switch_core_setrlimits();
 
 
diff --git a/src/switch_core.c b/src/switch_core.c
index ba0350a7fe..4edf0dc576 100644
--- a/src/switch_core.c
+++ b/src/switch_core.c
@@ -648,8 +648,10 @@ SWITCH_DECLARE(void) switch_core_set_globals(void)
 }
 
 
-static int32_t set_low_priority(void)
+SWITCH_DECLARE(int32_t) set_low_priority(void)
 {
+
+
 #ifdef WIN32
 	SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
 #else
@@ -679,13 +681,14 @@ static int32_t set_low_priority(void)
 	}
 #endif
 #endif
+
 	return 0;
 }
 
-static int32_t set_priority(void)
+SWITCH_DECLARE(int32_t) set_realtime_priority(void)
 {
 #ifdef WIN32
-	SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
+	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
 #else
 #ifdef USE_SCHED_SETSCHEDULER
 	/*
@@ -720,47 +723,20 @@ static int32_t set_priority(void)
 	return 0;
 }
 
-
 SWITCH_DECLARE(int32_t) set_normal_priority(void)
 {
-	return set_priority();
+	return 0;
 }
 
-SWITCH_DECLARE(int32_t) set_high_priority(void)
+SWITCH_DECLARE(int32_t) set_auto_priority(void)
 {
-#ifdef WIN32
-	SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
-#else
-	int pri;
+	runtime.cpu_count = sysconf (_SC_NPROCESSORS_ONLN);
 
-#ifdef USE_SETRLIMIT
-	struct rlimit lim = { RLIM_INFINITY, RLIM_INFINITY };
-#endif
-	
-	if ((pri = set_priority())) {
-		return pri;
+	/* If we have more than 1 cpu, we should use realtime priority so we can have priority threads */
+	if (runtime.cpu_count > 1) {
+		return set_realtime_priority();
 	}
 
-#ifdef USE_SETRLIMIT
-	/*
-	 * The amount of memory which can be mlocked is limited for non-root users.
-	 * FS will segfault (= hitting the limit) soon after mlockall has been called
-	 * and we've switched to a different user.
-	 * So let's try to remove the mlock limit here...
-	 */
-	if (setrlimit(RLIMIT_MEMLOCK, &lim) < 0) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failed to disable memlock limit, application may crash if run as non-root user!\n");
-	}
-#endif
-
-#ifdef USE_MLOCKALL
-	/*
-	 * Pin memory pages to RAM to prevent being swapped to disk
-	 */
-	mlockall(MCL_CURRENT | MCL_FUTURE);
-#endif
-
-#endif
 	return 0;
 }
 
@@ -1405,6 +1381,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc
 	runtime.min_dtmf_duration = SWITCH_MIN_DTMF_DURATION;
 	runtime.odbc_dbtype = DBTYPE_DEFAULT;
 	runtime.dbname = NULL;
+	runtime.cpu_count = sysconf (_SC_NPROCESSORS_ONLN);
+	
 
 	/* INIT APR and Create the pool context */
 	if (apr_initialize() != SWITCH_STATUS_SUCCESS) {