Greetings,

On Sun, 28 Apr 2024 18:53:09 +0200,
"Nathaniel Griswold" <[email protected]> wrote:
> 
> Any ideas if it's remediable or where to start digging?
> 

I had near the same question sometime ago but on different machine, and I've
discovered a patch which I've inlinded into this email.

My laptop on last snapshot has consumption:

  hw.sensors.acpibat0.volt1=11.14 VDC (current voltage)
  hw.sensors.acpibat0.current0=0.97 A (rate)

and after this patch:

  hw.sensors.acpibat0.volt1=11.14 VDC (current voltage)
  hw.sensors.acpibat0.current0=0.52 A (rate)

anyway, it has a price tag: response of the system is a bit slower.

So, the Alternate cpu policy on battery patch.
    
Original author of this patch is Solene which she announced at
https://marc.info/?l=openbsd-tech&m=163259444331471&w=2
    
She also made benchmarks and some explanation:
https://dataswamp.org/%7Esolene/2021-09-26-openbsd-power-usage.html

Here the version which follows minor changes.

It requires to rebuild kernel, apm and apmd.

diff --git sys/kern/sched_bsd.c sys/kern/sched_bsd.c
index 25b221c1ee2..b1e5bd142c3 100644
--- sys/kern/sched_bsd.c
+++ sys/kern/sched_bsd.c
@@ -573,6 +573,7 @@ void (*cpu_setperf)(int);
 #define PERFPOL_MANUAL 0
 #define PERFPOL_AUTO 1
 #define PERFPOL_HIGH 2
+#define PERFPOL_POWERSAVING 4
 int perflevel = 100;
 int perfpolicy = PERFPOL_AUTO;
 
@@ -583,7 +584,9 @@ int perfpolicy = PERFPOL_AUTO;
 #include <sys/sysctl.h>
 
 void setperf_auto(void *);
+void setperf_powersaving(void *);
 struct timeout setperf_to = TIMEOUT_INITIALIZER(setperf_auto, NULL);
+struct timeout setperf_to_powersaving = 
TIMEOUT_INITIALIZER(setperf_powersaving, NULL);
 extern int hw_power;
 
 void
@@ -653,6 +656,76 @@ faster:
        timeout_add_msec(&setperf_to, 100);
 }
 
