From b9146b9dbf4b02d8f23e0e3c0436c486942b2f89 Mon Sep 17 00:00:00 2001 From: Danny Allen Date: Wed, 12 Aug 2020 15:13:43 -0700 Subject: [PATCH] [dvs] Add options to limit CPU usage (#1394) Signed-off-by: Danny Allen --- tests/README.md | 12 ++++++++++++ tests/conftest.py | 30 +++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/tests/README.md b/tests/README.md index 5f9e0513a127..fb0d0558fc18 100644 --- a/tests/README.md +++ b/tests/README.md @@ -79,6 +79,18 @@ For those developing new features for SWSS or the DVS framework, you might find ``` ## Other useful test parameters +- You can specify a maximum amount of cores for the DVS to use (we recommend 2): + ``` + sudo pytest --max_cpu 2 + ``` + + For a persistent DVS: + ``` + docker run --privileged -v /var/run/redis-vs/sw:/var/run/redis --network container:sw -d --name vs --cpus 2 docker-sonic-vs + ``` + + For specific details about the performance impact of this, see [the Docker docs.](https://docs.docker.com/config/containers/resource_constraints/#configure-the-default-cfs-scheduler) + - You can see the output of all test cases that have been run by adding the verbose flag: ``` diff --git a/tests/conftest.py b/tests/conftest.py index 6ddf8c73b84c..f19135677579 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -45,6 +45,11 @@ def pytest_addoption(parser): help="keep testbed after test") parser.addoption("--imgname", action="store", default="docker-sonic-vs", help="image name") + parser.addoption("--max_cpu", + action="store", + default=2, + type=int, + help="Max number of CPU cores to use, if available. (default = 2)") class AsicDbValidator(DVSDatabase): @@ -158,7 +163,15 @@ class DockerVirtualSwitch(object): FLEX_COUNTER_DB_ID = 5 STATE_DB_ID = 6 - def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None, log_path=None): + def __init__( + self, + name=None, + imgname=None, + keeptb=False, + fakeplatform=None, + log_path=None, + max_cpu=2 + ): self.basicd = ['redis-server', 'rsyslogd'] self.swssd = ['orchagent', @@ -243,10 +256,13 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None, log self.environment = ["fake_platform={}".format(fakeplatform)] if fakeplatform else [] # create virtual switch container - self.ctn = self.client.containers.run(imgname, privileged=True, detach=True, - environment=self.environment, - network_mode="container:%s" % self.ctn_sw.name, - volumes={ self.mount: { 'bind': '/var/run/redis', 'mode': 'rw' } }) + self.ctn = self.client.containers.run(imgname, + privileged=True, + detach=True, + environment=self.environment, + network_mode=f"container:{self.ctn_sw.name}", + volumes={self.mount: {"bind": "/var/run/redis", "mode": "rw"}}, + cpu_count=max_cpu) self.redis_sock = self.mount + '/' + "redis.sock" @@ -973,11 +989,11 @@ def dvs(request) -> DockerVirtualSwitch: name = request.config.getoption("--dvsname") keeptb = request.config.getoption("--keeptb") imgname = request.config.getoption("--imgname") + max_cpu = request.config.getoption("--max_cpu") fakeplatform = getattr(request.module, "DVS_FAKE_PLATFORM", None) - log_path = name if name else request.module.__name__ - dvs = DockerVirtualSwitch(name, imgname, keeptb, fakeplatform, log_path) + dvs = DockerVirtualSwitch(name, imgname, keeptb, fakeplatform, log_path, max_cpu) yield dvs