Merge branch 'vendor/OPENSSL'
[dragonfly.git] / usr.sbin / powerd / powerd.c
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * The powerd daemon monitors the cpu load and adjusts cpu frequencies
37  * via hw.acpi.cpu.px_dom*.
38  */
39
40 #define _KERNEL_STRUCTURES
41 #include <sys/types.h>
42 #include <sys/sysctl.h>
43 #include <sys/kinfo.h>
44 #include <sys/file.h>
45 #include <sys/time.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <syslog.h>
51
52 static void usage(void);
53 static double getcputime(double);
54 static void acpi_setcpufreq(int nstate);
55 static void setupdominfo(void);
56 static int has_battery(void);
57 static int mon_battery(void);
58
59 int DebugOpt;
60 int TurboOpt = 1;
61 int CpuLimit;           /* # of cpus at max frequency */
62 int DomLimit;           /* # of domains at max frequency */
63 int PowerFd;
64 int DomBeg;
65 int DomEnd;
66 int NCpus;
67 int CpuCount[256];      /* # of cpus in any given domain */
68 int CpuToDom[256];      /* domain a particular cpu belongs to */
69 int Hysteresis = 10;    /* percentage */
70 double TriggerUp = 0.25;/* single-cpu load to force max freq */
71 double TriggerDown; /* load per cpu to force the min freq */
72 static int BatLifeMin = 2; /* shutdown the box, if low on battery life */
73 static struct timespec BatLifePrevT;
74 static struct timespec BatLifeStartT;
75 static int BatLifeLinger = 60; /* unit: sec */
76 static int BatLifePollIntvl = 5; /* unit: sec */
77
78 static void sigintr(int signo);
79
80 int
81 main(int ac, char **av)
82 {
83         double qavg;
84         double uavg;    /* uavg - used for speeding up */
85         double davg;    /* davg - used for slowing down */
86         double srt;
87         double pollrate;
88         int ch;
89         int ustate;
90         int dstate;
91         int nstate;
92         char buf[64];
93         int monbat;
94
95         srt = 8.0;      /* time for samples - 8 seconds */
96         pollrate = 1.0; /* polling rate in seconds */
97
98         while ((ch = getopt(ac, av, "dp:r:tu:B:L:P:T:")) != -1) {
99                 switch(ch) {
100                 case 'd':
101                         DebugOpt = 1;
102                         break;
103                 case 'p':
104                         Hysteresis = (int)strtol(optarg, NULL, 10);
105                         break;
106                 case 'r':
107                         pollrate = strtod(optarg, NULL);
108                         break;
109                 case 't':
110                         TurboOpt = 0;
111                         break;
112                 case 'u':
113                         TriggerUp = (double)strtol(optarg, NULL, 10) / 100;
114                         break;
115                 case 'B':
116                         BatLifeMin = strtol(optarg, NULL, 10);
117                         break;
118                 case 'L':
119                         BatLifeLinger = strtol(optarg, NULL, 10);
120                         break;
121                 case 'P':
122                         BatLifePollIntvl = strtol(optarg, NULL, 10);
123                         break;
124                 case 'T':
125                         srt = strtod(optarg, NULL);
126                         break;
127                 default:
128                         usage();
129                         /* NOT REACHED */
130                 }
131         }
132         ac -= optind;
133         av += optind;
134
135         if (0 > Hysteresis || Hysteresis > 99) {
136                 fprintf(stderr, "Invalid hysteresis value\n");
137                 exit(1);
138         }
139
140         if (0 > TriggerUp || TriggerUp > 1) {
141                 fprintf(stderr, "Invalid load limit value\n");
142                 exit(1);
143         }
144
145         TriggerDown = TriggerUp - (TriggerUp * (double) Hysteresis / 100);
146
147         /*
148          * Make sure powerd is not already running.
149          */
150         PowerFd = open("/var/run/powerd.pid", O_CREAT|O_RDWR, 0644);
151         if (PowerFd < 0) {
152                 fprintf(stderr,
153                         "Cannot create /var/run/powerd.pid, "
154                         "continuing anyway\n");
155         } else {
156                 if (flock(PowerFd, LOCK_EX|LOCK_NB) < 0) {
157                         fprintf(stderr, "powerd is already running\n");
158                         exit(1);
159                 }
160         }
161
162         /*
163          * Demonize and set pid
164          */
165         if (DebugOpt == 0) {
166                 daemon(0, 0);
167                 openlog("powerd", LOG_CONS | LOG_PID, LOG_DAEMON);
168         }
169
170         if (PowerFd >= 0) {
171                 ftruncate(PowerFd, 0);
172                 snprintf(buf, sizeof(buf), "%d\n", (int)getpid());
173                 write(PowerFd, buf, strlen(buf));
174         }
175
176         /* Do we need to monitor battery life? */
177         monbat = has_battery();
178
179         /*
180          * Wait hw.acpi.cpu.px_dom* sysctl to be created by kernel
181          *
182          * Since hw.acpi.cpu.px_dom* creation is queued into ACPI
183          * taskqueue and ACPI taskqueue is shared across various
184          * ACPI modules, any delay in other modules may cause
185          * hw.acpi.cpu.px_dom* to be created at quite a later time
186          * (e.g. cmbat module's task could take quite a lot of time).
187          */
188         for (;;) {
189                 /*
190                  * Prime delta cputime calculation, make sure at least
191                  * dom0 exists.
192                  */
193                 getcputime(pollrate);
194
195                 setupdominfo();
196                 if (DomBeg >= DomEnd) {
197                         usleep((int)(pollrate * 1000000.0));
198                         continue;
199                 }
200
201                 DomLimit = DomEnd;
202                 CpuLimit = NCpus;
203                 break;
204         }
205
206         /*
207          * Set to maximum performance if killed.
208          */
209         signal(SIGINT, sigintr);
210         signal(SIGTERM, sigintr);
211         uavg = 0.0;
212         davg = 0.0;
213
214         srt = srt / pollrate;   /* convert to sample count */
215
216         if (DebugOpt)
217                 printf("samples for downgrading: %5.2f\n", srt);
218
219         /*
220          * Monitoring loop
221          *
222          * Calculate nstate, the number of cpus we wish to run at max
223          * frequency.  All remaining cpus will be set to their lowest
224          * frequency and mapped out of the user process scheduler.
225          */
226         for (;;) {
227                 qavg = getcputime(pollrate);
228                 uavg = (uavg * 2.0 + qavg) / 3.0;       /* speeding up */
229                 davg = (davg * srt + qavg) / (srt + 1); /* slowing down */
230                 if (davg < uavg)
231                         davg = uavg;
232
233                 ustate = uavg / TriggerUp;
234                 if (ustate < CpuLimit)
235                         ustate = uavg / TriggerDown;
236                 dstate = davg / TriggerUp;
237                 if (dstate < CpuLimit)
238                         dstate = davg / TriggerDown;
239
240                 nstate = (ustate > dstate) ? ustate : dstate;
241                 if (nstate > NCpus)
242                         nstate = NCpus;
243
244                 if (DebugOpt) {
245                         printf("\rqavg=%5.2f uavg=%5.2f davg=%5.2f "
246                                "%2d/%2d ncpus=%d\r",
247                                 qavg, uavg, davg,
248                                 CpuLimit, DomLimit, nstate);
249                         fflush(stdout);
250                 }
251                 if (nstate != CpuLimit)
252                         acpi_setcpufreq(nstate);
253                 if (monbat)
254                         monbat = mon_battery();
255                 usleep((int)(pollrate * 1000000.0));
256         }
257 }
258
259 static
260 void
261 sigintr(int signo __unused)
262 {
263         syslog(LOG_INFO, "killed, setting max and exiting");
264         acpi_setcpufreq(NCpus);
265         exit(1);
266 }
267
268 /*
269  * Figure out the domains and calculate the CpuCount[] and CpuToDom[]
270  * arrays.
271  */
272 static
273 void
274 setupdominfo(void)
275 {
276         char buf[64];
277         char members[1024];
278         char *str;
279         size_t msize;
280         int i;
281         int n;
282
283         for (i = 0; i < 256; ++i) {
284                 snprintf(buf, sizeof(buf),
285                          "hw.acpi.cpu.px_dom%d.available", i);
286                 if (sysctlbyname(buf, NULL, NULL, NULL, 0) >= 0)
287                         break;
288         }
289         DomBeg = i;
290
291         for (i = 255; i >= DomBeg; --i) {
292                 snprintf(buf, sizeof(buf),
293                          "hw.acpi.cpu.px_dom%d.available", i);
294                 if (sysctlbyname(buf, NULL, NULL, NULL, 0) >= 0) {
295                         ++i;
296                         break;
297                 }
298         }
299         DomEnd = i;
300
301         for (i = DomBeg; i < DomEnd; ++i) {
302                 snprintf(buf, sizeof(buf),
303                          "hw.acpi.cpu.px_dom%d.members", i);
304                 msize = sizeof(members);
305                 if (sysctlbyname(buf, members, &msize, NULL, 0) == 0) {
306                         members[msize] = 0;
307                         for (str = strtok(members, " "); str;
308                              str = strtok(NULL, " ")) {
309                                 n = -1;
310                                 sscanf(str, "cpu%d", &n);
311                                 if (n >= 0) {
312                                         ++NCpus;
313                                         ++CpuCount[i];
314                                         CpuToDom[n]= i;
315                                 }
316                         }
317                 }
318         }
319 }
320
321 /*
322  * Return the one-second cpu load.  One cpu at 100% will return a value
323  * of 1.0.  On a SMP system N cpus running at 100% will return a value of N.
324  */
325 static
326 double
327 getcputime(double pollrate)
328 {
329         static struct kinfo_cputime ocpu_time[64];
330         static struct kinfo_cputime ncpu_time[64];
331         size_t slen;
332         int ncpu;
333         int cpu;
334         uint64_t delta;
335
336         bcopy(ncpu_time, ocpu_time, sizeof(ncpu_time));
337         slen = sizeof(ncpu_time);
338         if (sysctlbyname("kern.cputime", &ncpu_time, &slen, NULL, 0) < 0) {
339                 fprintf(stderr, "kern.cputime sysctl not available\n");
340                 exit(1);
341         }
342         ncpu = slen / sizeof(ncpu_time[0]);
343         delta = 0;
344
345         for (cpu = 0; cpu < ncpu; ++cpu) {
346                 delta += (ncpu_time[cpu].cp_user + ncpu_time[cpu].cp_sys +
347                           ncpu_time[cpu].cp_nice + ncpu_time[cpu].cp_intr) -
348                          (ocpu_time[cpu].cp_user + ocpu_time[cpu].cp_sys +
349                           ocpu_time[cpu].cp_nice + ocpu_time[cpu].cp_intr);
350         }
351         return((double)delta / (pollrate * 1000000.0));
352 }
353
354 /*
355  * nstate is the requested number of cpus that we wish to run at full
356  * frequency.  We calculate how many domains we have to adjust to reach
357  * this goal.
358  *
359  * This function also sets the user scheduler global cpu mask.
360  */
361 static
362 void
363 acpi_setcpufreq(int nstate)
364 {
365         int ncpus = 0;
366         int increasing = (nstate > CpuLimit);
367         int dom;
368         int domBeg;
369         int domEnd;
370         int lowest;
371         int highest;
372         int desired;
373         int v;
374         char *sysid;
375         char *ptr;
376         char buf[256];
377         size_t buflen;
378         cpumask_t global_cpumask;
379
380         /*
381          * Calculate the ending domain if the number of operating cpus
382          * has increased.
383          *
384          * Calculate the starting domain if the number of operating cpus
385          * has decreased.
386          */
387         for (dom = DomBeg; dom < DomEnd; ++dom) {
388                 if (ncpus >= nstate)
389                         break;
390                 ncpus += CpuCount[dom];
391         }
392
393         syslog(LOG_INFO, "using %d cpus", nstate);
394
395         /*
396          * Set the mask of cpus the userland scheduler is allowed to use.
397          */
398         CPUMASK_ASSBMASK(global_cpumask, nstate);
399         sysctlbyname("kern.usched_global_cpumask", NULL, 0,
400                      &global_cpumask, sizeof(global_cpumask));
401
402         if (increasing) {
403                 domBeg = DomLimit;
404                 domEnd = dom;
405         } else {
406                 domBeg = dom;
407                 domEnd = DomLimit;
408         }
409         DomLimit = dom;
410         CpuLimit = nstate;
411
412         /*
413          * Adjust the cpu frequency
414          */
415         if (DebugOpt)
416                 printf("\n");
417         for (dom = domBeg; dom < domEnd; ++dom) {
418                 /*
419                  * Retrieve availability list
420                  */
421                 asprintf(&sysid, "hw.acpi.cpu.px_dom%d.available", dom);
422                 buflen = sizeof(buf) - 1;
423                 v = sysctlbyname(sysid, buf, &buflen, NULL, 0);
424                 free(sysid);
425                 if (v < 0)
426                         continue;
427                 buf[buflen] = 0;
428
429                 /*
430                  * Parse out the highest and lowest cpu frequencies
431                  */
432                 ptr = buf;
433                 highest = lowest = 0;
434                 while (ptr && (v = strtol(ptr, &ptr, 10)) > 0) {
435                         if (lowest == 0 || lowest > v)
436                                 lowest = v;
437                         if (highest == 0 || highest < v)
438                                 highest = v;
439                         /* 
440                          * Detect turbo mode
441                          */
442                         if ((highest - v == 1) && ! TurboOpt)
443                                 highest = v;
444
445                 }
446
447                 /*
448                  * Calculate the desired cpu frequency, test, and set.
449                  */
450                 desired = increasing ? highest : lowest;
451
452                 asprintf(&sysid, "hw.acpi.cpu.px_dom%d.select", dom);
453                 buflen = sizeof(v);
454                 v = 0;
455                 sysctlbyname(sysid, &v, &buflen, NULL, 0);
456                 {
457                         if (DebugOpt) {
458                                 printf("dom%d set frequency %d\n",
459                                        dom, desired);
460                         }
461                         sysctlbyname(sysid, NULL, NULL,
462                                      &desired, sizeof(desired));
463                 }
464                 free(sysid);
465         }
466 }
467
468 static
469 void
470 usage(void)
471 {
472         fprintf(stderr, "usage: powerd [-dt] [-p hysteresis] "
473             "[-u trigger_up] [-T sample_interval] [-r poll_interval] "
474             "[-B min_battery_life] [-L low_battery_linger] "
475             "[-P battery_poll_interval]\n");
476         exit(1);
477 }
478
479 #ifndef timespecsub
480 #define timespecsub(vvp, uvp)                                           \
481         do {                                                            \
482                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
483                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
484                 if ((vvp)->tv_nsec < 0) {                               \
485                         (vvp)->tv_sec--;                                \
486                         (vvp)->tv_nsec += 1000000000;                   \
487                 }                                                       \
488         } while (0)
489 #endif
490
491 #define BAT_SYSCTL_TIME_MAX     50000000 /* unit: nanosecond */
492
493 static int
494 has_battery(void)
495 {
496         struct timespec s, e;
497         size_t len;
498         int val;
499
500         clock_gettime(CLOCK_MONOTONIC_FAST, &s);
501         BatLifePrevT = s;
502         BatLifeStartT = s;
503
504         len = sizeof(val);
505         if (sysctlbyname("hw.acpi.acline", &val, &len, NULL, 0) < 0) {
506                 /* No AC line information */
507                 return 0;
508         }
509         clock_gettime(CLOCK_MONOTONIC_FAST, &e);
510
511         timespecsub(&e, &s);
512         if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
513                 /* hw.acpi.acline takes to long to be useful */
514                 syslog(LOG_NOTICE, "hw.acpi.acline takes too long");
515                 return 0;
516         }
517
518         clock_gettime(CLOCK_MONOTONIC_FAST, &s);
519         len = sizeof(val);
520         if (sysctlbyname("hw.acpi.battery.life", &val, &len, NULL, 0) < 0) {
521                 /* No battery life */
522                 return 0;
523         }
524         clock_gettime(CLOCK_MONOTONIC_FAST, &e);
525
526         timespecsub(&e, &s);
527         if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) {
528                 /* hw.acpi.battery.life takes to long to be useful */
529                 syslog(LOG_NOTICE, "hw.acpi.battery.life takes too long");
530                 return 0;
531         }
532         return 1;
533 }
534
535 static int
536 mon_battery(void)
537 {
538         struct timespec cur, ts;
539         int acline, life;
540         size_t len;
541
542         clock_gettime(CLOCK_MONOTONIC_FAST, &cur);
543         ts = cur;
544         timespecsub(&ts, &BatLifePrevT);
545         if (ts.tv_sec < BatLifePollIntvl)
546                 return 1;
547         BatLifePrevT = cur;
548
549         len = sizeof(acline);
550         if (sysctlbyname("hw.acpi.acline", &acline, &len, NULL, 0) < 0)
551                 return 1;
552         if (acline)
553                 return 1;
554
555         len = sizeof(life);
556         if (sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0) < 0)
557                 return 1;
558
559         if (BatLifeLinger > 0) {
560                 ts = cur;
561                 timespecsub(&ts, &BatLifeStartT);
562                 if (ts.tv_sec > BatLifeLinger)
563                         BatLifeLinger = 0;
564         }
565
566         if (life <= BatLifeMin) {
567                 if (BatLifeLinger <= 0) {
568                         syslog(LOG_ALERT, "low battery life %d%%, "
569                             "shutting down", life);
570                         if (vfork() == 0)
571                                 execlp("poweroff", "poweroff", NULL);
572                         return 0;
573                 } else {
574                         static int logged;
575
576                         if (!logged) {
577                                 logged = 1;
578                                 syslog(LOG_ALERT, "low battery life %d%%, "
579                                     "please plugin AC line", life);
580                         }
581                 }
582         }
583         return 1;
584 }