Browse code

app_python3: execution log message based on core latency_limit_action

Daniel-Constantin Mierla authored on 12/03/2019 12:28:22
Showing 1 changed files
... ...
@@ -23,6 +23,7 @@
23 23
 #include <stdlib.h>
24 24
 
25 25
 #include <Python.h>
26
+#include <frameobject.h>
26 27
 
27 28
 #include "../../core/dprint.h"
28 29
 #include "../../core/route.h"
... ...
@@ -30,6 +31,7 @@
30 30
 #include "../../core/kemi.h"
31 31
 #include "../../core/locking.h"
32 32
 #include "../../core/pvar.h"
33
+#include "../../core/timer.h"
33 34
 #include "../../core/mem/pkg.h"
34 35
 #include "../../core/mem/shm.h"
35 36
 #include "../../core/rpc.h"
... ...
@@ -166,12 +168,11 @@ PyObject *sr_apy_kemi_return_str(sr_kemi_t *ket, char *sval, int slen)
166 166
 /**
167 167
  *
168 168
  */
169
-PyObject *sr_apy_kemi_exec_func(PyObject *self, PyObject *args, int idx)
169
+PyObject *sr_apy_kemi_exec_func_ex(sr_kemi_t *ket, PyObject *self, PyObject *args, int idx)
170 170
 {
171 171
 	str fname;
172 172
 	int i;
173 173
 	int ret;
174
-	sr_kemi_t *ket = NULL;
175 174
 	sr_kemi_val_t vps[SR_KEMI_PARAMS_MAX];
176 175
 	sr_apy_env_t *env_P;
177 176
 	sip_msg_t *lmsg = NULL;
... ...
@@ -188,10 +189,6 @@ PyObject *sr_apy_kemi_exec_func(PyObject *self, PyObject *args, int idx)
188 188
 		lmsg = env_P->msg;
189 189
 	}
190 190
 
191
-	ket = sr_apy_kemi_export_get(idx);
192
-	if(ket==NULL) {
193
-		return sr_kemi_apy_return_false();
194
-	}
195 191
 	if(ket->mname.len>0) {
196 192
 		LM_DBG("execution of method: %.*s\n", ket->fname.len, ket->fname.s);
197 193
 	} else {
... ...
@@ -727,6 +724,50 @@ PyObject *sr_apy_kemi_exec_func(PyObject *self, PyObject *args, int idx)
727 727
 /**
728 728
  *
729 729
  */
730
+PyObject *sr_apy_kemi_exec_func(PyObject *self, PyObject *args, int idx)
731
+{
732
+	sr_kemi_t *ket = NULL;
733
+	PyObject *ret = NULL;
734
+	unsigned int ms = 0;
735
+	PyThreadState *pstate = NULL;
736
+	PyFrameObject *pframe = NULL;
737
+
738
+	ket = sr_apy_kemi_export_get(idx);
739
+	if(ket==NULL) {
740
+		return sr_kemi_apy_return_false();
741
+	}
742
+	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)) {
743
+		ms = TICKS_TO_MS(get_ticks_raw());
744
+	}
745
+
746
+	ret = sr_apy_kemi_exec_func_ex(ket, self, args, idx);
747
+
748
+	if(unlikely(cfg_get(core, core_cfg, latency_limit_action)>0)) {
749
+		ms = TICKS_TO_MS(get_ticks_raw()) - ms;
750
+		if(ms >= cfg_get(core, core_cfg, latency_limit_action)
751
+				&& is_printable(cfg_get(core, core_cfg, latency_log))) {
752
+			pstate = PyThreadState_GET();
753
+			if (pstate != NULL && pstate->frame != NULL) {
754
+				pframe = pstate->frame;
755
+			}
756
+
757
+			LOG(cfg_get(core, core_cfg, latency_log),
758
+					"alert - action KSR.%s%s%s(...)"
759
+					" took too long [%u ms] (file:%s func:%s line:%d)\n",
760
+					(ket->mname.len>0)?ket->mname.s:"",
761
+					(ket->mname.len>0)?".":"", ket->fname.s, ms,
762
+					(pframe)?PyString_AsString(pframe->f_code->co_filename):"",
763
+					(pframe)?PyString_AsString(pframe->f_code->co_name):"",
764
+					(pframe)?PyCode_Addr2Line(pframe->f_code, pframe->f_lasti):0);
765
+		}
766
+	}
767
+
768
+	return ret;
769
+}
770
+
771
+/**
772
+ *
773
+ */
730 774
 PyObject *_sr_apy_ksr_module = NULL;
731 775
 PyObject **_sr_apy_ksr_modules_list = NULL;
732 776