From 47762f5199c7c4c4bf2033d706d5e328eb9f655e Mon Sep 17 00:00:00 2001 From: wushiqinlou Date: Wed, 7 Dec 2022 18:26:09 +0800 Subject: [PATCH] feat: Add spec sys_cpuset_cpus (#3611) * Add spec sys_cpuset_cpus Signed-off-by: jiazhang * Update spec name * Update style * Update import format Signed-off-by: jiazhang --- insights/parsers/cpuset_cpus.py | 40 +++++++++++++++++++--- insights/specs/__init__.py | 1 + insights/specs/default.py | 1 + insights/tests/parsers/test_cpuset_cpus.py | 33 +++++++++++++++++- 4 files changed, 69 insertions(+), 6 deletions(-) diff --git a/insights/parsers/cpuset_cpus.py b/insights/parsers/cpuset_cpus.py index db72fe8bb7..87447832dc 100644 --- a/insights/parsers/cpuset_cpus.py +++ b/insights/parsers/cpuset_cpus.py @@ -1,13 +1,14 @@ """ -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 of the content is string including comma. """ -from .. import parser, CommandParser +from insights.core import CommandParser, ContainerParser +from insights.core.plugins import parser from insights.specs import Specs @@ -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 1e0fdfab2b..e92c0d0f61 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -778,6 +778,7 @@ class Specs(SpecSet): # container_specs container_cpu_online = RegistryPoint(multi_output=True) + 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) diff --git a/insights/specs/default.py b/insights/specs/default.py index ce6a1dc04c..f929e568be 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -679,6 +679,7 @@ class DefaultSpecs(Specs): # Container collection specs container_cpu_online = container_collect(running_rhel_containers, "/sys/devices/system/cpu/online") + 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) diff --git a/insights/tests/parsers/test_cpuset_cpus.py b/insights/tests/parsers/test_cpuset_cpus.py index 17283ec591..9abf09fe9f 100644 --- a/insights/tests/parsers/test_cpuset_cpus.py +++ b/insights/tests/parsers/test_cpuset_cpus.py @@ -1,3 +1,4 @@ +import doctest from insights.parsers import cpuset_cpus from insights.tests import context_wrap @@ -7,7 +8,37 @@ """.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