From bfff5502d1313d7d801abefa53367ad292c84738 Mon Sep 17 00:00:00 2001 From: Akanksha J N Date: Fri, 26 Jul 2024 16:47:28 +0530 Subject: [PATCH 1/4] perf_pmu_test_enhancement Add test for randomly picking files from sysfs and check if a normal user has write access to those files Signed-off-by: Akanksha J N --- perf/perf_pmu.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/perf/perf_pmu.py b/perf/perf_pmu.py index 13cbf58d3..4494e98d2 100644 --- a/perf/perf_pmu.py +++ b/perf/perf_pmu.py @@ -15,6 +15,8 @@ import os import glob +import random +import re from avocado import Test from avocado.utils import cpu, dmesg, genio, linux_modules, process from avocado import skipIf, skipUnless @@ -82,6 +84,10 @@ def _remove_temp_user(self): if not process.system('id test_pmu', sudo=True, ignore_status=True): process.system('userdel -r test_pmu', sudo=True, ignore_status=True) + def get_random_filenames(self,directory, num_files=3): + files = os.listdir(directory) + random_files = random.sample(files, num_files) + return random_files def _check_kernel_config(self, config_option): # This function checks the kernel configuration with the input @@ -209,6 +215,27 @@ def test_cpu_event_count(self): # cpu events count self._check_count('cpu') + def test_sysfs_events(self): + devices_events = [ 'cpu' , 'hv_24x7' , 'hv_gpci' ] + for type_events in devices_events: + directory_base = '/sys/bus/event_source/devices' + directory = os.path.join(directory_base, type_events, 'events') + print(directory) + random_files = self.get_random_filenames(directory) + self._create_temp_user() + if not process.system('id test_pmu', sudo=True, ignore_status=True): + for file in random_files: + eventdir = os.path.join(directory, file) + result = process.run("su - test_pmu -c 'echo 1 > %s'" % eventdir, + shell=True, ignore_status=True) + output = result.stdout.decode() + result.stderr.decode() + self.log.info("Test passes for %s of %s"%(file , type_events)) + if 'Permission denied' not in output: + self.fail("Able to read %s of %s as normal user"%(file , type_events)) + self._remove_temp_user() + else: + self.log.warn('User test_pmu does not exist, skipping test') + @skipIf(IS_POWER_NV or IS_KVM_GUEST, "This test is for PowerVM") def test_hv_24x7_event_count(self): # This test checks for the sysfs event_source directory and checks for From f19028d31508a224e0e405f5e65505cb75ef3382 Mon Sep 17 00:00:00 2001 From: Akanksha J N Date: Fri, 26 Jul 2024 17:16:35 +0530 Subject: [PATCH 2/4] perf_pmu_test_enhancement - Check processor compat mode in sysfs Add test (test_caps_feat) for checking if sysfs cpu caps reports the processor compatibility mode Signed-off-by: Akanksha J N --- perf/perf_pmu.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/perf/perf_pmu.py b/perf/perf_pmu.py index 4494e98d2..92627b956 100644 --- a/perf/perf_pmu.py +++ b/perf/perf_pmu.py @@ -84,11 +84,23 @@ def _remove_temp_user(self): if not process.system('id test_pmu', sudo=True, ignore_status=True): process.system('userdel -r test_pmu', sudo=True, ignore_status=True) - def get_random_filenames(self,directory, num_files=3): + + def get_random_filenames(self, directory, num_files=3): files = os.listdir(directory) random_files = random.sample(files, num_files) return random_files + def check_ProcessorCompatMode(self, file_path, patterns): + found_patterns = set() + with open(file_path, 'r') as file: + content = file.read() + for pattern in patterns: + if re.search(pattern, content): + found_patterns.add(pattern) + compat_mode = str(found_patterns) + compat_mode = compat_mode.strip('{}').strip("'") + return compat_mode + def _check_kernel_config(self, config_option): # This function checks the kernel configuration with the input # 'config_option' a string. If the required configuration not set then @@ -216,25 +228,33 @@ def test_cpu_event_count(self): self._check_count('cpu') def test_sysfs_events(self): - devices_events = [ 'cpu' , 'hv_24x7' , 'hv_gpci' ] + devices_events = ['cpu', 'hv_24x7', 'hv_gpci'] + self._create_temp_user() for type_events in devices_events: directory_base = '/sys/bus/event_source/devices' directory = os.path.join(directory_base, type_events, 'events') print(directory) random_files = self.get_random_filenames(directory) - self._create_temp_user() if not process.system('id test_pmu', sudo=True, ignore_status=True): for file in random_files: eventdir = os.path.join(directory, file) result = process.run("su - test_pmu -c 'echo 1 > %s'" % eventdir, - shell=True, ignore_status=True) + shell=True, ignore_status=True) output = result.stdout.decode() + result.stderr.decode() - self.log.info("Test passes for %s of %s"%(file , type_events)) - if 'Permission denied' not in output: - self.fail("Able to read %s of %s as normal user"%(file , type_events)) - self._remove_temp_user() + self.log.info("Test passes for %s of %s" % + (file, type_events)) else: self.log.warn('User test_pmu does not exist, skipping test') + self._remove_temp_user() + def test_caps_feat(self): + modes = [ 'power10', 'power11'] + if self.model in modes: + cmd = "cat /sys/bus/event_source/devices/cpu/caps/pmu_name" + sysfs_value = process.system_output(cmd, shell=True, + ignore_status=True).decode() + self.log.info(" Sysfs caps version : %s "%sysfs_value) + else: + self.cancel("This test is supported only for Power10 and above") @skipIf(IS_POWER_NV or IS_KVM_GUEST, "This test is for PowerVM") def test_hv_24x7_event_count(self): From de0e86f207a41a638a9681c77a2d4cf4b817075e Mon Sep 17 00:00:00 2001 From: Praveen K Pandey Date: Tue, 3 Sep 2024 11:24:10 +0530 Subject: [PATCH 3/4] Removed unwanted logger statement Removed unwanted logger statement which is requested in review comment Signed-off-by: Praveen K Pandey --- perf/perf_pmu.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/perf/perf_pmu.py b/perf/perf_pmu.py index 92627b956..39abc6909 100644 --- a/perf/perf_pmu.py +++ b/perf/perf_pmu.py @@ -241,8 +241,6 @@ def test_sysfs_events(self): result = process.run("su - test_pmu -c 'echo 1 > %s'" % eventdir, shell=True, ignore_status=True) output = result.stdout.decode() + result.stderr.decode() - self.log.info("Test passes for %s of %s" % - (file, type_events)) else: self.log.warn('User test_pmu does not exist, skipping test') self._remove_temp_user() From e7c3641aa7d4eded6c6dc4b0355f37bfb08c61c0 Mon Sep 17 00:00:00 2001 From: Praveen K Pandey Date: Tue, 3 Sep 2024 11:28:23 +0530 Subject: [PATCH 4/4] Resolved Travis check issue Resolved Travis issue Signed-off-by: Praveen K Pandey --- perf/perf_pmu.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/perf/perf_pmu.py b/perf/perf_pmu.py index 39abc6909..2e5fa9c28 100644 --- a/perf/perf_pmu.py +++ b/perf/perf_pmu.py @@ -244,13 +244,14 @@ def test_sysfs_events(self): else: self.log.warn('User test_pmu does not exist, skipping test') self._remove_temp_user() + def test_caps_feat(self): - modes = [ 'power10', 'power11'] + modes = ['power10', 'power11'] if self.model in modes: cmd = "cat /sys/bus/event_source/devices/cpu/caps/pmu_name" sysfs_value = process.system_output(cmd, shell=True, - ignore_status=True).decode() - self.log.info(" Sysfs caps version : %s "%sysfs_value) + ignore_status=True).decode() + self.log.info(" Sysfs caps version : %s " % sysfs_value) else: self.cancel("This test is supported only for Power10 and above")