-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbatman2ppd.py
More file actions
executable file
·469 lines (375 loc) · 15.8 KB
/
batman2ppd.py
File metadata and controls
executable file
·469 lines (375 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/python3
# SPDX-License-Identifier: GPL-2.0-only
# Copyright (C) 2025 Bardia Moshiri <fakeshell@bardia.tech>
import configparser
import subprocess
import asyncio
import gbinder
import time
import os
from dbus_fast.aio import MessageBus
from dbus_fast.service import ServiceInterface, method, dbus_property, signal
from dbus_fast.constants import PropertyAccess
from dbus_fast import Variant, BusType
THERMAL_SYSFS_PATH = "/sys/class/thermal"
class PPDInterface(ServiceInterface):
def __init__(self, loop, bus):
super().__init__('net.hadess.PowerProfiles')
self.loop = loop
self.bus = bus
self.cookie = 0
self.cores = os.cpu_count()
self.props = {
'ActiveProfile': Variant('s', 'balanced'),
'PerformanceInhibited': Variant('s', ''),
'PerformanceDegraded': Variant('s', ''),
'Profiles': Variant('aa{sv}', [
{
'Profile': Variant('s', 'power-saver'),
'Driver': Variant('s', 'batman')
},
{
'Profile': Variant('s', 'balanced'),
'Driver': Variant('s', 'batman')
},
{
'Profile': Variant('s', 'performance'),
'Driver': Variant('s', 'batman')
}
]),
'Actions': Variant('as', ['trickle_charge']),
'ActionsInfo': Variant('aa{sv}', [
{
'Name': Variant('s', 'trickle_charge'),
'Description': Variant('s', 'Configure power supply to trickle charge'),
'Enabled': Variant('b', True)
}
]),
'ActiveProfileHolds': Variant('aa{sv}', []),
'Version': Variant('s', '0.30'),
'BatteryAware': Variant('b', True)
}
self.loop.create_task(self.initialize_profile())
async def thermal_check(self):
while True:
await self.update_performance_degraded()
await asyncio.sleep(5)
async def initialize_profile(self):
self.create_state_file()
profile = self.get_profile()
if profile:
await self.set_active_profile(profile)
self.loop.create_task(self.thermal_check())
@dbus_property(access=PropertyAccess.READWRITE)
async def ActiveProfile(self) -> 's':
return self.props['ActiveProfile'].value
@ActiveProfile.setter
async def ActiveProfile(self, profile: 's'):
await self.set_active_profile(profile)
async def set_active_profile(self, profile):
if os.path.exists("/var/lib/batman/default_cpu_governor"):
with open("/var/lib/batman/default_cpu_governor", "r") as default_governor_file:
default_governor = default_governor_file.read()
else:
default_governor = ""
if profile == "performance" and self.props['PerformanceDegraded'].value == "":
set_vr(True)
set_mtkpower(45)
if os.path.exists("/var/lib/batman/CUSTOM_FIRSTPOLCORE"):
online_half(self.cores)
os.remove("/var/lib/batman/CUSTOM_FIRSTPOLCORE")
if default_governor:
with open("/var/lib/batman/CUSTOM_DEFAULT_GOVERNOR", "w+") as f:
f.write("performance\n")
await restart_service('batman')
elif profile == "balanced":
set_vr(False)
set_mtkpower(21) # PROCESS_CREATE, much less intensive than UX_MOVE_SCROLLING (45) which pushes everything up to 100
if os.path.exists("/var/lib/batman/CUSTOM_FIRSTPOLCORE"):
online_half(self.cores)
os.remove("/var/lib/batman/CUSTOM_FIRSTPOLCORE")
if default_governor:
with open("/var/lib/batman/CUSTOM_DEFAULT_GOVERNOR", "w+") as f:
f.write(default_governor)
await restart_service('batman')
elif profile == "power-saver":
set_vr(False)
set_mtkpower(21) # PROCESS_CREATE, much less intensive than UX_MOVE_SCROLLING (45) which pushes everything up to 100
if default_governor:
with open("/var/lib/batman/CUSTOM_FIRSTPOLCORE", "w+") as f:
half_cores = self.cores // 2
#print(half_cores)
f.write(f'{half_cores}')
await restart_service('batman')
offline_half(self.cores)
self.props['ActiveProfile'] = Variant('s', profile)
self.set_profile(profile)
@dbus_property(access=PropertyAccess.READ)
def PerformanceInhibited(self) -> 's':
return self.props['PerformanceInhibited'].value
@dbus_property(access=PropertyAccess.READ)
def PerformanceDegraded(self) -> 's':
return self.props['PerformanceDegraded'].value
@dbus_property(access=PropertyAccess.READ)
def Profiles(self) -> 'aa{sv}':
return self.props['Profiles'].value
@dbus_property(access=PropertyAccess.READ)
def Actions(self) -> 'as':
return self.props['Actions'].value
@dbus_property(access=PropertyAccess.READ)
def ActionsInfo(self) -> 'aa{sv}':
return self.props['ActionsInfo'].value
@dbus_property(access=PropertyAccess.READ)
def ActiveProfileHolds(self) -> 'aa{sv}':
return self.props['ActiveProfileHolds'].value
def hold_profile(self, profile, reason, application_id):
self.cookie += 1
hold = {
'Profile': Variant('s', profile),
'Reason': Variant('s', reason),
'ApplicationId': Variant('s', application_id),
'Cookie': Variant('u', self.cookie)
}
current_holds = self.props['ActiveProfileHolds'].value
current_holds.append(hold)
self.props['ActiveProfileHolds'] = Variant('aa{sv}', current_holds)
return self.cookie
@method()
def HoldProfile(self, profile: 's', reason: 's', application_id: 's') -> 'u':
return self.hold_profile(profile, reason, application_id)
@method()
def ReleaseProfile(self, cookie: 'u'):
self.release_profile(cookie)
self.ProfileReleased()
@method()
def SetActionEnabled(self, action: 's', enabled: 'b'):
self.set_action_enabled(action, enabled)
def set_action_enabled(action, enabled):
pass
@signal()
def ProfileReleased(self) -> 'u':
return self.cookie
@dbus_property(access=PropertyAccess.READ)
def Version(self) -> 's':
return self.props['Version'].value
@dbus_property(access=PropertyAccess.READWRITE)
def BatteryAware(self) -> 'b':
return self.props['BatteryAware'].value
@BatteryAware.setter
def BatteryAware(self, battery_aware: 'b'):
self.set_battery_aware(battery_aware)
def set_battery_aware(self, battery_aware):
self.props['BatteryAware'] = Variant('b', battery_aware)
async def update_performance_degraded(self):
temp_avg = await get_thermal()
if temp_avg > 50:
self.props['PerformanceDegraded'] = Variant('s', 'high-operating-temperature')
else:
self.props['PerformanceDegraded'] = Variant('s', '')
def create_state_file(self):
directory = "/var/lib/power-profiles-daemon/"
file_path = os.path.join(directory, "state.ini")
if not os.path.exists(directory):
os.makedirs(directory)
config = configparser.ConfigParser()
if not os.path.isfile(file_path):
config['State'] = {'Driver': 'batman', 'Profile': 'balanced'}
with open(file_path, 'w') as state_file:
config.write(state_file)
else:
config.read(file_path)
if not config.has_option('State', 'Driver') or not config.has_option('State', 'Profile'):
config['State'] = {'Driver': 'batman', 'Profile': 'balanced'}
with open(file_path, 'w') as state_file:
config.write(state_file)
def set_profile(self, profile):
directory = "/var/lib/power-profiles-daemon/"
file_path = os.path.join(directory, "state.ini")
config = configparser.ConfigParser()
if not os.path.isfile(file_path):
config["State"] = {'Profile': profile}
with open(file_path, 'w') as state_file:
config.write(state_file)
else:
config.read(file_path)
config.set('State', 'Profile', profile)
with open(file_path, 'w') as state_file:
config.write(state_file)
def get_profile(self):
directory = "/var/lib/power-profiles-daemon/"
file_path = os.path.join(directory, "state.ini")
config = configparser.ConfigParser()
if os.path.isfile(file_path):
try:
config.read(file_path)
profile = config.get('State', 'Profile')
except (configparser.NoSectionError, configparser.NoOptionError):
profile = "balanced"
return profile
return "balanced"
class UPowerPPDInterface(ServiceInterface):
def __init__(self, ppd_interface):
super().__init__('org.freedesktop.UPower.PowerProfiles')
self.ppd_interface = ppd_interface
@dbus_property(access=PropertyAccess.READWRITE)
async def ActiveProfile(self) -> 's':
return self.ppd_interface.props['ActiveProfile'].value
@ActiveProfile.setter
async def ActiveProfile(self, profile: 's'):
await self.ppd_interface.set_active_profile(profile)
@dbus_property(access=PropertyAccess.READ)
def PerformanceInhibited(self) -> 's':
return self.ppd_interface.props['PerformanceInhibited'].value
@dbus_property(access=PropertyAccess.READ)
def PerformanceDegraded(self) -> 's':
return self.ppd_interface.props['PerformanceDegraded'].value
@dbus_property(access=PropertyAccess.READ)
def Profiles(self) -> 'aa{sv}':
return self.ppd_interface.props['Profiles'].value
@dbus_property(access=PropertyAccess.READ)
def Actions(self) -> 'as':
return self.ppd_interface.props['Actions'].value
@dbus_property(access=PropertyAccess.READ)
def ActionsInfo(self) -> 'aa{sv}':
return self.ppd_interface.props['ActionsInfo'].value
@dbus_property(access=PropertyAccess.READ)
def ActiveProfileHolds(self) -> 'aa{sv}':
return self.ppd_interface.props['ActiveProfileHolds'].value
@method()
def HoldProfile(self, profile: 's', reason: 's', application_id: 's') -> 'u':
return self.ppd_interface.hold_profile(profile, reason, application_id)
@method()
def ReleaseProfile(self, cookie: 'u'):
self.ProfileReleased()
@method()
def SetActionEnabled(self, action: 's', enabled: 'b'):
self.ppd_interface.set_action_enabled(action, enabled)
@signal()
def ProfileReleased(self) -> 'u':
return self.ppd_interface.cookie
@dbus_property(access=PropertyAccess.READ)
def Version(self) -> 's':
return self.ppd_interface.props['Version'].value
@dbus_property(access=PropertyAccess.READWRITE)
def BatteryAware(self) -> 'b':
return self.ppd_interface.props['BatteryAware'].value
@BatteryAware.setter
def BatteryAware(self, battery_aware: 'b'):
self.ppd_interface.set_battery_aware(battery_aware)
async def restart_service(service_name: str) -> bool:
try:
if not service_name.endswith('.service'):
service_name = f"{service_name}.service"
bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
introspection = await bus.introspect(
'org.freedesktop.systemd1',
'/org/freedesktop/systemd1'
)
proxy_object = bus.get_proxy_object(
'org.freedesktop.systemd1',
'/org/freedesktop/systemd1',
introspection
)
systemd = proxy_object.get_interface('org.freedesktop.systemd1.Manager')
await systemd.call_restart_unit(service_name, 'replace')
bus.disconnect()
return True
except Exception as e:
print(f"Error restarting {service_name}: {e}")
return False
def offline_half(cpu_count):
half_cpus = cpu_count // 2
for i in range(half_cpus):
cpu_path = f"/sys/devices/system/cpu/cpu{i}/online"
try:
with open(cpu_path, 'w') as f:
f.write('0')
except Exception as e:
print(f"Error disabling CPU {i}: {e}")
def online_half(cpu_count):
half_cpus = cpu_count // 2
for i in range(half_cpus):
cpu_path = f"/sys/devices/system/cpu/cpu{i}/online"
try:
with open(cpu_path, 'w') as f:
f.write('1')
#print(f"Enabled CPU {i}")
except Exception as e:
print(f"Error enabling CPU {i}: {e}")
def set_vr(enabled):
available = find_hidl("android.hardware.vr@1.0::IVr/default")
if available:
vr_state = "1" if enabled else "0"
result = subprocess.run(f"batman-hybris vr {vr_state}", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
return result.returncode == 0
return False
def set_mtkpower(state):
available = find_hidl("vendor.mediatek.hardware.mtkpower@1.0::IMtkPower/default")
if available:
result = subprocess.run(f"batman-hybris mtkpower {state}", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
return result.returncode == 0
return False
def find_hidl(intf):
try:
sm = gbinder.ServiceManager("/dev/hwbinder")
return intf in sm.list_sync()
except:
return False
### GTherm equivalent implementation ###
def get_thermal_zones():
zones = []
for item in os.listdir(THERMAL_SYSFS_PATH):
if item.startswith("thermal_zone"):
zones.append(item)
return zones
def read_sysfs_file(path):
try:
with open(path, 'r') as file:
return file.read().strip()
except IOError as e:
return None
def get_zone_temp(zone):
temp_path = os.path.join(THERMAL_SYSFS_PATH, zone, "temp")
return read_sysfs_file(temp_path)
def get_zone_type(zone):
type_path = os.path.join(THERMAL_SYSFS_PATH, zone, "type")
return read_sysfs_file(type_path)
async def connect_to_dbus():
return await MessageBus(bus_type=BusType.SYSTEM).connect()
async def get_thermal():
i = 0
temp_total = 0
temp_avg = 0
zones = get_thermal_zones()
for zone in zones:
raw_temperature = get_zone_temp(zone)
if raw_temperature is not None:
temperature_celsius = int(raw_temperature) / 1000.0
# most devices (exynos chipsets being one of them) show a bunch of useless nodes under 10c
# the temperature reading here is incorrect. instead of going through each kernel one by one
# we know that anything below 5-10C is basically impossible under normal usage and environment.
# this is not a perfect solution but should be good enough for now.
if temperature_celsius > 10:
#print(f"{zone}: {temperature_celsius:.1f}C")
temp_total += temperature_celsius
i += 1
if i > 0:
temp_avg = temp_total / i
#print(f"Average sys temp: {temp_avg:.1f}C")
return temp_avg
async def main():
hadess_bus = await connect_to_dbus()
upower_bus = await connect_to_dbus()
loop = asyncio.get_running_loop()
ppd_interface = PPDInterface(loop, hadess_bus)
hadess_bus.export('/net/hadess/PowerProfiles', ppd_interface)
await hadess_bus.request_name('net.hadess.PowerProfiles')
upower_interface = UPowerPPDInterface(ppd_interface)
upower_bus.export('/org/freedesktop/UPower/PowerProfiles', upower_interface)
await upower_bus.request_name('org.freedesktop.UPower.PowerProfiles')
await asyncio.gather(
hadess_bus.wait_for_disconnect(),
upower_bus.wait_for_disconnect()
)
if __name__ == "__main__":
asyncio.run(main())