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 7import os 8 9from autotest_lib.client.bin import utils, site_utils 10 11 12class SystemFacadeNativeError(Exception): 13 """Error in SystemFacadeNative.""" 14 pass 15 16 17class 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 ] 29 30 def set_scaling_governor_mode(self, index, mode): 31 """Set mode of CPU scaling governor on one CPU. 32 33 @param index: CPU index starting from 0. 34 35 @param mode: Mode of scaling governor, accept 'interactive' or 36 'performance'. 37 38 @returns: The original mode. 39 40 """ 41 if mode not in self.SCALING_GOVERNOR_MODES: 42 raise SystemFacadeNativeError('mode %s is invalid' % mode) 43 44 governor_path = os.path.join( 45 '/sys/devices/system/cpu/cpu%d' % index, 46 'cpufreq/scaling_governor') 47 if not os.path.exists(governor_path): 48 raise SystemFacadeNativeError( 49 'scaling governor of CPU %d is not available' % index) 50 51 original_mode = utils.read_one_line(governor_path) 52 utils.open_write_close(governor_path, mode) 53 54 return original_mode 55 56 57 def get_cpu_usage(self): 58 """Returns machine's CPU usage. 59 60 Returns: 61 A dictionary with 'user', 'nice', 'system' and 'idle' values. 62 Sample dictionary: 63 { 64 'user': 254544, 65 'nice': 9, 66 'system': 254768, 67 'idle': 2859878, 68 } 69 """ 70 return site_utils.get_cpu_usage() 71 72 73 def compute_active_cpu_time(self, cpu_usage_start, cpu_usage_end): 74 """Computes the fraction of CPU time spent non-idling. 75 76 This function should be invoked using before/after values from calls to 77 get_cpu_usage(). 78 """ 79 return site_utils.compute_active_cpu_time(cpu_usage_start, 80 cpu_usage_end) 81 82 83 def get_mem_total(self): 84 """Returns the total memory available in the system in MBytes.""" 85 return site_utils.get_mem_total() 86 87 88 def get_mem_free(self): 89 """Returns the currently free memory in the system in MBytes.""" 90 return site_utils.get_mem_free() 91 92 93 def get_ec_temperatures(self): 94 """Uses ectool to return a list of all sensor temperatures in Celsius. 95 """ 96 return site_utils.get_ec_temperatures() 97 98 99 def get_current_board(self): 100 """Returns the current device board name.""" 101 return utils.get_current_board() 102 103 104 def get_chromeos_release_version(self): 105 """Returns chromeos version in device under test as string. None on 106 fail. 107 """ 108 return utils.get_chromeos_release_version() 109