[Asterisk-code-review] res pjsip: Make system and global memory cache friendly (asterisk[13])

George Joseph asteriskteam at digium.com
Wed Feb 3 13:54:03 CST 2016


George Joseph has uploaded a new change for review.

  https://gerrit.asterisk.org/2156

Change subject: res_pjsip: Make system and global memory_cache friendly
......................................................................

res_pjsip: Make system and global memory_cache friendly

Since we can't depend on what an admin names their system and global config
sections, we've had to use retrieve_by_fields to get them which isn't
memory_cache friendly.

We now print a notice if the sections aren't named "system" and "global"
respectively informing the admin that renaming them will allow caching and the
"get" functions now retrieve_by_id for the "system" and "global" names first
and only retrieve_by_fields if the exact names aren't found.  The pjsip.conf
sample has also been updated to note this.

Change-Id: I32beaf688449808251b1eb966bb2cfcfc1d08362
---
M configs/samples/pjsip.conf.sample
M res/res_pjsip/config_global.c
M res/res_pjsip/config_system.c
3 files changed, 97 insertions(+), 36 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/56/2156/1

diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample
index a91ece9..9384750 100644
--- a/configs/samples/pjsip.conf.sample
+++ b/configs/samples/pjsip.conf.sample
@@ -837,6 +837,8 @@
 ;==========================SYSTEM SECTION OPTIONS=========================
 ;[system]
 ;  SYNOPSIS: Options that apply to the SIP stack as well as other system-wide settings
+;  NOTE:  Although this section's name can be set to anything, naming it "system"
+;         will allow the object to be cached internally.
 ;timer_t1=500   ; Set transaction timer T1 value milliseconds (default: "500")
 ;timer_b=32000  ; Set transaction timer B value milliseconds (default: "32000")
 ;compact_headers=no     ; Use the short forms of common SIP header names
@@ -860,6 +862,8 @@
 ;==========================GLOBAL SECTION OPTIONS=========================
 ;[global]
 ;  SYNOPSIS: Options that apply globally to all SIP communications
+;  NOTE:  Although this section's name can be set to anything, naming it "global"
+;         will allow the object to be cached internally.
 ;max_forwards=70        ; Value used in Max Forwards header for SIP requests
                         ; (default: "70")
 ;type=  ; Must be of type global (default: "")
diff --git a/res/res_pjsip/config_global.c b/res/res_pjsip/config_global.c
index 3d88ffc..2c90416 100644
--- a/res/res_pjsip/config_global.c
+++ b/res/res_pjsip/config_global.c
@@ -98,14 +98,17 @@
 	struct global_config *cfg;
 	struct ao2_container *globals;
 
-	globals = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "global",
-		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
-	if (!globals) {
-		return NULL;
+	cfg = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "global", "global");
+	if (!cfg) {
+		globals = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "global",
+			AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
+		if (!globals) {
+			return NULL;
+		}
+		cfg = ao2_find(globals, NULL, 0);
+		ao2_ref(globals, -1);
 	}
 
-	cfg = ao2_find(globals, NULL, 0);
-	ao2_ref(globals, -1);
 	return cfg;
 }
 
@@ -241,21 +244,28 @@
 		int count;
 
 		count = ao2_container_count(globals);
-		ao2_ref(globals, -1);
 
 		if (1 < count) {
 			ast_log(LOG_ERROR,
 				"At most one pjsip.conf type=global object can be defined.  You have %d defined.\n",
 				count);
+			ao2_ref(globals, -1);
 			return;
 		}
+
 		if (count) {
+			cfg = ao2_find(globals, NULL, OBJ_NOLOCK);
+			if (strcmp(ast_sorcery_object_get_id(cfg), "global")) {
+				ast_log(LOG_NOTICE, "Consider renaming the pjsip '%s' global object to 'global' to enable caching\n", ast_sorcery_object_get_id(cfg));
+			}
+			ao2_ref(cfg, -1);
+			ao2_ref(globals, -1);
 			return;
 		}
 	}
 
 	ast_debug(1, "No pjsip.conf type=global object exists so applying defaults.\n");
-	cfg = ast_sorcery_alloc(sorcery, "global", NULL);
+	cfg = ast_sorcery_alloc(sorcery, "global", "global");
 	if (!cfg) {
 		return;
 	}
