Hello,
during Kamailio World I and Camille discussed briefly about enhancing
kamailio to support selecting the memory manager without recompilation.
This morning I pushed a bunch of patches allowing this via command line
parameters.
I am writing now to describe shortly the process of adding a new memory
manager in the list of active ones.
1) a define should be available in the Makefile.defs to enable disable
that memory manger completely during compilation (right now there are
Q_MALLOC and F_MALLOC). The files with the code of the memory manager
must be wrapped in such define
2) each memory manager has to export a set of function to do pkg and shm
memory operations -- they are different as shm management requires some
'unsafe' functions (when locking must not be done). There are two API
structures defined in mem/memapi.h
3) the new memory manger has to export two functions to initialize the
memory management for pkg and shm. Theyr prototypes have to be added in
mem/memcore.h. The implementation of those two functions must be done in
the files with the code of the memory manager
4) extend memory manger conditions with the new name inside mem/pkg.c:
pkg_init_manager(...) and mem/shm.c:shm_init_manager(...).
5) code of the memory manger has to be extended to export the two apis
for pkg and shm -- looking at the last part of mem/f_malloc.c should
give rather straightforward code to follow
6) debugging mode for memory manager must be enabled if DBG_SR_MEMORY is
defined, because the API prototypes depend on it. For example,
mem/f_malloc.h at the top enables debugging for f_malloc via:
#ifdef DBG_SR_MEMORY
#define DBG_F_MALLOC
#endif
General remarks:
- it is theoretically possible to use one memory manger for shm and
another one for pkg (e.g., pkg can use system malloc)
- shm has some common api to allocate the pool of shared memory and
destroy it, the manager should request the pool from the core api.
Destroying of the shared pool is done also in the common code.
- pkg is allocating directly with malloc the pool, so it is done inside
each manager. Destroying at shut down has to be done there as well
- the plan is to extend the code to allow loading the memory manager
from a module -- it may need some time, for the moment existing memory
managers can be converted and be left in the core
Short term plans/goals:
- hopefully Camille will find some spare time to convert TLSF to the new
memory management api -- I can assist when needed
- I plan to add a system malloc custom manager to be able to use system
memory without recompilation. This will be different than compiling
kamailio without PKG_MALLOC (which does inline replacement with
malloc/free/...)
- maybe resurrect other memory managers that are implemented in mem/
folder -- at least Doug Lea malloc seems to worth the troubles
- said already, enhance so the code for memory malloc can be inside a
module, to move out of core some of those managers
The new memory management api is at beginning, suggestions to adjust it
are welcome.
Cheers,
Daniel
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Book: SIP Routing With Kamailio - http://www.asipto.com
Kamailio Advanced Training, Sep 28-30, 2015, in Berlin - http://asipto.com/u/kat
Module: kamailio
Branch: master
Commit: 4c2c7d229a6387bf4fb9bdfdc9c5c87cce0d47ba
URL: https://github.com/kamailio/kamailio/commit/4c2c7d229a6387bf4fb9bdfdc9c5c87…
Author: root <root(a)CSCFs-Development.it.za.corp.smilecoms.com>
Committer: root <root(a)CSCFs-Development.it.za.corp.smilecoms.com>
Date: 2015-09-14T17:07:12+02:00
modules/cdp: removed references to old shm management
---
Modified: modules/cdp/globals.c
---
Diff: https://github.com/kamailio/kamailio/commit/4c2c7d229a6387bf4fb9bdfdc9c5c87…
Patch: https://github.com/kamailio/kamailio/commit/4c2c7d229a6387bf4fb9bdfdc9c5c87…
---
diff --git a/modules/cdp/globals.c b/modules/cdp/globals.c
index 76f0d59..d901d4c 100644
--- a/modules/cdp/globals.c
+++ b/modules/cdp/globals.c
@@ -45,6 +45,7 @@
#include "globals.h"
#include "utils.h"
+#include "../../mem/shm_mem.h"
//str aaa_fqdn={"unset_fqdn",10};
//str aaa_realm={"unset_realm",11};
@@ -83,31 +84,6 @@
/** call it before exiting; if show_status==1, mem status is displayed */
void destroy_memory(int show_status)
{
- /*clean-up*/
- if (mem_lock)
- shm_unlock(); /* hack: force-unlock the shared memory lock in case
- some process crashed and let it locked; this will
- allow an almost gracious shutdown */
-#ifdef SHM_MEM
- if (show_status){
- LM_DBG( "Memory status (shm):\n");
- //shm_status();
-#ifndef SER_MOD_INTERFACE
- shm_sums();
-#endif
- }
- /* zero all shmem alloc vars that we still use */
- shm_mem_destroy();
-#endif
-#ifdef PKG_MALLOC
- if (show_status){
- LM_DBG( "Memory status (pkg):\n");
- //pkg_status();
-#ifndef SER_MOD_INTERFACE
- pkg_sums();
-#endif
- }
-#endif
}
Module: kamailio
Branch: master
Commit: 171553ebcac2f00a015191f36f2f228c0d3e483b
URL: https://github.com/kamailio/kamailio/commit/171553ebcac2f00a015191f36f2f228…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2015-09-14T12:53:54+02:00
tm: reset async suspended flag when continuing via failure_route
- suspended transaction can be resumed automatically on timeout, without
t_continue(), ending up in failure_route
---
Modified: modules/tm/t_reply.c
---
Diff: https://github.com/kamailio/kamailio/commit/171553ebcac2f00a015191f36f2f228…
Patch: https://github.com/kamailio/kamailio/commit/171553ebcac2f00a015191f36f2f228…
---
diff --git a/modules/tm/t_reply.c b/modules/tm/t_reply.c
index 8240ced..0041a4f 100644
--- a/modules/tm/t_reply.c
+++ b/modules/tm/t_reply.c
@@ -982,6 +982,8 @@ int run_failure_handlers(struct cell *t, struct sip_msg *rpl,
* set next failure route, failure_route will not be reentered
* on failure */
t->on_failure=0;
+ /* if continuing on timeout of a suspended transaction, reset the flag */
+ t->flags &= ~T_ASYNC_SUSPENDED;
if (exec_pre_script_cb(&faked_req, FAILURE_CB_TYPE)>0) {
/* run a failure_route action if some was marked */
if (run_top_route(failure_rt.rlist[on_failure], &faked_req, 0)<0)
Module: kamailio
Branch: master
Commit: 52c4965bbb4cde5d7bddcf697aa504e7c2202ac1
URL: https://github.com/kamailio/kamailio/commit/52c4965bbb4cde5d7bddcf697aa504e…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2015-09-14T12:45:28+02:00
tm: test if transaction was suspended before resuming inside t_continue()
---
Modified: modules/tm/t_suspend.c
---
Diff: https://github.com/kamailio/kamailio/commit/52c4965bbb4cde5d7bddcf697aa504e…
Patch: https://github.com/kamailio/kamailio/commit/52c4965bbb4cde5d7bddcf697aa504e…
---
diff --git a/modules/tm/t_suspend.c b/modules/tm/t_suspend.c
index 67572b6..1ca56db 100644
--- a/modules/tm/t_suspend.c
+++ b/modules/tm/t_suspend.c
@@ -106,10 +106,10 @@ int t_suspend(struct sip_msg *msg,
* - failure route to be executed if the branch is not continued
* before timeout */
t->uac[t->async_backup.blind_uac].on_failure = t->on_failure;
+ t->flags |= T_ASYNC_SUSPENDED;
} else {
LM_DBG("this is a suspend on reply - setting msg flag to SUSPEND\n");
msg->msg_flags |= FL_RPL_SUSPENDED;
- t->flags |= T_ASYNC_SUSPENDED;
/* this is a reply suspend find which branch */
if (t_check( msg , &branch )==-1){
@@ -131,6 +131,7 @@ int t_suspend(struct sip_msg *msg,
LM_DBG("saving transaction data\n");
t->uac[branch].reply->flags = msg->flags;
+ t->flags |= T_ASYNC_SUSPENDED;
}
*hash_index = t->hash_index;
@@ -176,7 +177,13 @@ int t_continue(unsigned int hash_index, unsigned int label,
return -1;
}
+ if (!(t->flags & T_ASYNC_SUSPENDED)) {
+ LM_ERR("transaction is not suspended [%u:%u]\n", hash_index, label);
+ return -2;
+ }
+
if (t->flags & T_CANCELED) {
+ t->flags &= ~T_ASYNC_SUSPENDED;
/* The transaction has already been canceled,
* needless to continue */
UNREF(t); /* t_unref would kill the transaction */
@@ -219,6 +226,7 @@ int t_continue(unsigned int hash_index, unsigned int label,
/* Either t_continue() has already been
* called or the branch has already timed out.
* Needless to continue. */
+ t->flags &= ~T_ASYNC_SUSPENDED;
UNLOCK_ASYNC_CONTINUE(t);
UNREF(t); /* t_unref would kill the transaction */
return 1;
@@ -444,12 +452,14 @@ int t_continue(unsigned int hash_index, unsigned int label,
t->uac[branch].reply = 0;
}
- t->flags &= ~T_ASYNC_SUSPENDED; /*This transaction is no longer suspended so unsetting the SUSPEND flag*/
+ /*This transaction is no longer suspended so unsetting the SUSPEND flag*/
+ t->flags &= ~T_ASYNC_SUSPENDED;
return 0;
kill_trans:
+ t->flags &= ~T_ASYNC_SUSPENDED;
/* The script has hopefully set the error code. If not,
* let us reply with a default error. */
if ((kill_transaction_unsafe(t,
Hello,
(cross-posting as it is relevant to user community as well).
I just pushed a bunch of patches to master branch that will allow
selecting the memory manger at kamailio startup, no longer needed to
re-compile sources. There are two new command line parameters:
* -x name: select the memory manager for shm
* -X name: select the memory manager for pkg
* if -x is provided but -X is not provided, then pkg uses same
memory manager as shm
* name can be:
* fm - fast malloc (f_malloc)
* qm - quick malloc (q_malloc)
* default memory manager is fm (what was default so far)
Both f_malloc and q_malloc are now compiled, selecting one or the other
being possible. Other managers expected to be added in the list soon,
especially TLSF, added not that long ago by Camille Oudot. Maybe
reviving Doug Lea malloc (existing in dl_malloc.c) worth a try now that
can be easily used.
The debugging mode for each memory manager is controlled by DB_SR_MEMORY
define via Makefile.defs -- right now it is enabled. All enabled memory
mangers must be compiled either with or without DB_SR_MEMORY (it won't
work to have one memory manger with DB_SR_MEMORY and another one without).
Using system malloc for private (pkg) memory is possible by removing the
define of PKG_MALLOC from Makefile.defs (same as it was before). This is
a mode suitable for troubleshooting using external tools such as
valgrind, because it does inline replacement of pkg_malloc() with
malloc(), so those tools can track the place of allocation. I plan to
add a system malloc custom manager, to allow selecting it with -X
(system malloc won't work for shm management, because it is only for
private memory).
There is no performance impact at runtime, it is only the startup
procedure that replaces the pointers to memory management functions.
Hopefully with the new mechanism will be able to track easier memory
issues -- e.g., qm malloc is better for troubleshooting, using it no
longer requires recompilation, just starting kamailio with -x qm.
Feedback, especially testing, will be very appreciated!
Cheers,
Daniel
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Book: SIP Routing With Kamailio - http://www.asipto.com
Kamailio Advanced Training, Sep 28-30, 2015, in Berlin - http://asipto.com/u/kat