minor cleanup, just removed dead code
[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, os.path
7 import pyinotify
8 import logger
9
10 # Base dir for libvirt
11 BASE_DIR = '/sys/fs/cgroup'
12
13 def get_cgroup_paths(subsystem="cpuset"):
14     subsystem_bases = [ 
15         # observed on f16-f18
16         os.path.join(BASE_DIR, subsystem, 'libvirt', 'lxc'),
17         # as observed on f20
18         os.path.join(BASE_DIR, subsystem ),
19         # f21
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'),
23         ]
24     # try several locations and return all the results
25     # get_cgroup_path will sort it out
26     
27     # just return all the subdirs in the listed bases 
28     return [ subdir
29                  # scan the 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) ]
34
35 def get_cgroup_path(name, subsystem="cpuset"):
36     """
37     Returns the base path for the cgroup with a specific name or None.
38     """
39     result = reduce(lambda a, b: b if name in os.path.basename(b) else a,
40                     get_cgroup_paths(subsystem), None)
41
42     if result is 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)
46
47     return result
48
49 def get_base_path():
50     return BASE_DIR
51
52 def get_cgroups():
53     """ Returns the list of cgroups active at this moment on the node """
54     return map(os.path.basename, get_cgroup_paths())
55
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:
60         print >>f, value
61     logger.verbose("cgroups.write: overwrote {}".format(base_path))
62
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:
67         print >>f, value
68     logger.verbose("cgroups.append: appended {}".format(base_path))
69
70 if __name__ == '__main__':
71
72     # goes with the system tests
73     name='inri_sl1' 
74
75     subsystems = 'blkio cpu cpu,cpuacct cpuacct cpuset devices freezer memory net_cls perf_event systemd'.split()
76
77     for subsystem in subsystems:
78         print 'get_cgroup_path({}, {}) =  {}'.\
79             format(name, subsystem, get_cgroup_path(name, subsystem))
80
81 #        print 'get_cgroup_paths = {}'.format(get_cgroup_paths(subsystem))