Skip to contentSkip to conversation

trace-cruncher: Add find_instance()

PATCH New Yordan Karadzhov (VMware) 1 participant 2 messages Sep 9, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 trace-cruncher: Add find_instance()

The method allows create a Python instance object from an already existing tracefs instance. The instance returned by the method is detached from the Python module.

---
 src/ftracepy-utils.c                          | 40 +++++++++++++++++--
 src/ftracepy-utils.h                          |  3 ++
 src/ftracepy.c                                |  5 +++
 .../tests/1_unit/test_01_ftracepy_unit.py     | 12 ++++++
 4 files changed, 57 insertions(+), 3 deletions(-)
src/ftracepy-utils.c +37 -3
index b94745d..bf811ae 100644
@@ -501,10 +501,15 @@ PyObject *PyFtrace_dir(PyObject *self)
 	return PyUnicode_FromString(tracefs_tracing_dir());
 }
 
+static void set_destroy_flag(PyObject *py_obj, bool val)
+{
+	PyFtrace_Object_HEAD *obj_head = (PyFtrace_Object_HEAD *)py_obj;
+	obj_head->destroy = val;
+}
+
 PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs)
 {
 	static char *kwlist[] = {"object", NULL};
-	PyFtrace_Object_HEAD *obj_head;
 	PyObject *py_obj = NULL;
 
 	if (!PyArg_ParseTupleAndKeywords(args,
@@ -515,8 +520,7 @@ PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs)
 		return false;
 	}
 
-	obj_head = (PyFtrace_Object_HEAD *)py_obj;
-	obj_head->destroy = false;
+	set_destroy_flag(py_obj, false);
 
 	Py_RETURN_NONE;
 }
@@ -583,6 +587,36 @@ PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args,
 	return PyTfsInstance_New(instance);
 }
 
+PyObject *PyFtrace_find_instance(PyObject *self, PyObject *args,
+						 PyObject *kwargs)
+{
+	struct tracefs_instance *instance;
+	PyObject *py_inst;
+	const char *name;
+
+	static char *kwlist[] = {"name", NULL};
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "s",
+					 kwlist,
+					 &name)) {
+		return NULL;
+	}
+
+	instance = tracefs_instance_alloc(NULL, name);
+	if (!instance) {
+		TfsError_fmt(instance,
+			     "Failed to find trace instance \'%s\'.",
+			     name);
+		return NULL;
+	}
+
+	py_inst = PyTfsInstance_New(instance);
+	set_destroy_flag(py_inst, false);
+
+	return py_inst;
+}
+
 static bool get_optional_instance(PyObject *py_obj,
 				  struct tracefs_instance **instance)
 {
src/ftracepy-utils.h +3 -0
index 218dcb3..c1eec21 100644
@@ -86,6 +86,9 @@ PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs);
 PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args,
 						   PyObject *kwargs);
 
+PyObject *PyFtrace_find_instance(PyObject *self, PyObject *args,
+						 PyObject *kwargs);
+
 PyObject *PyFtrace_available_tracers(PyObject *self, PyObject *args,
 						     PyObject *kwargs);
 
src/ftracepy.c +5 -0
index f03c488..e48adb6 100644
@@ -155,6 +155,11 @@ static PyMethodDef ftracepy_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Create new tracefs instance."
 	},
