2 * Copyright (c) 2010,2016 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
37 * - Monitor the cpu load and adjusts cpu and cpu power domain
38 * performance accordingly.
39 * - Monitor battery life. Alarm alerts and shutdown the machine
40 * if battery life goes low.
43 #define _KERNEL_STRUCTURES
44 #include <sys/types.h>
45 #include <sys/sysctl.h>
46 #include <sys/kinfo.h>
48 #include <sys/queue.h>
49 #include <sys/soundcard.h>
50 #include <sys/sensors.h>
52 #include <machine/cpufunc.h>
53 #include <machine/cpumask.h>
64 #define MAXDOM MAXCPU /* worst case, 1 cpu per domain */
69 #define NFREQ_MONPERF 0x0001
70 #define NFREQ_ADJPERF 0x0002
71 #define NFREQ_CPUTEMP 0x0004
73 #define NFREQ_ALL (NFREQ_MONPERF | NFREQ_ADJPERF | NFREQ_CPUTEMP)
76 TAILQ_ENTRY(cpu_pwrdom
) dom_link
;
79 cpumask_t dom_cpumask
;
84 double cpu_uavg
; /* used for speeding up */
85 double cpu_davg
; /* used for slowing down */
91 static void usage(void);
92 static void get_ncpus(void);
93 static void mon_cputemp(void);
96 static void get_uschedcpus(void);
97 static void set_uschedcpus(void);
100 static int has_perfbias(void);
101 static void set_perfbias(int, int);
103 /* acpi(4) P-state */
104 static void acpi_getcpufreq_str(int, int *, int *);
105 static int acpi_getcpufreq_bin(int, int *, int *);
106 static void acpi_get_cpufreq(int, int *, int *);
107 static void acpi_set_cpufreq(int, int);
108 static int acpi_get_cpupwrdom(void);
110 /* mwait C-state hint */
111 static int probe_cstate(void);
112 static void set_cstate(int, int);
114 /* Performance monitoring */
115 static void init_perf(void);
116 static void mon_perf(double);
117 static void adj_perf(cpumask_t
, cpumask_t
);
118 static void adj_cpu_pwrdom(int, int);
119 static void adj_cpu_perf(int, int);
120 static void get_cputime(double);
121 static int get_nstate(struct cpu_state
*, double);
122 static void add_spare_cpus(const cpumask_t
, int);
123 static void restore_perf(void);
124 static void set_global_freq(int freq
);
126 /* Battery monitoring */
127 static int has_battery(void);
128 static int mon_battery(void);
129 static void low_battery_alert(int);
132 static void restore_backlight(void);
134 /* Runtime states for performance monitoring */
135 static int global_pcpu_limit
;
136 static struct cpu_state pcpu_state
[MAXCPU
];
137 static struct cpu_state global_cpu_state
;
138 static cpumask_t cpu_used
; /* cpus w/ high perf */
139 static cpumask_t cpu_pwrdom_used
; /* cpu power domains w/ high perf */
140 static cpumask_t usched_cpu_used
; /* cpus for usched */
143 static cpumask_t cpu_pwrdom_mask
; /* usable cpu power domains */
144 static int cpu2pwrdom
[MAXCPU
]; /* cpu to cpu power domain map */
145 static struct cpu_pwrdom
*cpu_pwrdomain
[MAXDOM
];
146 static int NCpus
; /* # of cpus */
147 static char orig_global_cx
[CST_STRLEN
];
148 static char cpu_perf_cx
[CST_STRLEN
];
149 static int cpu_perf_cxlen
;
150 static char cpu_idle_cx
[CST_STRLEN
];
151 static int cpu_idle_cxlen
;
152 static int FreqAry
[MAXFREQ
];
154 static int NFreqChanged
= NFREQ_ALL
;
155 static int SavedPXGlobal
;
158 static int TurboOpt
= 1;
160 static int Hysteresis
= 10; /* percentage */
161 static double TriggerUp
= 0.25; /* single-cpu load to force max freq */
162 static double TriggerDown
; /* load per cpu to force the min freq */
163 static int HasPerfbias
= 0;
164 static int AdjustCpuFreq
= 1;
165 static int AdjustCstate
= 0;
166 static int HighestCpuFreq
;
167 static int LowestCpuFreq
;
169 static int AdjustCpuFreqOverride
;
171 static volatile int stopped
;
173 /* Battery life monitoring */
174 static int BatLifeMin
= 2; /* shutdown the box, if low on battery life */
175 static struct timespec BatLifePrevT
;
176 static int BatLifePollIntvl
= 5; /* unit: sec */
177 static struct timespec BatShutdownStartT
;
178 static int BatShutdownLinger
= -1;
179 static int BatShutdownLingerSet
= 60; /* unit: sec */
180 static int BatShutdownLingerCnt
;
181 static int BatShutdownAudioAlert
= 1;
182 static int MinTemp
= 75;
183 static int MaxTemp
= 85;
184 static int BackLightPct
= 100;
185 static int OldBackLightLevel
;
186 static int BackLightDown
;
188 static void sigintr(int signo
);
191 main(int ac
, char **av
)
202 srt
= 8.0; /* time for samples - 8 seconds */
203 pollrate
= 1.0; /* polling rate in seconds */
205 while ((ch
= getopt(ac
, av
, "b:cdefh:l:p:r:tu:B:H:L:P:QT:")) != -1) {
208 BackLightPct
= strtol(optarg
, NULL
, 10);
223 HighestCpuFreq
= strtol(optarg
, NULL
, 10);
226 LowestCpuFreq
= strtol(optarg
, NULL
, 10);
229 Hysteresis
= (int)strtol(optarg
, NULL
, 10);
232 pollrate
= strtod(optarg
, NULL
);
238 TriggerUp
= (double)strtol(optarg
, NULL
, 10) / 100;
241 BatLifeMin
= strtol(optarg
, NULL
, 10);
244 MaxTemp
= strtol(optarg
, &p2
, 0);
247 MaxTemp
= strtol(p2
+ 1, NULL
, 0);
249 MinTemp
= MaxTemp
* 9 / 10;
253 BatShutdownLingerSet
= strtol(optarg
, NULL
, 10);
254 if (BatShutdownLingerSet
< 0)
255 BatShutdownLingerSet
= 0;
258 BatLifePollIntvl
= strtol(optarg
, NULL
, 10);
261 BatShutdownAudioAlert
= 0;
264 srt
= strtod(optarg
, NULL
);
276 /* Get number of cpus */
280 acpi_get_cpufreq(0, &lowest
, &highest
);
282 if (Hysteresis
< 0 || Hysteresis
> 99) {
283 fprintf(stderr
, "Invalid hysteresis value\n");
287 if (TriggerUp
< 0 || TriggerUp
> 1) {
288 fprintf(stderr
, "Invalid load limit value\n");
292 if (BackLightPct
> 100 || BackLightPct
<= 0) {
293 fprintf(stderr
, "Invalid backlight setting, ignore\n");
297 TriggerDown
= TriggerUp
- (TriggerUp
* (double) Hysteresis
/ 100);
300 * Make sure powerd is not already running.
302 PowerFd
= open("/var/run/powerd.pid", O_CREAT
|O_RDWR
, 0644);
305 "Cannot create /var/run/powerd.pid, "
306 "continuing anyway\n");
311 r
= read(PowerFd
, buf
, sizeof(buf
) - 1);
314 pid
= strtol(buf
, NULL
, 0);
316 if (flock(PowerFd
, LOCK_EX
|LOCK_NB
) < 0) {
319 flock(PowerFd
, LOCK_EX
);
320 fprintf(stderr
, "restarting powerd\n");
323 "powerd is already running, "
324 "unable to kill pid for restart\n");
328 lseek(PowerFd
, 0L, 0);
332 * Demonize and set pid
336 openlog("powerd", LOG_CONS
| LOG_PID
, LOG_DAEMON
);
340 ftruncate(PowerFd
, 0);
341 snprintf(buf
, sizeof(buf
), "%d\n", (int)getpid());
342 write(PowerFd
, buf
, strlen(buf
));
345 /* Do we need to monitor battery life? */
346 if (BatLifePollIntvl
<= 0)
349 monbat
= has_battery();
351 /* Do we have perfbias(4)? */
353 HasPerfbias
= has_perfbias();
355 /* Could we adjust C-state? */
357 AdjustCstate
= probe_cstate();
360 * Wait hw.acpi.cpu.px_dom* sysctl to be created by kernel.
362 * Since hw.acpi.cpu.px_dom* creation is queued into ACPI
363 * taskqueue and ACPI taskqueue is shared across various
364 * ACPI modules, any delay in other modules may cause
365 * hw.acpi.cpu.px_dom* to be created at quite a later time
366 * (e.g. cmbat module's task could take quite a lot of time).
369 /* Prime delta cputime calculation. */
370 get_cputime(pollrate
);
372 /* Wait for all cpus to appear */
373 if (acpi_get_cpupwrdom())
375 usleep((int)(pollrate
* 1000000.0));
379 * Catch some signals so that max performance could be restored.
381 signal(SIGINT
, sigintr
);
382 signal(SIGTERM
, sigintr
);
384 /* Initialize performance states */
387 srt
= srt
/ pollrate
; /* convert to sample count */
389 printf("samples for downgrading: %5.2f\n", srt
);
396 * Monitor performance
398 get_cputime(pollrate
);
406 monbat
= mon_battery();
408 usleep((int)(pollrate
* 1000000.0));
412 * Set to maximum performance if killed.
414 syslog(LOG_INFO
, "killed, setting max and exiting");
416 set_global_freq(SavedPXGlobal
);
424 sigintr(int signo __unused
)
430 * Figure out the cpu power domains.
433 acpi_get_cpupwrdom(void)
435 struct cpu_pwrdom
*dom
;
436 cpumask_t pwrdom_mask
;
441 int n
, i
, ncpu
= 0, dom_id
;
443 memset(cpu2pwrdom
, 0, sizeof(cpu2pwrdom
));
444 memset(cpu_pwrdomain
, 0, sizeof(cpu_pwrdomain
));
445 CPUMASK_ASSZERO(cpu_pwrdom_mask
);
447 for (i
= 0; i
< MAXDOM
; ++i
) {
448 snprintf(buf
, sizeof(buf
),
449 "hw.acpi.cpu.px_dom%d.available", i
);
450 if (sysctlbyname(buf
, NULL
, NULL
, NULL
, 0) < 0)
453 dom
= calloc(1, sizeof(*dom
));
456 if (cpu_pwrdomain
[i
] != NULL
) {
457 fprintf(stderr
, "cpu power domain %d exists\n", i
);
460 cpu_pwrdomain
[i
] = dom
;
461 CPUMASK_ORBIT(cpu_pwrdom_mask
, i
);
463 pwrdom_mask
= cpu_pwrdom_mask
;
465 while (CPUMASK_TESTNZERO(pwrdom_mask
)) {
466 dom_id
= BSFCPUMASK(pwrdom_mask
);
467 CPUMASK_NANDBIT(pwrdom_mask
, dom_id
);
468 dom
= cpu_pwrdomain
[dom_id
];
470 CPUMASK_ASSZERO(dom
->dom_cpumask
);
472 snprintf(buf
, sizeof(buf
),
473 "hw.acpi.cpu.px_dom%d.members", dom
->dom_id
);
474 msize
= sizeof(members
);
475 if (sysctlbyname(buf
, members
, &msize
, NULL
, 0) < 0) {
476 cpu_pwrdomain
[dom_id
] = NULL
;
482 for (str
= strtok(members
, " "); str
; str
= strtok(NULL
, " ")) {
484 sscanf(str
, "cpu%d", &n
);
488 CPUMASK_ORBIT(dom
->dom_cpumask
, n
);
489 cpu2pwrdom
[n
] = dom
->dom_id
;
492 if (dom
->dom_ncpus
== 0) {
493 cpu_pwrdomain
[dom_id
] = NULL
;
498 printf("dom%d cpumask: ", dom
->dom_id
);
499 for (i
= 0; i
< (int)NELEM(dom
->dom_cpumask
.ary
); ++i
) {
501 (uintmax_t)dom
->dom_cpumask
.ary
[i
]);
509 printf("Found %d cpus, expecting %d\n", ncpu
, NCpus
);
511 pwrdom_mask
= cpu_pwrdom_mask
;
512 while (CPUMASK_TESTNZERO(pwrdom_mask
)) {
513 dom_id
= BSFCPUMASK(pwrdom_mask
);
514 CPUMASK_NANDBIT(pwrdom_mask
, dom_id
);
515 dom
= cpu_pwrdomain
[dom_id
];
525 * Save per-cpu load and sum of per-cpu load.
528 get_cputime(double pollrate
)
530 static struct kinfo_cputime ocpu_time
[MAXCPU
];
531 static struct kinfo_cputime ncpu_time
[MAXCPU
];
537 bcopy(ncpu_time
, ocpu_time
, sizeof(struct kinfo_cputime
) * NCpus
);
539 slen
= sizeof(ncpu_time
);
540 if (sysctlbyname("kern.cputime", &ncpu_time
, &slen
, NULL
, 0) < 0) {
541 fprintf(stderr
, "kern.cputime sysctl not available\n");
544 ncpu
= slen
/ sizeof(ncpu_time
[0]);
547 for (cpu
= 0; cpu
< ncpu
; ++cpu
) {
550 d
= (ncpu_time
[cpu
].cp_user
+ ncpu_time
[cpu
].cp_sys
+
551 ncpu_time
[cpu
].cp_nice
+ ncpu_time
[cpu
].cp_intr
) -
552 (ocpu_time
[cpu
].cp_user
+ ocpu_time
[cpu
].cp_sys
+
553 ocpu_time
[cpu
].cp_nice
+ ocpu_time
[cpu
].cp_intr
);
554 pcpu_state
[cpu
].cpu_qavg
= (double)d
/ (pollrate
* 1000000.0);
558 global_cpu_state
.cpu_qavg
= (double)delta
/ (pollrate
* 1000000.0);
562 acpi_getcpufreq_str(int dom_id
, int *highest0
, int *lowest0
)
564 char buf
[256], sysid
[64];
567 int v
, highest
, lowest
;
571 * Retrieve availability list
573 snprintf(sysid
, sizeof(sysid
),
574 "hw.acpi.cpu.px_dom%d.available", dom_id
);
575 buflen
= sizeof(buf
) - 1;
576 if (sysctlbyname(sysid
, buf
, &buflen
, NULL
, 0) < 0)
581 * Parse out the highest and lowest cpu frequencies
584 highest
= lowest
= 0;
586 while (ptr
&& (v
= strtol(ptr
, &ptr
, 10)) > 0) {
587 if ((lowest
== 0 || lowest
> v
) &&
588 (LowestCpuFreq
<= 0 || v
>= LowestCpuFreq
))
590 if ((highest
== 0 || highest
< v
) &&
591 (HighestCpuFreq
<= 0 || v
<= HighestCpuFreq
))
596 if (!TurboOpt
&& highest
- v
== 1)
604 if (freqidx
> MAXFREQ
)
606 if (NFreq
!= freqidx
) {
608 NFreqChanged
= NFREQ_ALL
;
611 while (ptr
&& (v
= strtol(ptr
, &ptr
, 10)) > 0) {
614 if (FreqAry
[freqidx
- 1] != v
)
615 NFreqChanged
= NFREQ_ALL
;
616 FreqAry
[--freqidx
] = v
;
624 acpi_getcpufreq_bin(int dom_id
, int *highest0
, int *lowest0
)
629 int freqary
[MAXFREQ
];
632 * Retrieve availability list
634 snprintf(sysid
, sizeof(sysid
), "hw.acpi.cpu.px_dom%d.avail", dom_id
);
635 freqlen
= sizeof(FreqAry
);
636 bzero(freqary
, sizeof(freqary
));
637 if (sysctlbyname(sysid
, freqary
, &freqlen
, NULL
, 0) < 0)
640 freqcnt
= freqlen
/ sizeof(freqary
[0]);
641 if (NFreq
!= freqcnt
) {
643 NFreqChanged
= NFREQ_ALL
;
645 if (bcmp(freqary
, FreqAry
, sizeof(FreqAry
)) != 0)
646 NFreqChanged
= NFREQ_ALL
;
647 bcopy(freqary
, FreqAry
, sizeof(FreqAry
));
651 for (i
= freqcnt
- 1; i
>= 0; --i
) {
652 *lowest0
= FreqAry
[i
];
653 if (LowestCpuFreq
<= 0 || *lowest0
>= LowestCpuFreq
)
658 *highest0
= FreqAry
[0];
659 if (!TurboOpt
&& freqcnt
> 1 && FreqAry
[0] - FreqAry
[1] == 1) {
661 *highest0
= FreqAry
[1];
663 for (; i
< freqcnt
; ++i
) {
664 if (HighestCpuFreq
<= 0 || *highest0
<= HighestCpuFreq
)
666 *highest0
= FreqAry
[i
];
672 acpi_get_cpufreq(int dom_id
, int *highest
, int *lowest
)
677 if (acpi_getcpufreq_bin(dom_id
, highest
, lowest
))
679 acpi_getcpufreq_str(dom_id
, highest
, lowest
);
686 fprintf(stderr
, "usage: powerd [-cdeftQ] [-p hysteresis] "
687 "[-h highest_freq] [-l lowest_freq] "
688 "[-r poll_interval] [-u trigger_up] "
689 "[-B min_battery_life] [-L low_battery_linger] "
690 "[-P battery_poll_interval] [-T sample_interval] "
696 #define timespecsub(vvp, uvp) \
698 (vvp)->tv_sec -= (uvp)->tv_sec; \
699 (vvp)->tv_nsec -= (uvp)->tv_nsec; \
700 if ((vvp)->tv_nsec < 0) { \
702 (vvp)->tv_nsec += 1000000000; \
707 #define BAT_SYSCTL_TIME_MAX 50000000 /* unit: nanosecond */
712 struct timespec s
, e
;
716 clock_gettime(CLOCK_MONOTONIC_FAST
, &s
);
720 if (sysctlbyname("hw.acpi.acline", &val
, &len
, NULL
, 0) < 0) {
721 /* No AC line information */
724 clock_gettime(CLOCK_MONOTONIC_FAST
, &e
);
727 if (e
.tv_sec
> 0 || e
.tv_nsec
> BAT_SYSCTL_TIME_MAX
) {
728 /* hw.acpi.acline takes to long to be useful */
729 syslog(LOG_NOTICE
, "hw.acpi.acline takes too long");
733 clock_gettime(CLOCK_MONOTONIC_FAST
, &s
);
735 if (sysctlbyname("hw.acpi.battery.life", &val
, &len
, NULL
, 0) < 0) {
736 /* No battery life */
739 clock_gettime(CLOCK_MONOTONIC_FAST
, &e
);
742 if (e
.tv_sec
> 0 || e
.tv_nsec
> BAT_SYSCTL_TIME_MAX
) {
743 /* hw.acpi.battery.life takes to long to be useful */
744 syslog(LOG_NOTICE
, "hw.acpi.battery.life takes too long");
751 low_battery_alert(int life
)
753 int fmt
, stereo
, freq
;
756 syslog(LOG_ALERT
, "low battery life %d%%, please plugin AC line, #%d",
757 life
, BatShutdownLingerCnt
);
758 ++BatShutdownLingerCnt
;
760 if (!BatShutdownAudioAlert
)
763 fd
= open("/dev/dsp", O_WRONLY
);
768 if (ioctl(fd
, SNDCTL_DSP_SETFMT
, &fmt
, sizeof(fmt
)) < 0)
772 if (ioctl(fd
, SNDCTL_DSP_STEREO
, &stereo
, sizeof(stereo
)) < 0)
776 if (ioctl(fd
, SNDCTL_DSP_SPEED
, &freq
, sizeof(freq
)) < 0)
779 write(fd
, alert1
, sizeof(alert1
));
780 write(fd
, alert1
, sizeof(alert1
));
789 struct timespec cur
, ts
;
793 clock_gettime(CLOCK_MONOTONIC_FAST
, &cur
);
795 timespecsub(&ts
, &BatLifePrevT
);
796 if (ts
.tv_sec
< BatLifePollIntvl
)
800 len
= sizeof(acline
);
801 if (sysctlbyname("hw.acpi.acline", &acline
, &len
, NULL
, 0) < 0)
804 BatShutdownLinger
= -1;
805 BatShutdownLingerCnt
= 0;
810 if (!BackLightDown
&& BackLightPct
!= 100) {
811 int backlight_max
, backlight
;
813 len
= sizeof(backlight_max
);
814 if (sysctlbyname("hw.backlight_max", &backlight_max
, &len
,
816 /* No more backlight adjustment */
818 goto after_backlight
;
821 len
= sizeof(OldBackLightLevel
);
822 if (sysctlbyname("hw.backlight_level", &OldBackLightLevel
, &len
,
824 /* No more backlight adjustment */
826 goto after_backlight
;
829 backlight
= (backlight_max
* BackLightPct
) / 100;
830 if (backlight
>= OldBackLightLevel
) {
831 /* No more backlight adjustment */
833 goto after_backlight
;
836 if (sysctlbyname("hw.backlight_level", NULL
, NULL
,
837 &backlight
, sizeof(backlight
)) < 0) {
838 /* No more backlight adjustment */
840 goto after_backlight
;
847 if (sysctlbyname("hw.acpi.battery.life", &life
, &len
, NULL
, 0) < 0)
850 if (BatShutdownLinger
> 0) {
852 timespecsub(&ts
, &BatShutdownStartT
);
853 if (ts
.tv_sec
> BatShutdownLinger
)
854 BatShutdownLinger
= 0;
857 if (life
<= BatLifeMin
) {
858 if (BatShutdownLinger
== 0 || BatShutdownLingerSet
== 0) {
859 syslog(LOG_ALERT
, "low battery life %d%%, "
860 "shutting down", life
);
862 execlp("poweroff", "poweroff", NULL
);
864 } else if (BatShutdownLinger
< 0) {
865 BatShutdownLinger
= BatShutdownLingerSet
;
866 BatShutdownStartT
= cur
;
868 low_battery_alert(life
);
878 slen
= sizeof(NCpus
);
879 if (sysctlbyname("hw.ncpu", &NCpus
, &slen
, NULL
, 0) < 0)
880 err(1, "sysctlbyname hw.ncpu failed");
882 printf("hw.ncpu %d\n", NCpus
);
890 slen
= sizeof(usched_cpu_used
);
891 if (sysctlbyname("kern.usched_global_cpumask", &usched_cpu_used
, &slen
,
893 err(1, "sysctlbyname kern.usched_global_cpumask failed");
897 printf("usched cpumask was: ");
898 for (i
= 0; i
< (int)NELEM(usched_cpu_used
.ary
); ++i
)
899 printf("%jx ", (uintmax_t)usched_cpu_used
.ary
[i
]);
910 printf("usched cpumask: ");
911 for (i
= 0; i
< (int)NELEM(usched_cpu_used
.ary
); ++i
) {
913 (uintmax_t)usched_cpu_used
.ary
[i
]);
917 sysctlbyname("kern.usched_global_cpumask", NULL
, 0,
918 &usched_cpu_used
, sizeof(usched_cpu_used
));
928 if (sysctlbyname("machdep.perfbias0.hint", &hint
, &len
, NULL
, 0) < 0)
934 set_perfbias(int cpu
, int inc
)
936 int hint
= inc
? 0 : 15;
940 printf("cpu%d set perfbias hint %d\n", cpu
, hint
);
941 snprintf(sysid
, sizeof(sysid
), "machdep.perfbias%d.hint", cpu
);
942 sysctlbyname(sysid
, NULL
, NULL
, &hint
, sizeof(hint
));
948 struct cpu_state
*state
;
951 /* Get usched cpumask */
955 * Assume everything are used and are maxed out, before we
958 CPUMASK_ASSBMASK(cpu_used
, NCpus
);
959 cpu_pwrdom_used
= cpu_pwrdom_mask
;
960 global_pcpu_limit
= NCpus
;
962 for (cpu
= 0; cpu
< NCpus
; ++cpu
) {
963 state
= &pcpu_state
[cpu
];
965 state
->cpu_uavg
= 0.0;
966 state
->cpu_davg
= 0.0;
967 state
->cpu_limit
= 1;
968 state
->cpu_count
= 1;
969 snprintf(state
->cpu_name
, sizeof(state
->cpu_name
), "cpu%d",
973 state
= &global_cpu_state
;
974 state
->cpu_uavg
= 0.0;
975 state
->cpu_davg
= 0.0;
976 state
->cpu_limit
= NCpus
;
977 state
->cpu_count
= NCpus
;
978 strlcpy(state
->cpu_name
, "global", sizeof(state
->cpu_name
));
982 get_nstate(struct cpu_state
*state
, double srt
)
984 int ustate
, dstate
, nstate
;
987 state
->cpu_uavg
= (state
->cpu_uavg
* 2.0 + state
->cpu_qavg
) / 3.0;
989 state
->cpu_davg
= (state
->cpu_davg
* srt
+ state
->cpu_qavg
) / (srt
+ 1);
990 if (state
->cpu_davg
< state
->cpu_uavg
)
991 state
->cpu_davg
= state
->cpu_uavg
;
993 ustate
= state
->cpu_uavg
/ TriggerUp
;
994 if (ustate
< state
->cpu_limit
)
995 ustate
= state
->cpu_uavg
/ TriggerDown
;
996 dstate
= state
->cpu_davg
/ TriggerUp
;
997 if (dstate
< state
->cpu_limit
)
998 dstate
= state
->cpu_davg
/ TriggerDown
;
1000 nstate
= (ustate
> dstate
) ? ustate
: dstate
;
1001 if (nstate
> state
->cpu_count
)
1002 nstate
= state
->cpu_count
;
1005 printf("%s qavg=%5.2f uavg=%5.2f davg=%5.2f "
1006 "%2d ncpus=%d\n", state
->cpu_name
,
1007 state
->cpu_qavg
, state
->cpu_uavg
, state
->cpu_davg
,
1008 state
->cpu_limit
, nstate
);
1014 mon_perf(double srt
)
1016 cpumask_t ocpu_used
, ocpu_pwrdom_used
;
1017 int pnstate
= 0, nstate
;
1021 * Find cpus requiring performance and their cooresponding power
1022 * domains. Save the number of cpus requiring performance in
1025 ocpu_used
= cpu_used
;
1026 ocpu_pwrdom_used
= cpu_pwrdom_used
;
1028 CPUMASK_ASSZERO(cpu_used
);
1029 CPUMASK_ASSZERO(cpu_pwrdom_used
);
1031 for (cpu
= 0; cpu
< NCpus
; ++cpu
) {
1032 struct cpu_state
*state
= &pcpu_state
[cpu
];
1035 s
= get_nstate(state
, srt
);
1037 CPUMASK_ORBIT(cpu_used
, cpu
);
1038 CPUMASK_ORBIT(cpu_pwrdom_used
, cpu2pwrdom
[cpu
]);
1042 state
->cpu_limit
= s
;
1046 * Calculate nstate, the number of cpus we wish to run at max
1049 nstate
= get_nstate(&global_cpu_state
, srt
);
1051 if (nstate
== global_cpu_state
.cpu_limit
&&
1052 (NFreqChanged
& NFREQ_MONPERF
) == 0 &&
1053 (pnstate
== global_pcpu_limit
|| nstate
> pnstate
)) {
1054 /* Nothing changed; keep the sets */
1055 cpu_used
= ocpu_used
;
1056 cpu_pwrdom_used
= ocpu_pwrdom_used
;
1058 global_pcpu_limit
= pnstate
;
1061 NFreqChanged
&= ~NFREQ_MONPERF
;
1062 global_pcpu_limit
= pnstate
;
1064 if (nstate
> pnstate
) {
1066 * Add spare cpus to meet global performance requirement.
1068 add_spare_cpus(ocpu_used
, nstate
- pnstate
);
1071 global_cpu_state
.cpu_limit
= nstate
;
1074 * Adjust cpu and cpu power domain performance
1076 adj_perf(ocpu_used
, ocpu_pwrdom_used
);
1080 add_spare_cpus(const cpumask_t ocpu_used
, int ncpu
)
1082 cpumask_t saved_pwrdom
, xcpu_used
;
1086 * Find more cpus in the previous cpu set.
1088 xcpu_used
= cpu_used
;
1089 CPUMASK_XORMASK(xcpu_used
, ocpu_used
);
1090 while (CPUMASK_TESTNZERO(xcpu_used
)) {
1091 cpu
= BSFCPUMASK(xcpu_used
);
1092 CPUMASK_NANDBIT(xcpu_used
, cpu
);
1094 if (CPUMASK_TESTBIT(ocpu_used
, cpu
)) {
1095 CPUMASK_ORBIT(cpu_pwrdom_used
, cpu2pwrdom
[cpu
]);
1096 CPUMASK_ORBIT(cpu_used
, cpu
);
1104 * Find more cpus in the used cpu power domains.
1106 saved_pwrdom
= cpu_pwrdom_used
;
1108 while (CPUMASK_TESTNZERO(saved_pwrdom
)) {
1109 cpumask_t unused_cpumask
;
1112 dom
= BSFCPUMASK(saved_pwrdom
);
1113 CPUMASK_NANDBIT(saved_pwrdom
, dom
);
1115 unused_cpumask
= cpu_pwrdomain
[dom
]->dom_cpumask
;
1116 CPUMASK_NANDMASK(unused_cpumask
, cpu_used
);
1118 while (CPUMASK_TESTNZERO(unused_cpumask
)) {
1119 cpu
= BSFCPUMASK(unused_cpumask
);
1120 CPUMASK_NANDBIT(unused_cpumask
, cpu
);
1122 CPUMASK_ORBIT(cpu_pwrdom_used
, dom
);
1123 CPUMASK_ORBIT(cpu_used
, cpu
);
1132 * Find more cpus in unused cpu power domains
1134 saved_pwrdom
= cpu_pwrdom_mask
;
1135 CPUMASK_NANDMASK(saved_pwrdom
, cpu_pwrdom_used
);
1139 printf("%d cpus not found\n", ncpu
);
1143 acpi_set_cpufreq(int dom
, int inc
)
1145 int lowest
, highest
, desired
;
1148 acpi_get_cpufreq(dom
, &highest
, &lowest
);
1149 if (highest
== 0 || lowest
== 0)
1151 desired
= inc
? highest
: lowest
;
1154 printf("dom%d set frequency %d\n", dom
, desired
);
1155 snprintf(sysid
, sizeof(sysid
), "hw.acpi.cpu.px_dom%d.select", dom
);
1156 sysctlbyname(sysid
, NULL
, NULL
, &desired
, sizeof(desired
));
1160 adj_cpu_pwrdom(int dom
, int inc
)
1162 if (AdjustCpuFreq
&& (inc
== 0 || AdjustCpuFreqOverride
== 0))
1163 acpi_set_cpufreq(dom
, inc
);
1167 adj_cpu_perf(int cpu
, int inc
)
1171 printf("cpu%d increase perf\n", cpu
);
1173 printf("cpu%d decrease perf\n", cpu
);
1177 set_perfbias(cpu
, inc
);
1179 set_cstate(cpu
, inc
);
1183 adj_perf(cpumask_t xcpu_used
, cpumask_t xcpu_pwrdom_used
)
1185 cpumask_t old_usched_used
;
1189 * Set cpus requiring performance to the userland process
1190 * scheduler. Leave the rest of cpus unmapped.
1192 old_usched_used
= usched_cpu_used
;
1193 usched_cpu_used
= cpu_used
;
1194 if (CPUMASK_TESTZERO(usched_cpu_used
))
1195 CPUMASK_ORBIT(usched_cpu_used
, 0);
1196 if (CPUMASK_CMPMASKNEQ(usched_cpu_used
, old_usched_used
))
1200 * Adjust per-cpu performance for any cpus which changed.
1202 CPUMASK_XORMASK(xcpu_used
, cpu_used
);
1203 if (NFreqChanged
& NFREQ_ADJPERF
)
1204 CPUMASK_ASSBMASK(xcpu_used
, NCpus
);
1205 while (CPUMASK_TESTNZERO(xcpu_used
)) {
1206 cpu
= BSFCPUMASK(xcpu_used
);
1207 CPUMASK_NANDBIT(xcpu_used
, cpu
);
1209 if (CPUMASK_TESTBIT(cpu_used
, cpu
)) {
1210 /* Increase cpu performance */
1213 /* Decrease cpu performance */
1216 adj_cpu_perf(cpu
, inc
);
1220 * Adjust cpu power domain performance. This could affect
1223 CPUMASK_XORMASK(xcpu_pwrdom_used
, cpu_pwrdom_used
);
1224 if (NFreqChanged
& NFREQ_ADJPERF
)
1225 CPUMASK_ASSBMASK(xcpu_pwrdom_used
, NCpus
);
1226 while (CPUMASK_TESTNZERO(xcpu_pwrdom_used
)) {
1229 dom
= BSFCPUMASK(xcpu_pwrdom_used
);
1230 CPUMASK_NANDBIT(xcpu_pwrdom_used
, dom
);
1232 if (CPUMASK_TESTBIT(cpu_pwrdom_used
, dom
)) {
1233 /* Increase cpu power domain performance */
1236 /* Decrease cpu power domain performance */
1239 adj_cpu_pwrdom(dom
, inc
);
1241 NFreqChanged
&= ~NFREQ_ADJPERF
;
1247 cpumask_t ocpu_used
, ocpu_pwrdom_used
;
1249 /* Remove highest cpu frequency limitation */
1252 ocpu_used
= cpu_used
;
1253 ocpu_pwrdom_used
= cpu_pwrdom_used
;
1255 /* Max out all cpus and cpu power domains performance */
1256 CPUMASK_ASSBMASK(cpu_used
, NCpus
);
1257 cpu_pwrdom_used
= cpu_pwrdom_mask
;
1259 adj_perf(ocpu_used
, ocpu_pwrdom_used
);
1263 * Restore the original mwait C-state
1266 printf("global set cstate %s\n", orig_global_cx
);
1267 sysctlbyname("machdep.mwait.CX.idle", NULL
, NULL
,
1268 orig_global_cx
, strlen(orig_global_cx
) + 1);
1275 char cx_supported
[1024];
1278 int idle_hlt
, deep
= 1;
1281 len
= sizeof(idle_hlt
);
1282 if (sysctlbyname("machdep.cpu_idle_hlt", &idle_hlt
, &len
, NULL
, 0) < 0)
1287 len
= sizeof(cx_supported
);
1288 if (sysctlbyname("machdep.mwait.CX.supported", cx_supported
, &len
,
1292 len
= sizeof(orig_global_cx
);
1293 if (sysctlbyname("machdep.mwait.CX.idle", orig_global_cx
, &len
,
1297 strlcpy(cpu_perf_cx
, "AUTODEEP", sizeof(cpu_perf_cx
));
1298 cpu_perf_cxlen
= strlen(cpu_perf_cx
) + 1;
1299 if (sysctlbyname("machdep.mwait.CX.idle", NULL
, NULL
,
1300 cpu_perf_cx
, cpu_perf_cxlen
) < 0) {
1301 /* AUTODEEP is not supported; try AUTO */
1303 strlcpy(cpu_perf_cx
, "AUTO", sizeof(cpu_perf_cx
));
1304 cpu_perf_cxlen
= strlen(cpu_perf_cx
) + 1;
1305 if (sysctlbyname("machdep.mwait.CX.idle", NULL
, NULL
,
1306 cpu_perf_cx
, cpu_perf_cxlen
) < 0)
1314 for (ptr
= strtok(cx_supported
, " "); ptr
!= NULL
;
1315 ptr
= strtok(NULL
, " ")) {
1316 if (target
== NULL
||
1317 (target
!= NULL
&& strcmp(ptr
, target
) == 0)) {
1318 strlcpy(cpu_idle_cx
, ptr
, sizeof(cpu_idle_cx
));
1319 cpu_idle_cxlen
= strlen(cpu_idle_cx
) + 1;
1324 if (cpu_idle_cxlen
== 0)
1328 printf("cstate orig %s, perf %s, idle %s\n",
1329 orig_global_cx
, cpu_perf_cx
, cpu_idle_cx
);
1335 set_cstate(int cpu
, int inc
)
1343 len
= cpu_perf_cxlen
;
1346 len
= cpu_idle_cxlen
;
1350 printf("cpu%d set cstate %s\n", cpu
, cst
);
1351 snprintf(sysid
, sizeof(sysid
), "machdep.mwait.CX.idle%d", cpu
);
1352 sysctlbyname(sysid
, NULL
, NULL
, cst
, len
);
1356 restore_backlight(void)
1358 if (BackLightDown
) {
1360 sysctlbyname("hw.backlight_level", NULL
, NULL
,
1361 &OldBackLightLevel
, sizeof(OldBackLightLevel
));
1366 * get_cputemp() / mon_cputemp()
1368 * This enforces the maximum cpu frequency based on temperature
1369 * verses MinTemp and MaxTemp.
1375 struct sensor sensor
;
1381 for (n
= 0; ; ++n
) {
1383 snprintf(sysid
, sizeof(sysid
),
1384 "hw.sensors.cpu_node%d.temp0", n
);
1385 sensor_size
= sizeof(sensor
);
1386 if (sysctlbyname(sysid
, &sensor
, &sensor_size
, NULL
, 0) < 0)
1389 if ((sensor
.flags
& (SENSOR_FINVALID
| SENSOR_FUNKNOWN
)) == 0) {
1390 t
= (int)((sensor
.value
- 273150000) / 1000000);
1399 * Missing nodeN for some reason, try cpuN.
1401 for (n
= 0; ; ++n
) {
1403 snprintf(sysid
, sizeof(sysid
),
1404 "hw.sensors.cpu%d.temp0", n
);
1405 sensor_size
= sizeof(sensor
);
1406 if (sysctlbyname(sysid
, &sensor
, &sensor_size
, NULL
, 0) < 0)
1409 if ((sensor
.flags
& (SENSOR_FINVALID
| SENSOR_FUNKNOWN
)) == 0) {
1410 t
= (int)((sensor
.value
- 273150000) / 1000000);
1419 set_global_freq(int freq
)
1422 sysctlbyname("hw.acpi.cpu.px_global",
1423 NULL
, NULL
, &freq
, sizeof(freq
));
1427 get_global_freq(void)
1433 freq_size
= sizeof(freq
);
1434 sysctlbyname("hw.acpi.cpu.px_global", &freq
, &freq_size
, NULL
, 0);
1442 static int last_temp
= -1;
1443 static int last_idx
= -1;
1444 int temp
= get_cputemp();
1448 static int CurPXGlobal __unused
;
1451 * Reseed FreqAry, it can change w/AC power state
1453 acpi_get_cpufreq(0, &lowest
, &highest
);
1456 * Some cpu frequency steps can cause large shifts in cpu temperature,
1457 * creating an oscillation that min-maxes the temperature in a way
1458 * that is not desireable. To deal with this, we impose an exponential
1459 * average for any temperature change.
1461 * We have to do this in both directions, otherwise (in particular)
1462 * laptop fan responsiveness and temperature sensor response times
1463 * can create major frequency oscillations.
1465 if (last_temp
< 0 || (NFreqChanged
& NFREQ_CPUTEMP
)) {
1466 NFreqChanged
&= ~NFREQ_CPUTEMP
;
1467 last_temp
= temp
<< 8;
1468 } else if (temp
< last_temp
) {
1469 last_temp
= (last_temp
* 15 + (temp
<< 8)) / 16;
1471 printf("Falling temp %d (use %d)\n",
1472 temp
, (last_temp
>> 8));
1475 last_temp
= (last_temp
* 15 + (temp
<< 8)) / 16;
1477 printf("Rising temp %d (use %d)\n",
1478 temp
, (last_temp
>> 8));
1481 temp
= last_temp
>> 8;
1484 * CPU Temp not available or available frequencies not yet
1488 printf("Temp %d {%d-%d} NFreq=%d)\n",
1489 temp
, MinTemp
, MaxTemp
, NFreq
);
1496 * Return to normal operation if under the minimum
1498 if (temp
<= MinTemp
) {
1499 if (AdjustCpuFreqOverride
) {
1500 AdjustCpuFreqOverride
= 0;
1502 NFreqChanged
= NFREQ_ALL
;
1505 "Temp below %d, returning to normal operation",
1508 set_global_freq(SavedPXGlobal
);
1514 * Hysteresis before entering temperature control mode
1516 if (AdjustCpuFreqOverride
== 0 &&
1517 temp
<= MinTemp
+ (MaxTemp
- MinTemp
) / 10 + 1) {
1522 * Override frequency controls (except for idle -> lowest)
1524 if (AdjustCpuFreqOverride
== 0) {
1525 AdjustCpuFreqOverride
= 1;
1526 SavedPXGlobal
= get_global_freq();
1528 NFreqChanged
= NFREQ_ALL
;
1531 "Temp %d {%d-%d}, entering temperature control mode",
1532 temp
, MinTemp
, MaxTemp
);
1534 if (temp
> MaxTemp
+ (MaxTemp
- MinTemp
) / 10 + 1) {
1536 "Temp %d {%d-%d}, TOO HOT!!!",
1537 temp
, MinTemp
, MaxTemp
);
1539 idx
= (temp
- MinTemp
) * NFreq
/ (MaxTemp
- MinTemp
);
1540 if (idx
< 0 || idx
>= NFreq
) /* overtemp */
1544 * Limit frequency shifts to single steps in both directions.
1545 * Some fans react very quickly, this will reduce oscillations.
1548 printf("Temp index %d (use %d)\n", idx
, last_idx
);
1549 if (last_idx
>= 0 && idx
< last_idx
)
1551 else if (last_idx
>= 0 && idx
> last_idx
)
1556 * One last thing, make sure our frequency adheres to
1557 * HighestCpuFreq. However, override LowestCpuFreq for
1558 * temperature control purposes.
1560 while (HighestCpuFreq
> 0 && idx
< NFreq
&&
1561 FreqAry
[idx
] > HighestCpuFreq
) {
1566 * Currently ignore LowestCpuFreq if temp control thinks it
1569 while (LowestCpuFreq
> 0 && idx
> 0 &&
1570 FreqAry
[idx
] < LowestCpuFreq
) {
1575 if (FreqAry
[idx
] != CurPXGlobal
) {
1576 CurPXGlobal
= FreqAry
[idx
];
1579 /* this can get noisy so don't log for now */
1581 "Temp %d {%d-%d}, set frequency %d",
1582 temp
, MinTemp
, MaxTemp
, CurPXGlobal
);
1585 set_global_freq(CurPXGlobal
);