core: added mem_summary config option
authorAndrei Pelinescu-Onciul <andrei@iptel.org>
Tue, 29 Sep 2009 11:32:48 +0000 (13:32 +0200)
committerAndrei Pelinescu-Onciul <andrei@iptel.org>
Tue, 29 Sep 2009 17:03:41 +0000 (19:03 +0200)
- added mem_summary which controls  the memory debugging info
  logged on exit or on SIGUSR1.  Its value is a combination of
  flags: 0 - off, 1 - dump all used memory blocks and some
  statistics (lots of output), 2 - dump a summary of the used
  memory blocks (works only if compiled with DBG_QM_MALLOC or
  DBG_F_MALLOC).
  The default value is 1 (same behaviour as older versions).

- fix: don't call the memory status/summary functions if
  memlog > debug level (since nothing will be logged anyway).

NEWS
cfg.lex
cfg.y
cfg_core.c
cfg_core.h
main.c

diff --git a/NEWS b/NEWS
index 7cd7fff..3cd2de4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -72,6 +72,13 @@ new config variables:
   - max_while_loops - maximum iterations allowed for a while  (can be changed
        at runtime). Default 100.
   - log_name - set the application name used when printing to syslog.
+  - mem_summary - memory debugging info logged on exit or on SIGUSR1.
+       The value is a combination of flags: 0 - off,
+       1 - dump all used memory blocks and some statistics (lots of output),
+       2 - dump a summary of the used memory blocks (works only if
+           compiled with DBG_QM_MALLOC or DBG_F_MALLOC).
+       Default: 1.
+       Can be changed at runtime.
 
 new script commands:
   add_local_rport() - adds the rport parameter to the added via header
diff --git a/cfg.lex b/cfg.lex
index bedf79c..8490364 100644 (file)
--- a/cfg.lex
+++ b/cfg.lex
@@ -353,6 +353,7 @@ PHONE2TEL   phone2tel
 SYN_BRANCH syn_branch
 MEMLOG         "memlog"|"mem_log"
 MEMDBG         "memdbg"|"mem_dbg"
+MEMSUM         "mem_summary"
 SIP_WARNING sip_warning
 SERVER_SIGNATURE server_signature
 SERVER_HEADER server_header
@@ -688,6 +689,7 @@ EAT_ABLE    [\ \t\b\r]
 <INITIAL>{SYN_BRANCH}  { count(); yylval.strval=yytext; return SYN_BRANCH; }
 <INITIAL>{MEMLOG}      { count(); yylval.strval=yytext; return MEMLOG; }
 <INITIAL>{MEMDBG}      { count(); yylval.strval=yytext; return MEMDBG; }
+<INITIAL>{MEMSUM}      { count(); yylval.strval=yytext; return MEMSUM; }
 <INITIAL>{SIP_WARNING} { count(); yylval.strval=yytext; return SIP_WARNING; }
 <INITIAL>{USER}                { count(); yylval.strval=yytext; return USER; }
 <INITIAL>{GROUP}       { count(); yylval.strval=yytext; return GROUP; }
diff --git a/cfg.y b/cfg.y
index fe8cbf2..ff56515 100644 (file)
--- a/cfg.y
+++ b/cfg.y
@@ -408,6 +408,7 @@ extern char *finame;
 %token SYN_BRANCH
 %token MEMLOG
 %token MEMDBG
+%token MEMSUM
 %token SIP_WARNING
 %token SERVER_SIGNATURE
 %token SERVER_HEADER
@@ -863,6 +864,8 @@ assign_stm:
        | MEMLOG EQUAL error { yyerror("int value expected"); }
        | MEMDBG EQUAL intno { memdbg=$3; }
        | MEMDBG EQUAL error { yyerror("int value expected"); }
+       | MEMSUM EQUAL intno { default_core_cfg.mem_summary=$3; }
+       | MEMSUM EQUAL error { yyerror("int value expected"); }
        | SIP_WARNING EQUAL NUMBER { sip_warning=$3; }
        | SIP_WARNING EQUAL error { yyerror("boolean value expected"); }
        | USER EQUAL STRING     { user=$3; }
index 778a4dd..ce0ae50 100644 (file)
@@ -92,7 +92,8 @@ struct cfg_group_core default_core_cfg = {
        DEFAULT_MAX_WHILE_LOOPS, /* max_while_loops */
        0, /* udp_mtu (disabled by default) */
        0, /* udp_mtu_try_proto -> default disabled */
-       0  /* force_rport */ 
+       0,  /* force_rport */
+       1 /* mem_summary -flags: 0 off, 1 shm/pkg_status, 2 shm/pkg_sums */
 };
 
 void   *core_cfg = &default_core_cfg;