+	{"find_instance",
+	 (PyCFunction) PyFtrace_find_instance,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Find an existing ftrace instance."
+	},
 	{"available_tracers",
 	 (PyCFunction) PyFtrace_available_tracers,
 	 METH_VARARGS | METH_KEYWORDS,
tracecruncher/tests/1_unit/test_01_ftracepy_unit.py +12 -0
index 710d3e1..a7010e2 100644
@@ -36,6 +36,18 @@ class InstanceTestCase(unittest.TestCase):
         instance_dir = tracefs_dir + '/instances/' + instance_name
         self.assertEqual(instance_dir, inst.dir())
 
+    def test_find(self):
+        inst = ft.create_instance(instance_name)
+        tracefs_dir = ft.dir();
+        instance_dir = tracefs_dir + '/instances/' + instance_name
+        inst_1 = ft.find_instance(instance_name)
+        self.assertEqual(instance_dir, inst_1.dir())
+
+        err='Failed to find trace instance'
+        with self.assertRaises(Exception) as context:
+            inst_2 = ft.find_instance(another_instance_name)
+        self.assertTrue(err in str(context.exception))
+
 
 class PyTepTestCase(unittest.TestCase):
     def test_init_local(self):
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 trace-cruncher: Add attach()

Here we add mechanisms that will make possible to reattach object to the ftracepy module. This means that the object will be properly destroyed when the module exits.

---
 src/ftracepy-utils.c                           | 18 ++++++++++++++----
 src/ftracepy-utils.h                           |  2 ++
 src/ftracepy.c                                 |  5 +++++
 .../tests/1_unit/test_01_ftracepy_unit.py      | 13 +++++++++++++
 4 files changed, 34 insertions(+), 4 deletions(-)
src/ftracepy-utils.c +14 -4
index bf811ae..d4fa59e 100644
@@ -507,24 +507,34 @@ static void set_destroy_flag(PyObject *py_obj, bool val)
 	obj_head->destroy = val;
 }
 
-PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs)
+static PyObject *set_destroy(PyObject *args, PyObject *kwargs, bool destroy)
 {
 	static char *kwlist[] = {"object", NULL};
-	PyObject *py_obj = NULL;
+	PyObject *py_obj;
 
 	if (!PyArg_ParseTupleAndKeywords(args,
 					 kwargs,
 					 "O",
 					 kwlist,
 					 &py_obj)) {
-		return false;
+		return NULL;
 	}
 
-	set_destroy_flag(py_obj, false);
+	set_destroy_flag(py_obj, destroy);
 
 	Py_RETURN_NONE;
 }
 
+PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+	return set_destroy(args, kwargs, false);
+}
+
+PyObject *PyFtrace_attach(PyObject *self, PyObject *args, PyObject *kwargs)
+{
+	return set_destroy(args, kwargs, true);
+}
+
 static char aname_pool[] =
 	"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
src/ftracepy-utils.h +2 -0
index c1eec21..be1b37c 100644
@@ -83,6 +83,8 @@ PyObject *PyFtrace_dir(PyObject *self);
 
 PyObject *PyFtrace_detach(PyObject *self, PyObject *args, PyObject *kwargs);
 
+PyObject *PyFtrace_attach(PyObject *self, PyObject *args, PyObject *kwargs);
+
 PyObject *PyFtrace_create_instance(PyObject *self, PyObject *args,
 						   PyObject *kwargs);
 
src/ftracepy.c +5 -0
index e48adb6..763a7d2 100644
@@ -150,6 +150,11 @@ static PyMethodDef ftracepy_methods[] = {
 	 METH_VARARGS | METH_KEYWORDS,
 	 "Detach object from the \'ftracepy\' module."
 	},
+	{"attach",
+	 (PyCFunction) PyFtrace_attach,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Attach object to the \'ftracepy\' module."
+	},
 	{"create_instance",
 	 (PyCFunction) PyFtrace_create_instance,
 	 METH_VARARGS | METH_KEYWORDS,
tracecruncher/tests/1_unit/test_01_ftracepy_unit.py +13 -0
index a7010e2..d3e3960 100644
@@ -48,6 +48,19 @@ class InstanceTestCase(unittest.TestCase):
             inst_2 = ft.find_instance(another_instance_name)
         self.assertTrue(err in str(context.exception))
 
+    def test_1_detach(self):
+        inst = ft.create_instance(instance_name)
+        ft.detach(inst)
+
+    def test_2_attach(self):
+        inst = ft.find_instance(instance_name)
+        ft.attach(inst)
+
+    def test_3_attach(self):
+        tracefs_dir = ft.dir();
+        instance_dir = tracefs_dir + '/instances/' + instance_name
+        self.assertFalse(os.path.isdir(instance_dir))
+
 
 class PyTepTestCase(unittest.TestCase):
     def test_init_local(self):
Signature · 1 line
2.30.2