diff --git a/res/res_pjsip/config_system.c b/res/res_pjsip/config_system.c
index dfd9240..ff7c108 100644
--- a/res/res_pjsip/config_system.c
+++ b/res/res_pjsip/config_system.c
@@ -117,17 +117,81 @@
 {
 	struct system_config *cfg;
 	struct ao2_container *systems;
-	systems = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
-		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
 
-	if (!systems) {
-		return NULL;
+	cfg = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "system", "system");
+	if (!cfg) {
+		systems = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
+			AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
+		if (!systems) {
+			return NULL;
+		}
+
+		cfg = ao2_find(systems, NULL, 0);
+		ao2_ref(systems, -1);
 	}
-
-	cfg = ao2_find(systems, NULL, 0);
-	ao2_ref(systems, -1);
 	return cfg;
 }
+
+/*!
+ * \internal
+ * \brief Observer to set default global object if none exist.
+ *
+ * \param name Module name owning the sorcery instance.
+ * \param sorcery Instance being observed.
+ * \param object_type Name of object being observed.
+ * \param reloaded Non-zero if the object is being reloaded.
+ *
+ * \return Nothing
+ */
+static void system_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
+{
+	struct ao2_container *systems;
+	struct system_config *cfg;
+
+	if (strcmp(object_type, "system")) {
+		/* Not interested */
+		return;
+	}
+
+	systems = ast_sorcery_retrieve_by_fields(sorcery, "system",
+		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
+	if (systems) {
+		int count;
+
+		count = ao2_container_count(systems);
+
+		if (1 < count) {
+			ast_log(LOG_ERROR,
+				"At most one pjsip.conf type=system object can be defined.  You have %d defined.\n",
+				count);
+			ao2_ref(systems, -1);
+			return;
+		}
+
+		if (count) {
+			cfg = ao2_find(systems, NULL, OBJ_NOLOCK);
+			if (strcmp(ast_sorcery_object_get_id(cfg), "system")) {
+				ast_log(LOG_NOTICE, "Consider renaming the pjsip '%s' system object to 'system' to enable caching\n", ast_sorcery_object_get_id(cfg));
+			}
+			ao2_ref(cfg, -1);
+			ao2_ref(systems, -1);
+			return;
+		}
+	}
+
+	ast_debug(1, "No pjsip.conf type=system object exists so applying defaults.\n");
+	cfg = ast_sorcery_alloc(sorcery, "system", "system");
+	if (!cfg) {
+		return;
+	}
+	system_apply(sorcery, cfg);
+	ao2_ref(cfg, -1);
+}
+
+static const struct ast_sorcery_instance_observer observer_callbacks_system = {
+	.object_type_loaded = system_loaded_observer,
+};
+
 
 int sip_cli_print_system(struct ast_sip_cli_context *context)
 {
@@ -185,28 +249,11 @@
 	ast_sorcery_object_field_register(system_sorcery, "system", "disable_tcp_switch", "yes",
 			OPT_BOOL_T, 1, FLDSET(struct system_config, disable_tcp_switch));
 
+	if (ast_sorcery_instance_observer_add(system_sorcery, &observer_callbacks_system)) {
+		return -1;
+	}
+
 	ast_sorcery_load(system_sorcery);
-
-	system_configs = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
-		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
-
-	if (ao2_container_count(system_configs)) {
-		return 0;
-	}
-
-	/* No config present, allocate one and apply defaults */
-	system = ast_sorcery_alloc(system_sorcery, "system", NULL);
-	if (!system) {
-		ast_log(LOG_ERROR, "Unable to allocate default system config.\n");
-		ast_sorcery_unref(system_sorcery);
-		return -1;
-	}
-
-	if (system_apply(system_sorcery, system)) {
-		ast_log(LOG_ERROR, "Failed to apply default system config.\n");
-		ast_sorcery_unref(system_sorcery);
-		return -1;
-	}
 
 	return 0;
 }

-- 
To view, visit https://gerrit.asterisk.org/2156
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I32beaf688449808251b1eb966bb2cfcfc1d08362
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>



More information about the asterisk-code-review mailing list