support for writing cgroups in subsystems other than cpuset
[nodemanager.git] / cgroups.py
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
3 #
4 # Xavi Leon <xleon@ac.upc.edu>
5
6 import os
7 import pyinotify
8 import logger
9
10 # Base dir for libvirt
11 BASE_DIR = '/sys/fs/cgroup'
12 SUB_SYSTEMS = ['blkio', 'freezer', 'devices', 'memory', 'cpu,cpuacct', 'cpuset']
13 VIRT_TECHS = ['lxc']
14
15 # Global cgroup mapping. 
16 CGROUPS = dict()
17
18 class CgroupWatch(pyinotify.ProcessEvent):
19
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))
25         
26     def process_IN_DELETE(self, event):
27         try:
28             del CGROUPS[event.name]
29         except:
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))
33
34
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
42
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,
49 #               rec=False)
50 #notifier.daemon = True
51 #notifier.start()
52
53 def get_cgroup_paths(subsystem="cpuset"):
54     cpusetBase  = os.path.join(BASE_DIR, subsystem, 'libvirt', 'lxc')
55     return filter(os.path.isdir,
56                   map(lambda f: os.path.join(cpusetBase, f),
57                       os.listdir(cpusetBase)))
58
59 def get_cgroup_path(name, subsystem="cpuset"):
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(subsystem), None)
63
64 def get_base_path():
65     return BASE_DIR
66
67 def get_cgroups():
68     """ Returns the list of cgroups active at this moment on the node """
69     return map(os.path.basename, get_cgroup_paths())
70
71 def write(name, key, value, subsystem="cpuset"):
72     """ Writes a value to the file key with the cgroup with name """
73     base_path = get_cgroup_path(name, subsystem)
74     with open(os.path.join(base_path, key), 'w') as f:
75         print >>f, value
76
77 def append(name, key, value, subsystem="cpuset"):
78     """ Appends a value to the file key with the cgroup with name """
79     base_path = get_cgroup_path(name, subsystem)
80     with open(os.path.join(base_path, key), 'a') as f:
81         print >>f, value