@@ -191,5 +192,8 @@ cfg_def_t core_cfg_def[] = {
                "if send size > udp_mtu use proto (1 udp, 2 tcp, 3 tls, 4 sctp)"},
        {"force_rport",     CFG_VAR_INT, 0, 1,  0, fix_global_req_flags,
                "force rport for all the received messages" },
+       {"mem_summary", CFG_VAR_INT|CFG_ATOMIC, 0, 3, 0, 0,
+               "memory debugging information displayed on exit (flags): "
+               " 0 - off, 1 - dump all used blocks, 2 - summary of used blocks" },
        {0, 0, 0, 0, 0, 0}
 };
index 4f493ef..dd5d7af 100644 (file)
@@ -89,6 +89,7 @@ struct cfg_group_core {
        int udp_mtu; /**< maximum send size for udp, if > try another protocol*/
        int udp_mtu_try_proto; /**< if packet> udp_mtu, try proto (e.g. TCP) */
        int force_rport; /**< if set rport will always be forced*/
+       int mem_summary; /**< display memory status/summary info on exit */
 };
 
 extern struct cfg_group_core default_core_cfg;
diff --git a/main.c b/main.c
index 6d85a57..0965051 100644 (file)
--- a/main.c
+++ b/main.c
@@ -529,21 +529,29 @@ void cleanup(show_status)
        destroy_routes();
        destroy_atomic_ops();
 #ifdef PKG_MALLOC
-       if (show_status){
-               LOG(memlog, "Memory status (pkg):\n");
-               pkg_status();
-               LOG(memlog, "Memory still-in-use summary (pkg):\n");
-               pkg_sums();
+       if (show_status && memlog <= cfg_get(core, core_cfg, debug)){
+               if (cfg_get(core, core_cfg, mem_summary) & 1) {
+                       LOG(memlog, "Memory status (pkg):\n");
+                       pkg_status();
+               }
+               if (cfg_get(core, core_cfg, mem_summary) & 2) {
+                       LOG(memlog, "Memory still-in-use summary (pkg):\n");
+                       pkg_sums();
+               }
        }
 #endif
 #ifdef SHM_MEM
        if (pt) shm_free(pt);
        pt=0;
-       if (show_status){
+       if (show_status && memlog <= cfg_get(core, core_cfg, debug)){
+               if (cfg_get(core, core_cfg, mem_summary) & 1) {
                        LOG(memlog, "Memory status (shm):\n");
                        shm_status();
+               }
+               if (cfg_get(core, core_cfg, mem_summary) & 2) {
                        LOG(memlog, "Memory still-in-use summary (shm):\n");
                        shm_sums();
+               }
        }
        /* zero all shmem alloc vars that we still use */
        shm_mem_destroy();
@@ -664,12 +672,28 @@ void handle_sigs()
                        dump_all_statistic();
 #endif
 #ifdef PKG_MALLOC
-                       LOG(memlog, "Memory status (pkg):\n");
-                       pkg_status();
+               if (memlog <= cfg_get(core, core_cfg, debug)){
+                       if (cfg_get(core, core_cfg, mem_summary) & 1) {
+                               LOG(memlog, "Memory status (pkg):\n");
+                               pkg_status();
+                       }
+                       if (cfg_get(core, core_cfg, mem_summary) & 2) {
+                               LOG(memlog, "Memory still-in-use summary (pkg):\n");
+                               pkg_sums();
+                       }
+               }
 #endif
 #ifdef SHM_MEM
-                       LOG(memlog, "Memory status (shm):\n");
-                       shm_status();
+               if (memlog <= cfg_get(core, core_cfg, debug)){
+                       if (cfg_get(core, core_cfg, mem_summary) & 1) {
+                               LOG(memlog, "Memory status (shm):\n");
+                               shm_status();
+                       }
+                       if (cfg_get(core, core_cfg, mem_summary) & 2) {
+                               LOG(memlog, "Memory still-in-use summary (shm):\n");
+                               shm_sums();
+                       }
+               }
 #endif
                        break;
 
@@ -747,8 +771,17 @@ void sig_usr(int signo)
                                        LOG(L_INFO, "INFO: signal %d received\n", signo);
                                        /* print memory stats for non-main too */
                                        #ifdef PKG_MALLOC
-                                       LOG(memlog, "Memory status (pkg):\n");
-                                       pkg_status();
+                                       if (memlog <= cfg_get(core, core_cfg, debug)){
+                                               if (cfg_get(core, core_cfg, mem_summary) & 1) {
+                                                       LOG(memlog, "Memory status (pkg):\n");
+                                                       pkg_status();
+                                               }
+                                               if (cfg_get(core, core_cfg, mem_summary) & 2) {
+                                                       LOG(memlog, "Memory still-in-use summary (pkg):"
+                                                                       "\n");
+                                                       pkg_sums();
+                                               }
+                                       }
                                        #endif
 #endif
                                        _exit(0);