1 # Simple wrapper arround cgroups so we don't have to worry the type of
2 # virtualization the sliver runs on (lxc, qemu/kvm, etc.) managed by libvirt
4 # Xavi Leon <xleon@ac.upc.edu>
10 # Base dir for libvirt
11 BASE_DIR = '/sys/fs/cgroup'
13 def get_cgroup_paths(subsystem="cpuset"):
16 os.path.join(BASE_DIR, subsystem, 'libvirt', 'lxc'),
18 os.path.join(BASE_DIR, subsystem ),
20 os.path.join(BASE_DIR, subsystem, 'machine.slice'),
21 # as observed on f16 libvirt 1.2.1
22 os.path.join(BASE_DIR, subsystem, 'machine'),
24 # try several locations and return all the results
25 # get_cgroup_path will sort it out
27 # just return all the subdirs in the listed bases
30 for subsystem_base in subsystem_bases if os.path.isdir(subsystem_base)
31 # in each base search the immediate sons that are also dirs
32 for subdir in [ os.path.join(subsystem_base, f) for f in os.listdir(subsystem_base) ]
33 if os.path.isdir(subdir) ]
35 def get_cgroup_path(name, subsystem="cpuset"):
37 Returns the base path for the cgroup with a specific name or None.
39 result = reduce(lambda a, b: b if name in os.path.basename(b) else a,
40 get_cgroup_paths(subsystem), None)
43 name = name + ".libvirt-lxc"
44 result = reduce(lambda a, b: b if name in os.path.basename(b) else a,
45 get_cgroup_paths(subsystem), None)
53 """ Returns the list of cgroups active at this moment on the node """
54 return map(os.path.basename, get_cgroup_paths())
56 def write(name, key, value, subsystem="cpuset"):
57 """ Writes a value to the file key with the cgroup with name """
58 base_path = get_cgroup_path(name, subsystem)
59 with open(os.path.join(base_path, key), 'w') as f:
61 logger.verbose("cgroups.write: overwrote {}".format(base_path))
63 def append(name, key, value, subsystem="cpuset"):
64 """ Appends a value to the file key with the cgroup with name """
65 base_path = get_cgroup_path(name, subsystem)
66 with open(os.path.join(base_path, key), 'a') as f:
68 logger.verbose("cgroups.append: appended {}".format(base_path))
70 if __name__ == '__main__':
72 # goes with the system tests
75 subsystems = 'blkio cpu cpu,cpuacct cpuacct cpuset devices freezer memory net_cls perf_event systemd'.split()
77 for subsystem in subsystems:
78 print 'get_cgroup_path({}, {}) = {}'.\
79 format(name, subsystem, get_cgroup_path(name, subsystem))
81 # print 'get_cgroup_paths = {}'.format(get_cgroup_paths(subsystem))