[sr-dev] git:master: core: pkg mem size configurable form the cmd line

Andrei Pelinescu-Onciul andrei at iptel.org
Tue Apr 12 21:56:33 CEST 2011


Module: sip-router
Branch: master
Commit: 7b3a6eca7bb6dd1ca2a1395ca1e5821808ec85cd
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=7b3a6eca7bb6dd1ca2a1395ca1e5821808ec85cd

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Tue Apr 12 21:54:07 2011 +0200

core: pkg mem size configurable form the cmd line

The pkg mem size used can be configured by using the "-M"
parameter on the command line, e.g.:
ser -M 8  -f test.cfg # start ser with 8mb of pkg memory
The default remains 4Mb.

---

 globals.h |    1 +
 main.c    |   83 ++++++++++++++++++++++++++++++++++++++++++++----------------
 mem/mem.c |   36 +++++++++++++++++++++-----
 mem/mem.h |    3 +-
 4 files changed, 93 insertions(+), 30 deletions(-)

diff --git a/globals.h b/globals.h
index 40f7214..8b3a81d 100644
--- a/globals.h
+++ b/globals.h
@@ -150,6 +150,7 @@ extern int cfg_warnings;
 extern unsigned int msg_no;
 
 extern unsigned long shm_mem_size;
+extern unsigned long pkg_mem_size;
 
 /* AVP configuration */
 extern char *avp_db_url;  /* db url used by user preferences (AVPs) */
diff --git a/main.c b/main.c
index fcc25ae..d525fb4 100644
--- a/main.c
+++ b/main.c
@@ -245,6 +245,7 @@ Options:\n\
     -b nr        Maximum receive buffer size which will not be exceeded by\n\
                   auto-probing procedure even if  OS allows\n\
     -m nr        Size of shared memory allocated in Megabytes\n\
+    -M nr        Size of private memory allocated, in Megabytes\n\
     -w dir       Change the working directory to \"dir\" (default: \"/\")\n\
     -t dir       Chroot to \"dir\"\n\
     -u uid       Change uid \n\
@@ -273,7 +274,7 @@ void print_ct_constants()
 #endif
 */
 	printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
-			" MAX_URI_SIZE %d, BUF_SIZE %d, PKG_SIZE %uMB\n",
+			" MAX_URI_SIZE %d, BUF_SIZE %d, DEFAULT PKG_SIZE %uMB\n",
 		MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
 		BUF_SIZE, PKG_MEM_SIZE);
 #ifdef USE_TCP
@@ -488,6 +489,8 @@ int cfg_warnings=0;
 
 /* shared memory (in MB) */
 unsigned long shm_mem_size=0;
+/* private (pkg) memory (in MB) */
+unsigned long pkg_mem_size=0;
 
 /* export command-line to anywhere else */
 int my_argc;
@@ -590,6 +593,7 @@ void cleanup(show_status)
 	destroy_lock_ops();
 	if (pid_file) unlink(pid_file);
 	if (pgid_file) unlink(pgid_file);
+	destroy_pkg_mallocs();
 }
 
 
@@ -1750,7 +1754,52 @@ int main(int argc, char** argv)
 	dont_fork_cnt=0;
 
 	daemon_status_init();
-	/*init pkg mallocs (before parsing cfg or cmd line !)*/
+	/* command line options */
+	options=  ":f:cm:M:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
+#ifdef STATS
+		"s:"
+#endif
+	;
+	/* Handle special command line arguments, that must be treated before
+	 * intializing the various subsystem or before parsing other arguments:
+	 *  - get the startup debug and log_stderr values
+	 *  - look if pkg mem size is overriden on the command line (-M) and get
+	 *    the new value here (before intializing pkg_mem).
+	 *  - look if there is a -h, e.g. -f -h construction won't be caught
+	 *    later
+	 */
+	opterr = 0;
+	while((c=getopt(argc,argv,options))!=-1) {
+		switch(c) {
+			case 'd':
+					debug_flag = 1;
+					default_core_cfg.debug++;
+					break;
+			case 'E':
+					log_stderr=1;
+					break;
+			case 'M':
+					pkg_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
+					if (tmp &&(*tmp)){
+						fprintf(stderr, "bad private mem size number: -M %s\n",
+											optarg);
+						goto error;
+					};
+					break;
+			default:
+					if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) {
+						printf("version: %s\n", full_version);
+						printf("%s",help_msg);
+						exit(0);
+					}
+					break;
+		}
+	}
+	
+	/*init pkg mallocs (before parsing cfg or the rest of the cmd line !)*/
+	if (pkg_mem_size)
+		LOG(L_INFO, " private (per process) memory: %ld bytes\n",
+								pkg_mem_size );
 	if (init_pkg_mallocs()==-1)
 		goto error;
 
@@ -1759,11 +1808,6 @@ int main(int argc, char** argv)
 		"DBG_MSG_QA enabled, ser may exit abruptly\n");
 #endif
 
-	options=  ":f:cm:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
-#ifdef STATS
-		"s:"
-#endif
-	;
 	/* init counters / stats */
 	if (init_counters() == -1)
 		goto error;
@@ -1773,16 +1817,6 @@ int main(int argc, char** argv)
 #ifdef USE_SCTP
 	init_sctp_options(); /* set defaults before the config */
 #endif
