2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Colin Percival
5 * Copyright (c) 2005 Nate Lawson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted providing 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 the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/sysctl.h>
36 #include <sys/resource.h>
37 #include <sys/socket.h>
57 #include <machine/apm_bios.h>
60 #define DEFAULT_ACTIVE_PERCENT 75
61 #define DEFAULT_IDLE_PERCENT 50
62 #define DEFAULT_POLL_INTERVAL 250 /* Poll interval in milliseconds */
77 static const char *modes[] = {
83 #define ACPIAC "hw.acpi.acline"
84 #define PMUAC "dev.pmu.0.acline"
85 #define APMDEV "/dev/apm"
86 #define DEVDPIPE "/var/run/devd.pipe"
87 #define DEVCTL_MAXBUF 1024
89 static int read_usage_times(int *load, int nonice);
90 static int read_freqs(int *numfreqs, int **freqs, int **power,
91 int minfreq, int maxfreq);
92 static int set_freq(int freq);
93 static void acline_init(void);
94 static void acline_read(void);
95 static int devd_init(void);
96 static void devd_close(void);
97 static void handle_sigs(int sig);
98 static void parse_mode(char *arg, int *mode, int ch);
99 static void usage(void);
101 /* Sysctl data structures. */
102 static int cp_times_mib[2];
103 static int freq_mib[4];
104 static int levels_mib[4];
105 static int acline_mib[4];
106 static size_t acline_mib_len;
109 static int cpu_running_mark;
110 static int cpu_idle_mark;
111 static int poll_ival;
114 static volatile sig_atomic_t exit_requested;
115 static power_src_t acline_status;
124 static acline_mode_t acline_mode;
125 static acline_mode_t acline_mode_user = ac_none;
127 static int apm_fd = -1;
129 static int devd_pipe = -1;
131 #define DEVD_RETRY_INTERVAL 60 /* seconds */
132 static struct timeval tried_devd;
135 * This function returns summary load of all CPUs. It was made so
136 * intentionally to not reduce performance in scenarios when several
137 * threads are processing requests as a pipeline -- running one at
138 * a time on different CPUs and waiting for each other. If nonice
139 * is nonzero, only user+sys+intr time will be counted as load; any
140 * nice time will be treated as if idle.
143 read_usage_times(int *load, int nonice)
145 static long *cp_times = NULL, *cp_times_old = NULL;
146 static int ncpus = 0;
148 int error, cpu, i, total, excl;
150 if (cp_times == NULL) {
152 error = sysctl(cp_times_mib, 2, NULL, &cp_times_len, NULL, 0);
155 if ((cp_times = malloc(cp_times_len)) == NULL)
157 if ((cp_times_old = malloc(cp_times_len)) == NULL) {
162 ncpus = cp_times_len / (sizeof(long) * CPUSTATES);
165 cp_times_len = sizeof(long) * CPUSTATES * ncpus;
166 error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
172 for (cpu = 0; cpu < ncpus; cpu++) {
174 for (i = 0; i < CPUSTATES; i++) {
175 total += cp_times[cpu * CPUSTATES + i] -
176 cp_times_old[cpu * CPUSTATES + i];
180 excl = cp_times[cpu * CPUSTATES + CP_IDLE] -
181 cp_times_old[cpu * CPUSTATES + CP_IDLE];
183 excl += cp_times[cpu * CPUSTATES + CP_NICE] -
184 cp_times_old[cpu * CPUSTATES + CP_NICE];
185 *load += 100 - excl * 100 / total;
189 memcpy(cp_times_old, cp_times, cp_times_len);
195 read_freqs(int *numfreqs, int **freqs, int **power, int minfreq, int maxfreq)
197 char *freqstr, *p, *q;
201 if (sysctl(levels_mib, 4, NULL, &len, NULL, 0))
203 if ((freqstr = malloc(len)) == NULL)
205 if (sysctl(levels_mib, 4, freqstr, &len, NULL, 0)) {
211 for (p = freqstr; *p != '\0'; p++)
215 if ((*freqs = malloc(*numfreqs * sizeof(int))) == NULL) {
219 if ((*power = malloc(*numfreqs * sizeof(int))) == NULL) {
224 for (i = 0, j = 0, p = freqstr; i < *numfreqs; i++) {
228 if (sscanf(p, "%d/%d", &(*freqs)[j], &(*power)[i]) != 2) {
234 if (((*freqs)[j] >= minfreq || minfreq == -1) &&
235 ((*freqs)[j] <= maxfreq || maxfreq == -1))
241 if ((*freqs = realloc(*freqs, *numfreqs * sizeof(int))) == NULL) {
258 len = sizeof(curfreq);
259 if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) {
261 warn("error reading current CPU frequency");
271 if (sysctl(freq_mib, 4, NULL, NULL, &freq, sizeof(freq))) {
280 get_freq_id(int freq, int *freqs, int numfreqs)
284 while (i < numfreqs) {
293 * Try to use ACPI to find the AC line status. If this fails, fall back
294 * to APM. If nothing succeeds, we'll just run in default mode.
299 int skip_source_check;
302 acline_status = SRC_UNKNOWN;
303 skip_source_check = (acline_mode_user == ac_none ||
304 acline_mode_user == ac_acpi_devd);
306 if ((skip_source_check || acline_mode_user == ac_sysctl) &&
307 sysctlnametomib(ACPIAC, acline_mib, &acline_mib_len) == 0) {
308 acline_mode = ac_sysctl;
310 warnx("using sysctl for AC line status");
312 } else if ((skip_source_check || acline_mode_user == ac_sysctl) &&
313 sysctlnametomib(PMUAC, acline_mib, &acline_mib_len) == 0) {
314 acline_mode = ac_sysctl;
316 warnx("using sysctl for AC line status");
319 } else if ((skip_source_check || acline_mode_user == ac_apm) &&
320 (apm_fd = open(APMDEV, O_RDONLY)) >= 0) {
322 warnx("using APM for AC line status");
323 acline_mode = ac_apm;
326 warnx("unable to determine AC line status");
327 acline_mode = ac_none;
334 if (acline_mode == ac_acpi_devd) {
335 char buf[DEVCTL_MAXBUF], *ptr;
339 rlen = read(devd_pipe, buf, sizeof(buf));
340 if (rlen == 0 || (rlen < 0 && errno != EWOULDBLOCK)) {
342 warnx("lost devd connection, switching to sysctl");
344 acline_mode = ac_sysctl;
348 (ptr = strstr(buf, "system=ACPI")) != NULL &&
349 (ptr = strstr(ptr, "subsystem=ACAD")) != NULL &&
350 (ptr = strstr(ptr, "notify=")) != NULL &&
351 sscanf(ptr, "notify=%x", ¬ify) == 1)
352 acline_status = (notify ? SRC_AC : SRC_BATTERY);
354 if (acline_mode == ac_sysctl) {
358 len = sizeof(acline);
359 if (sysctl(acline_mib, acline_mib_len, &acline, &len,
361 acline_status = (acline ? SRC_AC : SRC_BATTERY);
363 acline_status = SRC_UNKNOWN;
366 if (acline_mode == ac_apm) {
367 struct apm_info info;
369 if (ioctl(apm_fd, APMIO_GETINFO, &info) == 0) {
370 acline_status = (info.ai_acline ? SRC_AC : SRC_BATTERY);
374 acline_mode = ac_none;
375 acline_status = SRC_UNKNOWN;
379 /* try to (re)connect to devd */
381 if ((acline_mode == ac_sysctl &&
382 (acline_mode_user == ac_none ||
383 acline_mode_user == ac_acpi_devd)) ||
384 (acline_mode == ac_apm &&
385 acline_mode_user == ac_acpi_devd)) {
387 if (acline_mode == ac_sysctl &&
388 (acline_mode_user == ac_none ||
389 acline_mode_user == ac_acpi_devd)) {
393 gettimeofday(&now, NULL);
394 if (now.tv_sec > tried_devd.tv_sec + DEVD_RETRY_INTERVAL) {
395 if (devd_init() >= 0) {
397 warnx("using devd for AC line status");
398 acline_mode = ac_acpi_devd;
408 struct sockaddr_un devd_addr;
410 bzero(&devd_addr, sizeof(devd_addr));
411 if ((devd_pipe = socket(PF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0)) < 0) {
413 warn("%s(): socket()", __func__);
417 devd_addr.sun_family = PF_LOCAL;
418 strlcpy(devd_addr.sun_path, DEVDPIPE, sizeof(devd_addr.sun_path));
419 if (connect(devd_pipe, (struct sockaddr *)&devd_addr,
420 sizeof(devd_addr)) == -1) {
422 warn("%s(): connect()", __func__);
440 parse_mode(char *arg, int *mode, int ch)
443 if (strcmp(arg, "minimum") == 0 || strcmp(arg, "min") == 0)
445 else if (strcmp(arg, "maximum") == 0 || strcmp(arg, "max") == 0)
447 else if (strcmp(arg, "adaptive") == 0 || strcmp(arg, "adp") == 0)
448 *mode = MODE_ADAPTIVE;
449 else if (strcmp(arg, "hiadaptive") == 0 || strcmp(arg, "hadp") == 0)
450 *mode = MODE_HIADAPTIVE;
452 errx(1, "bad option: -%c %s", (char)ch, optarg);
456 parse_acline_mode(char *arg, int ch)
458 if (strcmp(arg, "sysctl") == 0)
459 acline_mode_user = ac_sysctl;
460 else if (strcmp(arg, "devd") == 0)
461 acline_mode_user = ac_acpi_devd;
463 else if (strcmp(arg, "apm") == 0)
464 acline_mode_user = ac_apm;
467 errx(1, "bad option: -%c %s", (char)ch, optarg);
471 handle_sigs(int __unused sig)
482 "usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-m freq] [-M freq] [-N] [-n mode] [-p ival] [-r %%] [-s source] [-P pidfile]\n");
487 main(int argc, char * argv[])
489 struct timeval timeout;
492 struct pidfh *pfh = NULL;
493 const char *pidfile = NULL;
494 int freq, curfreq, initfreq, *freqs, i, j, *mwatts, numfreqs, load;
495 int minfreq = -1, maxfreq = -1;
496 int ch, mode, mode_ac, mode_battery, mode_none, idle, to;
497 uint64_t mjoules_used;
501 /* Default mode for all AC states is adaptive. */
502 mode_ac = mode_none = MODE_HIADAPTIVE;
503 mode_battery = MODE_ADAPTIVE;
504 cpu_running_mark = DEFAULT_ACTIVE_PERCENT;
505 cpu_idle_mark = DEFAULT_IDLE_PERCENT;
506 poll_ival = DEFAULT_POLL_INTERVAL;
511 /* User must be root to control frequencies. */
513 errx(1, "must be root to run");
515 while ((ch = getopt(argc, argv, "a:b:i:m:M:Nn:p:P:r:s:v")) != -1)
518 parse_mode(optarg, &mode_ac, ch);
521 parse_mode(optarg, &mode_battery, ch);
524 parse_acline_mode(optarg, ch);
527 cpu_idle_mark = atoi(optarg);
528 if (cpu_idle_mark < 0 || cpu_idle_mark > 100) {
529 warnx("%d is not a valid percent",
535 minfreq = atoi(optarg);
537 warnx("%d is not a valid CPU frequency",
543 maxfreq = atoi(optarg);
545 warnx("%d is not a valid CPU frequency",
554 parse_mode(optarg, &mode_none, ch);
557 poll_ival = atoi(optarg);
559 warnx("poll interval is in units of ms");
567 cpu_running_mark = atoi(optarg);
568 if (cpu_running_mark <= 0 || cpu_running_mark > 100) {
569 warnx("%d is not a valid percent",
583 /* Poll interval is in units of ms. */
586 /* Look up various sysctl MIBs. */
588 if (sysctlnametomib("kern.cp_times", cp_times_mib, &len))
589 err(1, "lookup kern.cp_times");
591 if (sysctlnametomib("dev.cpu.0.freq", freq_mib, &len))
592 err(EX_UNAVAILABLE, "no cpufreq(4) support -- aborting");
594 if (sysctlnametomib("dev.cpu.0.freq_levels", levels_mib, &len))
595 err(1, "lookup freq_levels");
597 /* Check if we can read the load and supported freqs. */
598 if (read_usage_times(NULL, nonice))
599 err(1, "read_usage_times");
600 if (read_freqs(&numfreqs, &freqs, &mwatts, minfreq, maxfreq))
601 err(1, "error reading supported CPU frequencies");
603 errx(1, "no CPU frequencies in user-specified range");
605 /* Run in the background unless in verbose mode. */
609 pfh = pidfile_open(pidfile, 0600, &otherpid);
611 if (errno == EEXIST) {
612 errx(1, "powerd already running, pid: %d",
615 warn("cannot open pid file");
617 if (daemon(0, 0) != 0) {
618 warn("cannot enter daemon mode, exiting");
626 /* Decide whether to use ACPI or APM to read the AC line status. */
630 * Exit cleanly on signals.
632 signal(SIGINT, handle_sigs);
633 signal(SIGTERM, handle_sigs);
635 freq = initfreq = curfreq = get_freq();
636 i = get_freq_id(curfreq, freqs, numfreqs);
641 * If we are in adaptive mode and the current frequency is outside the
642 * user-defined range, adjust it to be within the user-defined range.
645 if (acline_status > SRC_UNKNOWN)
646 errx(1, "invalid AC line status %d", acline_status);
647 if ((acline_status == SRC_AC &&
648 (mode_ac == MODE_ADAPTIVE || mode_ac == MODE_HIADAPTIVE)) ||
649 (acline_status == SRC_BATTERY &&
650 (mode_battery == MODE_ADAPTIVE || mode_battery == MODE_HIADAPTIVE)) ||
651 (acline_status == SRC_UNKNOWN &&
652 (mode_none == MODE_ADAPTIVE || mode_none == MODE_HIADAPTIVE))) {
653 /* Read the current frequency. */
654 len = sizeof(curfreq);
655 if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) {
657 warn("error reading current CPU frequency");
659 if (curfreq < freqs[numfreqs - 1]) {
661 printf("CPU frequency is below user-defined "
662 "minimum; changing frequency to %d "
663 "MHz\n", freqs[numfreqs - 1]);
665 if (set_freq(freqs[numfreqs - 1]) != 0) {
666 warn("error setting CPU freq %d",
667 freqs[numfreqs - 1]);
669 } else if (curfreq > freqs[0]) {
671 printf("CPU frequency is above user-defined "
672 "maximum; changing frequency to %d "
675 if (set_freq(freqs[0]) != 0) {
676 warn("error setting CPU freq %d",
686 if (devd_pipe >= 0) {
687 FD_SET(devd_pipe, &fdset);
688 nfds = devd_pipe + 1;
692 if (mode == MODE_HIADAPTIVE || idle < 120)
698 timeout.tv_sec = to / 1000000;
699 timeout.tv_usec = to % 1000000;
700 select(nfds, &fdset, NULL, &fdset, &timeout);
702 /* If the user requested we quit, print some statistics. */
703 if (exit_requested) {
704 if (vflag && mjoules_used != 0)
705 printf("total joules used: %u.%03u\n",
706 (u_int)(mjoules_used / 1000),
707 (int)mjoules_used % 1000);
711 /* Read the current AC status and record the mode. */
713 switch (acline_status) {
724 errx(1, "invalid AC line status %d", acline_status);
727 /* Read the current frequency. */
728 if (idle % 32 == 0) {
729 if ((curfreq = get_freq()) == 0)
731 i = get_freq_id(curfreq, freqs, numfreqs);
735 /* Keep a sum of all power actually used. */
738 (mwatts[i] * (poll_ival / 1000)) / 1000;
741 /* Always switch to the lowest frequency in min mode. */
742 if (mode == MODE_MIN) {
743 freq = freqs[numfreqs - 1];
744 if (curfreq != freq) {
746 printf("now operating on %s power; "
747 "changing frequency to %d MHz\n",
748 modes[acline_status], freq);
751 if (set_freq(freq) != 0) {
752 warn("error setting CPU freq %d",
760 /* Always switch to the highest frequency in max mode. */
761 if (mode == MODE_MAX) {
763 if (curfreq != freq) {
765 printf("now operating on %s power; "
766 "changing frequency to %d MHz\n",
767 modes[acline_status], freq);
770 if (set_freq(freq) != 0) {
771 warn("error setting CPU freq %d",
779 /* Adaptive mode; get the current CPU usage times. */
780 if (read_usage_times(&load, nonice)) {
782 warn("read_usage_times() failed");
786 if (mode == MODE_ADAPTIVE) {
787 if (load > cpu_running_mark) {
788 if (load > 95 || load > cpu_running_mark * 2)
791 freq = freq * load / cpu_running_mark;
794 } else if (load < cpu_idle_mark &&
795 curfreq * load < freqs[get_freq_id(
796 freq * 7 / 8, freqs, numfreqs)] *
799 if (freq < freqs[numfreqs - 1])
800 freq = freqs[numfreqs - 1];
802 } else { /* MODE_HIADAPTIVE */
803 if (load > cpu_running_mark / 2) {
804 if (load > 95 || load > cpu_running_mark)
807 freq = freq * load * 2 / cpu_running_mark;
808 if (freq > freqs[0] * 2)
810 } else if (load < cpu_idle_mark / 2 &&
811 curfreq * load < freqs[get_freq_id(
812 freq * 31 / 32, freqs, numfreqs)] *
813 cpu_running_mark / 2) {
814 freq = freq * 31 / 32;
815 if (freq < freqs[numfreqs - 1])
816 freq = freqs[numfreqs - 1];
820 printf("load %3d%%, current freq %4d MHz (%2d), wanted freq %4d MHz\n",
821 load, curfreq, i, freq);
823 j = get_freq_id(freq, freqs, numfreqs);
826 printf("changing clock"
827 " speed from %d MHz to %d MHz\n",
831 if (set_freq(freqs[j]))
832 warn("error setting CPU frequency %d",
836 if (set_freq(initfreq))
837 warn("error setting CPU frequency %d", initfreq);