diff mbox series

[03/10] trace-cruncher: APIs for adding arithmetic fields to synth. events

Message ID 20220124085625.92297-4-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series trace-cruncher: Synthetic events | expand

Commit Message

Yordan Karadzhov Jan. 24, 2022, 8:56 a.m. UTC
Here we add the following methods to the Python type for synthetic
events:
add_delta_start(),
add_delta_end(),
add_delta_T(),
add_sum()

The new APIs allows for adding to the new synthetic event, fields that
are calculated from the fields of the original 'start' and 'end' events
by using simple addition or subtraction.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/ftracepy-utils.c | 81 ++++++++++++++++++++++++++++++++++++++++++++
 src/ftracepy-utils.h | 12 +++++++
 src/ftracepy.c       | 20 +++++++++++
 3 files changed, 113 insertions(+)
diff mbox series

Patch

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 42bd9d0..9245b07 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -1073,6 +1073,87 @@  PyObject *PySynthEvent_add_end_fields(PySynthEvent *self, PyObject *args,
 	return synth_add_fields(self, args, kwargs, false);
 }
 
+static inline void add_synth_field_err(const char *field, const char *event)
+{
+	TfsError_fmt(NULL, "Failed to add field '%s' to synth. event %s",
+		     field, event);
+}
+
+static inline PyObject *
+add_synth_field(PySynthEvent *self, PyObject *args, PyObject *kwargs,
+		enum tracefs_synth_calc calc)
+{
+	static char *kwlist[] = {"name", "start_field", "end_field", NULL};
+	const char *start_field, *end_field, *name;
+	int ret;
+
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "sss",
+					 kwlist,
+					 &name,
+					 &start_field,
+					 &end_field)) {
+		return NULL;
+	}
+
+	ret = tracefs_synth_add_compare_field(self->ptrObj,
+					      start_field, end_field, calc,
+					      name);
+	if (ret < 0) {
+		add_synth_field_err(name, tracefs_synth_get_name(self->ptrObj));
+		return NULL;
+	}
+
+	Py_RETURN_NONE;
+}
+
+PyObject *PySynthEvent_add_delta_start(PySynthEvent *self, PyObject *args,
+							   PyObject *kwargs)
+{
+	return add_synth_field(self, args, kwargs, TRACEFS_SYNTH_DELTA_START);
+}
+
+PyObject *PySynthEvent_add_delta_end(PySynthEvent *self, PyObject *args,
+							 PyObject *kwargs)
+{
+	return add_synth_field(self, args, kwargs, TRACEFS_SYNTH_DELTA_END);
+}
+
+PyObject *PySynthEvent_add_sum(PySynthEvent *self, PyObject *args,
+						   PyObject *kwargs)
+{
+	return add_synth_field(self, args, kwargs, TRACEFS_SYNTH_ADD);
+}
+
+PyObject *PySynthEvent_add_delta_T(PySynthEvent *self, PyObject *args,
+						       PyObject *kwargs)
+{
+	static char *kwlist[] = {"name", "hd", NULL};
+	const char *name = "delta_T";
+	const char* time_rez;
+	int ret, hd = 0;
+
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "|sp",
+					 kwlist,
+					 &name,
+					 &hd)) {
+		return NULL;
+	}
+
+	time_rez = hd ? TRACEFS_TIMESTAMP : TRACEFS_TIMESTAMP_USECS;
+	ret = tracefs_synth_add_compare_field(self->ptrObj, time_rez, time_rez,
+					      TRACEFS_SYNTH_DELTA_END, name);
+	if (ret < 0) {
+		add_synth_field_err(name, tracefs_synth_get_name(self->ptrObj));
+		return NULL;
+	}
+
+	Py_RETURN_NONE;
+}
+
 PyObject *PyFtrace_dir(PyObject *self)
 {
 	return PyUnicode_FromString(tracefs_tracing_dir());
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index 3c6c0f3..7b798fc 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -127,6 +127,18 @@  PyObject *PySynthEvent_add_start_fields(PySynthEvent *self, PyObject *args,
 PyObject *PySynthEvent_add_end_fields(PySynthEvent *self, PyObject *args,
 							  PyObject *kwargs);
 
+PyObject *PySynthEvent_add_delta_start(PySynthEvent *self, PyObject *args,
+							   PyObject *kwargs);
+
+PyObject *PySynthEvent_add_delta_end(PySynthEvent *self, PyObject *args,
+							 PyObject *kwargs);
+
+PyObject *PySynthEvent_add_delta_T(PySynthEvent *self, PyObject *args,
+						       PyObject *kwargs);
+
+PyObject *PySynthEvent_add_sum(PySynthEvent *self, PyObject *args,
+						   PyObject *kwargs);
+
 PyObject *PyFtrace_dir(PyObject *self);
 
 PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs);
diff --git a/src/ftracepy.c b/src/ftracepy.c
index 44114f0..066fc65 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -232,6 +232,26 @@  static PyMethodDef PySynthEvent_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Add fields from the end event to save."
 	},
+	{"add_delta_start",
+	 (PyCFunction) PySynthEvent_add_delta_start,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Add 'start - end' field."
+	},
+	{"add_delta_end",
+	 (PyCFunction) PySynthEvent_add_delta_end,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Add 'end - start' field."
+	},
+	{"add_delta_T",
+	 (PyCFunction) PySynthEvent_add_delta_T,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Add time-difference field."
+	},
+	{"add_sum",
+	 (PyCFunction) PySynthEvent_add_delta_T,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Add 'start + end' field."
+	},
 	{NULL, NULL, 0, NULL}
 };