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 274
 #endif
274 275
 */
275 276
 	printf("MAX_RECV_BUFFER_SIZE %d, MAX_LISTEN %d,"
276
-			" MAX_URI_SIZE %d, BUF_SIZE %d, PKG_SIZE %uMB\n",
277
+			" MAX_URI_SIZE %d, BUF_SIZE %d, DEFAULT PKG_SIZE %uMB\n",
277 278
 		MAX_RECV_BUFFER_SIZE, MAX_LISTEN, MAX_URI_SIZE,
278 279
 		BUF_SIZE, PKG_MEM_SIZE);
279 280
 #ifdef USE_TCP
... ...
@@ -488,6 +489,8 @@ int cfg_warnings=0;
488 489
 
489 490
 /* shared memory (in MB) */
490 491
 unsigned long shm_mem_size=0;
492
+/* private (pkg) memory (in MB) */
493
+unsigned long pkg_mem_size=0;
491 494
 
492 495
 /* export command-line to anywhere else */
493 496
 int my_argc;
... ...
@@ -590,6 +593,7 @@ void cleanup(show_status)
590 593
 	destroy_lock_ops();
591 594
 	if (pid_file) unlink(pid_file);
592 595
 	if (pgid_file) unlink(pgid_file);
596
+	destroy_pkg_mallocs();
593 597
 }
594 598
 
595 599
 
... ...
@@ -1750,7 +1754,52 @@ int main(int argc, char** argv)
1750 1754
 	dont_fork_cnt=0;
1751 1755
 
1752 1756
 	daemon_status_init();
1753
-	/*init pkg mallocs (before parsing cfg or cmd line !)*/
1757
+	/* command line options */
1758
+	options=  ":f:cm:M:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
1759
+#ifdef STATS
1760
+		"s:"
1761
+#endif
1762
+	;
1763
+	/* Handle special command line arguments, that must be treated before
1764
+	 * intializing the various subsystem or before parsing other arguments:
1765
+	 *  - get the startup debug and log_stderr values
1766
+	 *  - look if pkg mem size is overriden on the command line (-M) and get
1767
+	 *    the new value here (before intializing pkg_mem).
1768
+	 *  - look if there is a -h, e.g. -f -h construction won't be caught
1769
+	 *    later
1770
+	 */
1771
+	opterr = 0;
1772
+	while((c=getopt(argc,argv,options))!=-1) {
1773
+		switch(c) {
1774
+			case 'd':
1775
+					debug_flag = 1;
1776
+					default_core_cfg.debug++;
1777
+					break;
1778
+			case 'E':
1779
+					log_stderr=1;
1780
+					break;
1781
+			case 'M':
1782
+					pkg_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
1783
+					if (tmp &&(*tmp)){
1784
+						fprintf(stderr, "bad private mem size number: -M %s\n",
1785
+											optarg);
1786
+						goto error;
1787
+					};
1788
+					break;
1789
+			default:
1790
+					if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) {
1791
+						printf("version: %s\n", full_version);
1792
+						printf("%s",help_msg);
1793
+						exit(0);
1794
+					}
1795
+					break;
1796
+		}
1797
+	}
1798
+	
1799
+	/*init pkg mallocs (before parsing cfg or the rest of the cmd line !)*/
1800
+	if (pkg_mem_size)
1801
+		LOG(L_INFO, " private (per process) memory: %ld bytes\n",
1802
+								pkg_mem_size );
1754 1803
 	if (init_pkg_mallocs()==-1)
1755 1804
 		goto error;
1756 1805
 
... ...
@@ -1759,11 +1808,6 @@ int main(int argc, char** argv)
1759 1808
 		"DBG_MSG_QA enabled, ser may exit abruptly\n");
1760 1809
 #endif
1761 1810
 
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 1811
 	/* init counters / stats */
1768 1812
 	if (init_counters() == -1)
1769 1813
 		goto error;
... ...
@@ -1773,16 +1817,6 @@ int main(int argc, char** argv)
1773 1817
 #ifdef USE_SCTP
1774 1818
 	init_sctp_options(); /* set defaults before the config */
1775 1819
 #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 1820
 	/* process command line (cfg. file path etc) */
1787 1821
 	optind = 1;  /* reset getopt */
1788 1822
 	/* switches required before script processing */
... ...
@@ -1808,9 +1842,12 @@ int main(int argc, char** argv)
1808 1842
 					LOG(L_INFO, "ser: shared memory: %ld bytes\n",
1809 1843
 									shm_mem_size );
1810 1844
 					break;
1845
+			case 'M':
1846
+					/* ignore it, it was parsed immediately after startup,
1847
+					   the pkg mem. is already initialized at this point */
1848
+					break;
1811 1849
 			case 'd':
