2005-02-26 21:17:31 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2004 Colin Percival
|
|
|
|
* Copyright (c) 2005 Nate Lawson
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted providing that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2005-10-19 04:48:44 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
2005-02-26 21:17:31 +00:00
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2005-10-19 04:48:44 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
2005-02-26 21:17:31 +00:00
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
2005-10-19 04:48:44 +00:00
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
2005-02-26 21:17:31 +00:00
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-10-19 04:48:44 +00:00
|
|
|
#include <sys/types.h>
|
2005-08-24 17:32:41 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/resource.h>
|
2005-10-19 04:48:44 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2005-08-24 17:32:41 +00:00
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
#include <err.h>
|
2005-07-18 20:15:31 +00:00
|
|
|
#include <errno.h>
|
2005-02-26 21:17:31 +00:00
|
|
|
#include <fcntl.h>
|
2005-08-24 17:32:41 +00:00
|
|
|
#include <libutil.h>
|
2005-10-19 04:48:44 +00:00
|
|
|
#include <pthread.h>
|
2005-04-10 19:02:29 +00:00
|
|
|
#include <signal.h>
|
2005-02-26 21:17:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2005-02-27 00:23:19 +00:00
|
|
|
#ifdef __i386__
|
2005-02-26 21:17:31 +00:00
|
|
|
#include <machine/apm_bios.h>
|
2005-02-27 00:23:19 +00:00
|
|
|
#endif
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2005-04-10 20:42:55 +00:00
|
|
|
#define DEFAULT_ACTIVE_PERCENT 65
|
2005-02-26 23:22:00 +00:00
|
|
|
#define DEFAULT_IDLE_PERCENT 90
|
2005-04-10 20:42:55 +00:00
|
|
|
#define DEFAULT_POLL_INTERVAL 500 /* Poll interval in milliseconds */
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
enum modes_t {
|
|
|
|
MODE_MIN,
|
|
|
|
MODE_ADAPTIVE,
|
|
|
|
MODE_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum power_src_t {
|
|
|
|
SRC_AC,
|
|
|
|
SRC_BATTERY,
|
|
|
|
SRC_UNKNOWN,
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *modes[] = {
|
|
|
|
"AC",
|
|
|
|
"battery",
|
|
|
|
"unknown"
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ACPIAC "hw.acpi.acline"
|
|
|
|
#define APMDEV "/dev/apm"
|
2005-10-19 04:48:44 +00:00
|
|
|
#define DEVDPIPE "/var/run/devd.pipe"
|
|
|
|
#define DEVCTL_MAXBUF 1024
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
static int read_usage_times(long *idle, long *total);
|
2005-04-10 19:02:29 +00:00
|
|
|
static int read_freqs(int *numfreqs, int **freqs, int **power);
|
2005-02-26 21:17:31 +00:00
|
|
|
static int set_freq(int freq);
|
2005-02-27 01:58:49 +00:00
|
|
|
static void acline_init(void);
|
|
|
|
static int acline_read(void);
|
2005-10-19 04:48:44 +00:00
|
|
|
static int devd_init(void);
|
|
|
|
static void devd_close(void);
|
|
|
|
static void *devd_read(void *arg);
|
2005-04-10 19:02:29 +00:00
|
|
|
static void handle_sigs(int sig);
|
2005-02-26 21:17:31 +00:00
|
|
|
static void parse_mode(char *arg, int *mode, int ch);
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
/* Sysctl data structures. */
|
|
|
|
static int cp_time_mib[2];
|
|
|
|
static int freq_mib[4];
|
|
|
|
static int levels_mib[4];
|
|
|
|
static int acline_mib[3];
|
|
|
|
|
2005-10-19 04:48:44 +00:00
|
|
|
/* devd-cached value provided by our thread. */
|
|
|
|
static int devd_acline;
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
/* Configuration */
|
|
|
|
static int cpu_running_mark;
|
|
|
|
static int cpu_idle_mark;
|
|
|
|
static int poll_ival;
|
2005-10-19 04:48:44 +00:00
|
|
|
static int vflag;
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2005-02-27 01:58:49 +00:00
|
|
|
static int apm_fd;
|
2005-10-19 04:48:44 +00:00
|
|
|
static int devd_pipe;
|
|
|
|
static pthread_t devd_thread;
|
2005-04-10 19:02:29 +00:00
|
|
|
static int exit_requested;
|
2005-02-27 01:58:49 +00:00
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
static int
|
|
|
|
read_usage_times(long *idle, long *total)
|
|
|
|
{
|
|
|
|
static long idle_old, total_old;
|
|
|
|
long cp_time[CPUSTATES], i, total_new;
|
|
|
|
size_t cp_time_len;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
cp_time_len = sizeof(cp_time);
|
|
|
|
error = sysctl(cp_time_mib, 2, cp_time, &cp_time_len, NULL, 0);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
for (total_new = 0, i = 0; i < CPUSTATES; i++)
|
|
|
|
total_new += cp_time[i];
|
|
|
|
|
|
|
|
if (idle)
|
|
|
|
*idle = cp_time[CP_IDLE] - idle_old;
|
|
|
|
if (total)
|
|
|
|
*total = total_new - total_old;
|
|
|
|
|
|
|
|
idle_old = cp_time[CP_IDLE];
|
|
|
|
total_old = total_new;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-04-10 19:02:29 +00:00
|
|
|
read_freqs(int *numfreqs, int **freqs, int **power)
|
2005-02-26 21:17:31 +00:00
|
|
|
{
|
|
|
|
char *freqstr, *p, *q;
|
|
|
|
int i;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if (sysctl(levels_mib, 4, NULL, &len, NULL, 0))
|
|
|
|
return (-1);
|
|
|
|
if ((freqstr = malloc(len)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
if (sysctl(levels_mib, 4, freqstr, &len, NULL, 0))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
*numfreqs = 1;
|
|
|
|
for (p = freqstr; *p != '\0'; p++)
|
|
|
|
if (*p == ' ')
|
|
|
|
(*numfreqs)++;
|
|
|
|
|
|
|
|
if ((*freqs = malloc(*numfreqs * sizeof(int))) == NULL) {
|
|
|
|
free(freqstr);
|
|
|
|
return (-1);
|
|
|
|
}
|
2005-04-10 19:02:29 +00:00
|
|
|
if ((*power = malloc(*numfreqs * sizeof(int))) == NULL) {
|
|
|
|
free(freqstr);
|
|
|
|
free(*freqs);
|
|
|
|
return (-1);
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
for (i = 0, p = freqstr; i < *numfreqs; i++) {
|
|
|
|
q = strchr(p, ' ');
|
|
|
|
if (q != NULL)
|
|
|
|
*q = '\0';
|
2005-04-10 19:02:29 +00:00
|
|
|
if (sscanf(p, "%d/%d", &(*freqs)[i], &(*power)[i]) != 2) {
|
2005-02-26 21:17:31 +00:00
|
|
|
free(freqstr);
|
|
|
|
free(*freqs);
|
2005-04-10 19:02:29 +00:00
|
|
|
free(*power);
|
2005-02-26 21:17:31 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
p = q + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(freqstr);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_freq(int freq)
|
|
|
|
{
|
|
|
|
|
2005-07-18 20:15:31 +00:00
|
|
|
if (sysctl(freq_mib, 4, NULL, NULL, &freq, sizeof(freq))) {
|
|
|
|
if (errno != EPERM)
|
|
|
|
return (-1);
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-02-27 01:58:49 +00:00
|
|
|
/*
|
|
|
|
* Try to use ACPI to find the AC line status. If this fails, fall back
|
2005-10-19 04:48:44 +00:00
|
|
|
* to APM. If nothing succeeds, we'll just run in default mode. If we are
|
|
|
|
* using ACPI, try opening a pipe to devd to detect AC line events.
|
2005-02-27 01:58:49 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
acline_init()
|
|
|
|
{
|
|
|
|
int acline;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
apm_fd = -1;
|
2005-10-19 04:48:44 +00:00
|
|
|
devd_pipe = -1;
|
2005-02-27 01:58:49 +00:00
|
|
|
len = sizeof(acline);
|
|
|
|
if (sysctlbyname(ACPIAC, &acline, &len, NULL, 0) == 0) {
|
|
|
|
len = 3;
|
|
|
|
if (sysctlnametomib(ACPIAC, acline_mib, &len))
|
|
|
|
err(1, "lookup acline");
|
2005-10-19 04:48:44 +00:00
|
|
|
|
|
|
|
/* Read line status once so that we have an initial value. */
|
|
|
|
devd_acline = acline_read();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try connecting to the devd pipe and start a read thread
|
|
|
|
* if we succeed.
|
|
|
|
*/
|
|
|
|
if ((devd_pipe = devd_init()) >= 0) {
|
|
|
|
if (pthread_create(&devd_thread, NULL, devd_read,
|
|
|
|
&devd_pipe))
|
|
|
|
err(1, "pthread_create devd thread");
|
|
|
|
} else if (vflag) {
|
|
|
|
warnx(
|
|
|
|
"unable to connect to devd pipe, using polling mode instead");
|
|
|
|
}
|
2005-02-27 01:58:49 +00:00
|
|
|
} else {
|
|
|
|
apm_fd = open(APMDEV, O_RDONLY);
|
|
|
|
if (apm_fd == -1)
|
|
|
|
warnx(
|
|
|
|
"cannot read AC line status, using default settings");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
acline_read()
|
|
|
|
{
|
2005-10-19 04:48:44 +00:00
|
|
|
int acline;
|
2005-02-27 01:58:49 +00:00
|
|
|
size_t len;
|
|
|
|
#ifdef __i386__
|
|
|
|
struct apm_info info;
|
2005-10-19 04:48:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
acline = SRC_UNKNOWN;
|
|
|
|
len = sizeof(acline);
|
2005-02-27 01:58:49 +00:00
|
|
|
|
2005-10-19 04:48:44 +00:00
|
|
|
/*
|
|
|
|
* Get state from our devd thread, the ACPI sysctl, or APM. We
|
|
|
|
* prefer sources in this order.
|
|
|
|
*/
|
|
|
|
if (devd_pipe >= 0)
|
|
|
|
acline = devd_acline;
|
|
|
|
else if (sysctl(acline_mib, 3, &acline, &len, NULL, 0) == 0)
|
|
|
|
acline = acline ? SRC_AC : SRC_BATTERY;
|
|
|
|
#ifdef __i386__
|
|
|
|
else if (apm_fd != -1 && ioctl(apm_fd, APMIO_GETINFO, &info) == 0)
|
2005-02-27 01:58:49 +00:00
|
|
|
acline = info.ai_acline ? SRC_AC : SRC_BATTERY;
|
|
|
|
#endif
|
|
|
|
|
2005-10-19 04:48:44 +00:00
|
|
|
return (acline);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devd_init(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_un devd_addr;
|
|
|
|
int devd_sock;
|
|
|
|
|
|
|
|
bzero(&devd_addr, sizeof(devd_addr));
|
|
|
|
if ((devd_sock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
|
|
|
|
if (vflag)
|
|
|
|
warn("failed to create devd socket");
|
|
|
|
return (-1);
|
2005-02-27 01:58:49 +00:00
|
|
|
}
|
|
|
|
|
2005-10-19 04:48:44 +00:00
|
|
|
devd_addr.sun_family = PF_LOCAL;
|
|
|
|
strlcpy(devd_addr.sun_path, DEVDPIPE, sizeof(devd_addr.sun_path));
|
|
|
|
if (connect(devd_sock, (struct sockaddr *)&devd_addr,
|
|
|
|
sizeof(devd_addr)) == -1) {
|
|
|
|
close(devd_sock);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (devd_sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
devd_close(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (devd_pipe < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_kill(devd_thread, SIGTERM);
|
|
|
|
close(devd_pipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This loop runs as a separate thread. It reads events from devd, but
|
|
|
|
* spends most of its time blocked in select(2).
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
devd_read(void *arg)
|
|
|
|
{
|
|
|
|
char buf[DEVCTL_MAXBUF], *ptr;
|
|
|
|
fd_set fdset;
|
|
|
|
int fd, notify, rlen;
|
|
|
|
|
|
|
|
fd = *(int *)arg;
|
|
|
|
notify = -1;
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
while (!exit_requested) {
|
|
|
|
FD_SET(fd, &fdset);
|
|
|
|
if (select(fd + 1, &fdset, NULL, NULL, NULL) < 0)
|
|
|
|
break;
|
|
|
|
if (!FD_ISSET(fd, &fdset))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Read the notify string, devd NULL-terminates it. */
|
|
|
|
rlen = read(fd, buf, sizeof(buf));
|
|
|
|
if (rlen <= 0) {
|
|
|
|
close(devd_pipe);
|
|
|
|
devd_pipe = -1;
|
|
|
|
if (vflag)
|
|
|
|
warnx(
|
|
|
|
"devd disappeared, downgrading to polling mode");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep trying to reconnect to devd but sleep in
|
|
|
|
* between to avoid wasting CPU cycles.
|
|
|
|
*/
|
|
|
|
while (!exit_requested && (fd = devd_init()) < 0)
|
|
|
|
sleep(300);
|
|
|
|
|
|
|
|
if (fd >= 0) {
|
|
|
|
devd_pipe = fd;
|
|
|
|
if (vflag)
|
|
|
|
warnx(
|
|
|
|
"devd came back, upgrading to event mode");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loosely match the notify string. */
|
|
|
|
if ((ptr = strstr(buf, "system=ACPI")) != NULL &&
|
|
|
|
(ptr = strstr(ptr, "subsystem=ACAD")) != NULL &&
|
|
|
|
(ptr = strstr(ptr, "notify=")) != NULL) {
|
|
|
|
if (sscanf(ptr, "notify=%x", ¬ify) != 1) {
|
|
|
|
warnx("bad devd notify string");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
devd_acline = notify ? SRC_AC : SRC_BATTERY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
2005-02-27 01:58:49 +00:00
|
|
|
}
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
static void
|
|
|
|
parse_mode(char *arg, int *mode, int ch)
|
|
|
|
{
|
|
|
|
|
2005-09-26 12:16:27 +00:00
|
|
|
if (strcmp(arg, "minimum") == 0 || strcmp(arg, "min") == 0)
|
2005-02-26 21:17:31 +00:00
|
|
|
*mode = MODE_MIN;
|
2005-09-26 12:16:27 +00:00
|
|
|
else if (strcmp(arg, "maximum") == 0 || strcmp(arg, "max") == 0)
|
2005-02-26 21:17:31 +00:00
|
|
|
*mode = MODE_MAX;
|
|
|
|
else if (strcmp(arg, "adaptive") == 0)
|
|
|
|
*mode = MODE_ADAPTIVE;
|
|
|
|
else
|
|
|
|
errx(1, "bad option: -%c %s", (char)ch, optarg);
|
|
|
|
}
|
|
|
|
|
2005-04-10 19:02:29 +00:00
|
|
|
static void
|
|
|
|
handle_sigs(int __unused sig)
|
|
|
|
{
|
2005-10-19 04:48:44 +00:00
|
|
|
|
2005-04-10 19:02:29 +00:00
|
|
|
exit_requested = 1;
|
|
|
|
}
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2005-08-24 17:32:41 +00:00
|
|
|
"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%] [-P pidfile]\n");
|
2005-02-26 21:17:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char * argv[])
|
|
|
|
{
|
2005-08-24 19:42:40 +00:00
|
|
|
struct pidfh *pfh = NULL;
|
2005-08-24 17:32:41 +00:00
|
|
|
const char *pidfile = NULL;
|
2005-02-26 21:17:31 +00:00
|
|
|
long idle, total;
|
2005-10-19 04:48:44 +00:00
|
|
|
int acline, curfreq, *freqs, i, *mwatts, numfreqs;
|
|
|
|
int ch, mode, mode_ac, mode_battery, mode_none;
|
2005-04-10 19:02:29 +00:00
|
|
|
uint64_t mjoules_used;
|
2005-02-26 21:17:31 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Default mode for all AC states is adaptive. */
|
|
|
|
mode_ac = mode_battery = mode_none = MODE_ADAPTIVE;
|
|
|
|
cpu_running_mark = DEFAULT_ACTIVE_PERCENT;
|
|
|
|
cpu_idle_mark = DEFAULT_IDLE_PERCENT;
|
|
|
|
poll_ival = DEFAULT_POLL_INTERVAL;
|
2005-04-10 19:02:29 +00:00
|
|
|
mjoules_used = 0;
|
2005-02-26 21:17:31 +00:00
|
|
|
vflag = 0;
|
2005-02-27 01:58:49 +00:00
|
|
|
apm_fd = -1;
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2005-07-25 16:43:12 +00:00
|
|
|
/* User must be root to control frequencies. */
|
|
|
|
if (geteuid() != 0)
|
|
|
|
errx(1, "must be root to run");
|
|
|
|
|
2005-08-24 17:32:41 +00:00
|
|
|
while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:v")) != EOF)
|
2005-02-26 21:17:31 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
parse_mode(optarg, &mode_ac, ch);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
parse_mode(optarg, &mode_battery, ch);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
cpu_idle_mark = atoi(optarg);
|
|
|
|
if (cpu_idle_mark < 0 || cpu_idle_mark > 100) {
|
|
|
|
warnx("%d is not a valid percent",
|
|
|
|
cpu_idle_mark);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
parse_mode(optarg, &mode_none, ch);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
poll_ival = atoi(optarg);
|
|
|
|
if (poll_ival < 5) {
|
|
|
|
warnx("poll interval is in units of ms");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
2005-08-24 17:32:41 +00:00
|
|
|
case 'P':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
2005-02-26 21:17:31 +00:00
|
|
|
case 'r':
|
|
|
|
cpu_running_mark = atoi(optarg);
|
|
|
|
if (cpu_running_mark < 0 || cpu_running_mark > 100) {
|
|
|
|
warnx("%d is not a valid percent",
|
|
|
|
cpu_running_mark);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2005-10-19 04:48:44 +00:00
|
|
|
mode = mode_none;
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
/* Poll interval is in units of ms. */
|
|
|
|
poll_ival *= 1000;
|
|
|
|
|
|
|
|
/* Look up various sysctl MIBs. */
|
|
|
|
len = 2;
|
|
|
|
if (sysctlnametomib("kern.cp_time", cp_time_mib, &len))
|
|
|
|
err(1, "lookup kern.cp_time");
|
|
|
|
len = 4;
|
|
|
|
if (sysctlnametomib("dev.cpu.0.freq", freq_mib, &len))
|
|
|
|
err(1, "lookup freq");
|
|
|
|
len = 4;
|
|
|
|
if (sysctlnametomib("dev.cpu.0.freq_levels", levels_mib, &len))
|
|
|
|
err(1, "lookup freq_levels");
|
|
|
|
|
|
|
|
/* Check if we can read the idle time and supported freqs. */
|
|
|
|
if (read_usage_times(NULL, NULL))
|
|
|
|
err(1, "read_usage_times");
|
2005-04-10 19:02:29 +00:00
|
|
|
if (read_freqs(&numfreqs, &freqs, &mwatts))
|
2005-02-26 21:17:31 +00:00
|
|
|
err(1, "error reading supported CPU frequencies");
|
|
|
|
|
2005-10-20 05:12:20 +00:00
|
|
|
/*
|
|
|
|
* Exit cleanly on signals; devd may send a SIGPIPE if it dies. We
|
|
|
|
* do this before acline_init() since it may create a thread and we
|
|
|
|
* want it to inherit our signal mask.
|
|
|
|
*/
|
|
|
|
signal(SIGINT, handle_sigs);
|
|
|
|
signal(SIGTERM, handle_sigs);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
/* Decide whether to use ACPI or APM to read the AC line status. */
|
2005-02-27 01:58:49 +00:00
|
|
|
acline_init();
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
/* Run in the background unless in verbose mode. */
|
2005-08-24 17:32:41 +00:00
|
|
|
if (!vflag) {
|
|
|
|
pid_t otherpid;
|
|
|
|
|
|
|
|
pfh = pidfile_open(pidfile, 0600, &otherpid);
|
|
|
|
if (pfh == NULL) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
errx(1, "powerd already running, pid: %d",
|
|
|
|
otherpid);
|
|
|
|
}
|
|
|
|
warn("cannot open pid file");
|
|
|
|
}
|
2005-10-20 05:12:20 +00:00
|
|
|
if (daemon(0, 0) != 0) {
|
|
|
|
warn("cannot enter daemon mode, exiting");
|
|
|
|
pidfile_remove(pfh);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
}
|
2005-08-24 17:32:41 +00:00
|
|
|
pidfile_write(pfh);
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
/* Main loop. */
|
|
|
|
for (;;) {
|
|
|
|
/* Check status every few milliseconds. */
|
|
|
|
usleep(poll_ival);
|
|
|
|
|
2005-04-10 19:02:29 +00:00
|
|
|
/* If the user requested we quit, print some statistics. */
|
|
|
|
if (exit_requested) {
|
|
|
|
if (vflag && mjoules_used != 0)
|
|
|
|
printf("total joules used: %u.%03u\n",
|
|
|
|
(u_int)(mjoules_used / 1000),
|
|
|
|
(int)mjoules_used % 1000);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
/* Read the current AC status and record the mode. */
|
2005-02-27 01:58:49 +00:00
|
|
|
acline = acline_read();
|
2005-02-26 21:17:31 +00:00
|
|
|
switch (acline) {
|
|
|
|
case SRC_AC:
|
|
|
|
mode = mode_ac;
|
|
|
|
break;
|
|
|
|
case SRC_BATTERY:
|
|
|
|
mode = mode_battery;
|
|
|
|
break;
|
|
|
|
case SRC_UNKNOWN:
|
|
|
|
mode = mode_none;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "invalid AC line status %d", acline);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the current frequency. */
|
|
|
|
len = sizeof(curfreq);
|
2005-10-19 04:48:44 +00:00
|
|
|
if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) {
|
|
|
|
if (vflag)
|
|
|
|
warn("error reading current CPU frequency");
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2005-04-10 19:02:29 +00:00
|
|
|
if (vflag) {
|
|
|
|
for (i = 0; i < numfreqs; i++) {
|
|
|
|
if (freqs[i] == curfreq)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep a sum of all power actually used. */
|
|
|
|
if (i < numfreqs && mwatts[i] != -1)
|
|
|
|
mjoules_used +=
|
|
|
|
(mwatts[i] * (poll_ival / 1000)) / 1000;
|
|
|
|
}
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
/* Always switch to the lowest frequency in min mode. */
|
|
|
|
if (mode == MODE_MIN) {
|
|
|
|
if (curfreq != freqs[numfreqs - 1]) {
|
|
|
|
if (vflag) {
|
|
|
|
printf("now operating on %s power; "
|
|
|
|
"changing frequency to %d MHz\n",
|
|
|
|
modes[acline], freqs[numfreqs - 1]);
|
|
|
|
}
|
2005-10-19 04:48:44 +00:00
|
|
|
if (set_freq(freqs[numfreqs - 1]) != 0) {
|
|
|
|
warn("error setting CPU freq %d",
|
2005-02-26 21:17:31 +00:00
|
|
|
freqs[numfreqs - 1]);
|
2005-10-19 04:48:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Always switch to the highest frequency in max mode. */
|
|
|
|
if (mode == MODE_MAX) {
|
|
|
|
if (curfreq != freqs[0]) {
|
|
|
|
if (vflag) {
|
2005-02-27 01:58:49 +00:00
|
|
|
printf("now operating on %s power; "
|
2005-02-26 21:17:31 +00:00
|
|
|
"changing frequency to %d MHz\n",
|
|
|
|
modes[acline], freqs[0]);
|
|
|
|
}
|
2005-10-19 04:48:44 +00:00
|
|
|
if (set_freq(freqs[0]) != 0) {
|
|
|
|
warn("error setting CPU freq %d",
|
|
|
|
freqs[0]);
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adaptive mode; get the current CPU usage times. */
|
2005-10-19 04:48:44 +00:00
|
|
|
if (read_usage_times(&idle, &total)) {
|
|
|
|
if (vflag)
|
|
|
|
warn("read_usage_times() failed");
|
|
|
|
continue;
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
/*
|
2005-08-24 07:52:59 +00:00
|
|
|
* If we're idle less than the active mark, bump up two levels.
|
|
|
|
* If we're idle more than the idle mark, drop down one level.
|
2005-02-26 21:17:31 +00:00
|
|
|
*/
|
2005-08-24 07:52:59 +00:00
|
|
|
for (i = 0; i < numfreqs - 1; i++) {
|
|
|
|
if (freqs[i] == curfreq)
|
|
|
|
break;
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
if (idle < (total * cpu_running_mark) / 100 &&
|
|
|
|
curfreq < freqs[0]) {
|
2005-08-24 07:52:59 +00:00
|
|
|
i -= 2;
|
|
|
|
if (i < 0)
|
|
|
|
i = 0;
|
2005-02-26 21:17:31 +00:00
|
|
|
if (vflag) {
|
|
|
|
printf("idle time < %d%%, increasing clock"
|
|
|
|
" speed from %d MHz to %d MHz\n",
|
2005-08-24 07:52:59 +00:00
|
|
|
cpu_running_mark, curfreq, freqs[i]);
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
2005-08-24 07:52:59 +00:00
|
|
|
if (set_freq(freqs[i]))
|
2005-02-26 21:17:31 +00:00
|
|
|
err(1, "error setting CPU frequency %d",
|
2005-08-24 07:52:59 +00:00
|
|
|
freqs[i]);
|
2005-02-26 21:17:31 +00:00
|
|
|
} else if (idle > (total * cpu_idle_mark) / 100 &&
|
|
|
|
curfreq > freqs[numfreqs - 1]) {
|
2005-08-24 07:52:59 +00:00
|
|
|
i++;
|
2005-02-26 21:17:31 +00:00
|
|
|
if (vflag) {
|
|
|
|
printf("idle time > %d%%, decreasing clock"
|
|
|
|
" speed from %d MHz to %d MHz\n",
|
|
|
|
cpu_idle_mark, curfreq, freqs[i]);
|
|
|
|
}
|
2005-10-19 04:48:44 +00:00
|
|
|
if (set_freq(freqs[i]) != 0)
|
|
|
|
warn("error setting CPU frequency %d",
|
|
|
|
freqs[i]);
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-10 19:02:29 +00:00
|
|
|
free(freqs);
|
|
|
|
free(mwatts);
|
2005-10-19 04:48:44 +00:00
|
|
|
devd_close();
|
2005-08-24 17:32:41 +00:00
|
|
|
if (!vflag)
|
|
|
|
pidfile_remove(pfh);
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|