Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SunkenHero Code optimization & addition of battery percentage #432

Merged
merged 16 commits into from
Sep 10, 2022
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 13 additions & 69 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
)
CPUS = os.cpu_count()

# ignore these devices under /sys/class/power_supply/
POWER_SUPPLY_IGNORELIST = ["hidpp_battery"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its not needed anymore, but i am not sure

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do still need it, check #332 PR for reference.

# Note:
# "load1m" & "cpuload" can't be global vars and to in order to show correct data must be
# decraled where their execution takes place
Expand Down Expand Up @@ -200,60 +197,14 @@ def charging():
"""
get charge state: is battery charging or discharging
"""
power_supply_path = "/sys/class/power_supply/"
power_supplies = os.listdir(Path(power_supply_path))
# sort it so AC is 'always' first
power_supplies = sorted(power_supplies)

# check if we found power supplies. on a desktop these are not found
# and we assume we are on a powercable.
if len(power_supplies) == 0:
# nothing found found, so nothing to check
return True
# we found some power supplies, lets check their state
else:
for supply in power_supplies:
# Check if supply is in ignore list
ignore_supply = any(item in supply for item in POWER_SUPPLY_IGNORELIST)
# If found in ignore list, skip it.
if ignore_supply:
continue
return True if psutil.sensors_battery().power_plugged else False

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using psutil to get charging status

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in other comment this was all added with #308 as battery state would not be showing properly on certain devices, still thinking if we might need some of it or if psutil will be able to pick it all up itself ...

try:
with open(Path(power_supply_path + supply + "/type")) as f:
supply_type = f.read()[:-1]
if supply_type == "Mains":
# we found an AC
try:
with open(Path(power_supply_path + supply + "/online")) as f:
val = int(f.read()[:-1])
if val == 1:
# we are definitely charging
return True
except FileNotFoundError:
# we could not find online, check next item
continue
elif supply_type == "Battery":
# we found a battery, check if its being discharged
try:
with open(Path(power_supply_path + supply + "/status")) as f:
val = str(f.read()[:-1])
if val == "Discharging":
# we found a discharging battery
return False
except FileNotFoundError:
# could not find status, check the next item
continue
else:
# continue to next item because current is not
# "Mains" or "Battery"
continue
except FileNotFoundError:
# could not find type, check the next item
continue

# we cannot determine discharging state, assume we are on powercable
return True
def battery_percentage():
"""
get batery percentage
"""
return psutil.sensors_battery().percent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Function to get Battery Percentage

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reference on why it was the way it was done in the past #308


def get_avail_gov():
Expand Down Expand Up @@ -993,10 +944,10 @@ def set_autofreq():

# determine which governor should be used
if charging():
print("Battery is: charging\n")
print("Battery: " + str(battery_percentage()) + "%\tcharging\n")
set_performance()
else:
print("Battery is: discharging\n")
print("Battery: " + str(battery_percentage()) + "%\tdischarging\n")
set_powersave()


Expand All @@ -1009,12 +960,12 @@ def mon_autofreq():

# determine which governor should be used
if charging():
print("Battery is: charging\n")
print("Battery: " + str(battery_percentage()) + "%\tcharging\n")
get_current_gov()
print(f'Suggesting use of "{get_avail_performance()}" governor')
mon_performance()
else:
print("Battery is: discharging\n")
print("Battery: " + str(battery_percentage()) + "%\tdischarging\n")
get_current_gov()
print(f'Suggesting use of "{get_avail_powersave()}" governor')
mon_powersave()
Expand Down Expand Up @@ -1119,16 +1070,9 @@ def sysinfo():
core = cpu_core[cpu]
cpu_temp_index = core_temp_labels.index(f"Core {core}")
temp_per_cpu[i] = core_temp["coretemp"][cpu_temp_index].current
elif "k10temp" in core_temp:
# https://www.kernel.org/doc/Documentation/hwmon/k10temp
temp_per_cpu = [core_temp["k10temp"][0].current] * online_cpu_count
elif "zenpower" in core_temp:
# https://github.com/AdnanHodzic/auto-cpufreq/issues/145#issuecomment-763294009
temp_per_cpu = [core_temp["zenpower"][0].current] * online_cpu_count
elif "acpitz" in core_temp:
temp_per_cpu = [core_temp["acpitz"][0].current] * online_cpu_count
elif "thinkpad" in core_temp:
temp_per_cpu = [core_temp["thinkpad"][0].current] * online_cpu_count
else:
temp = list(psutil.sensors_temperatures())
temp_per_cpu = [core_temp[temp[0]][0].current] * online_cpu_count
except Exception as e:
print(repr(e))
pass
Expand Down