Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 1 | # Copyright 2014-2015 ARM Limited |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | # pylint: disable=attribute-defined-outside-init |
| 16 | import logging |
| 17 | from collections import namedtuple |
| 18 | |
| 19 | from devlib.module import Module |
| 20 | from devlib.exception import TargetError |
| 21 | from devlib.utils.misc import list_to_ranges, isiterable |
| 22 | from devlib.utils.types import boolean |
| 23 | |
| 24 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 25 | class Controller(object): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 26 | |
| 27 | def __new__(cls, arg): |
| 28 | if isinstance(arg, cls): |
| 29 | return arg |
| 30 | else: |
| 31 | return object.__new__(cls, arg) |
| 32 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 33 | def __init__(self, kind): |
| 34 | self.mount_name = 'devlib_'+kind |
| 35 | self.kind = kind |
| 36 | self.target = None |
Patrick Bellasi | 15f9c03 | 2016-04-26 15:23:56 +0100 | [diff] [blame] | 37 | self._noprefix = False |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 38 | |
| 39 | self.logger = logging.getLogger('cgroups.'+self.kind) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 40 | self.mount_point = None |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 41 | self._cgroups = {} |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 42 | |
| 43 | def probe(self, target): |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 44 | try: |
| 45 | exists = target.execute('{} grep {} /proc/cgroups'\ |
| 46 | .format(target.busybox, self.kind)) |
| 47 | except TargetError: |
| 48 | return False |
| 49 | return True |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 50 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 51 | def mount(self, target, mount_root): |
| 52 | |
| 53 | mounted = target.list_file_systems() |
| 54 | if self.mount_name in [e.device for e in mounted]: |
| 55 | # Identify mount point if controller is already in use |
| 56 | self.mount_point = [ |
| 57 | fs.mount_point |
| 58 | for fs in mounted |
| 59 | if fs.device == self.mount_name |
| 60 | ][0] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 61 | else: |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 62 | # Mount the controller if not already in use |
| 63 | self.mount_point = target.path.join(mount_root, self.mount_name) |
| 64 | target.execute('mkdir -p {} 2>/dev/null'\ |
| 65 | .format(self.mount_point), as_root=True) |
| 66 | target.execute('mount -t cgroup -o {} {} {}'\ |
| 67 | .format(self.kind, |
| 68 | self.mount_name, |
| 69 | self.mount_point), |
| 70 | as_root=True) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 71 | |
Patrick Bellasi | 15f9c03 | 2016-04-26 15:23:56 +0100 | [diff] [blame] | 72 | # Check if this controller uses "noprefix" option |
| 73 | output = target.execute('mount | grep "{} "'.format(self.mount_name)) |
| 74 | if 'noprefix' in output: |
| 75 | self._noprefix = True |
| 76 | # self.logger.debug('Controller %s using "noprefix" option', |
| 77 | # self.kind) |
| 78 | |
| 79 | self.logger.debug('Controller %s mounted under: %s (noprefix=%s)', |
| 80 | self.kind, self.mount_point, self._noprefix) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 81 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 82 | # Mark this contoller as available |
| 83 | self.target = target |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 84 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 85 | # Create root control group |
| 86 | self.cgroup('/') |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 87 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 88 | def cgroup(self, name): |
| 89 | if not self.target: |
| 90 | raise RuntimeError('CGroup creation failed: {} controller not mounted'\ |
| 91 | .format(self.kind)) |
| 92 | if name not in self._cgroups: |
| 93 | self._cgroups[name] = CGroup(self, name) |
| 94 | return self._cgroups[name] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 95 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 96 | def exists(self, name): |
| 97 | if not self.target: |
| 98 | raise RuntimeError('CGroup creation failed: {} controller not mounted'\ |
| 99 | .format(self.kind)) |
| 100 | if name not in self._cgroups: |
| 101 | self._cgroups[name] = CGroup(self, name, create=False) |
| 102 | return self._cgroups[name].existe() |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 103 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 104 | def list_all(self): |
| 105 | self.logger.debug('Listing groups for %s controller', self.kind) |
| 106 | output = self.target.execute('{} find {} -type d'\ |
Patrick Bellasi | 3acf5d5 | 2016-04-26 15:31:05 +0100 | [diff] [blame] | 107 | .format(self.target.busybox, self.mount_point), |
| 108 | as_root=True) |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 109 | cgroups = [] |
Patrick Bellasi | e2e5e68 | 2016-02-23 12:12:28 +0000 | [diff] [blame] | 110 | for cg in output.splitlines(): |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 111 | cg = cg.replace(self.mount_point + '/', '/') |
| 112 | cg = cg.replace(self.mount_point, '/') |
| 113 | cg = cg.strip() |
| 114 | if cg == '': |
| 115 | continue |
| 116 | self.logger.debug('Populate %s cgroup: %s', self.kind, cg) |
| 117 | cgroups.append(cg) |
| 118 | return cgroups |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 119 | |
Patrick Bellasi | 3cab786 | 2016-08-26 16:27:23 +0100 | [diff] [blame] | 120 | def move_tasks(self, source, dest, exclude=[]): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 121 | try: |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 122 | srcg = self._cgroups[source] |
| 123 | dstg = self._cgroups[dest] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 124 | except KeyError as e: |
| 125 | raise ValueError('Unkown group: {}'.format(e)) |
Patrick Bellasi | 3cab786 | 2016-08-26 16:27:23 +0100 | [diff] [blame] | 126 | output = self.target._execute_util( |
| 127 | 'cgroups_tasks_move {} {} \'{}\''.format( |
| 128 | srcg.directory, dstg.directory, exclude), |
| 129 | as_root=True) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 130 | |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 131 | def move_all_tasks_to(self, dest, exclude=[]): |
| 132 | """ |
| 133 | Move all the tasks to the specified CGroup |
| 134 | |
| 135 | Tasks are moved from all their original CGroup the the specified on. |
| 136 | The tasks which name matches one of the string in exclude are moved |
| 137 | instead in the root CGroup for the controller. |
| 138 | The name of a tasks to exclude must be a substring of the task named as |
| 139 | reported by the "ps" command. Indeed, this list will be translated into |
| 140 | a: "ps | grep -e name1 -e name2..." in order to obtain the PID of these |
| 141 | tasks. |
| 142 | |
| 143 | :param exclude: list of commands to keep in the root CGroup |
| 144 | :type exlude: list(str) |
| 145 | """ |
| 146 | |
| 147 | if isinstance(exclude, str): |
| 148 | exclude = [exclude] |
| 149 | if not isinstance(exclude, list): |
| 150 | raise ValueError('wrong type for "exclude" parameter, ' |
| 151 | 'it must be a str or a list') |
| 152 | |
Patrick Bellasi | c8f118d | 2016-08-31 11:39:33 +0100 | [diff] [blame] | 153 | logging.debug('Moving all tasks into %s', dest) |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 154 | |
| 155 | # Build list of tasks to exclude |
| 156 | grep_filters = '' |
| 157 | for comm in exclude: |
| 158 | grep_filters += '-e "{}" '.format(comm) |
Patrick Bellasi | c8f118d | 2016-08-31 11:39:33 +0100 | [diff] [blame] | 159 | logging.debug(' using grep filter: %s', grep_filters) |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 160 | if grep_filters != '': |
Patrick Bellasi | c8f118d | 2016-08-31 11:39:33 +0100 | [diff] [blame] | 161 | logging.debug(' excluding tasks which name matches:') |
| 162 | logging.debug(' %s', ', '.join(exclude)) |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 163 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 164 | for cgroup in self._cgroups: |
| 165 | if cgroup != dest: |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 166 | self.move_tasks(cgroup, dest, grep_filters) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 167 | |
Patrick Bellasi | 42efd0a | 2016-08-26 16:25:43 +0100 | [diff] [blame] | 168 | def tasks(self, cgroup): |
| 169 | try: |
| 170 | cg = self._cgroups[cgroup] |
| 171 | except KeyError as e: |
| 172 | raise ValueError('Unkown group: {}'.format(e)) |
| 173 | output = self.target._execute_util( |
| 174 | 'cgroups_tasks_in {}'.format(cg.directory), |
| 175 | as_root=True) |
| 176 | entries = output.splitlines() |
| 177 | tasks = {} |
| 178 | for task in entries: |
| 179 | tid = task.split(',')[0] |
| 180 | try: |
| 181 | tname = task.split(',')[1] |
| 182 | except: continue |
| 183 | try: |
| 184 | tcmdline = task.split(',')[2] |
| 185 | except: |
| 186 | tcmdline = '' |
| 187 | tasks[int(tid)] = (tname, tcmdline) |
| 188 | return tasks |
| 189 | |
Patrick Bellasi | d8ae3ab | 2016-08-26 16:28:45 +0100 | [diff] [blame] | 190 | def tasks_count(self, cgroup): |
| 191 | try: |
| 192 | cg = self._cgroups[cgroup] |
| 193 | except KeyError as e: |
| 194 | raise ValueError('Unkown group: {}'.format(e)) |
| 195 | output = self.target.execute( |
| 196 | '{} wc -l {}/tasks'.format( |
| 197 | self.target.busybox, cg.directory), |
| 198 | as_root=True) |
| 199 | return int(output.split()[0]) |
| 200 | |
| 201 | def tasks_per_group(self): |
| 202 | tasks = {} |
| 203 | for cg in self.list_all(): |
| 204 | tasks[cg] = self.tasks_count(cg) |
| 205 | return tasks |
| 206 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 207 | class CGroup(object): |
| 208 | |
| 209 | def __init__(self, controller, name, create=True): |
| 210 | self.logger = logging.getLogger('cgroups.' + controller.kind) |
| 211 | self.target = controller.target |
| 212 | self.controller = controller |
| 213 | self.name = name |
| 214 | |
| 215 | # Control cgroup path |
| 216 | self.directory = controller.mount_point |
| 217 | if name != '/': |
| 218 | self.directory = self.target.path.join(controller.mount_point, name[1:]) |
| 219 | |
| 220 | # Setup path for tasks file |
| 221 | self.tasks_file = self.target.path.join(self.directory, 'tasks') |
| 222 | self.procs_file = self.target.path.join(self.directory, 'cgroup.procs') |
| 223 | |
| 224 | if not create: |
| 225 | return |
| 226 | |
Patrick Bellasi | 7112cfe | 2016-02-25 16:54:16 +0000 | [diff] [blame] | 227 | self.logger.debug('Creating cgroup %s', self.directory) |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 228 | self.target.execute('[ -d {0} ] || mkdir -p {0}'\ |
| 229 | .format(self.directory), as_root=True) |
| 230 | |
| 231 | def exists(self): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 232 | try: |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 233 | self.target.execute('[ -d {0} ]'\ |
Patrick Bellasi | 3acf5d5 | 2016-04-26 15:31:05 +0100 | [diff] [blame] | 234 | .format(self.directory), as_root=True) |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 235 | return True |
| 236 | except TargetError: |
| 237 | return False |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 238 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 239 | def get(self): |
| 240 | conf = {} |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 241 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 242 | logging.debug('Reading %s attributes from:', |
| 243 | self.controller.kind) |
| 244 | logging.debug(' %s', |
| 245 | self.directory) |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 246 | output = self.target._execute_util( |
| 247 | 'cgroups_get_attributes {} {}'.format( |
Patrick Bellasi | 3acf5d5 | 2016-04-26 15:31:05 +0100 | [diff] [blame] | 248 | self.directory, self.controller.kind), |
| 249 | as_root=True) |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 250 | for res in output.splitlines(): |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 251 | attr = res.split(':')[0] |
| 252 | value = res.split(':')[1] |
| 253 | conf[attr] = value |
| 254 | |
| 255 | return conf |
| 256 | |
| 257 | def set(self, **attrs): |
| 258 | for idx in attrs: |
| 259 | if isiterable(attrs[idx]): |
| 260 | attrs[idx] = list_to_ranges(attrs[idx]) |
| 261 | # Build attribute path |
Patrick Bellasi | 658005a | 2016-04-26 15:26:44 +0100 | [diff] [blame] | 262 | if self.controller._noprefix: |
| 263 | attr_name = '{}'.format(idx) |
| 264 | else: |
| 265 | attr_name = '{}.{}'.format(self.controller.kind, idx) |
| 266 | path = self.target.path.join(self.directory, attr_name) |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 267 | |
| 268 | self.logger.debug('Set attribute [%s] to: %s"', |
| 269 | path, attrs[idx]) |
| 270 | |
| 271 | # Set the attribute value |
Patrick Bellasi | 658005a | 2016-04-26 15:26:44 +0100 | [diff] [blame] | 272 | try: |
| 273 | self.target.write_value(path, attrs[idx]) |
| 274 | except TargetError: |
| 275 | # Check if the error is due to a non-existing attribute |
| 276 | attrs = self.get() |
| 277 | if idx not in attrs: |
| 278 | raise ValueError('Controller [{}] does not provide attribute [{}]'\ |
| 279 | .format(self.controller.kind, attr_name)) |
| 280 | raise |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 281 | |
| 282 | def get_tasks(self): |
| 283 | task_ids = self.target.read_value(self.tasks_file).split() |
| 284 | logging.debug('Tasks: %s', task_ids) |
| 285 | return map(int, task_ids) |
| 286 | |
| 287 | def add_task(self, tid): |
| 288 | self.target.write_value(self.tasks_file, tid, verify=False) |
| 289 | |
| 290 | def add_tasks(self, tasks): |
| 291 | for tid in tasks: |
| 292 | self.add_task(tid) |
| 293 | |
| 294 | def add_proc(self, pid): |
| 295 | self.target.write_value(self.procs_file, pid, verify=False) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 296 | |
| 297 | CgroupSubsystemEntry = namedtuple('CgroupSubsystemEntry', 'name hierarchy num_cgroups enabled') |
| 298 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 299 | class CgroupsModule(Module): |
| 300 | |
| 301 | name = 'cgroups' |
Patrick Bellasi | 616f229 | 2016-05-13 18:17:22 +0100 | [diff] [blame] | 302 | stage = 'setup' |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 303 | cgroup_root = '/sys/fs/cgroup' |
| 304 | |
| 305 | @staticmethod |
| 306 | def probe(target): |
Patrick Bellasi | 4b58c57 | 2016-04-26 15:33:23 +0100 | [diff] [blame] | 307 | if not target.is_rooted: |
| 308 | return False |
| 309 | if target.file_exists('/proc/cgroups'): |
| 310 | return True |
| 311 | return target.config.has('cgroups') |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 312 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 313 | def __init__(self, target): |
| 314 | super(CgroupsModule, self).__init__(target) |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 315 | |
| 316 | self.logger = logging.getLogger('CGroups') |
| 317 | |
| 318 | # Initialize controllers mount point |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 319 | mounted = self.target.list_file_systems() |
| 320 | if self.cgroup_root not in [e.mount_point for e in mounted]: |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 321 | self.target.execute('mount -t tmpfs {} {}'\ |
| 322 | .format('cgroup_root', |
| 323 | self.cgroup_root), |
| 324 | as_root=True) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 325 | else: |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 326 | self.logger.debug('cgroup_root already mounted at %s', |
| 327 | self.cgroup_root) |
| 328 | |
Patrick Bellasi | da128f9 | 2016-10-28 18:54:10 +0100 | [diff] [blame] | 329 | # Ensure CGroups is mounted RW |
| 330 | self.target.execute('mount -o remount,rw {}'\ |
| 331 | .format(self.cgroup_root), |
| 332 | as_root=True) |
| 333 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 334 | # Load list of available controllers |
| 335 | controllers = [] |
| 336 | subsys = self.list_subsystems() |
| 337 | for (n, h, c, e) in subsys: |
| 338 | controllers.append(n) |
Patrick Bellasi | 7112cfe | 2016-02-25 16:54:16 +0000 | [diff] [blame] | 339 | self.logger.debug('Available controllers: %s', controllers) |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 340 | |
| 341 | # Initialize controllers |
| 342 | self.controllers = {} |
| 343 | for idx in controllers: |
| 344 | controller = Controller(idx) |
| 345 | self.logger.debug('Init %s controller...', controller.kind) |
| 346 | if not controller.probe(self.target): |
| 347 | continue |
| 348 | try: |
| 349 | controller.mount(self.target, self.cgroup_root) |
| 350 | except TargetError: |
| 351 | message = 'cgroups {} controller is not supported by the target' |
| 352 | raise TargetError(message.format(controller.kind)) |
| 353 | self.logger.debug('Controller %s enabled', controller.kind) |
| 354 | self.controllers[idx] = controller |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 355 | |
| 356 | def list_subsystems(self): |
| 357 | subsystems = [] |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 358 | for line in self.target.execute('{} cat /proc/cgroups'\ |
Patrick Bellasi | e2e5e68 | 2016-02-23 12:12:28 +0000 | [diff] [blame] | 359 | .format(self.target.busybox)).splitlines()[1:]: |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 360 | line = line.strip() |
| 361 | if not line or line.startswith('#'): |
| 362 | continue |
| 363 | name, hierarchy, num_cgroups, enabled = line.split() |
| 364 | subsystems.append(CgroupSubsystemEntry(name, |
| 365 | int(hierarchy), |
| 366 | int(num_cgroups), |
| 367 | boolean(enabled))) |
| 368 | return subsystems |
| 369 | |
| 370 | |
Patrick Bellasi | 9c9a748 | 2015-11-03 11:49:31 +0000 | [diff] [blame] | 371 | def controller(self, kind): |
| 372 | if kind not in self.controllers: |
| 373 | self.logger.warning('Controller %s not available', kind) |
| 374 | return None |
| 375 | return self.controllers[kind] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 376 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 377 | def run_into(self, cgroup, cmdline): |
| 378 | """ |
| 379 | Run the specified command into the specified CGroup |
| 380 | """ |
| 381 | return self.target._execute_util( |
| 382 | 'cgroups_run_into {} {}'.format(cgroup, cmdline), |
| 383 | as_root=True) |
| 384 | |
| 385 | |
| 386 | def cgroups_tasks_move(self, srcg, dstg, exclude=''): |
| 387 | """ |
| 388 | Move all the tasks from the srcg CGroup to the dstg one. |
| 389 | A regexps of tasks names can be used to defined tasks which should not |
| 390 | be moved. |
| 391 | """ |
| 392 | return self.target._execute_util( |
| 393 | 'cgroups_tasks_move {} {} {}'.format(srcg, dstg, exclude), |
| 394 | as_root=True) |
| 395 | |
Patrick Bellasi | 75a086d | 2016-08-26 16:29:43 +0100 | [diff] [blame] | 396 | def isolate(self, cpus, exclude=[]): |
| 397 | """ |
| 398 | Remove all userspace tasks from specified CPUs. |
| 399 | |
| 400 | A list of CPUs can be specified where we do not want userspace tasks |
| 401 | running. This functions creates a sandbox cpuset CGroup where all |
| 402 | user-space tasks and not-pinned kernel-space tasks are moved into. |
| 403 | This should allows to isolate the specified CPUs which will not get |
| 404 | tasks running unless explicitely moved into the isolated group. |
| 405 | |
| 406 | :param cpus: the list of CPUs to isolate |
| 407 | :type cpus: list(int) |
| 408 | |
| 409 | :return: the (sandbox, isolated) tuple, where: |
| 410 | sandbox is the CGroup of sandboxed CPUs |
| 411 | isolated is the CGroup of isolated CPUs |
| 412 | """ |
| 413 | all_cpus = set(range(self.target.number_of_cpus)) |
| 414 | sbox_cpus = list(all_cpus - set(cpus)) |
| 415 | isol_cpus = list(all_cpus - set(sbox_cpus)) |
| 416 | |
| 417 | # Create Sandbox and Isolated cpuset CGroups |
| 418 | cpuset = self.controller('cpuset') |
| 419 | sbox_cg = cpuset.cgroup('/DEVLIB_SBOX') |
| 420 | isol_cg = cpuset.cgroup('/DEVLIB_ISOL') |
| 421 | |
| 422 | # Set CPUs for Sandbox and Isolated CGroups |
| 423 | sbox_cg.set(cpus=sbox_cpus, mems=0) |
| 424 | isol_cg.set(cpus=isol_cpus, mems=0) |
| 425 | |
| 426 | # Move all currently running tasks to the Sandbox CGroup |
| 427 | cpuset.move_all_tasks_to('/DEVLIB_SBOX', exclude) |
| 428 | |
| 429 | return sbox_cg, isol_cg |
| 430 | |
Patrick Bellasi | 23ad61f | 2016-08-26 18:14:37 +0100 | [diff] [blame] | 431 | def freeze(self, exclude=[], thaw=False): |
| 432 | """ |
Patrick Bellasi | 730bb60 | 2016-08-31 11:40:10 +0100 | [diff] [blame] | 433 | Freeze all user-space tasks but the specified ones |
Patrick Bellasi | 23ad61f | 2016-08-26 18:14:37 +0100 | [diff] [blame] | 434 | |
| 435 | A freezer cgroup is used to stop all the tasks in the target system but |
| 436 | the ones which name match one of the path specified by the exclude |
| 437 | paramater. The name of a tasks to exclude must be a substring of the |
| 438 | task named as reported by the "ps" command. Indeed, this list will be |
| 439 | translated into a: "ps | grep -e name1 -e name2..." in order to obtain |
| 440 | the PID of these tasks. |
| 441 | |
| 442 | :param exclude: list of commands paths to exclude from freezer |
Patrick Bellasi | 730bb60 | 2016-08-31 11:40:10 +0100 | [diff] [blame] | 443 | :type exclude: list(str) |
| 444 | |
| 445 | :param thaw: if true thaw tasks instead |
| 446 | :type thaw: bool |
Patrick Bellasi | 23ad61f | 2016-08-26 18:14:37 +0100 | [diff] [blame] | 447 | """ |
| 448 | |
| 449 | # Create Freezer CGroup |
| 450 | freezer = self.controller('freezer') |
Brendan Jackman | c89f712 | 2016-11-25 11:56:42 +0000 | [diff] [blame^] | 451 | if freezer is None: |
| 452 | raise RuntimeError('freezer cgroup controller not present') |
Patrick Bellasi | 23ad61f | 2016-08-26 18:14:37 +0100 | [diff] [blame] | 453 | freezer_cg = freezer.cgroup('/DEVLIB_FREEZER') |
| 454 | thawed_cg = freezer.cgroup('/') |
| 455 | |
| 456 | if thaw: |
| 457 | # Restart froozen tasks |
| 458 | freezer_cg.set(state='THAWED') |
| 459 | # Remove all tasks from freezer |
| 460 | freezer.move_all_tasks_to('/') |
| 461 | return |
| 462 | |
| 463 | # Move all tasks into the freezer group |
| 464 | freezer.move_all_tasks_to('/DEVLIB_FREEZER', exclude) |
| 465 | |
Patrick Bellasi | c8f118d | 2016-08-31 11:39:33 +0100 | [diff] [blame] | 466 | # Get list of not frozen tasks, which is reported as output |
Patrick Bellasi | 23ad61f | 2016-08-26 18:14:37 +0100 | [diff] [blame] | 467 | tasks = freezer.tasks('/') |
Patrick Bellasi | 23ad61f | 2016-08-26 18:14:37 +0100 | [diff] [blame] | 468 | |
| 469 | # Freeze all tasks |
| 470 | freezer_cg.set(state='FROZEN') |
| 471 | |
Patrick Bellasi | c8f118d | 2016-08-31 11:39:33 +0100 | [diff] [blame] | 472 | return tasks |
| 473 | |