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

(cherry picked from commit 5e88de63f5)

Change-Id: Id4739e48bb4706a47a16ed4339c5d0ff3d8ecaa2
This commit is contained in:
Emilien Macchi 2019-09-10 09:36:28 -04:00
parent 50e5f4412a
commit 2875dadd2c
8 changed files with 157 additions and 21 deletions

View File

@ -12,6 +12,7 @@
#
from paunch.builder import base
from paunch.utils import common
class ComposeV1Builder(base.BaseBuilder):
@ -79,6 +80,15 @@ class ComposeV1Builder(base.BaseBuilder):
self.string_arg(cconfig, cmd, 'mem_swappiness', '--memory-swappiness')
self.string_arg(cconfig, cmd, 'security_opt', '--security-opt')
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())
self.string_arg(cconfig, cmd,
'stop_grace_period', '--stop-timeout',
@ -99,7 +109,6 @@ class ComposeV1Builder(base.BaseBuilder):
# stop_signal
# volume_driver
# cpu_quota
# cpuset
# domainname
# hostname
# mac_address

View File

@ -13,6 +13,7 @@
import os
from paunch.builder import base
from paunch.utils import common
class PodmanBuilder(base.BaseBuilder):
@ -93,6 +94,15 @@ class PodmanBuilder(base.BaseBuilder):
for extra_host in cconfig.get('extra_hosts', []):
if extra_host:
cmd.append('--add-host=%s' % extra_host)
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())
self.string_arg(cconfig, cmd,
'stop_grace_period', '--stop-timeout',

View File

@ -26,7 +26,8 @@ from paunch.tests import base
class TestBaseBuilder(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 = {
@ -140,7 +141,8 @@ class TestBaseBuilder(base.TestCase):
'--label', 'container_name=one',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['one']),
'--detach=true', 'centos:7'], mock.ANY
'--detach=true', '--cpuset-cpus=0,1,2,3',
'centos:7'], mock.ANY
),
# run two
mock.call(
@ -149,7 +151,8 @@ class TestBaseBuilder(base.TestCase):
'--label', 'container_name=two',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['two']),
'--detach=true', 'centos:7'], mock.ANY
'--detach=true', '--cpuset-cpus=0,1,2,3',
'centos:7'], mock.ANY
),
# run three
mock.call(
@ -158,7 +161,8 @@ class TestBaseBuilder(base.TestCase):
'--label', 'container_name=three',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['three']),
'--detach=true', 'centos:6'], mock.ANY
'--detach=true', '--cpuset-cpus=0,1,2,3',
'centos:6'], mock.ANY
),
# run four
mock.call(
@ -167,7 +171,8 @@ class TestBaseBuilder(base.TestCase):
'--label', 'container_name=four',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['four']),
'--detach=true', 'centos:7'], mock.ANY
'--detach=true', '--cpuset-cpus=0,1,2,3',
'centos:7'], mock.ANY
),
# execute within four
mock.call(
@ -175,7 +180,8 @@ class TestBaseBuilder(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': {
@ -296,7 +302,8 @@ three-12345678 three''', '', 0),
'--label', 'container_name=one',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['one']),
'--detach=true', 'centos:7'], mock.ANY
'--detach=true', '--cpuset-cpus=0,1,2,3',
'centos:7'], mock.ANY
),
# run two
mock.call(
@ -305,7 +312,8 @@ three-12345678 three''', '', 0),
'--label', 'container_name=two',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['two']),
'--detach=true', 'centos:7'], mock.ANY
'--detach=true', '--cpuset-cpus=0,1,2,3',
'centos:7'], mock.ANY
),
# don't run three, its already running
# run four
@ -315,7 +323,8 @@ three-12345678 three''', '', 0),
'--label', 'container_name=four',
'--label', 'managed_by=tester',
'--label', 'config_data=%s' % json.dumps(config['four']),
'--detach=true', 'centos:7'], mock.ANY
'--detach=true', '--cpuset-cpus=0,1,2,3',
'centos:7'], mock.ANY
),
# execute within four
mock.call(
@ -452,7 +461,8 @@ three-12345678 three''', '', 0),
self.assertIn(arg, cmd)
@mock.patch('paunch.runner.DockerRunner', autospec=True)
def test_container_run_args_lists(self, runner):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_container_run_args_lists(self, mock_cpu, runner):
config = {
'one': {
'image': 'centos:7',
@ -484,6 +494,47 @@ three-12345678 three''', '', 0),
'--group-add=docker', '--group-add=zuul',
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
'--volumes-from=two', '--volumes-from=three',
'--cpuset-cpus=0,1,2,3',
'--cap-add=SYS_ADMIN', '--cap-add=SETUID', '--cap-drop=NET_RAW',
'centos:7', 'ls', '-l', '/foo'],
cmd
)
@mock.patch('paunch.runner.DockerRunner', autospec=True)
def test_container_run_args_lists_with_cpu(self, runner):
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'],
'cap_add': ['SYS_ADMIN', 'SETUID'],
'cap_drop': ['NET_RAW'],
'cpuset_cpus': '0-2',
}
}
builder = compose1.ComposeV1Builder('foo', config, runner)
cmd = ['docker', 'run', '--name', 'one']
builder.container_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',
'--cap-add=SYS_ADMIN', '--cap-add=SETUID', '--cap-drop=NET_RAW',
'centos:7', 'ls', '-l', '/foo'],
cmd

View File

@ -20,7 +20,8 @@ from paunch.tests import test_builder_base as tbb
class TestComposeV1Builder(tbb.TestBaseBuilder):
@mock.patch('paunch.runner.DockerRunner', autospec=True)
def test_cont_run_args(self, runner):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_cont_run_args(self, mock_cpu, runner):
config = {
'one': {
'image': 'centos:7',
@ -67,13 +68,15 @@ class TestComposeV1Builder(tbb.TestBaseBuilder):
'--memory-swap=1G',
'--memory-swappiness=60',
'--security-opt=label:disable',
'--cpuset-cpus=0,1,2,3',
'--cap-add=SYS_ADMIN', '--cap-add=SETUID', '--cap-drop=NET_RAW',
'centos:7'],
cmd
)
@mock.patch('paunch.runner.DockerRunner', autospec=True)
def test_cont_run_args_validation_true(self, runner):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_cont_run_args_validation_true(self, mock_cpu, runner):
config = {
'one': {
'image': 'foo',
@ -87,12 +90,15 @@ class TestComposeV1Builder(tbb.TestBaseBuilder):
self.assertTrue(builder.container_run_args(cmd, 'one'))
self.assertEqual(
['docker', '--detach=true',
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro', 'foo'],
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
'--cpuset-cpus=0,1,2,3',
'foo'],
cmd
)
@mock.patch('paunch.runner.DockerRunner', autospec=True)
def test_cont_run_args_validation_false(self, runner):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_cont_run_args_validation_false(self, mock_cpu, runner):
config = {
'one': {
'image': 'foo',
@ -106,6 +112,7 @@ class TestComposeV1Builder(tbb.TestBaseBuilder):
self.assertFalse(builder.container_run_args(cmd, 'one'))
self.assertEqual(
['docker', '--detach=true',
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro', 'foo'],
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
'--cpuset-cpus=0,1,2,3', 'foo'],
cmd
)

View File

@ -19,7 +19,9 @@ from paunch.tests import test_builder_base as base
class TestPodmanBuilder(base.TestBaseBuilder):
def test_cont_run_args(self):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_cont_run_args(self, mock_cpu):
config = {
'one': {
'image': 'centos:7',
@ -65,13 +67,15 @@ class TestPodmanBuilder(base.TestBaseBuilder):
'--hostname=foohostname',
'--add-host=foohost:127.0.0.1',
'--add-host=barhost:127.0.0.2',
'--cpuset-cpus=0,1,2,3',
'--cap-add=SYS_ADMIN', '--cap-add=SETUID', '--cap-drop=NET_RAW',
'centos:7'],
cmd
)
@mock.patch('paunch.runner.PodmanRunner', autospec=True)
def test_cont_run_args_validation_true(self, runner):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_cont_run_args_validation_true(self, mock_cpu, runner):
config = {
'one': {
'image': 'foo',
@ -85,12 +89,14 @@ class TestPodmanBuilder(base.TestBaseBuilder):
self.assertTrue(builder.container_run_args(cmd, 'one'))
self.assertEqual(
['podman', '--conmon-pidfile=/var/run/one.pid', '--detach=true',
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro', 'foo'],
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
'--cpuset-cpus=0,1,2,3', 'foo'],
cmd
)
@mock.patch('paunch.runner.PodmanRunner', autospec=True)
def test_cont_run_args_validation_false(self, runner):
@mock.patch("psutil.Process.cpu_affinity", return_value=[0, 1, 2, 3])
def test_cont_run_args_validation_false(self, mock_cpu, runner):
config = {
'one': {
'image': 'foo',
@ -104,6 +110,7 @@ class TestPodmanBuilder(base.TestBaseBuilder):
self.assertFalse(builder.container_run_args(cmd, 'one'))
self.assertEqual(
['podman', '--conmon-pidfile=/var/run/one.pid', '--detach=true',
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro', 'foo'],
'--volume=/foo:/foo:rw', '--volume=/bar:/bar:ro',
'--cpuset-cpus=0,1,2,3', '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)

View File

@ -15,6 +15,7 @@
import logging
import os
import psutil
import sys
from paunch import constants
@ -64,3 +65,19 @@ def configure_logging_from_args(name, app_args):
log = utils.common.configure_logging(
__name__, log_level, log_file)
return (log, log_file, log_level)
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

@ -7,3 +7,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
jmespath>=0.9.0 # MIT
psutil>=3.2.2 # BSD