def main(): parser = ArgumentParser(prog='presentation') parser.add_argument('task', nargs='?', help='one of [{}]'.format(', '.join(TASKS))) parser.add_argument('configuration', nargs='?', default=None, help='empty or one of [{}]'.format( ', '.join(CONFIGURATIONS))) args = parser.parse_args() print(args) with DEFAULTS_JSON.open() as f: PARAMETERS = load(f) task_dir = TASKS_DIR / args.task parameter_json = task_dir / 'parameters.json' with parameter_json.open() as f: CHANGES = load(f) PARAMETERS = update(PARAMETERS, CHANGES) if args.configuration is not None: parameter_json = CONFIG_DIR / f'{args.configuration}.json' with parameter_json.open() as f: CHANGES = load(f) PARAMETERS = update(PARAMETERS, CHANGES) PARAMETERS['task'] = args.task PARAMETERS['configuration'] = args.configuration now = datetime.now() logname = LOG_DIR / f'log_{now:%Y%m%d_%H%M%S}.txt' logging.basicConfig( filename=logname, filemode='w', format='%(asctime)s.%(msecs)03d\t%(name)s\t%(levelname)s\t%(message)s', datefmt='%H:%M:%S', level=logging.DEBUG) logging.info(str(now)) PARAMETERS['logname'] = logname if Process is not None: ps = Process() ps.nice(HIGH_PRIORITY_CLASS) task_tsv = (task_dir / PARAMETERS['TASK_TSV']).resolve() if task_tsv.exists(): PARAMETERS['TASK_TSV'] = task_tsv else: lg.debug(f'Cannot find {task_tsv}, using default "timing.tsv"') PARAMETERS['TASK_TSV'] = (task_dir / 'timing.tsv').resolve() lg.debug(pformat(PARAMETERS)) w = PrettyWidget(PARAMETERS) app.exec()
def lower_child_priority(): try: from psutil import Process, BELOW_NORMAL_PRIORITY_CLASS parent = Process() parent.nice(BELOW_NORMAL_PRIORITY_CLASS) for child in parent.children(): child.nice(BELOW_NORMAL_PRIORITY_CLASS) except: pass
def apply(self, process: psutil.Process) -> None: try: if self.nice is not None and process.nice() != self.nice: process.nice(self.nice) except psutil.AccessDenied: pass try: if (self.ioclass is not None and process.ionice().ioclass != self.ioclass or self.ionice is not None and process.ionice().value != self.ionice): process.ionice(self.ioclass, self.ionice) except psutil.AccessDenied: pass if self.sched is not None: try: if os.sched_getscheduler(process.pid) != self.sched: sched_prio: int = 0 if self.sched in (Scheduler.FIFO, Scheduler.RR): assert self.sched_prio is not None sched_prio = self.sched_prio os.sched_setscheduler( process.pid, self.sched, os.sched_param(sched_prio) # type: ignore ) except PermissionError: pass elif self.sched_prio is not None: try: if (os.sched_getscheduler(process.pid) in (Scheduler.FIFO, Scheduler.RR)): os.sched_setparam( process.pid, os.sched_param(sched_prio) # type: ignore ) except PermissionError: pass if self.oom_score_adj is not None: try: with open('/proc/%(pid)s/oom_score_adj' % {'pid': process.pid}, 'r+') as file: prev_oom_score_adj: int = int(file.read()) if prev_oom_score_adj != self.oom_score_adj: file.write(str(self.oom_score_adj)) except PermissionError: pass if self.cgroup is not None: # TODO: implement pass
def _get_process_priority(process: psutil.Process): # nice = int(process.nice()) try: nice = int(process.nice()) except psutil.AccessDenied: nice = 0 return nice
def configure_process(proc: psutil.Process, cores: List[int], infer_multi_team=False): try: if infer_multi_team: existing_affinity = proc.cpu_affinity() if len(existing_affinity) < psutil.cpu_count(): # The process in question has already been pinned to a subset of the CPUs. # This might be a process spanning multiple teams, so we will set affinity to # the combination of existing and newly requested cores. cores = sorted(set(existing_affinity + cores)) proc.cpu_affinity(cores) if platform.system() == 'Windows': proc.nice(psutil.HIGH_PRIORITY_CLASS ) # Allow the process to run at high priority except Exception as e: get_logger(DEFAULT_LOGGER).info(e)