1812
-					debug_flag = 1;
1813
-					default_core_cfg.debug++;
1850
+					/* ignore it, was parsed immediately after startup */
1814 1851
 					break;
1815 1852
 			case 'V':
1816 1853
 					printf("version: %s\n", full_version);
... ...
@@ -1829,7 +1866,7 @@ int main(int argc, char** argv)
1829 1866
 					exit(0);
1830 1867
 					break;
1831 1868
 			case 'E':
1832
-					log_stderr=1;
1869
+					/* ignore it, was parsed immediately after startup */
1833 1870
 					break;
1834 1871
 			case 'O':
1835 1872
 					scr_opt_lev=strtol(optarg, &tmp, 10);
... ...
@@ -1974,6 +2011,7 @@ try_again:
1974 2011
 			case 'f':
1975 2012
 			case 'c':
1976 2013
 			case 'm':
2014
+			case 'M':
1977 2015
 			case 'd':
1978 2016
 			case 'V':
1979 2017
 			case 'h':
... ...
@@ -1981,7 +2019,8 @@ try_again:
1981 2019
 			case 'A':
1982 2020
 					break;
1983 2021
 			case 'E':
1984
-					log_stderr=1;	// use in both getopt switches
2022
+					log_stderr=1;	/* use in both getopt switches,
2023
+									   takes priority over config */
1985 2024
 					break;
1986 2025
 			case 'b':
1987 2026
 					maxbuffer=strtol(optarg, &tmp, 10);
... ...
@@ -2322,7 +2361,7 @@ try_again:
2322 2361
 	}
2323 2362
 
2324 2363
 	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);
2364
+	else set_core_dump(1, shm_mem_size+pkg_mem_size+4*1024*1024);
2326 2365
 	if (open_files_limit>0){
2327 2366
 		if(increase_open_fds(open_files_limit)<0){
2328 2367
 			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 42
 
42 43
 #ifdef PKG_MALLOC
43 44
 	#ifndef DL_MALLOC
44
-	char mem_pool[PKG_MEM_POOL_SIZE];
45
+	char* mem_pool = 0;
45 46
 	#endif
46 47
 
47 48
 	#ifdef F_MALLOC
48
-		struct fm_block* mem_block;
49
+		struct fm_block* mem_block = 0;
49 50
 	#elif defined DL_MALLOC
50 51
 		/* don't need this */
51 52
 	#else
52
-		struct qm_block* mem_block;
53
+		struct qm_block* mem_block = 0;
53 54
 	#endif
54 55
 #endif
55 56
 
... ...
@@ -58,18 +59,25 @@ int init_pkg_mallocs()
58 59
 {
59 60
 #ifdef PKG_MALLOC
60 61
 	/*init mem*/
62
+	#ifndef DL_MALLOC
63
+		if (pkg_mem_size == 0)
64
+			pkg_mem_size = PKG_MEM_POOL_SIZE;
65
+		mem_pool = malloc(pkg_mem_size);
66
+	#endif
61 67
 	#ifdef F_MALLOC
62
-		mem_block=fm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
68
+		if (mem_pool)
69
+			mem_block=fm_malloc_init(mem_pool, pkg_mem_size);
63 70
 	#elif DL_MALLOC
64 71
 		/* don't need this */
65 72
 	#else
66
-		mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
73
+		if (mem_pool)
74
+			mem_block=qm_malloc_init(mem_pool, pkg_mem_size);
67 75
 	#endif
68 76
 	#ifndef DL_MALLOC
69 77
 	if (mem_block==0){
70 78
 		LOG(L_CRIT, "could not initialize memory pool\n");
71
-		fprintf(stderr, "Too much pkg memory demanded: %d\n",
72
-			PKG_MEM_POOL_SIZE );
79
+		fprintf(stderr, "Too much pkg memory demanded: %ld bytes\n",
80
+						pkg_mem_size);
73 81
 		return -1;
74 82
 	}
75 83
 	#endif
... ...
@@ -79,6 +87,20 @@ int init_pkg_mallocs()
79 87
 
80 88
 
81 89
 
90
+void destroy_pkg_mallocs()
91
+{
92
+#ifdef PKG_MALLOC
93
+	#ifndef DL_MALLOC
94
+		if (mem_pool) {
95
+			free(mem_pool);
96
+			mem_pool = 0;
97
+		}
98
+	#endif
99
+#endif /* PKG_MALLOC */
100
+}
101
+
102
+
103
+
82 104
 int init_shm_mallocs(int force_alloc)
83 105
 {
84 106
 #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 */