From 6d01ad5e38ebc536ad3f7166a024db48b71123ef Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Wed, 3 Oct 2012 14:10:22 -0500
Subject: [PATCH 1/4] some more priority tweaks

---
 src/mod/endpoints/mod_sofia/sofia.c | 4 ++--
 src/switch_core.c                   | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c
index a74a490da8..26ff378ee4 100644
--- a/src/mod/endpoints/mod_sofia/sofia.c
+++ b/src/mod/endpoints/mod_sofia/sofia.c
@@ -1650,7 +1650,7 @@ void sofia_msg_thread_start(int idx)
 
 				switch_threadattr_create(&thd_attr, mod_sofia_globals.pool);
 				switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
-				switch_threadattr_priority_set(thd_attr, SWITCH_PRI_REALTIME);
+				//switch_threadattr_priority_set(thd_attr, SWITCH_PRI_REALTIME);
 				switch_thread_create(&mod_sofia_globals.msg_queue_thread[i], 
 									 thd_attr, 
 									 sofia_msg_thread_run, 
@@ -2376,7 +2376,7 @@ switch_thread_t *launch_sofia_worker_thread(sofia_profile_t *profile)
 
 	switch_threadattr_create(&thd_attr, profile->pool);
 	switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
-	switch_threadattr_priority_set(thd_attr, SWITCH_PRI_REALTIME);
+	//switch_threadattr_priority_set(thd_attr, SWITCH_PRI_REALTIME);
 	switch_thread_create(&thread, thd_attr, sofia_profile_worker_thread_run, profile, profile->pool);
 
 	while (!sofia_test_pflag(profile, PFLAG_WORKER_RUNNING)) {
diff --git a/src/switch_core.c b/src/switch_core.c
index 4912f0abd8..4e47519757 100644
--- a/src/switch_core.c
+++ b/src/switch_core.c
@@ -736,7 +736,7 @@ SWITCH_DECLARE(int32_t) set_realtime_priority(void)
 	char data[] = "-1\n";
 	struct sched_param sched = { 0 };
 	sched.sched_priority = SWITCH_PRI_LOW;
-	if (sched_setscheduler(0, SCHED_RR, &sched)) {
+	if (sched_setscheduler(0, SCHED_FIFO, &sched)) {
 		sched.sched_priority = 0;
 		if (sched_setscheduler(0, SCHED_OTHER, &sched)) {
 			return -1;

From 61033f420bd0a4a11061b705a415b6007c96be99 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Wed, 3 Oct 2012 14:19:34 -0500
Subject: [PATCH 2/4] revert

---
 src/include/switch_core.h |  3 ---
 src/switch_console.c      |  4 +--
 src/switch_core_sqldb.c   | 53 ++-------------------------------------
 3 files changed, 4 insertions(+), 56 deletions(-)

diff --git a/src/include/switch_core.h b/src/include/switch_core.h
index efd983ded8..540de6e537 100644
--- a/src/include/switch_core.h
+++ b/src/include/switch_core.h
@@ -2303,9 +2303,6 @@ SWITCH_DECLARE(switch_status_t) _switch_core_db_handle(switch_cache_db_handle_t
 SWITCH_DECLARE(switch_status_t) _switch_core_recovery_db_handle(switch_cache_db_handle_t ** dbh, const char *file, const char *func, int line);
 #define switch_core_recovery_db_handle(_a) _switch_core_recovery_db_handle(_a, __FILE__, __SWITCH_FUNC__, __LINE__)
 
-SWITCH_DECLARE(switch_status_t) _switch_core_persist_db_handle(switch_cache_db_handle_t ** dbh, const char *file, const char *func, int line);
-#define switch_core_persist_db_handle(_a) _switch_core_persist_db_handle(_a, __FILE__, __SWITCH_FUNC__, __LINE__)
-
 SWITCH_DECLARE(switch_bool_t) switch_cache_db_test_reactive(switch_cache_db_handle_t *db,
 															const char *test_sql, const char *drop_sql, const char *reactive_sql);
 SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute(switch_cache_db_handle_t *dbh, const char *sql, uint32_t retries);
diff --git a/src/switch_console.c b/src/switch_console.c
index d461333860..059ff79d59 100644
--- a/src/switch_console.c
+++ b/src/switch_console.c
@@ -248,7 +248,7 @@ SWITCH_DECLARE(char *) switch_console_expand_alias(char *cmd, char *arg)
 		return NULL;
 	}
 
-	if (switch_core_persist_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
+	if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database Error\n");
 		return NULL;
 	}
@@ -1859,7 +1859,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string)
 			switch_cache_db_handle_t *db = NULL;
 			char *sql = NULL;
 
-			if (switch_core_persist_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
+			if (switch_core_db_handle(&db) != SWITCH_STATUS_SUCCESS) {
 				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Database Error\n");
 				free(mydata);
 				return SWITCH_STATUS_FALSE;
diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c
index 556e3abab2..b4a58bbd9d 100644
--- a/src/switch_core_sqldb.c
+++ b/src/switch_core_sqldb.c
@@ -176,7 +176,7 @@ static switch_cache_db_handle_t *get_handle(const char *db_str, const char *user
 }
 
 
-#define SWITCH_CORE_DB "file:scoreboard?mode=memory&cache=shared"
+#define SWITCH_CORE_DB "core"
 /*!
   \brief Open the default system database
 */
@@ -218,49 +218,6 @@ SWITCH_DECLARE(switch_status_t) _switch_core_db_handle(switch_cache_db_handle_t
 	return r;
 }
 
-#define SWITCH_CORE_PERSIST_DB "core"
-/*!
-  \brief Open the default system database
-*/
-SWITCH_DECLARE(switch_status_t) _switch_core_persist_db_handle(switch_cache_db_handle_t **dbh, const char *file, const char *func, int line)
-{
-	switch_cache_db_connection_options_t options = { {0} };
-	switch_status_t r;
-	
-	if (!sql_manager.manage) {
-		return SWITCH_STATUS_FALSE;
-	}
-
-	if (zstr(runtime.odbc_dsn)) {
-		if (switch_test_flag((&runtime), SCF_CORE_ODBC_REQ)) {
-			return SWITCH_STATUS_FALSE;
-		}
-
-		if (runtime.dbname) {
-			options.core_db_options.db_path = runtime.dbname;
-		} else {
-			options.core_db_options.db_path = SWITCH_CORE_PERSIST_DB;
-		}
-		r = _switch_cache_db_get_db_handle(dbh, SCDB_TYPE_CORE_DB, &options, file, func, line);
-		
-	} else {
-		options.odbc_options.dsn = runtime.odbc_dsn;
-		options.odbc_options.user = runtime.odbc_user;
-		options.odbc_options.pass = runtime.odbc_pass;
-
-		r = _switch_cache_db_get_db_handle(dbh, SCDB_TYPE_ODBC, &options, file, func, line);
-	}
-
-	/* I *think* we can do without this now, if not let me know 
-	   if (r == SWITCH_STATUS_SUCCESS && !(*dbh)->io_mutex) {
-	   (*dbh)->io_mutex = sql_manager.io_mutex;
-	   }
-	*/
-
-	return r;
-}
-
-
 #define SWITCH_CORE_RECOVERY_DB "core_recovery"
 SWITCH_DECLARE(switch_status_t) _switch_core_recovery_db_handle(switch_cache_db_handle_t **dbh, const char *file, const char *func, int line)
 {
@@ -2409,13 +2366,7 @@ switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_
 		break;
 	}
 
-	{
-		switch_cache_db_handle_t *db = NULL;
-		switch_core_persist_db_handle(&db);
-		switch_cache_db_test_reactive(db, "select hostname from aliases", "DROP TABLE aliases", create_alias_sql);
-		switch_cache_db_release_db_handle(&db);
-	}
-
+	switch_cache_db_test_reactive(sql_manager.dbh, "select hostname from aliases", "DROP TABLE aliases", create_alias_sql);
 	switch_cache_db_test_reactive(sql_manager.dbh, "select hostname from complete", "DROP TABLE complete", create_complete_sql);
 	switch_cache_db_test_reactive(sql_manager.dbh, "select hostname from nat", "DROP TABLE nat", create_nat_sql);
 	switch_cache_db_test_reactive(sql_manager.dbh, "delete from registrations where reg_user='' or network_proto='tcp' or network_proto='tls'", 

From 7afd4cae6708fb68accb663c56a639daef8d8178 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Thu, 4 Oct 2012 08:02:32 -0500
Subject: [PATCH 3/4] FS-4677

---
 src/mod/endpoints/mod_sofia/sofia_presence.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c
index d7d7a49cf7..eea01d50a5 100644
--- a/src/mod/endpoints/mod_sofia/sofia_presence.c
+++ b/src/mod/endpoints/mod_sofia/sofia_presence.c
@@ -934,7 +934,7 @@ static void send_conference_data(sofia_profile_t *profile, switch_event_t *event
 		sql = switch_mprintf("select full_to, full_from, contact %q ';_;isfocus', expires, call_id, event, network_ip, network_port, "
 							 "'%q' as ct,'%q' as pt "
 							 " from sip_subscriptions where "
-							 "hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'"
+							 "hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q' "
 							 "and call_id = '%q' ", 
 							 switch_sql_concat(),
 							 type,
@@ -956,10 +956,19 @@ static void send_conference_data(sofia_profile_t *profile, switch_event_t *event
 	sofia_glue_execute_sql_callback(profile, profile->ireg_mutex, sql, sofia_presence_send_sql, &cb);
 
 	if (switch_true(final)) {
-		sql = switch_mprintf("delete from sip_subscriptions where "
-							 "hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'",
-							 mod_sofia_globals.hostname, profile->name,
-							 from_user, from_host, event_str);
+		if (call_id) {
+                        sql = switch_mprintf("delete from sip_subscriptions where "
+                                                                 "hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q' "
+								 "and call_id = '%q' ",
+                                                                 mod_sofia_globals.hostname, profile->name,
+                                                                 from_user, from_host, event_str, call_id);
+
+		} else {
+			sql = switch_mprintf("delete from sip_subscriptions where "
+								 "hostname='%q' and profile_name='%q' and sub_to_user='%q' and sub_to_host='%q' and event='%q'",
+								 mod_sofia_globals.hostname, profile->name,
+								 from_user, from_host, event_str);
+		}
 
 		sofia_glue_execute_sql_now(profile, &sql, SWITCH_TRUE);
 	}

From 3d57cbda984ebad46fa6c1a94dfd8109b130a2b9 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Thu, 4 Oct 2012 08:29:26 -0500
Subject: [PATCH 4/4] pull this code echo -1 >
 /proc/sys/kernel/sched_rt_runtime_us yourself if you want to enable this

---
 src/switch_core.c | 18 ------------------
 1 file changed, 18 deletions(-)

diff --git a/src/switch_core.c b/src/switch_core.c
index 4e47519757..95a7b4dbed 100644
--- a/src/switch_core.c
+++ b/src/switch_core.c
@@ -731,9 +731,6 @@ SWITCH_DECLARE(int32_t) set_realtime_priority(void)
 	 * Try to use a round-robin scheduler
 	 * with a fallback if that does not work
 	 */
-	int fd;
-	const char *rt = "/proc/sys/kernel/sched_rt_runtime_us";
-	char data[] = "-1\n";
 	struct sched_param sched = { 0 };
 	sched.sched_priority = SWITCH_PRI_LOW;
 	if (sched_setscheduler(0, SCHED_FIFO, &sched)) {
@@ -742,21 +739,6 @@ SWITCH_DECLARE(int32_t) set_realtime_priority(void)
 			return -1;
 		}
 	}
-
-	if ((fd = open(rt, O_WRONLY)) > 0) {
-		int r;
-		
-		if (!(r = write(fd, data, sizeof(data)))) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Disablling RT limits [%s][%d]\n", rt, r);
-		} else {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Disabling RT throttling.\n");
-		}
-		close(fd);
-	} else {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error opening %s\n", rt);
-	}
-
-	
 #endif