-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcgroup_util.py
52 lines (34 loc) · 1.1 KB
/
cgroup_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import logging
import os
from pykit import fsutil
logger = logging.getLogger(__name__)
def create_cgroup(cgroup_path):
os.mkdir(cgroup_path, 0755)
return
def add_pids(cgroup_path, pids):
task_file = os.path.join(cgroup_path, 'tasks')
for pid in pids:
try:
fsutil.write_file(task_file, str(pid), fsync=False)
except Exception as e:
logger.info('failed to add pid: %s to file: %s, %s' %
(str(pid), task_file, repr(e)))
return
def clear_pids(subsystem_dir, cgroup_path):
task_file = os.path.join(cgroup_path, 'tasks')
f = open(task_file, 'r')
while True:
line = f.readline()
if line == '':
break
pid = line.strip()
add_pids(subsystem_dir, [pid])
return
def remove_cgroup(subsystem_dir, cgroup_path):
sub_dirs = fsutil.get_sub_dirs(cgroup_path)
for sub_dir in sub_dirs:
sub_cgroup_path = os.path.join(cgroup_path, sub_dir)
remove_cgroup(subsystem_dir, sub_cgroup_path)
clear_pids(subsystem_dir, cgroup_path)
os.rmdir(cgroup_path)
return