Add --cpuset-cpus support for both Docker and Podman
Limit the specific CPUs or cores a container can use.
If cpuset-cpus is configured in the container layout, then the value
will be used when running the container cli with --cpuset-cpus.
If 'all' is used as a value, we'll then take all available cpus,
computed by: "0-" + str(psutil.cpu_count()-1)
If unset (default), the cpuset-cpus value is computed by using psutil with a
new function which returns a comma-separated list range of CPUs that a
container can use.
This parameter is particulary useful for NFV:
https://bugzilla.redhat.com/show_bug.cgi?id=1750781
Indeed, for NFV workloads, in order to achieve 0 packet loss, linux processes,
ovs-dpdk (if applicable) and VMs are isolated thanks to kernel args (isolcpus)
and tuned profiles (cpu-partitioning).
Change-Id: I9443ad60affe9c7823b17daa259efee542c6fe22
(cherry picked from commit d6ae074d25
)
This commit is contained in:
parent
d48caf33c8
commit
ec6c50d198
|
@ -17,6 +17,8 @@ import re
|
||||||
import tenacity
|
import tenacity
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from paunch.utils import common
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -211,6 +213,16 @@ class ComposeV1Builder(object):
|
||||||
self.string_arg(cconfig, cmd, 'security_opt', '--security-opt')
|
self.string_arg(cconfig, cmd, 'security_opt', '--security-opt')
|
||||||
self.string_arg(cconfig, cmd, 'stop_signal', '--stop-signal')
|
self.string_arg(cconfig, cmd, 'stop_signal', '--stop-signal')
|
||||||
|
|
||||||
|
if 'cpuset_cpus' in cconfig:
|
||||||
|
# 'all' is a special value to directly configure all CPUs
|
||||||
|
# that are available.
|
||||||
|
if cconfig['cpuset_cpus'] == 'all':
|
||||||
|
cmd.append('--cpuset-cpus=%s' % common.get_all_cpus())
|
||||||
|
else:
|
||||||
|
cmd.append('--cpuset-cpus=%s' % cconfig['cpuset_cpus'])
|
||||||
|
else:
|
||||||
|
cmd.append('--cpuset-cpus=%s' % common.get_cpus_allowed_list())
|
||||||
|
|
||||||
def duration(a):
|
def duration(a):
|
||||||
if isinstance(a, (int, float)):
|
if isinstance(a, (int, float)):
|
||||||
return a
|
return a
|
||||||
|
@ -258,7 +270,6 @@ class ComposeV1Builder(object):
|
||||||
# stop_signal
|
# stop_signal
|
||||||
# volume_driver
|
# volume_driver
|
||||||
# cpu_quota
|
# cpu_quota
|
||||||
# cpuset
|
|
||||||
# domainname
|
# domainname
|
||||||
# hostname
|
# hostname
|
||||||
# mac_address
|
# mac_address
|
||||||
|
|
|
@ -25,7 +25,8 @@ from paunch.tests import base
|
||||||
|
|
||||||
class TestComposeV1Builder(base.TestCase):
|
class TestComposeV1Builder(base.TestCase):
|
||||||
|
|
||||||
def test_apply(self):
|
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
|
||||||
|
def test_apply(self, mock_cpu):
|
||||||
orig_call = tenacity.wait.wait_random_exponential.__call__
|
orig_call = tenacity.wait.wait_random_exponential.__call__
|
||||||
orig_argspec = inspect.getargspec(orig_call)
|
orig_argspec = inspect.getargspec(orig_call)
|
||||||
config = {
|
config = {
|
||||||
|
@ -136,7 +137,7 @@ class TestComposeV1Builder(base.TestCase):
|
||||||
'--label', 'container_name=one',
|
'--label', 'container_name=one',
|
||||||
'--label', 'managed_by=tester',
|
'--label', 'managed_by=tester',
|
||||||
'--label', 'config_data=%s' % json.dumps(config['one']),
|
'--label', 'config_data=%s' % json.dumps(config['one']),
|
||||||
'--detach=true', 'centos:7']
|
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
|
||||||
),
|
),
|
||||||
# run two
|
# run two
|
||||||
mock.call(
|
mock.call(
|
||||||
|
@ -145,7 +146,7 @@ class TestComposeV1Builder(base.TestCase):
|
||||||
'--label', 'container_name=two',
|
'--label', 'container_name=two',
|
||||||
'--label', 'managed_by=tester',
|
'--label', 'managed_by=tester',
|
||||||
'--label', 'config_data=%s' % json.dumps(config['two']),
|
'--label', 'config_data=%s' % json.dumps(config['two']),
|
||||||
'--detach=true', 'centos:7']
|
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
|
||||||
),
|
),
|
||||||
# run three
|
# run three
|
||||||
mock.call(
|
mock.call(
|
||||||
|
@ -154,7 +155,7 @@ class TestComposeV1Builder(base.TestCase):
|
||||||
'--label', 'container_name=three',
|
'--label', 'container_name=three',
|
||||||
'--label', 'managed_by=tester',
|
'--label', 'managed_by=tester',
|
||||||
'--label', 'config_data=%s' % json.dumps(config['three']),
|
'--label', 'config_data=%s' % json.dumps(config['three']),
|
||||||
'--detach=true', 'centos:6']
|
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:6']
|
||||||
),
|
),
|
||||||
# run four
|
# run four
|
||||||
mock.call(
|
mock.call(
|
||||||
|
@ -163,7 +164,7 @@ class TestComposeV1Builder(base.TestCase):
|
||||||
'--label', 'container_name=four',
|
'--label', 'container_name=four',
|
||||||
'--label', 'managed_by=tester',
|
'--label', 'managed_by=tester',
|
||||||
'--label', 'config_data=%s' % json.dumps(config['four']),
|
'--label', 'config_data=%s' % json.dumps(config['four']),
|
||||||
'--detach=true', 'centos:7']
|
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
|
||||||
),
|
),
|
||||||
# execute within four
|
# execute within four
|
||||||
mock.call(
|
mock.call(
|
||||||
|
@ -171,7 +172,8 @@ class TestComposeV1Builder(base.TestCase):
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
|
|
||||||
def test_apply_idempotency(self):
|
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
|
||||||
|
def test_apply_idempotency(self, mock_cpu):
|
||||||
config = {
|
config = {
|
||||||
# not running yet
|
# not running yet
|
||||||
'one': {
|
'one': {
|
||||||
|
@ -289,7 +291,7 @@ three-12345678 three''', '', 0),
|
||||||
'--label', 'container_name=one',
|
'--label', 'container_name=one',
|
||||||
'--label', 'managed_by=tester',
|
'--label', 'managed_by=tester',
|
||||||
'--label', 'config_data=%s' % json.dumps(config['one']),
|
'--label', 'config_data=%s' % json.dumps(config['one']),
|
||||||
'--detach=true', 'centos:7']
|
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
|
||||||
),
|
),
|
||||||
# run two
|
# run two
|
||||||
mock.call(
|
mock.call(
|
||||||
|
@ -298,7 +300,7 @@ three-12345678 three''', '', 0),
|
||||||
'--label', 'container_name=two',
|
'--label', 'container_name=two',
|
||||||
'--label', 'managed_by=tester',
|
'--label', 'managed_by=tester',
|
||||||
'--label', 'config_data=%s' % json.dumps(config['two']),
|
'--label', 'config_data=%s' % json.dumps(config['two']),
|
||||||
'--detach=true', 'centos:7']
|
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
|
||||||
),
|
),
|
||||||
# don't run three, its already running
|
# don't run three, its already running
|
||||||
# run four
|
# run four
|
||||||
|
@ -308,7 +310,7 @@ three-12345678 three''', '', 0),
|
||||||
'--label', 'container_name=four',
|
'--label', 'container_name=four',
|
||||||
'--label', 'managed_by=tester',
|
'--label', 'managed_by=tester',
|
||||||
'--label', 'config_data=%s' % json.dumps(config['four']),
|
'--label', 'config_data=%s' % json.dumps(config['four']),
|
||||||
'--detach=true', 'centos:7']
|
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
|
||||||
),
|
),
|
||||||
# execute within four
|
# execute within four
|
||||||
mock.call(
|
mock.call(
|
||||||
|
@ -449,7 +451,8 @@ three-12345678 three''', '', 0),
|
||||||
'--privileged=true', '--restart=always', '--user=bar',
|
'--privileged=true', '--restart=always', '--user=bar',
|
||||||
'--log-opt=tag={{.ImageName}}/{{.Name}}/{{.ID}}',
|
'--log-opt=tag={{.ImageName}}/{{.Name}}/{{.ID}}',
|
||||||
'--cpu-shares=600',
|
'--cpu-shares=600',
|
||||||
'--security-opt=label:disable', 'centos:7'],
|
'--security-opt=label:disable', '--cpuset-cpus=0,1,2,3',
|
||||||
|
'centos:7'],
|
||||||
cmd
|
cmd
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -484,7 +487,8 @@ three-12345678 three''', '', 0),
|
||||||
builder.docker_run_args(cmd, container)
|
builder.docker_run_args(cmd, container)
|
||||||
self.assertIn(arg, cmd)
|
self.assertIn(arg, cmd)
|
||||||
|
|
||||||
def test_docker_run_args_lists(self):
|
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
|
||||||
|
def test_docker_run_args_lists(self, mock_cpu):
|
||||||
config = {
|
config = {
|
||||||
'one': {
|
'one': {
|
||||||
'image': 'centos:7',
|
'image': 'centos:7',
|
||||||
|
@ -514,6 +518,44 @@ three-12345678 three''', '', 0),
|
||||||
'--group-add=docker', '--group-add=zuul',
|
'--group-add=docker', '--group-add=zuul',
|
||||||
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
|
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
|
||||||
'--volumes-from=two', '--volumes-from=three',
|
'--volumes-from=two', '--volumes-from=three',
|
||||||
|
'--cpuset-cpus=0,1,2,3',
|
||||||
|
'centos:7', 'ls', '-l', '/foo'],
|
||||||
|
cmd
|
||||||
|
)
|
||||||
|
|
||||||
|
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
|
||||||
|
def test_docker_run_args_lists_with_cpu(self, mock_cpu):
|
||||||
|
config = {
|
||||||
|
'one': {
|
||||||
|
'image': 'centos:7',
|
||||||
|
'detach': False,
|
||||||
|
'command': 'ls -l /foo',
|
||||||
|
'remove': True,
|
||||||
|
'tty': True,
|
||||||
|
'interactive': True,
|
||||||
|
'environment': ['FOO=BAR', 'BAR=BAZ'],
|
||||||
|
'env_file': ['/tmp/foo.env', '/tmp/bar.env'],
|
||||||
|
'ulimit': ['nofile=1024', 'nproc=1024'],
|
||||||
|
'volumes': ['/foo:/foo:rw', '/bar:/bar:ro'],
|
||||||
|
'volumes_from': ['two', 'three'],
|
||||||
|
'group_add': ['docker', 'zuul'],
|
||||||
|
'cpuset_cpus': '0-2',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder = compose1.ComposeV1Builder('foo', config, None)
|
||||||
|
|
||||||
|
cmd = ['docker', 'run', '--name', 'one']
|
||||||
|
builder.docker_run_args(cmd, 'one')
|
||||||
|
self.assertEqual(
|
||||||
|
['docker', 'run', '--name', 'one',
|
||||||
|
'--env-file=/tmp/foo.env', '--env-file=/tmp/bar.env',
|
||||||
|
'--env=FOO=BAR', '--env=BAR=BAZ',
|
||||||
|
'--rm', '--interactive', '--tty',
|
||||||
|
'--ulimit=nofile=1024', '--ulimit=nproc=1024',
|
||||||
|
'--group-add=docker', '--group-add=zuul',
|
||||||
|
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
|
||||||
|
'--volumes-from=two', '--volumes-from=three',
|
||||||
|
'--cpuset-cpus=0-2',
|
||||||
'centos:7', 'ls', '-l', '/foo'],
|
'centos:7', 'ls', '-l', '/foo'],
|
||||||
cmd
|
cmd
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
# Copyright 2019 Red Hat, Inc.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
import mock
|
||||||
|
|
||||||
|
from paunch.tests import base
|
||||||
|
from paunch.utils import common
|
||||||
|
|
||||||
|
|
||||||
|
class TestUtilsCommon(base.TestCase):
|
||||||
|
|
||||||
|
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
|
||||||
|
def test_get_cpus_allowed_list(self, mock_cpu):
|
||||||
|
expected_list = '0,1,2,3'
|
||||||
|
actual_list = common.get_cpus_allowed_list()
|
||||||
|
self.assertEqual(actual_list, expected_list)
|
||||||
|
|
||||||
|
@mock.patch("psutil.cpu_count", return_value=4)
|
||||||
|
def test_get_all_cpus(self, mock_cpu):
|
||||||
|
expected_list = '0-3'
|
||||||
|
actual_list = common.get_all_cpus()
|
||||||
|
self.assertEqual(actual_list, expected_list)
|
|
@ -0,0 +1,32 @@
|
||||||
|
# Copyright 2018 Red Hat, Inc.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
import psutil
|
||||||
|
|
||||||
|
|
||||||
|
def get_cpus_allowed_list(**args):
|
||||||
|
"""Returns the process's Cpus_allowed on which CPUs may be scheduled.
|
||||||
|
|
||||||
|
:return: Value for Cpus_allowed, e.g. '0-3'
|
||||||
|
"""
|
||||||
|
return ','.join([str(c) for c in psutil.Process().cpu_affinity()])
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_cpus(**args):
|
||||||
|
"""Returns a single list of all CPUs.
|
||||||
|
|
||||||
|
:return: Value computed by psutil, e.g. '0-3'
|
||||||
|
"""
|
||||||
|
return "0-" + str(psutil.cpu_count() - 1)
|
|
@ -6,3 +6,4 @@ pbr>=2.0.0,!=2.1.0 # Apache-2.0
|
||||||
|
|
||||||
cliff>=2.6.0 # Apache-2.0
|
cliff>=2.6.0 # Apache-2.0
|
||||||
tenacity>=3.2.1 # Apache-2.0
|
tenacity>=3.2.1 # Apache-2.0
|
||||||
|
psutil>=3.2.2 # BSD
|
||||||
|
|
Loading…
Reference in New Issue