def rename_set(options, args): """rename cpuset as specified in options and args lists""" log.debug('entering rename_set, options=%s args=%s', options, args) # figure out target cpuset name, if --set not used, use first arg name = options.newname if options.set: tset = cset.unique_set(options.set) elif len(args) > 0: tset = cset.unique_set(args[0]) else: raise CpusetException('desired cpuset not specified') path = tset.path[0:tset.path.rfind('/')+1] log.debug('target set="%s", path="%s", name="%s"', tset.path, path, name) try: if name.find('/') == -1: chk = cset.unique_set(path+name) else: if name[0:name.rfind('/')+1] != path: raise CpusetException('desired name cannot have different path') chk = cset.unique_set(name) raise CpusetException('cpuset "'+chk.path+'" already exists') except CpusetNotFound: pass except: raise if name.rfind('/') != -1: name = name[name.rfind('/')+1:] log.info('--> renaming "%s" to "%s"', cset.CpuSet.basepath+tset.path, name) os.rename(cset.CpuSet.basepath+tset.path, cset.CpuSet.basepath+path+name) cset.rescan()
def rename_set(options, args): """rename cpuset as specified in options and args lists""" log.debug('entering rename_set, options=%s args=%s', options, args) # figure out target cpuset name, if --set not used, use first arg name = options.newname if options.set: tset = cset.unique_set(options.set) elif len(args) > 0: tset = cset.unique_set(args[0]) else: raise CpusetException('desired cpuset not specified') path = tset.path[0:tset.path.rfind('/') + 1] log.debug('target set="%s", path="%s", name="%s"', tset.path, path, name) try: if name.find('/') == -1: chk = cset.unique_set(path + name) else: if name[0:name.rfind('/') + 1] != path: raise CpusetException( 'desired name cannot have different path') chk = cset.unique_set(name) raise CpusetException('cpuset "' + chk.path + '" already exists') except CpusetNotFound: pass except: raise if name.rfind('/') != -1: name = name[name.rfind('/') + 1:] log.info('--> renaming "%s" to "%s"', cset.CpuSet.basepath + tset.path, name) os.rename(cset.CpuSet.basepath + tset.path, cset.CpuSet.basepath + path + name) cset.rescan()
def destroy(name): """destroy one cpuset by name as cset or string""" log.debug('entering destroy, name=%s', name) if isinstance(name, str): set = cset.unique_set(name) elif not isinstance(name, cset.CpuSet): raise CpusetException( "passed name=%s, which is not a string or CpuSet" % name) else: set = name tsks = set.tasks if len(tsks) > 0: # wait for tasks, sometimes it takes a little while to # have them leave the set ii = 0 while len(tsks)>0: log.debug('%i tasks still running in set %s, waiting interval %s...', len(tsks), set.name, ii+1) time.sleep(0.5) tsks = set.tasks ii += 1 if (ii) > 6: # try it for 3 seconds, bail if tasks still there raise CpusetException( "trying to destroy cpuset %s with tasks running: %s" % (set.path, set.tasks)) log.debug("tasks expired, deleting set %s" % set.path) os.rmdir(cset.CpuSet.basepath+set.path) # fixme: perhaps reparsing the all the sets is not so efficient... cset.rescan()
def destroy(name): """destroy one cpuset by name as cset or string""" log.debug('entering destroy, name=%s', name) if isinstance(name, str): set = cset.unique_set(name) elif not isinstance(name, cset.CpuSet): raise CpusetException( "passed name=%s, which is not a string or CpuSet" % name) else: set = name tsks = set.tasks if len(tsks) > 0: # wait for tasks, sometimes it takes a little while to # have them leave the set ii = 0 while len(tsks) > 0: log.debug( '%i tasks still running in set %s, waiting interval %s...', len(tsks), set.name, ii + 1) time.sleep(0.5) tsks = set.tasks ii += 1 if (ii) > 6: # try it for 3 seconds, bail if tasks still there raise CpusetException( "trying to destroy cpuset %s with tasks running: %s" % (set.path, set.tasks)) log.debug("tasks expired, deleting set %s" % set.path) os.rmdir(cset.CpuSet.basepath + set.path) # fixme: perhaps reparsing the all the sets is not so efficient... cset.rescan()
def create(name, cpuspec, memspec, cx, mx): """create one cpuset by name, cpuspec, memspec, cpu and mem exclusive flags""" log.debug('entering create, name=%s cpuspec=%s memspec=%s cx=%s mx=%s', name, cpuspec, memspec, cx, mx) try: cset.unique_set(name) except CpusetNotFound: pass except: raise CpusetException('cpuset "%s" not unique, please specify by path' % name) else: raise CpusetExists('attempt to create already existing set: "%s"' % name) # FIXME: check if name is a path here os.mkdir(cset.CpuSet.basepath+'/'+name) # fixme: perhaps reparsing the all the sets is not so efficient... cset.rescan() log.debug('created new cpuset "%s"', name) modify(name, cpuspec, memspec, cx, mx)
def func(parser, options, args): log.debug("entering func, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() if options.list: if options.set: list_sets(options.set, options.recurse, options.usehex) return if len(args): list_sets(args, options.recurse, options.usehex) else: list_sets('root', options.recurse, options.usehex) return if options.cpu or options.mem: # create or modify cpuset create_from_options(options, args) return if options.newname: rename_set(options, args) return if options.destroy: if options.set: destroy_sets(options.set, options.recurse, options.force) else: destroy_sets(args, options.recurse, options.force) return if options.cpu_exclusive or options.mem_exclusive: # FIXME: modification of existing cpusets for exclusivity log.info( "Modification of cpu_exclusive and mem_exclusive not implemented.") return # default behavior if no options specified is list log.debug('no options set, default is listing cpusets') if options.set: list_sets(options.set, options.recurse, options.usehex) elif len(args): list_sets(args, options.recurse, options.usehex) else: list_sets('root', options.recurse, options.usehex)
def func(parser, options, args): log.debug("entering func, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() if options.list: if options.set: list_sets(options.set, options.recurse, options.usehex) return if len(args): list_sets(args, options.recurse, options.usehex) else: list_sets('root', options.recurse, options.usehex) return if options.cpu or options.mem: # create or modify cpuset create_from_options(options, args) return if options.newname: rename_set(options, args) return if options.destroy: if options.set: destroy_sets(options.set, options.recurse, options.force) else: destroy_sets(args, options.recurse, options.force) return if options.cpu_exclusive or options.mem_exclusive: # FIXME: modification of existing cpusets for exclusivity log.info("Modification of cpu_exclusive and mem_exclusive not implemented.") return # default behavior if no options specified is list log.debug('no options set, default is listing cpusets') if options.set: list_sets(options.set, options.recurse, options.usehex) elif len(args): list_sets(args, options.recurse, options.usehex) else: list_sets('root', options.recurse, options.usehex)
def func(parser, options, args): log.debug("entering func, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() tset = None if options.list or options.exc: if options.set: tset = cset.unique_set(options.set) elif options.toset: tset = cset.unique_set(options.toset) elif len(args) > 0: if options.exc: tset = cset.unique_set(args[0]) del args[0] else: tset = args else: raise CpusetException("cpuset not specified") try: log.debug("operating on set %s", tset.path) except: log.debug("operating on sets %s", tset) if options.exc: run(tset, args, options.user, options.group) if options.list: list_sets(tset) return if options.move or options.kthread: fset = None tset = None # first, we need to know the destination if options.toset: tset = cset.unique_set(options.toset) elif options.set and options.pid: tset = cset.unique_set(options.set) elif options.set and options.fromset: tset = cset.unique_set(options.set) elif len(args) > 0: if len(args) > 1 and options.pid == None: options.pid = args[0] if len(args) < 3: tset = cset.unique_set(args[1]) else: # "-m 123 set1 set2" shortcut fset = cset.unique_set(args[1]) tset = cset.unique_set(args[2]) # take care of set1->set2 shortcut pids = pidspec_to_list(options.pid, threads=options.threads) if len(pids) == 1: try: fset = cset.unique_set(pids[0]) options.pid = None except: pass # must be real pidspec else: if len(args) < 2: tset = cset.unique_set(args[0]) else: fset = cset.unique_set(args[0]) tset = cset.unique_set(args[1]) else: raise CpusetException("destination cpuset not specified") set.active(tset) # next, if there is a pidspec, move just that if options.pid: if options.fromset and not options.force: fset = cset.unique_set(options.fromset) elif options.toset and options.set: fset = cset.unique_set(options.set) pids = pidspec_to_list(options.pid, fset, options.threads) if len(pids): log.info('moving following pidspec: %s' % ','.join(pids)) selective_move(None, tset, pids, options.kthread, options.force) else: log.info('**> no tasks moved') log.info('done') else: fset = None # here we assume move everything from fromset to toset if options.fromset: fset = cset.unique_set(options.fromset) elif options.set: fset = cset.unique_set(options.set) elif len(args) > 0: # this must be the fromset, then... fset = cset.unique_set(args[0]) if fset == None: raise CpusetException("origination cpuset not specified") nt = len(fset.tasks) if nt == 0: raise CpusetException('no tasks to move from cpuset "%s"' % fset.path) if options.move: log.info('moving all tasks from %s to %s', fset.name, tset.path) selective_move(fset, tset, None, options.kthread, options.force, options.threads) else: log.info('moving all kernel threads from %s to %s', fset.path, tset.path) # this is a -k "move", so only move kernel threads pids = [] for task in fset.tasks: try: os.readlink('/proc/' + task + '/exe') except: pids.append(task) selective_move(fset, tset, pids, options.kthread, options.force) log.info('done') return # default no options is list list_sets(args)
def func(parser, options, args): log.debug("entering func, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() tset = None if options.list or options.exc: if options.set: tset = cset.unique_set(options.set) elif options.toset: tset = cset.unique_set(options.toset) elif len(args) > 0: if options.exc: tset = cset.unique_set(args[0]) del args[0] else: tset = args else: raise CpusetException("cpuset not specified") try: log.debug("operating on set %s", tset.path) except: log.debug("operating on sets %s", tset) if options.exc: run(tset, args, options.user, options.group) if options.list: list_sets(tset) return if options.move or options.kthread: fset = None tset = None # first, we need to know the destination if options.toset: tset = cset.unique_set(options.toset) elif options.set and options.pid: tset = cset.unique_set(options.set) elif options.set and options.fromset: tset = cset.unique_set(options.set) elif len(args) > 0: if len(args) > 1 and options.pid == None: options.pid = args[0] if len(args) < 3: tset = cset.unique_set(args[1]) else: # "-m 123 set1 set2" shortcut fset = cset.unique_set(args[1]) tset = cset.unique_set(args[2]) # take care of set1->set2 shortcut pids = pidspec_to_list(options.pid, threads=options.threads) if len(pids) == 1: try: fset = cset.unique_set(pids[0]) options.pid = None except: pass # must be real pidspec else: if len(args) < 2: tset = cset.unique_set(args[0]) else: fset = cset.unique_set(args[0]) tset = cset.unique_set(args[1]) else: raise CpusetException("destination cpuset not specified") set.active(tset) # next, if there is a pidspec, move just that if options.pid: if options.fromset and not options.force: fset = cset.unique_set(options.fromset) elif options.toset and options.set: fset = cset.unique_set(options.set) pids = pidspec_to_list(options.pid, fset, options.threads) if len(pids): log.info('moving following pidspec: %s' % ','.join(pids)) selective_move(None, tset, pids, options.kthread, options.force) else: log.info('**> no tasks moved') log.info('done') else: fset = None # here we assume move everything from fromset to toset if options.fromset: fset = cset.unique_set(options.fromset) elif options.set: fset = cset.unique_set(options.set) elif len(args) > 0: # this must be the fromset, then... fset = cset.unique_set(args[0]) if fset == None: raise CpusetException("origination cpuset not specified") nt = len(fset.tasks) if nt == 0: raise CpusetException('no tasks to move from cpuset "%s"' % fset.path) if options.move: log.info('moving all tasks from %s to %s', fset.name, tset.path) selective_move(fset, tset, None, options.kthread, options.force, options.threads) else: log.info('moving all kernel threads from %s to %s', fset.path, tset.path) # this is a -k "move", so only move kernel threads pids = [] for task in fset.tasks: try: os.readlink('/proc/'+task+'/exe') except: pids.append(task) selective_move(fset, tset, pids, options.kthread, options.force) log.info('done') return # default no options is list list_sets(args)
def func(parser, options, args): log.debug("entering shield, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() if options.sysset: global SYS_SET SYS_SET = options.sysset if options.userset: global USR_SET USR_SET = options.userset if (not options.cpu and not options.reset and not options.exc and not options.shield and not options.unshield and not options.kthread): shield_exists() doshield = False if len(args) == 0: log.info("--> shielding system active with") print_all_stats() else: # shortcut: first assume that arg is a pidspec, if not, then exec it try: plist = proc.pidspec_to_list(args[0]) for pid in plist: int(pid) doshield = True # ok, if we're here, then it's probably a pidspec, shield it except: exec_args(args, options.user, options.group) if doshield: # drop through to shield section below options.pid = args[0] options.shield = True else: return if options.reset: reset_shield() return if options.cpu: make_shield(options.cpu, options.kthread) return if options.kthread: make_kthread(options.kthread) return if options.exc: exec_args(args, options.user, options.group) # exec_args does not return... if options.shield or options.unshield: shield_exists() if options.shield: smsg = 'shielding' to_set = USR_SET from_set = SYS_SET print_stats = print_usr_stats else: smsg = 'unshielding' to_set = SYS_SET from_set = USR_SET print_stats = print_sys_stats if options.pid == None: if len(args) > 0: # shortcut, assumes arg[0] is a pidspec options.pid = args[0] else: # no pidspec so output shield state print_stats() if options.pid: if options.threads: tmsg = '(with threads)' else: tmsg = '' log.info('--> %s following pidspec: %s %s', smsg, options.pid, tmsg) if options.force: proc.move_pidspec(options.pid, to_set, None, options.threads) else: try: proc.move_pidspec(options.pid, to_set, from_set, options.threads) except CpusetException as err: if str(err).find('do not match all criteria') != -1: log.info("--> hint: perhaps use --force if sure of command") raise log.info('done') return
def setUp(self): cset.rescan() self.test_set = cset.unique_set("user")
def func(parser, options, args): log.debug("entering shield, options=%s, args=%s", options, args) global verbose if options.verbose: verbose = options.verbose cset.rescan() if options.sysset: global SYS_SET SYS_SET = options.sysset if options.userset: global USR_SET USR_SET = options.userset if (not options.cpu and not options.reset and not options.exc and not options.shield and not options.unshield and not options.kthread): shield_exists() doshield = False if len(args) == 0: log.info("--> shielding system active with") print_all_stats() else: # shortcut: first assume that arg is a pidspec, if not, then exec it try: plist = proc.pidspec_to_list(args[0]) for pid in plist: int(pid) doshield = True # ok, if we're here, then it's probably a pidspec, shield it except: exec_args(args, options.user, options.group) if doshield: # drop through to shield section below options.pid = args[0] options.shield = True else: return if options.reset: reset_shield() return if options.cpu: make_shield(options.cpu, options.kthread) return if options.kthread: make_kthread(options.kthread) return if options.exc: exec_args(args, options.user, options.group) # exec_args does not return... if options.shield or options.unshield: shield_exists() if options.shield: smsg = 'shielding' to_set = USR_SET from_set = SYS_SET print_stats = print_usr_stats else: smsg = 'unshielding' to_set = SYS_SET from_set = USR_SET print_stats = print_sys_stats if options.pid == None: if len(args) > 0: # shortcut, assumes arg[0] is a pidspec options.pid = args[0] else: # no pidspec so output shield state print_stats() if options.pid: if options.threads: tmsg = '(with threads)' else: tmsg = '' log.info('--> %s following pidspec: %s %s', smsg, options.pid, tmsg) if options.force: proc.move_pidspec(options.pid, to_set, None, options.threads) else: try: proc.move_pidspec(options.pid, to_set, from_set, options.threads) except CpusetException, err: if str(err).find('do not match all criteria') != -1: log.info("--> hint: perhaps use --force if sure of command") raise log.info('done') return