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 #include <sys/types.h>
41 #include <sys/sysctl.h>
42 #include <sys/kinfo.h>
50 static void usage(void);
51 static double getcputime(void);
52 static void acpi_setcpufreq(int nstate);
53 static void setupdominfo(void);
56 int CpuLimit; /* # of cpus at max frequency */
57 int DomLimit; /* # of domains at max frequency */
62 int CpuCount[256]; /* # of cpus in any given domain */
63 int CpuToDom[256]; /* domain a particular cpu belongs to */
64 double Trigger = 0.25; /* load per cpu to force max freq */
67 main(int ac, char **av)
75 while ((ch = getopt(ac, av, "d")) != -1) {
89 * Prime delta cputime calculation, make sure at least dom0 exists,
90 * and make sure powerd is not already running.
96 if (DomBeg >= DomEnd) {
97 fprintf(stderr, "hw.acpi.cpu.px_dom* sysctl not available\n");
103 PowerFd = open("/var/run/powerd.pid", O_CREAT|O_RDWR, 0644);
106 "Cannot create /var/run/powerd.pid, "
107 "continuing anyway\n");
109 if (flock(PowerFd, LOCK_EX|LOCK_NB) < 0) {
110 fprintf(stderr, "powerd is already running\n");
116 * Demonize and set pid
120 openlog("powerd", LOG_CONS | LOG_PID, LOG_DAEMON);
124 ftruncate(PowerFd, 0);
125 snprintf(buf, sizeof(buf), "%d\n", (int)getpid());
126 write(PowerFd, buf, strlen(buf));
132 * Calculate nstate, the number of cpus we wish to run at max
133 * frequency. All remaining cpus will be set to their lowest
134 * frequency and mapped out of the user process scheduler.
138 savg = (savg * 7.0 + qavg) / 8.0;
140 nstate = savg / Trigger;
144 printf("\rqavg=%5.2f savg=%5.2f %2d/%2d ncpus=%d\r",
145 qavg, savg, CpuLimit, DomLimit, nstate);
148 if (nstate != CpuLimit)
149 acpi_setcpufreq(nstate);
155 * Figure out the domains and calculate the CpuCount[] and CpuToDom[]
169 for (i = 0; i < 256; ++i) {
170 snprintf(buf, sizeof(buf),
171 "hw.acpi.cpu.px_dom%d.available", i);
172 if (sysctlbyname(buf, NULL, NULL, NULL, 0) >= 0)
177 for (i = 255; i >= DomBeg; --i) {
178 snprintf(buf, sizeof(buf),
179 "hw.acpi.cpu.px_dom%d.available", i);
180 if (sysctlbyname(buf, NULL, NULL, NULL, 0) >= 0) {
187 for (i = DomBeg; i < DomEnd; ++i) {
188 snprintf(buf, sizeof(buf),
189 "hw.acpi.cpu.px_dom%d.members", i);
190 msize = sizeof(members);
191 if (sysctlbyname(buf, members, &msize, NULL, 0) == 0) {
193 for (str = strtok(members, " "); str;
194 str = strtok(NULL, " ")) {
196 sscanf(str, "cpu%d", &n);
208 * Return the one-second cpu load. One cpu at 100% will return a value
209 * of 1.0. On a SMP system N cpus running at 100% will return a value of N.
215 static struct kinfo_cputime ocpu_time[64];
216 static struct kinfo_cputime ncpu_time[64];
222 bcopy(ncpu_time, ocpu_time, sizeof(ncpu_time));
223 slen = sizeof(ncpu_time);
224 if (sysctlbyname("kern.cputime", &ncpu_time, &slen, NULL, 0) < 0) {
225 fprintf(stderr, "kern.cputime sysctl not available\n");
228 ncpu = slen / sizeof(ncpu_time[0]);
231 for (cpu = 0; cpu < ncpu; ++cpu) {
232 delta += (ncpu_time[cpu].cp_user + ncpu_time[cpu].cp_sys +
233 ncpu_time[cpu].cp_intr) -
234 (ocpu_time[cpu].cp_user + ocpu_time[cpu].cp_sys +
235 ocpu_time[cpu].cp_intr);
237 return((double)delta / 1000000.0);
241 * nstate is the requested number of cpus that we wish to run at full
242 * frequency. We calculate how many domains we have to adjust to reach
245 * This function also sets the user scheduler global cpu mask.
249 acpi_setcpufreq(int nstate)
252 int increasing = (nstate > CpuLimit);
264 cpumask_t global_cpumask;
267 * Calculate the ending domain if the number of operating cpus
270 * Calculate the starting domain if the number of operating cpus
273 for (dom = DomBeg; dom < DomEnd; ++dom) {
276 ncpus += CpuCount[dom];
279 syslog(LOG_INFO, "using %d cpus", nstate);
282 * Set the mask of cpus the userland scheduler is allowed to use.
284 global_cpumask = (1L << nstate) - 1;
285 sysctlbyname("kern.usched_global_cpumask", NULL, 0,
286 &global_cpumask, sizeof(global_cpumask));
299 * Adjust the cpu frequency
303 for (dom = domBeg; dom < domEnd; ++dom) {
305 * Retrieve availability list
307 asprintf(&sysid, "hw.acpi.cpu.px_dom%d.available", dom);
308 buflen = sizeof(buf) - 1;
309 v = sysctlbyname(sysid, buf, &buflen, NULL, 0);
316 * Parse out the highest and lowest cpu frequencies
319 highest = lowest = 0;
320 while (ptr && (v = strtol(ptr, &ptr, 10)) > 0) {
321 if (lowest == 0 || lowest > v)
323 if (highest == 0 || highest < v)
328 * Calculate the desired cpu frequency, test, and set.
330 desired = increasing ? highest : lowest;
332 asprintf(&sysid, "hw.acpi.cpu.px_dom%d.select", dom);
335 sysctlbyname(sysid, &v, &buflen, NULL, 0);
338 printf("dom%d set frequency %d\n",
341 sysctlbyname(sysid, NULL, NULL,
342 &desired, sizeof(desired));
352 fprintf(stderr, "usage: powerd [-d]\n");