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-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>
|
2006-01-03 21:32:02 +00:00
|
|
|
#include <sys/time.h>
|
2005-10-19 04:48:44 +00:00
|
|
|
#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-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>
|
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
#ifdef USE_APM
|
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
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
#define DEFAULT_ACTIVE_PERCENT 75
|
|
|
|
#define DEFAULT_IDLE_PERCENT 50
|
|
|
|
#define DEFAULT_POLL_INTERVAL 250 /* Poll interval in milliseconds */
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
typedef enum {
|
2005-02-26 21:17:31 +00:00
|
|
|
MODE_MIN,
|
|
|
|
MODE_ADAPTIVE,
|
2008-11-18 13:24:38 +00:00
|
|
|
MODE_HIADAPTIVE,
|
2005-02-26 21:17:31 +00:00
|
|
|
MODE_MAX,
|
2006-01-03 21:32:02 +00:00
|
|
|
} modes_t;
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
typedef enum {
|
2005-02-26 21:17:31 +00:00
|
|
|
SRC_AC,
|
|
|
|
SRC_BATTERY,
|
|
|
|
SRC_UNKNOWN,
|
2006-01-03 21:32:02 +00:00
|
|
|
} power_src_t;
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
static int read_usage_times(int *load);
|
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);
|
2006-01-03 21:32:02 +00:00
|
|
|
static void acline_read(void);
|
2005-10-19 04:48:44 +00:00
|
|
|
static int devd_init(void);
|
|
|
|
static void devd_close(void);
|
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. */
|
2008-11-18 13:24:38 +00:00
|
|
|
static int cp_times_mib[2];
|
2005-02-26 21:17:31 +00:00
|
|
|
static int freq_mib[4];
|
|
|
|
static int levels_mib[4];
|
|
|
|
static int acline_mib[3];
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
static volatile sig_atomic_t exit_requested;
|
|
|
|
static power_src_t acline_status;
|
|
|
|
static enum {
|
|
|
|
ac_none,
|
|
|
|
ac_acpi_sysctl,
|
|
|
|
ac_acpi_devd,
|
|
|
|
#ifdef USE_APM
|
|
|
|
ac_apm,
|
|
|
|
#endif
|
|
|
|
} acline_mode;
|
|
|
|
#ifdef USE_APM
|
|
|
|
static int apm_fd = -1;
|
|
|
|
#endif
|
|
|
|
static int devd_pipe = -1;
|
|
|
|
|
|
|
|
#define DEVD_RETRY_INTERVAL 60 /* seconds */
|
|
|
|
static struct timeval tried_devd;
|
2005-02-27 01:58:49 +00:00
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
static int
|
2008-11-18 13:24:38 +00:00
|
|
|
read_usage_times(int *load)
|
2005-02-26 21:17:31 +00:00
|
|
|
{
|
2008-11-18 13:24:38 +00:00
|
|
|
static long *cp_times = NULL, *cp_times_old = NULL;
|
|
|
|
static int ncpus = 0;
|
|
|
|
size_t cp_times_len;
|
|
|
|
int error, cpu, i, total;
|
|
|
|
|
|
|
|
if (cp_times == NULL) {
|
|
|
|
cp_times_len = 0;
|
|
|
|
error = sysctl(cp_times_mib, 2, NULL, &cp_times_len, NULL, 0);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if ((cp_times = malloc(cp_times_len)) == NULL)
|
|
|
|
return (errno);
|
|
|
|
if ((cp_times_old = malloc(cp_times_len)) == NULL) {
|
|
|
|
free(cp_times);
|
|
|
|
cp_times = NULL;
|
|
|
|
return (errno);
|
|
|
|
}
|
|
|
|
ncpus = cp_times_len / (sizeof(long) * CPUSTATES);
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
cp_times_len = sizeof(long) * CPUSTATES * ncpus;
|
|
|
|
error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
|
2005-02-26 21:17:31 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2008-11-18 13:24:38 +00:00
|
|
|
|
|
|
|
if (load) {
|
|
|
|
*load = 0;
|
|
|
|
for (cpu = 0; cpu < ncpus; cpu++) {
|
|
|
|
total = 0;
|
|
|
|
for (i = 0; i < CPUSTATES; i++) {
|
|
|
|
total += cp_times[cpu * CPUSTATES + i] -
|
|
|
|
cp_times_old[cpu * CPUSTATES + i];
|
|
|
|
}
|
|
|
|
if (total == 0)
|
|
|
|
continue;
|
|
|
|
*load += 100 - (cp_times[cpu * CPUSTATES + CP_IDLE] -
|
|
|
|
cp_times_old[cpu * CPUSTATES + CP_IDLE]) * 100 / total;
|
|
|
|
}
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
memcpy(cp_times_old, cp_times, cp_times_len);
|
2005-02-26 21:17:31 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
static int
|
|
|
|
get_freq(void)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int curfreq;
|
|
|
|
|
|
|
|
len = sizeof(curfreq);
|
|
|
|
if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) {
|
|
|
|
if (vflag)
|
|
|
|
warn("error reading current CPU frequency");
|
|
|
|
curfreq = 0;
|
|
|
|
}
|
|
|
|
return (curfreq);
|
|
|
|
}
|
|
|
|
|
2005-02-26 21:17:31 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
static int
|
|
|
|
get_freq_id(int freq, int *freqs, int numfreqs)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
while (i < numfreqs) {
|
|
|
|
if (freqs[i] < freq)
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (i - 1);
|
|
|
|
}
|
|
|
|
|
2005-02-27 01:58:49 +00:00
|
|
|
/*
|
|
|
|
* Try to use ACPI to find the AC line status. If this fails, fall back
|
2006-01-03 21:32:02 +00:00
|
|
|
* to APM. If nothing succeeds, we'll just run in default mode.
|
2005-02-27 01:58:49 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
acline_init()
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
len = 3;
|
|
|
|
if (sysctlnametomib(ACPIAC, acline_mib, &len) == 0) {
|
|
|
|
acline_mode = ac_acpi_sysctl;
|
|
|
|
if (vflag)
|
|
|
|
warnx("using sysctl for AC line status");
|
|
|
|
#ifdef USE_APM
|
|
|
|
} else if ((apm_fd = open(APMDEV, O_RDONLY)) >= 0) {
|
|
|
|
if (vflag)
|
|
|
|
warnx("using APM for AC line status");
|
|
|
|
acline_mode = ac_apm;
|
|
|
|
#endif
|
2005-02-27 01:58:49 +00:00
|
|
|
} else {
|
2006-01-03 21:32:02 +00:00
|
|
|
warnx("unable to determine AC line status");
|
|
|
|
acline_mode = ac_none;
|
2005-02-27 01:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
static void
|
|
|
|
acline_read(void)
|
2005-02-27 01:58:49 +00:00
|
|
|
{
|
2006-01-03 21:32:02 +00:00
|
|
|
if (acline_mode == ac_acpi_devd) {
|
|
|
|
char buf[DEVCTL_MAXBUF], *ptr;
|
|
|
|
ssize_t rlen;
|
|
|
|
int notify;
|
2005-10-19 04:48:44 +00:00
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
rlen = read(devd_pipe, buf, sizeof(buf));
|
|
|
|
if (rlen == 0 || (rlen < 0 && errno != EWOULDBLOCK)) {
|
|
|
|
if (vflag)
|
|
|
|
warnx("lost devd connection, switching to sysctl");
|
|
|
|
devd_close();
|
|
|
|
acline_mode = ac_acpi_sysctl;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
}
|
|
|
|
if (rlen > 0 &&
|
|
|
|
(ptr = strstr(buf, "system=ACPI")) != NULL &&
|
|
|
|
(ptr = strstr(ptr, "subsystem=ACAD")) != NULL &&
|
|
|
|
(ptr = strstr(ptr, "notify=")) != NULL &&
|
|
|
|
sscanf(ptr, "notify=%x", ¬ify) == 1)
|
|
|
|
acline_status = (notify ? SRC_AC : SRC_BATTERY);
|
|
|
|
}
|
|
|
|
if (acline_mode == ac_acpi_sysctl) {
|
|
|
|
int acline;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = sizeof(acline);
|
|
|
|
if (sysctl(acline_mib, 3, &acline, &len, NULL, 0) == 0)
|
|
|
|
acline_status = (acline ? SRC_AC : SRC_BATTERY);
|
|
|
|
else
|
|
|
|
acline_status = SRC_UNKNOWN;
|
|
|
|
}
|
|
|
|
#ifdef USE_APM
|
|
|
|
if (acline_mode == ac_apm) {
|
|
|
|
struct apm_info info;
|
|
|
|
|
|
|
|
if (ioctl(apm_fd, APMIO_GETINFO, &info) == 0) {
|
|
|
|
acline_status = (info.ai_acline ? SRC_AC : SRC_BATTERY);
|
|
|
|
} else {
|
|
|
|
close(apm_fd);
|
|
|
|
apm_fd = -1;
|
|
|
|
acline_mode = ac_none;
|
|
|
|
acline_status = SRC_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2005-02-27 01:58:49 +00:00
|
|
|
#endif
|
2006-01-03 21:32:02 +00:00
|
|
|
/* try to (re)connect to devd */
|
|
|
|
if (acline_mode == ac_acpi_sysctl) {
|
|
|
|
struct timeval now;
|
2005-02-27 01:58:49 +00:00
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
if (now.tv_sec > tried_devd.tv_sec + DEVD_RETRY_INTERVAL) {
|
|
|
|
if (devd_init() >= 0) {
|
|
|
|
if (vflag)
|
|
|
|
warnx("using devd for AC line status");
|
|
|
|
acline_mode = ac_acpi_devd;
|
|
|
|
}
|
|
|
|
tried_devd = now;
|
|
|
|
}
|
|
|
|
}
|
2005-10-19 04:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devd_init(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_un devd_addr;
|
|
|
|
|
|
|
|
bzero(&devd_addr, sizeof(devd_addr));
|
2006-01-03 21:32:02 +00:00
|
|
|
if ((devd_pipe = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
|
2005-10-19 04:48:44 +00:00
|
|
|
if (vflag)
|
2006-01-03 21:32:02 +00:00
|
|
|
warn("%s(): socket()", __func__);
|
2005-10-19 04:48:44 +00:00
|
|
|
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));
|
2006-01-03 21:32:02 +00:00
|
|
|
if (connect(devd_pipe, (struct sockaddr *)&devd_addr,
|
2005-10-19 04:48:44 +00:00
|
|
|
sizeof(devd_addr)) == -1) {
|
2006-01-03 21:32:02 +00:00
|
|
|
if (vflag)
|
|
|
|
warn("%s(): connect()", __func__);
|
|
|
|
close(devd_pipe);
|
|
|
|
devd_pipe = -1;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(devd_pipe, F_SETFL, O_NONBLOCK) == -1) {
|
|
|
|
if (vflag)
|
|
|
|
warn("%s(): fcntl()", __func__);
|
|
|
|
close(devd_pipe);
|
2005-10-19 04:48:44 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
return (devd_pipe);
|
2005-10-19 04:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
devd_close(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
close(devd_pipe);
|
2006-01-03 21:32:02 +00:00
|
|
|
devd_pipe = -1;
|
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;
|
2008-06-22 17:52:57 +00:00
|
|
|
else if (strcmp(arg, "adaptive") == 0 || strcmp(arg, "adp") == 0)
|
2005-02-26 21:17:31 +00:00
|
|
|
*mode = MODE_ADAPTIVE;
|
2008-11-18 13:24:38 +00:00
|
|
|
else if (strcmp(arg, "hiadaptive") == 0 || strcmp(arg, "hadp") == 0)
|
|
|
|
*mode = MODE_HIADAPTIVE;
|
2005-02-26 21:17:31 +00:00
|
|
|
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[])
|
|
|
|
{
|
2006-01-03 21:32:02 +00:00
|
|
|
struct timeval timeout;
|
|
|
|
fd_set fdset;
|
|
|
|
int nfds;
|
2005-08-24 19:42:40 +00:00
|
|
|
struct pidfh *pfh = NULL;
|
2005-08-24 17:32:41 +00:00
|
|
|
const char *pidfile = NULL;
|
2008-11-18 15:48:23 +00:00
|
|
|
int freq, curfreq, initfreq, *freqs, i, j, *mwatts, numfreqs, load;
|
2005-10-19 04:48:44 +00:00
|
|
|
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. */
|
2008-11-18 13:24:38 +00:00
|
|
|
mode_ac = mode_none = MODE_HIADAPTIVE;
|
|
|
|
mode_battery = MODE_ADAPTIVE;
|
2005-02-26 21:17:31 +00:00
|
|
|
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-07-25 16:43:12 +00:00
|
|
|
/* User must be root to control frequencies. */
|
|
|
|
if (geteuid() != 0)
|
|
|
|
errx(1, "must be root to run");
|
|
|
|
|
2008-02-18 03:19:25 +00:00
|
|
|
while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:v")) != -1)
|
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);
|
2008-11-18 13:24:38 +00:00
|
|
|
if (cpu_running_mark <= 0 || cpu_running_mark > 100) {
|
2005-02-26 21:17:31 +00:00
|
|
|
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;
|
2008-11-18 13:24:38 +00:00
|
|
|
if (sysctlnametomib("kern.cp_times", cp_times_mib, &len))
|
|
|
|
err(1, "lookup kern.cp_times");
|
2005-02-26 21:17:31 +00:00
|
|
|
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");
|
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
/* Check if we can read the load and supported freqs. */
|
|
|
|
if (read_usage_times(NULL))
|
2005-02-26 21:17:31 +00:00
|
|
|
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");
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2005-10-24 18:34:54 +00:00
|
|
|
/* Decide whether to use ACPI or APM to read the AC line status. */
|
|
|
|
acline_init();
|
|
|
|
|
2006-01-03 21:32:02 +00:00
|
|
|
/*
|
|
|
|
* Exit cleanly on signals.
|
|
|
|
*/
|
|
|
|
signal(SIGINT, handle_sigs);
|
|
|
|
signal(SIGTERM, handle_sigs);
|
|
|
|
|
2008-11-18 15:48:23 +00:00
|
|
|
freq = initfreq = get_freq();
|
2008-11-18 13:24:38 +00:00
|
|
|
if (freq < 1)
|
|
|
|
freq = 1;
|
2005-02-26 21:17:31 +00:00
|
|
|
/* Main loop. */
|
|
|
|
for (;;) {
|
2006-01-03 21:32:02 +00:00
|
|
|
FD_ZERO(&fdset);
|
|
|
|
if (devd_pipe >= 0) {
|
|
|
|
FD_SET(devd_pipe, &fdset);
|
|
|
|
nfds = devd_pipe + 1;
|
|
|
|
} else {
|
|
|
|
nfds = 0;
|
|
|
|
}
|
|
|
|
timeout.tv_sec = poll_ival / 1000000;
|
|
|
|
timeout.tv_usec = poll_ival % 1000000;
|
|
|
|
select(nfds, &fdset, NULL, &fdset, &timeout);
|
2005-02-26 21:17:31 +00:00
|
|
|
|
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. */
|
2006-01-03 21:32:02 +00:00
|
|
|
acline_read();
|
|
|
|
switch (acline_status) {
|
2005-02-26 21:17:31 +00:00
|
|
|
case SRC_AC:
|
|
|
|
mode = mode_ac;
|
|
|
|
break;
|
|
|
|
case SRC_BATTERY:
|
|
|
|
mode = mode_battery;
|
|
|
|
break;
|
|
|
|
case SRC_UNKNOWN:
|
|
|
|
mode = mode_none;
|
|
|
|
break;
|
|
|
|
default:
|
2006-01-03 21:32:02 +00:00
|
|
|
errx(1, "invalid AC line status %d", acline_status);
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the current frequency. */
|
2008-11-18 13:24:38 +00:00
|
|
|
if ((curfreq = get_freq()) == 0)
|
2005-10-19 04:48:44 +00:00
|
|
|
continue;
|
2005-02-26 21:17:31 +00:00
|
|
|
|
2008-11-18 13:24:38 +00:00
|
|
|
i = get_freq_id(curfreq, freqs, numfreqs);
|
|
|
|
|
2005-04-10 19:02:29 +00:00
|
|
|
if (vflag) {
|
|
|
|
/* Keep a sum of all power actually used. */
|
2008-11-18 13:24:38 +00:00
|
|
|
if (mwatts[i] != -1)
|
2005-04-10 19:02:29 +00:00
|
|
|
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) {
|
2008-11-18 13:24:38 +00:00
|
|
|
freq = freqs[numfreqs - 1];
|
|
|
|
if (curfreq != freq) {
|
2005-02-26 21:17:31 +00:00
|
|
|
if (vflag) {
|
|
|
|
printf("now operating on %s power; "
|
|
|
|
"changing frequency to %d MHz\n",
|
2008-11-18 13:24:38 +00:00
|
|
|
modes[acline_status], freq);
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
2008-11-18 13:24:38 +00:00
|
|
|
if (set_freq(freq) != 0) {
|
2005-10-19 04:48:44 +00:00
|
|
|
warn("error setting CPU freq %d",
|
2008-11-18 13:24:38 +00:00
|
|
|
freq);
|
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) {
|
2008-11-18 13:24:38 +00:00
|
|
|
freq = freqs[0];
|
|
|
|
if (curfreq != freq) {
|
2005-02-26 21:17:31 +00:00
|
|
|
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",
|
2008-11-18 13:24:38 +00:00
|
|
|
modes[acline_status], freq);
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
2008-11-18 13:24:38 +00:00
|
|
|
if (set_freq(freq) != 0) {
|
2005-10-19 04:48:44 +00:00
|
|
|
warn("error setting CPU freq %d",
|
2008-11-18 13:24:38 +00:00
|
|
|
freq);
|
2005-10-19 04:48:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adaptive mode; get the current CPU usage times. */
|
2008-11-18 13:24:38 +00:00
|
|
|
if (read_usage_times(&load)) {
|
2005-10-19 04:48:44 +00:00
|
|
|
if (vflag)
|
|
|
|
warn("read_usage_times() failed");
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-18 13:24:38 +00:00
|
|
|
|
|
|
|
if (mode == MODE_ADAPTIVE) {
|
|
|
|
if (load > cpu_running_mark) {
|
|
|
|
if (load > 95 || load > cpu_running_mark * 2)
|
|
|
|
freq *= 2;
|
|
|
|
else
|
|
|
|
freq = freq * load / cpu_running_mark;
|
|
|
|
if (freq > freqs[0])
|
|
|
|
freq = freqs[0];
|
|
|
|
} else if (load < cpu_idle_mark &&
|
|
|
|
curfreq * load < freqs[get_freq_id(
|
|
|
|
freq * 7 / 8, freqs, numfreqs)] *
|
|
|
|
cpu_running_mark) {
|
|
|
|
freq = freq * 7 / 8;
|
|
|
|
if (freq < freqs[numfreqs - 1])
|
|
|
|
freq = freqs[numfreqs - 1];
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
2008-11-18 13:24:38 +00:00
|
|
|
} else { /* MODE_HIADAPTIVE */
|
|
|
|
if (load > cpu_running_mark / 2) {
|
|
|
|
if (load > 95 || load > cpu_running_mark)
|
|
|
|
freq *= 4;
|
|
|
|
else
|
|
|
|
freq = freq * load * 2 / cpu_running_mark;
|
|
|
|
if (freq > freqs[0] * 2)
|
|
|
|
freq = freqs[0] * 2;
|
|
|
|
} else if (load < cpu_idle_mark / 2 &&
|
|
|
|
curfreq * load < freqs[get_freq_id(
|
|
|
|
freq * 31 / 32, freqs, numfreqs)] *
|
|
|
|
cpu_running_mark / 2) {
|
|
|
|
freq = freq * 31 / 32;
|
|
|
|
if (freq < freqs[numfreqs - 1])
|
|
|
|
freq = freqs[numfreqs - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (vflag) {
|
|
|
|
printf("load %3d%%, current freq %4d MHz (%2d), wanted freq %4d MHz\n",
|
|
|
|
load, curfreq, i, freq);
|
|
|
|
}
|
|
|
|
j = get_freq_id(freq, freqs, numfreqs);
|
|
|
|
if (i != j) {
|
2005-02-26 21:17:31 +00:00
|
|
|
if (vflag) {
|
2008-11-18 13:24:38 +00:00
|
|
|
printf("changing clock"
|
2005-02-26 21:17:31 +00:00
|
|
|
" speed from %d MHz to %d MHz\n",
|
2008-11-18 13:24:38 +00:00
|
|
|
freqs[i], freqs[j]);
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
2008-11-18 13:24:38 +00:00
|
|
|
if (set_freq(freqs[j]))
|
2005-10-19 04:48:44 +00:00
|
|
|
warn("error setting CPU frequency %d",
|
2008-11-18 13:24:38 +00:00
|
|
|
freqs[j]);
|
2005-02-26 21:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-18 15:48:23 +00:00
|
|
|
if (set_freq(initfreq))
|
|
|
|
warn("error setting CPU frequency %d", initfreq);
|
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);
|
|
|
|
}
|