From c5e731f246c22cc15219facae70773d8cf0edbbf Mon Sep 17 00:00:00 2001 From: jiazhang Date: Fri, 25 Nov 2022 21:04:56 +0800 Subject: [PATCH 1/4] Add spec sys_cpuset_cpus Signed-off-by: jiazhang --- insights/parsers/cpuset_cpus.py | 38 +++++++++++++++++++--- insights/specs/__init__.py | 1 + insights/specs/default.py | 1 + insights/tests/parsers/test_cpuset_cpus.py | 34 +++++++++++++++++-- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/insights/parsers/cpuset_cpus.py b/insights/parsers/cpuset_cpus.py index db72fe8bb7..c0bce4e7f3 100644 --- a/insights/parsers/cpuset_cpus.py +++ b/insights/parsers/cpuset_cpus.py @@ -1,6 +1,6 @@ """ -CpuSetCpus - File ``/sys/fs/cgroup/cpuset/cpuset.cpus`` -======================================================= +cpuset_cpus - File ``/sys/fs/cgroup/cpuset/cpuset.cpus`` +======================================================== This parser reads the content of ``/sys/fs/cgroup/cpuset/cpuset.cpus``. This file shows the default cgroup cpuset.cpu of system. The format @@ -9,6 +9,7 @@ from .. import parser, CommandParser from insights.specs import Specs +from insights import ContainerParser @parser(Specs.cpuset_cpus) @@ -28,8 +29,8 @@ class CpusetCpus(CommandParser): Examples: >>> type(cpusetinfo) - >>> cpusetinfo.cpuset - ["0", "2", "3", "4", "7"] + >>> cpusetinfo.cpu_set + ['0', '2', '3', '4', '7'] >>> cpusetinfo.cpu_number 5 """ @@ -46,3 +47,32 @@ def parse_content(self, content): else: self.cpu_set.append(value) self.cpu_number = len(self.cpu_set) + + +@parser(Specs.container_cpuset_cpus) +class ContainerCpusetCpus(ContainerParser, CpusetCpus): + """ + Class ``ContainerCpusetCpus`` parses the content of the ``/sys/fs/cgroup/cpuset/cpuset.cpus`` from containers. + + Attributes: + cpu_set (list): It is used to show the list of allowed cpu. + + cpu_number (int): It is used to display the number of allowed cpu. + + A small sample of the content of this file looks like:: + + 0,2-4,7 + + Examples: + >>> type(container_cpusetinfo) + + >>> container_cpusetinfo.container_id + '2869b4e2541c' + >>> container_cpusetinfo.image + 'registry.access.redhat.com/ubi8/nginx-120' + >>> container_cpusetinfo.cpu_set + ['0', '2', '3', '4', '7'] + >>> container_cpusetinfo.cpu_number + 5 + """ + pass diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 9bffd1b6e3..aac6e61f51 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -799,4 +799,5 @@ class Specs(SpecSet): container_nginx_conf = RegistryPoint(multi_output=True) container_installed_rpms = RegistryPoint(multi_output=True) container_inspect_keys = RegistryPoint(filterable=True) + container_cpu_online = RegistryPoint(multi_output=True) containers_inspect = RegistryPoint() diff --git a/insights/specs/default.py b/insights/specs/default.py index a0f6dd4bcf..fdf8359595 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -681,4 +681,5 @@ class DefaultSpecs(Specs): container_installed_rpms = container_execute(running_rhel_containers, "/usr/bin/rpm -qa --qf '%s'" % _rpm_format, context=HostContext, signum=signal.SIGTERM) container_nginx_conf = container_collect(container_nginx_conf_ds) container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release") + container_cpuset_cpus = container_collect(running_rhel_containers, "/sys/fs/cgroup/cpuset/cpuset.cpus") containers_inspect = containers_inspect.containers_inspect_data_datasource diff --git a/insights/tests/parsers/test_cpuset_cpus.py b/insights/tests/parsers/test_cpuset_cpus.py index 17283ec591..e7c62c510b 100644 --- a/insights/tests/parsers/test_cpuset_cpus.py +++ b/insights/tests/parsers/test_cpuset_cpus.py @@ -1,13 +1,43 @@ from insights.parsers import cpuset_cpus from insights.tests import context_wrap - +import doctest CPUSET_CPU = """ 0,2-4,7 """.strip() -def test_init_process_cgroup(): +def test_cpuset_cpu(): cpusetinfo = cpuset_cpus.CpusetCpus(context_wrap(CPUSET_CPU)) assert cpusetinfo.cpu_set == ["0", "2", "3", "4", "7"] assert cpusetinfo.cpu_number == 5 + + +def test_container_cpuset_cpu(): + container_cpusetinfo = cpuset_cpus.ContainerCpusetCpus( + context_wrap( + CPUSET_CPU, + container_id='2869b4e2541c', + image='registry.access.redhat.com/ubi8/nginx-120', + engine='podman', + path='insights_containers/2869b4e2541c/sys/fs/cgroup/cpuset/cpuset.cpus' + ) + ) + assert container_cpusetinfo.cpu_set == ["0", "2", "3", "4", "7"] + assert container_cpusetinfo.cpu_number == 5 + + +def test_doc(): + env = { + "cpusetinfo": cpuset_cpus.CpusetCpus(context_wrap(CPUSET_CPU)), + "container_cpusetinfo": cpuset_cpus.ContainerCpusetCpus( + context_wrap( + CPUSET_CPU, + container_id='2869b4e2541c', + image='registry.access.redhat.com/ubi8/nginx-120', + engine='podman' + ) + ) + } + failed_count, total = doctest.testmod(cpuset_cpus, globs=env) + assert failed_count == 0 From ea035f3f5080a49f77f69c98fafb419cf98fcbde Mon Sep 17 00:00:00 2001 From: jiazhang Date: Wed, 30 Nov 2022 16:37:21 +0800 Subject: [PATCH 2/4] Update spec name Signed-off-by: jiazhang --- insights/specs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index aac6e61f51..7643763d41 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -794,10 +794,10 @@ class Specs(SpecSet): zipl_conf = RegistryPoint() # container_specs + container_cpuset_cpus = RegistryPoint(multi_output=True) container_dotnet_version = RegistryPoint(multi_output=True) container_redhat_release = RegistryPoint(multi_output=True) container_nginx_conf = RegistryPoint(multi_output=True) container_installed_rpms = RegistryPoint(multi_output=True) container_inspect_keys = RegistryPoint(filterable=True) - container_cpu_online = RegistryPoint(multi_output=True) containers_inspect = RegistryPoint() From 4cc4153ef550cf3b0eeb95a724cf931ddf7422d1 Mon Sep 17 00:00:00 2001 From: jiazhang Date: Wed, 30 Nov 2022 16:49:18 +0800 Subject: [PATCH 3/4] Update style Signed-off-by: jiazhang --- insights/parsers/cpuset_cpus.py | 4 ++-- insights/specs/default.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/insights/parsers/cpuset_cpus.py b/insights/parsers/cpuset_cpus.py index c0bce4e7f3..f5c8e0e4b5 100644 --- a/insights/parsers/cpuset_cpus.py +++ b/insights/parsers/cpuset_cpus.py @@ -7,9 +7,9 @@ of the content is string including comma. """ -from .. import parser, CommandParser -from insights.specs import Specs from insights import ContainerParser +from insights import parser, CommandParser +from insights.specs import Specs @parser(Specs.cpuset_cpus) diff --git a/insights/specs/default.py b/insights/specs/default.py index fdf8359595..67da41830a 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -677,9 +677,9 @@ class DefaultSpecs(Specs): zipl_conf = simple_file("/etc/zipl.conf") # Container collection specs + container_cpuset_cpus = container_collect(running_rhel_containers, "/sys/fs/cgroup/cpuset/cpuset.cpus") container_dotnet_version = container_execute(running_rhel_containers, "/usr/bin/dotnet --version") container_installed_rpms = container_execute(running_rhel_containers, "/usr/bin/rpm -qa --qf '%s'" % _rpm_format, context=HostContext, signum=signal.SIGTERM) container_nginx_conf = container_collect(container_nginx_conf_ds) container_redhat_release = container_collect(running_rhel_containers, "/etc/redhat-release") - container_cpuset_cpus = container_collect(running_rhel_containers, "/sys/fs/cgroup/cpuset/cpuset.cpus") containers_inspect = containers_inspect.containers_inspect_data_datasource From ed5ea530af49bfdbbb32f38db75630320b29f107 Mon Sep 17 00:00:00 2001 From: jiazhang Date: Tue, 6 Dec 2022 10:05:01 +0800 Subject: [PATCH 4/4] Update import format Signed-off-by: jiazhang --- insights/parsers/cpuset_cpus.py | 4 ++-- insights/tests/parsers/test_cpuset_cpus.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/insights/parsers/cpuset_cpus.py b/insights/parsers/cpuset_cpus.py index f5c8e0e4b5..87447832dc 100644 --- a/insights/parsers/cpuset_cpus.py +++ b/insights/parsers/cpuset_cpus.py @@ -7,8 +7,8 @@ of the content is string including comma. """ -from insights import ContainerParser -from insights import parser, CommandParser +from insights.core import CommandParser, ContainerParser +from insights.core.plugins import parser from insights.specs import Specs diff --git a/insights/tests/parsers/test_cpuset_cpus.py b/insights/tests/parsers/test_cpuset_cpus.py index e7c62c510b..9abf09fe9f 100644 --- a/insights/tests/parsers/test_cpuset_cpus.py +++ b/insights/tests/parsers/test_cpuset_cpus.py @@ -1,6 +1,7 @@ +import doctest from insights.parsers import cpuset_cpus from insights.tests import context_wrap -import doctest + CPUSET_CPU = """ 0,2-4,7