Skip to content

lib: handle sysfs extraction failures by enabling failbit exception - #183

Open
sdharme wants to merge 1 commit into
fenrus75:masterfrom
sdharme:master
Open

lib: handle sysfs extraction failures by enabling failbit exception#183
sdharme wants to merge 1 commit into
fenrus75:masterfrom
sdharme:master

Conversation

@sdharme

@sdharme sdharme commented Jul 4, 2025

Copy link
Copy Markdown

ensure read failure is detected if file is empty or contains invalid data. Closes #179.

@paulmenzel paulmenzel left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Awesome, that you came up with a fix. Maybe elaborate in the commit message and give your battery reporting issue as an example? Also give the old and new power usage value?

Comment thread src/lib.cpp Outdated
On some systems, reading a sysfs attribute may fail due to underlying
kernel or firmware (BIOS/ACPI) issues. In my case, accessing the
"current_now" attribute while on battery power results in:

$ cat /sys/class/power_supply/BAT0/current_now
cat: /sys/class/power_supply/BAT0/current_now: No such device

This commit simplifies the error handling in read_sysfs() function by
enabling failbit exception, which also covers the edge case mentioned
above.

Closes fenrus75#179.

Signed-off-by: Siddhesh Dharme <siddheshdharme18@gmail.com>
@sdharme sdharme changed the title lib: handle stream extraction failure with fail() check lib: handle sysfs extraction failures by enabling failbit exception Jul 7, 2025
@sdharme

sdharme commented Jul 7, 2025

Copy link
Copy Markdown
Author

Awesome, that you came up with a fix. Maybe elaborate in the commit message and give your battery reporting issue as an example? Also give the old and new power usage value?

Thanks! I’ve updated the commit message. Here are before and after screenshots:

Before:
powertop-before

After:
powertop-after

The discharge rate is now omitted because read_sysfs() fails cleanly and returns 0 when the sysfs read is invalid, instead of parsing garbage values. Since 0 < 0.0001, it's filtered out in the UI:

if (pw > 0.0001) {
char buf[32];
wprintw(win, _("The battery reports a discharge rate of %sW\n"),
fmt_prefix(pw, buf));
wprintw(win, _("The energy consumed was %sJ\n"),
fmt_prefix(joules, buf));
need_linebreak = 1;
}

I think that's preferable to showing incorrect values.

@paulmenzel

Copy link
Copy Markdown

Awesome, that you came up with a fix. Maybe elaborate in the commit message and give your battery reporting issue as an example? Also give the old and new power usage value?

Thanks! I’ve updated the commit message. Here are before and after screenshots:

[…]

Awesome. Thank you for the great commit message, and your thorough answer. Much appreciated!

@dyfrgi dyfrgi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Would it make sense to enable exceptions for the other two failure modes, eof and bad? eof would occur if the file exists but is empty, and bad if there's something like a failure in allocation or other exception caught by the istream operations.

Otherwise, LGTM.

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.

Unusually high battery discharge rate reported by powertop

3 participants