Browse code

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.

Andrei Pelinescu-Onciul authored on 12/04/2011 19:54:07
Showing 4 changed files
... ...
@@ -150,6 +150,7 @@ extern int cfg_warnings;
150 150
 extern unsigned int msg_no;
151 151
 
152 152
 extern unsigned long shm_mem_size;
153
+extern unsigned long pkg_mem_size;
153 154
 
154 155
 /* AVP configuration */
155 156
 extern char *avp_db_url;  /* db url used by user preferences (AVPs) */
... ...
@@ -245,6 +245,7 @@ Options:\n\
245 245
     -b nr        Maximum receive buffer size which will not be exceeded by\n\
246 246
                   auto-probing procedure even if  OS allows\n\
247 247
     -m nr        Size of shared memory allocated in Megabytes\n\
248
+    -M nr        Size of private memory allocated, in Megabytes\n\
248 249
     -w dir       Change the working directory to \"dir\" (default: \"/\")\n\
249 250
     -t dir       Chroot to \"dir\"\n\
250 251
     -u uid       Change uid \n\
... ...
@@ -273,7 +274,7 @@ void print_ct_constants()
273 273
 #endif
274 274
 */
275 275
 	printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
276
-			" MAX_URI_SIZE %d, BUF_SIZE %d, PKG_SIZE %uMB\n",
276
+			" MAX_URI_SIZE %d, BUF_SIZE %d, DEFAULT PKG_SIZE %uMB\n",
277 277
 		MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
278 278
 		BUF_SIZE, PKG_MEM_SIZE);
279 279
 #ifdef USE_TCP
... ...
@@ -488,6 +489,8 @@ int cfg_warnings=0;
488 488
 
489 489
 /* shared memory (in MB) */
490 490
 unsigned long shm_mem_size=0;
491
+/* private (pkg) memory (in MB) */
492
+unsigned long pkg_mem_size=0;
491 493
 
492 494
 /* export command-line to anywhere else */
493 495
 int my_argc;
... ...
@@ -590,6 +593,7 @@ void cleanup(show_status)
590 590
 	destroy_lock_ops();
591 591
 	if (pid_file) unlink(pid_file);
592 592
 	if (pgid_file) unlink(pgid_file);
593
+	destroy_pkg_mallocs();
593 594
 }
594 595
 
595 596
 
... ...
@@ -1750,7 +1754,52 @@ int main(int argc, char** argv)
1750 1750
 	dont_fork_cnt=0;
1751 1751
 
1752 1752
 	daemon_status_init();
1753
-	/*init pkg mallocs (before parsing cfg or cmd line !)*/
1753
+	/* command line options */
1754
+	options=  ":f:cm:M:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
1755
+#ifdef STATS
1756
+		"s:"
1757
+#endif
1758
+	;
1759
+	/* Handle special command line arguments, that must be treated before
1760
+	 * intializing the various subsystem or before parsing other arguments:
1761
+	 *  - get the startup debug and log_stderr values
1762
+	 *  - look if pkg mem size is overriden on the command line (-M) and get
1763
+	 *    the new value here (before intializing pkg_mem).
1764
+	 *  - look if there is a -h, e.g. -f -h construction won't be caught
1765
+	 *    later
1766
+	 */
1767
+	opterr = 0;
1768
+	while((c=getopt(argc,argv,options))!=-1) {
1769
+		switch(c) {
1770
+			case 'd':
1771
+					debug_flag = 1;
1772
+					default_core_cfg.debug++;
1773
+					break;
1774
+			case 'E':
1775
+					log_stderr=1;
1776
+					break;
1777
+			case 'M':
1778
+					pkg_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
1779
+					if (tmp &&(*tmp)){
1780
+						fprintf(stderr, "bad private mem size number: -M %s\n",
1781
+											optarg);
1782
+						goto error;
1783
+					};
1784
+					break;
1785
+			default:
1786
+					if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) {
1787
+						printf("version: %s\n", full_version);
1788
+						printf("%s",help_msg);
1789
+						exit(0);
1790
+					}
1791
+					break;
1792
+		}
1793
+	}
1794
+	
1795
+	/*init pkg mallocs (before parsing cfg or the rest of the cmd line !)*/
1796
+	if (pkg_mem_size)
1797
+		LOG(L_INFO, " private (per process) memory: %ld bytes\n",
1798
+								pkg_mem_size );
1754 1799
 	if (init_pkg_mallocs()==-1)
