Add --cpuset-cpus support

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)
(cherry picked from commit ec6c50d198)
This commit is contained in:
Emilien Macchi 2019-09-10 09:36:28 -04:00
parent ac9362eb80
commit 4f0b72a32b
6 changed files with 131 additions and 12 deletions

View File

@ -16,6 +16,8 @@ import logging
import tenacity
import yaml
from paunch.utils import common
LOG = logging.getLogger(__name__)
@ -188,6 +190,15 @@ class ComposeV1Builder(object):
cmd.append('--log-opt=tag=%s' % cconfig['log_tag'])
if 'security_opt' in cconfig:
cmd.append('--security-opt=%s' % cconfig['security_opt'])
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())
cmd.append(cconfig.get('image', ''))
cmd.extend(self.command_argument(cconfig.get('command')))

View File

@ -25,7 +25,8 @@ from paunch.tests import base
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_argspec = inspect.getargspec(orig_call)
config = {
@ -136,7 +137,7 @@ class TestComposeV1Builder(base.TestCase):
'--label', 'container_name=one',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['one']),
'--detach=true', 'centos:7']
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
),
# run two
mock.call(
@ -145,7 +146,7 @@ class TestComposeV1Builder(base.TestCase):
'--label', 'container_name=two',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['two']),
'--detach=true', 'centos:7']
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
),
# run three
mock.call(
@ -154,7 +155,7 @@ class TestComposeV1Builder(base.TestCase):
'--label', 'container_name=three',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['three']),
'--detach=true', 'centos:6']
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:6']
),
# run four
mock.call(
@ -163,7 +164,7 @@ class TestComposeV1Builder(base.TestCase):
'--label', 'container_name=four',
'--label', 'managed_by=tester',
'--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
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 = {
# not running yet
'one': {
@ -289,7 +291,7 @@ three-12345678 three''', '', 0),
'--label', 'container_name=one',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['one']),
'--detach=true', 'centos:7']
'--detach=true', '--cpuset-cpus=0,1,2,3', 'centos:7']
),
# run two
mock.call(
@ -298,7 +300,7 @@ three-12345678 three''', '', 0),
'--label', 'container_name=two',
'--label', 'managed_by=tester',
'--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
# run four
@ -308,7 +310,7 @@ three-12345678 three''', '', 0),
'--label', 'container_name=four',
'--label', 'managed_by=tester',
'--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
mock.call(
@ -412,7 +414,8 @@ three-12345678 three''', '', 0),
'--label', 'config_data=null'],
cmd)
def test_docker_run_args(self):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_docker_run_args(self, mock_cpu):
config = {
'one': {
'image': 'centos:7',
@ -446,11 +449,13 @@ three-12345678 three''', '', 0),
'--health-timeout=10s', '--health-retries=3',
'--privileged=true', '--restart=always', '--user=bar',
'--log-opt=tag={{.ImageName}}/{{.Name}}/{{.ID}}',
'--security-opt=label:disable', 'centos:7'],
'--security-opt=label:disable', '--cpuset-cpus=0,1,2,3',
'centos:7'],
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 = {
'one': {
'image': 'centos:7',
@ -478,6 +483,42 @@ three-12345678 three''', '', 0),
'--ulimit=nofile=1024', '--ulimit=nproc=1024',
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
'--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'],
'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',
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
'--volumes-from=two', '--volumes-from=three',
'--cpuset-cpus=0-2',
'centos:7', 'ls', '-l', '/foo'],
cmd
)

View File

@ -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
paunch/utils/__init__.py Normal file
View File

32
paunch/utils/common.py Normal file
View File

@ -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)

View File

@ -6,3 +6,4 @@ pbr>=2.0.0,!=2.1.0 # Apache-2.0
cliff>=2.6.0 # Apache-2.0
tenacity>=3.2.1 # Apache-2.0
psutil>=3.2.2 # BSD