Skip to content

Commit 896d36e

Browse files
committed
Add support for Block IO constraints in HostConfig
This adds support for Block IO constraint options: - blkio-weight - blkio-weight-device - device-read-bps - device-write-bps - device-read-iops - device-write-iops Signed-off-by: yunzhu-li <[email protected]>
1 parent 88811a2 commit 896d36e

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

docker/utils/utils.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,12 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
612612
security_opt=None, ulimits=None, log_config=None,
613613
mem_limit=None, memswap_limit=None, mem_swappiness=None,
614614
cgroup_parent=None, group_add=None, cpu_quota=None,
615-
cpu_period=None, oom_kill_disable=False, shm_size=None,
616-
version=None, tmpfs=None, oom_score_adj=None):
615+
cpu_period=None, blkio_weight=None,
616+
blkio_weight_device=None, device_read_bps=None,
617+
device_write_bps=None, device_read_iops=None,
618+
device_write_iops=None, oom_kill_disable=False,
619+
shm_size=None, version=None, tmpfs=None,
620+
oom_score_adj=None):
617621

618622
host_config = {}
619623

@@ -789,6 +793,58 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None,
789793

790794
host_config['CpuPeriod'] = cpu_period
791795

796+
if blkio_weight:
797+
if not isinstance(blkio_weight, int):
798+
raise host_config_type_error('blkio_weight', blkio_weight, 'int')
799+
if version_lt(version, '1.22'):
800+
raise host_config_version_error('blkio_weight', '1.22')
801+
host_config["BlkioWeight"] = blkio_weight
802+
803+
if blkio_weight_device:
804+
if not isinstance(blkio_weight_device, list):
805+
raise host_config_type_error(
806+
'blkio_weight_device', blkio_weight_device, 'list'
807+
)
808+
if version_lt(version, '1.22'):
809+
raise host_config_version_error('blkio_weight_device', '1.22')
810+
host_config["BlkioWeightDevice"] = blkio_weight_device
811+
812+
if device_read_bps:
813+
if not isinstance(device_read_bps, list):
814+
raise host_config_type_error(
815+
'device_read_bps', device_read_bps, 'list'
816+
)
817+
if version_lt(version, '1.22'):
818+
raise host_config_version_error('device_read_bps', '1.22')
819+
host_config["BlkioDeviceReadBps"] = device_read_bps
820+
821+
if device_write_bps:
822+
if not isinstance(device_write_bps, list):
823+
raise host_config_type_error(
824+
'device_write_bps', device_write_bps, 'list'
825+
)
826+
if version_lt(version, '1.22'):
827+
raise host_config_version_error('device_write_bps', '1.22')
828+
host_config["BlkioDeviceWriteBps"] = device_write_bps
829+
830+
if device_read_iops:
831+
if not isinstance(device_read_iops, list):
832+
raise host_config_type_error(
833+
'device_read_iops', device_read_iops, 'list'
834+
)
835+
if version_lt(version, '1.22'):
836+
raise host_config_version_error('device_read_iops', '1.22')
837+
host_config["BlkioDeviceReadIOps"] = device_read_iops
838+
839+
if device_write_iops:
840+
if not isinstance(device_write_iops, list):
841+
raise host_config_type_error(
842+
'device_write_iops', device_write_iops, 'list'
843+
)
844+
if version_lt(version, '1.22'):
845+
raise host_config_version_error('device_write_iops', '1.22')
846+
host_config["BlkioDeviceWriteIOps"] = device_write_iops
847+
792848
if tmpfs:
793849
if version_lt(version, '1.22'):
794850
raise host_config_version_error('tmpfs', '1.22')

docs/hostconfig.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ for example:
109109
* cpu_group (int): The length of a CPU period in microseconds.
110110
* cpu_period (int): Microseconds of CPU time that the container can get in a
111111
CPU period.
112+
* blkio_weight: Block IO weight (relative weight), accepts a weight value between 10 and 1000.
113+
* blkio_weight_device: Block IO weight (relative device weight) in the form of:
114+
`[{"Path": "device_path", "Weight": weight}]`
115+
* device_read_bps: Limit read rate (bytes per second) from a device in the form of:
116+
`[{"Path": "device_path", "Rate": rate}]`
117+
* device_write_bps: Limit write rate (bytes per second) from a device.
118+
* device_read_iops: Limit read rate (IO per second) from a device.
119+
* device_write_iops: Limit write rate (IO per second) from a device.
112120
* group_add (list): List of additional group names and/or IDs that the
113121
container process will run as.
114122
* devices (list): Host device bindings. See [host devices](host-devices.md)

tests/unit/utils_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ def test_create_host_config_with_cpu_period(self):
6464
config = create_host_config(version='1.20', cpu_period=1999)
6565
self.assertEqual(config.get('CpuPeriod'), 1999)
6666

67+
def test_create_host_config_with_blkio_constraints(self):
68+
blkio_rate = [{"Path": "/dev/sda", "Rate": 1000}]
69+
config = create_host_config(version='1.22',
70+
blkio_weight=1999,
71+
blkio_weight_device=blkio_rate,
72+
device_read_bps=blkio_rate,
73+
device_write_bps=blkio_rate,
74+
device_read_iops=blkio_rate,
75+
device_write_iops=blkio_rate)
76+
77+
self.assertEqual(config.get('BlkioWeight'), 1999)
78+
self.assertTrue(config.get('BlkioWeightDevice') is blkio_rate)
79+
self.assertTrue(config.get('BlkioDeviceReadBps') is blkio_rate)
80+
self.assertTrue(config.get('BlkioDeviceWriteBps') is blkio_rate)
81+
self.assertTrue(config.get('BlkioDeviceReadIOps') is blkio_rate)
82+
self.assertTrue(config.get('BlkioDeviceWriteIOps') is blkio_rate)
83+
self.assertEqual(blkio_rate[0]['Path'], "/dev/sda")
84+
self.assertEqual(blkio_rate[0]['Rate'], 1000)
85+
6786
def test_create_host_config_with_shm_size(self):
6887
config = create_host_config(version='1.22', shm_size=67108864)
6988
self.assertEqual(config.get('ShmSize'), 67108864)

0 commit comments

Comments
 (0)