def set_memory_hardlimit(cgrp, limit):
"""Set the cgroup hard-limits to the desired value.
The logic here is complicated since the ordering of operations depends on
weither we are lowering or raising the value.
"""
def _lower_memory_hardlimit(cgrp, limit):
"""Lower the cgroup memory hardlimit."""
cgroups.set_value('memory', cgrp,
'memory.limit_in_bytes', limit)
cgroups.set_value('memory', cgrp,
'memory.memsw.limit_in_bytes', limit)
def _raise_memory_hardlimit(cgrp, limit):
"""Raise the cgroup memory hardlimit."""
cgroups.set_value('memory', cgrp,
'memory.memsw.limit_in_bytes', limit)
cgroups.set_value('memory', cgrp,
'memory.limit_in_bytes', limit)
memory_hardlimit_funs = [_lower_memory_hardlimit, _raise_memory_hardlimit]
while memory_hardlimit_funs:
try:
memory_hardlimit_fun = memory_hardlimit_funs.pop(0)
memory_hardlimit_fun(cgrp, limit)
break
except IOError as err:
if err.errno == errno.EBUSY:
# Resource busy, this means the cgroup is already using more
# then the limit we are trying to set.
raise TreadmillCgroupError('Unable to set hard limit to %d. '
'Cgroup %r memory over limit.' %
(limit, cgrp))
elif err.errno == errno.EINVAL:
# This means we did the hard limit operation in the wrong
# order. If we have a different ordering to try, go ahead.
if memory_hardlimit_funs:
continue
# For any other case, raise the exception
raise
评论列表
文章目录