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'
12 SUB_SYSTEMS = ['blkio', 'freezer', 'devices', 'memory', 'cpu,cpuacct', 'cpuset']
15 # Global cgroup mapping.
18 class CgroupWatch(pyinotify.ProcessEvent):
20 def process_IN_CREATE(self, event):
21 path = os.path.join(event.path, event.name)
22 CGROUPS[event.name] = path
23 logger.verbose("Cgroup Notify: Created cgroup %s on %s" % \
24 (event.name, event.path))
26 def process_IN_DELETE(self, event):
28 del CGROUPS[event.name]
30 logger.verbose("Cgroup Notify: Cgroup %s does not exist, continuing..."%event.name)
31 logger.verbose("Cgroup Notify: Deleted cgroup %s on %s" % \
32 (event.name, event.path))
35 #logger.verbose("Cgroups: Recognizing already existing cgroups...")
36 #for virt in VIRT_TECHS:
37 # filenames = os.listdir(os.path.join(BASE_DIR, virt))
38 # for filename in filenames:
39 # path = os.path.join(BASE_DIR, virt, filename)
40 # if os.path.isdir(path):
41 # CGROUPS[filename] = path
43 #logger.verbose("Cgroups: Initializing watchers...")
44 #wm = pyinotify.WatchManager()
45 #notifier = pyinotify.ThreadedNotifier(wm, CgroupWatch())
46 #for virt in VIRT_TECHS:
47 # wdd = wm.add_watch(os.path.join(BASE_DIR, virt),
48 # pyinotify.IN_DELETE | pyinotify.IN_CREATE,
50 #notifier.daemon = True
53 def get_cgroup_paths():
54 cpusetBase = os.path.join(BASE_DIR, 'cpuset', 'libvirt', 'lxc')
55 return filter(os.path.isdir,
56 map(lambda f: os.path.join(cpusetBase, f),
57 os.listdir(cpusetBase)))
59 def get_cgroup_path(name):
60 """ Returns the base path for the cgroup with a specific name or None."""
61 return reduce(lambda a, b: b if os.path.basename(b) == name else a,
62 get_cgroup_paths(), None)
68 """ Returns the list of cgroups active at this moment on the node """
69 return map(os.path.basename, get_cgroup_paths())
71 def write(name, key, value):
72 """ Writes a value to the file key with the cgroup with name """
73 base_path = get_cgroup_path(name)
74 with open(os.path.join(base_path, key), 'w') as f:
77 def append(name, key, value):
78 """ Appends a value to the file key with the cgroup with name """
79 base_path = get_cgroup_path(name)
80 with open(os.path.join(base_path, key), 'a') as f: