Skip to content

Instantly share code, notes, and snippets.

@rwngwn
Created June 3, 2018 13:24
Show Gist options
  • Save rwngwn/49db5e9a9240ab8f92847a8a7b22e9a0 to your computer and use it in GitHub Desktop.
Save rwngwn/49db5e9a9240ab8f92847a8a7b22e9a0 to your computer and use it in GitHub Desktop.
import os
groups_types = {'cpu': '/sys/fs/cgroup/cpu/system.slice/',
'memory': '/sys/fs/cgroup/memory/system.slice/'}
def create_cgroups(name):
for group, path in groups_types.items():
if os.path.exists(path):
target = os.path.join(path, name)
if not os.path.exists(target):
os.makedirs(target)
else:
print('%s cgroup is not mounted' % group)
def assing_cgroups(name, pid):
for _, path in groups_types.items():
path = os.path.join(path, name, 'cgroup.procs')
_write_data(path, pid)
def set_cpu_shares(name, shares):
path = os.path.join(groups_types['cpu'], name, 'cpu.shares')
_write_data(path, shares)
def set_memory_limit(name, limit):
path = os.path.join(groups_types['memory'], name, 'cpu.shares')
_write_data(path, limit)
def _write_data(path, data):
if not os.path.exists(path):
return
with open(path, 'w') as fd:
fd.write('%d\n' % int(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment