def _get_platform_descriptor(self):
        '''Fill out the platform descriptor to be used by the test.'''

        present = utils.read_file(os.path.join(CPU_INFO_ROOT, 'present'))
        if present.count('-') != 1:
            raise error.TestError("can't determine number of cores from %s" %
                                  present)
        (min_core, max_core) = tuple(int(x) for x in present.split('-'))
        min_freq = int(
            utils.read_file(PER_CORE_FREQ_TEMPLATE % (0, 'cpuinfo_min_freq')))
        max_freq = int(
            utils.read_file(PER_CORE_FREQ_TEMPLATE % (0, 'cpuinfo_max_freq')))

        return PlatformDescriptor(max_core - min_core + 1, max_freq, min_freq)
    def _get_cpu_freq_raised(self):
        '''
        Bring all cores clock to max frequency.

        This function uses the scaling_governor mechanism to force the cores
        to run at maximum frequency, writing the string 'performance' into
        each core's governor file.

        The current value (if not 'performance') is preserved to be restored
        in the end of the test.

        Returns a dictionary where keys are the core numbers and values are
        the preserved governor setting.

        raises TestError in case 'performance' setting is not allowed on any
               of the cores, or the clock frequency does not reach max on any
               of the cores in 1 second.
        '''

        rv = {}
        for cpu in range(self.pl_desc.num_cores):
            target = 'performance'
            gov_file = PER_CORE_FREQ_TEMPLATE % (cpu, GOVERNOR)
            current_gov = utils.read_file(gov_file).strip()
            available_govs = utils.read_file(
                PER_CORE_FREQ_TEMPLATE % (cpu, AVAILABLE_GOVERNORS)).split()

            if current_gov != target:
                if not target in available_govs:
                    raise error.TestError('core %d does not allow setting %s' %
                                          (cpu, target))
                logging.info('changing core %d governor from %s to %s' %
                             (cpu, current_gov, target))
                utils.open_write_close(gov_file, target)
                rv[cpu] = current_gov

        for _ in range(2):  # Wait for no more than 1 second
            for cpu in range(self.pl_desc.num_cores):
                if self._cpu_freq(cpu) != self.pl_desc.max_cpu_freq:
                    break
            else:
                return rv

        freqs = []
        for cpu in range(self.pl_desc.num_cores):
            freqs.append('%d' % self._cpu_freq(cpu))
        raise error.TestError('failed to speed up some CPU clocks: %s' %
                              ', '.join(freqs))
    def _cpu_temp(self):
        '''Return current CPU temperature, a float value.'''

        return float(
            utils.read_file(
                os.path.join(self.temperature_data_path,
                             EXT_TEMP_SENSOR_FILE)))
Esempio n. 4
0
def lazy_load(path):
    """
    Lazily loads the file indicated by the path given, and caches the result
    """
    if path not in LAZY_LOADED_FILES:
        LAZY_LOADED_FILES[path] = utils.read_file(path)

    return LAZY_LOADED_FILES[path]
Esempio n. 5
0
def lazy_load(path):
    """
    Lazily loads the file indicated by the path given, and caches the result
    """
    if path not in LAZY_LOADED_FILES:
        LAZY_LOADED_FILES[path] = utils.read_file(path)

    return LAZY_LOADED_FILES[path]
Esempio n. 6
0
def set_trace_clock():
    """ Sets trace clock to mono time as chrome tracing in CrOS. """
    PREFERRED_TRACE_CLOCK = 'mono'
    clock = common_lib_utils.read_file(TRACE_CLOCK)
    m = re.match(r'.*\[(\w+)\]', clock)
    if m:
        orig_clock = m.group(1)
        atexit.register(common_lib_utils.open_write_close,
                        TRACE_CLOCK, orig_clock)
    common_lib_utils.open_write_close(TRACE_CLOCK, PREFERRED_TRACE_CLOCK)
Esempio n. 7
0
    def _migrate_from_base(self):
        self.confirm_initialization()

        migration_script = utils.read_file(
            os.path.join(os.path.dirname(__file__), 'schema_051.sql'))
        migration_script = migration_script % (dict(
            username=self._database.get_database_info()['username']))
        self.execute_script(migration_script)

        self.create_migrate_table()
        self.set_db_version(51)
Esempio n. 8
0
    def _migrate_from_base(self):
        self.confirm_initialization()

        migration_script = utils.read_file(
                os.path.join(os.path.dirname(__file__), 'schema_051.sql'))
        migration_script = migration_script % (
                dict(username=self._database.get_database_info()['username']))
        self.execute_script(migration_script)

        self.create_migrate_table()
        self.set_db_version(51)
    def _throttle_count(self):
        '''
        Return current throttling status of all cores.

        The return integer value is the sum of all cores' throttling status.
        When the sum is equal the core number - all cores are throttling.
        '''

        count = 0
        for cpu in range(self.pl_desc.num_cores):
            count += int(
                utils.read_file(PER_CORE_FREQ_TEMPLATE % (cpu, 'throttle')))
        return count
