2 * Copyright (c) 2004 Colin Percival
3 * Copyright (c) 2005 Nate Lawson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted providing that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/sysctl.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
54 #include <machine/apm_bios.h>
57 #define DEFAULT_ACTIVE_PERCENT 75
58 #define DEFAULT_IDLE_PERCENT 50
59 #define DEFAULT_POLL_INTERVAL 250 /* Poll interval in milliseconds */
74 const char *modes[] = {
80 #define ACPIAC "hw.acpi.acline"
81 #define PMUAC "dev.pmu.0.acline"
82 #define APMDEV "/dev/apm"
83 #define DEVDPIPE "/var/run/devd.pipe"
84 #define DEVCTL_MAXBUF 1024
86 static int read_usage_times(int *load);
87 static int read_freqs(int *numfreqs, int **freqs, int **power,
88 int minfreq, int maxfreq);
89 static int set_freq(int freq);
90 static void acline_init(void);
91 static void acline_read(void);
92 static int devd_init(void);
93 static void devd_close(void);
94 static void handle_sigs(int sig);
95 static void parse_mode(char *arg, int *mode, int ch);
96 static void usage(void);
98 /* Sysctl data structures. */
99 static int cp_times_mib[2];
100 static int freq_mib[4];
101 static int levels_mib[4];
102 static int acline_mib[4];
103 static size_t acline_mib_len;
106 static int cpu_running_mark;
107 static int cpu_idle_mark;
108 static int poll_ival;
111 static volatile sig_atomic_t exit_requested;
112 static power_src_t acline_status;
122 static int apm_fd = -1;
124 static int devd_pipe = -1;
126 #define DEVD_RETRY_INTERVAL 60 /* seconds */
127 static struct timeval tried_devd;
130 * This function returns summary load of all CPUs. It was made so
131 * intentionally to not reduce performance in scenarios when several
132 * threads are processing requests as a pipeline -- running one at
133 * a time on different CPUs and waiting for each other.
136 read_usage_times(int *load)
138 static long *cp_times = NULL, *cp_times_old = NULL;
139 static int ncpus = 0;
141 int error, cpu, i, total;
143 if (cp_times == NULL) {
145 error = sysctl(cp_times_mib, 2, NULL, &cp_times_len, NULL, 0);
148 if ((cp_times = malloc(cp_times_len)) == NULL)
150 if ((cp_times_old = malloc(cp_times_len)) == NULL) {
155 ncpus = cp_times_len / (sizeof(long) * CPUSTATES);
158 cp_times_len = sizeof(long) * CPUSTATES * ncpus;
159 error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
165 for (cpu = 0; cpu < ncpus; cpu++) {
167 for (i = 0; i < CPUSTATES; i++) {
168 total += cp_times[cpu * CPUSTATES + i] -
169 cp_times_old[cpu * CPUSTATES + i];
173 *load += 100 - (cp_times[cpu * CPUSTATES + CP_IDLE] -
174 cp_times_old[cpu * CPUSTATES + CP_IDLE]) * 100 / total;
178 memcpy(cp_times_old, cp_times, cp_times_len);
184 read_freqs(int *numfreqs, int **freqs, int **power, int minfreq, int maxfreq)
186 char *freqstr, *p, *q;
190 if (sysctl(levels_mib, 4, NULL, &len, NULL, 0))
192 if ((freqstr = malloc(len)) == NULL)
194 if (sysctl(levels_mib, 4, freqstr, &len, NULL, 0))
198 for (p = freqstr; *p != '\0'; p++)
202 if ((*freqs = malloc(*numfreqs * sizeof(int))) == NULL) {
206 if ((*power = malloc(*numfreqs * sizeof(int))) == NULL) {
211 for (i = 0, j = 0, p = freqstr; i < *numfreqs; i++) {
215 if (sscanf(p, "%d/%d", &(*freqs)[j], &(*power)[i]) != 2) {
221 if (((*freqs)[j] >= minfreq || minfreq == -1) &&
222 ((*freqs)[j] <= maxfreq || maxfreq == -1))
228 if ((*freqs = realloc(*freqs, *numfreqs * sizeof(int))) == NULL) {
245 len = sizeof(curfreq);
246 if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) {
248 warn("error reading current CPU frequency");
258 if (sysctl(freq_mib, 4, NULL, NULL, &freq, sizeof(freq))) {
267 get_freq_id(int freq, int *freqs, int numfreqs)
271 while (i < numfreqs) {
280 * Try to use ACPI to find the AC line status. If this fails, fall back
281 * to APM. If nothing succeeds, we'll just run in default mode.
287 acline_status = SRC_UNKNOWN;
289 if (sysctlnametomib(ACPIAC, acline_mib, &acline_mib_len) == 0) {
290 acline_mode = ac_sysctl;
292 warnx("using sysctl for AC line status");
294 } else if (sysctlnametomib(PMUAC, acline_mib, &acline_mib_len) == 0) {
295 acline_mode = ac_sysctl;
297 warnx("using sysctl for AC line status");
300 } else if ((apm_fd = open(APMDEV, O_RDONLY)) >= 0) {
302 warnx("using APM for AC line status");
303 acline_mode = ac_apm;
306 warnx("unable to determine AC line status");
307 acline_mode = ac_none;
314 if (acline_mode == ac_acpi_devd) {
315 char buf[DEVCTL_MAXBUF], *ptr;
319 rlen = read(devd_pipe, buf, sizeof(buf));
320 if (rlen == 0 || (rlen < 0 && errno != EWOULDBLOCK)) {
322 warnx("lost devd connection, switching to sysctl");
324 acline_mode = ac_sysctl;
328 (ptr = strstr(buf, "system=ACPI")) != NULL &&
329 (ptr = strstr(ptr, "subsystem=ACAD")) != NULL &&
330 (ptr = strstr(ptr, "notify=")) != NULL &&
331 sscanf(ptr, "notify=%x", ¬ify) == 1)
332 acline_status = (notify ? SRC_AC : SRC_BATTERY);
334 if (acline_mode == ac_sysctl) {
338 len = sizeof(acline);
339 if (sysctl(acline_mib, acline_mib_len, &acline, &len,
341 acline_status = (acline ? SRC_AC : SRC_BATTERY);
343 acline_status = SRC_UNKNOWN;
346 if (acline_mode == ac_apm) {
347 struct apm_info info;
349 if (ioctl(apm_fd, APMIO_GETINFO, &info) == 0) {
350 acline_status = (info.ai_acline ? SRC_AC : SRC_BATTERY);
354 acline_mode = ac_none;
355 acline_status = SRC_UNKNOWN;
359 /* try to (re)connect to devd */
360 if (acline_mode == ac_sysctl) {
363 gettimeofday(&now, NULL);
364 if (now.tv_sec > tried_devd.tv_sec + DEVD_RETRY_INTERVAL) {
365 if (devd_init() >= 0) {
367 warnx("using devd for AC line status");
368 acline_mode = ac_acpi_devd;
378 struct sockaddr_un devd_addr;
380 bzero(&devd_addr, sizeof(devd_addr));
381 if ((devd_pipe = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
383 warn("%s(): socket()", __func__);
387 devd_addr.sun_family = PF_LOCAL;
388 strlcpy(devd_addr.sun_path, DEVDPIPE, sizeof(devd_addr.sun_path));
389 if (connect(devd_pipe, (struct sockaddr *)&devd_addr,
390 sizeof(devd_addr)) == -1) {
392 warn("%s(): connect()", __func__);
398 if (fcntl(devd_pipe, F_SETFL, O_NONBLOCK) == -1) {
400 warn("%s(): fcntl()", __func__);
417 parse_mode(char *arg, int *mode, int ch)
420 if (strcmp(arg, "minimum") == 0 || strcmp(arg, "min") == 0)
422 else if (strcmp(arg, "maximum") == 0 || strcmp(arg, "max") == 0)
424 else if (strcmp(arg, "adaptive") == 0 || strcmp(arg, "adp") == 0)
425 *mode = MODE_ADAPTIVE;
426 else if (strcmp(arg, "hiadaptive") == 0 || strcmp(arg, "hadp") == 0)
427 *mode = MODE_HIADAPTIVE;
429 errx(1, "bad option: -%c %s", (char)ch, optarg);
433 handle_sigs(int __unused sig)
444 "usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-m freq] [-M freq] [-n mode] [-p ival] [-r %%] [-P pidfile]\n");
449 main(int argc, char * argv[])
451 struct timeval timeout;
454 struct pidfh *pfh = NULL;
455 const char *pidfile = NULL;
456 int freq, curfreq, initfreq, *freqs, i, j, *mwatts, numfreqs, load;
457 int minfreq = -1, maxfreq = -1;
458 int ch, mode, mode_ac, mode_battery, mode_none, idle, to;
459 uint64_t mjoules_used;
462 /* Default mode for all AC states is adaptive. */
463 mode_ac = mode_none = MODE_HIADAPTIVE;
464 mode_battery = MODE_ADAPTIVE;
465 cpu_running_mark = DEFAULT_ACTIVE_PERCENT;
466 cpu_idle_mark = DEFAULT_IDLE_PERCENT;
467 poll_ival = DEFAULT_POLL_INTERVAL;
471 /* User must be root to control frequencies. */
473 errx(1, "must be root to run");
475 while ((ch = getopt(argc, argv, "a:b:i:m:M:n:p:P:r:v")) != -1)
478 parse_mode(optarg, &mode_ac, ch);
481 parse_mode(optarg, &mode_battery, ch);
484 cpu_idle_mark = atoi(optarg);
485 if (cpu_idle_mark < 0 || cpu_idle_mark > 100) {
486 warnx("%d is not a valid percent",
492 minfreq = atoi(optarg);
494 warnx("%d is not a valid CPU frequency",
500 maxfreq = atoi(optarg);
502 warnx("%d is not a valid CPU frequency",
508 parse_mode(optarg, &mode_none, ch);
511 poll_ival = atoi(optarg);
513 warnx("poll interval is in units of ms");
521 cpu_running_mark = atoi(optarg);
522 if (cpu_running_mark <= 0 || cpu_running_mark > 100) {
523 warnx("%d is not a valid percent",
537 /* Poll interval is in units of ms. */
540 /* Look up various sysctl MIBs. */
542 if (sysctlnametomib("kern.cp_times", cp_times_mib, &len))
543 err(1, "lookup kern.cp_times");
545 if (sysctlnametomib("dev.cpu.0.freq", freq_mib, &len))
546 err(1, "lookup freq");
548 if (sysctlnametomib("dev.cpu.0.freq_levels", levels_mib, &len))
549 err(1, "lookup freq_levels");
551 /* Check if we can read the load and supported freqs. */
552 if (read_usage_times(NULL))
553 err(1, "read_usage_times");
554 if (read_freqs(&numfreqs, &freqs, &mwatts, minfreq, maxfreq))
555 err(1, "error reading supported CPU frequencies");
557 errx(1, "no CPU frequencies in user-specified range");
559 /* Run in the background unless in verbose mode. */
563 pfh = pidfile_open(pidfile, 0600, &otherpid);
565 if (errno == EEXIST) {
566 errx(1, "powerd already running, pid: %d",
569 warn("cannot open pid file");
571 if (daemon(0, 0) != 0) {
572 warn("cannot enter daemon mode, exiting");
580 /* Decide whether to use ACPI or APM to read the AC line status. */
584 * Exit cleanly on signals.
586 signal(SIGINT, handle_sigs);
587 signal(SIGTERM, handle_sigs);
589 freq = initfreq = curfreq = get_freq();
590 i = get_freq_id(curfreq, freqs, numfreqs);
595 * If we are in adaptive mode and the current frequency is outside the
596 * user-defined range, adjust it to be within the user-defined range.
599 if (acline_status > SRC_UNKNOWN)
600 errx(1, "invalid AC line status %d", acline_status);
601 if ((acline_status == SRC_AC &&
602 (mode_ac == MODE_ADAPTIVE || mode_ac == MODE_HIADAPTIVE)) ||
603 (acline_status == SRC_BATTERY &&
604 (mode_battery == MODE_ADAPTIVE || mode_battery == MODE_HIADAPTIVE)) ||
605 (acline_status == SRC_UNKNOWN &&
606 (mode_none == MODE_ADAPTIVE || mode_none == MODE_HIADAPTIVE))) {
607 /* Read the current frequency. */
608 len = sizeof(curfreq);
609 if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) {
611 warn("error reading current CPU frequency");
613 if (curfreq < freqs[numfreqs - 1]) {
615 printf("CPU frequency is below user-defined "
616 "minimum; changing frequency to %d "
617 "MHz\n", freqs[numfreqs - 1]);
619 if (set_freq(freqs[numfreqs - 1]) != 0) {
620 warn("error setting CPU freq %d",
621 freqs[numfreqs - 1]);
623 } else if (curfreq > freqs[0]) {
625 printf("CPU frequency is above user-defined "
626 "maximum; changing frequency to %d "
629 if (set_freq(freqs[0]) != 0) {
630 warn("error setting CPU freq %d",
640 if (devd_pipe >= 0) {
641 FD_SET(devd_pipe, &fdset);
642 nfds = devd_pipe + 1;
646 if (mode == MODE_HIADAPTIVE || idle < 120)
652 timeout.tv_sec = to / 1000000;
653 timeout.tv_usec = to % 1000000;
654 select(nfds, &fdset, NULL, &fdset, &timeout);
656 /* If the user requested we quit, print some statistics. */
657 if (exit_requested) {
658 if (vflag && mjoules_used != 0)
659 printf("total joules used: %u.%03u\n",
660 (u_int)(mjoules_used / 1000),
661 (int)mjoules_used % 1000);
665 /* Read the current AC status and record the mode. */
667 switch (acline_status) {
678 errx(1, "invalid AC line status %d", acline_status);
681 /* Read the current frequency. */
682 if (idle % 32 == 0) {
683 if ((curfreq = get_freq()) == 0)
685 i = get_freq_id(curfreq, freqs, numfreqs);
689 /* Keep a sum of all power actually used. */
692 (mwatts[i] * (poll_ival / 1000)) / 1000;
695 /* Always switch to the lowest frequency in min mode. */
696 if (mode == MODE_MIN) {
697 freq = freqs[numfreqs - 1];
698 if (curfreq != freq) {
700 printf("now operating on %s power; "
701 "changing frequency to %d MHz\n",
702 modes[acline_status], freq);
705 if (set_freq(freq) != 0) {
706 warn("error setting CPU freq %d",
714 /* Always switch to the highest frequency in max mode. */
715 if (mode == MODE_MAX) {
717 if (curfreq != freq) {
719 printf("now operating on %s power; "
720 "changing frequency to %d MHz\n",
721 modes[acline_status], freq);
724 if (set_freq(freq) != 0) {
725 warn("error setting CPU freq %d",
733 /* Adaptive mode; get the current CPU usage times. */
734 if (read_usage_times(&load)) {
736 warn("read_usage_times() failed");
740 if (mode == MODE_ADAPTIVE) {
741 if (load > cpu_running_mark) {
742 if (load > 95 || load > cpu_running_mark * 2)
745 freq = freq * load / cpu_running_mark;
748 } else if (load < cpu_idle_mark &&
749 curfreq * load < freqs[get_freq_id(
750 freq * 7 / 8, freqs, numfreqs)] *
753 if (freq < freqs[numfreqs - 1])
754 freq = freqs[numfreqs - 1];
756 } else { /* MODE_HIADAPTIVE */
757 if (load > cpu_running_mark / 2) {
758 if (load > 95 || load > cpu_running_mark)
761 freq = freq * load * 2 / cpu_running_mark;
762 if (freq > freqs[0] * 2)
764 } else if (load < cpu_idle_mark / 2 &&
765 curfreq * load < freqs[get_freq_id(
766 freq * 31 / 32, freqs, numfreqs)] *
767 cpu_running_mark / 2) {
768 freq = freq * 31 / 32;
769 if (freq < freqs[numfreqs - 1])
770 freq = freqs[numfreqs - 1];
774 printf("load %3d%%, current freq %4d MHz (%2d), wanted freq %4d MHz\n",
775 load, curfreq, i, freq);
777 j = get_freq_id(freq, freqs, numfreqs);
780 printf("changing clock"
781 " speed from %d MHz to %d MHz\n",
785 if (set_freq(freqs[j]))
786 warn("error setting CPU frequency %d",
790 if (set_freq(initfreq))
791 warn("error setting CPU frequency %d", initfreq);