Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 import glob, logging, os, re, time | 1 import glob, logging, os, re, time |
| 2 from autotest_lib.client.bin import utils | 2 from autotest_lib.client.bin import utils |
| 3 from autotest_lib.client.common_lib import error | 3 from autotest_lib.client.common_lib import error |
| 4 | 4 |
| 5 | 5 |
| 6 class DevStat(object): | 6 class DevStat(object): |
| 7 """ | 7 """ |
| 8 Device power status. This class implements generic status initialization | 8 Device power status. This class implements generic status initialization |
| 9 and parsing routines. | 9 and parsing routines. |
| 10 """ | 10 """ |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 return field_type(0) | 34 return field_type(0) |
| 35 | 35 |
| 36 | 36 |
| 37 def read_all_vals(self): | 37 def read_all_vals(self): |
| 38 for field, prop in self.fields.iteritems(): | 38 for field, prop in self.fields.iteritems(): |
| 39 if prop[0]: | 39 if prop[0]: |
| 40 val = self.read_val(prop[0], prop[1]) | 40 val = self.read_val(prop[0], prop[1]) |
| 41 setattr(self, field, val) | 41 setattr(self, field, val) |
| 42 | 42 |
| 43 | 43 |
| 44 class ThermalStat(DevStat): | |
| 45 """ | |
| 46 Thermal status. | |
| 47 | |
| 48 Fields: | |
|
Duncan Laurie
2011/04/06 16:57:34
I think the list in the comments was copy+paste fr
Simon Que
2011/04/07 20:59:46
Done.
| |
| 49 | |
| 50 float charge_full: Last full capacity reached [Ah] | |
| 51 float charge_full_design: Full capacity by design [Ah] | |
| 52 float charge_now: Remaining charge [Ah] | |
| 53 float current_now: Battery discharge rate [A] | |
| 54 float energy: Current battery charge [Wh] | |
| 55 float energy_full: Last full capacity reached [Wh] | |
| 56 float energy_full_design: Full capacity by design [Wh] | |
| 57 float energy_rate: Battery discharge rate [W] | |
| 58 float remaining_time: Remaining discharging time [h] | |
| 59 float voltage_min_design: Minimum voltage by design [V] | |
| 60 float voltage_now: Voltage now [V] | |
|
Sameer Nanda
2011/04/06 17:53:54
comments need to fixed up to match thermal fields.
Simon Que
2011/04/07 20:59:46
Done.
| |
| 61 """ | |
| 62 | |
| 63 thermal_fields = { | |
| 64 'enabled': ['enabled', float], | |
| 65 'temp': ['temp', float], | |
| 66 'type': ['type', float], | |
| 67 'num_points_tripped': ['', ''] | |
| 68 } | |
| 69 def __init__(self, path=None): | |
| 70 # Dynamically generate the rest of the fields | |
| 71 thermal_point_strings = [ 'trip_point_%d_type', | |
| 72 'trip_point_%d_temp', | |
| 73 'cdev%d_trip_point' ] | |
| 74 thermal_point_types = [ str, int, int ] | |
| 75 self.num_trip_points = 0 | |
| 76 | |
| 77 for i in range(10): | |
|
Duncan Laurie
2011/04/06 16:57:34
10 is probably safe but it may be useful to make t
Sameer Nanda
2011/04/06 17:53:54
lets use python glob to find all matching names in
Simon Que
2011/04/07 20:59:46
Done.
| |
| 78 for j in range(len(thermal_point_strings)): | |
| 79 file = thermal_point_strings[j] % i | |
| 80 | |
| 81 if os.path.exists(path + file): | |
| 82 self.thermal_fields[file] = [file, thermal_point_types[j]] | |
| 83 if j == 0: | |
| 84 num_trip_points += 1 | |
| 85 | |
| 86 super(ThermalStat, self).__init__(self.thermal_fields, path) | |
| 87 self.update() | |
| 88 | |
| 89 def update(self): | |
| 90 if not os.path.exists(self.path): | |
| 91 return | |
| 92 | |
| 93 self.read_all_vals() | |
| 94 self.num_points_tripped = 0 | |
| 95 | |
| 96 for i in range(self.num_trip_points): | |
| 97 if self.temp > self.read_val('trip_point_%d_temp' % i, int): | |
| 98 self.num_points_tripped += 1 | |
| 99 | |
| 100 | |
| 101 | |
| 44 class BatteryStat(DevStat): | 102 class BatteryStat(DevStat): |
| 45 """ | 103 """ |
| 46 Battery status. | 104 Battery status. |
| 47 | 105 |
| 48 Fields: | 106 Fields: |
| 49 | 107 |
| 50 float charge_full: Last full capacity reached [Ah] | 108 float charge_full: Last full capacity reached [Ah] |
| 51 float charge_full_design: Full capacity by design [Ah] | 109 float charge_full_design: Full capacity by design [Ah] |
| 52 float charge_now: Remaining charge [Ah] | 110 float charge_now: Remaining charge [Ah] |
| 53 float current_now: Battery discharge rate [A] | 111 float current_now: Battery discharge rate [A] |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 self.online = self.is_online == 1 | 186 self.online = self.is_online == 1 |
| 129 | 187 |
| 130 | 188 |
| 131 class SysStat(object): | 189 class SysStat(object): |
| 132 """ | 190 """ |
| 133 System power status for a given host. | 191 System power status for a given host. |
| 134 | 192 |
| 135 Fields: | 193 Fields: |
| 136 | 194 |
| 137 battery: A list of BatteryStat objects. | 195 battery: A list of BatteryStat objects. |
| 138 linepower: A list of LineStat opbjects. | 196 linepower: A list of LineStat objects. |
| 139 """ | 197 """ |
| 140 | 198 |
| 141 def __init__(self): | 199 def __init__(self): |
| 142 power_supply_path = '/sys/class/power_supply/*' | 200 power_supply_path = '/sys/class/power_supply/*' |
| 143 self.battery = None | 201 self.battery = None |
| 144 self.linepower = None | 202 self.linepower = None |
| 203 self.thermal = None | |
| 145 battery_path = None | 204 battery_path = None |
| 146 linepower_path = None | 205 linepower_path = None |
| 206 thermal_path = '/sys/class/thermal/thermal_zone0' | |
| 207 | |
| 147 power_supplies = glob.glob(power_supply_path) | 208 power_supplies = glob.glob(power_supply_path) |
| 148 for path in power_supplies: | 209 for path in power_supplies: |
| 149 type_path = os.path.join(path,'type') | 210 type_path = os.path.join(path,'type') |
| 150 if not os.path.exists(type_path): | 211 if not os.path.exists(type_path): |
| 151 continue | 212 continue |
| 152 type = utils.read_one_line(type_path) | 213 type = utils.read_one_line(type_path) |
| 153 if type == 'Battery': | 214 if type == 'Battery': |
| 154 battery_path = path | 215 battery_path = path |
| 155 elif type == 'Mains': | 216 elif type == 'Mains': |
| 156 linepower_path = path | 217 linepower_path = path |
| 157 if battery_path and linepower_path: | 218 if battery_path and linepower_path: |
| 158 self.battery_path = battery_path | 219 self.battery_path = battery_path |
| 159 self.linepower_path = linepower_path | 220 self.linepower_path = linepower_path |
| 160 else: | 221 else: |
| 161 raise error.TestError('Battery or Linepower path not found') | 222 raise error.TestError('Battery or Linepower path not found') |
| 223 self.thermal_path = thermal_path | |
| 162 | 224 |
| 225 self.min_temp = 999999999 | |
| 226 self.max_temp = -999999999 | |
| 163 | 227 |
| 164 def refresh(self): | 228 def refresh(self): |
| 165 """ | 229 """ |
| 166 Initialize device power status objects for a single battery and a | 230 Initialize device power status objects for a single battery and a |
| 167 single power line by parsing the output of devkit-power -d. | 231 single power line by parsing the output of devkit-power -d. |
| 168 """ | 232 """ |
| 169 self.battery = [ BatteryStat(self.battery_path) ] | 233 self.battery = [ BatteryStat(self.battery_path) ] |
| 170 self.linepower = [ LineStat(self.linepower_path) ] | 234 self.linepower = [ LineStat(self.linepower_path) ] |
| 235 self.thermal = [ ThermalStat(self.thermal_path) ] | |
| 236 | |
| 237 try: | |
| 238 if self.thermal[0].temp < self.min_temp: | |
| 239 self.min_temp = self.thermal[0].temp | |
| 240 if self.thermal[0].temp > self.max_temp: | |
| 241 self.max_temp = self.thermal[0].temp | |
| 242 except: | |
| 243 logging.error('Could not read temperature, skipping.') | |
| 171 | 244 |
| 172 | 245 |
| 173 def get_status(): | 246 def get_status(): |
| 174 """ | 247 """ |
| 175 Return a new power status object (SysStat). A new power status snapshot | 248 Return a new power status object (SysStat). A new power status snapshot |
| 176 for a given host can be obtained by either calling this routine again and | 249 for a given host can be obtained by either calling this routine again and |
| 177 constructing a new SysStat object, or by using the refresh method of the | 250 constructing a new SysStat object, or by using the refresh method of the |
| 178 SysStat object. | 251 SysStat object. |
| 179 """ | 252 """ |
| 180 status = SysStat() | 253 status = SysStat() |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 | 462 |
| 390 active = int(utils.read_file(active_duration_path)) | 463 active = int(utils.read_file(active_duration_path)) |
| 391 connected = int(utils.read_file(connected_duration_path)) | 464 connected = int(utils.read_file(connected_duration_path)) |
| 392 logging.debug('device %s active for %.2f%%', | 465 logging.debug('device %s active for %.2f%%', |
| 393 path, active * 100.0 / connected) | 466 path, active * 100.0 / connected) |
| 394 | 467 |
| 395 total_active += active | 468 total_active += active |
| 396 total_connected += connected | 469 total_connected += connected |
| 397 | 470 |
| 398 return total_active, total_connected | 471 return total_active, total_connected |
| OLD | NEW |