1# Copyright (c) 2012 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
5import logging, time
6
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib.cros import chrome
9from autotest_lib.client.cros import power_rapl, power_status, power_utils
10from autotest_lib.client.cros import service_stopper
11
12
13class power_Idle(test.test):
14    version = 1
15
16    def initialize(self):
17        """Perform necessary initialization prior to test run.
18
19        Private Attributes:
20          _backlight: power_utils.Backlight object
21          _services: service_stopper.ServiceStopper object
22        """
23        super(power_Idle, self).initialize()
24        self._backlight = None
25        self._services = service_stopper.ServiceStopper(
26            service_stopper.ServiceStopper.POWER_DRAW_SERVICES)
27        self._services.stop_services()
28
29
30    def warmup(self, warmup_time=60):
31        time.sleep(warmup_time)
32
33
34    def run_once(self, idle_time=120, sleep=10):
35        with chrome.Chrome():
36            self._backlight = power_utils.Backlight()
37            self._backlight.set_default()
38
39            self._start_time = time.time()
40            self.status = power_status.get_status()
41            self._stats = power_status.StatoMatic()
42
43            measurements = []
44            if not self.status.on_ac():
45                measurements.append(
46                    power_status.SystemPower(self.status.battery_path))
47            if power_utils.has_rapl_support():
48                measurements += power_rapl.create_rapl()
49            self._plog = power_status.PowerLogger(measurements,
50                                                  seconds_period=sleep)
51            self._tlog = power_status.TempLogger([], seconds_period=sleep)
52            self._plog.start()
53            self._tlog.start()
54
55            for _ in xrange(0, idle_time, sleep):
56                time.sleep(sleep)
57                self.status.refresh()
58            self.status.refresh()
59            self._plog.checkpoint('', self._start_time)
60            self._tlog.checkpoint('', self._start_time)
61
62
63    def postprocess_iteration(self):
64        keyvals = self._stats.publish()
65
66        # record the current and max backlight levels
67        self._backlight = power_utils.Backlight()
68        keyvals['level_backlight_max'] = self._backlight.get_max_level()
69        keyvals['level_backlight_current'] = self._backlight.get_level()
70
71        # record battery stats if not on AC
72        if self.status.on_ac():
73            keyvals['b_on_ac'] = 1
74        else:
75            keyvals['b_on_ac'] = 0
76            keyvals['ah_charge_full'] = self.status.battery[0].charge_full
77            keyvals['ah_charge_full_design'] = \
78                                self.status.battery[0].charge_full_design
79            keyvals['ah_charge_now'] = self.status.battery[0].charge_now
80            keyvals['a_current_now'] = self.status.battery[0].current_now
81            keyvals['wh_energy'] = self.status.battery[0].energy
82            keyvals['w_energy_rate'] = self.status.battery[0].energy_rate
83            keyvals['h_remaining_time'] = self.status.battery[0].remaining_time
84            keyvals['v_voltage_min_design'] = \
85                                self.status.battery[0].voltage_min_design
86            keyvals['v_voltage_now'] = self.status.battery[0].voltage_now
87            keyvals.update(self._plog.calc())
88
89        keyvals.update(self._tlog.calc())
90        logging.debug("keyvals = %s", keyvals)
91
92        self.write_perf_keyval(keyvals)
93
94
95    def cleanup(self):
96        if self._backlight:
97            self._backlight.restore()
98        self._services.restore_services()
99        super(power_Idle, self).cleanup()
100