-	/* look if there is a -h, e.g. -f -h construction won't catch it later */
-	opterr = 0;
-	while((c=getopt(argc,argv,options))!=-1) {
-		if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) {
-			printf("version: %s\n", full_version);
-			printf("%s",help_msg);
-			exit(0);
-			break;
-		}
-	}
 	/* process command line (cfg. file path etc) */
 	optind = 1;  /* reset getopt */
 	/* switches required before script processing */
@@ -1808,9 +1842,12 @@ int main(int argc, char** argv)
 					LOG(L_INFO, "ser: shared memory: %ld bytes\n",
 									shm_mem_size );
 					break;
+			case 'M':
+					/* ignore it, it was parsed immediately after startup,
+					   the pkg mem. is already initialized at this point */
+					break;
 			case 'd':
-					debug_flag = 1;
-					default_core_cfg.debug++;
+					/* ignore it, was parsed immediately after startup */
 					break;
 			case 'V':
 					printf("version: %s\n", full_version);
@@ -1829,7 +1866,7 @@ int main(int argc, char** argv)
 					exit(0);
 					break;
 			case 'E':
-					log_stderr=1;
+					/* ignore it, was parsed immediately after startup */
 					break;
 			case 'O':
 					scr_opt_lev=strtol(optarg, &tmp, 10);
@@ -1974,6 +2011,7 @@ try_again:
 			case 'f':
 			case 'c':
 			case 'm':
+			case 'M':
 			case 'd':
 			case 'V':
 			case 'h':
@@ -1981,7 +2019,8 @@ try_again:
 			case 'A':
 					break;
 			case 'E':
-					log_stderr=1;	// use in both getopt switches
+					log_stderr=1;	/* use in both getopt switches,
+									   takes priority over config */
 					break;
 			case 'b':
 					maxbuffer=strtol(optarg, &tmp, 10);
@@ -2322,7 +2361,7 @@ try_again:
 	}
 
 	if (disable_core_dump) set_core_dump(0, 0);
-	else set_core_dump(1, shm_mem_size+PKG_MEM_POOL_SIZE+4*1024*1024);
+	else set_core_dump(1, shm_mem_size+pkg_mem_size+4*1024*1024);
 	if (open_files_limit>0){
 		if(increase_open_fds(open_files_limit)<0){
 			fprintf(stderr, "ERROR: error could not increase file limits\n");
diff --git a/mem/mem.c b/mem/mem.c
index a2f1b9a..1180a2a 100644
--- a/mem/mem.c
+++ b/mem/mem.c
@@ -26,6 +26,7 @@
 
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "../config.h"
 #include "../dprint.h"
 #include "../globals.h"
@@ -41,15 +42,15 @@
 
 #ifdef PKG_MALLOC
 	#ifndef DL_MALLOC
-	char mem_pool[PKG_MEM_POOL_SIZE];
+	char* mem_pool = 0;
 	#endif
 
 	#ifdef F_MALLOC
-		struct fm_block* mem_block;
+		struct fm_block* mem_block = 0;
 	#elif defined DL_MALLOC
 		/* don't need this */
 	#else
-		struct qm_block* mem_block;
+		struct qm_block* mem_block = 0;
 	#endif
 #endif
 
@@ -58,18 +59,25 @@ int init_pkg_mallocs()
 {
 #ifdef PKG_MALLOC
 	/*init mem*/
+	#ifndef DL_MALLOC
+		if (pkg_mem_size == 0)
+			pkg_mem_size = PKG_MEM_POOL_SIZE;
+		mem_pool = malloc(pkg_mem_size);
+	#endif
 	#ifdef F_MALLOC
-		mem_block=fm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
+		if (mem_pool)
+			mem_block=fm_malloc_init(mem_pool, pkg_mem_size);
 	#elif DL_MALLOC
 		/* don't need this */
 	#else
-		mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
+		if (mem_pool)
+			mem_block=qm_malloc_init(mem_pool, pkg_mem_size);
 	#endif
 	#ifndef DL_MALLOC
 	if (mem_block==0){
 		LOG(L_CRIT, "could not initialize memory pool\n");
-		fprintf(stderr, "Too much pkg memory demanded: %d\n",
-			PKG_MEM_POOL_SIZE );
+		fprintf(stderr, "Too much pkg memory demanded: %ld bytes\n",
+						pkg_mem_size);
 		return -1;
 	}
 	#endif
@@ -79,6 +87,20 @@ int init_pkg_mallocs()
 
 
 
+void destroy_pkg_mallocs()
+{
+#ifdef PKG_MALLOC
+	#ifndef DL_MALLOC
+		if (mem_pool) {
+			free(mem_pool);
+			mem_pool = 0;
+		}
+	#endif
+#endif /* PKG_MALLOC */
+}
+
+
+
 int init_shm_mallocs(int force_alloc)
 {
 #ifdef SHM_MEM
diff --git a/mem/mem.h b/mem/mem.h
index aed1442..510698a 100644
--- a/mem/mem.h
+++ b/mem/mem.h
@@ -58,7 +58,7 @@
 		extern struct qm_block* mem_block;
 #	endif
 
-	extern char mem_pool[PKG_MEM_POOL_SIZE];
+	extern char* mem_pool;
 
 
 #	ifdef DBG_QM_MALLOC
@@ -133,6 +133,7 @@
 #endif
 
 int init_pkg_mallocs();
+void destroy_pkg_mallocs();
 int init_shm_mallocs(int force_alloc);
 
 /*! generic logging helper for allocation errors in private memory pool/ system */




More information about the sr-dev mailing list