Esempio n. 10
0
    def get_control_file_contents(self, test_path):
        """
        Get the contents of the control file at |test_path|.

        @return The contents of the aforementioned file.
        @throws ControlFileNotFound if the file cannot be retrieved.
        """
        try:
            return utils.read_file(test_path)
        except EnvironmentError as (errno, strerror):
            msg = "Can't retrieve {0}: {1} ({2})".format(
                test_path, strerror, errno)
            raise error.ControlFileNotFound(msg)
Esempio n. 11
0
def get_ui_use_flags():
    """Parses the USE flags as listed in /etc/ui_use_flags.txt.

    @return: A list of flag strings found in the ui use flags file.
    """
    flags = []
    for flag in utils.read_file(_UI_USE_FLAGS_FILE_PATH).splitlines():
        # Removes everything after the '#'.
        flag_before_comment = flag.split('#')[0].strip()
        if len(flag_before_comment) != 0:
            flags.append(flag_before_comment)

    return flags
Esempio n. 12
0
 def init(self):
     self.cros_system_data = {}
     (_, fname) = tempfile.mkstemp()
     f = open(fname, 'w')
     self._client.run('crossystem', stdout_tee=f)
     f.close()
     text = utils.read_file(fname)
     for line in text.splitlines():
         assignment_string = line.split('#')[0]
         if not assignment_string.count('='):
             continue
         (name, value) = assignment_string.split('=', 1)
         self.cros_system_data[name.strip()] = value.strip()
     os.remove(fname)
Esempio n. 13
0
    def _throttle_limit(self):
        '''
        Return current CPU throttling temperature threshold.

        If CPU temperature exceeds this value, clock throttling is activated,
        causing CPU slowdown.

        Returns the limit as a float value.
        '''

        return float(
            utils.read_file(
                os.path.join(self.temperature_data_path,
                             THROTTLE_EXT_LIMIT_FILE)))
Esempio n. 14
0
def read_file(path_to_file, host=None):
    """Reads the file and returns the file content
    @param path_to_file: Full path to the file
    @param host: DUT object
    @return: Returns the content of file
    """
    if host:
        if not host.path_exists(path_to_file):
            raise error.TestError("No such file or directory %s" %
                                  path_to_file)
        return host.run('cat %s' % path_to_file).stdout.strip()

    if not os.path.isfile(path_to_file):
        raise error.TestError("No such file or directory %s" % path_to_file)
    return utils.read_file(path_to_file)
Esempio n. 15
0
    def initialize(self):
        cpufreq_path = '/sys/devices/system/cpu/cpu*/cpufreq'

        dirs = glob.glob(cpufreq_path)
        if not dirs:
            raise error.TestFail('cpufreq not supported')

        self._cpufreq_dirs = dirs
        self._cpus = [cpufreq(dirname) for dirname in dirs]
        for cpu in self._cpus:
            cpu.save_state()

        # Store the setting if the system has CPUQuiet feature
        if os.path.exists(SYSFS_CPUQUIET_ENABLE):
            self.is_cpuquiet_enabled = utils.read_file(SYSFS_CPUQUIET_ENABLE)
            utils.write_one_line(SYSFS_CPUQUIET_ENABLE, '0')
 def initialize(self):
     # Store the setting if the system has CPUQuiet feature
     if os.path.exists(SYSFS_CPUQUIET_ENABLE):
         self.is_cpuquiet_enabled = utils.read_file(SYSFS_CPUQUIET_ENABLE)
         utils.write_one_line(SYSFS_CPUQUIET_ENABLE, '0')
 def _append_hash(self):
     f = open(self.file, 'ab')
     f.write(utils.read_file(self.hash_file))
     f.close()
Esempio n. 18
0
    def _cpu_freq(self, cpu):
        '''Return current clock frequency of a CPU, integer in Kilohertz.'''

        return int(
            utils.read_file(PER_CORE_FREQ_TEMPLATE %
                            (cpu, 'cpuinfo_cur_freq')))
Esempio n. 19
0
def set_simple_switch(value, filename):
    """ Sets simple switch '1' to the file. """
    orig = common_lib_utils.read_file(filename).strip()
    atexit.register(common_lib_utils.open_write_close, filename, orig)
    common_lib_utils.open_write_close(filename, value)