Example #1
0
    def __init__(self, run_driver_name: str = None):
        """
        Create an instance.

        :param run_driver_name: name of the used run driver, if None the one configured in the settings is used
        """
        self.submit_queue = Queue()  # type: Queue
        """ Queue for submitted but not benchmarked run program blocks """
        self.result_queue = Queue()  # type: Queue
        """
        Queue of benchmarking results.
        The queue items are tuples consisting of
        the benchmarked block, the benchmarking result and the
        blocks id.
        """
        self.parallel_number = 1  # type: int
        """ Number of instances in which the benchmarks takes place in parallel """
        run_driver_name = run_driver_name or RunDriverRegistry.get_used()
        self.run_driver = RunDriverRegistry().get_for_name(run_driver_name)  # type: AbstractRunDriver
        """ Used run driver instance """
        self.cpuset = None  # type: CPUSet
        """ Used cpu set instance """
        if Settings()["run/disable_hyper_threading"]:
            if not has_root_privileges():
                logging.warning("Can't disable hyper threading as root privileges are missing")
                return
            #if Settings()["run/cpuset/active"]:
            #    logging.warning("Currently disabling hyper threading doesn't work well in combination with cpusets")
            #    return
            self._disable_hyper_threading()
Example #2
0
    def __init__(self, misc_settings: dict = None):
        """
        Creates an instance.
        Also calls the setup methods on all registered plugins.
        It calls the setup() method.

        :param misc_settings: further settings
        """
        self.misc_settings = misc_settings
        """ Further settings """
        self.used_plugins = []  # type: t.List[RunDriverPlugin]
        """ Used and active plugins """
        miss_root_plugins = []
        is_root = has_root_privileges()
        for used in self.get_used():
            klass = self.get_class(used)
            if klass.needs_root_privileges and not is_root:
                miss_root_plugins.append(used)
            else:
                self.used_plugins.append(self.get_for_name(used))
        if miss_root_plugins:
            logging.warning(
                "The following plugins are disabled because they need root privileges: "
                + join_strs(miss_root_plugins))
        self.setup()
Example #3
0
    def __init__(self, run_driver_name: str = None, end_time: float = -1):
        """
        Create an instance.

        :param run_driver_name: name of the used run driver, if None the one configured in the settings is used
        """
        self.submit_queue = Queue()  # type: Queue
        """ Queue for submitted but not benchmarked run program blocks """
        self.result_queue = Queue()  # type: Queue
        """
        Queue of benchmarking results.
        The queue items are tuples consisting of
        the benchmarked block, the benchmarking result and the
        blocks id.
        """
        self.parallel_number = 1  # type: int
        """ Number of instances in which the benchmarks takes place in parallel """
        run_driver_name = run_driver_name or RunDriverRegistry.get_used()
        self.run_driver = RunDriverRegistry().get_for_name(
            run_driver_name)  # type: AbstractRunDriver
        """ Used run driver instance """
        self.cpuset = None  # type: CPUSet
        """ Used cpu set instance """
        self.end_time = end_time
        self._disabled_ht_cores = []  # type: t.List[int]
        if Settings()["run/disable_hyper_threading"]:
            if not has_root_privileges():
                logging.warning(
                    "Can't disable hyper threading as root privileges are missing"
                )
                return
            #if Settings()["run/cpuset/active"]:
            #    logging.warning("Currently disabling hyper threading doesn't work well in combination with cpusets")
            #    return
            self._disabled_ht_cores = self.disable_hyper_threading()
