diff mbox series

[08/10] trace-cruncher: API to show descriptor of the synth. event

Message ID 20220124085625.92297-9-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 method to the Python type for synthetic
events:
repr()

The new APIs provides a representative descriptor of the synth. event,
including the dynamic event and the two histograms. It can be useful
in the case where the user wants to check what exactly gets passed to
the kernel as definition of a synth event.

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

Patch

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 43c0f07..8c46590 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -1192,6 +1192,58 @@  PyObject *PySynthEvent_unregister(PySynthEvent *self)
 	Py_RETURN_NONE;
 }
 
+PyObject *PySynthEvent_repr(PySynthEvent *self, PyObject *args, PyObject *kwargs)
+{
+	static char *kwlist[] = {"event", "hist_start", "hist_end", NULL};
+	int event, hist_start, hist_end;
+	char buff[2048] = {0};
+	bool new_line = false;
+	const char *str = NULL;
+
+	event = hist_start = hist_end = true;
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "|ppp",
+					 kwlist,
+					 &event,
+					 &hist_start,
+					 &hist_end)) {
+		return NULL;
+	}
+
+	if (event) {
+		strcat(buff, "synth. event: ");
+		str = tracefs_synth_show_event(self->ptrObj);
+		if (str)
+			strcat(buff, str);
+		new_line = true;
+	}
+
+	if (hist_start) {
+		if (new_line)
+			strcat(buff, "\n");
+		else
+			new_line = true;
+
+		strcat(buff, "hist. start: ");
+		str = tracefs_synth_show_start_hist(self->ptrObj);
+		if (str)
+			strcat(buff, str);
+	}
+
+	if (hist_end) {
+		if (new_line)
+			strcat(buff, "\n");
+
+		strcat(buff, "hist. end: ");
+		str = tracefs_synth_show_end_hist(self->ptrObj);
+		if (str)
+			strcat(buff, str);
+	}
+
+	return PyUnicode_FromString(strdup(buff));
+}
+
 PyObject *PyFtrace_dir(PyObject *self)
 {
 	return PyUnicode_FromString(tracefs_tracing_dir());
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index f31b330..7612df6 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -161,6 +161,8 @@  PyObject *PySynthEvent_get_filter(PySynthEvent *self, PyObject *args,
 PyObject *PySynthEvent_clear_filter(PySynthEvent *self, PyObject *args,
 							PyObject *kwargs);
 
+PyObject *PySynthEvent_repr(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 50675c0..3f71b5e 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -292,6 +292,11 @@  static PyMethodDef PySynthEvent_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Clear the filter of a synthetic event."
 	},
+	{"repr",
+	 (PyCFunction) PySynthEvent_repr,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Show a representative descriptor of the synth. event."
+	},
 	{NULL, NULL, 0, NULL}
 };