You are not logged in.
My laptop doesn't reliably generate battery discharge ACPI events, so I created a systemd timer, unit, and script to check the battery level periodically and hibernate my machine if it drops too low. However, when this timer is running, dbus-daemon uses approximately 60% CPU and systemd-logind uses approximately 25% CPU continuously. Here's the timer file:
[Unit]
Description=Check battery level periodically and hibernate when low
[Timer]
OnBootSec=30s
OnUnitActiveSec=30s
[Install]
WantedBy=timers.targetI tried running strace on both dbus-daemon and got an overwhelming number of system calls to sendmsg and recvmsg of the form
sendmsg(10, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\4\1\1\7\1\0\0\351=\323\7\245\0\0\0\1\1o\0007\0\0\0/org/freedesktop/systemd1/unit/auto_5fhibernate_2etimer\0\2\1s\0\37\0\0\0org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\7\1s\0\4\0\0\0:1.1\0\0\0\0", iov_len=184}, {iov_base="\36\0\0\0org.freedesktop.systemd1.Timer\0\0\264\0\0\0\26\0\0\0NextElapseUSecRealtime\0\1t\0\0\0\377\377\377\377\377\377\377\377\27\0\0\0NextElapseUSecMonotonic\0\1t\0\0\377\377\377\377\377\377\377\377\17\0\0\0LastTriggerUSec\0\1t\0\0\277WU\\ L\5\0\30\0\0\0LastTriggerUSecMonotonic\0\1t\0b\270~\361\3\0\0\0\6\0\0\0Result\0\1s\0\0\0\7\0\0\0success\0'\0\0\0\17\0\0\0TimersMonotonic\0\16\0\0\0TimersCalendar\0", iov_len=263}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 447
sendmsg(10, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="l\4\1\1\34\3\0\0\352=\323\7\245\0\0\0\1\1o\0007\0\0\0/org/freedesktop/systemd1/unit/auto_5fhibernate_2etimer\0\2\1s\0\37\0\0\0org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\7\1s\0\4\0\0\0:1.1\0\0\0\0", iov_len=184}, {iov_base="\35\0\0\0org.freedesktop.systemd1.Unit\0\0\0\360\2\0\0\v\0\0\0ActiveState\0\1s\0\0\6\0\0\0active\0\0\10\0\0\0SubState\0\1s\0\7\0\0\0running\0\0\0\0\0\24\0\0\0StateChangeTimestamp\0\1t\0\0\0\0\0\300WU\\ L\5\0\35\0\0\0StateChangeTimestampMonotonic\0\1t\0\0\0\0b\270~\361\3\0\0\0\25\0\0\0InactiveExitTimestamp\0\1t\0\0\0\0\224i\234\352\37L\5\0\36\0\0\0InactiveExitTimestampMonotonic\0\1t\0\0\0007\312\305\177\3\0\0\0\24\0\0\0ActiveEnterTimestamp\0\1t\0\0\0\0\0\224i\234\352\37L\5\0\35\0\0\0ActiveEnterTimestampMonotonic\0\1t\0\0\0\0007\312\305\177\3\0\0\0\23\0\0\0ActiveExitTimestamp\0\1t\0\0\0\0\0\0\4X\27\334\37L\5\0\34\0\0\0ActiveExitTimestampMonotonic\0\1t\0\0\0\0\0\247\270@q\3\0\0\0\26\0\0\0InactiveEnterTimestamp\0\1t\0\0\0\4X\27\334\37L\5\0\37\0\0\0InactiveEnterTimestampMonotonic\0\1t\0\0\247\270@q\3\0\0\0\3\0\0\0Job\0\4(uo)\0\0\0\0\0\0\0\1\0\0\0/\0\0\0\0\0\0\0\17\0\0\0ConditionResult\0\1b\0\0\1\0\0\0\0\0\0\0\f\0\0\0AssertResult\0\1b\0\1\0\0\0\22\0\0\0ConditionTimestamp\0\1t\0\0\0\0\0\0\0\215i\234\352\37L\5\0\33\0\0\0ConditionTimestampMonotonic\0\1t\0\0\0\0\0\0/\312\305\177\3\0\0\0\17\0\0\0AssertTimestamp\0\1t\0\0\215i\234\352\37L\5\0\30\0\0\0AssertTimestampMonotonic\0\1t\0000\312\305\177\3\0\0\0\0\0\0\0", iov_len=796}], msg_iovlen=2, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 980and
recvmsg(9, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="\0\0/org/freedesktop/systemd1\0\0\0\0\0\0\0\2\1s\0 \0\0\0org.freedesktop.systemd1.Manager\0\0\0\0\0\0\0\0\3\1s\0\n\0\0\0JobRemoved\0\0\0\0\0\0\10\1g\0\4uoss\0\0\0\0\0\0\0\211\4\216O(\0\0\0/org/freedesktop/systemd1/job/1334707337\0\0\0\0\26\0\0\0auto_hibernate.service\0\0\4\0\0\0done\0l\4\1\1\7\1\0\0\355=\323\7\226\0\0\0\1\1o\0007\0\0\0/org/freedesktop/systemd1/unit/auto_5fhibernate_2etimer\0\2\1s\0\37\0\0\0org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\36\0\0\0org.freedesktop.systemd1.Timer\0\0\264\0\0\0\26\0\0\0NextElapseUSecRealtime\0\1t\0\0\0\0\0\0\0\0\0\0\0\27\0\0\0NextElapseUSecMonotonic\0\1t\0\0\320\242\325\33\2\0\0\0\17\0\0\0LastTriggerUSec\0\1t\0\0\277WU\\ L\5\0\30\0\0\0LastTriggerUSecMonotonic\0\1t\0b\270~\361\3\0\0\0\6\0\0\0Result\0\1s\0\0\0\7\0\0\0success\0'\0\0\0\17\0\0\0TimersMonotonic\0\16\0\0\0TimersCalendar\0l\4\1\1\34\3\0\0\356=\323\7\226\0\0\0\1\1o\0007\0\0\0/org/freedesktop/systemd1/unit/auto_5fhibernate_2etimer\0\2\1s\0\37\0\0\0org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\35\0\0\0org.freedesktop.systemd1.Unit\0\0\0\360\2\0\0\v\0\0\0ActiveState\0\1s\0\0\6\0\0\0active\0\0\10\0\0\0SubState\0\1s\0\7\0\0\0waiting\0\0\0\0\0\24\0\0\0StateChangeTimestamp\0\1t\0\0\0\0\0PXU\\ L\5\0\35\0\0\0StateChangeTimestampMonotonic\0\1t\0\0\0\0\363\270~\361\3\0\0\0\25\0\0\0InactiveExitTimestamp\0\1t\0\0\0\0\224i\234\352\37L\5\0\36\0\0\0InactiveExitTimestampMonotonic\0\1t\0\0\0007\312\305\177\3\0\0\0\24\0\0\0ActiveEnterTimestamp\0\1t\0\0\0\0\0\224i\234\352\37L\5\0\35\0\0\0ActiveEnterTimestampMonotonic\0\1t\0\0\0\0007\312\305\177\3\0\0\0\23\0\0\0ActiveExitTimestamp\0\1t\0\0\0\0\0\0\4X\27\334\37L\5\0\34\0\0\0ActiveExitTimestampMonotonic\0\1t\0\0\0\0\0\247\270@q\3\0\0\0\26\0\0\0InactiveEnterTimestamp\0\1t\0\0\0\4X\27\334\37L\5\0\37\0\0\0InactiveEnterTimestampMonotonic\0\1t\0\0\247\270@q\3\0\0\0\3\0\0\0Job\0\4(uo)\0\0\0\0\0\0\0\1\0\0\0/\0\0\0\0\0\0\0\17\0\0\0ConditionResult\0\1b\0\0\1\0\0\0\0\0\0\0\f\0\0\0AssertResult\0\1b\0\1\0\0\0\22\0\0\0ConditionTimestamp\0\1t\0\0\0\0\0\0\0\215i\234\352\37L\5\0\33\0\0\0ConditionTimestampMonotonic\0\1t\0\0\0\0\0\0/\312\305\177\3\0\0\0\17\0\0\0AssertTimestamp\0\1t\0\0\215i\234\352\37L\5\0\30\0\0\0AssertTimestampMonotonic\0\1t\0000\312\305\177\3\0\0\0\0\0\0\0l\4\1\1\7\1\0\0\357=\323\7\226\0\0\0\1\1o\0007\0\0\0/org/freedesktop/systemd1/unit/auto_5fhibernate_2etimer\0\2\1s\0\37\0\0\0org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\36\0\0\0org.freedesktop.systemd1.Timer\0\0\264\0\0\0\26\0\0\0NextElapseUSecRealtime\0\1t\0\0\0\377\377\377\377\377\377\377\377\27\0\0\0NextElapseUSecMonotonic\0\1t\0\0\377\377\377\377\377\377\377\377\17\0\0\0LastTriggerUSec\0\1t\0\0\311XU\\ L\5\0\30\0\0\0LastTriggerUSecMonotonic\0\1t\0l\271~\361\3\0\0\0\6\0\0\0Result\0\1s\0\0\0\7\0\0\0success\0'\0\0\0\17\0\0\0TimersMonotonic\0\16\0\0\0TimersCalendar\0l\4\1\1\34\3\0\0\360=\323", iov_len=2048}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 2048respectively. I also tried running strace on systemd-logind and got a similar number of calls to recvmsg of the form
recvmsg(12, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="/org/freedesktop/systemd1\0\0\0\0\0\0\0\2\1s\0 \0\0\0org.freedesktop.systemd1.Manager\0\0\0\0\0\0\0\0\3\1s\0\n\0\0\0JobRemoved\0\0\0\0\0\0\10\1g\0\4uoss\0\0\0\0\0\0\0\7\1s\0\4\0\0\0:1.1\0\0\0\0\207\202sR(\0\0\0/org/freedesktop/systemd1/job/1383301767\0\0\0\0\26\0\0\0auto_hibernate.service\0\0\4\0\0\0done\0", iov_len=225}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) = 225
recvmsg(12, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="/org/freedesktop/systemd1/unit/auto_5fhibernate_2etimer\0\2\1s\0\37\0\0\0org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\7\1s\0\4\0\0\0:1.1\0\0\0\0\36\0\0\0org.freedesktop.systemd1.Timer\0\0\264\0\0\0\26\0\0\0NextElapseUSecRealtime\0\1t\0\0\0\0\0\0\0\0\0\0\0\27\0\0\0NextElapseUSecMonotonic\0\1t\0\0\320\242\325\33\2\0\0\0\17\0\0\0LastTriggerUSec\0\1t\0\0\373}\321k L\5\0\30\0\0\0LastTriggerUSecMonotonic\0\1t\0\236\336\372\0\4\0\0\0\6\0\0\0Result\0\1s\0\0\0\7\0\0\0success\0'\0\0\0\17\0\0\0TimersMonotonic\0\16\0\0\0TimersCalendar\0", iov_len=423}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) = 423
recvmsg(12, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="/org/freedesktop/systemd1/unit/auto_5fhibernate_2etimer\0\2\1s\0\37\0\0\0org.freedesktop.DBus.Properties\0\3\1s\0\21\0\0\0PropertiesChanged\0\0\0\0\0\0\0\10\1g\0\10sa{sv}as\0\0\0\7\1s\0\4\0\0\0:1.1\0\0\0\0\35\0\0\0org.freedesktop.systemd1.Unit\0\0\0\360\2\0\0\v\0\0\0ActiveState\0\1s\0\0\6\0\0\0active\0\0\10\0\0\0SubState\0\1s\0\7\0\0\0waiting\0\0\0\0\0\24\0\0\0StateChangeTimestamp\0\1t\0\0\0\0\0\322~\321k L\5\0\35\0\0\0StateChangeTimestampMonotonic\0\1t\0\0\0\0t\337\372\0\4\0\0\0\25\0\0\0InactiveExitTimestamp\0\1t\0\0\0\0\224i\234\352\37L\5\0\36\0\0\0InactiveExitTimestampMonotonic\0\1t\0\0\0007\312\305\177\3\0\0\0\24\0\0\0ActiveEnterTimestamp\0\1t\0\0\0\0\0\224i\234\352\37L\5\0\35\0\0\0ActiveEnterTimestampMonotonic\0\1t\0\0\0\0007\312\305\177\3\0\0\0\23\0\0\0ActiveExitTimestamp\0\1t\0\0\0\0\0\0\4X\27\334\37L\5\0\34\0\0\0ActiveExitTimestampMonotonic\0\1t\0\0\0\0\0\247\270@q\3\0\0\0\26\0\0\0InactiveEnterTimestamp\0\1t\0\0\0\4X\27\334\37L\5\0\37\0\0\0InactiveEnterTimestampMonotonic\0\1t\0\0\247\270@q\3\0\0\0\3\0\0\0Job\0\4(uo)\0\0\0\0\0\0\0\1\0\0\0/\0\0\0\0\0\0\0\17\0\0\0ConditionResult\0\1b\0\0\1\0\0\0\0\0\0\0\f\0\0\0AssertResult\0\1b\0\1\0\0\0\22\0\0\0ConditionTimestamp\0\1t\0\0\0\0\0\0\0\215i\234\352\37L\5\0\33\0\0\0ConditionTimestampMonotonic\0\1t\0\0\0\0\0\0/\312\305\177\3\0\0\0\17\0\0\0AssertTimestamp\0\1t\0\0\215i\234\352\37L\5\0\30\0\0\0AssertTimestampMonotonic\0\1t\0000\312\305\177\3\0\0\0\0\0\0\0", iov_len=956}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) = 956It seems almost like a message is being sent every microsecond as long as the timer is active. When I stop the timer, these system calls stop and the CPU usage drops down to normal levels. How can I fix this problem?
Offline
Offline
My script is
#!/usr/bin/env bash
# Configuration.
BATTERY_PATH=/sys/class/power_supply/BAT0
CRITICAL_BATTERY_PERCENTAGE=5
# Calculate (the floor of) the battery percentage.
current_battery_level=$(< ${BATTERY_PATH}/energy_now)
max_battery_level=$(< ${BATTERY_PATH}/energy_full)
current_battery_percentage=$(((current_battery_level * 100)/max_battery_level))
if ((current_battery_percentage <= critical_battery_percentage)); then
logger 'Hibernating due to low battery.'
systemctl hibernate
fiand the service file that runs it is
[Unit]
Description=Check battery level and hibernate if low
ConditionACPower=false
[Service]
Type=oneshot
ExecStart=/usr/local/bin/hibernate-if-low-batteryOffline