diff mbox series

[02/10] trace-cruncher: APIs for adding start/end fields to synth. event

Message ID 20220124085625.92297-3-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_start_fields()
add_end_fields()

The new APIs allows for adding fields from the original 'start' and
'end' events to the new synthetic event.

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

Patch

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 1fcf2f8..42bd9d0 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -1009,6 +1009,70 @@  PyObject *PyTraceHist_close(PyTraceHist *self, PyObject *args,
 	Py_RETURN_NONE;
 }
 
+typedef int (*add_field_ptr)(struct tracefs_synth *,
+			     const char *,
+			     const char *);
+
+PyObject *synth_add_fields(PySynthEvent *self, PyObject *args,
+					       PyObject *kwargs,
+					       bool to_start)
+{
+	static char *kwlist[] = {"fields", "names", NULL};
+	PyObject *py_fields, *py_names = NULL;
+	PyObject *item_field, *item_name;
+	add_field_ptr add_field_func;
+	const char *field, *name;
+	int ret, n, i;
+
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "O|O",
+					 kwlist,
+					 &py_fields,
+					 &py_names)) {
+		return NULL;
+	}
+
+	add_field_func = to_start ? tracefs_synth_add_start_field:
+				    tracefs_synth_add_end_field;
+
+	n = PyList_Size(py_fields);
+	for (i = 0; i < n; ++i) {
+		item_field = PyList_GetItem(py_fields, i);
+		field = PyUnicode_DATA(item_field);
+
+		name = NULL;
+		if (py_names) {
+			item_name = PyList_GetItem(py_names, i);
+			if (item_name && item_name != Py_None)
+				name = PyUnicode_DATA(item_name);
+		}
+
+		ret = add_field_func(self->ptrObj, field, name);
+		if (ret < 0) {
+			TfsError_fmt(NULL,
+				     "Failed to add %s field '%s' to synth. event %s",
+				     to_start ? "start" : "end", field,
+				     tracefs_synth_get_name(self->ptrObj));
+			return NULL;
+		}
+	}
+
+	Py_RETURN_NONE;
+}
+
+PyObject *PySynthEvent_add_start_fields(PySynthEvent *self, PyObject *args,
+							    PyObject *kwargs)
+{
+	return synth_add_fields(self, args, kwargs, true);
+}
+
+PyObject *PySynthEvent_add_end_fields(PySynthEvent *self, PyObject *args,
+							  PyObject *kwargs)
+{
+	return synth_add_fields(self, args, kwargs, false);
+}
+
 PyObject *PyFtrace_dir(PyObject *self)
 {
 	return PyUnicode_FromString(tracefs_tracing_dir());
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index e8db811..3c6c0f3 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -121,6 +121,12 @@  PyObject *PyTraceHist_read(PyTraceHist *self, PyObject *args,
 PyObject *PyTraceHist_close(PyTraceHist *self, PyObject *args,
 					       PyObject *kwargs);
 
+PyObject *PySynthEvent_add_start_fields(PySynthEvent *self, PyObject *args,
+							    PyObject *kwargs);
+
+PyObject *PySynthEvent_add_end_fields(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 f8b8b36..44114f0 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -222,6 +222,16 @@  C_OBJECT_WRAPPER(tracefs_hist, PyTraceHist,
 		 tracefs_hist_free)
 
 static PyMethodDef PySynthEvent_methods[] = {
+	{"add_start_fields",
+	 (PyCFunction) PySynthEvent_add_start_fields,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Add fields from the start event to save."
+	},
+	{"add_end_fields",
+	 (PyCFunction) PySynthEvent_add_end_fields,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Add fields from the end event to save."
+	},
 	{NULL, NULL, 0, NULL}
 };