def initialize(self, seconds_period=20., pdash_note=''): """Perform necessary initialization prior to power test run. @param seconds_period: float of probing interval in seconds. @param pdash_note: note of the current run to send to power dashboard. @var backlight: power_utils.Backlight object. @var keyvals: dictionary of result keyvals. @var status: power_status.SysStat object. @var _checkpoint_logger: power_status.CheckpointLogger to track checkpoint data. @var _plog: power_status.PowerLogger object to monitor power. @var _psr: power_utils.DisplayPanelSelfRefresh object to monitor PSR. @var _services: service_stopper.ServiceStopper object. @var _start_time: float of time in seconds since Epoch test started. @var _stats: power_status.StatoMatic object. @var _tlog: power_status.TempLogger object to monitor temperatures. @var _clog: power_status.CPUStatsLogger object to monitor CPU(s) frequencies and c-states. @var _meas_logs: list of power_status.MeasurementLoggers """ super(power_Test, self).initialize() self.backlight = power_utils.Backlight() self.backlight.set_default() self.keyvals = dict() self.status = power_status.get_status() self._checkpoint_logger = power_status.CheckpointLogger() measurements = [] if not self.status.on_ac(): measurements.append( power_status.SystemPower(self.status.battery_path)) if power_utils.has_powercap_support(): measurements += power_rapl.create_powercap() elif power_utils.has_rapl_support(): measurements += power_rapl.create_rapl() self._plog = power_status.PowerLogger( measurements, seconds_period=seconds_period, checkpoint_logger=self._checkpoint_logger) self._psr = power_utils.DisplayPanelSelfRefresh() self._services = service_stopper.ServiceStopper( service_stopper.ServiceStopper.POWER_DRAW_SERVICES) self._services.stop_services() self._stats = power_status.StatoMatic() self._tlog = power_status.TempLogger( [], seconds_period=seconds_period, checkpoint_logger=self._checkpoint_logger) self._clog = power_status.CPUStatsLogger( seconds_period=seconds_period, checkpoint_logger=self._checkpoint_logger) self._meas_logs = [self._plog, self._tlog, self._clog] self._pdash_note = pdash_note
def __init__(self, youtube_tab, power_logging=False): self._tab = youtube_tab self._video_duration = 0 self.power_logger = None if power_logging and power_utils.has_rapl_support(): self.power_logger = power_status.PowerLogger( power_rapl.create_rapl()) self.power_logger.start()
def run_once(self, idle_time=120, sleep=10, bt_warmup_time=20): """Collect power stats when bluetooth adapter is on or off. """ with chrome.Chrome(): self._backlight = power_utils.Backlight() self._backlight.set_default() t0 = time.time() self._start_time = t0 self._psr = power_utils.DisplayPanelSelfRefresh(init_time=t0) self.status = power_status.get_status() self._stats = power_status.StatoMatic() measurements = [] if not self.status.on_ac(): measurements.append( power_status.SystemPower(self.status.battery_path)) if power_utils.has_powercap_support(): measurements += power_rapl.create_powercap() elif power_utils.has_rapl_support(): measurements += power_rapl.create_rapl() self._plog = power_status.PowerLogger(measurements, seconds_period=sleep) self._tlog = power_status.TempLogger([], seconds_period=sleep) self._plog.start() self._tlog.start() for _ in xrange(0, idle_time, sleep): time.sleep(sleep) self.status.refresh() self.status.refresh() self._plog.checkpoint('bluetooth_adapter_off', self._start_time) self._tlog.checkpoint('', self._start_time) self._psr.refresh() # Turn on bluetooth adapter. bt_device = bluetooth_device_xmlrpc_server \ .BluetoothDeviceXmlRpcDelegate() # If we cannot start bluetoothd, fail gracefully and still write # data with bluetooth adapter off to file, as we are interested in # just that data too. start_bluetoothd() already logs the error so # not logging any error here. if not bt_device.start_bluetoothd(): return if not bt_device.set_powered(True): logging.warning("Cannot turn on bluetooth adapter.") return time.sleep(bt_warmup_time) if not bt_device._is_powered_on(): logging.warning("Bluetooth adapter is off.") return t1 = time.time() time.sleep(idle_time) self._plog.checkpoint('bluetooth_adapter_on', t1) bt_device.set_powered(False) bt_device.stop_bluetoothd()
def test_power(self, local_path): """ Runs the video power consumption test. @param local_path: the path to the video file. @return a dictionary that contains the test result. """ self._backlight = power_utils.Backlight() self._backlight.set_default() self._service_stopper = service_stopper.ServiceStopper( service_stopper.ServiceStopper.POWER_DRAW_SERVICES) self._service_stopper.stop_services() self._power_status = power_status.get_status() # We expect the DUT is powered by battery now. But this is not always # true due to other bugs. Disable this test temporarily as workaround. # TODO(kcwu): remove this workaround after AC control is stable # crbug.com/723968 if self._power_status.on_ac(): logging.warning('Still powered by AC. Skip this test') return {} # Verify that the battery is sufficiently charged. self._power_status.assert_battery_state(BATTERY_INITIAL_CHARGED_MIN) measurements = [ power_status.SystemPower(self._power_status.battery_path) ] if power_utils.has_rapl_support(): measurements += power_rapl.create_rapl() def get_power(cr): power_logger = power_status.PowerLogger(measurements) power_logger.start() time.sleep(STABILIZATION_DURATION) start_time = time.time() time.sleep(MEASUREMENT_DURATION) power_logger.checkpoint('result', start_time) keyval = power_logger.calc() keyval = { key: keyval[key] for key in keyval if key.endswith('_pwr') } return keyval return self.test_playback(local_path, get_power)
def get_rapl_measurement(tname, exe_function=time.sleep, exe_args=(10,), exe_kwargs={}): """Get rapl measurement. @param name: String of test name. @param exe_function: function that should be executed during measuring rapl readings. @param exe_args: tuple of args to be passed into exe_function. @param exe_kwargs: dict of args to be passed into exe_function. """ logging.info('Now measuring rapl power consumption.') measurement = [] if power_utils.has_rapl_support(): measurement += create_rapl() power_logger = power_status.PowerLogger(measurement) power_logger.start() with power_logger.checkblock(tname): exe_function(*exe_args, **exe_kwargs) keyval = power_logger.calc() return keyval
def initialize(self, percent_initial_charge_min=None, check_network=True, loop_time=3600, loop_count=1, should_scroll='true', should_scroll_up='true', scroll_loop='false', scroll_interval_ms='10000', scroll_by_pixels='600', test_low_batt_p=3, verbose=True, force_wifi=False, wifi_ap='', wifi_sec='none', wifi_pw='', wifi_timeout=60, tasks='', volume_level=10, mic_gain=10, low_batt_margin_p=2, ac_ok=False, log_mem_bandwidth=False, gaia_login=None, force_discharge=False, pdash_note=''): """ percent_initial_charge_min: min battery charge at start of test check_network: check that Ethernet interface is not running loop_time: length of time to run the test for in each loop loop_count: number of times to loop the test for should_scroll: should the extension scroll pages should_scroll_up: should scroll in up direction scroll_loop: continue scrolling indefinitely scroll_interval_ms: how often to scoll scroll_by_pixels: number of pixels to scroll each time test_low_batt_p: percent battery at which test should stop verbose: add more logging information force_wifi: should we force to test to run on wifi wifi_ap: the name (ssid) of the wifi access point wifi_sec: the type of security for the wifi ap wifi_pw: password for the wifi ap wifi_timeout: The timeout for wifi configuration volume_level: percent audio volume level mic_gain: percent audio microphone gain level low_batt_margin_p: percent low battery margin to be added to sys_low_batt_p to guarantee test completes prior to powerd shutdown ac_ok: boolean to allow running on AC log_mem_bandwidth: boolean to log memory bandwidth during the test gaia_login: whether real GAIA login should be attempted. If 'None' (default) then boolean is determined from URL. force_discharge: boolean of whether to tell ec to discharge battery even when the charger is plugged in. pdash_note: note of the current run to send to power dashboard. """ self._backlight = None self._services = None self._browser = None self._loop_time = loop_time self._loop_count = loop_count self._mseconds = self._loop_time * 1000 self._verbose = verbose self._sys_low_batt_p = 0. self._sys_low_batt_s = 0. self._test_low_batt_p = test_low_batt_p self._should_scroll = should_scroll self._should_scroll_up = should_scroll_up self._scroll_loop = scroll_loop self._scroll_interval_ms = scroll_interval_ms self._scroll_by_pixels = scroll_by_pixels self._tmp_keyvals = {} self._power_status = None self._force_wifi = force_wifi self._testServer = None self._tasks = tasks.replace(' ','') self._backchannel = None self._shill_proxy = None self._volume_level = volume_level self._mic_gain = mic_gain self._ac_ok = ac_ok self._log_mem_bandwidth = log_mem_bandwidth self._wait_time = 60 self._stats = collections.defaultdict(list) self._force_discharge = force_discharge self._pdash_note = pdash_note if not power_utils.has_battery(): if ac_ok and (power_utils.has_powercap_support() or power_utils.has_rapl_support()): logging.info("Device has no battery but has powercap data.") else: rsp = "Skipping test for device without battery and powercap." raise error.TestNAError(rsp) self._power_status = power_status.get_status() self._tmp_keyvals['b_on_ac'] = self._power_status.on_ac() self._gaia_login = gaia_login if gaia_login is None: self._gaia_login = power_load_util.use_gaia_login() self._username = power_load_util.get_username() self._password = power_load_util.get_password() if not ac_ok: self._power_status.assert_battery_state(percent_initial_charge_min) if force_discharge: if not power_utils.charge_control_by_ectool(False): raise error.TestError('Could not run battery force discharge.') # If force wifi enabled, convert eth0 to backchannel and connect to the # specified WiFi AP. if self._force_wifi: sec_config = None # TODO(dbasehore): Fix this when we get a better way of figuring out # the wifi security configuration. if wifi_sec == 'rsn' or wifi_sec == 'wpa': sec_config = xmlrpc_security_types.WPAConfig( psk=wifi_pw, wpa_mode=xmlrpc_security_types.WPAConfig.MODE_PURE_WPA2, wpa2_ciphers= [xmlrpc_security_types.WPAConfig.CIPHER_CCMP]) wifi_config = xmlrpc_datatypes.AssociationParameters( ssid=wifi_ap, security_config=sec_config, configuration_timeout=wifi_timeout) # If backchannel is already running, don't run it again. self._backchannel = backchannel.Backchannel() if not self._backchannel.setup(): raise error.TestError('Could not setup Backchannel network.') self._shill_proxy = wifi_proxy.WifiProxy() self._shill_proxy.remove_all_wifi_entries() for i in xrange(1,4): raw_output = self._shill_proxy.connect_to_wifi_network( wifi_config.ssid, wifi_config.security, wifi_config.security_parameters, wifi_config.save_credentials, station_type=wifi_config.station_type, hidden_network=wifi_config.is_hidden, discovery_timeout_seconds= wifi_config.discovery_timeout, association_timeout_seconds= wifi_config.association_timeout, configuration_timeout_seconds= wifi_config.configuration_timeout * i) result = xmlrpc_datatypes.AssociationResult. \ from_dbus_proxy_output(raw_output) if result.success: break logging.warn('wifi connect: disc:%d assoc:%d config:%d fail:%s', result.discovery_time, result.association_time, result.configuration_time, result.failure_reason) else: raise error.TestError('Could not connect to WiFi network.') else: # Find all wired ethernet interfaces. ifaces = [ iface for iface in interface.get_interfaces() if (not iface.is_wifi_device() and iface.name.startswith('eth')) ] logging.debug(str([iface.name for iface in ifaces])) for iface in ifaces: if check_network and iface.is_lower_up: raise error.TestError('Ethernet interface is active. ' + 'Please remove Ethernet cable') # record the max backlight level self._backlight = power_utils.Backlight() self._tmp_keyvals['level_backlight_max'] = \ self._backlight.get_max_level() self._services = service_stopper.ServiceStopper( service_stopper.ServiceStopper.POWER_DRAW_SERVICES) self._services.stop_services() self._detachable_handler = power_utils.BaseActivitySimulator() # fix up file perms for the power test extension so that chrome # can access it os.system('chmod -R 755 %s' % self.bindir) # setup a HTTP Server to listen for status updates from the power # test extension self._testServer = httpd.HTTPListener(8001, docroot=self.bindir) self._testServer.run() # initialize various interesting power related stats self._statomatic = power_status.StatoMatic() self._power_status.refresh() self._sys_low_batt_p = float(utils.system_output( 'check_powerd_config --low_battery_shutdown_percent')) self._sys_low_batt_s = int(utils.system_output( 'check_powerd_config --low_battery_shutdown_time')) if self._sys_low_batt_p and self._sys_low_batt_s: raise error.TestError( "Low battery percent and seconds are non-zero.") min_low_batt_p = min(self._sys_low_batt_p + low_batt_margin_p, 100) if self._sys_low_batt_p and (min_low_batt_p > self._test_low_batt_p): logging.warning("test low battery threshold is below system " + "low battery requirement. Setting to %f", min_low_batt_p) self._test_low_batt_p = min_low_batt_p if self._power_status.battery: self._ah_charge_start = self._power_status.battery[0].charge_now self._wh_energy_start = self._power_status.battery[0].energy
def run_once(self): """Test main loop.""" t0 = time.time() # record the PSR related info. psr = power_utils.DisplayPanelSelfRefresh(init_time=t0) try: self._keyboard_backlight = power_utils.KbdBacklight() self._set_keyboard_backlight_level() except power_utils.KbdBacklightException as e: logging.info("Assuming no keyboard backlight due to :: %s", str(e)) self._keyboard_backlight = None measurements = [] if self._power_status.battery: measurements += \ [power_status.SystemPower(self._power_status.battery_path)] if power_utils.has_powercap_support(): measurements += power_rapl.create_powercap() elif power_utils.has_rapl_support(): measurements += power_rapl.create_rapl() self._checkpoint_logger = power_status.CheckpointLogger() self._plog = power_status.PowerLogger(measurements, seconds_period=20, checkpoint_logger=self._checkpoint_logger) self._tlog = power_status.TempLogger([], seconds_period=20, checkpoint_logger=self._checkpoint_logger) self._clog = power_status.CPUStatsLogger( seconds_period=20, checkpoint_logger=self._checkpoint_logger) self._meas_logs = [self._plog, self._tlog, self._clog] for log in self._meas_logs: log.start() if self._log_mem_bandwidth: self._mlog = memory_bandwidth_logger.MemoryBandwidthLogger( raw=False, seconds_period=2) self._mlog.start() # record start time and end time for each task self._task_tracker = [] ext_path = os.path.join(os.path.dirname(__file__), 'extension') self._tmp_keyvals['username'] = self._username arc_mode = arc_common.ARC_MODE_DISABLED if utils.is_arc_available(): arc_mode = arc_common.ARC_MODE_ENABLED try: self._browser = chrome.Chrome(extension_paths=[ext_path], gaia_login=self._gaia_login, username=self._username, password=self._password, arc_mode=arc_mode) except exceptions.LoginException: # already failed guest login if not self._gaia_login: raise self._gaia_login = False logging.warn("Unable to use GAIA acct %s. Using GUEST instead.\n", self._username) self._browser = chrome.Chrome(extension_paths=[ext_path], gaia_login=self._gaia_login) if not self._gaia_login: self._tmp_keyvals['username'] = '******' extension = self._browser.get_extension(ext_path) for k in params_dict: if getattr(self, params_dict[k]) is not '': extension.ExecuteJavaScript('var %s = %s;' % (k, getattr(self, params_dict[k]))) # This opens a trap start page to capture tabs opened for first login. # It will be closed when startTest is run. extension.ExecuteJavaScript('chrome.windows.create(null, null);') for i in range(self._loop_count): start_time = time.time() extension.ExecuteJavaScript('startTest();') # the power test extension will report its status here latch = self._testServer.add_wait_url('/status') # this starts a thread in the server that listens to log # information from the script script_logging = self._testServer.add_wait_url(url='/log') # dump any log entry that comes from the script into # the debug log self._testServer.add_url_handler(url='/log',\ handler_func=(lambda handler, forms, loop_counter=i:\ _extension_log_handler(handler, forms, loop_counter))) pagetime_tracking = self._testServer.add_wait_url(url='/pagetime') self._testServer.add_url_handler(url='/pagetime',\ handler_func=(lambda handler, forms, test_instance=self, loop_counter=i:\ _extension_page_time_info_handler(handler, forms, loop_counter, test_instance))) # setup a handler to simulate waking up the base of a detachable # on user interaction. On scrolling, wake for 1s, on page # navigation, wake for 10s. self._testServer.add_url(url='/pagenav') self._testServer.add_url(url='/scroll') self._testServer.add_url_handler(url='/pagenav', handler_func=(lambda handler, args, plt=self: plt._detachable_handler.wake_base(10000))) self._testServer.add_url_handler(url='/scroll', handler_func=(lambda handler, args, plt=self: plt._detachable_handler.wake_base(1000))) # reset backlight level since powerd might've modified it # based on ambient light self._set_backlight_level() self._set_lightbar_level() if self._keyboard_backlight: self._set_keyboard_backlight_level() audio_helper.set_volume_levels(self._volume_level, self._mic_gain) low_battery = self._do_wait(self._verbose, self._loop_time, latch) script_logging.set() pagetime_tracking.set() self._log_loop_checkpoint(i, start_time, time.time()) if self._verbose: logging.debug('loop %d completed', i) if low_battery: logging.info('Exiting due to low battery') break # done with logging from the script, so we can collect that thread t1 = time.time() psr.refresh() self._tmp_keyvals['minutes_battery_life_tested'] = (t1 - t0) / 60 self._tmp_keyvals.update(psr.get_keyvals())
def run_once(self, video_url, video_short_name): """Runs the graphics_VideoRenderingPower test. @param video_url: URL with autoplay video inside. It's assumed that there's just one <video> in the HTML, and that it fits in the viewport. @param video_short_name: short string describing the video; itt will be presented as part of the dashboard entry name. """ # TODO(mcasas): Extend this test to non-Intel platforms. if not power_utils.has_rapl_support(): logging.warning( 'This board has no RAPL power measurement support, ' 'skipping test.') return rapl = [] if power_utils.has_battery(): rapl.append( power_status.SystemPower(self._power_status.battery_path)) else: logging.warning('This board has no battery.') rapl += power_rapl.create_rapl() for test_name_and_flags in TEST_NAME_AND_FLAGS: logging.info('Test case: %s', test_name_and_flags[0]) # Launch Chrome with the appropriate flag combination. with chrome.Chrome(extra_browser_args=test_name_and_flags[1], init_network_controller=True) as cr: if not utils.wait_for_idle_cpu(IDLE_CPU_WAIT_TIMEOUT_SECONDS, IDLE_CPU_LOAD_PERCENTAGE): raise error.TestFail('Failed: Could not get idle CPU.') if not utils.wait_for_cool_machine(): raise error.TestFail('Failed: Could not get cold machine.') tab = cr.browser.tabs[0] tab.Navigate(video_url) tab.WaitForDocumentReadyStateToBeComplete() tab.EvaluateJavaScript( 'document.' 'getElementsByTagName(\'video\')[0].scrollIntoView(true)') # Disabling hardware overlays is difficult because the flag is # already in the browser. Instead, scroll a bit down to make the # video bleed out of the viewport. if '--enable-hardware-overlays=' in test_name_and_flags[1]: tab.EvaluateJavaScript('window.scrollBy(0, 1)') power_logger = power_status.PowerLogger(rapl) power_logger.start() time.sleep(PREAMBLE_DURATION_SECONDS) start_time = time.time() time.sleep(MEASUREMENT_DURATION_SECONDS) power_logger.checkpoint('result', start_time) measurements = power_logger.calc() logging.debug(measurements) for category in sorted(measurements): if category.endswith('_pwr'): description = '%s_%s_%s' % ( video_short_name, test_name_and_flags[0], category) self.output_perf_value(description=description, value=measurements[category], units='W', higher_is_better=False, graph=GRAPH_NAME) if category.endswith('_pwr_avg'): # write_perf_keyval() wants units (W) first in lowercase. description = '%s_%s_%s' % ( video_short_name, test_name_and_flags[0], category) self.write_perf_keyval( {'w_' + description: measurements[category]})