Example #4
0
    def _exec_command(self,
                      cmds: list,
                      block: RunProgramBlock,
                      cpuset: CPUSet = None,
                      set_id: int = 0) -> ExecResult:
        """
        Executes one randomly chosen command of the passed ones.
        And takes additional settings in the passed run program block into account.

        :param cmds: list of commands
        :param block: passed run program block
        :return: time in seconds the execution needed to finish
        """
        typecheck(cmds, List(Str()))
        rand_index = random.randrange(
            0, len(cmds)) if self.misc_settings["random_cmd"] else 0
        cmd = cmds[rand_index]
        cwd = block["cwds"][rand_index]
        executed_cmd = block["cmd_prefix"] + [cmd]
        if cpuset is not None and has_root_privileges():
            executed_cmd.insert(0, "cset proc --move --force --pid $$ {} > /dev/null" \
                                .format(cpuset.get_sub_set(set_id)))
        env = os.environ.copy()
        env.update(block["env"])
        env.update({'LC_NUMERIC': 'en_US.UTF-8'})
        # print(env["PATH"])
        t = time.time()
        executed_cmd = "; ".join(executed_cmd)
        proc = None
        try:
            proc = subprocess.Popen(
                ["/bin/sh", "-c", executed_cmd],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                universal_newlines=True,
                cwd=cwd,
                env=env,
            )
            # preexec_fn=os.setsid)
            out, err = proc.communicate()
            t = time.time() - t
            ExecValidator(block["validator"]).validate(cmd, out, err,
                                                       proc.poll())
            # if proc.poll() > 0:
            #    msg = "Error executing " + cmd + ": "+ str(err) + " " + str(out)
            # logging.error(msg)
            #    raise BenchmarkingError(msg)
            return self.ExecResult(time=t, stderr=str(err), stdout=str(out))
        except:
            if proc is not None:
                try:
                    proc.terminate()
                    # os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
                except BaseException as err:
                    pass
            raise
Example #5
0
    def _exec_command(self, cmds: list, block: RunProgramBlock,
                      cpuset: CPUSet = None, set_id: int = 0) -> ExecResult:
        """
        Executes one randomly chosen command of the passed ones.
        And takes additional settings in the passed run program block into account.

        :param cmds: list of commands
        :param block: passed run program block
        :return: time in seconds the execution needed to finish
        """
        typecheck(cmds, List(Str()))
        rand_index = random.randrange(0, len(cmds)) if self.misc_settings["random_cmd"] else 0
        cmd = cmds[rand_index]
        cwd = block["cwds"][rand_index]
        executed_cmd = block["cmd_prefix"] + [cmd]
        if cpuset is not None and has_root_privileges():
            executed_cmd.insert(0, "cset proc --move --force --pid $$ {} > /dev/null" \
                                .format(cpuset.get_sub_set(set_id)))
        env = os.environ.copy()
        env.update(block["env"])
        env.update({'LC_NUMERIC': 'en_US.UTF-8'})
        # print(env["PATH"])
        t = time.time()
        executed_cmd = "; ".join(executed_cmd)
        proc = None
        try:
            proc = subprocess.Popen(["/bin/sh", "-c", executed_cmd], stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE,
                                    universal_newlines=True,
                                    cwd=cwd,
                                    env=env, )
            # preexec_fn=os.setsid)
            out, err = proc.communicate()
            t = time.time() - t
            ExecValidator(block["validator"]).validate(cmd, out, err, proc.poll())
            # if proc.poll() > 0:
            #    msg = "Error executing " + cmd + ": "+ str(err) + " " + str(out)
            # logging.error(msg)
            #    raise BenchmarkingError(msg)
            return self.ExecResult(time=t, stderr=str(err), stdout=str(out))
        except:
            if proc is not None:
                try:
                    proc.terminate()
                    # os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
                except BaseException as err:
                    pass
            raise
Example #6
0
def run():
    sudo_opt_index = sys.argv.index(
        "--sudo") if "--sudo" in sys.argv else sys.maxsize
    raw_opt_index = sys.argv.index("--") if "--" in sys.argv else sys.maxsize
    has_sudo_opt = sudo_opt_index != sys.maxsize and sudo_opt_index < raw_opt_index

    if not has_sudo_opt or has_root_privileges():
        from temci.scripts.cli import cli_with_error_catching
        cli_with_error_catching()
    else:
        if has_sudo_opt:
            sys.argv.remove("--sudo")
        import json, shlex
        cmd = "sudo TEMCI_ENV={} {}".format(
            shlex.quote(json.dumps({k: os.environ[k]
                                    for k in os.environ})),
            " ".join(shlex.quote(arg) for arg in sys.argv))
        os.system(cmd)
