diff mbox series

[U/N,2/2] UBUNTU: [Packaging] provide a wrapper module for python-perf

Message ID 20240322154631.804565-3-andrea.righi@canonical.com
State New
Headers show
Series Provide python perf module | expand

Commit Message

Andrea Righi March 22, 2024, 3:42 p.m. UTC
BugLink: https://bugs.launchpad.net/bugs/2051560

Provide a virtual python module wrapper to load the actual versioned
python perf module, based on the running kernel version.

Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
---
 debian/rules.d/2-binary-arch.mk  |  3 ++-
 debian/rules.d/3-binary-indep.mk |  7 +++++++
 debian/tools/python-perf.py      | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 debian/tools/python-perf.py
diff mbox series

Patch

diff --git a/debian/rules.d/2-binary-arch.mk b/debian/rules.d/2-binary-arch.mk
index 0e9e389e57e3..84c2e13bfb04 100644
--- a/debian/rules.d/2-binary-arch.mk
+++ b/debian/rules.d/2-binary-arch.mk
@@ -703,7 +703,8 @@  ifeq ($(do_tools_perf_jvmti),true)
 	install -m755 $(builddirpa)/tools/perf/libperf-jvmti.so $(toolspkgdir)/usr/lib/$(src_pkg_name)-tools-$(abi_release)
 endif
 ifeq ($(do_tools_perf_python),true)
-	install -m755 $(builddirpa)/tools/perf/python/perf*.so $(toolspkgdir)/usr/lib/python3/dist-packages
+	install -d $(toolspkgdir)/usr/lib/python3/dist-packages/$(src_pkg_name)-tools-$(abi_release)
+	install -m755 $(builddirpa)/tools/perf/python/perf*.so $(toolspkgdir)/usr/lib/python3/dist-packages/$(src_pkg_name)-tools-$(abi_release)
 endif
 endif
 ifeq ($(do_tools_bpftool),true)
diff --git a/debian/rules.d/3-binary-indep.mk b/debian/rules.d/3-binary-indep.mk
index c34161332257..efe66df01d8b 100644
--- a/debian/rules.d/3-binary-indep.mk
+++ b/debian/rules.d/3-binary-indep.mk
@@ -55,6 +55,7 @@  install-tools: toolspkg = $(tools_common_pkg_name)
 install-tools: toolsbin = $(CURDIR)/debian/$(toolspkg)/usr/bin
 install-tools: toolssbin = $(CURDIR)/debian/$(toolspkg)/usr/sbin
 install-tools: toolsman = $(CURDIR)/debian/$(toolspkg)/usr/share/man
+install-tools: toolspython = $(CURDIR)/debian/$(toolspkg)/usr/lib/python3/dist-packages
 install-tools: toolsbashcomp = $(CURDIR)/debian/$(toolspkg)/usr/share/bash-completion/completions
 install-tools: hosttoolspkg = $(hosttools_pkg_name)
 install-tools: hosttoolsbin = $(CURDIR)/debian/$(hosttoolspkg)/usr/bin
@@ -79,6 +80,7 @@  ifeq ($(do_tools_common),true)
 	install -d $(toolsman)/man1
 	install -d $(toolsman)/man8
 	install -d $(toolsbashcomp)
+	install -d $(toolspython)
 
 	install -m755 debian/tools/generic $(toolsbin)/usbip
 	install -m755 debian/tools/generic $(toolsbin)/usbipd
@@ -106,6 +108,11 @@  ifeq ($(do_tools_common),true)
 	install -m644 $(CURDIR)/tools/power/x86/x86_energy_perf_policy/*.8 $(toolsman)/man8
 	install -m644 $(CURDIR)/tools/power/x86/turbostat/*.8 $(toolsman)/man8
 
+ifeq ($(do_tools_perf_python),true)
+	# Python wrapper module for python-perf
+	install -d $(toolspython)/perf
+	install -m755 debian/tools/python-perf.py $(toolspython)/perf/__init__.py
+endif
 ifeq ($(do_cloud_tools),true)
 ifeq ($(do_tools_hyperv),true)
 	install -d $(cloudsbin)
diff --git a/debian/tools/python-perf.py b/debian/tools/python-perf.py
new file mode 100644
index 000000000000..d572fece82aa
--- /dev/null
+++ b/debian/tools/python-perf.py
@@ -0,0 +1,32 @@ 
+import os
+import importlib.util
+from glob import glob
+
+class KernelNotFoundError(Exception):
+    def __init__(self):
+        kernel_version = os.uname().release
+        super().__init__(f"WARNING: python perf module not found for kernel {kernel_version}\n\n"
+                         f"You may need to install the following packages for this specific kernel:\n"
+                         f"  linux-tools-{kernel_version}-generic\n"
+                         f"You may also want to install of the following package to keep up to date:\n"
+                         f"  linux-tools-generic")
+
+# Extract ABI number from kernel version
+def _get_abi_version():
+    _kernel_version = os.uname().release
+    _parts = _kernel_version.split("-")
+    return "-".join(_parts[:-1])
+
+# Load the actual python-perf module for the running kernel
+_abi_version = _get_abi_version()
+_perf_dir = f"/usr/lib/python3/dist-packages/linux-tools-{_abi_version}"
+if not os.path.exists(_perf_dir):
+    raise KernelNotFoundError()
+_perf_lib = glob(os.path.join(_perf_dir, "*.so"))[-1]
+
+_spec = importlib.util.spec_from_file_location("perf", _perf_lib)
+_perf = importlib.util.module_from_spec(_spec)
+_spec.loader.exec_module(_perf)
+
+# Expose the 'perf' module.
+__all__ = ['perf']