Skip to contentSkip to conversation

trace-cruncher: Add optional sort to tfs_list2py_list()

PATCH New Yordan Karadzhov (VMware) 1 participant 4 messages Aug 31, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 trace-cruncher: Add optional sort to tfs_list2py_list()

This helper function is used to convert the NULL-terminated array of strings returned by the APIs of libtracefs into a Python list of strings. Here we add an option the returned string to be sorted in alphabetical order.

---
 src/ftracepy-utils.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
src/ftracepy-utils.c +8 -4
index 53fc95f..0fcadd8 100644
@@ -475,7 +475,7 @@ static bool write_to_file_and_check(struct tracefs_instance *instance,
 	return ret == 0 ? true : false;
 }
 
-static PyObject *tfs_list2py_list(char **list)
+static PyObject *tfs_list2py_list(char **list, bool sort)
 {
 	PyObject *py_list = PyList_New(0);
 	int i;
@@ -483,6 +483,9 @@ static PyObject *tfs_list2py_list(char **list)
 	for (i = 0; list && list[i]; i++)
 		PyList_Append(py_list, PyUnicode_FromString(list[i]));
 
+	if (sort)
+		PyList_Sort(py_list);
+
 	tracefs_list_free(list);
 
 	return py_list;
@@ -636,7 +639,7 @@ PyObject *PyFtrace_available_tracers(PyObject *self, PyObject *args,
 	if (!list)
 		return NULL;
 
-	return tfs_list2py_list(list);
+	return tfs_list2py_list(list, false);
 }
 
 PyObject *PyFtrace_set_current_tracer(PyObject *self, PyObject *args,
@@ -724,7 +727,7 @@ PyObject *PyFtrace_available_event_systems(PyObject *self, PyObject *args,
 	if (!list)
 		return NULL;
 
-	return tfs_list2py_list(list);
+	return tfs_list2py_list(list, false);
 }
 
 PyObject *PyFtrace_available_system_events(PyObject *self, PyObject *args,
@@ -750,10 +753,11 @@ PyObject *PyFtrace_available_system_events(PyObject *self, PyObject *args,
 
 	list = tracefs_system_events(tracefs_instance_get_dir(instance),
 				     system);
+
 	if (!list)
 		return NULL;
 
-	return tfs_list2py_list(list);
+	return tfs_list2py_list(list, false);
 }
 
 bool get_event_enable_file(struct tracefs_instance *instance,
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/4 trace-cruncher: Add optional sort to available_system_events()

An optional parameter 'sort' is added. If set to True, the returned list of event names is sorted in alphabetical order. The default value of 'sort' is False.

---
 src/ftracepy-utils.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/ftracepy-utils.c +6 -4
index 0fcadd8..f884106 100644
@@ -733,18 +733,20 @@ PyObject *PyFtrace_available_event_systems(PyObject *self, PyObject *args,
 PyObject *PyFtrace_available_system_events(PyObject *self, PyObject *args,
 							   PyObject *kwargs)
 {
-	static char *kwlist[] = {"system", "instance", NULL};
+	static char *kwlist[] = {"system", "instance", "sort", NULL};
 	struct tracefs_instance *instance;
 	PyObject *py_inst = NULL;
 	const char *system;
+	int sort = false;
 	char **list;
 
 	if (!PyArg_ParseTupleAndKeywords(args,
 					 kwargs,
-					 "s|O",
+					 "s|Op",
 					 kwlist,
 					 &system,
-					 &py_inst)) {
+					 &py_inst,
+					 &sort)) {
 		return NULL;
 	}
 
@@ -757,7 +759,7 @@ PyObject *PyFtrace_available_system_events(PyObject *self, PyObject *args,
 	if (!list)
 		return NULL;
 
-	return tfs_list2py_list(list, false);
+	return tfs_list2py_list(list, sort);
 }
 
 bool get_event_enable_file(struct tracefs_instance *instance,
Signature · 1 line
2.30.2
#2 lore raw

PATCH 3/4 trace-cruncher: Add optional sort to available_event_systems()

In reply to Yordan Karadzhov (VMware)

An optional parameter 'sort' is added. If set to True, the returned list of system names is sorted in alphabetical order. The default value of 'sort' is False.

---
 src/ftracepy-utils.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
src/ftracepy-utils.c +14 -2
index f884106..b94745d 100644
@@ -717,17 +717,29 @@ PyObject *PyFtrace_get_current_tracer(PyObject *self, PyObject *args,
 PyObject *PyFtrace_available_event_systems(PyObject *self, PyObject *args,
 							   PyObject *kwargs)
 {
+	static char *kwlist[] = {"instance", "sort", NULL};
 	struct tracefs_instance *instance;
+	PyObject *py_inst = NULL;
+	int sort = false;
 	char **list;
 
-	if (!get_instance_from_arg(args, kwargs, &instance))
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "|Op",
+					 kwlist,
+					 &py_inst,
+					 &sort)) {
+		return false;
+	}
+
+	if (!get_optional_instance(py_inst, &instance))
 		return NULL;
 
 	list = tracefs_event_systems(tracefs_instance_get_dir(instance));
 	if (!list)
 		return NULL;
 
-	return tfs_list2py_list(list, false);
+	return tfs_list2py_list(list, sort);
 }
 
 PyObject *PyFtrace_available_system_events(PyObject *self, PyObject *args,
Signature · 1 line
2.30.2
#3 lore raw

PATCH 4/4 trace-cruncher: Add tests for sorted events/systems

In reply to Yordan Karadzhov (VMware)

An optional parameter 'sort' was added to available_event_systems() and available_system_events(). Here we add the corresponding test cases.

---
 .../tests/1_unit/test_01_ftracepy_unit.py          | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
tracecruncher/tests/1_unit/test_01_ftracepy_unit.py +9 -5
index 971fd5f..710d3e1 100644
@@ -111,19 +111,23 @@ class TracersTestCase(unittest.TestCase):
 
 class EventsTestCase(unittest.TestCase):
     def test_available_systems(self):
-        systems = ft.available_event_systems()
-        self.assertTrue(len(systems) > 1)
+        systems = ft.available_event_systems(sort=True)
+        self.assertTrue(len(systems) > 2)
         self.assertTrue('sched' in systems)
+        for i in range(len(systems) - 2):
+            self.assertTrue(systems[i] < systems[i + 1])
 
         inst = ft.create_instance(instance_name)
         systems = ft.available_event_systems(inst)
-        self.assertTrue(len(systems) > 1)
+        self.assertTrue(len(systems) > 2)
         self.assertTrue('sched' in systems)
 
     def test_available_system_events(self):
-        events = ft.available_system_events(system='sched')
-        self.assertTrue(len(events) > 1)
+        events = ft.available_system_events(system='sched', sort=True)
+        self.assertTrue(len(events) > 2)
         self.assertTrue('sched_switch' in events)
+        for i in range(len(events) - 2):
+            self.assertTrue(events[i] < events[i + 1])
 
         inst = ft.create_instance(instance_name)
         events = ft.available_system_events(instance=inst,
Signature · 1 line
2.30.2