1755 1800
 		goto error;
1756 1801
 
... ...
@@ -1759,11 +1808,6 @@ int main(int argc, char** argv)
1759 1759
 		"DBG_MSG_QA enabled, ser may exit abruptly\n");
1760 1760
 #endif
1761 1761
 
1762
-	options=  ":f:cm:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
1763
-#ifdef STATS
1764
-		"s:"
1765
-#endif
1766
-	;
1767 1762
 	/* init counters / stats */
1768 1763
 	if (init_counters() == -1)
1769 1764
 		goto error;
... ...
@@ -1773,16 +1817,6 @@ int main(int argc, char** argv)
1773 1773
 #ifdef USE_SCTP
1774 1774
 	init_sctp_options(); /* set defaults before the config */
1775 1775
 #endif
1776
-	/* look if there is a -h, e.g. -f -h construction won't catch it later */
1777
-	opterr = 0;
1778
-	while((c=getopt(argc,argv,options))!=-1) {
1779
-		if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) {
1780
-			printf("version: %s\n", full_version);
1781
-			printf("%s",help_msg);
1782
-			exit(0);
1783
-			break;
1784
-		}
1785
-	}
1786 1776
 	/* process command line (cfg. file path etc) */
1787 1777
 	optind = 1;  /* reset getopt */
1788 1778
 	/* switches required before script processing */
... ...
@@ -1808,9 +1842,12 @@ int main(int argc, char** argv)
1808 1808
 					LOG(L_INFO, "ser: shared memory: %ld bytes\n",
1809 1809
 									shm_mem_size );
1810 1810
 					break;
1811
+			case 'M':
1812
+					/* ignore it, it was parsed immediately after startup,
1813
+					   the pkg mem. is already initialized at this point */
1814
+					break;
1811 1815
 			case 'd':
1812
-					debug_flag = 1;
1813
-					default_core_cfg.debug++;
1816
+					/* ignore it, was parsed immediately after startup */
1814 1817
 					break;
1815 1818
 			case 'V':
1816 1819
 					printf("version: %s\n", full_version);
... ...
@@ -1829,7 +1866,7 @@ int main(int argc, char** argv)
1829 1829
 					exit(0);
1830 1830
 					break;
1831 1831
 			case 'E':
1832
-					log_stderr=1;
1832
+					/* ignore it, was parsed immediately after startup */
1833 1833
 					break;
1834 1834
 			case 'O':
1835 1835
 					scr_opt_lev=strtol(optarg, &tmp, 10);
... ...
@@ -1974,6 +2011,7 @@ try_again:
1974 1974
 			case 'f':
1975 1975
 			case 'c':
1976 1976
 			case 'm':
1977
+			case 'M':
1977 1978
 			case 'd':
1978 1979
 			case 'V':
1979 1980
 			case 'h':
... ...
@@ -1981,7 +2019,8 @@ try_again:
1981 1981
 			case 'A':
1982 1982
 					break;
1983 1983
 			case 'E':
1984
-					log_stderr=1;	// use in both getopt switches
1984
+					log_stderr=1;	/* use in both getopt switches,
1985
+									   takes priority over config */
1985 1986
 					break;
1986 1987
 			case 'b':
1987 1988
 					maxbuffer=strtol(optarg, &tmp, 10);
... ...
@@ -2322,7 +2361,7 @@ try_again:
2322 2322
 	}
2323 2323
 
2324 2324
 	if (disable_core_dump) set_core_dump(0, 0);
