View Single Post
  #1   (View Single Post)  
Old 9th November 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,056
Post HOWTO: FreeBSD CPU Scaling with cpufreq.ko

For those who do not know FreeBSD is able to scale CPU speed (both desktop and mobile onesm thy just nned to support it and have enabled it in BIOS).

To enable that feature you need to add this line to /etc/rc.conf:
Code:
powerd_enable="YES"
You can also tweak how much you CPU will scale depends on the load, for example:
Code:
powerd_flags="-i 85 -r 60 -p 100"
powerd by default use adaptive mode (thanks to BSDKaffee)

You can also tweak lowest CPU frequency used by CPU by setting this in /etc/sysctl.conf or /boot/loader.conf:
Code:
debug.cpufreq.lowest=600
You can also set it by hand in terminal using sysctl:
Code:
sysctl debug.cpufreq.lowest=600
Up to yesterday there was no option to set highest value to limit max CPU speed to save power or limit overheat, but Boris Kochergin wrote a patch to support also the highest limit with debug.cpufreq.highest oid:
Code:
sysctl debug.cpufreq.highest=1200
These patches are for 7.0-RELEASE and 7-STABLE (I did not checked 8-CURRENT but propably also works):

/usr/src/sys/kern/kern_cpu.c (driver):
Code:
--- kern_cpu.c.orig	2008-11-08 13:12:24.000000000 -0500
+++ kern_cpu.c	2008-11-08 10:33:18.000000000 -0500
@@ -131,12 +131,16 @@
 DRIVER_MODULE(cpufreq, cpu, cpufreq_driver, cpufreq_dc, 0, 0);
 
 static int		cf_lowest_freq;
+static int		cf_highest_freq;
 static int		cf_verbose;
 TUNABLE_INT("debug.cpufreq.lowest", &cf_lowest_freq);
+TUNABLE_INT("debug.cpufreq.highest", &cf_highest_freq);
 TUNABLE_INT("debug.cpufreq.verbose", &cf_verbose);
 SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL, "cpufreq debugging");
 SYSCTL_INT(_debug_cpufreq, OID_AUTO, lowest, CTLFLAG_RW, &cf_lowest_freq, 1,
     "Don't provide levels below this frequency.");
+SYSCTL_INT(_debug_cpufreq, OID_AUTO, highest, CTLFLAG_RW, &cf_highest_freq, 1,
+    "Don't provide levels above this frequency.");
 SYSCTL_INT(_debug_cpufreq, OID_AUTO, verbose, CTLFLAG_RW, &cf_verbose, 1,
     "Print verbose debugging messages");
 
@@ -295,6 +299,14 @@
 		goto out;
 	}
 
+	/* Reject levels that are above our specified threshold. */
+	if (cf_highest_freq > 0 && level->total_set.freq > cf_highest_freq) {
+		CF_DEBUG("rejecting freq %d, greater than %d limit\n",
+		    level->total_set.freq, cf_highest_freq);
+		error = EINVAL;
+		goto out;
+	}
+
 	/* If already at this level, just return. */
 	if (CPUFREQ_CMP(sc->curr_level.total_set.freq, level->total_set.freq)) {
 		CF_DEBUG("skipping freq %d, same as current level %d\n",
@@ -617,8 +629,13 @@
 			continue;
 		}
 
-		/* Skip levels that have a frequency that is too low. */
-		if (lev->total_set.freq < cf_lowest_freq) {
+		/*
+		 * Skip levels that have a frequency that is too low or too
+		 * high.
+		 */
+		if (lev->total_set.freq < cf_lowest_freq ||
+		    (cf_highest_freq > 0 &&
+		     lev->total_set.freq > cf_highest_freq)) {
 			sc->all_count--;
 			continue;
 		}
/usr/src/share/man/man4/cpufreq.4 (man page):
Code:
--- cpufreq.4.orig	2008-11-08 13:08:19.000000000 -0500
+++ cpufreq.4	2008-11-08 13:08:51.000000000 -0500
@@ -98,6 +98,11 @@
 This setting is also accessible via a tunable with the same name.
 This can be used to disable very low levels that may be unusable on
 some systems.
+.It Va debug.cpufreq.highest
+Highest CPU frequency in MHz to offer to users.
+This setting is also accessible via a tunable with the same name.
+This can be used to disable very high levels that may be unusable on
+some systems.
 .It Va debug.cpufreq.verbose
 Print verbose messages.
 This setting is also accessible via a tunable with the same name.
Apply them like that:
Code:
# cd /usr/src/share/man/man4
# patch < /path/to/cpufreq.4.patch
# 
# cd /usr/src/sys/kern
# patch < /path/to/kern_cpu.c
Then rebuild kernel and reboot to use it.

This /usr/src/share/man/man4/cpufreq.4 is just a manpage so its not mandatory to apply/rebuid it.

Abialable CPU frequencies are aviable via dev.cpu.0.freq_levels oid, example:
Code:
# sysctl dev.cpu.0.freq_levels 
dev.cpu.0.freq_levels: 1200/13000 1050/11375 900/9750 750/8125 600/6500
You can also set Cx sleep state for your CPUs with dev.cpu.1.cx_lowest and dev.cpu.0.cx_lowest and so per CPU.

You can change them that:
Code:
# sysctl dev.cpu.0.cx_lowest=C3
dev.cpu.1.cx_lowest: C1 -> C3
WARN: Dunno for other laptops but when I use lowest C3 step for all cores, then I have little lag when I use my touchpad, this can be easily eliminated when you set one of the CPUs to C2 and all other to C3 to save power, no lag with that settings.

List of supported states are avialable via these oids:
Code:
dev.cpu.0.cx_supported: C1/1 C2/1 C3/57
dev.cpu.1.cx_supported: C1/1 C2/1 C3/57
You can read more about Intel C power states here:
http://software.intel.com/en-us/blog...more-c-states/
http://www.techarp.com/showarticle.a...tno=420&pgno=6

I measured power consumption of my CPU which is Intel T7300 (in my Dell D630) under full load*[1], by a small device called wattmeter, it is connected like that:

Code:
power (in the wall) <--> wattmeter <--> laptop (without batteries)
Here are the results:
Code:
 MHz    system power consumption (whole laptop)
 150    22W
 300    22W
 450    23W
 600    23W
 750    24W
 900    25W
1050    26W
1200    27W
1400    33W
1750    42W
2000    47W
1200MHz seems to have best power/performance ratio and that is what I personally use.

[1] 999999999999999999999999999 ** 999999999999999999999999999; launched 4 times (to full load two cores) in python.

... and by the way, setting kern.hz=100 in /boot/loader.conf will also make your battery life little longer.

WARN: If these options differ for AMD CPUs, then let me know, or just pot them in this thread.

If you have any questions or I forgot about something then let me know
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote