# Simple but effective ondemand CPUfreq governor tweak

## trippels

Since October last year the kernel  (>=2.6.37)  has a new tunable for the 

ondemand CPUfreq governor called sampling_down_factor.

Quote from Documentation/cpu-freq/governors.txt:

 *Quote:*   

> sampling_down_factor: this parameter controls the rate at which the
> 
> kernel makes a decision on when to decrease the frequency while running
> 
> at top speed. When set to 1 (the default) decisions to reevaluate load
> ...

 

If you set for example:

```
echo 200 > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor
```

in your startup script, you'll get a nice speed boost especially during

the configuration phase of bigger projects.

The maximum value is 100000.

Here are the merge times for sys-libs/ncurses:

```
sampling_down_factor    merge time

1 (default)             1 minute and 59 seconds.

20                      1 minute and 47 seconds. 

100                     1 minute and 29 seconds.

150                     1 minute and 24 seconds.

200                     1 minute and 22 seconds.

300                     1 minute and 20 seconds.

500                     1 minute and 12 seconds.

1500                    1 minute and 7 seconds.

```

To check the effect simply run:

```
watch -n.1 'cat /proc/cpuinfo|grep MHz'
```

Last edited by trippels on Fri Jul 22, 2011 12:41 pm; edited 3 times in total

----------

## kimmie

Thanks very much for that! My laptop has trouble playing HD video unless it's locked at full speed... until now I've been doing that manually, I believe you've just fixed that for me.

----------

## albright

sampling_down_factor does not exist in my

/sys/devices/system/cpu/cpu0/cpufreq/ondemand

```

ls -l

total 0

-rw-r--r-- 1 root root 4096 May 11 09:45 ignore_nice_load

-rw-r--r-- 1 root root 4096 May 11 09:45 powersave_bias

-rw-r--r-- 1 root root 4096 May 11 09:45 sampling_rate

-r--r--r-- 1 root root 4096 May 11 09:45 sampling_rate_max

-r--r--r-- 1 root root 4096 May 11 09:45 sampling_rate_min

-rw-r--r-- 1 root root 4096 May 11 09:45 up_threshold

```

How do you enable this?

----------

## trippels

You need a 2.6.36 or newer kernel.

And the path is: /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor

----------

## theotherjoe

```
localhost ~ # uname -a

Linux bigbuzz 2.6.36-gentoo-r8 #2 SMP PREEMPT Sat Apr 30 18:15:05 CEST 2011 x86_64 AMD Athlon(tm) II X4 630 Processor AuthenticAMD GNU/Linux

localhost ~ # cat  /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor

cat: /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor: No such file or directory

```

so maybe it was introduced in 2.6.37...

----------

## trippels

 *theotherjoe wrote:*   

> 
> 
> ```
> localhost ~ # uname -a
> 
> ...

 

Hm, 

```
% git describe 3f78a9f7fcee0e9b44a15f39ac382664e301fad5

v2.6.35-7791-g3f78a9f

```

so it should be in 2.6.36...

Edit: Just double checked and you're right:  it was introduced in 2.6.37.

```
 % git describe --contains 3f78a9f7fcee0e9b44a15f39ac382664e301fad5 

v2.6.37-rc1~112^2~1
```

----------

## albright

sorry, I should have noted my kernel

```
Linux gandalf 2.6.38-gentoo-r4 #2 SMP PREEMPT Mon May 9 08:15:02 EDT 2011 x86_64 Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz GenuineIntel GNU/Linux

```

But anyway, I was looking in the wrong directory, as you noted.

Got it now, and thanks!>

----------

## Naib

 *Quote:*   

> 
> 
> cat /etc/conf.d/cpufrequtils 
> 
> # /etc/conf.d/cpufrequtils: config file for /etc/init.d/cpufrequtils
> ...

 

a more elegant solution, then just add cpufrequtils to your runtime via rc-update

----------

## albright

I tried this for a while now and -- for me at least -- there is a big

downside.

HD video plays extremely "choppy" with sound break-ups.

I'm not sure why this is happening, but when sampling_down_factor is

reset to 1, my videos play smooth.

(note this is on a notebook with 1.2ghz core 2 duo that downclocks to

800 mhz)

----------

## toralf

Do you compared the merge times of the 2nd and 3rd re-emerge of the same package ? Because emerging a package only 2 times in a row give false results due to file and/or compiler cache effects.

----------

## trippels

 *toralf wrote:*   

> Do you compared the merge times of the 2nd and 3rd re-emerge of the same package ? Because emerging a package only 2 times in a row give false results due to file and/or compiler cache effects.

 

Yes, I've tested this more extensively a few weeks ago these were the results:

(ncurses merge times)

```

sampling_down_factor    merge time

1                       1 minute and 59 seconds.

20                      1 minute and 47 seconds.  

100                     1 minute and 29 seconds. 

150                     1 minute and 24 seconds.

200                     1 minute and 22 seconds.

300                     1 minute and 20 seconds.

500                     1 minute and 12 seconds.

1500                    1 minute and 7 seconds.
```

----------

## trippels

 *albright wrote:*   

> I tried this for a while now and -- for me at least -- there is a big
> 
> downside.
> 
> HD video plays extremely "choppy" with sound break-ups.
> ...

 

What kernel version are you running? 

Because there was an important bug-fix in 2.6.39. Before the fix

the CPU frequency could accidentally be stuck at a low frequency 

for a long time (depending on the sampling_down_factor).

(see commit 5cb2c3bd in the kernel git tree.)

That could explain your symptoms.

----------

## albright

my kernel is:

2.6.39-pf4 #1 SMP PREEMPT

----------

## trippels

 *albright wrote:*   

> my kernel is:
> 
> 2.6.39-pf4 #1 SMP PREEMPT

 

Ah, ck messes with cpufreq.c.

Please try a vanilla kernel and see if your issue vanishes.

----------

## tclover

Hey, thanks trippels for this thread! I was having quite an issue recently with my desktop cpu being stucked at the lowest p-state while my laptop was stucked on the highest. I had and still have powersave as the default governor--I'll go for ondemand next time I build my kernels. With this trick and ondemand governor my issue vanish. I was looking for a solution only to find nothing. And anyway, I use kernel--2.6.3{8{-pf8,.8-zen},9-pf3}--with zcache (and cleancache) pach for .38*. I started, at first, suspecting zcache patch but it wasn't the culpirit. The weird thing is 38-pf8 without zcache worked great a few weeks before, the same goes for .39-pf*. As there were a few issues with BFS and such in .39, I decided to stick with .38! Actually, I have a few power profiles with hprofile but I did not bother until now to put a power profile in the init service. Now it's done! I won't bother changing it with on demand governor or change my powersave profile with ondemand governor.

----------