+void
+setperf_powersaving(void *v)
+{
+       static uint64_t *idleticks, *totalticks;
+       static int downbeats;
+       int i, j = 0;
+       int speedup = 0;
+       CPU_INFO_ITERATOR cii;
+       struct cpu_info *ci;
+       uint64_t idle, total, allidle = 0, alltotal = 0;
+
+       if (perfpolicy != PERFPOL_POWERSAVING)
+               return;
+
+       if (cpu_setperf == NULL)
+               return;
+
+       if (hw_power) {
+               speedup = 1;
+               goto faster;
+       }
+
+       if (!idleticks)
+               if (!(idleticks = mallocarray(ncpusfound, sizeof(*idleticks),
+                   M_DEVBUF, M_NOWAIT | M_ZERO)))
+                       return;
+       if (!totalticks)
+               if (!(totalticks = mallocarray(ncpusfound, sizeof(*totalticks),
+                   M_DEVBUF, M_NOWAIT | M_ZERO))) {
+                       free(idleticks, M_DEVBUF,
+                           sizeof(*idleticks) * ncpusfound);
+                       return;
+               }
+       CPU_INFO_FOREACH(cii, ci) {
+               if (!cpu_is_online(ci))
+                       continue;
+               total = 0;
+               for (i = 0; i < CPUSTATES; i++) {
+                       total += ci->ci_schedstate.spc_cp_time[i];
+               }
+               total -= totalticks[j];
+               idle = ci->ci_schedstate.spc_cp_time[CP_IDLE] - idleticks[j];
+               if (idle < total / 3)
+                       speedup = 1;
+               alltotal += total;
+               allidle += idle;
+               idleticks[j] += idle;
+               totalticks[j] += total;
+               j++;
+       }
+       if (allidle < alltotal / 3)
+               speedup = 1;
+       if (speedup)
+               /* twice as long here because we check every 200ms */
+               downbeats = 1;
+
+       if (speedup && perflevel != 100) {
+faster:
+               perflevel = 100;
+               cpu_setperf(perflevel);
+       } else if (!speedup && perflevel != 0 && --downbeats <= 0) {
+               perflevel = 0;
+               cpu_setperf(perflevel);
+       }
+
+       /* every 200ms to have a better resolution of the load */
+       timeout_add_msec(&setperf_to_powersaving, 200);
+}
+
+
 int
 sysctl_hwsetperf(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
 {
@@ -691,6 +764,9 @@ sysctl_hwperfpolicy(void *oldp, size_t *oldlenp, void 
*newp, size_t newlen)
        case PERFPOL_AUTO:
                strlcpy(policy, "auto", sizeof(policy));
                break;
+       case PERFPOL_POWERSAVING:
+               strlcpy(policy, "powersaving", sizeof(policy));
+               break;
        case PERFPOL_HIGH:
                strlcpy(policy, "high", sizeof(policy));
                break;
@@ -709,6 +785,8 @@ sysctl_hwperfpolicy(void *oldp, size_t *oldlenp, void 
*newp, size_t newlen)
                perfpolicy = PERFPOL_MANUAL;
        else if (strcmp(policy, "auto") == 0)
                perfpolicy = PERFPOL_AUTO;
+       else if (strcmp(policy, "powersaving") == 0)
+               perfpolicy = PERFPOL_POWERSAVING;
        else if (strcmp(policy, "high") == 0)
                perfpolicy = PERFPOL_HIGH;
        else
@@ -716,6 +794,8 @@ sysctl_hwperfpolicy(void *oldp, size_t *oldlenp, void 
*newp, size_t newlen)
 
        if (perfpolicy == PERFPOL_AUTO) {
                timeout_add_msec(&setperf_to, 200);
+       } else if (perfpolicy == PERFPOL_POWERSAVING) {
+               timeout_add_msec(&setperf_to_powersaving, 200);
        } else if (perfpolicy == PERFPOL_HIGH) {
                perflevel = 100;
                cpu_setperf(perflevel);
diff --git usr.sbin/apmd/apm-proto.h usr.sbin/apmd/apm-proto.h
index 867d0afbd70..166618e996f 100644
--- usr.sbin/apmd/apm-proto.h
+++ usr.sbin/apmd/apm-proto.h
@@ -38,6 +38,7 @@ enum apm_action {
        SETPERF_LOW,
        SETPERF_HIGH,
        SETPERF_AUTO,
+       SETPERF_POWERSAVING,
 };
 
 enum apm_state {
@@ -51,6 +52,7 @@ enum apm_perfmode {
        PERF_NONE = -1,
        PERF_MANUAL,
        PERF_AUTO,
+       PERF_POWERSAVING,
 };
 
 struct apm_command {
diff --git usr.sbin/apmd/apmd.c usr.sbin/apmd/apmd.c
index f29d5c9a081..cc2e026dc11 100644
--- usr.sbin/apmd/apmd.c
+++ usr.sbin/apmd/apmd.c
@@ -139,6 +139,10 @@ power_status(int fd, int force, struct apm_power_info 
*pinfo)
        static struct apm_power_info last;
        int acon = 0, priority = LOG_NOTICE;
 
+       int perfpol_mib[] = { CTL_HW, HW_PERFPOLICY };
+       char perfpol[32];
+       size_t perfpol_sz = sizeof(perfpol);
+
        if (fd == -1) {
                if (pinfo) {
                        bstate.battery_state = 255;
@@ -151,11 +155,18 @@ power_status(int fd, int force, struct apm_power_info 
*pinfo)
                return 0;
        }
 
+       if (sysctl(perfpol_mib, 2, perfpol, &perfpol_sz, NULL, 0) == -1)
+               logmsg(LOG_INFO, "cannot read hw.perfpolicy");
+
        if (ioctl(fd, APM_IOC_GETPOWER, &bstate) == 0) {
        /* various conditions under which we report status:  something changed
         * enough since last report, or asked to force a print */
-               if (bstate.ac_state == APM_AC_ON)
+               if (bstate.ac_state == APM_AC_ON) {
                        acon = 1;
+                       if(strcmp(perfpol, "powersaving") == 0) 
setperfpolicy("auto");
+               } else
+                       if(strcmp(perfpol, "auto") == 0) 
setperfpolicy("powersaving");
+
                if (bstate.battery_state == APM_BATT_CRITICAL &&
                    bstate.battery_state != last.battery_state)
                        priority = LOG_EMERG;
@@ -282,6 +293,11 @@ handle_client(int sock_fd, int ctl_fd)
                logmsg(LOG_NOTICE, "setting hw.perfpolicy to high");
                setperfpolicy("high");
                break;
+       case SETPERF_POWERSAVING:
+               reply.newstate = NORMAL;
+               logmsg(LOG_NOTICE, "setting hw.perfpolicy to powersaving");
+               setperfpolicy("powersaving");
+               break;
        case SETPERF_AUTO:
                reply.newstate = NORMAL;
                logmsg(LOG_NOTICE, "setting hw.perfpolicy to auto");
@@ -299,8 +315,10 @@ handle_client(int sock_fd, int ctl_fd)
                if (strcmp(perfpol, "manual") == 0 ||
                    strcmp(perfpol, "high") == 0) {
                        reply.perfmode = PERF_MANUAL;
-               } else if (strcmp(perfpol, "auto") == 0)
+               } else if (strcmp(perfpol, "auto") == 0) {
                        reply.perfmode = PERF_AUTO;
+               } else if (strcmp(perfpol, "powersaving") == 0)
+                       reply.perfmode = PERF_POWERSAVING;
        }
 
        if (sysctl(cpuspeed_mib, 2, &cpuspeed, &cpuspeed_sz, NULL, 0) == -1) {
diff --git usr.sbin/apmd/apmsubr.c usr.sbin/apmd/apmsubr.c
index 6504fe823bb..a1dbcfb2c61 100644
--- usr.sbin/apmd/apmsubr.c
+++ usr.sbin/apmd/apmsubr.c
@@ -79,6 +79,8 @@ perf_mode(int mode)
                return "manual";
        case PERF_AUTO:
                return "auto";
+       case PERF_POWERSAVING:
+               return "powersaving";
        default:
                return "invalid";
        }


-- 
wbr, Kirill

Reply via email to