def kill_apps_in_cgroup(subsystem, cgrp, delete_cgrp=False): """Kill all apps found in a cgroup""" path = cgroups.makepath(subsystem, cgrp, 'tasks') tasks_files = glob.glob(path) for tasks_file in tasks_files: cgrp_path = os.path.dirname(tasks_file) try: with io.open(tasks_file) as tasks: for pid in tasks: _LOGGER.info('killing process from %r: %s', tasks_file, pid) try: os.kill(int(pid), signal.SIGKILL) except OSError as err: # it is OK to fail to find the PID if err.errno == errno.ESRCH: continue _LOGGER.exception('Unable to kill processes in %r: %s', cgrp_path, err) except IOError as err: # it is OK to fail if the tasks file is already gone if err.errno == errno.ENOENT: _LOGGER.debug('Skipping nonexistent cgroup %r', cgrp_path) continue raise if delete_cgrp: cgrp = cgroups.extractpath(cgrp_path, subsystem) delete(subsystem, cgrp)
def delete(system, group): """ safely delete cgroup path """ fullpath = cgroups.makepath(system, group) for (dirname, _subdirs, _files) in os.walk(fullpath, topdown=False): cgrp = cgroups.extractpath(dirname, system) # free empty memory before the cgroups is destroyed if system == 'memory': memory_force_empty(cgrp) try: cgroups.delete(system, cgrp) except OSError as err: _LOGGER.exception('Unable remove cgroup %s %s, %r', system, cgrp, err) raise err
def test_extractpath(self): """ test cgroup name from a cgroup path""" treadmill.cgroups.get_mountpoint.return_value = '/fs/cgroup/memory' cgrp = cgroups.extractpath('/fs/cgroup/memory/treadmill/core', 'memory') self.assertEqual(cgrp, 'treadmill/core') cgrp = cgroups.extractpath('/fs/cgroup/memory/treadmill/core/foo', 'memory', 'foo') self.assertEqual(cgrp, 'treadmill/core') with self.assertRaises(ValueError): cgroups.extractpath('/cgroup/memory/treadmill/core', 'memory') with self.assertRaises(ValueError): cgroups.extractpath('/fs/cgroup/memory/treadmill/core/foo', 'cpu', 'bar')