<commit_before>import logging
import time
from collections import defaultdict, OrderedDict
from copy import copy

from wa.framework.exception import ConfigError
from wa.framework.plugin import Plugin, Parameter
from wa.utils.misc import resolve_cpus, resolve_unique_domain_cpus
from wa.utils.types import caseless_string, enum

from devlib.exception import TargetError
from devlib.utils.misc import unique
from devlib.utils.types import integer

logger = logging.getLogger('RuntimeConfig')


class RuntimeParameter(Parameter):
    def __init__(self, name, setter, setter_params=None, **kwargs):
        super(RuntimeParameter, self).__init__(name, **kwargs)
        self.setter = setter
        self.setter_params = setter_params or {}

    def set(self, obj, value):
        self.validate_value(self.name, value)
        self.setter(obj, value, **self.setter_params)


class RuntimeConfig(Plugin):

    name = None
    kind = 'runtime-config'

    @property
    def supported_parameters(self):
        return self._runtime_params.values()

    @property
    def core_names(self):
        return unique(self.target.core_names)

    def __init__(self, target, **kwargs):
        super(RuntimeConfig, self).__init__(**kwargs)
        self.target = target
        self._target_checked = False
        self._runtime_params = {}
        try:
            self.initialize()
        except TargetError:
            msg = 'Failed to initialize: "{}"'
            self.logger.debug(msg.format(self.name))
            self._runtime_params = {}

    def initialize(self):
        raise NotImplementedError()

    def commit(self):
        raise NotImplementedError()

    def set_runtime_parameter(self, name, value):
        if not self._target_checked:
            self.check_target()
            self._target_checked = True
        self._runtime_params[name].set(self, value)

    def set_defaults(self):
        for p in self.supported_parameters:
            if p.default:
                self.set_runtime_parameter(p.name, p.default)

    def validate_parameters(self):
        raise NotImplementedError()

    def check_target(self):
        raise NotImplementedError()

    def clear(self):
        raise NotImplementedError()


class HotplugRuntimeConfig(RuntimeConfig):
    '''
    NOTE: Currently will fail if trying to hotplug back a core that
    was hotplugged out when the devlib target was created.
    '''

    name = 'rt-hotplug'

    @staticmethod
    def set_num_cores(obj, value, core):
        cpus = resolve_cpus(core, obj.target)
        max_cores = len(cpus)
        value = integer(value)
        if value > max_cores:
            msg = 'Cannot set number of {}\'s to {}; max is {}'
            raise ValueError(msg.format(core, value, max_cores))

        msg = 'CPU{} Hotplugging already configured'
        # Set cpus to be enabled
        for cpu in cpus[:value]:
            if cpu in obj.num_cores:
                raise ConfigError(msg.format(cpu))
            obj.num_cores[cpu] = True
        # Set the remaining cpus to be disabled.
        for cpu in cpus[value:]:
            if cpu in obj.num_cores:
                raise ConfigError(msg.format(cpu))
            obj.num_cores[cpu] = False

    def __init__(self, target):
        self.num_cores = defaultdict(dict)
        super(HotplugRuntimeConfig, self).__init__(target)

    def initialize(self):
        if not self.target.has('hotplug'):
            return
        param_name = 'num_cores'
        self._runtime_params[param_name] = \
                RuntimeParameter(param_name, kind=int,
                                 constraint=lambda x:
                                         0 < x <= self.target.number_of_cpus,
                                 description="""
                                 The number of cpu cores to be online
                                 """,
                                 setter=self.set_num_cores,
                                 setter_params={'core': None})

        for name in unique(self.target.platform.core_names):
            param_name = 'num_{}_cores'.format(name)
            self._runtime_params[param_name] = \
                    RuntimeParameter(param_name, kind=int,
                                     constraint=lambda x, name=name:
                                             0 < x <= len(self.target.core_cpus(name)),
                                     description="""
                                     The number of {} cores to be online
                                     """.format(name),
                                     setter=self.set_num_cores,
                                     setter_params={'core': name})

        for cpu_no in range(self.target.number_of_cpus):
            param_name = 'cpu{}_online'.format(cpu_no)
            self._runtime_params[param_name] = \
                    RuntimeParameter(param_name, kind=bool,
                                     description="""
                                     Specify whether cpu{} should be online
                                     """.format(cpu_no),
                                     setter=self.set_num_cores,
                                     setter_params={'core': cpu_no})

        if self.target.has('bl'):
            for cluster in ['big', 'little']:
                param_name = 'num_{}_cores'.format(cluster)
                self._runtime_params[param_name] = \
                        RuntimeParameter(param_name, kind=int,
                                         constraint=lambda x, cluster=cluster:
                                                   0 < x <= len(resolve_cpus(cluster, self.target)),
                                         description="""
                                         The number of cores on the {} cluster to be online
                                         """.format(cluster),
                                         setter=self.set_num_cores,
                                         setter_params={'core': cluster})

    def check_target(self):
        if not self.target.has('hotplug'):
            raise TargetError('Target does not appear to support hotplug')

    def validate_parameters(self):
        if len(self.num_cores) == self.target.number_of_cpus:
            if all(v is False for v in self.num_cores.values()):
                raise ValueError('Cannot set number of all cores to 0')

    def commit(self):
        '''Online all CPUs required in order before then off-lining'''
        num_cores = sorted(self.num_cores.iteritems())
        for cpu, online in num_cores:
            if online:<commit_msg>framework/rt_config: Fix not being able to turn off all cpus in cluster<commit_after>import logging
import time
from collections import defaultdict, OrderedDict
from copy import copy

