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

Wait AC Power #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Wait AC Power #45

wants to merge 2 commits into from

Conversation

comio
Copy link
Contributor

@comio comio commented Jan 30, 2018

NOT TESTED
Only for review and discussion purpose.

@comio comio changed the title Wait ac power Wait AC Power Jan 30, 2018
wait_ac_power() {
local timecount=0
local timeout=0
local ac_power_device=/sys/class/power_supply/AC/online
Copy link
Owner

Choose a reason for hiding this comment

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

How reliable is this? The file does not exist on my system. Though there's a user-defined path for that I don't think we should require to go to /sys and let the user find the right one. I don't see any "stable ABI" path defined in the kernel docs so we'd need at least some heuristic for the path of fallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, I'm still thinking about a way to grab the information without use external tools (like acpi client) and reading from /sys directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

#45 (comment)

That said, I seem to remember reading that the /sys interface for power was somewhat depreciated...IIRC acpi client or upower were the recommended alternatives. 'could be wrong about that, as the memory is very vague!

@comio
Copy link
Contributor Author

comio commented Jan 31, 2018

@sten0, Can you review the proposed code.
Can you attach output of find /sys/class/power_supply -name online?. Thanks.

luigi

@sten0
Copy link
Contributor

sten0 commented Feb 7, 2018

@comio No output.
Linux-4.9.80 on Debian 9.
/sys/class/power_supply/AC: symbolic link to ../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/ACPI0003:00/power_supply/AC
/sys/class/power_supply/BAT0: symbolic link to ../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:08/PNP0C09:00/PNP0C0A:00/power_supply/BAT0

On the other hand, if [ -f /sys/class/power_supply/*/online ]; then echo 'We are online"; fi works

wait_ac_power() {
local timecount=0
local timeout=0
local ac_power_device=/sys/class/power_supply/AC/online
Copy link
Contributor

Choose a reason for hiding this comment

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

#45 (comment)

That said, I seem to remember reading that the /sys interface for power was somewhat depreciated...IIRC acpi client or upower were the recommended alternatives. 'could be wrong about that, as the memory is very vague!

# AC is not online
[ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1
sleep 1s
timecount=$((timecount+1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we sleep for at least 30sec, so laptops' cpus can stay in deep sleep longer? (or is it appropriate to make this configurable?)

Copy link
Owner

Choose a reason for hiding this comment

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

I'd rather make the timeout longer and not add another config variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

how long? I hate hidden timeouts :D

#
# Path of AC status flag
BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/AC/online"
BTRFS_AC_POWER_DEVICE="/sys/class/power_supply/*/online"
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like it will do the trick for laptops. I mentioned upower in another comment, because I think that /sys/class/power_supply/*/online will return true even when a server is on UPS battery. (eg: NUT or apcupsd). IIRC it provides a generic interface, and alternatives are upsmon (NUT) and apcaccess -p TONBATT (apcupsd)

Copy link
Owner

Choose a reason for hiding this comment

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

We may want to revisit that later, with support for UPS, but for now I'm fine with the more relaxed check. Meanwhile I verified the path exists on my other systems, so I think there's nothing more left to discuss.

Copy link
Contributor Author

@comio comio Feb 20, 2018

Choose a reason for hiding this comment

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

I had no time to dedicate to search a portable solution yet. @sten0, can you provide some example of upower, upsmon, apsacces (with outputs). I have a very basic system (my home NAS) and I cannot checks all these tools.

@comio
Copy link
Contributor Author

comio commented Feb 22, 2018

@stem0 @kdave

we can backport this script from OpenRC:

on_ac_power

and use this implementation if on_ac_power command is not present. This will move all /proc/blablabla logic into an external command/function.

@kdave
Copy link
Owner

kdave commented Mar 6, 2018

I'm not sure if the on_ac_power is generally available, so we'll need some fallback anyway, but systemd has /usr/lib/systemd/systemd-ac-power so we can cover most cases I think.

local timecount=0
local timeout=$1

while ! check_power_status "$@"; do
Copy link
Owner

Choose a reason for hiding this comment

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

The timeout value will be propagated to check_power_status, so we need a shift

# AC is not online
[ $timeout -gt 0 ] && [ $timecount -ge $timeout ] && return 1
sleep 30s
timecount=$((timecount+1))
Copy link
Owner

Choose a reason for hiding this comment

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

If my math is correct, this will wait the 30 times more than the config value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was 1s but i changed just before to commit ("1s is too short" I said).

@comio
Copy link
Contributor Author

comio commented Mar 6, 2018

this is the code of on_ac_power:

systemd:on_ac_power

it's almost the same.

@kdave
Copy link
Owner

kdave commented Mar 6, 2018

So the way you implement it in 14c44e9 it will just wait for AC and if it does not show up, the task continues. Is this desired from the user's POV? Eg. should we make it more configurable:

  • if AC is not up after timeout, cancel the task (eg. skip balance)
  • if AC is not up, continue anyway (eg. run scrub that's read-only and typically not that hungry as balance)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants