DaemonForums  

Go Back   DaemonForums > Miscellaneous > Guides

Guides All Guides and HOWTO's.

Reply
 
Thread Tools Display Modes
  #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
  #2   (View Single Post)  
Old 9th November 2008
Oliver_H's Avatar
Oliver_H Oliver_H is offline
Real Name: Oliver Herold
UNIX lover
 
Join Date: May 2008
Location: Germany
Posts: 427
Default

>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

Yeah I've seen your request on the ml =)
__________________
use UNIX or die :-)
Reply With Quote
  #3   (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
Default

Quote:
Originally Posted by cabal View Post
Yeah I've seen your request on the ml =)
Heh I wonder if it will make it into RELEASE
__________________
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
  #4   (View Single Post)  
Old 9th November 2008
Oliver_H's Avatar
Oliver_H Oliver_H is offline
Real Name: Oliver Herold
UNIX lover
 
Join Date: May 2008
Location: Germany
Posts: 427
Default

I hope so it's a useful addon.
__________________
use UNIX or die :-)
Reply With Quote
  #5   (View Single Post)  
Old 10th November 2008
BSDKaffee's Avatar
BSDKaffee BSDKaffee is offline
Real Name: Jason Hale
Coffee Addict
 
Join Date: May 2008
Location: Wintersville, Ohio
Posts: 212
Default

I don't mean to sound too critical, but I think the multiple spelling errors detract from this otherwise interesting guide. I know other guides have the same problem, so I'm not just picking on vermaden here. In my opinion guides should be given special care from ordinary posts and be written with a minimally educated user in mind as they may take an error at face value.

A few things besides spelling (that are somewhat important):
Quote:
Originally Posted by vermaden View Post
Code:
power_enable="YES"
Should be:
Code:
powerd_enable="YES"
Quote:
Originally Posted by vermaden View Post
Code:
powerd_flags="-a adaptive -b adaptive -n adaptive -i 85 -r 60 -p 100"
powerd defaults to use adaptive mode, so the following line would be sufficient:
Code:
powerd_flags="-i 85 -r 60 -p 100"
Just out of curiosity, could you also explain why you think these are better values than the defaults? Especially the polling interval.
Quote:
Originally Posted by vermaden View Post
Code:
# sudo sysctl dev.cpu.0.cx_lowest=C3
dev.cpu.1.cx_lowest: C1 -> C3
Not everyone uses sudo, and since the prompt is already '#', I think that would be sufficient enough to explain that the command needs to be run as root.

Thanks...just trying to be constructive.
Reply With Quote
  #6   (View Single Post)  
Old 10th November 2008
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,056
Default

Quote:
Originally Posted by BSDKaffee View Post
I don't mean to sound too critical, but I think the multiple spelling errors detract from this otherwise interesting guide.
Thanks mate, I think I corrected them.

Quote:
Originally Posted by BSDKaffee View Post
Just out of curiosity, could you also explain why you think these are better values than the defaults? Especially the polling interval.
Uses lower frequencies, these values (as from manual) specifya at what load level to change current speed to lower or higher value, default values higher the clock at lower load and the same for lower clock. The interval specifies how often the clock speed will change, many processes lower down their CPU usage for short period of time, with 500ms (0.5 second) you will not be able to "catch" these chances to save power, with 100 (or even 50 but you use more CPU power for polling) you are able to higher frequency much faster, in both sides.

Quote:
Originally Posted by BSDKaffee View Post
Not everyone uses sudo, and since the prompt is already '#', I think that would be sufficient enough to explain that the command needs to be run as root.
And that was the purpose of #, I just forgot to remove sudo.

Quote:
Originally Posted by BSDKaffee View Post
Thanks...just trying to be constructive.
Thanks for corrections mate

I added power consumption of my CPU depends on MHz used.
__________________
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
  #7   (View Single Post)  
Old 5th January 2009
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,056
Default

Check this of you want to get your AMD Phenom/Barcelona/K10 CPU scaling:
https://forums.freebsd.org/showpost....9&postcount=12
__________________
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
  #8   (View Single Post)  
Old 20th March 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Tcpdump Spy
 
Join Date: Apr 2008
Location: Netherlands
Posts: 2,243
Default

Quote:
Originally Posted by vermaden
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
I've been thinking, and I wonder just how useful this is.

A 2GHz CPU running at 1GHz will not consume anywhere near half the power, while a CPU running at 2GHz will complete a task close to twice as fast.
The result is that the CPU will take more time to complete a task, and it will require more power in the end.

I haven't done any test, but I suspect that setting this value lower than the maximum will actually cause the battery to last shorter.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #9   (View Single Post)  
Old 24th September 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

CPU scaling is something I would love to take advantage of, especially as my battery gets older and older... but running powerd has a tendency to cause my laptop to hang!
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Old 27th October 2010
royce royce is offline
Real Name: Royce Williams
New User
 
Join Date: Oct 2010
Location: Anchorage, AK
Posts: 1
Default powerd(8) support for CPU scaling minimum and maximum

A breadcrumb for others finding this thread:

While Boris' original patch to add debug.cpufreq.highest sysctl support to kern_cpu.c was gently rejected (PR 144232, I'm too new to port URLs yet), he then filed a patch to add -M and -m to powerd(8) (PR 145063), which allows the user to set minimum and maximum clock speeds.

The patch was committed to 7-STABLE and 8-STABLE in September 2010, and should be part of 7.4 and 8.2.
Reply With Quote
Old 27th October 2010
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,056
Default

@royce

Yes, I have heard about these great news, but not about MFC into 8.x

Currently I always add these patches into GENERIC config and rebuild kernel with:
Code:
# make NO_MODULES=1 buildkernel
__________________
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
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FreeBSD GPT howto graudeejs Guides 10 21st December 2010 12:24 AM
HOWTO: QEMU on FreeBSD vermaden Guides 10 9th March 2009 07:10 PM
openbsd cpufreq jaideep_jdof OpenBSD General 6 18th November 2008 11:22 AM
HOWTO: FreeBSD with CCACHE vermaden Guides 10 9th July 2008 06:14 PM
cpu scaling chill FreeBSD General 2 3rd June 2008 06:18 PM


All times are GMT. The time now is 04:37 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick