2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * The powerd daemon monitors the cpu load and adjusts cpu frequencies
37 * via hw.acpi.cpu.px_dom*.
40 #define _KERNEL_STRUCTURES
41 #include <sys/types.h>
42 #include <sys/sysctl.h>
43 #include <sys/kinfo.h>
45 #include <sys/soundcard.h>
56 static void usage(void);
57 static double getcputime(double);
58 static void acpi_setcpufreq(int nstate);
59 static void setupdominfo(void);
60 static int has_battery(void);
61 static int mon_battery(void);
62 static void getncpus(void);
67 int CpuLimit; /* # of cpus at max frequency */
68 int DomLimit; /* # of domains at max frequency */
73 int CpuCount[256]; /* # of cpus in any given domain */
74 int CpuToDom[256]; /* domain a particular cpu belongs to */
75 int Hysteresis = 10; /* percentage */
76 double TriggerUp = 0.25;/* single-cpu load to force max freq */
77 double TriggerDown; /* load per cpu to force the min freq */
78 static int BatLifeMin = 2; /* shutdown the box, if low on battery life */
79 static struct timespec BatLifePrevT;
80 static int BatLifePollIntvl = 5; /* unit: sec */
82 static struct timespec BatShutdownStartT;
83 static int BatShutdownLinger = -1;
84 static int BatShutdownLingerSet = 60; /* unit: sec */
85 static int BatShutdownLingerCnt;
86 static int BatShutdownAudioAlert = 1;
88 static void sigintr(int signo);
91 main(int ac, char **av)
94 double uavg; /* uavg - used for speeding up */
95 double davg; /* davg - used for slowing down */
105 srt = 8.0; /* time for samples - 8 seconds */
106 pollrate = 1.0; /* polling rate in seconds */
108 while ((ch = getopt(ac, av, "dp:r:tu:B:L:P:QT:")) != -1) {
114 Hysteresis = (int)strtol(optarg, NULL, 10);
117 pollrate = strtod(optarg, NULL);
123 TriggerUp = (double)strtol(optarg, NULL, 10) / 100;
126 BatLifeMin = strtol(optarg, NULL, 10);
129 BatShutdownLingerSet = strtol(optarg, NULL, 10);
130 if (BatShutdownLingerSet < 0)
131 BatShutdownLingerSet = 0;
134 BatLifePollIntvl = strtol(optarg, NULL, 10);
137 BatShutdownAudioAlert = 0;
140 srt = strtod(optarg, NULL);
150 /* Get the number of cpus */
153 if (0 > Hysteresis || Hysteresis > 99) {
154 fprintf(stderr, "Invalid hysteresis value\n");
158 if (0 > TriggerUp || TriggerUp > 1) {
159 fprintf(stderr, "Invalid load limit value\n");
163 TriggerDown = TriggerUp - (TriggerUp * (double) Hysteresis / 100);
166 * Make sure powerd is not already running.
168 PowerFd = open("/var/run/powerd.pid", O_CREAT|O_RDWR, 0644);
171 "Cannot create /var/run/powerd.pid, "
172 "continuing anyway\n");
174 if (flock(PowerFd, LOCK_EX|LOCK_NB) < 0) {
175 fprintf(stderr, "powerd is already running\n");
181 * Demonize and set pid
185 openlog("powerd", LOG_CONS | LOG_PID, LOG_DAEMON);
189 ftruncate(PowerFd, 0);
190 snprintf(buf, sizeof(buf), "%d\n", (int)getpid());
191 write(PowerFd, buf, strlen(buf));
194 /* Do we need to monitor battery life? */
195 if (BatLifePollIntvl <= 0)
198 monbat = has_battery();
201 * Wait hw.acpi.cpu.px_dom* sysctl to be created by kernel
203 * Since hw.acpi.cpu.px_dom* creation is queued into ACPI
204 * taskqueue and ACPI taskqueue is shared across various
205 * ACPI modules, any delay in other modules may cause
206 * hw.acpi.cpu.px_dom* to be created at quite a later time
207 * (e.g. cmbat module's task could take quite a lot of time).
211 * Prime delta cputime calculation, make sure at least
214 getcputime(pollrate);
217 if (DomBeg >= DomEnd) {
218 usleep((int)(pollrate * 1000000.0));
228 * Set to maximum performance if killed.
230 signal(SIGINT, sigintr);
231 signal(SIGTERM, sigintr);
235 srt = srt / pollrate; /* convert to sample count */
238 printf("samples for downgrading: %5.2f\n", srt);
243 * Calculate nstate, the number of cpus we wish to run at max
244 * frequency. All remaining cpus will be set to their lowest
245 * frequency and mapped out of the user process scheduler.
248 qavg = getcputime(pollrate);
249 uavg = (uavg * 2.0 + qavg) / 3.0; /* speeding up */
250 davg = (davg * srt + qavg) / (srt + 1); /* slowing down */
254 ustate = uavg / TriggerUp;
255 if (ustate < CpuLimit)
256 ustate = uavg / TriggerDown;
257 dstate = davg / TriggerUp;
258 if (dstate < CpuLimit)
259 dstate = davg / TriggerDown;
261 nstate = (ustate > dstate) ? ustate : dstate;
266 printf("\rqavg=%5.2f uavg=%5.2f davg=%5.2f "
267 "%2d/%2d ncpus=%d\r",
269 CpuLimit, DomLimit, nstate);
272 if (nstate != CpuLimit)
273 acpi_setcpufreq(nstate);
275 monbat = mon_battery();
276 usleep((int)(pollrate * 1000000.0));
282 sigintr(int signo __unused)
284 syslog(LOG_INFO, "killed, setting max and exiting");
285 acpi_setcpufreq(NCpus);
290 * Figure out the domains and calculate the CpuCount[] and CpuToDom[]
304 for (i = 0; i < 256; ++i) {
305 snprintf(buf, sizeof(buf),
306 "hw.acpi.cpu.px_dom%d.available", i);
307 if (sysctlbyname(buf, NULL, NULL, NULL, 0) >= 0)
312 for (i = 255; i >= DomBeg; --i) {
313 snprintf(buf, sizeof(buf),
314 "hw.acpi.cpu.px_dom%d.available", i);
315 if (sysctlbyname(buf, NULL, NULL, NULL, 0) >= 0) {
322 for (i = DomBeg; i < DomEnd; ++i) {
323 snprintf(buf, sizeof(buf),
324 "hw.acpi.cpu.px_dom%d.members", i);
325 msize = sizeof(members);
326 if (sysctlbyname(buf, members, &msize, NULL, 0) == 0) {
328 for (str = strtok(members, " "); str;
329 str = strtok(NULL, " ")) {
331 sscanf(str, "cpu%d", &n);
343 * Return the one-second cpu load. One cpu at 100% will return a value
344 * of 1.0. On a SMP system N cpus running at 100% will return a value of N.
348 getcputime(double pollrate)
350 static struct kinfo_cputime ocpu_time[MAXCPU];
351 static struct kinfo_cputime ncpu_time[MAXCPU];
357 bcopy(ncpu_time, ocpu_time, sizeof(struct kinfo_cputime) * TotalCpus);
358 slen = sizeof(ncpu_time);
359 if (sysctlbyname("kern.cputime", &ncpu_time, &slen, NULL, 0) < 0) {
360 fprintf(stderr, "kern.cputime sysctl not available\n");
363 ncpu = slen / sizeof(ncpu_time[0]);
366 for (cpu = 0; cpu < ncpu; ++cpu) {
367 delta += (ncpu_time[cpu].cp_user + ncpu_time[cpu].cp_sys +
368 ncpu_time[cpu].cp_nice + ncpu_time[cpu].cp_intr) -
369 (ocpu_time[cpu].cp_user + ocpu_time[cpu].cp_sys +
370 ocpu_time[cpu].cp_nice + ocpu_time[cpu].cp_intr);
372 return((double)delta / (pollrate * 1000000.0));
376 * nstate is the requested number of cpus that we wish to run at full
377 * frequency. We calculate how many domains we have to adjust to reach
380 * This function also sets the user scheduler global cpu mask.
384 acpi_setcpufreq(int nstate)
387 int increasing = (nstate > CpuLimit);
399 cpumask_t global_cpumask;
402 * Calculate the ending domain if the number of operating cpus
405 * Calculate the starting domain if the number of operating cpus
408 for (dom = DomBeg; dom < DomEnd; ++dom) {
411 ncpus += CpuCount[dom];
414 syslog(LOG_INFO, "using %d cpus", nstate);
417 * Set the mask of cpus the userland scheduler is allowed to use.
419 CPUMASK_ASSBMASK(global_cpumask, nstate == 0 ? 1 : nstate);
420 sysctlbyname("kern.usched_global_cpumask", NULL, 0,
421 &global_cpumask, sizeof(global_cpumask));
434 * Adjust the cpu frequency
438 for (dom = domBeg; dom < domEnd; ++dom) {
440 * Retrieve availability list
442 asprintf(&sysid, "hw.acpi.cpu.px_dom%d.available", dom);
443 buflen = sizeof(buf) - 1;
444 v = sysctlbyname(sysid, buf, &buflen, NULL, 0);
451 * Parse out the highest and lowest cpu frequencies
454 highest = lowest = 0;
455 while (ptr && (v = strtol(ptr, &ptr, 10)) > 0) {
456 if (lowest == 0 || lowest > v)
458 if (highest == 0 || highest < v)
463 if ((highest - v == 1) && ! TurboOpt)
469 * Calculate the desired cpu frequency, test, and set.
471 desired = increasing ? highest : lowest;
473 asprintf(&sysid, "hw.acpi.cpu.px_dom%d.select", dom);
476 sysctlbyname(sysid, &v, &buflen, NULL, 0);
479 printf("dom%d set frequency %d\n",
482 sysctlbyname(sysid, NULL, NULL,
483 &desired, sizeof(desired));
493 fprintf(stderr, "usage: powerd [-dt] [-p hysteresis] "
494 "[-u trigger_up] [-T sample_interval] [-r poll_interval] "
495 "[-B min_battery_life] [-L low_battery_linger] "
496 "[-P battery_poll_interval] [-Q]\n");
501 #define timespecsub(vvp, uvp) \
503 (vvp)->tv_sec -= (uvp)->tv_sec; \
504 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
505 if ((vvp)->tv_nsec < 0) { \
507 (vvp)->tv_nsec += 1000000000; \
512 #define BAT_SYSCTL_TIME_MAX 50000000 /* unit: nanosecond */
517 struct timespec s, e;
521 clock_gettime(CLOCK_MONOTONIC_FAST, &s);
525 if (sysctlbyname("hw.acpi.acline", &val, &len, NULL, 0) < 0) {
526 /* No AC line information */
529 clock_gettime(CLOCK_MONOTONIC_FAST, &e);
532 if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
533 /* hw.acpi.acline takes to long to be useful */
534 syslog(LOG_NOTICE, "hw.acpi.acline takes too long");
538 clock_gettime(CLOCK_MONOTONIC_FAST, &s);
540 if (sysctlbyname("hw.acpi.battery.life", &val, &len, NULL, 0) < 0) {
541 /* No battery life */
544 clock_gettime(CLOCK_MONOTONIC_FAST, &e);
547 if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
548 /* hw.acpi.battery.life takes to long to be useful */
549 syslog(LOG_NOTICE, "hw.acpi.battery.life takes too long");
556 low_battery_alert(int life)
558 int fmt, stereo, freq;
561 syslog(LOG_ALERT, "low battery life %d%%, please plugin AC line, #%d",
562 life, BatShutdownLingerCnt);
563 ++BatShutdownLingerCnt;
565 if (!BatShutdownAudioAlert)
568 fd = open("/dev/dsp", O_WRONLY);
573 if (ioctl(fd, SNDCTL_DSP_SETFMT, &fmt, sizeof(fmt)) < 0)
577 if (ioctl(fd, SNDCTL_DSP_STEREO, &stereo, sizeof(stereo)) < 0)
581 if (ioctl(fd, SNDCTL_DSP_SPEED, &freq, sizeof(freq)) < 0)
584 write(fd, alert1, sizeof(alert1));
585 write(fd, alert1, sizeof(alert1));
594 struct timespec cur, ts;
598 clock_gettime(CLOCK_MONOTONIC_FAST, &cur);
600 timespecsub(&ts, &BatLifePrevT);
601 if (ts.tv_sec < BatLifePollIntvl)
605 len = sizeof(acline);
606 if (sysctlbyname("hw.acpi.acline", &acline, &len, NULL, 0) < 0)
609 BatShutdownLinger = -1;
610 BatShutdownLingerCnt = 0;
615 if (sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0) < 0)
618 if (BatShutdownLinger > 0) {
620 timespecsub(&ts, &BatShutdownStartT);
621 if (ts.tv_sec > BatShutdownLinger)
622 BatShutdownLinger = 0;
625 if (life <= BatLifeMin) {
626 if (BatShutdownLinger == 0 || BatShutdownLingerSet == 0) {
627 syslog(LOG_ALERT, "low battery life %d%%, "
628 "shutting down", life);
630 execlp("poweroff", "poweroff", NULL);
632 } else if (BatShutdownLinger < 0) {
633 BatShutdownLinger = BatShutdownLingerSet;
634 BatShutdownStartT = cur;
636 low_battery_alert(life);
646 slen = sizeof(TotalCpus);
647 if (sysctlbyname("hw.ncpu", &TotalCpus, &slen, NULL, 0) < 0)
648 err(1, "sysctlbyname hw.ncpu failed");
650 printf("hw.ncpu %d\n", TotalCpus);