[sr-dev] git:master:a300cf00: app_python: use system time to compute execution duration

Daniel-Constantin Mierla miconda at gmail.com
Sun Mar 31 12:55:28 CEST 2019


Module: kamailio
Branch: master
Commit: a300cf008b86dec22a1006328ab73c3ce399c6c3
URL: https://github.com/kamailio/kamailio/commit/a300cf008b86dec22a1006328ab73c3ce399c6c3

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2019-03-31T12:50:14+02:00

app_python: use system time to compute execution duration

---

Modified: src/modules/app_python/apy_kemi.c

---

Diff:  https://github.com/kamailio/kamailio/commit/a300cf008b86dec22a1006328ab73c3ce399c6c3.diff
Patch: https://github.com/kamailio/kamailio/commit/a300cf008b86dec22a1006328ab73c3ce399c6c3.patch

---

diff --git a/src/modules/app_python/apy_kemi.c b/src/modules/app_python/apy_kemi.c
index 7a3717ecce..f89ff5252a 100644
--- a/src/modules/app_python/apy_kemi.c
+++ b/src/modules/app_python/apy_kemi.c
@@ -706,24 +706,29 @@ PyObject *sr_apy_kemi_exec_func(PyObject *self, PyObject *args, int idx)
 {
 	sr_kemi_t *ket = NULL;
 	PyObject *ret = NULL;
-	unsigned int ms = 0;
 	PyThreadState *pstate = NULL;
 	PyFrameObject *pframe = NULL;
+	struct timeval tvb, tve;
+	struct timezone tz;
+	unsigned int tdiff;
 
 	ket = sr_apy_kemi_export_get(idx);
 	if(ket==NULL) {
 		return sr_kemi_apy_return_false();
 	}
-	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)) {
-			ms = TICKS_TO_MS(get_ticks_raw());
+	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)
+			&& is_printable(cfg_get(core, core_cfg, latency_log))) {
+		gettimeofday(&tvb, &tz);
 	}
 
 	ret = sr_apy_kemi_exec_func_ex(ket, self, args, idx);
 
-	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)) {
-		ms = TICKS_TO_MS(get_ticks_raw()) - ms;
-		if(ms >= cfg_get(core, core_cfg, latency_limit_action)
-				&& is_printable(cfg_get(core, core_cfg, latency_log))) {
+	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)
+			&& is_printable(cfg_get(core, core_cfg, latency_log))) {
+		gettimeofday(&tve, &tz);
+		tdiff = (tve.tv_sec - tvb.tv_sec) * 1000000
+				   + (tve.tv_usec - tvb.tv_usec);
+		if(tdiff >= cfg_get(core, core_cfg, latency_limit_action)) {
 			pstate = PyThreadState_GET();
 			if (pstate != NULL && pstate->frame != NULL) {
 				pframe = pstate->frame;
@@ -731,9 +736,9 @@ PyObject *sr_apy_kemi_exec_func(PyObject *self, PyObject *args, int idx)
 
 			LOG(cfg_get(core, core_cfg, latency_log),
 					"alert - action KSR.%s%s%s(...)"
-					" took too long [%u ms] (file:%s func:%s line:%d)\n",
+					" took too long [%u us] (file:%s func:%s line:%d)\n",
 					(ket->mname.len>0)?ket->mname.s:"",
-					(ket->mname.len>0)?".":"", ket->fname.s, ms,
+					(ket->mname.len>0)?".":"", ket->fname.s, tdiff,
 					(pframe)?PyString_AsString(pframe->f_code->co_filename):"",
 					(pframe)?PyString_AsString(pframe->f_code->co_name):"",
 					(pframe)?PyCode_Addr2Line(pframe->f_code, pframe->f_lasti):0);




More information about the sr-dev mailing list