Skip to content

Commit

Permalink
feat: Add spec sys_cpuset_cpus (#3611)
Browse files Browse the repository at this point in the history
* Add spec sys_cpuset_cpus

Signed-off-by: jiazhang <[email protected]>

* Update spec name
* Update style
* Update import format

Signed-off-by: jiazhang <[email protected]>
  • Loading branch information
wushiqinlou authored Dec 7, 2022
1 parent eafa97a commit 47762f5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
40 changes: 35 additions & 5 deletions insights/parsers/cpuset_cpus.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -28,8 +29,8 @@ class CpusetCpus(CommandParser):
Examples:
>>> type(cpusetinfo)
<class 'insights.parsers.cpuset_cpus.CpusetCpus'>
>>> cpusetinfo.cpuset
["0", "2", "3", "4", "7"]
>>> cpusetinfo.cpu_set
['0', '2', '3', '4', '7']
>>> cpusetinfo.cpu_number
5
"""
Expand All @@ -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)
<class 'insights.parsers.cpuset_cpus.ContainerCpusetCpus'>
>>> 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
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 32 additions & 1 deletion insights/tests/parsers/test_cpuset_cpus.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import doctest
from insights.parsers import cpuset_cpus
from insights.tests import context_wrap

Expand All @@ -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

0 comments on commit 47762f5

Please sign in to comment.