Example #7
0
    def __init__(self, misc_settings: dict = None):
        """
        Creates an instance.
        Also calls the setup methods on all registered plugins.
        It calls the setup() method.

        :param misc_settings: further settings
        """
        self.misc_settings = misc_settings
        """ Further settings """
        self.used_plugins = []  # type: t.List[RunDriverPlugin]
        """ Used and active plugins """
        miss_root_plugins = []
        is_root = has_root_privileges()
        for used in self.get_used():
            klass = self.get_class(used)
            if klass.needs_root_privileges and not is_root:
                miss_root_plugins.append(used)
            else:
                self.used_plugins.append(self.get_for_name(used))
        if miss_root_plugins:
            logging.warning("The following plugins are disabled because they need root privileges: " +
                            join_strs(miss_root_plugins))
        self.setup()
Example #8
0
 def _execute(self, db: Database):
     if not has_root_privileges():
         self._fail("Root privileges are missing")
Example #9
0
 def _dry_run_message(self) -> str:
     return "Would check for root privileges (and {})".format("succeed" if has_root_privileges() else "fail")
Example #10
0
 def enable_hyper_threading(cls, disabled_cores: t.List[int]):
     if has_root_privileges():
         cls._set_status_of_ht_cores(disabled_cores, 1)
Example #11
0
 def disable_hyper_threading(cls) -> t.List[int]:
     if has_root_privileges():
         cores = cls.get_hyper_threading_cores()
         cls._set_status_of_ht_cores(cores, 0)
         return cores
     return []
Example #12
0
 def _execute(self, db: Database):
     if not has_root_privileges():
         self._fail("Root privileges are missing")
Example #13
0
 def _dry_run_message(self) -> str:
     return "Would check for root privileges (and {})".format(
         "succeed" if has_root_privileges() else "fail")
Example #14
0
    def __init__(self,
                 active: bool = True,
                 base_core_number: int = None,
                 parallel: int = None,
                 sub_core_number: int = None,
                 temci_in_base_set: bool = True):
        """
        Initializes the cpu sets an determines the number of parallel programs (parallel_number variable).

        :param active: are cpu sets actually used?
        :param base_core_number: number of cpu cores for the base (remaining part of the) system
        :param parallel: 0: benchmark sequential, > 0: benchmark parallel with n instances, -1: determine n automatically
        :param sub_core_number: number of cpu cores per parallel running program
        :param temci_in_base_set: place temci in the same cpu set as the rest of the system?
        :raises ValueError: if the passed parameters don't work together on the current platform
        :raises EnvironmentError: if the environment can't be setup properly (e.g. no root privileges)
        """
        # self.bench_set = "bench.set"
        self.active = active and has_root_privileges()  # type: bool
        """ Are cpu sets actually used? """
        self.base_core_number = Settings().default(
            base_core_number, "run/cpuset/base_core_number")  # type: int
        """ Number of cpu cores for the base (remaining part of the) system """
        self.parallel = Settings().default(parallel,
                                           "run/cpuset/parallel")  # type: int
        """ 0: benchmark sequential, > 0: benchmark parallel with n instances, -1: determine n automatically """
        self.sub_core_number = Settings().default(
            sub_core_number, "run/cpuset/sub_core_number")  # type: int
        """ Number of cpu cores per parallel running program """
        self.av_cores = len(self._cpus_of_set(
            "")) if active else multiprocessing.cpu_count()  # type: int
        """ Number of available cpu cores """
        self.parallel_number = 0  # type: int
        """ Number of used parallel instances, zero if the benchmarking is done sequentially """
        self.temci_in_base_set = Settings().default(
            temci_in_base_set, "run/cpuset/temci_in_base_set")
        """ Place temci in the same cpu set as the rest of the system? """
        if self.parallel != 0:
            if self.parallel == -1:
                self.parallel_number = self._number_of_parallel_sets(
                    self.base_core_number, True, self.sub_core_number)
            else:
                self.parallel_number = self.parallel
                if self.parallel > self._number_of_parallel_sets(self.base_core_number, True, self.sub_core_number) \
                        and self.active:
                    raise ValueError(
                        "Invalid values for base_core_number and sub_core_number "
                        "on system with just {} cores. Note: The benchmark controller "
                        "needs a cpuset too.".format(self.av_cores))
            self.base_core_number = self.av_cores - self.sub_core_number * self.parallel_number - 1
        if not self.active:
            if active and not has_root_privileges():
                logging.warning(
                    "CPUSet functionality is disabled because root privileges are missing."
                )
            return
        logging.info("Initialize CPUSet")
        typecheck(self.base_core_number, PositiveInt())
        typecheck(self.parallel_number, NaturalNumber())
        self.own_sets = [SUB_BENCH_SET.format(i) for i in range(0, self.parallel_number)] \
                        + [CONTROLLER_SUB_BENCH_SET, NEW_ROOT_SET, BENCH_SET]
        try:
            self._init_cpuset()
        except BaseException:
            logging.error("Forced teardown of CPUSet")
            self.teardown()
            raise
        logging.info("Finished initializing CPUSet")
