Home | History | Annotate | Download | only in multimedia
      1 # Copyright 2015 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Facade to access the system-related functionality."""
      6 
      7 import os
      8 
      9 from autotest_lib.client.bin import utils
     10 
     11 
     12 class SystemFacadeNativeError(Exception):
     13     """Error in SystemFacadeNative."""
     14     pass
     15 
     16 
     17 class SystemFacadeNative(object):
     18     """Facede to access the system-related functionality.
     19 
     20     The methods inside this class only accept Python native types.
     21 
     22     """
     23     SCALING_GOVERNOR_MODES = [
     24             'interactive',
     25             'performance',
     26             'ondemand',
     27             'powersave',
     28             'sched'
     29             ]
     30 
     31     def set_scaling_governor_mode(self, index, mode):
     32         """Set mode of CPU scaling governor on one CPU.
     33 
     34         @param index: CPU index starting from 0.
     35 
     36         @param mode: Mode of scaling governor, accept 'interactive' or
     37                      'performance'.
     38 
     39         @returns: The original mode.
     40 
     41         """
     42         if mode not in self.SCALING_GOVERNOR_MODES:
     43             raise SystemFacadeNativeError('mode %s is invalid' % mode)
     44 
     45         governor_path = os.path.join(
     46                 '/sys/devices/system/cpu/cpu%d' % index,
     47                 'cpufreq/scaling_governor')
     48         if not os.path.exists(governor_path):
     49             raise SystemFacadeNativeError(
     50                     'scaling governor of CPU %d is not available' % index)
     51 
     52         original_mode = utils.read_one_line(governor_path)
     53         utils.open_write_close(governor_path, mode)
     54 
     55         return original_mode
     56 
     57 
     58     def get_cpu_usage(self):
     59         """Returns machine's CPU usage.
     60 
     61         Returns:
     62             A dictionary with 'user', 'nice', 'system' and 'idle' values.
     63             Sample dictionary:
     64             {
     65                 'user': 254544,
     66                 'nice': 9,
     67                 'system': 254768,
     68                 'idle': 2859878,
     69             }
     70         """
     71         return utils.get_cpu_usage()
     72 
     73 
     74     def compute_active_cpu_time(self, cpu_usage_start, cpu_usage_end):
     75         """Computes the fraction of CPU time spent non-idling.
     76 
     77         This function should be invoked using before/after values from calls to
     78         get_cpu_usage().
     79         """
     80         return utils.compute_active_cpu_time(cpu_usage_start,
     81                                                   cpu_usage_end)
     82 
     83 
     84     def get_mem_total(self):
     85         """Returns the total memory available in the system in MBytes."""
     86         return utils.get_mem_total()
     87 
     88 
     89     def get_mem_free(self):
     90         """Returns the currently free memory in the system in MBytes."""
     91         return utils.get_mem_free()
     92 
     93 
     94     def get_ec_temperatures(self):
     95         """Uses ectool to return a list of all sensor temperatures in Celsius.
     96         """
     97         return utils.get_ec_temperatures()
     98 
     99 
    100     def get_current_board(self):
    101         """Returns the current device board name."""
    102         return utils.get_current_board()
    103 
    104 
    105     def get_chromeos_release_version(self):
    106         """Returns chromeos version in device under test as string. None on
    107         fail.
    108         """
    109         return utils.get_chromeos_release_version()
    110