diff mbox series

[v2,4/7] trace-cruncher: Define Python type for instances

Message ID 20210819130827.12327-5-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series trace-cruncher: Refactor instances and kprobes | expand

Commit Message

Yordan Karadzhov Aug. 19, 2021, 1:08 p.m. UTC
We add a custom Python type that wraps the 'tracefs_instance' object,
defined in libtracefs. This is a preparation for a general refactorung
of the way libtracefs instances are handled by trace-cruncher.

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

Patch

diff --git a/src/ftracepy-utils.c b/src/ftracepy-utils.c
index 197804b..a75f19b 100644
--- a/src/ftracepy-utils.c
+++ b/src/ftracepy-utils.c
@@ -531,6 +531,11 @@  bool get_instance_from_arg(PyObject *args, PyObject *kwargs,
 	return true;
 }
 
+PyObject *PyTfsInstance_dir(PyTfsInstance *self)
+{
+	return PyUnicode_FromString(tracefs_instance_get_dir(self->ptrObj));
+}
+
 PyObject *PyFtrace_dir(PyObject *self)
 {
 	return PyUnicode_FromString(tracefs_tracing_dir());
diff --git a/src/ftracepy-utils.h b/src/ftracepy-utils.h
index 514c79b..c674804 100644
--- a/src/ftracepy-utils.h
+++ b/src/ftracepy-utils.h
@@ -22,6 +22,8 @@  C_OBJECT_WRAPPER_DECLARE(tep_event, PyTepEvent)
 
 C_OBJECT_WRAPPER_DECLARE(tep_handle, PyTep)
 
+C_OBJECT_WRAPPER_DECLARE(tracefs_instance, PyTfsInstance)
+
 PyObject *PyTepRecord_time(PyTepRecord* self);
 
 PyObject *PyTepRecord_cpu(PyTepRecord* self);
@@ -44,6 +46,8 @@  PyObject *PyTep_init_local(PyTep *self, PyObject *args,
 PyObject *PyTep_get_event(PyTep *self, PyObject *args,
 				       PyObject *kwargs);
 
+PyObject *PyTfsInstance_dir(PyTfsInstance *self);
+
 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 2296ec9..97b410f 100644
--- a/src/ftracepy.c
+++ b/src/ftracepy.c
@@ -73,6 +73,19 @@  static PyMethodDef PyTep_methods[] = {
 
 C_OBJECT_WRAPPER(tep_handle, PyTep, NO_DESTROY, tep_free)
 
+static PyMethodDef PyTfsInstance_methods[] = {
+	{"dir",
+	 (PyCFunction) PyTfsInstance_dir,
+	 METH_NOARGS,
+	 "Get the absolute path to the instance directory."
+	},
+	{NULL, NULL, 0, NULL}
+};
+
+C_OBJECT_WRAPPER(tracefs_instance, PyTfsInstance,
+		 tracefs_instance_destroy,
+		 tracefs_instance_free)
+
 static PyMethodDef ftracepy_methods[] = {
 	{"dir",
 	 (PyCFunction) PyFtrace_dir,
@@ -326,6 +339,9 @@  PyMODINIT_FUNC PyInit_ftracepy(void)
 	if (!PyTepRecordTypeInit())
 		return NULL;
 
+	if (!PyTfsInstanceTypeInit())
+		return NULL;
+
 	TFS_ERROR = PyErr_NewException("tracecruncher.ftracepy.tfs_error",
 				       NULL, NULL);
 
@@ -340,6 +356,7 @@  PyMODINIT_FUNC PyInit_ftracepy(void)
 	PyModule_AddObject(module, "tep_handle", (PyObject *) &PyTepType);
 	PyModule_AddObject(module, "tep_event", (PyObject *) &PyTepEventType);
 	PyModule_AddObject(module, "tep_record", (PyObject *) &PyTepRecordType);
+	PyModule_AddObject(module, "tracefs_instance", (PyObject *) &PyTfsInstanceType);
 
 	PyModule_AddObject(module, "tfs_error", TFS_ERROR);
 	PyModule_AddObject(module, "tep_error", TEP_ERROR);