Example #15
0
    def __init__(
        self,
        active: bool = has_root_privileges(),
        base_core_number: int = None,
        parallel: int = None,
        sub_core_number: int = None,
    ):
        """
        Initializes the cpu sets an determines the number of parallel programs (parallel_number variable).

        :param active: are cpu sets actually used?
        :param base_core_number: number of cpu cores for the base (remaining part of the) system
        :param parallel: 0: benchmark sequential, > 0: benchmark parallel with n instances, -1: determine n automatically
        :param sub_core_number: number of cpu cores per parallel running program
        :raises ValueError: if the passed parameters don't work together on the current platform
        :raises EnvironmentError: if the environment can't be setup properly (e.g. no root privileges)
        """
        # self.bench_set = "bench.set"
        self.active = active and has_root_privileges()  # type: bool
        """ Are cpu sets actually used? """
        self.base_core_number = Settings().default(base_core_number, "run/cpuset/base_core_number")  # type: int
        """ Number of cpu cores for the base (remaining part of the) system """
        self.parallel = Settings().default(parallel, "run/cpuset/parallel")  # type: int
        """ 0: benchmark sequential, > 0: benchmark parallel with n instances, -1: determine n automatically """
        self.sub_core_number = Settings().default(sub_core_number, "run/cpuset/sub_core_number")  # type: int
        """ Number of cpu cores per parallel running program """
        self.av_cores = len(self._cpus_of_set("")) if active else multiprocessing.cpu_count()  # zype: int
        """ Number of available cpu cores """
        self.parallel_number = 0  # type: int
        """ Number of used parallel instances, zero if the benchmarking is done sequentially """
        if self.parallel != 0:
            if self.parallel == -1:
                self.parallel_number = self._number_of_parallel_sets(self.base_core_number, True, self.sub_core_number)
            else:
                self.parallel_number = self.parallel
                if (
                    self.parallel > self._number_of_parallel_sets(self.base_core_number, True, self.sub_core_number)
                    and self.active
                ):
                    raise ValueError(
                        "Invalid values for base_core_number and sub_core_number "
                        "on system with just {} cores. Note: The benchmark controller "
                        "needs a cpuset too.".format(self.av_cores)
                    )
            self.base_core_number = self.av_cores - self.sub_core_number * self.parallel_number - 1
        if not active:
            if not has_root_privileges():
                logging.warning("CPUSet functionality is disabled because root privileges are missing.")
            return
        logging.info("Initialize CPUSet")
        typecheck(self.base_core_number, PositiveInt())
        typecheck(self.parallel_number, NaturalNumber())
        self.own_sets = [SUB_BENCH_SET.format(i) for i in range(0, self.parallel_number)] + [
            CONTROLLER_SUB_BENCH_SET,
            NEW_ROOT_SET,
            BENCH_SET,
        ]
        try:
            self._init_cpuset()
        except BaseException:
            logging.error("Forced teardown of CPUSet")
            self.teardown()
            raise
        logging.info("Finished initializing CPUSet")
Example #16
0
 def enable_hyper_threading(cls):
     if has_root_privileges():
         cls._set_status_of_ht_cores(cls.get_hyper_threading_cores(), 1)