Skip to content

Commit b9146b9

Browse files
authored
[dvs] Add options to limit CPU usage (sonic-net#1394)
Signed-off-by: Danny Allen <[email protected]>
1 parent 36fe710 commit b9146b9

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

tests/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ For those developing new features for SWSS or the DVS framework, you might find
7979
```
8080
8181
## Other useful test parameters
82+
- You can specify a maximum amount of cores for the DVS to use (we recommend 2):
83+
```
84+
sudo pytest --max_cpu 2
85+
```
86+
87+
For a persistent DVS:
88+
```
89+
docker run --privileged -v /var/run/redis-vs/sw:/var/run/redis --network container:sw -d --name vs --cpus 2 docker-sonic-vs
90+
```
91+
92+
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)
93+
8294
- You can see the output of all test cases that have been run by adding the verbose flag:
8395
8496
```

tests/conftest.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def pytest_addoption(parser):
4545
help="keep testbed after test")
4646
parser.addoption("--imgname", action="store", default="docker-sonic-vs",
4747
help="image name")
48+
parser.addoption("--max_cpu",
49+
action="store",
50+
default=2,
51+
type=int,
52+
help="Max number of CPU cores to use, if available. (default = 2)")
4853

4954

5055
class AsicDbValidator(DVSDatabase):
@@ -158,7 +163,15 @@ class DockerVirtualSwitch(object):
158163
FLEX_COUNTER_DB_ID = 5
159164
STATE_DB_ID = 6
160165

161-
def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None, log_path=None):
166+
def __init__(
167+
self,
168+
name=None,
169+
imgname=None,
170+
keeptb=False,
171+
fakeplatform=None,
172+
log_path=None,
173+
max_cpu=2
174+
):
162175
self.basicd = ['redis-server',
163176
'rsyslogd']
164177
self.swssd = ['orchagent',
@@ -243,10 +256,13 @@ def __init__(self, name=None, imgname=None, keeptb=False, fakeplatform=None, log
243256
self.environment = ["fake_platform={}".format(fakeplatform)] if fakeplatform else []
244257

245258
# create virtual switch container
246-
self.ctn = self.client.containers.run(imgname, privileged=True, detach=True,
247-
environment=self.environment,
248-
network_mode="container:%s" % self.ctn_sw.name,
249-
volumes={ self.mount: { 'bind': '/var/run/redis', 'mode': 'rw' } })
259+
self.ctn = self.client.containers.run(imgname,
260+
privileged=True,
261+
detach=True,
262+
environment=self.environment,
263+
network_mode=f"container:{self.ctn_sw.name}",
264+
volumes={self.mount: {"bind": "/var/run/redis", "mode": "rw"}},
265+
cpu_count=max_cpu)
250266

251267
self.redis_sock = self.mount + '/' + "redis.sock"
252268

@@ -973,11 +989,11 @@ def dvs(request) -> DockerVirtualSwitch:
973989
name = request.config.getoption("--dvsname")
974990
keeptb = request.config.getoption("--keeptb")
975991
imgname = request.config.getoption("--imgname")
992+
max_cpu = request.config.getoption("--max_cpu")
976993
fakeplatform = getattr(request.module, "DVS_FAKE_PLATFORM", None)
977-
978994
log_path = name if name else request.module.__name__
979995

980-
dvs = DockerVirtualSwitch(name, imgname, keeptb, fakeplatform, log_path)
996+
dvs = DockerVirtualSwitch(name, imgname, keeptb, fakeplatform, log_path, max_cpu)
981997

982998
yield dvs
983999

0 commit comments

Comments
 (0)