from wa.framework.exception import ConfigError
from wa.framework.plugin import Plugin, Parameter
from wa.utils.misc import resolve_cpus, resolve_unique_domain_cpus
from wa.utils.types import caseless_string, enum

from devlib.exception import TargetError
from devlib.utils.misc import unique
from devlib.utils.types import integer

logger = logging.getLogger('RuntimeConfig')


class RuntimeParameter(Parameter):
    def __init__(self, name, setter, setter_params=None, **kwargs):
        super(RuntimeParameter, self).__init__(name, **kwargs)
        self.setter = setter
        self.setter_params = setter_params or {}

    def set(self, obj, value):
        self.validate_value(self.name, value)
        self.setter(obj, value, **self.setter_params)


class RuntimeConfig(Plugin):

    name = None
    kind = 'runtime-config'

    @property
    def supported_parameters(self):
        return self._runtime_params.values()

    @property
    def core_names(self):
        return unique(self.target.core_names)

    def __init__(self, target, **kwargs):
        super(RuntimeConfig, self).__init__(**kwargs)
        self.target = target
        self._target_checked = False
        self._runtime_params = {}
        try:
            self.initialize()
        except TargetError:
            msg = 'Failed to initialize: "{}"'
            self.logger.debug(msg.format(self.name))
            self._runtime_params = {}

    def initialize(self):
        raise NotImplementedError()

    def commit(self):
        raise NotImplementedError()

    def set_runtime_parameter(self, name, value):
        if not self._target_checked:
            self.check_target()
            self._target_checked = True
        self._runtime_params[name].set(self, value)

    def set_defaults(self):
        for p in self.supported_parameters:
            if p.default:
                self.set_runtime_parameter(p.name, p.default)

    def validate_parameters(self):
        raise NotImplementedError()

    def check_target(self):
        raise NotImplementedError()

    def clear(self):
        raise NotImplementedError()


class HotplugRuntimeConfig(RuntimeConfig):
    '''
    NOTE: Currently will fail if trying to hotplug back a core that
    was hotplugged out when the devlib target was created.
    '''

    name = 'rt-hotplug'

    @staticmethod
    def set_num_cores(obj, value, core):
        cpus = resolve_cpus(core, obj.target)
        max_cores = len(cpus)
        value = integer(value)
        if value > max_cores:
            msg = 'Cannot set number of {}\'s to {}; max is {}'
            raise ValueError(msg.format(core, value, max_cores))

        msg = 'CPU{} Hotplugging already configured'
        # Set cpus to be enabled
        for cpu in cpus[:value]:
            if cpu in obj.num_cores:
                raise ConfigError(msg.format(cpu))
            obj.num_cores[cpu] = True
        # Set the remaining cpus to be disabled.
        for cpu in cpus[value:]:
            if cpu in obj.num_cores:
                raise ConfigError(msg.format(cpu))
            obj.num_cores[cpu] = False

    def __init__(self, target):
        self.num_cores = defaultdict(dict)
        super(HotplugRuntimeConfig, self).__init__(target)

    def initialize(self):
        if not self.target.has('hotplug'):
            return
        param_name = 'num_cores'
        self._runtime_params[param_name] = \
                RuntimeParameter(param_name, kind=int,
                                 constraint=lambda x:
                                         0 <= x <= self.target.number_of_cpus,
                                 description="""
                                 The number of cpu cores to be online
                                 """,
                                 setter=self.set_num_cores,
                                 setter_params={'core': None})

        for name in unique(self.target.platform.core_names):
            param_name = 'num_{}_cores'.format(name)
            self._runtime_params[param_name] = \
                    RuntimeParameter(param_name, kind=int,
                                     constraint=lambda x, name=name:
                                             0 <= x <= len(self.target.core_cpus(name)),
                                     description="""
                                     The number of {} cores to be online
                                     """.format(name),
                                     setter=self.set_num_cores,
                                     setter_params={'core': name})

        for cpu_no in range(self.target.number_of_cpus):
            param_name = 'cpu{}_online'.format(cpu_no)
            self._runtime_params[param_name] = \
                    RuntimeParameter(param_name, kind=bool,
                                     description="""
                                     Specify whether cpu{} should be online
                                     """.format(cpu_no),
                                     setter=self.set_num_cores,
                                     setter_params={'core': cpu_no})

        if self.target.has('bl'):
            for cluster in ['big', 'little']:
                param_name = 'num_{}_cores'.format(cluster)
                self._runtime_params[param_name] = \
                        RuntimeParameter(param_name, kind=int,
                                         constraint=lambda x, cluster=cluster:
                                                   0 <= x <= len(resolve_cpus(cluster, self.target)),
                                         description="""
                                         The number of cores on the {} cluster to be online
                                         """.format(cluster),
                                         setter=self.set_num_cores,
                                         setter_params={'core': cluster})

    def check_target(self):
        if not self.target.has('hotplug'):
            raise TargetError('Target does not appear to support hotplug')

    def validate_parameters(self):
        if len(self.num_cores) == self.target.number_of_cpus:
            if all(v is False for v in self.num_cores.values()):
                raise ValueError('Cannot set number of all cores to 0')

    def commit(self):
        '''Online all CPUs required in order before then off-lining'''
        num_cores = sorted(self.num_cores.iteritems())
        for cpu, online in num_cores:
            if online:
{'fraction_non_alphanumeric': 0.08829506962739932, 'fraction_numerical': 0.000602182913059842, 'mean_word_length': 1.6651955867602808, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 72, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3937636', 'n_tokens_mistral': 3422, 'n_tokens_neox': 3267, 'n_words': 842}