49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
"""Low-overhead, read-only battery and thermal sampler for a RAM trial."""
|
|
import json
|
|
from pathlib import Path
|
|
import time
|
|
|
|
|
|
def values(directory, names):
|
|
result = {}
|
|
for name in names:
|
|
try:
|
|
result[name] = (directory / name).read_text().strip()
|
|
except OSError:
|
|
pass
|
|
return result
|
|
|
|
|
|
def sample():
|
|
supplies = {}
|
|
for path in sorted(Path('/sys/class/power_supply').iterdir()):
|
|
supplies[path.name] = values(path, (
|
|
'type', 'status', 'online', 'capacity', 'current_now',
|
|
'voltage_now', 'power_now', 'charge_now', 'energy_now', 'temp'))
|
|
thermal = {}
|
|
for path in sorted(Path('/sys/class/thermal').glob('thermal_zone*')):
|
|
item = values(path, ('type', 'temp'))
|
|
if item:
|
|
thermal[path.name] = item
|
|
idle = {}
|
|
for path in sorted(Path('/sys/devices/system/cpu').glob('cpu[0-9]*/cpuidle/state*')):
|
|
item = values(path, ('name', 'time', 'usage', 'disable'))
|
|
if item:
|
|
idle[str(path.relative_to('/sys/devices/system/cpu'))] = item
|
|
return {'timeUnix': time.time(), 'timeMonotonic': time.monotonic(),
|
|
'powerSupplies': supplies, 'thermalZones': thermal, 'cpuIdle': idle}
|
|
|
|
|
|
def main():
|
|
output = Path('/run/tundra/battery-idle.jsonl')
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
with output.open('a', buffering=1) as stream:
|
|
while True:
|
|
stream.write(json.dumps(sample(), separators=(',', ':')) + '\n')
|
|
time.sleep(30)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|