2325
-	else set_core_dump(1, shm_mem_size+PKG_MEM_POOL_SIZE+4*1024*1024);
2325
+	else set_core_dump(1, shm_mem_size+pkg_mem_size+4*1024*1024);
2326 2326
 	if (open_files_limit>0){
2327 2327
 		if(increase_open_fds(open_files_limit)<0){
2328 2328
 			fprintf(stderr, "ERROR: error could not increase file limits\n");
... ...
@@ -26,6 +26,7 @@
26 26
 
27 27
 
28 28
 #include <stdio.h>
29
+#include <stdlib.h>
29 30
 #include "../config.h"
30 31
 #include "../dprint.h"
31 32
 #include "../globals.h"
... ...
@@ -41,15 +42,15 @@
41 41
 
42 42
 #ifdef PKG_MALLOC
43 43
 	#ifndef DL_MALLOC
44
-	char mem_pool[PKG_MEM_POOL_SIZE];
44
+	char* mem_pool = 0;
45 45
 	#endif
46 46
 
47 47
 	#ifdef F_MALLOC
48
-		struct fm_block* mem_block;
48
+		struct fm_block* mem_block = 0;
49 49
 	#elif defined DL_MALLOC
50 50
 		/* don't need this */
51 51
 	#else
52
-		struct qm_block* mem_block;
52
+		struct qm_block* mem_block = 0;
53 53
 	#endif
54 54
 #endif
55 55
 
... ...
@@ -58,18 +59,25 @@ int init_pkg_mallocs()
58 58
 {
59 59
 #ifdef PKG_MALLOC
60 60
 	/*init mem*/
61
+	#ifndef DL_MALLOC
62
+		if (pkg_mem_size == 0)
63
+			pkg_mem_size = PKG_MEM_POOL_SIZE;
64
+		mem_pool = malloc(pkg_mem_size);
65
+	#endif
61 66
 	#ifdef F_MALLOC
62
-		mem_block=fm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
67
+		if (mem_pool)
68
+			mem_block=fm_malloc_init(mem_pool, pkg_mem_size);
63 69
 	#elif DL_MALLOC
64 70
 		/* don't need this */
65 71
 	#else
66
-		mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
72
+		if (mem_pool)
73
+			mem_block=qm_malloc_init(mem_pool, pkg_mem_size);
67 74
 	#endif
68 75
 	#ifndef DL_MALLOC
69 76
 	if (mem_block==0){
70 77
 		LOG(L_CRIT, "could not initialize memory pool\n");
71
-		fprintf(stderr, "Too much pkg memory demanded: %d\n",
72
-			PKG_MEM_POOL_SIZE );
78
+		fprintf(stderr, "Too much pkg memory demanded: %ld bytes\n",
79
+						pkg_mem_size);
73 80
 		return -1;
74 81
 	}
75 82
 	#endif
... ...
@@ -79,6 +87,20 @@ int init_pkg_mallocs()
79 79
 
80 80
 
81 81
 
82
+void destroy_pkg_mallocs()
83
+{
84
+#ifdef PKG_MALLOC
85
+	#ifndef DL_MALLOC
86
+		if (mem_pool) {
87
+			free(mem_pool);
88
+			mem_pool = 0;
89
+		}
90
+	#endif
91
+#endif /* PKG_MALLOC */
92
+}
93
+
94
+
95
+
82 96
 int init_shm_mallocs(int force_alloc)
83 97
 {
84 98
 #ifdef SHM_MEM
... ...
@@ -58,7 +58,7 @@
58 58
 		extern struct qm_block* mem_block;
59 59
 #	endif
60 60
 
61
-	extern char mem_pool[PKG_MEM_POOL_SIZE];
61
+	extern char* mem_pool;
62 62
 
63 63
 
64 64
 #	ifdef DBG_QM_MALLOC
... ...
@@ -133,6 +133,7 @@
133 133
 #endif
134 134
 
135 135
 int init_pkg_mallocs();
136
+void destroy_pkg_mallocs();
136 137
 int init_shm_mallocs(int force_alloc);
137 138
 
138 139
 /*! generic logging helper for allocation errors in private memory pool/ system */